2 * Copyright (c) 2003-2004, Yannick Verschueren
3 * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
38 #define JPIP_JPIP 0x6a706970
40 #define JP2_JP 0x6a502020
41 #define JP2_FTYP 0x66747970
42 #define JP2_JP2H 0x6a703268
43 #define JP2_IHDR 0x69686472
44 #define JP2_COLR 0x636f6c72
45 #define JP2_JP2C 0x6a703263
46 #define JP2_URL 0x75726c20
47 #define JP2_DBTL 0x6474626c
48 #define JP2_BPCC 0x62706363
49 #define JP2_JP2 0x6a703220
57 int jp2_read_boxhdr(jp2_box_t * box)
59 box->init_pos = cio_tell();
60 box->length = cio_read(4);
61 box->type = cio_read(4);
62 if (box->length == 1) {
63 if (cio_read(4) != 0) {
64 fprintf(stderr, "Error: Cannot handle box sizes higher than 2^32\n");
67 box->length = cio_read(4);
69 box->length = cio_numbytesleft() + 12;
71 else if (box->length == 0) {
72 box->length = cio_numbytesleft() + 8;
79 * Initialisation of a Standard JP2 structure
82 int jp2_init_stdjp2(jp2_struct_t * jp2_struct, j2k_image_t * img)
84 int depth_0, sign, depth, i;
86 jp2_struct->h = img->y1 - img->y0; // HEIGHT
87 jp2_struct->w = img->x1 - img->x0; // WIDTH
88 jp2_struct->numcomps = img->numcomps; // NC
89 jp2_struct->comps = (jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(jp2_comps_t));
91 depth_0 = img->comps[0].prec - 1;
92 sign = img->comps[0].sgnd;
93 jp2_struct->bpc = depth_0 + (sign << 7);
95 for (i = 1; i < img->numcomps; i++) {
96 depth = img->comps[i].prec - 1;
97 sign = img->comps[i].sgnd;
99 jp2_struct->bpc = 255;
104 jp2_struct->C = 7; // C : Always 7
105 jp2_struct->UnkC = 0; // UnkC, colorspace specified in colr box
106 jp2_struct->IPR = 0; // IPR, no intellectual property
108 for (i = 0; i < img->numcomps; i++)
109 jp2_struct->comps[i].bpcc =
110 img->comps[i].prec - 1 + (img->comps[i].sgnd << 7);
112 jp2_struct->precedence = 0; // PRECEDENCE
113 jp2_struct->approx = 0; // APPROX
115 if ((img->numcomps == 1 || img->numcomps == 3)
116 && (jp2_struct->bpc != 255))
117 jp2_struct->meth = 1;
119 jp2_struct->meth = 2;
121 if (jp2_struct->meth == 1) {
122 if (img->color_space == 1)
123 jp2_struct->enumcs = 16;
124 else if (img->color_space == 2)
125 jp2_struct->enumcs = 17;
126 else if (img->color_space == 3)
127 jp2_struct->enumcs = 18; // YUV
129 jp2_struct->enumcs = 0; // PROFILE (??)
131 jp2_struct->brand = JP2_JP2; /* BR */
132 jp2_struct->minversion = 0; /* MinV */
133 jp2_struct->numcl = 1;
134 jp2_struct->cl = (int *) malloc(jp2_struct->numcl * sizeof(int));
135 jp2_struct->cl[0] = JP2_JP2; /* CL0 : JP2 */
140 void jp2_write_url(char *Idx_file)
146 sprintf(str, "%s", Idx_file);
149 box.init_pos = cio_tell();
151 cio_write(JP2_URL, 4); // DBTL
152 cio_write(0, 1); // VERS
153 cio_write(0, 3); // FLAG
155 for (i = 0; i < strlen(str); i++) {
156 cio_write(str[i], 1);
159 box.length = cio_tell() - box.init_pos;
160 cio_seek(box.init_pos);
161 cio_write(box.length, 4); /* L */
162 cio_seek(box.init_pos + box.length);
171 int jp2_read_ihdr(jp2_struct_t * jp2_struct)
175 jp2_read_boxhdr(&box);
176 if (JP2_IHDR != box.type) {
177 fprintf(stderr, "Error: Expected IHDR Marker\n");
181 jp2_struct->h = cio_read(4); // HEIGHT
182 jp2_struct->w = cio_read(4); // WIDTH
183 jp2_struct->numcomps = cio_read(2); // NC
185 jp2_struct->bpc = cio_read(1); // BPC
187 jp2_struct->C = cio_read(1); // C
188 jp2_struct->UnkC = cio_read(1); // UnkC
189 jp2_struct->IPR = cio_read(1); // IPR
191 if (cio_tell() - box.init_pos != box.length) {
192 fprintf(stderr, "Error with IHDR Box\n");
198 void jp2_write_ihdr(jp2_struct_t * jp2_struct)
202 box.init_pos = cio_tell();
204 cio_write(JP2_IHDR, 4); // IHDR
206 cio_write(jp2_struct->h, 4); // HEIGHT
207 cio_write(jp2_struct->w, 4); // WIDTH
208 cio_write(jp2_struct->numcomps, 2); // NC
210 cio_write(jp2_struct->bpc, 1); // BPC
212 cio_write(jp2_struct->C, 1); // C : Always 7
213 cio_write(jp2_struct->UnkC, 1); // UnkC, colorspace unknow
214 cio_write(jp2_struct->IPR, 1); // IPR, no intellectual property
216 box.length = cio_tell() - box.init_pos;
217 cio_seek(box.init_pos);
218 cio_write(box.length, 4); /* L */
219 cio_seek(box.init_pos + box.length);
223 void jp2_write_bpcc(jp2_struct_t * jp2_struct)
228 box.init_pos = cio_tell();
230 cio_write(JP2_BPCC, 4); // BPCC
232 for (i = 0; i < jp2_struct->numcomps; i++)
233 cio_write(jp2_struct->comps[i].bpcc, 1);
235 box.length = cio_tell() - box.init_pos;
236 cio_seek(box.init_pos);
237 cio_write(box.length, 4); /* L */
238 cio_seek(box.init_pos + box.length);
242 int jp2_read_bpcc(jp2_struct_t * jp2_struct)
247 jp2_read_boxhdr(&box);
248 if (JP2_BPCC != box.type) {
249 fprintf(stderr, "Error: Expected BPCC Marker\n");
253 for (i = 0; i < jp2_struct->numcomps; i++)
254 jp2_struct->comps[i].bpcc = cio_read(1);
256 if (cio_tell() - box.init_pos != box.length) {
257 fprintf(stderr, "Error with BPCC Box\n");
263 void jp2_write_colr(jp2_struct_t * jp2_struct)
267 box.init_pos = cio_tell();
269 cio_write(JP2_COLR, 4); // COLR
271 cio_write(jp2_struct->meth, 1); // METH
272 cio_write(jp2_struct->precedence, 1); // PRECEDENCE
273 cio_write(jp2_struct->approx, 1); // APPROX
275 if (jp2_struct->meth == 1)
276 cio_write(jp2_struct->enumcs, 4); // EnumCS
278 cio_write(0, 1); // PROFILE (??)
280 box.length = cio_tell() - box.init_pos;
281 cio_seek(box.init_pos);
282 cio_write(box.length, 4); /* L */
283 cio_seek(box.init_pos + box.length);
286 int jp2_read_colr(jp2_struct_t * jp2_struct)
290 jp2_read_boxhdr(&box);
291 if (JP2_COLR != box.type) {
292 fprintf(stderr, "Error: Expected COLR Marker\n");
296 jp2_struct->meth = cio_read(1); // METH
297 jp2_struct->precedence = cio_read(1); // PRECEDENCE
298 jp2_struct->approx = cio_read(1); // APPROX
300 if (jp2_struct->meth == 1)
301 jp2_struct->enumcs = cio_read(4); // EnumCS
303 cio_read(1); // PROFILE
305 if (cio_tell() - box.init_pos != box.length) {
306 fprintf(stderr, "Error with BPCC Box\n");
318 void jp2_write_jp2h(jp2_struct_t * jp2_struct)
322 box.init_pos = cio_tell();
324 cio_write(JP2_JP2H, 4); /* JP2H */
326 jp2_write_ihdr(jp2_struct);
328 if (jp2_struct->bpc == 255)
329 jp2_write_bpcc(jp2_struct);
330 jp2_write_colr(jp2_struct);
332 box.length = cio_tell() - box.init_pos;
333 cio_seek(box.init_pos);
334 cio_write(box.length, 4); /* L */
335 cio_seek(box.init_pos + box.length);
345 int jp2_read_jp2h(jp2_struct_t * jp2_struct)
349 jp2_read_boxhdr(&box);
350 if (JP2_JP2H != box.type) {
351 fprintf(stderr, "Error: Expected JP2H Marker\n");
355 if (jp2_read_ihdr(jp2_struct))
358 if (jp2_struct->bpc == 255)
359 if (jp2_read_bpcc(jp2_struct))
361 if (jp2_read_colr(jp2_struct))
364 if (cio_tell() - box.init_pos != box.length) {
365 fprintf(stderr, "Error with JP2H Box\n");
377 void jp2_write_ftyp(jp2_struct_t * jp2_struct)
382 box.init_pos = cio_tell();
384 cio_write(JP2_FTYP, 4); /* FTYP */
386 cio_write(jp2_struct->brand, 4); /* BR */
387 cio_write(jp2_struct->minversion, 4); /* MinV */
389 for (i = 0; i < jp2_struct->numcl; i++)
390 cio_write(jp2_struct->cl[i], 4); /* CL */
392 box.length = cio_tell() - box.init_pos;
393 cio_seek(box.init_pos);
394 cio_write(box.length, 4); /* L */
395 cio_seek(box.init_pos + box.length);
404 int jp2_read_ftyp(jp2_struct_t * jp2_struct)
409 jp2_read_boxhdr(&box);
411 if (JP2_FTYP != box.type) {
412 fprintf(stderr, "Error: Excpected FTYP Marker\n");
416 jp2_struct->brand = cio_read(4); /* BR */
417 jp2_struct->minversion = cio_read(4); /* MinV */
418 jp2_struct->numcl = (box.length - 16) / 4;
420 (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
422 for (i = 0; i < (int)jp2_struct->numcl; i++)
423 jp2_struct->cl[i] = cio_read(4); /* CLi */
425 if (cio_tell() - box.init_pos != box.length) {
426 fprintf(stderr, "Error with FTYP Box\n");
432 int jp2_write_jp2c(jp2_struct_t * jp2_struct, char *j2k_codestream)
436 box.init_pos = cio_tell();
438 cio_write(JP2_JP2C, 4); // JP2C
440 jp2_struct->j2k_codestream_offset = cio_tell();
441 memcpy(cio_getbp(),j2k_codestream, jp2_struct->j2k_codestream_len);
443 box.length = 8 + jp2_struct->j2k_codestream_len;
444 cio_seek(box.init_pos);
445 cio_write(box.length, 4); /* L */
446 cio_seek(box.init_pos + box.length);
452 int jp2_read_jp2c(unsigned char *src, jp2_struct_t * jp2_struct)
456 jp2_read_boxhdr(&box);
457 if (JP2_JP2C != box.type) {
458 fprintf(stderr, "Error: Expected JP2C Marker\n");
462 jp2_struct->j2k_codestream_offset = cio_tell();
463 jp2_struct->j2k_codestream_len = box.length - 8;
472 box.init_pos = cio_tell();
474 cio_write(JP2_JP, 4); // JP
475 cio_write(0x0d0a870a, 4);
477 box.length = cio_tell() - box.init_pos;
478 cio_seek(box.init_pos);
479 cio_write(box.length, 4); /* L */
480 cio_seek(box.init_pos + box.length);
486 * JPEG 2000 signature
488 * return 1 if error else 0
494 jp2_read_boxhdr(&box);
495 if (JP2_JP != box.type) {
496 fprintf(stderr, "Error: Expected JP Marker\n");
499 if (0x0d0a870a != cio_read(4)) {
500 fprintf(stderr, "Error with JP Marker\n");
503 if (cio_tell() - box.init_pos != box.length) {
504 fprintf(stderr, "Error with JP Box size\n");
512 int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len)
518 if (jp2_read_ftyp(jp2_struct))
520 if (jp2_read_jp2h(jp2_struct))
522 if (jp2_read_jp2c(src, jp2_struct))
527 int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream, char *output)
530 jp2_write_ftyp(jp2_struct);
531 jp2_write_jp2h(jp2_struct);
532 jp2_write_jp2c(jp2_struct, j2k_codestream);