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;
69 else if (box->length == 0) {
70 box->length = cio_numbytesleft() + 8;
77 * Initialisation of a Standard JP2 structure
80 int jp2_init_stdjp2(jp2_struct_t * jp2_struct)
83 jp2_struct->comps = (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 = (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)
252 jp2_read_boxhdr(&box);
253 if (JP2_COLR != box.type) {
254 fprintf(stderr, "Error: Expected COLR Marker\n");
258 jp2_struct->meth = cio_read(1); // METH
259 jp2_struct->precedence = cio_read(1); // PRECEDENCE
260 jp2_struct->approx = cio_read(1); // APPROX
262 if (jp2_struct->meth == 1)
263 jp2_struct->enumcs = cio_read(4); // EnumCS
265 cio_read(1); // PROFILE
267 if (cio_tell() - box.init_pos != box.length) {
268 fprintf(stderr, "Error with BPCC Box\n");
280 void jp2_write_jp2h(jp2_struct_t * jp2_struct)
284 box.init_pos = cio_tell();
286 cio_write(JP2_JP2H, 4); /* JP2H */
288 jp2_write_ihdr(jp2_struct);
290 if (jp2_struct->bpc == 255)
291 jp2_write_bpcc(jp2_struct);
292 jp2_write_colr(jp2_struct);
294 box.length = cio_tell() - box.init_pos;
295 cio_seek(box.init_pos);
296 cio_write(box.length, 4); /* L */
297 cio_seek(box.init_pos + box.length);
307 int jp2_read_jp2h(jp2_struct_t * jp2_struct)
311 jp2_read_boxhdr(&box);
312 if (JP2_JP2H != box.type) {
313 fprintf(stderr, "Error: Expected JP2H Marker\n");
317 if (jp2_read_ihdr(jp2_struct))
320 if (jp2_struct->bpc == 255)
321 if (jp2_read_bpcc(jp2_struct))
323 if (jp2_read_colr(jp2_struct))
326 if (cio_tell() - box.init_pos != box.length) {
327 fprintf(stderr, "Error with JP2H Box\n");
339 void jp2_write_ftyp(jp2_struct_t * jp2_struct)
344 box.init_pos = cio_tell();
346 cio_write(JP2_FTYP, 4); /* FTYP */
348 cio_write(jp2_struct->brand, 4); /* BR */
349 cio_write(jp2_struct->minversion, 4); /* MinV */
351 for (i = 0; i < jp2_struct->numcl; i++)
352 cio_write(jp2_struct->cl[i], 4); /* CL */
354 box.length = cio_tell() - box.init_pos;
355 cio_seek(box.init_pos);
356 cio_write(box.length, 4); /* L */
357 cio_seek(box.init_pos + box.length);
366 int jp2_read_ftyp(jp2_struct_t * jp2_struct)
371 jp2_read_boxhdr(&box);
373 if (JP2_FTYP != box.type) {
374 fprintf(stderr, "Error: Excpected FTYP Marker\n");
378 jp2_struct->brand = cio_read(4); /* BR */
379 jp2_struct->minversion = cio_read(4); /* MinV */
380 jp2_struct->numcl = (box.length - 16) / 4;
382 (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
384 for (i = 0; i < (int)jp2_struct->numcl; i++)
385 jp2_struct->cl[i] = cio_read(4); /* CLi */
387 if (cio_tell() - box.init_pos != box.length) {
388 fprintf(stderr, "Error with FTYP Box\n");
394 int jp2_write_jp2c(int j2k_codestream_len, int *j2k_codestream_offset, char *j2k_codestream)
398 box.init_pos = cio_tell();
400 cio_write(JP2_JP2C, 4); // JP2C
402 *j2k_codestream_offset = cio_tell();
403 memcpy(cio_getbp(),j2k_codestream, j2k_codestream_len);
405 box.length = 8 + j2k_codestream_len;
406 cio_seek(box.init_pos);
407 cio_write(box.length, 4); /* L */
408 cio_seek(box.init_pos + box.length);
414 int jp2_read_jp2c(unsigned int *j2k_codestream_len, unsigned int *j2k_codestream_offset)
418 jp2_read_boxhdr(&box);
419 if (JP2_JP2C != box.type) {
420 fprintf(stderr, "Error: Expected JP2C Marker\n");
424 *j2k_codestream_offset = cio_tell();
425 *j2k_codestream_len = box.length - 8;
434 box.init_pos = cio_tell();
436 cio_write(JP2_JP, 4); // JP
437 cio_write(0x0d0a870a, 4);
439 box.length = cio_tell() - box.init_pos;
440 cio_seek(box.init_pos);
441 cio_write(box.length, 4); /* L */
442 cio_seek(box.init_pos + box.length);
448 * JPEG 2000 signature
450 * return 1 if error else 0
456 jp2_read_boxhdr(&box);
457 if (JP2_JP != box.type) {
458 fprintf(stderr, "Error: Expected JP Marker\n");
461 if (0x0d0a870a != cio_read(4)) {
462 fprintf(stderr, "Error with JP Marker\n");
465 if (cio_tell() - box.init_pos != box.length) {
466 fprintf(stderr, "Error with JP Box size\n");
474 int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len)
480 if (jp2_read_ftyp(jp2_struct))
482 if (jp2_read_jp2h(jp2_struct))
484 if (jp2_read_jp2c(&jp2_struct->j2k_codestream_len, &jp2_struct->j2k_codestream_offset))
489 int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream, char *output)
492 jp2_write_ftyp(jp2_struct);
493 jp2_write_jp2h(jp2_struct);
495 jp2_write_jp2c(jp2_struct->j2k_codestream_len, &jp2_struct->j2k_codestream_offset, j2k_codestream);