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);
74 * Initialisation of a Standard JP2 structure
77 int jp2_init_stdjp2(jp2_struct_t * jp2_struct, j2k_image_t * img)
79 int depth_0, sign, depth, i;
81 jp2_struct->h = img->y1 - img->y0; // HEIGHT
82 jp2_struct->w = img->x1 - img->x0; // WIDTH
83 jp2_struct->numcomps = img->numcomps; // NC
84 jp2_struct->comps = (jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(jp2_comps_t));
86 depth_0 = img->comps[0].prec - 1;
87 sign = img->comps[0].sgnd;
88 jp2_struct->bpc = depth_0 + (sign << 7);
90 for (i = 1; i < img->numcomps; i++) {
91 depth = img->comps[i].prec - 1;
92 sign = img->comps[i].sgnd;
94 jp2_struct->bpc = 255;
99 jp2_struct->C = 7; // C : Always 7
100 jp2_struct->UnkC = 0; // UnkC, colorspace specified in colr box
101 jp2_struct->IPR = 0; // IPR, no intellectual property
103 for (i = 0; i < img->numcomps; i++)
104 jp2_struct->comps[i].bpcc =
105 img->comps[i].prec - 1 + (img->comps[i].sgnd << 7);
107 jp2_struct->precedence = 0; // PRECEDENCE
108 jp2_struct->approx = 0; // APPROX
110 if ((img->numcomps == 1 || img->numcomps == 3)
111 && (jp2_struct->bpc != 255))
112 jp2_struct->meth = 1;
114 jp2_struct->meth = 2;
116 if (jp2_struct->meth == 1) {
117 if (img->color_space == 1)
118 jp2_struct->enumcs = 16;
119 else if (img->color_space == 2)
120 jp2_struct->enumcs = 17;
121 else if (img->color_space == 3)
122 jp2_struct->enumcs = 18; // YUV
124 jp2_struct->enumcs = 0; // PROFILE (??)
126 jp2_struct->brand = JP2_JP2; /* BR */
127 jp2_struct->minversion = 0; /* MinV */
128 jp2_struct->numcl = 1;
129 jp2_struct->cl = (int *) malloc(jp2_struct->numcl * sizeof(int));
130 jp2_struct->cl[0] = JP2_JP2; /* CL0 : JP2 */
135 void jp2_write_url(char *Idx_file)
141 sprintf(str, "%s", Idx_file);
144 box.init_pos = cio_tell();
146 cio_write(JP2_URL, 4); // DBTL
147 cio_write(0, 1); // VERS
148 cio_write(0, 3); // FLAG
150 for (i = 0; i < strlen(str); i++) {
151 cio_write(str[i], 1);
154 box.length = cio_tell() - box.init_pos;
155 cio_seek(box.init_pos);
156 cio_write(box.length, 4); /* L */
157 cio_seek(box.init_pos + box.length);
166 int jp2_read_ihdr(jp2_struct_t * jp2_struct)
170 jp2_read_boxhdr(&box);
171 if (JP2_IHDR != box.type) {
172 fprintf(stderr, "Error: Expected IHDR Marker\n");
176 jp2_struct->h = cio_read(4); // HEIGHT
177 jp2_struct->w = cio_read(4); // WIDTH
178 jp2_struct->numcomps = cio_read(2); // NC
180 jp2_struct->bpc = cio_read(1); // BPC
182 jp2_struct->C = cio_read(1); // C
183 jp2_struct->UnkC = cio_read(1); // UnkC
184 jp2_struct->IPR = cio_read(1); // IPR
186 if (cio_tell() - box.init_pos != box.length) {
187 fprintf(stderr, "Error with IHDR Box\n");
193 void jp2_write_ihdr(jp2_struct_t * jp2_struct)
197 box.init_pos = cio_tell();
199 cio_write(JP2_IHDR, 4); // IHDR
201 cio_write(jp2_struct->h, 4); // HEIGHT
202 cio_write(jp2_struct->w, 4); // WIDTH
203 cio_write(jp2_struct->numcomps, 2); // NC
205 cio_write(jp2_struct->bpc, 1); // BPC
207 cio_write(jp2_struct->C, 1); // C : Always 7
208 cio_write(jp2_struct->UnkC, 1); // UnkC, colorspace unknow
209 cio_write(jp2_struct->IPR, 1); // IPR, no intellectual property
211 box.length = cio_tell() - box.init_pos;
212 cio_seek(box.init_pos);
213 cio_write(box.length, 4); /* L */
214 cio_seek(box.init_pos + box.length);
218 void jp2_write_bpcc(jp2_struct_t * jp2_struct)
223 box.init_pos = cio_tell();
225 cio_write(JP2_BPCC, 4); // BPCC
227 for (i = 0; i < jp2_struct->numcomps; i++)
228 cio_write(jp2_struct->comps[i].bpcc, 1);
230 box.length = cio_tell() - box.init_pos;
231 cio_seek(box.init_pos);
232 cio_write(box.length, 4); /* L */
233 cio_seek(box.init_pos + box.length);
237 int jp2_read_bpcc(jp2_struct_t * jp2_struct)
242 jp2_read_boxhdr(&box);
243 if (JP2_BPCC != box.type) {
244 fprintf(stderr, "Error: Expected BPCC Marker\n");
248 for (i = 0; i < jp2_struct->numcomps; i++)
249 jp2_struct->comps[i].bpcc = cio_read(1);
251 if (cio_tell() - box.init_pos != box.length) {
252 fprintf(stderr, "Error with BPCC Box\n");
258 void jp2_write_colr(jp2_struct_t * jp2_struct)
262 box.init_pos = cio_tell();
264 cio_write(JP2_COLR, 4); // COLR
266 cio_write(jp2_struct->meth, 1); // METH
267 cio_write(jp2_struct->precedence, 1); // PRECEDENCE
268 cio_write(jp2_struct->approx, 1); // APPROX
270 if (jp2_struct->meth == 1)
271 cio_write(jp2_struct->enumcs, 4); // EnumCS
273 cio_write(0, 1); // PROFILE (??)
275 box.length = cio_tell() - box.init_pos;
276 cio_seek(box.init_pos);
277 cio_write(box.length, 4); /* L */
278 cio_seek(box.init_pos + box.length);
281 int jp2_read_colr(jp2_struct_t * jp2_struct)
285 jp2_read_boxhdr(&box);
286 if (JP2_COLR != box.type) {
287 fprintf(stderr, "Error: Expected COLR Marker\n");
291 jp2_struct->meth = cio_read(1); // METH
292 jp2_struct->precedence = cio_read(1); // PRECEDENCE
293 jp2_struct->approx = cio_read(1); // APPROX
295 if (jp2_struct->meth == 1)
296 jp2_struct->enumcs = cio_read(4); // EnumCS
298 cio_read(1); // PROFILE
300 if (cio_tell() - box.init_pos != box.length) {
301 fprintf(stderr, "Error with BPCC Box\n");
313 void jp2_write_jp2h(jp2_struct_t * jp2_struct)
317 box.init_pos = cio_tell();
319 cio_write(JP2_JP2H, 4); /* JP2H */
321 jp2_write_ihdr(jp2_struct);
323 if (jp2_struct->bpc == 255)
324 jp2_write_bpcc(jp2_struct);
325 jp2_write_colr(jp2_struct);
327 box.length = cio_tell() - box.init_pos;
328 cio_seek(box.init_pos);
329 cio_write(box.length, 4); /* L */
330 cio_seek(box.init_pos + box.length);
340 int jp2_read_jp2h(jp2_struct_t * jp2_struct)
344 jp2_read_boxhdr(&box);
345 if (JP2_JP2H != box.type) {
346 fprintf(stderr, "Error: Expected JP2H Marker\n");
350 if (jp2_read_ihdr(jp2_struct))
353 if (jp2_struct->bpc == 255)
354 if (jp2_read_bpcc(jp2_struct))
356 if (jp2_read_colr(jp2_struct))
359 if (cio_tell() - box.init_pos != box.length) {
360 fprintf(stderr, "Error with JP2H Box\n");
372 void jp2_write_ftyp(jp2_struct_t * jp2_struct)
377 box.init_pos = cio_tell();
379 cio_write(JP2_FTYP, 4); /* FTYP */
381 cio_write(jp2_struct->brand, 4); /* BR */
382 cio_write(jp2_struct->minversion, 4); /* MinV */
384 for (i = 0; i < jp2_struct->numcl; i++)
385 cio_write(jp2_struct->cl[i], 4); /* CL */
387 box.length = cio_tell() - box.init_pos;
388 cio_seek(box.init_pos);
389 cio_write(box.length, 4); /* L */
390 cio_seek(box.init_pos + box.length);
399 int jp2_read_ftyp(jp2_struct_t * jp2_struct)
404 jp2_read_boxhdr(&box);
406 if (JP2_FTYP != box.type) {
407 fprintf(stderr, "Error: Excpected FTYP Marker\n");
411 jp2_struct->brand = cio_read(4); /* BR */
412 jp2_struct->minversion = cio_read(4); /* MinV */
413 jp2_struct->numcl = (box.length - 16) / 4;
415 (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
417 for (i = 0; i < (int)jp2_struct->numcl; i++)
418 jp2_struct->cl[i] = cio_read(4); /* CLi */
420 if (cio_tell() - box.init_pos != box.length) {
421 fprintf(stderr, "Error with FTYP Box\n");
427 int jp2_write_jp2c(j2k_image_t * img, j2k_cp_t * cp, char *jp2_buffer,
433 box.init_pos = cio_tell();
435 cio_write(JP2_JP2C, 4); // JP2C
437 len = j2k_encode(img, cp, jp2_buffer, cp->tdx * cp->tdy * cp->th * cp->tw * 2, index);
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);
447 int jp2_read_jp2c(unsigned char *src, int len, jp2_struct_t * jp2_struct,
452 jp2_read_boxhdr(&box);
453 if (JP2_JP2C != box.type) {
454 fprintf(stderr, "Error: Expected JP2C Marker\n");
460 if (j2k_decode(src, len, jp2_struct->image, cp) == 0) {
461 fprintf(stderr, "JP2F box: failed to decode J2K bitstream image!\n");
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");
511 int jp2_decode(unsigned char *src, int len, jp2_struct_t * jp2_struct,
518 if (jp2_read_ftyp(jp2_struct))
520 if (jp2_read_jp2h(jp2_struct))
522 if (jp2_read_jp2c(src, len, jp2_struct, cp))
527 int jp2_encode(jp2_struct_t * jp2_struct, j2k_cp_t * cp, char *output,
533 jp2_write_ftyp(jp2_struct);
534 jp2_write_jp2h(jp2_struct);
535 len = jp2_write_jp2c(jp2_struct->image, cp, output, index);