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;
82 jp2_struct->h = img->y1 - img->y0; // HEIGHT
83 jp2_struct->w = img->x1 - img->x0; // WIDTH
84 jp2_struct->numcomps = img->numcomps; // NC
85 jp2_struct->comps = (jp2_comps_t *) malloc(sizeof(jp2_comps_t));
87 depth_0 = img->comps[0].prec - 1;
88 sign = img->comps[0].sgnd;
89 jp2_struct->bpc = depth_0 + (sign << 7);
91 for (i = 1; i < img->numcomps; i++) {
92 depth = img->comps[i].prec - 1;
93 sign = img->comps[i].sgnd;
95 jp2_struct->bpc = 255;
100 jp2_struct->C = 7; // C : Always 7
101 jp2_struct->UnkC = 0; // UnkC, colorspace specified in colr box
102 jp2_struct->IPR = 0; // IPR, no intellectual property
104 for (i = 0; i < img->numcomps; i++)
105 jp2_struct->comps[i].bpcc =
106 img->comps[i].prec - 1 + (img->comps[i].sgnd << 7);
108 jp2_struct->precedence = 0; // PRECEDENCE
109 jp2_struct->approx = 0; // APPROX
111 if ((img->numcomps == 1 || img->numcomps == 3)
112 && (jp2_struct->bpc != 255))
113 jp2_struct->meth = 1;
115 jp2_struct->meth = 2;
117 if (jp2_struct->meth == 1) {
118 if (img->color_space == 1)
119 jp2_struct->enumcs = 16;
120 else if (img->color_space == 2)
121 jp2_struct->enumcs = 17;
122 else if (img->color_space == 3)
123 jp2_struct->enumcs = 18; // YUV
125 jp2_struct->enumcs = 0; // PROFILE (??)
127 jp2_struct->brand = JP2_JP2; /* BR */
128 jp2_struct->minversion = 0; /* MinV */
129 jp2_struct->numcl = 1;
130 jp2_struct->cl = (int *) malloc(jp2_struct->numcl * sizeof(int));
131 jp2_struct->cl[0] = JP2_JP2; /* CL0 : JP2 */
136 void jp2_write_url(char *Idx_file)
142 sprintf(str, "%s", Idx_file);
145 box.init_pos = cio_tell();
147 cio_write(JP2_URL, 4); // DBTL
148 cio_write(0, 1); // VERS
149 cio_write(0, 3); // FLAG
151 for (i = 0; i < strlen(str); i++) {
152 cio_write(str[i], 1);
155 box.length = cio_tell() - box.init_pos;
156 cio_seek(box.init_pos);
157 cio_write(box.length, 4); /* L */
158 cio_seek(box.init_pos + box.length);
167 int jp2_read_ihdr(jp2_struct_t * jp2_struct)
171 jp2_read_boxhdr(&box);
172 if (JP2_IHDR != box.type) {
173 fprintf(stderr, "Error: Expected IHDR Marker\n");
177 jp2_struct->h = cio_read(4); // HEIGHT
178 jp2_struct->w = cio_read(4); // WIDTH
179 jp2_struct->numcomps = cio_read(2); // NC
181 jp2_struct->bpc = cio_read(1); // BPC
183 jp2_struct->C = cio_read(1); // C
184 jp2_struct->UnkC = cio_read(1); // UnkC
185 jp2_struct->IPR = cio_read(1); // IPR
187 if (cio_tell() - box.init_pos != box.length) {
188 fprintf(stderr, "Error with IHDR Box\n");
194 void jp2_write_ihdr(jp2_struct_t * jp2_struct)
198 box.init_pos = cio_tell();
200 cio_write(JP2_IHDR, 4); // IHDR
202 cio_write(jp2_struct->h, 4); // HEIGHT
203 cio_write(jp2_struct->w, 4); // WIDTH
204 cio_write(jp2_struct->numcomps, 2); // NC
206 cio_write(jp2_struct->bpc, 1); // BPC
208 cio_write(jp2_struct->C, 1); // C : Always 7
209 cio_write(jp2_struct->UnkC, 1); // UnkC, colorspace unknow
210 cio_write(jp2_struct->IPR, 1); // IPR, no intellectual property
212 box.length = cio_tell() - box.init_pos;
213 cio_seek(box.init_pos);
214 cio_write(box.length, 4); /* L */
215 cio_seek(box.init_pos + box.length);
219 void jp2_write_bpcc(jp2_struct_t * jp2_struct)
224 box.init_pos = cio_tell();
226 cio_write(JP2_BPCC, 4); // BPCC
228 for (i = 0; i < jp2_struct->numcomps; i++)
229 cio_write(jp2_struct->comps[i].bpcc, 1);
231 box.length = cio_tell() - box.init_pos;
232 cio_seek(box.init_pos);
233 cio_write(box.length, 4); /* L */
234 cio_seek(box.init_pos + box.length);
238 int jp2_read_bpcc(jp2_struct_t * jp2_struct)
243 jp2_read_boxhdr(&box);
244 if (JP2_BPCC != box.type) {
245 fprintf(stderr, "Error: Expected BPCC Marker\n");
249 for (i = 0; i < jp2_struct->numcomps; i++)
250 jp2_struct->comps[i].bpcc = cio_read(1);
252 if (cio_tell() - box.init_pos != box.length) {
253 fprintf(stderr, "Error with BPCC Box\n");
259 void jp2_write_colr(jp2_struct_t * jp2_struct)
263 box.init_pos = cio_tell();
265 cio_write(JP2_COLR, 4); // COLR
267 cio_write(jp2_struct->meth, 1); // METH
268 cio_write(jp2_struct->precedence, 1); // PRECEDENCE
269 cio_write(jp2_struct->approx, 1); // APPROX
271 if (jp2_struct->meth == 1)
272 cio_write(jp2_struct->enumcs, 4); // EnumCS
274 cio_write(0, 1); // PROFILE (??)
276 box.length = cio_tell() - box.init_pos;
277 cio_seek(box.init_pos);
278 cio_write(box.length, 4); /* L */
279 cio_seek(box.init_pos + box.length);
282 int jp2_read_colr(jp2_struct_t * jp2_struct)
286 jp2_read_boxhdr(&box);
287 if (JP2_COLR != box.type) {
288 fprintf(stderr, "Error: Expected COLR Marker\n");
292 jp2_struct->meth = cio_read(1); // METH
293 jp2_struct->precedence = cio_read(1); // PRECEDENCE
294 jp2_struct->approx = cio_read(1); // APPROX
296 if (jp2_struct->meth == 1)
297 jp2_struct->enumcs = cio_read(4); // EnumCS
299 cio_read(1); // PROFILE
301 if (cio_tell() - box.init_pos != box.length) {
302 fprintf(stderr, "Error with BPCC Box\n");
314 void jp2_write_jp2h(jp2_struct_t * jp2_struct)
318 box.init_pos = cio_tell();
320 cio_write(JP2_JP2H, 4); /* JP2H */
322 jp2_write_ihdr(jp2_struct);
324 if (jp2_struct->bpc == 255)
325 jp2_write_bpcc(jp2_struct);
326 jp2_write_colr(jp2_struct);
328 box.length = cio_tell() - box.init_pos;
329 cio_seek(box.init_pos);
330 cio_write(box.length, 4); /* L */
331 cio_seek(box.init_pos + box.length);
341 int jp2_read_jp2h(jp2_struct_t * jp2_struct)
345 jp2_read_boxhdr(&box);
346 if (JP2_JP2H != box.type) {
347 fprintf(stderr, "Error: Expected JP2H Marker\n");
351 if (jp2_read_ihdr(jp2_struct))
354 if (jp2_struct->bpc == 255)
355 if (jp2_read_bpcc(jp2_struct))
357 if (jp2_read_colr(jp2_struct))
360 if (cio_tell() - box.init_pos != box.length) {
361 fprintf(stderr, "Error with JP2H Box\n");
373 void jp2_write_ftyp(jp2_struct_t * jp2_struct)
378 box.init_pos = cio_tell();
380 cio_write(JP2_FTYP, 4); /* FTYP */
382 cio_write(jp2_struct->brand, 4); /* BR */
383 cio_write(jp2_struct->minversion, 4); /* MinV */
385 for (i = 0; i < jp2_struct->numcl; i++)
386 cio_write(jp2_struct->cl[i], 4); /* CL */
388 box.length = cio_tell() - box.init_pos;
389 cio_seek(box.init_pos);
390 cio_write(box.length, 4); /* L */
391 cio_seek(box.init_pos + box.length);
400 int jp2_read_ftyp(jp2_struct_t * jp2_struct)
405 jp2_read_boxhdr(&box);
407 if (JP2_FTYP != box.type) {
408 fprintf(stderr, "Error: Excpected FTYP Marker\n");
412 jp2_struct->brand = cio_read(4); /* BR */
413 jp2_struct->minversion = cio_read(4); /* MinV */
414 jp2_struct->numcl = (box.length - 16) / 4;
416 (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
418 for (i = jp2_struct->numcl; i > 0; i--)
419 jp2_struct->cl[i] = cio_read(4); /* CLi */
421 if (cio_tell() - box.init_pos != box.length) {
422 fprintf(stderr, "Error with FTYP Box\n");
428 int jp2_write_jp2c(j2k_image_t * img, j2k_cp_t * cp, char *jp2_buffer,
434 box.init_pos = cio_tell();
436 cio_write(JP2_JP2C, 4); // JP2C
438 len = j2k_encode(img, cp, jp2_buffer, cp->tdx * cp->tdy * 2, index);
440 box.length = cio_tell() - box.init_pos;
441 cio_seek(box.init_pos);
442 cio_write(box.length, 4); /* L */
443 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);