Debian Codegraph @

converter/other/jpeg2000/libjasper/jp2/jp2_dec.c

raw
1 /*
2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3 * British Columbia.
4 * Copyright (c) 2001-2002 Michael David Adams.
5 * All rights reserved.
6 */
7
8 /* __START_OF_JASPER_LICENSE__
9 *
10 * JasPer Software License
11 *
12 * IMAGE POWER JPEG-2000 PUBLIC LICENSE
13 * ************************************
14 *
15 * GRANT:
16 *
17 * Permission is hereby granted, free of charge, to any person (the "User")
18 * obtaining a copy of this software and associated documentation, to deal
19 * in the JasPer Software without restriction, including without limitation
20 * the right to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the JasPer Software (in source and binary forms),
22 * and to permit persons to whom the JasPer Software is furnished to do so,
23 * provided further that the License Conditions below are met.
24 *
25 * License Conditions
26 * ******************
27 *
28 * A. Redistributions of source code must retain the above copyright notice,
29 * and this list of conditions, and the following disclaimer.
30 *
31 * B. Redistributions in binary form must reproduce the above copyright
32 * notice, and this list of conditions, and the following disclaimer in
33 * the documentation and/or other materials provided with the distribution.
34 *
35 * C. Neither the name of Image Power, Inc. nor any other contributor
36 * (including, but not limited to, the University of British Columbia and
37 * Michael David Adams) may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
39 *
40 * D. User agrees that it shall not commence any action against Image Power,
41 * Inc., the University of British Columbia, Michael David Adams, or any
42 * other contributors (collectively "Licensors") for infringement of any
43 * intellectual property rights ("IPR") held by the User in respect of any
44 * technology that User owns or has a right to license or sublicense and
45 * which is an element required in order to claim compliance with ISO/IEC
46 * 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property
47 * rights worldwide arising under statutory or common law, and whether
48 * or not perfected, including, without limitation, all (i) patents and
49 * patent applications owned or licensable by User; (ii) rights associated
50 * with works of authorship including copyrights, copyright applications,
51 * copyright registrations, mask work rights, mask work applications,
52 * mask work registrations; (iii) rights relating to the protection of
53 * trade secrets and confidential information; (iv) any right analogous
54 * to those set forth in subsections (i), (ii), or (iii) and any other
55 * proprietary rights relating to intangible property (other than trademark,
56 * trade dress, or service mark rights); and (v) divisions, continuations,
57 * renewals, reissues and extensions of the foregoing (as and to the extent
58 * applicable) now existing, hereafter filed, issued or acquired.
59 *
60 * E. If User commences an infringement action against any Licensor(s) then
61 * such Licensor(s) shall have the right to terminate User's license and
62 * all sublicenses that have been granted hereunder by User to other parties.
63 *
64 * F. This software is for use only in hardware or software products that
65 * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license
66 * or right to this Software is granted for products that do not comply
67 * with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased
68 * from the ISO.
69 *
70 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
71 * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
72 * THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
73 * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
74 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
75 * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
76 * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING
77 * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
78 * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
79 * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
80 * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
81 * IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
82 * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
83 * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
84 * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
85 * REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
86 * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
87 * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
88 * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
89 * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
90 * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
91 * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
92 * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
93 * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
94 * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
95 * OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
96 * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
97 * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
98 * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
99 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
100 * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
101 * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
102 * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
103 * ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
104 * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT
105 * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
106 * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
107 * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
108 * NOTICE SPECIFIED IN THIS SECTION.
109 *
110 * __END_OF_JASPER_LICENSE__
111 */
112
113 #include "jasper/jas_image.h"
114 #include "jasper/jas_stream.h"
115 #include "jasper/jas_math.h"
116 #include "jasper/jas_debug.h"
117 #include "jasper/jas_malloc.h"
118 #include "jasper/jas_version.h"
119
120 #include "netpbm/nstring.h"
121
122 #include "jp2_cod.h"
123 #include "jp2_dec.h"
124
125 #define JP2_VALIDATELEN (JAS_MIN(JP2_JP_LEN + 16, JAS_STREAM_MAXPUTBACK))
126
127
128
129 static jp2_dec_t *
130 jp2_dec_create(void) {
131
132 jp2_dec_t *dec;
133
134 dec = jas_malloc(sizeof(jp2_dec_t));
135 if (dec) {
136 dec->ihdr = 0;
137 dec->bpcc = 0;
138 dec->cdef = 0;
139 dec->pclr = 0;
140 dec->image = 0;
141 dec->chantocmptlut = 0;
142 dec->cmap = 0;
143 dec->colr = 0;
144 }
145 return dec;
146 }
147
148
149
150 static void
151 jp2_dec_destroy(jp2_dec_t *dec) {
152
153 if (dec->ihdr)
154 jp2_box_destroy(dec->ihdr);
155
156 if (dec->bpcc)
157 jp2_box_destroy(dec->bpcc);
158
159 if (dec->cdef)
160 jp2_box_destroy(dec->cdef);
161
162 if (dec->pclr)
163 jp2_box_destroy(dec->pclr);
164
165 if (dec->image)
166 jas_image_destroy(dec->image);
167
168 if (dec->cmap)
169 jp2_box_destroy(dec->cmap);
170
171 if (dec->colr)
172 jp2_box_destroy(dec->colr);
173
174 if (dec->chantocmptlut)
175 jas_free(dec->chantocmptlut);
176
177 jas_free(dec);
178 }
179
180
181
182 static int
183 jp2_getct(int colorspace, int type, int assoc) {
184
185 if (type == 1 && assoc == 0)
186 return JAS_IMAGE_CT_OPACITY;
187
188 if (type == 0 && assoc >= 1 && assoc <= 65534) {
189 switch (colorspace) {
190 case JAS_IMAGE_CS_RGB:
191 switch (assoc) {
192 case JP2_CDEF_RGB_R:
193 return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R);
194 break;
195 case JP2_CDEF_RGB_G:
196 return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G);
197 break;
198 case JP2_CDEF_RGB_B:
199 return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B);
200 break;
201 }
202 break;
203 case JAS_IMAGE_CS_YCBCR:
204 switch (assoc) {
205 case JP2_CDEF_YCBCR_Y:
206 return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_Y);
207 break;
208 case JP2_CDEF_YCBCR_CB:
209 return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CB);
210 break;
211 case JP2_CDEF_YCBCR_CR:
212 return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CR);
213 break;
214 }
215 break;
216 case JAS_IMAGE_CS_GRAY:
217 switch (assoc) {
218 case JP2_CDEF_GRAY_Y:
219 return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y);
220 break;
221 }
222 break;
223 #if 0
224 case JAS_IMAGE_CS_ICC:
225 #endif
226 default:
227 return JAS_IMAGE_CT_COLOR(assoc - 1);
228 break;
229 }
230 }
231 return JAS_IMAGE_CT_UNKNOWN;
232 }
233
234
235
236 static int
237 jp2_getcs(jp2_colr_t *colr) {
238
239 if (colr->method == JP2_COLR_ENUM) {
240 switch (colr->csid) {
241 case JP2_COLR_SRGB:
242 return JAS_IMAGE_CS_RGB;
243 break;
244 case JP2_COLR_SYCC:
245 return JAS_IMAGE_CS_YCBCR;
246 break;
247 case JP2_COLR_SGRAY:
248 return JAS_IMAGE_CS_GRAY;
249 break;
250 }
251 }
252 return JAS_IMAGE_CS_UNKNOWN;
253 }
254
255
256
257 static int
258 fromiccpcs(int cs) {
259
260 switch (cs) {
261 case ICC_CS_RGB:
262 return JAS_IMAGE_CS_RGB;
263 break;
264 case ICC_CS_YCBCR:
265 return JAS_IMAGE_CS_YCBCR;
266 break;
267 case ICC_CS_GRAY:
268 return JAS_IMAGE_CS_GRAY;
269 break;
270 }
271 return JAS_IMAGE_CS_UNKNOWN;
272 }
273
274
275
276 void
277 jp2_decode(jas_stream_t * const in,
278 const char * const optstr,
279 jas_image_t ** const imagePP,
280 const char ** const errorP) {
281
282 jp2_box_t *box;
283 int found;
284 jas_image_t *image;
285 jp2_dec_t *dec;
286 bool samedtype;
287 int dtype;
288 int i;
289 jp2_cmap_t *cmapd;
290 jp2_pclr_t *pclrd;
291 int channo;
292 int newcmptno;
293 int_fast32_t *lutents;
294 jp2_cmapent_t *cmapent;
295 unsigned char *iccp;
296 int cs;
297
298 dec = 0;
299 box = 0;
300 image = 0;
301
302 if (!(dec = jp2_dec_create())) {
303 pm_asprintf(errorP, "jp2_dec_create failed");
304 goto cleanup;
305 }
306
307 /* Get the first box. This should be a JP box. */
308 if (!(box = jp2_box_get(in))) {
309 pm_asprintf(errorP, "cannot get box");
310 goto cleanup;
311 }
312 if (box->type != JP2_BOX_JP) {
313 pm_asprintf(errorP, "expecting signature box");
314 goto cleanup;
315 }
316 if (box->data.jp.magic != JP2_JP_MAGIC) {
317 pm_asprintf(errorP, "incorrect magic number");
318 goto cleanup;
319 }
320 jp2_box_destroy(box);
321 box = 0;
322
323 /* Get the second box. This should be a FTYP box. */
324 if (!(box = jp2_box_get(in))) {
325 pm_asprintf(errorP, "cannot get second box");
326 goto cleanup;
327 }
328 if (box->type != JP2_BOX_FTYP) {
329 pm_asprintf(errorP, "expecting file type box");
330 goto cleanup;
331 }
332 jp2_box_destroy(box);
333 box = 0;
334
335 /* Get more boxes... */
336 found = 0;
337 while ((box = jp2_box_get(in))) {
338 if (jas_getdbglevel() >= 1) {
339 jas_eprintf("box type '%s'\n", box->info->name);
340 }
341 switch (box->type) {
342 case JP2_BOX_JP2C:
343 found = 1;
344 break;
345 case JP2_BOX_IHDR:
346 if (!dec->ihdr) {
347 dec->ihdr = box;
348 box = 0;
349 }
350 break;
351 case JP2_BOX_BPCC:
352 if (!dec->bpcc) {
353 dec->bpcc = box;
354 box = 0;
355 }
356 break;
357 case JP2_BOX_CDEF:
358 if (!dec->cdef) {
359 dec->cdef = box;
360 box = 0;
361 }
362 break;
363 case JP2_BOX_PCLR:
364 if (!dec->pclr) {
365 dec->pclr = box;
366 box = 0;
367 }
368 break;
369 case JP2_BOX_CMAP:
370 if (!dec->cmap) {
371 dec->cmap = box;
372 box = 0;
373 }
374 break;
375 case JP2_BOX_COLR:
376 if (!dec->colr) {
377 dec->colr = box;
378 box = 0;
379 }
380 break;
381 }
382 if (box) {
383 jp2_box_destroy(box);
384 box = 0;
385 }
386 if (found) {
387 break;
388 }
389 }
390
391 if (!found) {
392 pm_asprintf(errorP, "no code stream found");
393 goto cleanup;
394 }
395
396 {
397 const char * decodeError;
398 jpc_decode(in, optstr, &dec->image, &decodeError);
399 if (decodeError) {
400 pm_asprintf(errorP, "cannot decode code stream. %s", decodeError);
401 pm_strfree(decodeError);
402 goto cleanup;
403 }
404 }
405
406 /* An IHDR box must be present. */
407 if (!dec->ihdr) {
408 pm_asprintf(errorP, "missing IHDR box");
409 goto cleanup;
410 }
411
412 /* Does the number of components indicated in the IHDR box match
413 the value specified in the code stream? */
414 if (dec->ihdr->data.ihdr.numcmpts != jas_image_numcmpts(dec->image)) {
415 jas_eprintf("warning: number of components mismatch\n");
416 }
417
418 /* At least one component must be present. */
419 if (!jas_image_numcmpts(dec->image)) {
420 pm_asprintf(errorP, "no components");
421 goto cleanup;
422 }
423
424 /* Determine if all components have the same data type. */
425 samedtype = true;
426 dtype = jas_image_cmptdtype(dec->image, 0);
427 for (i = 1; i < jas_image_numcmpts(dec->image); ++i) {
428 if (jas_image_cmptdtype(dec->image, i) != dtype) {
429 samedtype = false;
430 break;
431 }
432 }
433
434 /* Is the component data type indicated in the IHDR box consistent
435 with the data in the code stream? */
436 if ((samedtype && dec->ihdr->data.ihdr.bpc != JP2_DTYPETOBPC(dtype)) ||
437 (!samedtype && dec->ihdr->data.ihdr.bpc != JP2_IHDR_BPCNULL)) {
438 jas_eprintf("warning: component data type mismatch\n");
439 }
440
441 /* Can we handle the compression type? */
442 if (dec->ihdr->data.ihdr.comptype != JP2_IHDR_COMPTYPE) {
443 pm_asprintf(errorP, "not capable of this compression type");
444 goto cleanup;
445 }
446
447 if (dec->bpcc) {
448 /* Is the number of components indicated in the BPCC box
449 consistent with the code stream data? */
450 if (dec->bpcc->data.bpcc.numcmpts != jas_image_numcmpts(
451 dec->image)) {
452 jas_eprintf("warning: number of components mismatch\n");
453 }
454 /* Is the component data type information indicated in the BPCC
455 box consistent with the code stream data? */
456 if (!samedtype) {
457 for (i = 0; i < jas_image_numcmpts(dec->image); ++i) {
458 if (jas_image_cmptdtype(dec->image, i) !=
459 JP2_BPCTODTYPE(dec->bpcc->data.bpcc.bpcs[i])) {
460 jas_eprintf("warning: component data type mismatch\n");
461 }
462 }
463 } else {
464 jas_eprintf("warning: superfluous BPCC box\n");
465 }
466 }
467
468 /* A COLR box must be present. */
469 if (!dec->colr) {
470 pm_asprintf(errorP, "no COLR box");
471 goto cleanup;
472 }
473
474 switch (dec->colr->data.colr.method) {
475 case JP2_COLR_ENUM:
476 jas_image_setcolorspace(dec->image, jp2_getcs(&dec->colr->data.colr));
477 break;
478 case JP2_COLR_ICC:
479 if (dec->colr->data.colr.iccplen < 128) {
480 abort();
481 }
482 iccp = dec->colr->data.colr.iccp;
483 cs = (iccp[16] << 24) | (iccp[17] << 16) | (iccp[18] << 8) |
484 iccp[19];
485 if (jas_getdbglevel() > 1)
486 jas_eprintf("ICC Profile CS %08x\n", cs);
487 jas_image_setcolorspace(dec->image, fromiccpcs(cs));
488 break;
489 }
490
491 /* If a CMAP box is present, a PCLR box must also be present. */
492 if (dec->cmap && !dec->pclr) {
493 jas_eprintf("warning: missing PCLR box or superfluous CMAP box\n");
494 jp2_box_destroy(dec->cmap);
495 dec->cmap = 0;
496 }
497
498 /* If a CMAP box is not present, a PCLR box must not be present. */
499 if (!dec->cmap && dec->pclr) {
500 jas_eprintf("warning: missing CMAP box or superfluous PCLR box\n");
501 jp2_box_destroy(dec->pclr);
502 dec->pclr = 0;
503 }
504
505 /* Determine the number of channels (which is essentially the number
506 of components after any palette mappings have been applied). */
507 dec->numchans = dec->cmap ?
508 dec->cmap->data.cmap.numchans :
509 jas_image_numcmpts(dec->image);
510
511 /* Perform a basic sanity check on the CMAP box if present. */
512 if (dec->cmap) {
513 for (i = 0; i < dec->numchans; ++i) {
514 /* Is the component number reasonable? */
515 if (dec->cmap->data.cmap.ents[i].cmptno >=
516 jas_image_numcmpts(dec->image)) {
517 pm_asprintf(errorP, "invalid component number in CMAP box");
518 goto cleanup;
519 }
520 /* Is the LUT index reasonable? */
521 if (dec->cmap->data.cmap.ents[i].pcol >=
522 dec->pclr->data.pclr.numchans) {
523 pm_asprintf(errorP, "invalid CMAP LUT index");
524 goto cleanup;
525 }
526 }
527 }
528
529 /* Allocate space for the channel-number to component-number LUT. */
530 dec->chantocmptlut = jas_malloc(dec->numchans * sizeof(uint_fast16_t));
531 if (!dec->chantocmptlut) {
532 pm_asprintf(errorP, "no memory");
533 goto cleanup;
534 }
535
536 if (!dec->cmap) {
537 for (i = 0; i < dec->numchans; ++i) {
538 dec->chantocmptlut[i] = i;
539 }
540 } else {
541 cmapd = &dec->cmap->data.cmap;
542 pclrd = &dec->pclr->data.pclr;
543 for (channo = 0; channo < cmapd->numchans; ++channo) {
544 cmapent = &cmapd->ents[channo];
545 if (cmapent->map == JP2_CMAP_DIRECT) {
546 dec->chantocmptlut[channo] = channo;
547 } else if (cmapent->map == JP2_CMAP_PALETTE) {
548 lutents = jas_malloc(pclrd->numlutents * sizeof(int_fast32_t));
549 for (i = 0; i < pclrd->numlutents; ++i) {
550 lutents[i] =
551 pclrd->lutdata[cmapent->pcol + i * pclrd->numchans];
552 }
553 newcmptno = jas_image_numcmpts(dec->image);
554 jas_image_depalettize(
555 dec->image, cmapent->cmptno, pclrd->numlutents, lutents,
556 JP2_BPCTODTYPE(pclrd->bpc[cmapent->pcol]), newcmptno);
557 dec->chantocmptlut[channo] = newcmptno;
558 jas_free(lutents);
559 #if 0
560 if (dec->cdef) {
561 jp2_cdef_t * const cdefd = &dec->cdef->data.cdef;
562 cdefent = jp2_cdef_lookup(cdefd, channo);
563 if (!cdefent) {
564 abort();
565 }
566 jas_image_setcmpttype(
567 dec->image, newcmptno,
568 jp2_getct(jas_image_colorspace(dec->image),
569 cdefent->type, cdefent->assoc));
570 } else {
571 jas_image_setcmpttype(
572 dec->image, newcmptno,
573 jp2_getct(jas_image_colorspace(dec->image),
574 0,
575 channo + 1));
576 }
577 #endif
578 } else {
579 pm_asprintf(errorP, "invalid MTYP in CMAP box");
580 goto cleanup;
581 }
582 }
583 }
584
585 /* Mark all components as being of unknown type. */
586
587 for (i = 0; i < jas_image_numcmpts(dec->image); ++i) {
588 jas_image_setcmpttype(dec->image, i, JAS_IMAGE_CT_UNKNOWN);
589 }
590
591 /* Determine the type of each component. */
592 if (dec->cdef) {
593 unsigned int i;
594 for (i = 0; i < dec->numchans; ++i) {
595 jas_image_setcmpttype(
596 dec->image,
597 dec->chantocmptlut[dec->cdef->data.cdef.ents[i].channo],
598 jp2_getct(jas_image_colorspace(dec->image),
599 dec->cdef->data.cdef.ents[i].type,
600 dec->cdef->data.cdef.ents[i].assoc));
601 }
602 } else {
603 unsigned int i;
604 for (i = 0; i < dec->numchans; ++i) {
605 jas_image_setcmpttype(dec->image, dec->chantocmptlut[i],
606 jp2_getct(jas_image_colorspace(dec->image), 0, i + 1));
607 }
608 }
609
610 /* Delete any components that are not of interest. */
611 for (i = jas_image_numcmpts(dec->image) - 1; i >= 0; --i) {
612 if (jas_image_cmpttype(dec->image, i) == JAS_IMAGE_CT_UNKNOWN) {
613 jas_image_delcmpt(dec->image, i);
614 }
615 }
616
617 /* Ensure that some components survived. */
618 if (!jas_image_numcmpts(dec->image)) {
619 pm_asprintf(errorP, "no components");
620 goto cleanup;
621 }
622
623 /* Prevent the image from being destroyed later. */
624 image = dec->image;
625 dec->image = 0;
626
627 jp2_dec_destroy(dec);
628
629 *imagePP = image;
630 *errorP = NULL;
631 return;
632
633 cleanup:
634 if (box) {
635 jp2_box_destroy(box);
636 }
637 if (dec) {
638 jp2_dec_destroy(dec);
639 }
640 }
641
642
643
644 int
645 jp2_validate(jas_stream_t *in) {
646
647 char buf[JP2_VALIDATELEN];
648 int i;
649 int n;
650
651 assert(JAS_STREAM_MAXPUTBACK >= JP2_VALIDATELEN);
652
653 /* Read the validation data (i.e., the data used for detecting
654 the format). */
655 if ((n = jas_stream_read(in, buf, JP2_VALIDATELEN)) < 0) {
656 return -1;
657 }
658
659 /* Put the validation data back onto the stream, so that the
660 stream position will not be changed. */
661 for (i = n - 1; i >= 0; --i) {
662 if (jas_stream_ungetc(in, buf[i]) == EOF) {
663 return -1;
664 }
665 }
666
667 /* Did we read enough data? */
668 if (n < JP2_VALIDATELEN) {
669 return -1;
670 }
671
672 /* Is the box type correct? */
673 if (((buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]) !=
674 JP2_BOX_JP)
675 {
676 return -1;
677 }
678
679 return 0;
680 }
681
682
683