]> Creatis software - gdcm.git/blob - src/gdcmjasper/src/libjasper/jp2/jp2_dec.c
590e28e8d2d3679f9d030967f05f8cb7fcbdac0f
[gdcm.git] / src / gdcmjasper / src / libjasper / jp2 / jp2_dec.c
1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  *   British Columbia.
4  * Copyright (c) 2001-2003 Michael David Adams.
5  * All rights reserved.
6  */
7
8 /* __START_OF_JASPER_LICENSE__
9  * 
10  * JasPer License Version 2.0
11  * 
12  * Copyright (c) 1999-2000 Image Power, Inc.
13  * Copyright (c) 1999-2000 The University of British Columbia
14  * Copyright (c) 2001-2003 Michael David Adams
15  * 
16  * All rights reserved.
17  * 
18  * Permission is hereby granted, free of charge, to any person (the
19  * "User") obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without restriction,
21  * including without limitation the rights to use, copy, modify, merge,
22  * publish, distribute, and/or sell copies of the Software, and to permit
23  * persons to whom the Software is furnished to do so, subject to the
24  * following conditions:
25  * 
26  * 1.  The above copyright notices and this permission notice (which
27  * includes the disclaimer below) shall be included in all copies or
28  * substantial portions of the Software.
29  * 
30  * 2.  The name of a copyright holder shall not be used to endorse or
31  * promote products derived from the Software without specific prior
32  * written permission.
33  * 
34  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35  * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36  * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37  * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39  * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
40  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
45  * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46  * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47  * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48  * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49  * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
50  * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51  * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
52  * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53  * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55  * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56  * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57  * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58  * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59  * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60  * 
61  * __END_OF_JASPER_LICENSE__
62  */
63
64 /*
65  * JP2 Library
66  *
67  * $Id: jp2_dec.c,v 1.2 2005/06/09 22:09:40 malaterre Exp $
68  */
69
70 /******************************************************************************\
71 * Includes.
72 \******************************************************************************/
73
74 #include "jasper/jas_image.h"
75 #include "jasper/jas_stream.h"
76 #include "jasper/jas_math.h"
77 #include "jasper/jas_debug.h"
78 #include "jasper/jas_malloc.h"
79 #include "jasper/jas_version.h"
80
81 #include "jp2_cod.h"
82 #include "jp2_dec.h"
83
84 #define  JP2_VALIDATELEN  (JAS_MIN(JP2_JP_LEN + 16, JAS_STREAM_MAXPUTBACK))
85
86 static jp2_dec_t *jp2_dec_create(void);
87 static void jp2_dec_destroy(jp2_dec_t *dec);
88 static int jp2_getcs(jp2_colr_t *colr);
89 static int fromiccpcs(int cs);
90 static int jp2_getct(int colorspace, int type, int assoc);
91
92 /******************************************************************************\
93 * Functions.
94 \******************************************************************************/
95
96 jas_image_t *jp2_decode(jas_stream_t *in, char *optstr)
97 {
98   jp2_box_t *box;
99   int found;
100   jas_image_t *image;
101   jp2_dec_t *dec;
102   bool samedtype;
103   int dtype;
104   unsigned int i;
105   jp2_cmap_t *cmapd;
106   jp2_pclr_t *pclrd;
107   jp2_cdef_t *cdefd;
108   unsigned int channo;
109   int newcmptno;
110   int_fast32_t *lutents;
111 #if 0
112   jp2_cdefchan_t *cdefent;
113   int cmptno;
114 #endif
115   jp2_cmapent_t *cmapent;
116   jas_icchdr_t icchdr;
117   jas_iccprof_t *iccprof;
118
119   dec = 0;
120   box = 0;
121   image = 0;
122
123   if (!(dec = jp2_dec_create())) {
124     goto error;
125   }
126
127   /* Get the first box.  This should be a JP box. */
128   if (!(box = jp2_box_get(in))) {
129     jas_eprintf("error: cannot get box\n");
130     goto error;
131   }
132   if (box->type != JP2_BOX_JP) {
133     jas_eprintf("error: expecting signature box\n");
134     goto error;
135   }
136   if (box->data.jp.magic != JP2_JP_MAGIC) {
137     jas_eprintf("incorrect magic number\n");
138     goto error;
139   }
140   jp2_box_destroy(box);
141   box = 0;
142
143   /* Get the second box.  This should be a FTYP box. */
144   if (!(box = jp2_box_get(in))) {
145     goto error;
146   }
147   if (box->type != JP2_BOX_FTYP) {
148     jas_eprintf("expecting file type box\n");
149     goto error;
150   }
151   jp2_box_destroy(box);
152   box = 0;
153
154   /* Get more boxes... */
155   found = 0;
156   while ((box = jp2_box_get(in))) {
157     if (jas_getdbglevel() >= 1) {
158       fprintf(stderr, "box type %s\n", box->info->name);
159     }
160     switch (box->type) {
161     case JP2_BOX_JP2C:
162       found = 1;
163       break;
164     case JP2_BOX_IHDR:
165       if (!dec->ihdr) {
166         dec->ihdr = box;
167         box = 0;
168       }
169       break;
170     case JP2_BOX_BPCC:
171       if (!dec->bpcc) {
172         dec->bpcc = box;
173         box = 0;
174       }
175       break;
176     case JP2_BOX_CDEF:
177       if (!dec->cdef) {
178         dec->cdef = box;
179         box = 0;
180       }
181       break;
182     case JP2_BOX_PCLR:
183       if (!dec->pclr) {
184         dec->pclr = box;
185         box = 0;
186       }
187       break;
188     case JP2_BOX_CMAP:
189       if (!dec->cmap) {
190         dec->cmap = box;
191         box = 0;
192       }
193       break;
194     case JP2_BOX_COLR:
195       if (!dec->colr) {
196         dec->colr = box;
197         box = 0;
198       }
199       break;
200     }
201     if (box) {
202       jp2_box_destroy(box);
203       box = 0;
204     }
205     if (found) {
206       break;
207     }
208   }
209
210   if (!found) {
211     jas_eprintf("error: no code stream found\n");
212     goto error;
213   }
214
215   if (!(dec->image = jpc_decode(in, optstr))) {
216     jas_eprintf("error: cannot decode code stream\n");
217     goto error;
218   }
219
220   /* An IHDR box must be present. */
221   if (!dec->ihdr) {
222     jas_eprintf("error: missing IHDR box\n");
223     goto error;
224   }
225
226   /* Does the number of components indicated in the IHDR box match
227     the value specified in the code stream? */
228   if (dec->ihdr->data.ihdr.numcmpts != JAS_CAST(uint, jas_image_numcmpts(dec->image))) {
229     jas_eprintf("warning: number of components mismatch\n");
230   }
231
232   /* At least one component must be present. */
233   if (!jas_image_numcmpts(dec->image)) {
234     jas_eprintf("error: no components\n");
235     goto error;
236   }
237
238   /* Determine if all components have the same data type. */
239   samedtype = true;
240   dtype = jas_image_cmptdtype(dec->image, 0);
241   for (i = 1; i < JAS_CAST(uint, jas_image_numcmpts(dec->image)); ++i) {
242     if (jas_image_cmptdtype(dec->image, i) != dtype) {
243       samedtype = false;
244       break;
245     }
246   }
247
248   /* Is the component data type indicated in the IHDR box consistent
249     with the data in the code stream? */
250   if ((samedtype && dec->ihdr->data.ihdr.bpc != JP2_DTYPETOBPC(dtype)) ||
251     (!samedtype && dec->ihdr->data.ihdr.bpc != JP2_IHDR_BPCNULL)) {
252     jas_eprintf("warning: component data type mismatch\n");
253   }
254
255   /* Is the compression type supported? */
256   if (dec->ihdr->data.ihdr.comptype != JP2_IHDR_COMPTYPE) {
257     jas_eprintf("error: unsupported compression type\n");
258     goto error;
259   }
260
261   if (dec->bpcc) {
262     /* Is the number of components indicated in the BPCC box
263       consistent with the code stream data? */
264     if (dec->bpcc->data.bpcc.numcmpts != JAS_CAST(uint, jas_image_numcmpts(
265       dec->image))) {
266       jas_eprintf("warning: number of components mismatch\n");
267     }
268     /* Is the component data type information indicated in the BPCC
269       box consistent with the code stream data? */
270     if (!samedtype) {
271       for (i = 0; i < JAS_CAST(uint, jas_image_numcmpts(dec->image)); ++i) {
272         if (jas_image_cmptdtype(dec->image, i) != JP2_BPCTODTYPE(dec->bpcc->data.bpcc.bpcs[i])) {
273           jas_eprintf("warning: component data type mismatch\n");
274         }
275       }
276     } else {
277       jas_eprintf("warning: superfluous BPCC box\n");
278     }
279   }
280
281   /* A COLR box must be present. */
282   if (!dec->colr) {
283     jas_eprintf("error: no COLR box\n");
284     goto error;
285   }
286
287   switch (dec->colr->data.colr.method) {
288   case JP2_COLR_ENUM:
289     jas_image_setclrspc(dec->image, jp2_getcs(&dec->colr->data.colr));
290     break;
291   case JP2_COLR_ICC:
292     iccprof = jas_iccprof_createfrombuf(dec->colr->data.colr.iccp,
293       dec->colr->data.colr.iccplen);
294     assert(iccprof);
295     jas_iccprof_gethdr(iccprof, &icchdr);
296     jas_eprintf("ICC Profile CS %08x\n", icchdr.colorspc);
297     jas_image_setclrspc(dec->image, fromiccpcs(icchdr.colorspc));
298     dec->image->cmprof_ = jas_cmprof_createfromiccprof(iccprof);
299     assert(dec->image->cmprof_);
300     jas_iccprof_destroy(iccprof);
301     break;
302   }
303
304   /* If a CMAP box is present, a PCLR box must also be present. */
305   if (dec->cmap && !dec->pclr) {
306     jas_eprintf("warning: missing PCLR box or superfluous CMAP box\n");
307     jp2_box_destroy(dec->cmap);
308     dec->cmap = 0;
309   }
310
311   /* If a CMAP box is not present, a PCLR box must not be present. */
312   if (!dec->cmap && dec->pclr) {
313     jas_eprintf("warning: missing CMAP box or superfluous PCLR box\n");
314     jp2_box_destroy(dec->pclr);
315     dec->pclr = 0;
316   }
317
318   /* Determine the number of channels (which is essentially the number
319     of components after any palette mappings have been applied). */
320   dec->numchans = dec->cmap ? dec->cmap->data.cmap.numchans : JAS_CAST(uint, jas_image_numcmpts(dec->image));
321
322   /* Perform a basic sanity check on the CMAP box if present. */
323   if (dec->cmap) {
324     for (i = 0; i < dec->numchans; ++i) {
325       /* Is the component number reasonable? */
326       if (dec->cmap->data.cmap.ents[i].cmptno >= JAS_CAST(uint, jas_image_numcmpts(dec->image))) {
327         jas_eprintf("error: invalid component number in CMAP box\n");
328         goto error;
329       }
330       /* Is the LUT index reasonable? */
331       if (dec->cmap->data.cmap.ents[i].pcol >= dec->pclr->data.pclr.numchans) {
332         jas_eprintf("error: invalid CMAP LUT index\n");
333         goto error;
334       }
335     }
336   }
337
338   /* Allocate space for the channel-number to component-number LUT. */
339   if (!(dec->chantocmptlut = jas_malloc(dec->numchans * sizeof(uint_fast16_t)))) {
340     jas_eprintf("error: no memory\n");
341     goto error;
342   }
343
344   if (!dec->cmap) {
345     for (i = 0; i < dec->numchans; ++i) {
346       dec->chantocmptlut[i] = i;
347     }
348   } else {
349     cmapd = &dec->cmap->data.cmap;
350     pclrd = &dec->pclr->data.pclr;
351     cdefd = &dec->cdef->data.cdef;
352     for (channo = 0; channo < cmapd->numchans; ++channo) {
353       cmapent = &cmapd->ents[channo];
354       if (cmapent->map == JP2_CMAP_DIRECT) {
355         dec->chantocmptlut[channo] = channo;
356       } else if (cmapent->map == JP2_CMAP_PALETTE) {
357         lutents = jas_malloc(pclrd->numlutents * sizeof(int_fast32_t));
358         for (i = 0; i < pclrd->numlutents; ++i) {
359           lutents[i] = pclrd->lutdata[cmapent->pcol + i * pclrd->numchans];
360         }
361         newcmptno = jas_image_numcmpts(dec->image);
362         jas_image_depalettize(dec->image, cmapent->cmptno, pclrd->numlutents, lutents, JP2_BPCTODTYPE(pclrd->bpc[cmapent->pcol]), newcmptno);
363         dec->chantocmptlut[channo] = newcmptno;
364         jas_free(lutents);
365 #if 0
366         if (dec->cdef) {
367           cdefent = jp2_cdef_lookup(cdefd, channo);
368           if (!cdefent) {
369             abort();
370           }
371         jas_image_setcmpttype(dec->image, newcmptno, jp2_getct(jas_image_clrspc(dec->image), cdefent->type, cdefent->assoc));
372         } else {
373         jas_image_setcmpttype(dec->image, newcmptno, jp2_getct(jas_image_clrspc(dec->image), 0, channo + 1));
374         }
375 #else
376         (void)cdefd;
377 #endif
378       }
379     }
380   }
381
382   /* Mark all components as being of unknown type. */
383
384   for (i = 0; i < JAS_CAST(uint, jas_image_numcmpts(dec->image)); ++i) {
385     jas_image_setcmpttype(dec->image, i, JAS_IMAGE_CT_UNKNOWN);
386   }
387
388   /* Determine the type of each component. */
389   if (dec->cdef) {
390     for (i = 0; i < dec->numchans; ++i) {
391       jas_image_setcmpttype(dec->image,
392         dec->chantocmptlut[dec->cdef->data.cdef.ents[i].channo],
393         jp2_getct(jas_image_clrspc(dec->image),
394         dec->cdef->data.cdef.ents[i].type, dec->cdef->data.cdef.ents[i].assoc));
395     }
396   } else {
397     for (i = 0; i < dec->numchans; ++i) {
398       jas_image_setcmpttype(dec->image, dec->chantocmptlut[i],
399         jp2_getct(jas_image_clrspc(dec->image), 0, i + 1));
400     }
401   }
402
403   /* Delete any components that are not of interest. */
404   for (i = jas_image_numcmpts(dec->image); i > 0; --i) {
405     if (jas_image_cmpttype(dec->image, i - 1) == JAS_IMAGE_CT_UNKNOWN) {
406       jas_image_delcmpt(dec->image, i - 1);
407     }
408   }
409
410   /* Ensure that some components survived. */
411   if (!jas_image_numcmpts(dec->image)) {
412     jas_eprintf("error: no components\n");
413     goto error;
414   }
415 #if 0
416 fprintf(stderr, "no of components is %d\n", jas_image_numcmpts(dec->image));
417 #endif
418
419   /* Prevent the image from being destroyed later. */
420   image = dec->image;
421   dec->image = 0;
422
423   jp2_dec_destroy(dec);
424
425   return image;
426
427 error:
428   if (box) {
429     jp2_box_destroy(box);
430   }
431   if (dec) {
432     jp2_dec_destroy(dec);
433   }
434   return 0;
435 }
436
437 int jp2_validate(jas_stream_t *in)
438 {
439   char buf[JP2_VALIDATELEN];
440   int i;
441   int n;
442 #if 0
443   jas_stream_t *tmpstream;
444   jp2_box_t *box;
445 #endif
446
447   assert(JAS_STREAM_MAXPUTBACK >= JP2_VALIDATELEN);
448
449   /* Read the validation data (i.e., the data used for detecting
450     the format). */
451   if ((n = jas_stream_read(in, buf, JP2_VALIDATELEN)) < 0) {
452     return -1;
453   }
454
455   /* Put the validation data back onto the stream, so that the
456     stream position will not be changed. */
457   for (i = n - 1; i >= 0; --i) {
458     if (jas_stream_ungetc(in, buf[i]) == EOF) {
459       return -1;
460     }
461   }
462
463   /* Did we read enough data? */
464   if (n < JP2_VALIDATELEN) {
465     return -1;
466   }
467
468   /* Is the box type correct? */
469   if (((buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]) !=
470     JP2_BOX_JP)
471   {
472     return -1;
473   }
474
475   return 0;
476 }
477
478 static jp2_dec_t *jp2_dec_create(void)
479 {
480   jp2_dec_t *dec;
481
482   if (!(dec = jas_malloc(sizeof(jp2_dec_t)))) {
483     return 0;
484   }
485   dec->ihdr = 0;
486   dec->bpcc = 0;
487   dec->cdef = 0;
488   dec->pclr = 0;
489   dec->image = 0;
490   dec->chantocmptlut = 0;
491   dec->cmap = 0;
492   dec->colr = 0;
493   return dec;
494 }
495
496 static void jp2_dec_destroy(jp2_dec_t *dec)
497 {
498   if (dec->ihdr) {
499     jp2_box_destroy(dec->ihdr);
500   }
501   if (dec->bpcc) {
502     jp2_box_destroy(dec->bpcc);
503   }
504   if (dec->cdef) {
505     jp2_box_destroy(dec->cdef);
506   }
507   if (dec->pclr) {
508     jp2_box_destroy(dec->pclr);
509   }
510   if (dec->image) {
511     jas_image_destroy(dec->image);
512   }
513   if (dec->cmap) {
514     jp2_box_destroy(dec->cmap);
515   }
516   if (dec->colr) {
517     jp2_box_destroy(dec->colr);
518   }
519   if (dec->chantocmptlut) {
520     jas_free(dec->chantocmptlut);
521   }
522   jas_free(dec);
523 }
524
525 static int jp2_getct(int colorspace, int type, int assoc)
526 {
527   if (type == 1 && assoc == 0) {
528     return JAS_IMAGE_CT_OPACITY;
529   }
530   if (type == 0 && assoc >= 1 && assoc <= 65534) {
531     switch (colorspace) {
532     case JAS_CLRSPC_FAM_RGB:
533       switch (assoc) {
534       case JP2_CDEF_RGB_R:
535         return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R);
536         break;
537       case JP2_CDEF_RGB_G:
538         return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G);
539         break;
540       case JP2_CDEF_RGB_B:
541         return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B);
542         break;
543       }
544       break;
545     case JAS_CLRSPC_FAM_YCBCR:
546       switch (assoc) {
547       case JP2_CDEF_YCBCR_Y:
548         return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_Y);
549         break;
550       case JP2_CDEF_YCBCR_CB:
551         return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_CB);
552         break;
553       case JP2_CDEF_YCBCR_CR:
554         return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_CR);
555         break;
556       }
557       break;
558     case JAS_CLRSPC_FAM_GRAY:
559       switch (assoc) {
560       case JP2_CDEF_GRAY_Y:
561         return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
562         break;
563       }
564       break;
565     default:
566       return JAS_IMAGE_CT_COLOR(assoc - 1);
567       break;
568     }
569   }
570   return JAS_IMAGE_CT_UNKNOWN;
571 }
572
573 static int jp2_getcs(jp2_colr_t *colr)
574 {
575   if (colr->method == JP2_COLR_ENUM) {
576     switch (colr->csid) {
577     case JP2_COLR_SRGB:
578       return JAS_CLRSPC_SRGB;
579       break;
580     case JP2_COLR_SYCC:
581       return JAS_CLRSPC_SYCBCR;
582       break;
583     case JP2_COLR_SGRAY:
584       return JAS_CLRSPC_SGRAY;
585       break;
586     }
587   }
588   return JAS_CLRSPC_UNKNOWN;
589 }
590
591 static int fromiccpcs(int cs)
592 {
593   switch (cs) {
594   case ICC_CS_RGB:
595     return JAS_CLRSPC_GENRGB;
596     break;
597   case ICC_CS_YCBCR:
598     return JAS_CLRSPC_GENYCBCR;
599     break;
600   case ICC_CS_GRAY:
601     return JAS_CLRSPC_GENGRAY;
602     break;
603   }
604   return JAS_CLRSPC_UNKNOWN;
605 }