45f5c2faa612f5ddf7fc2df38790f2e65bd3eba9
[openjpeg.git] / libopenjpeg / jp2.c
1 /*
2  * Copyright (c) 2004, Yannick Verschueren
3  * Copyright (c) 2005, Herv� Drolon, FreeImage Team
4  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "opj_includes.h"
30
31
32 /* ----------------------------------------------------------------------- */
33
34 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
35   box->init_pos = cio_tell(cio);
36   box->length = cio_read(cio, 4);
37   box->type = cio_read(cio, 4);
38   if (box->length == 1) {
39     if (cio_read(cio, 4) != 0) {
40       opg_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
41       return false;
42     }
43     box->length = cio_read(cio, 4);
44     if (box->length == 0) 
45       box->length = cio_numbytesleft(cio) + 12;
46   }
47   else if (box->length == 0) {
48     box->length = cio_numbytesleft(cio) + 8;
49   }
50   
51   return true;
52 }
53
54 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
55   unsigned int i;
56   opj_jp2_box_t box;
57
58   box.init_pos = cio_tell(cio);
59   cio_skip(cio, 4);
60   cio_write(cio, JP2_URL, 4); /* DBTL */
61   cio_write(cio, 0, 1);   /* VERS */
62   cio_write(cio, 0, 3);   /* FLAG */
63
64   if(Idx_file) {
65     for (i = 0; i < strlen(Idx_file); i++) {
66       cio_write(cio, Idx_file[i], 1);
67     }
68   }
69
70   box.length = cio_tell(cio) - box.init_pos;
71   cio_seek(cio, box.init_pos);
72   cio_write(cio, box.length, 4);  /* L */
73   cio_seek(cio, box.init_pos + box.length);
74 }
75
76 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
77   opj_jp2_box_t box;
78
79   opj_common_ptr cinfo = jp2->cinfo;
80
81   jp2_read_boxhdr(cinfo, cio, &box);
82   if (JP2_IHDR != box.type) {
83     opg_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
84     return false;
85   }
86
87   jp2->h = cio_read(cio, 4);      /* HEIGHT */
88   jp2->w = cio_read(cio, 4);      /* WIDTH */
89   jp2->numcomps = cio_read(cio, 2); /* NC */
90
91   jp2->bpc = cio_read(cio, 1);    /* BPC */
92
93   jp2->C = cio_read(cio, 1);      /* C */
94   jp2->UnkC = cio_read(cio, 1);   /* UnkC */
95   jp2->IPR = cio_read(cio, 1);    /* IPR */
96
97   if (cio_tell(cio) - box.init_pos != box.length) {
98     opg_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
99     return false;
100   }
101
102   return true;
103 }
104
105 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
106   opj_jp2_box_t box;
107
108   box.init_pos = cio_tell(cio);
109   cio_skip(cio, 4);
110   cio_write(cio, JP2_IHDR, 4);    /* IHDR */
111
112   cio_write(cio, jp2->h, 4);      /* HEIGHT */
113   cio_write(cio, jp2->w, 4);      /* WIDTH */
114   cio_write(cio, jp2->numcomps, 2); /* NC */
115
116   cio_write(cio, jp2->bpc, 1);    /* BPC */
117
118   cio_write(cio, jp2->C, 1);      /* C : Always 7 */
119   cio_write(cio, jp2->UnkC, 1);   /* UnkC, colorspace unknown */
120   cio_write(cio, jp2->IPR, 1);    /* IPR, no intellectual property */
121
122   box.length = cio_tell(cio) - box.init_pos;
123   cio_seek(cio, box.init_pos);
124   cio_write(cio, box.length, 4);  /* L */
125   cio_seek(cio, box.init_pos + box.length);
126 }
127
128 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
129   unsigned int i;
130   opj_jp2_box_t box;
131
132   box.init_pos = cio_tell(cio);
133   cio_skip(cio, 4);
134   cio_write(cio, JP2_BPCC, 4);  /* BPCC */
135
136   for (i = 0; i < jp2->numcomps; i++) {
137     cio_write(cio, jp2->comps[i].bpcc, 1);
138   }
139
140   box.length = cio_tell(cio) - box.init_pos;
141   cio_seek(cio, box.init_pos);
142   cio_write(cio, box.length, 4);  /* L */
143   cio_seek(cio, box.init_pos + box.length);
144 }
145
146
147 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
148   unsigned int i;
149   opj_jp2_box_t box;
150
151   opj_common_ptr cinfo = jp2->cinfo;
152
153   jp2_read_boxhdr(cinfo, cio, &box);
154   if (JP2_BPCC != box.type) {
155     opg_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
156     return false;
157   }
158
159   for (i = 0; i < jp2->numcomps; i++) {
160     jp2->comps[i].bpcc = cio_read(cio, 1);
161   }
162
163   if (cio_tell(cio) - box.init_pos != box.length) {
164     opg_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
165     return false;
166   }
167
168   return true;
169 }
170
171 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
172   opj_jp2_box_t box;
173
174   box.init_pos = cio_tell(cio);
175   cio_skip(cio, 4);
176   cio_write(cio, JP2_COLR, 4);    /* COLR */
177
178   cio_write(cio, jp2->meth, 1);   /* METH */
179   cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
180   cio_write(cio, jp2->approx, 1);   /* APPROX */
181
182   if (jp2->meth == 1) {
183     cio_write(cio, jp2->enumcs, 4); /* EnumCS */
184   } else {
185     cio_write(cio, 0, 1);     /* PROFILE (??) */
186   }
187
188   box.length = cio_tell(cio) - box.init_pos;
189   cio_seek(cio, box.init_pos);
190   cio_write(cio, box.length, 4);  /* L */
191   cio_seek(cio, box.init_pos + box.length);
192 }
193
194 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
195   opj_jp2_box_t box;
196   int skip_len;
197
198   opj_common_ptr cinfo = jp2->cinfo;
199
200   jp2_read_boxhdr(cinfo, cio, &box);
201   do {
202     if (JP2_COLR != box.type) {
203       cio_skip(cio, box.length - 8);
204       jp2_read_boxhdr(cinfo, cio, &box);
205     }
206   } while(JP2_COLR != box.type);
207
208   jp2->meth = cio_read(cio, 1);   /* METH */
209   jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
210   jp2->approx = cio_read(cio, 1);   /* APPROX */
211
212   if (jp2->meth == 1) {
213     jp2->enumcs = cio_read(cio, 4); /* EnumCS */
214   } else {
215     /* skip PROFILE */
216     skip_len = box.init_pos + box.length - cio_tell(cio);
217     if (skip_len < 0) {
218       opg_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n");
219       return false;
220     }
221     cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
222   }
223
224   if (cio_tell(cio) - box.init_pos != box.length) {
225     opg_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
226     return false;
227   }
228   return true;
229 }
230
231 static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
232   opj_jp2_box_t box;
233
234   box.init_pos = cio_tell(cio);
235   cio_skip(cio, 4);
236   cio_write(cio, JP2_JP2H, 4);  /* JP2H */
237
238   jp2_write_ihdr(jp2, cio);
239
240   if (jp2->bpc == 255) {
241     jp2_write_bpcc(jp2, cio);
242   }
243   jp2_write_colr(jp2, cio);
244
245   box.length = cio_tell(cio) - box.init_pos;
246   cio_seek(cio, box.init_pos);
247   cio_write(cio, box.length, 4);  /* L */
248   cio_seek(cio, box.init_pos + box.length);
249 }
250
251 static bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
252   opj_jp2_box_t box;
253   int skip_len;
254
255   opj_common_ptr cinfo = jp2->cinfo;
256
257   jp2_read_boxhdr(cinfo, cio, &box);
258   do {
259     if (JP2_JP2H != box.type) {
260       if (box.type == JP2_JP2C) {
261         opg_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
262         return false;
263       }
264       cio_skip(cio, box.length - 8);
265       jp2_read_boxhdr(cinfo, cio, &box);
266     }
267   } while(JP2_JP2H != box.type);
268
269   if (!jp2_read_ihdr(jp2, cio))
270     return false;
271
272   if (jp2->bpc == 255) {
273     if (!jp2_read_bpcc(jp2, cio))
274       return false;
275   }
276   if (!jp2_read_colr(jp2, cio))
277     return false;
278
279   skip_len = box.init_pos + box.length - cio_tell(cio);
280   if (skip_len < 0) {
281     opg_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n");
282     return false;
283   }
284   cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
285
286   return true;
287 }
288
289 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
290   unsigned int i;
291   opj_jp2_box_t box;
292
293   box.init_pos = cio_tell(cio);
294   cio_skip(cio, 4);
295   cio_write(cio, JP2_FTYP, 4);    /* FTYP */
296
297   cio_write(cio, jp2->brand, 4);    /* BR */
298   cio_write(cio, jp2->minversion, 4); /* MinV */
299
300   for (i = 0; i < jp2->numcl; i++) {
301     cio_write(cio, jp2->cl[i], 4);  /* CL */
302   }
303
304   box.length = cio_tell(cio) - box.init_pos;
305   cio_seek(cio, box.init_pos);
306   cio_write(cio, box.length, 4);  /* L */
307   cio_seek(cio, box.init_pos + box.length);
308 }
309
310 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
311   int i;
312   opj_jp2_box_t box;
313
314   opj_common_ptr cinfo = jp2->cinfo;
315
316   jp2_read_boxhdr(cinfo, cio, &box);
317
318   if (JP2_FTYP != box.type) {
319     opg_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
320     return false;
321   }
322
323   jp2->brand = cio_read(cio, 4);    /* BR */
324   jp2->minversion = cio_read(cio, 4); /* MinV */
325   jp2->numcl = (box.length - 16) / 4;
326   jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
327
328   for (i = 0; i < (int)jp2->numcl; i++) {
329     jp2->cl[i] = cio_read(cio, 4);  /* CLi */
330   }
331
332   if (cio_tell(cio) - box.init_pos != box.length) {
333     opg_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
334     return false;
335   }
336
337   return true;
338 }
339
340 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, char *index) {
341   unsigned int j2k_codestream_offset, j2k_codestream_length;
342   opj_jp2_box_t box;
343
344   opj_j2k_t *j2k = jp2->j2k;
345   opj_image_t *image = jp2->image;
346
347   box.init_pos = cio_tell(cio);
348   cio_skip(cio, 4);
349   cio_write(cio, JP2_JP2C, 4);  /* JP2C */
350
351   /* J2K encoding */
352   j2k_codestream_offset = cio_tell(cio);
353   if(!j2k_encode(j2k, cio, image, index)) {
354     opg_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
355     return 0;
356   }
357   j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
358
359   jp2->j2k_codestream_offset = j2k_codestream_offset;
360   jp2->j2k_codestream_length = j2k_codestream_length;
361
362   box.length = 8 + jp2->j2k_codestream_length;
363   cio_seek(cio, box.init_pos);
364   cio_write(cio, box.length, 4);  /* L */
365   cio_seek(cio, box.init_pos + box.length);
366
367   return box.length;
368 }
369
370 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
371   opj_jp2_box_t box;
372
373   opj_common_ptr cinfo = jp2->cinfo;
374
375   jp2_read_boxhdr(cinfo, cio, &box);
376   do {
377     if(JP2_JP2C != box.type) {
378       cio_skip(cio, box.length - 8);
379       jp2_read_boxhdr(cinfo, cio, &box);
380     }
381   } while(JP2_JP2C != box.type);
382
383   *j2k_codestream_offset = cio_tell(cio);
384   *j2k_codestream_length = box.length - 8;
385
386   return true;
387 }
388
389 static void jp2_write_jp(opj_cio_t *cio) {
390   opj_jp2_box_t box;
391
392   box.init_pos = cio_tell(cio);
393   cio_skip(cio, 4);
394   cio_write(cio, JP2_JP, 4);    /* JP2 signature */
395   cio_write(cio, 0x0d0a870a, 4);
396
397   box.length = cio_tell(cio) - box.init_pos;
398   cio_seek(cio, box.init_pos);
399   cio_write(cio, box.length, 4);  /* L */
400   cio_seek(cio, box.init_pos + box.length);
401 }
402
403 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
404   opj_jp2_box_t box;
405
406   opj_common_ptr cinfo = jp2->cinfo;
407
408   jp2_read_boxhdr(cinfo, cio, &box);
409   if (JP2_JP != box.type) {
410     opg_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
411     return false;
412   }
413   if (0x0d0a870a != cio_read(cio, 4)) {
414     opg_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
415     return false;
416   }
417   if (cio_tell(cio) - box.init_pos != box.length) {
418     opg_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
419     return false;
420   }
421
422   return true;
423 }
424
425
426 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) {
427   if (!jp2_read_jp(jp2, cio))
428     return false;
429   if (!jp2_read_ftyp(jp2, cio))
430     return false;
431   if (!jp2_read_jp2h(jp2, cio))
432     return false;
433   if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
434     return false;
435   
436   return true;
437 }
438
439 /* ----------------------------------------------------------------------- */
440 /* JP2 decoder interface                                             */
441 /* ----------------------------------------------------------------------- */
442
443 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
444   opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
445   if(jp2) {
446     jp2->cinfo = cinfo;
447     /* create the J2K codec */
448     jp2->j2k = j2k_create_decompress(cinfo);
449     if(jp2->j2k == NULL) {
450       jp2_destroy_decompress(jp2);
451       return NULL;
452     }
453   }
454   return jp2;
455 }
456
457 void jp2_destroy_decompress(opj_jp2_t *jp2) {
458   if(jp2) {
459     /* destroy the J2K codec */
460     j2k_destroy_decompress(jp2->j2k);
461
462     if(jp2->comps) {
463       opj_free(jp2->comps);
464     }
465     if(jp2->cl) {
466       opj_free(jp2->cl);
467     }
468     opj_free(jp2);
469   }
470 }
471
472 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
473   /* setup the J2K codec */
474   j2k_setup_decoder(jp2->j2k, parameters);
475   /* further JP2 initializations go here */
476 }
477
478 opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio) {
479   opj_common_ptr cinfo;
480   opj_image_t *image = NULL;
481
482   if(!jp2 || !cio) {
483     return NULL;
484   }
485
486   cinfo = jp2->cinfo;
487
488   /* JP2 decoding */
489   if(!jp2_read_struct(jp2, cio)) {
490     opg_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
491     return NULL;
492   }
493
494   /* J2K decoding */
495   image = j2k_decode(jp2->j2k, cio);
496   if(!image) {
497     opg_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
498   }
499
500   return image;
501 }
502
503 /* ----------------------------------------------------------------------- */
504 /* JP2 encoder interface                                             */
505 /* ----------------------------------------------------------------------- */
506
507 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
508   opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
509   if(jp2) {
510     jp2->cinfo = cinfo;
511     /* create the J2K codec */
512     jp2->j2k = j2k_create_compress(cinfo);
513     if(jp2->j2k == NULL) {
514       jp2_destroy_compress(jp2);
515       return NULL;
516     }
517   }
518   return jp2;
519 }
520
521 void jp2_destroy_compress(opj_jp2_t *jp2) {
522   if(jp2) {
523     /* destroy the J2K codec */
524     j2k_destroy_compress(jp2->j2k);
525
526     if(jp2->comps) {
527       opj_free(jp2->comps);
528     }
529     if(jp2->cl) {
530       opj_free(jp2->cl);
531     }
532     opj_free(jp2);
533   }
534 }
535
536 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
537   int i;
538   int depth_0, sign;
539
540   if(!jp2 || !parameters || !image)
541     return;
542
543   /* setup the J2K codec */
544   /* ------------------- */
545
546   j2k_setup_encoder(jp2->j2k, parameters, image);
547
548   /* setup the JP2 codec */
549   /* ------------------- */
550   
551   /* Profile box */
552
553   jp2->brand = JP2_JP2; /* BR */
554   jp2->minversion = 0;  /* MinV */
555   jp2->numcl = 1;
556   jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
557   jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
558
559   /* Image Header box */
560
561   jp2->image = image;
562
563   jp2->numcomps = image->numcomps;  /* NC */
564   jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
565   jp2->h = image->y1 - image->y0;   /* HEIGHT */
566   jp2->w = image->x1 - image->x0;   /* WIDTH */
567   /* BPC */
568   depth_0 = image->comps[0].prec - 1;
569   sign = image->comps[0].sgnd;
570   jp2->bpc = depth_0 + (sign << 7);
571   for (i = 1; i < image->numcomps; i++) {
572     int depth = image->comps[i].prec - 1;
573     sign = image->comps[i].sgnd;
574     if (depth_0 != depth)
575       jp2->bpc = 255;
576   }
577   jp2->C = 7;     /* C : Always 7 */
578   jp2->UnkC = 0;    /* UnkC, colorspace specified in colr box */
579   jp2->IPR = 0;   /* IPR, no intellectual property */
580   
581   /* BitsPerComponent box */
582
583   for (i = 0; i < image->numcomps; i++) {
584     jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
585   }
586
587   /* Colour Specification box */
588
589   if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
590     jp2->meth = 1;  /* METH: Enumerated colourspace */
591   } else {
592     jp2->meth = 2;  /* METH: Restricted ICC profile */
593   }
594   if (jp2->meth == 1) {
595     if (image->color_space == 1)
596       jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
597     else if (image->color_space == 2)
598       jp2->enumcs = 17; /* greyscale */
599     else if (image->color_space == 3)
600       jp2->enumcs = 18; /* YUV */
601   } else {
602     jp2->enumcs = 0;    /* PROFILE (??) */
603   }
604   jp2->precedence = 0;  /* PRECEDENCE */
605   jp2->approx = 0;    /* APPROX */
606
607 }
608
609 bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
610
611   /* JP2 encoding */
612
613   /* JPEG 2000 Signature box */
614   jp2_write_jp(cio);
615   /* File Type box */
616   jp2_write_ftyp(jp2, cio);
617   /* JP2 Header box */
618   jp2_write_jp2h(jp2, cio);
619
620   /* J2K encoding */
621
622   if(!jp2_write_jp2c(jp2, cio, index)) {
623     opg_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
624     return false;
625   }
626
627   return true;
628 }
629
630