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.
36 #define JPIP_JPIP 0x6a706970
38 #define JP2_JP 0x6a502020
39 #define JP2_FTYP 0x66747970
40 #define JP2_JP2H 0x6a703268
41 #define JP2_IHDR 0x69686472
42 #define JP2_COLR 0x636f6c72
43 #define JP2_JP2C 0x6a703263
44 #define JP2_URL 0x75726c20
45 #define JP2_DBTL 0x6474626c
46 #define JP2_BPCC 0x62706363
47 #define JP2_JP2 0x6a703220
55 int jp2_read_boxhdr(jp2_box_t * box)
57 box->init_pos = cio_tell();
58 box->length = cio_read(4);
59 box->type = cio_read(4);
60 if (box->length == 1) {
61 if (cio_read(4) != 0) {
62 fprintf(stderr, "Error: Cannot handle box sizes higher than 2^32\n");
65 box->length = cio_read(4);
67 box->length = cio_numbytesleft() + 12;
68 } else if (box->length == 0) {
69 box->length = cio_numbytesleft() + 8;
76 * Initialisation of a Standard JP2 structure
79 int jp2_init_stdjp2(jp2_struct_t * jp2_struct)
83 (jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(jp2_comps_t));
85 jp2_struct->precedence = 0; /* PRECEDENCE*/
86 jp2_struct->approx = 0; /* APPROX*/
88 jp2_struct->brand = JP2_JP2; /* BR */
89 jp2_struct->minversion = 0; /* MinV */
90 jp2_struct->numcl = 1;
91 jp2_struct->cl = (unsigned int *) malloc(jp2_struct->numcl * sizeof(int));
92 jp2_struct->cl[0] = JP2_JP2; /* CL0 : JP2 */
94 jp2_struct->C = 7; /* C : Always 7*/
95 jp2_struct->UnkC = 0; /* UnkC, colorspace specified in colr box*/
96 jp2_struct->IPR = 0; /* IPR, no intellectual property*/
102 void jp2_write_url(char *Idx_file)
108 sprintf(str, "%s", Idx_file);
111 box.init_pos = cio_tell();
113 cio_write(JP2_URL, 4); /* DBTL*/
114 cio_write(0, 1); /* VERS*/
115 cio_write(0, 3); /* FLAG*/
117 for (i = 0; i < strlen(str); i++) {
118 cio_write(str[i], 1);
121 box.length = cio_tell() - box.init_pos;
122 cio_seek(box.init_pos);
123 cio_write(box.length, 4); /* L */
124 cio_seek(box.init_pos + box.length);
133 int jp2_read_ihdr(jp2_struct_t * jp2_struct)
137 jp2_read_boxhdr(&box);
138 if (JP2_IHDR != box.type) {
139 fprintf(stderr, "Error: Expected IHDR Marker\n");
143 jp2_struct->h = cio_read(4); /* HEIGHT*/
144 jp2_struct->w = cio_read(4); /* WIDTH*/
145 jp2_struct->numcomps = cio_read(2); /* NC*/
147 jp2_struct->bpc = cio_read(1); /* BPC*/
149 jp2_struct->C = cio_read(1); /* C */
150 jp2_struct->UnkC = cio_read(1); /* UnkC*/
151 jp2_struct->IPR = cio_read(1); /* IPR*/
153 if (cio_tell() - box.init_pos != box.length) {
154 fprintf(stderr, "Error with IHDR Box\n");
160 void jp2_write_ihdr(jp2_struct_t * jp2_struct)
164 box.init_pos = cio_tell();
166 cio_write(JP2_IHDR, 4); /* IHDR*/
168 cio_write(jp2_struct->h, 4); /* HEIGHT*/
169 cio_write(jp2_struct->w, 4); /* WIDTH*/
170 cio_write(jp2_struct->numcomps, 2); /* NC*/
172 cio_write(jp2_struct->bpc, 1); /* BPC */
174 cio_write(jp2_struct->C, 1); /* C : Always 7*/
175 cio_write(jp2_struct->UnkC, 1); /* UnkC, colorspace unknow*/
176 cio_write(jp2_struct->IPR, 1); /* IPR, no intellectual property*/
178 box.length = cio_tell() - box.init_pos;
179 cio_seek(box.init_pos);
180 cio_write(box.length, 4); /* L */
181 cio_seek(box.init_pos + box.length);
185 void jp2_write_bpcc(jp2_struct_t * jp2_struct)
190 box.init_pos = cio_tell();
192 cio_write(JP2_BPCC, 4); /* BPCC*/
194 for (i = 0; i < jp2_struct->numcomps; i++)
195 cio_write(jp2_struct->comps[i].bpcc, 1);
197 box.length = cio_tell() - box.init_pos;
198 cio_seek(box.init_pos);
199 cio_write(box.length, 4); /* L */
200 cio_seek(box.init_pos + box.length);
204 int jp2_read_bpcc(jp2_struct_t * jp2_struct)
209 jp2_read_boxhdr(&box);
210 if (JP2_BPCC != box.type) {
211 fprintf(stderr, "Error: Expected BPCC Marker\n");
215 for (i = 0; i < jp2_struct->numcomps; i++)
216 jp2_struct->comps[i].bpcc = cio_read(1);
218 if (cio_tell() - box.init_pos != box.length) {
219 fprintf(stderr, "Error with BPCC Box\n");
225 void jp2_write_colr(jp2_struct_t * jp2_struct)
229 box.init_pos = cio_tell();
231 cio_write(JP2_COLR, 4); /* COLR*/
233 cio_write(jp2_struct->meth, 1); /* METH*/
234 cio_write(jp2_struct->precedence, 1); /* PRECEDENCE*/
235 cio_write(jp2_struct->approx, 1); /* APPROX*/
237 if (jp2_struct->meth == 1)
238 cio_write(jp2_struct->enumcs, 4); /* EnumCS*/
240 cio_write(0, 1); /* PROFILE (??)*/
242 box.length = cio_tell() - box.init_pos;
243 cio_seek(box.init_pos);
244 cio_write(box.length, 4); /* L */
245 cio_seek(box.init_pos + box.length);
248 int jp2_read_colr(jp2_struct_t * jp2_struct)
253 jp2_read_boxhdr(&box);
255 if (JP2_COLR != box.type) {
256 cio_skip(box.length - 8);
257 jp2_read_boxhdr(&box);
259 } while (JP2_COLR != box.type);
261 jp2_struct->meth = cio_read(1); /* METH*/
262 jp2_struct->precedence = cio_read(1); /* PRECEDENCE*/
263 jp2_struct->approx = cio_read(1); /* APPROX*/
265 if (jp2_struct->meth == 1)
266 jp2_struct->enumcs = cio_read(4); /* EnumCS*/
269 skip_len = box.init_pos + box.length - cio_tell();
271 fprintf(stderr, "Error with JP2H box size\n");
274 cio_skip(box.init_pos + box.length - cio_tell());
277 if (cio_tell() - box.init_pos != box.length) {
278 fprintf(stderr, "Error with BPCC Box\n");
290 void jp2_write_jp2h(jp2_struct_t * jp2_struct)
294 box.init_pos = cio_tell();
296 cio_write(JP2_JP2H, 4); /* JP2H */
298 jp2_write_ihdr(jp2_struct);
300 if (jp2_struct->bpc == 255)
301 jp2_write_bpcc(jp2_struct);
302 jp2_write_colr(jp2_struct);
304 box.length = cio_tell() - box.init_pos;
305 cio_seek(box.init_pos);
306 cio_write(box.length, 4); /* L */
307 cio_seek(box.init_pos + box.length);
317 int jp2_read_jp2h(jp2_struct_t * jp2_struct)
322 jp2_read_boxhdr(&box);
324 if (JP2_JP2H != box.type) {
325 if (box.type == JP2_JP2C) {
326 fprintf(stderr, "Error: Expected JP2H Marker\n");
329 cio_skip(box.length - 8);
330 jp2_read_boxhdr(&box);
332 } while (JP2_JP2H != box.type);
334 if (jp2_read_ihdr(jp2_struct))
337 if (jp2_struct->bpc == 255) {
338 if (jp2_read_bpcc(jp2_struct))
342 if (jp2_read_colr(jp2_struct))
345 skip_len = box.init_pos + box.length - cio_tell();
347 fprintf(stderr, "Error with JP2H box size\n");
350 cio_skip(box.init_pos + box.length - cio_tell());
361 void jp2_write_ftyp(jp2_struct_t * jp2_struct)
366 box.init_pos = cio_tell();
368 cio_write(JP2_FTYP, 4); /* FTYP */
370 cio_write(jp2_struct->brand, 4); /* BR */
371 cio_write(jp2_struct->minversion, 4); /* MinV */
373 for (i = 0; i < jp2_struct->numcl; i++)
374 cio_write(jp2_struct->cl[i], 4); /* CL */
376 box.length = cio_tell() - box.init_pos;
377 cio_seek(box.init_pos);
378 cio_write(box.length, 4); /* L */
379 cio_seek(box.init_pos + box.length);
388 int jp2_read_ftyp(jp2_struct_t * jp2_struct)
393 jp2_read_boxhdr(&box);
395 if (JP2_FTYP != box.type) {
396 fprintf(stderr, "Error: Excpected FTYP Marker\n");
400 jp2_struct->brand = cio_read(4); /* BR */
401 jp2_struct->minversion = cio_read(4); /* MinV */
402 jp2_struct->numcl = (box.length - 16) / 4;
404 (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
406 for (i = 0; i < (int) jp2_struct->numcl; i++)
407 jp2_struct->cl[i] = cio_read(4); /* CLi */
409 if (cio_tell() - box.init_pos != box.length) {
410 fprintf(stderr, "Error with FTYP Box\n");
416 int jp2_write_jp2c(int j2k_codestream_len, int *j2k_codestream_offset,
417 char *j2k_codestream)
421 box.init_pos = cio_tell();
423 cio_write(JP2_JP2C, 4); /* JP2C*/
425 *j2k_codestream_offset = cio_tell();
426 memcpy(cio_getbp(), j2k_codestream, j2k_codestream_len);
428 box.length = 8 + j2k_codestream_len;
429 cio_seek(box.init_pos);
430 cio_write(box.length, 4); /* L */
431 cio_seek(box.init_pos + box.length);
437 int jp2_read_jp2c(unsigned int *j2k_codestream_len,
438 unsigned int *j2k_codestream_offset)
442 jp2_read_boxhdr(&box);
444 if (JP2_JP2C != box.type) {
445 cio_skip(box.length - 8);
446 jp2_read_boxhdr(&box);
448 } while (JP2_JP2C != box.type);
450 *j2k_codestream_offset = cio_tell();
451 *j2k_codestream_len = box.length - 8;
460 box.init_pos = cio_tell();
462 cio_write(JP2_JP, 4); /* JP*/
463 cio_write(0x0d0a870a, 4);
465 box.length = cio_tell() - box.init_pos;
466 cio_seek(box.init_pos);
467 cio_write(box.length, 4); /* L */
468 cio_seek(box.init_pos + box.length);
474 * JPEG 2000 signature
476 * return 1 if error else 0
482 jp2_read_boxhdr(&box);
483 if (JP2_JP != box.type) {
484 fprintf(stderr, "Error: Expected JP Marker\n");
487 if (0x0d0a870a != cio_read(4)) {
488 fprintf(stderr, "Error with JP Marker\n");
491 if (cio_tell() - box.init_pos != box.length) {
492 fprintf(stderr, "Error with JP Box size\n");
500 int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len)
506 if (jp2_read_ftyp(jp2_struct))
508 if (jp2_read_jp2h(jp2_struct))
511 (&jp2_struct->j2k_codestream_len,
512 &jp2_struct->j2k_codestream_offset))
517 int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,
522 jp2_write_ftyp(jp2_struct);
523 jp2_write_jp2h(jp2_struct);
525 jp2_write_jp2c(jp2_struct->j2k_codestream_len,
526 &jp2_struct->j2k_codestream_offset, j2k_codestream);