JunkBox_Lib++ (for Windows) 1.10.1
Loading...
Searching...
No Matches
Jpeg2KTool.cpp
Go to the documentation of this file.
1
17#include "Jpeg2KTool.h"
18
19
20#ifdef ENABLE_OPENJPEG
21
22#if OPENJPEG_VER <= JP2K_VER_15
23
24using namespace jbxl;
25
26
27void JPEG2KImage::init(void)
28{
29 xs = 0;
30 ys = 0;
31 ws = 0;
32 hs = 0;
33 col = 0;
34 cmode = GRAPH_COLOR_RGBA;
35 state = 0;
36 image = NULL;
37}
38
39
40bool JPEG2KImage::isNull(void)
41{
42 if (image!=NULL && image->comps!=NULL) return false;
43 return true;
44}
45
46
47void JPEG2KImage::free(void)
48{
49 if (image!=NULL) {
50 opj_image_destroy(image);
51 }
52
53 init();
54}
55
56
57void JPEG2KImage::fill(int v)
58{
59 if (!isNull()) {
60 for (int k=0; k<col; k++) {
61 if (image->comps[k].data!=NULL) {
62 for (int i=0; i<xs*ys; i++) {
63 image->comps[k].data[i] = v;
64 }
65 }
66 }
67 }
68}
69
70
71void JPEG2KImage::setup_image(void)
72{
73 if (!isNull()) {
74 //
75 xs = image->x1 - image->x0;
76 ys = image->y1 - image->y0;
77
78 int fac = (int)image->comps->factor;
79 ws = (xs + (1<<fac) -1)>>fac;
80 hs = (ys + (1<<fac) -1)>>fac;
81
82 col = (int)image->numcomps;
83 /*
84 if (image->color_space==OPJ_CLRSPC_SRGB) { /// 1
85 col = 3;
86 }
87 else if (image->color_space==OPJ_CLRSPC_GRAY) { /// 2
88 col = 1;
89 }
90 else if (image->color_space==OPJ_CLRSPC_SYCC || image->color_space==OPJ_CLRSPC_EYCC) { /// 3, 4
91 col = 3;
92 }
93 else if (image->color_space==OPJ_CLRSPC_CMYK) { /// 5
94 col = 4;
95 }
96 */
97
98 // 設定されないものについては,未対応
99 cmode = GRAPH_COLOR_UNKNOWN;
100 int depth = (int)image->comps->bpp;
101
102 DEBUG_MODE PRINT_MESG("JBXL::JPEG2KIMage::setup_image: INFO: xs = %d, ys = %d, col = %d, depth = %d\n", xs, ys, col, depth);
103 if (depth==0 || depth==8) {
104 if (col==1) cmode = GRAPH_COLOR_GRAY;
105 else if (col==3) cmode = GRAPH_COLOR_RGB;
106 else if (col==4) cmode = GRAPH_COLOR_RGBA;
107 }
108 if (cmode==GRAPH_COLOR_UNKNOWN) {
109 PRINT_MESG("JBXL::JPEG2KIMage::setup_image: unknown color mode: col = %d, depth = %d\n", col, depth);
110 cmode = GRAPH_COLOR_RGBA;
111 }
112
113 //
114#if OPENJPEG_VER > JP2K_VER_12
115 freeNull(image->icc_profile_buf);
116 image->icc_profile_len = 0;
117#endif
118
119/*
120 DEBUG_MODE {
121 PRINT_MESG("JPEG2KImage::setup_image: OFFSET %d %d %d %d\n", image->x0, image->y0, image->x1, image->y1);
122 PRINT_MESG("JPEG2KImage::setup_image: NUMCOMP %d\n", image->numcomps);
123 PRINT_MESG("JPEG2KImage::setup_image: COLORSP %d\n", image->color_space);
124 PRINT_MESG("JPEG2KImage::setup_image: XS YS %d %d\n", xs, ys);
125 PRINT_MESG("JPEG2KImage::setup_image: COLOR %d\n", col);
126 PRINT_MESG("JPEG2KImage::setup_image: CMODE %d\n", cmode);
127 PRINT_MESG("\n");
128 for (int i=0; i<(int)image->numcomps; i++) {
129 PRINT_MESG("JPEG2KImage::setup_image: DX DY %d %d\n", image->comps[i].dx, image->comps[i].dy);
130 PRINT_MESG("JPEG2KImage::setup_image: W H %d %d\n", image->comps[i].w, image->comps[i].h);
131 PRINT_MESG("JPEG2KImage::setup_image: X0 Y0 %d %d\n", image->comps[i].x0, image->comps[i].y0);
132 PRINT_MESG("JPEG2KImage::setup_image: PREC %d\n", image->comps[i].prec);
133 PRINT_MESG("JPEG2KImage::setup_image: DEPTH %d\n", image->comps[i].bpp);
134 PRINT_MESG("JPEG2KImage::setup_image: SGND %d\n", image->comps[i].sgnd);
135 PRINT_MESG("JPEG2KImage::setup_image: RESNO %d\n", image->comps[i].resno_decoded);
136 PRINT_MESG("JPEG2KImage::setup_image: FACT %d\n", image->comps[i].factor);
137 PRINT_MESG("\n");
138 }
139 }
140*/
141 }
142}
143
144
145
147
148JPEG2KImage jbxl::readJPEG2KFile(const char* fname)
149{
150 JPEG2KImage jp;
151 //
152 if (fname==NULL) {
153 jp.state = JBXL_GRAPH_IVDARG_ERROR;
154 return jp;
155 }
156
157 int len = (int)file_size(fname);
158
159 FILE* fp = fopen(fname, "rb");
160 if (fp==NULL) {
161 jp.state = JBXL_GRAPH_OPFILE_ERROR;
162 return jp;
163 }
164
165 unsigned char* data = (unsigned char*)malloc(len);
166 if (data==NULL) {
167 fclose(fp);
168 jp.state = JBXL_GRAPH_MEMORY_ERROR;
169 return jp;
170 }
171 memset(data, 0, len);
172
173 Buffer buf = read_Buffer_data(fp, 12);
174 if (buf.vldsz<12) {
175 free_Buffer(&buf);
176 jp.state = JBXL_GRAPH_FILESZ_ERROR;
177 fclose(fp);
178 return jp;
179 }
180
181 fseek(fp, 0, 0);
182 int format = isJPEG2KHeader(buf);
183 if (format==JP2K_FMT_NONE) format = JP2K_FMT_JPT;
184 free_Buffer(&buf);
185
186 fread(data, len, 1, fp);
187 fclose(fp);
188
189 jp = readJPEG2KData(data, len, format);
190 ::free(data);
191
192 return jp;
193}
194
195
196JPEG2KImage jbxl::readJPEG2KData(unsigned char* data, int len, int format)
197{
198 JPEG2KImage jp;
199
200 opj_dinfo_t* codec = NULL;
201 opj_cio_t* stream = NULL;
202
203 opj_dparameters_t parameters;
204 opj_set_default_decoder_parameters(&parameters);
205
206 if (format==JP2K_FMT_J2K) { // JPEG 2000 codestream
207 codec = opj_create_decompress(CODEC_J2K);
208 }
209 else if (format==JP2K_FMT_JP2) { // JPEG 2000 compressed image data
210 codec = opj_create_decompress(CODEC_JP2);
211 }
212 else if (format==JP2K_FMT_JPT) { // JPEG 2000 JPIP
213 codec = opj_create_decompress(CODEC_JPT);
214 }
215 else {
216 PRINT_MESG("JBXL::readJPEG2KData: ERROR: unknown file format!\n");
217 jp.state = JBXL_GRAPH_NODATA_ERROR;
218 return jp;
219 }
220
221 opj_setup_decoder(codec, &parameters);
222 stream = opj_cio_open((opj_common_ptr)codec, data, len);
223 jp.image = opj_decode(codec, stream);
224 if (jp.image==NULL) {
225 opj_cio_close(stream);
226 opj_destroy_decompress(codec);
227 jp.state = JBXL_GRAPH_IVDDATA_ERROR;
228 return jp;
229 }
230
231 jp.setup_image();
232
233 opj_cio_close(stream);
234 opj_destroy_decompress(codec);
235
236 return jp;
237}
238
239
240#include "Jpeg2KToolCommon.cpp"
241
242
243#endif // OPENJPEG_VER
244#endif // ENABLE_OPENJPEG
245
JPEG 2000グラフィックデータ定義用ヘッダ
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Definition buffer.cpp:128
Buffer read_Buffer_data(FILE *fp, int sz)
ファイルポインタ fp から szバイトをBuffer型変数に読み込む.
Definition buffer.cpp:1497
#define GRAPH_COLOR_UNKNOWN
0x0070
Definition gheader.h:302
#define GRAPH_COLOR_RGBA
Definition gheader.h:283
#define GRAPH_COLOR_RGB
Definition gheader.h:270
#define GRAPH_COLOR_GRAY
Definition gheader.h:244
#define JBXL_GRAPH_IVDARG_ERROR
無効な引数
Definition jbxl_state.h:178
#define JBXL_GRAPH_NODATA_ERROR
データが無い
Definition jbxl_state.h:171
#define JBXL_GRAPH_OPFILE_ERROR
ファイルのオープンエラー
Definition jbxl_state.h:173
#define JBXL_GRAPH_FILESZ_ERROR
ファイルサイズのエラー
Definition jbxl_state.h:176
#define JBXL_GRAPH_MEMORY_ERROR
メモリエラー
Definition jbxl_state.h:170
#define JBXL_GRAPH_IVDDATA_ERROR
無効なデータ
Definition jbxl_state.h:181
Definition Brep.h:29
void freeNull(T &p)
Definition common++.h:37
int vldsz
データの長さ.バイナリデータの場合も使用可能.文字列の場合は 0x00 を含まない.
Definition buffer.h:37
unsigned long int file_size(const char *fn)
ファイルの大きさを返す.
Definition tools.cpp:2309
int isNull(void *p)
Definition tools.cpp:51
#define PRINT_MESG(...)
環境依存用の出力関数.MS Windows用は未実装
Definition tools.h:469
#define DEBUG_MODE
Definition tools.h:502