2 * Copyright (c) 2003-2004, Yannick Verschueren
\r
3 * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
\r
4 * All rights reserved.
\r
6 * Redistribution and use in source and binary forms, with or without
\r
7 * modification, are permitted provided that the following conditions
\r
9 * 1. Redistributions of source code must retain the above copyright
\r
10 * notice, this list of conditions and the following disclaimer.
\r
11 * 2. Redistributions in binary form must reproduce the above copyright
\r
12 * notice, this list of conditions and the following disclaimer in the
\r
13 * documentation and/or other materials provided with the distribution.
\r
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
\r
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
\r
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\r
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\r
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
25 * POSSIBILITY OF SUCH DAMAGE.
\r
36 #define JPIP_JPIP 0x6a706970
\r
38 #define JP2_JP 0x6a502020
\r
39 #define JP2_FTYP 0x66747970
\r
40 #define JP2_JP2H 0x6a703268
\r
41 #define JP2_IHDR 0x69686472
\r
42 #define JP2_COLR 0x636f6c72
\r
43 #define JP2_JP2C 0x6a703263
\r
44 #define JP2_URL 0x75726c20
\r
45 #define JP2_DBTL 0x6474626c
\r
46 #define JP2_BPCC 0x62706363
\r
47 #define JP2_JP2 0x6a703220
\r
55 int jp2_read_boxhdr(jp2_box_t * box)
\r
57 box->init_pos = cio_tell();
\r
58 box->length = cio_read(4);
\r
59 box->type = cio_read(4);
\r
60 if (box->length == 1) {
\r
61 if (cio_read(4) != 0) {
\r
62 fprintf(stderr, "Error: Cannot handle box sizes higher than 2^32\n");
\r
65 box->length = cio_read(4);
\r
66 if (box->length == 0)
\r
67 box->length = cio_numbytesleft() + 12;
\r
68 } else if (box->length == 0) {
\r
69 box->length = cio_numbytesleft() + 8;
\r
76 * Initialisation of a Standard JP2 structure
\r
79 int jp2_init_stdjp2(jp2_struct_t * jp2_struct)
\r
83 (jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(jp2_comps_t));
\r
85 jp2_struct->precedence = 0; /* PRECEDENCE*/
\r
86 jp2_struct->approx = 0; /* APPROX*/
\r
88 jp2_struct->brand = JP2_JP2; /* BR */
\r
89 jp2_struct->minversion = 0; /* MinV */
\r
90 jp2_struct->numcl = 1;
\r
91 jp2_struct->cl = (unsigned int *) malloc(jp2_struct->numcl * sizeof(int));
\r
92 jp2_struct->cl[0] = JP2_JP2; /* CL0 : JP2 */
\r
94 jp2_struct->C = 7; /* C : Always 7*/
\r
95 jp2_struct->UnkC = 0; /* UnkC, colorspace specified in colr box*/
\r
96 jp2_struct->IPR = 0; /* IPR, no intellectual property*/
\r
102 void jp2_write_url(char *Idx_file)
\r
108 sprintf(str, "%s", Idx_file);
\r
111 box.init_pos = cio_tell();
\r
113 cio_write(JP2_URL, 4); /* DBTL*/
\r
114 cio_write(0, 1); /* VERS*/
\r
115 cio_write(0, 3); /* FLAG*/
\r
117 for (i = 0; i < strlen(str); i++) {
\r
118 cio_write(str[i], 1);
\r
121 box.length = cio_tell() - box.init_pos;
\r
122 cio_seek(box.init_pos);
\r
123 cio_write(box.length, 4); /* L */
\r
124 cio_seek(box.init_pos + box.length);
\r
128 * Read the IHDR box
\r
133 int jp2_read_ihdr(jp2_struct_t * jp2_struct)
\r
137 jp2_read_boxhdr(&box);
\r
138 if (JP2_IHDR != box.type) {
\r
139 fprintf(stderr, "Error: Expected IHDR Marker\n");
\r
143 jp2_struct->h = cio_read(4); /* HEIGHT*/
\r
144 jp2_struct->w = cio_read(4); /* WIDTH*/
\r
145 jp2_struct->numcomps = cio_read(2); /* NC*/
\r
147 jp2_struct->bpc = cio_read(1); /* BPC*/
\r
149 jp2_struct->C = cio_read(1); /* C */
\r
150 jp2_struct->UnkC = cio_read(1); /* UnkC*/
\r
151 jp2_struct->IPR = cio_read(1); /* IPR*/
\r
153 if (cio_tell() - box.init_pos != box.length) {
\r
154 fprintf(stderr, "Error with IHDR Box\n");
\r
160 void jp2_write_ihdr(jp2_struct_t * jp2_struct)
\r
164 box.init_pos = cio_tell();
\r
166 cio_write(JP2_IHDR, 4); /* IHDR*/
\r
168 cio_write(jp2_struct->h, 4); /* HEIGHT*/
\r
169 cio_write(jp2_struct->w, 4); /* WIDTH*/
\r
170 cio_write(jp2_struct->numcomps, 2); /* NC*/
\r
172 cio_write(jp2_struct->bpc, 1); /* BPC */
\r
174 cio_write(jp2_struct->C, 1); /* C : Always 7*/
\r
175 cio_write(jp2_struct->UnkC, 1); /* UnkC, colorspace unknow*/
\r
176 cio_write(jp2_struct->IPR, 1); /* IPR, no intellectual property*/
\r
178 box.length = cio_tell() - box.init_pos;
\r
179 cio_seek(box.init_pos);
\r
180 cio_write(box.length, 4); /* L */
\r
181 cio_seek(box.init_pos + box.length);
\r
185 void jp2_write_bpcc(jp2_struct_t * jp2_struct)
\r
190 box.init_pos = cio_tell();
\r
192 cio_write(JP2_BPCC, 4); /* BPCC*/
\r
194 for (i = 0; i < jp2_struct->numcomps; i++)
\r
195 cio_write(jp2_struct->comps[i].bpcc, 1);
\r
197 box.length = cio_tell() - box.init_pos;
\r
198 cio_seek(box.init_pos);
\r
199 cio_write(box.length, 4); /* L */
\r
200 cio_seek(box.init_pos + box.length);
\r
204 int jp2_read_bpcc(jp2_struct_t * jp2_struct)
\r
209 jp2_read_boxhdr(&box);
\r
210 if (JP2_BPCC != box.type) {
\r
211 fprintf(stderr, "Error: Expected BPCC Marker\n");
\r
215 for (i = 0; i < jp2_struct->numcomps; i++)
\r
216 jp2_struct->comps[i].bpcc = cio_read(1);
\r
218 if (cio_tell() - box.init_pos != box.length) {
\r
219 fprintf(stderr, "Error with BPCC Box\n");
\r
225 void jp2_write_colr(jp2_struct_t * jp2_struct)
\r
229 box.init_pos = cio_tell();
\r
231 cio_write(JP2_COLR, 4); /* COLR*/
\r
233 cio_write(jp2_struct->meth, 1); /* METH*/
\r
234 cio_write(jp2_struct->precedence, 1); /* PRECEDENCE*/
\r
235 cio_write(jp2_struct->approx, 1); /* APPROX*/
\r
237 if (jp2_struct->meth == 1)
\r
238 cio_write(jp2_struct->enumcs, 4); /* EnumCS*/
\r
240 cio_write(0, 1); /* PROFILE (??)*/
\r
242 box.length = cio_tell() - box.init_pos;
\r
243 cio_seek(box.init_pos);
\r
244 cio_write(box.length, 4); /* L */
\r
245 cio_seek(box.init_pos + box.length);
\r
248 int jp2_read_colr(jp2_struct_t * jp2_struct)
\r
253 jp2_read_boxhdr(&box);
\r
255 if (JP2_COLR != box.type) {
\r
256 cio_skip(box.length - 8);
\r
257 jp2_read_boxhdr(&box);
\r
259 } while (JP2_COLR != box.type);
\r
261 jp2_struct->meth = cio_read(1); /* METH*/
\r
262 jp2_struct->precedence = cio_read(1); /* PRECEDENCE*/
\r
263 jp2_struct->approx = cio_read(1); /* APPROX*/
\r
265 if (jp2_struct->meth == 1)
\r
266 jp2_struct->enumcs = cio_read(4); /* EnumCS*/
\r
269 skip_len = box.init_pos + box.length - cio_tell();
\r
270 if (skip_len < 0) {
\r
271 fprintf(stderr, "Error with JP2H box size\n");
\r
274 cio_skip(box.init_pos + box.length - cio_tell());
\r
277 if (cio_tell() - box.init_pos != box.length) {
\r
278 fprintf(stderr, "Error with BPCC Box\n");
\r
285 * Write the JP2H box
\r
290 void jp2_write_jp2h(jp2_struct_t * jp2_struct)
\r
294 box.init_pos = cio_tell();
\r
296 cio_write(JP2_JP2H, 4); /* JP2H */
\r
298 jp2_write_ihdr(jp2_struct);
\r
300 if (jp2_struct->bpc == 255)
\r
301 jp2_write_bpcc(jp2_struct);
\r
302 jp2_write_colr(jp2_struct);
\r
304 box.length = cio_tell() - box.init_pos;
\r
305 cio_seek(box.init_pos);
\r
306 cio_write(box.length, 4); /* L */
\r
307 cio_seek(box.init_pos + box.length);
\r
312 * Read the JP2H box
\r
317 int jp2_read_jp2h(jp2_struct_t * jp2_struct)
\r
322 jp2_read_boxhdr(&box);
\r
324 if (JP2_JP2H != box.type) {
\r
325 if (box.type == JP2_JP2C) {
\r
326 fprintf(stderr, "Error: Expected JP2H Marker\n");
\r
329 cio_skip(box.length - 8);
\r
330 jp2_read_boxhdr(&box);
\r
332 } while (JP2_JP2H != box.type);
\r
334 if (jp2_read_ihdr(jp2_struct))
\r
337 if (jp2_struct->bpc == 255) {
\r
338 if (jp2_read_bpcc(jp2_struct))
\r
342 if (jp2_read_colr(jp2_struct))
\r
345 skip_len = box.init_pos + box.length - cio_tell();
\r
346 if (skip_len < 0) {
\r
347 fprintf(stderr, "Error with JP2H box size\n");
\r
350 cio_skip(box.init_pos + box.length - cio_tell());
\r
356 * Write the FTYP box
\r
361 void jp2_write_ftyp(jp2_struct_t * jp2_struct)
\r
366 box.init_pos = cio_tell();
\r
368 cio_write(JP2_FTYP, 4); /* FTYP */
\r
370 cio_write(jp2_struct->brand, 4); /* BR */
\r
371 cio_write(jp2_struct->minversion, 4); /* MinV */
\r
373 for (i = 0; i < jp2_struct->numcl; i++)
\r
374 cio_write(jp2_struct->cl[i], 4); /* CL */
\r
376 box.length = cio_tell() - box.init_pos;
\r
377 cio_seek(box.init_pos);
\r
378 cio_write(box.length, 4); /* L */
\r
379 cio_seek(box.init_pos + box.length);
\r
383 * Read the FTYP box
\r
388 int jp2_read_ftyp(jp2_struct_t * jp2_struct)
\r
393 jp2_read_boxhdr(&box);
\r
395 if (JP2_FTYP != box.type) {
\r
396 fprintf(stderr, "Error: Excpected FTYP Marker\n");
\r
400 jp2_struct->brand = cio_read(4); /* BR */
\r
401 jp2_struct->minversion = cio_read(4); /* MinV */
\r
402 jp2_struct->numcl = (box.length - 16) / 4;
\r
404 (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
\r
406 for (i = 0; i < (int) jp2_struct->numcl; i++)
\r
407 jp2_struct->cl[i] = cio_read(4); /* CLi */
\r
409 if (cio_tell() - box.init_pos != box.length) {
\r
410 fprintf(stderr, "Error with FTYP Box\n");
\r
416 int jp2_write_jp2c(int j2k_codestream_len, int *j2k_codestream_offset,
\r
417 char *j2k_codestream)
\r
421 box.init_pos = cio_tell();
\r
423 cio_write(JP2_JP2C, 4); /* JP2C*/
\r
425 *j2k_codestream_offset = cio_tell();
\r
426 memcpy(cio_getbp(), j2k_codestream, j2k_codestream_len);
\r
428 box.length = 8 + j2k_codestream_len;
\r
429 cio_seek(box.init_pos);
\r
430 cio_write(box.length, 4); /* L */
\r
431 cio_seek(box.init_pos + box.length);
\r
437 int jp2_read_jp2c(unsigned int *j2k_codestream_len,
\r
438 unsigned int *j2k_codestream_offset)
\r
442 jp2_read_boxhdr(&box);
\r
444 if (JP2_JP2C != box.type) {
\r
445 cio_skip(box.length - 8);
\r
446 jp2_read_boxhdr(&box);
\r
448 } while (JP2_JP2C != box.type);
\r
450 *j2k_codestream_offset = cio_tell();
\r
451 *j2k_codestream_len = box.length - 8;
\r
456 void jp2_write_jp()
\r
460 box.init_pos = cio_tell();
\r
462 cio_write(JP2_JP, 4); /* JP*/
\r
463 cio_write(0x0d0a870a, 4);
\r
465 box.length = cio_tell() - box.init_pos;
\r
466 cio_seek(box.init_pos);
\r
467 cio_write(box.length, 4); /* L */
\r
468 cio_seek(box.init_pos + box.length);
\r
474 * JPEG 2000 signature
\r
476 * return 1 if error else 0
\r
482 jp2_read_boxhdr(&box);
\r
483 if (JP2_JP != box.type) {
\r
484 fprintf(stderr, "Error: Expected JP Marker\n");
\r
487 if (0x0d0a870a != cio_read(4)) {
\r
488 fprintf(stderr, "Error with JP Marker\n");
\r
491 if (cio_tell() - box.init_pos != box.length) {
\r
492 fprintf(stderr, "Error with JP Box size\n");
\r
500 int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len)
\r
502 cio_init(src, len);
\r
506 if (jp2_read_ftyp(jp2_struct))
\r
508 if (jp2_read_jp2h(jp2_struct))
\r
511 (&jp2_struct->j2k_codestream_len,
\r
512 &jp2_struct->j2k_codestream_offset))
\r
517 int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,
\r
522 jp2_write_ftyp(jp2_struct);
\r
523 jp2_write_jp2h(jp2_struct);
\r
525 jp2_write_jp2c(jp2_struct->j2k_codestream_len,
\r
526 &jp2_struct->j2k_codestream_offset, j2k_codestream);
\r