blob: c8667dc1af1f5f0e9b237d726eab26e69bb06b10 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% JJJ PPPP 222 %
7% J P P 2 2 %
8% J PPPP 22 %
9% J J P 2 %
10% JJ P 22222 %
11% %
12% %
13% Read/Write JPEG-2000 Image Format %
14% %
15% John Cristy %
16% Nathan Brown %
17% June 2001 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/attribute.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000048#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000049#include "MagickCore/color.h"
50#include "MagickCore/color-private.h"
51#include "MagickCore/exception.h"
52#include "MagickCore/exception-private.h"
53#include "MagickCore/image.h"
54#include "MagickCore/image-private.h"
55#include "MagickCore/list.h"
56#include "MagickCore/magick.h"
57#include "MagickCore/memory_.h"
58#include "MagickCore/monitor.h"
59#include "MagickCore/monitor-private.h"
60#include "MagickCore/option.h"
61#include "MagickCore/pixel-accessor.h"
62#include "MagickCore/profile.h"
63#include "MagickCore/quantum-private.h"
64#include "MagickCore/static.h"
65#include "MagickCore/statistic.h"
66#include "MagickCore/string_.h"
67#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000068#if defined(MAGICKCORE_JP2_DELEGATE)
69#ifndef JAS_IMAGE_CM_GRAY
70#define JAS_IMAGE_CM_GRAY JAS_IMAGE_CS_GRAY
71#endif
72#ifndef JAS_IMAGE_CM_RGB
73#define JAS_IMAGE_CM_RGB JAS_IMAGE_CS_RGB
74#endif
75#if !defined(uchar)
76#define uchar unsigned char
77#endif
78#if !defined(ushort)
79#define ushort unsigned short
80#endif
81#if !defined(uint)
82#define uint unsigned int
83#endif
cristybb503372010-05-27 20:51:26 +000084#if !defined(ssize_tssize_t)
85#define ssize_tssize_t long long
cristy3ed852e2009-09-05 21:47:34 +000086#endif
cristybb503372010-05-27 20:51:26 +000087#if !defined(ussize_tssize_t)
88#define ussize_tssize_t unsigned long long
cristy3ed852e2009-09-05 21:47:34 +000089#endif
90
cristy30faca22009-09-29 13:49:52 +000091#undef PACKAGE_NAME
92#undef PACKAGE_STRING
93#undef PACKAGE_TARNAME
94#undef PACKAGE_VERSION
cristy3ed852e2009-09-05 21:47:34 +000095#include "jasper/jasper.h"
cristy30faca22009-09-29 13:49:52 +000096#undef PACKAGE_NAME
97#undef PACKAGE_STRING
98#undef PACKAGE_TARNAME
99#undef PACKAGE_VERSION
100
cristy3ed852e2009-09-05 21:47:34 +0000101#endif
102
103/*
104 Forward declarations.
105*/
106#if defined(MAGICKCORE_JP2_DELEGATE)
107static MagickBooleanType
108 WriteJP2Image(const ImageInfo *,Image *);
cristy1e09ca22009-12-27 18:04:58 +0000109
110static volatile MagickBooleanType
111 instantiate_jp2 = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000112#endif
113
114/*
115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116% %
117% %
118% %
119% I s J P 2 %
120% %
121% %
122% %
123%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124%
125% IsJP2() returns MagickTrue if the image format type, identified by the
126% magick string, is JP2.
127%
128% The format of the IsJP2 method is:
129%
130% MagickBooleanType IsJP2(const unsigned char *magick,const size_t length)
131%
132% A description of each parameter follows:
133%
134% o magick: compare image format pattern against these bytes.
135%
136% o length: Specifies the length of the magick string.
137%
138*/
139static MagickBooleanType IsJP2(const unsigned char *magick,const size_t length)
140{
141 if (length < 9)
142 return(MagickFalse);
143 if (memcmp(magick+4,"\152\120\040\040\015",5) == 0)
144 return(MagickTrue);
145 return(MagickFalse);
146}
147
148/*
149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150% %
151% %
152% %
153% I s J P C %
154% %
155% %
156% %
157%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158%
159% IsJPC()() returns MagickTrue if the image format type, identified by the
160% magick string, is JPC.
161%
162% The format of the IsJPC method is:
163%
164% MagickBooleanType IsJPC(const unsigned char *magick,const size_t length)
165%
166% A description of each parameter follows:
167%
168% o magick: compare image format pattern against these bytes.
169%
170% o length: Specifies the length of the magick string.
171%
172*/
173static MagickBooleanType IsJPC(const unsigned char *magick,const size_t length)
174{
175 if (length < 2)
176 return(MagickFalse);
177 if (memcmp(magick,"\377\117",2) == 0)
178 return(MagickTrue);
179 return(MagickFalse);
180}
181
182/*
183%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184% %
185% %
186% %
187% R e a d J P 2 I m a g e %
188% %
189% %
190% %
191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192%
193% ReadJP2Image() reads a JPEG 2000 Image file (JP2) or JPEG 2000
194% codestream (JPC) image file and returns it. It allocates the memory
195% necessary for the new Image structure and returns a pointer to the new
196% image or set of images.
197%
198% JP2 support is originally written by Nathan Brown, nathanbrown@letu.edu.
199%
200% The format of the ReadJP2Image method is:
201%
202% Image *ReadJP2Image(const ImageInfo *image_info,
203% ExceptionInfo *exception)
204%
205% A description of each parameter follows:
206%
207% o image_info: the image info.
208%
209% o exception: return any errors or warnings in this structure.
210%
211*/
212#if defined(MAGICKCORE_JP2_DELEGATE)
213
214typedef struct _StreamManager
215{
216 jas_stream_t
217 *stream;
218
219 Image
220 *image;
221} StreamManager;
222
223static int BlobRead(jas_stream_obj_t *object,char *buffer,const int length)
224{
225 ssize_t
226 count;
227
228 StreamManager
229 *source;
230
231 source=(StreamManager *) object;
232 count=ReadBlob(source->image,(size_t) length,(unsigned char *) buffer);
233 return((int) count);
234}
235
236static int BlobWrite(jas_stream_obj_t *object,char *buffer,const int length)
237{
238 ssize_t
239 count;
240
241 StreamManager
242 *source;
243
244 source=(StreamManager *) object;
245 count=WriteBlob(source->image,(size_t) length,(unsigned char *) buffer);
246 return((int) count);
247}
248
cristyf1d91242010-05-28 02:23:19 +0000249static long BlobSeek(jas_stream_obj_t *object,long offset,int origin)
cristy3ed852e2009-09-05 21:47:34 +0000250{
251 StreamManager
252 *source;
253
254 source=(StreamManager *) object;
cristyf1d91242010-05-28 02:23:19 +0000255 return((long) SeekBlob(source->image,offset,origin));
cristy3ed852e2009-09-05 21:47:34 +0000256}
257
258static int BlobClose(jas_stream_obj_t *object)
259{
260 StreamManager
261 *source;
262
263 source=(StreamManager *) object;
264 (void) CloseBlob(source->image);
cristyb41ee102010-10-04 16:46:15 +0000265 free(source);
266 source=(StreamManager *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000267 return(0);
268}
269
270static inline size_t MagickMax(const size_t x,const size_t y)
271{
272 if (x > y)
273 return(x);
274 return(y);
275}
276
277static inline size_t MagickMin(const size_t x,const size_t y)
278{
279 if (x < y)
280 return(x);
281 return(y);
282}
283
284static jas_stream_t *JP2StreamManager(Image *image)
285{
286 static jas_stream_ops_t
287 StreamOperators =
288 {
289 BlobRead,
290 BlobWrite,
291 BlobSeek,
292 BlobClose
293 };
294
295 jas_stream_t
296 *stream;
297
298 StreamManager
299 *source;
300
301 stream=(jas_stream_t *) jas_malloc(sizeof(*stream));
302 if (stream == (jas_stream_t *) NULL)
303 return((jas_stream_t *) NULL);
304 (void) ResetMagickMemory(stream,0,sizeof(*stream));
305 stream->rwlimit_=(-1);
306 stream->obj_=(jas_stream_obj_t *) jas_malloc(sizeof(StreamManager));
307 if (stream->obj_ == (jas_stream_obj_t *) NULL)
308 return((jas_stream_t *) NULL);
309 (void) ResetMagickMemory(stream->obj_,0,sizeof(StreamManager));
310 stream->ops_=(&StreamOperators);
311 stream->openmode_=JAS_STREAM_READ | JAS_STREAM_WRITE | JAS_STREAM_BINARY;
312 stream->bufbase_=(unsigned char *) jas_malloc(JAS_STREAM_BUFSIZE+
313 JAS_STREAM_MAXPUTBACK);
314 if (stream->bufbase_ == (void *) NULL)
315 {
316 stream->bufbase_=stream->tinybuf_;
317 stream->bufsize_=1;
318 }
319 else
320 {
321 stream->bufmode_=JAS_STREAM_FREEBUF | JAS_STREAM_BUFMODEMASK;
322 stream->bufsize_=JAS_STREAM_BUFSIZE;
323 }
324 stream->bufstart_=(&stream->bufbase_[JAS_STREAM_MAXPUTBACK]);
325 stream->ptr_=stream->bufstart_;
326 stream->cnt_=0;
327 source=(StreamManager *) stream->obj_;
328 source->image=image;
329 return(stream);
330}
331
332static Image *ReadJP2Image(const ImageInfo *image_info,ExceptionInfo *exception)
333{
334 Image
335 *image;
336
cristy3ed852e2009-09-05 21:47:34 +0000337 jas_cmprof_t
338 *cm_profile;
339
340 jas_iccprof_t
341 *icc_profile;
342
343 jas_image_t
344 *jp2_image;
345
346 jas_matrix_t
347 *pixels[4];
348
349 jas_stream_t
350 *jp2_stream;
351
352 MagickBooleanType
353 status;
354
355 QuantumAny
cristy30faca22009-09-29 13:49:52 +0000356 pixel,
cristy722af032009-12-04 01:50:06 +0000357 range[4];
cristy3ed852e2009-09-05 21:47:34 +0000358
cristybb503372010-05-27 20:51:26 +0000359 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000360 i,
361 x;
362
cristy4c08aed2011-07-01 19:47:50 +0000363 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000364 *q;
365
cristybb503372010-05-27 20:51:26 +0000366 size_t
cristy3ed852e2009-09-05 21:47:34 +0000367 maximum_component_depth,
368 number_components,
cristy3ed852e2009-09-05 21:47:34 +0000369 x_step[4],
370 y_step[4];
371
cristy524222d2011-04-25 00:37:06 +0000372 ssize_t
373 components[4],
374 y;
375
cristy3ed852e2009-09-05 21:47:34 +0000376 /*
377 Open image file.
378 */
379 assert(image_info != (const ImageInfo *) NULL);
380 assert(image_info->signature == MagickSignature);
381 if (image_info->debug != MagickFalse)
382 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
383 image_info->filename);
384 assert(exception != (ExceptionInfo *) NULL);
385 assert(exception->signature == MagickSignature);
386 image=AcquireImage(image_info);
387 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
388 if (status == MagickFalse)
389 {
390 image=DestroyImageList(image);
391 return((Image *) NULL);
392 }
393 /*
394 Initialize JPEG 2000 API.
395 */
396 jp2_stream=JP2StreamManager(image);
397 if (jp2_stream == (jas_stream_t *) NULL)
398 ThrowReaderException(DelegateError,"UnableToManageJP2Stream");
399 jp2_image=jas_image_decode(jp2_stream,-1,0);
400 if (jp2_image == (jas_image_t *) NULL)
401 {
402 (void) jas_stream_close(jp2_stream);
403 ThrowReaderException(DelegateError,"UnableToDecodeImageFile");
404 }
cristy3ed852e2009-09-05 21:47:34 +0000405 switch (jas_clrspc_fam(jas_image_clrspc(jp2_image)))
406 {
407 case JAS_CLRSPC_FAM_RGB:
408 {
409 components[0]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_RGB_R);
410 components[1]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_RGB_G);
411 components[2]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_RGB_B);
412 if ((components[0] < 0) || (components[1] < 0) || (components[2] < 0))
413 {
414 (void) jas_stream_close(jp2_stream);
415 jas_image_destroy(jp2_image);
416 ThrowReaderException(CorruptImageError,"MissingImageChannel");
417 }
418 number_components=3;
419 components[3]=jas_image_getcmptbytype(jp2_image,3);
420 if (components[3] > 0)
421 {
422 image->matte=MagickTrue;
423 number_components++;
424 }
425 break;
426 }
427 case JAS_CLRSPC_FAM_GRAY:
428 {
429 components[0]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_GRAY_Y);
430 if (components[0] < 0)
431 {
432 (void) jas_stream_close(jp2_stream);
433 jas_image_destroy(jp2_image);
434 ThrowReaderException(CorruptImageError,"MissingImageChannel");
435 }
436 number_components=1;
437 break;
438 }
439 case JAS_CLRSPC_FAM_YCBCR:
440 {
441 components[0]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_YCBCR_Y);
442 components[1]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_YCBCR_CB);
443 components[2]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_YCBCR_CR);
444 if ((components[0] < 0) || (components[1] < 0) || (components[2] < 0))
445 {
446 (void) jas_stream_close(jp2_stream);
447 jas_image_destroy(jp2_image);
448 ThrowReaderException(CorruptImageError,"MissingImageChannel");
449 }
450 number_components=3;
451 components[3]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_UNKNOWN);
452 if (components[3] > 0)
453 {
454 image->matte=MagickTrue;
455 number_components++;
456 }
457 image->colorspace=YCbCrColorspace;
458 break;
459 }
460 default:
461 {
462 (void) jas_stream_close(jp2_stream);
463 jas_image_destroy(jp2_image);
464 ThrowReaderException(CoderError,"ColorspaceModelIsNotSupported");
465 }
466 }
cristy30faca22009-09-29 13:49:52 +0000467 image->columns=jas_image_width(jp2_image);
468 image->rows=jas_image_height(jp2_image);
cristy3ed852e2009-09-05 21:47:34 +0000469 image->compression=JPEG2000Compression;
cristybb503372010-05-27 20:51:26 +0000470 for (i=0; i < (ssize_t) number_components; i++)
cristy3ed852e2009-09-05 21:47:34 +0000471 {
cristybb503372010-05-27 20:51:26 +0000472 size_t
cristy3ed852e2009-09-05 21:47:34 +0000473 height,
474 width;
475
cristybb503372010-05-27 20:51:26 +0000476 width=(size_t) (jas_image_cmptwidth(jp2_image,components[i])*
cristy3ed852e2009-09-05 21:47:34 +0000477 jas_image_cmpthstep(jp2_image,components[i]));
cristybb503372010-05-27 20:51:26 +0000478 height=(size_t) (jas_image_cmptheight(jp2_image,components[i])*
cristy3ed852e2009-09-05 21:47:34 +0000479 jas_image_cmptvstep(jp2_image,components[i]));
cristy30faca22009-09-29 13:49:52 +0000480 x_step[i]=(unsigned int) jas_image_cmpthstep(jp2_image,components[i]);
481 y_step[i]=(unsigned int) jas_image_cmptvstep(jp2_image,components[i]);
cristy3ed852e2009-09-05 21:47:34 +0000482 if ((width != image->columns) || (height != image->rows) ||
483 (jas_image_cmpttlx(jp2_image,components[i]) != 0) ||
cristy30faca22009-09-29 13:49:52 +0000484 (jas_image_cmpttly(jp2_image,components[i]) != 0) ||
485 (x_step[i] != 1) || (y_step[i] != 1) ||
486 (jas_image_cmptsgnd(jp2_image,components[i]) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000487 {
488 (void) jas_stream_close(jp2_stream);
489 jas_image_destroy(jp2_image);
490 ThrowReaderException(CoderError,"IrregularChannelGeometryNotSupported");
491 }
cristy3ed852e2009-09-05 21:47:34 +0000492 }
493 /*
494 Convert JPEG 2000 pixels.
495 */
496 image->matte=number_components > 3 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000497 maximum_component_depth=0;
cristybb503372010-05-27 20:51:26 +0000498 for (i=0; i < (ssize_t) number_components; i++)
cristy3ed852e2009-09-05 21:47:34 +0000499 {
500 maximum_component_depth=(unsigned int) MagickMax((size_t)
501 jas_image_cmptprec(jp2_image,components[i]),(size_t)
502 maximum_component_depth);
503 pixels[i]=jas_matrix_create(1,(int) (image->columns/x_step[i]));
504 if (pixels[i] == (jas_matrix_t *) NULL)
505 {
cristy30faca22009-09-29 13:49:52 +0000506 for (--i; i >= 0; i--)
507 jas_matrix_destroy(pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000508 jas_image_destroy(jp2_image);
509 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
510 }
511 }
cristy30faca22009-09-29 13:49:52 +0000512 image->depth=maximum_component_depth;
513 if (image_info->ping != MagickFalse)
514 {
515 (void) jas_stream_close(jp2_stream);
516 jas_image_destroy(jp2_image);
517 return(GetFirstImageInList(image));
518 }
cristybb503372010-05-27 20:51:26 +0000519 for (i=0; i < (ssize_t) number_components; i++)
520 range[i]=GetQuantumRange((size_t) jas_image_cmptprec(jp2_image,
cristy3ed852e2009-09-05 21:47:34 +0000521 components[i]));
cristybb503372010-05-27 20:51:26 +0000522 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000523 {
524 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000525 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000526 break;
cristybb503372010-05-27 20:51:26 +0000527 for (i=0; i < (ssize_t) number_components; i++)
cristy3ed852e2009-09-05 21:47:34 +0000528 (void) jas_image_readcmpt(jp2_image,(short) components[i],0,
cristyf6fe0a12010-05-30 00:44:47 +0000529 (jas_image_coord_t) (y/y_step[i]),(jas_image_coord_t) (image->columns/
530 x_step[i]),1,pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000531 switch (number_components)
532 {
533 case 1:
534 {
535 /*
536 Grayscale.
537 */
cristybb503372010-05-27 20:51:26 +0000538 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000539 {
540 pixel=(QuantumAny) jas_matrix_getv(pixels[0],x/x_step[0]);
cristy4c08aed2011-07-01 19:47:50 +0000541 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) pixel,range[0]),q);
542 SetPixelGreen(image,GetPixelRed(image,q),q);
543 SetPixelBlue(image,GetPixelRed(image,q),q);
544 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000545 }
546 break;
547 }
548 case 3:
549 {
550 /*
551 RGB.
552 */
cristybb503372010-05-27 20:51:26 +0000553 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000554 {
555 pixel=(QuantumAny) jas_matrix_getv(pixels[0],x/x_step[0]);
cristy4c08aed2011-07-01 19:47:50 +0000556 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) pixel,range[0]),q);
cristy3ed852e2009-09-05 21:47:34 +0000557 pixel=(QuantumAny) jas_matrix_getv(pixels[1],x/x_step[1]);
cristy4c08aed2011-07-01 19:47:50 +0000558 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) pixel,range[1]),q);
cristy3ed852e2009-09-05 21:47:34 +0000559 pixel=(QuantumAny) jas_matrix_getv(pixels[2],x/x_step[2]);
cristy4c08aed2011-07-01 19:47:50 +0000560 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) pixel,range[2]),q);
561 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000562 }
563 break;
564 }
565 case 4:
566 {
567 /*
568 RGBA.
569 */
cristybb503372010-05-27 20:51:26 +0000570 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000571 {
572 pixel=(QuantumAny) jas_matrix_getv(pixels[0],x/x_step[0]);
cristy4c08aed2011-07-01 19:47:50 +0000573 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) pixel,range[0]),q);
cristy3ed852e2009-09-05 21:47:34 +0000574 pixel=(QuantumAny) jas_matrix_getv(pixels[1],x/x_step[1]);
cristy4c08aed2011-07-01 19:47:50 +0000575 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) pixel,range[1]),q);
cristy3ed852e2009-09-05 21:47:34 +0000576 pixel=(QuantumAny) jas_matrix_getv(pixels[2],x/x_step[2]);
cristy4c08aed2011-07-01 19:47:50 +0000577 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) pixel,range[2]),q);
cristy3ed852e2009-09-05 21:47:34 +0000578 pixel=(QuantumAny) jas_matrix_getv(pixels[3],x/x_step[3]);
cristy4c08aed2011-07-01 19:47:50 +0000579 SetPixelAlpha(image,ScaleAnyToQuantum((QuantumAny) pixel,range[3]),q);
580 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000581 }
582 break;
583 }
584 }
585 if (SyncAuthenticPixels(image,exception) == MagickFalse)
586 break;
cristycee97112010-05-28 00:44:52 +0000587 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristy524222d2011-04-25 00:37:06 +0000588 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000589 if (status == MagickFalse)
590 break;
591 }
592 cm_profile=jas_image_cmprof(jp2_image);
593 icc_profile=(jas_iccprof_t *) NULL;
594 if (cm_profile != (jas_cmprof_t *) NULL)
595 icc_profile=jas_iccprof_createfromcmprof(cm_profile);
596 if (icc_profile != (jas_iccprof_t *) NULL)
597 {
598 jas_stream_t
599 *icc_stream;
600
601 icc_stream=jas_stream_memopen(NULL,0);
602 if ((icc_stream != (jas_stream_t *) NULL) &&
603 (jas_iccprof_save(icc_profile,icc_stream) == 0) &&
604 (jas_stream_flush(icc_stream) == 0))
605 {
606 StringInfo
607 *icc_profile,
608 *profile;
609
610 jas_stream_memobj_t
611 *blob;
612
613 /*
614 Extract the icc profile, handle errors without much noise.
615 */
616 blob=(jas_stream_memobj_t *) icc_stream->obj_;
617 if (image->debug != MagickFalse)
618 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000619 "Profile: ICC, %.20g bytes",(double) blob->len_);
cristy3ed852e2009-09-05 21:47:34 +0000620 profile=AcquireStringInfo(blob->len_);
621 SetStringInfoDatum(profile,blob->buf_);
622 icc_profile=(StringInfo *) GetImageProfile(image,"icc");
623 if (icc_profile == (StringInfo *) NULL)
624 (void) SetImageProfile(image,"icc",profile);
625 else
626 (void) ConcatenateStringInfo(icc_profile,profile);
627 profile=DestroyStringInfo(profile);
628 (void) jas_stream_close(icc_stream);
629 }
630 }
631 (void) jas_stream_close(jp2_stream);
632 jas_image_destroy(jp2_image);
cristybb503372010-05-27 20:51:26 +0000633 for (i=0; i < (ssize_t) number_components; i++)
cristy3ed852e2009-09-05 21:47:34 +0000634 jas_matrix_destroy(pixels[i]);
635 return(GetFirstImageInList(image));
636}
637#endif
638
639/*
640%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641% %
642% %
643% %
644% R e g i s t e r J P 2 I m a g e %
645% %
646% %
647% %
648%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649%
650% RegisterJP2Image() adds attributes for the JP2 image format to the list of
651% supported formats. The attributes include the image format tag, a method
652% method to read and/or write the format, whether the format supports the
653% saving of more than one frame to the same file or blob, whether the format
654% supports native in-memory I/O, and a brief description of the format.
655%
656% The format of the RegisterJP2Image method is:
657%
cristybb503372010-05-27 20:51:26 +0000658% size_t RegisterJP2Image(void)
cristy3ed852e2009-09-05 21:47:34 +0000659%
660*/
cristybb503372010-05-27 20:51:26 +0000661ModuleExport size_t RegisterJP2Image(void)
cristy3ed852e2009-09-05 21:47:34 +0000662{
663 MagickInfo
664 *entry;
665
666 entry=SetMagickInfo("JP2");
667 entry->description=ConstantString("JPEG-2000 File Format Syntax");
668 entry->module=ConstantString("JP2");
669 entry->magick=(IsImageFormatHandler *) IsJP2;
670 entry->adjoin=MagickFalse;
671 entry->seekable_stream=MagickTrue;
672 entry->thread_support=NoThreadSupport;
673#if defined(MAGICKCORE_JP2_DELEGATE)
674 entry->decoder=(DecodeImageHandler *) ReadJP2Image;
675 entry->encoder=(EncodeImageHandler *) WriteJP2Image;
676#endif
677 (void) RegisterMagickInfo(entry);
678 entry=SetMagickInfo("JPC");
679 entry->description=ConstantString("JPEG-2000 Code Stream Syntax");
cristy02f1fda2009-12-10 15:23:56 +0000680 entry->module=ConstantString("JP2");
681 entry->magick=(IsImageFormatHandler *) IsJPC;
682 entry->adjoin=MagickFalse;
683 entry->seekable_stream=MagickTrue;
684 entry->thread_support=NoThreadSupport;
685#if defined(MAGICKCORE_JP2_DELEGATE)
686 entry->decoder=(DecodeImageHandler *) ReadJP2Image;
687 entry->encoder=(EncodeImageHandler *) WriteJP2Image;
688#endif
689 (void) RegisterMagickInfo(entry);
690 entry=SetMagickInfo("J2C");
691 entry->description=ConstantString("JPEG-2000 Code Stream Syntax");
692 entry->module=ConstantString("JP2");
cristy3ed852e2009-09-05 21:47:34 +0000693 entry->magick=(IsImageFormatHandler *) IsJPC;
694 entry->adjoin=MagickFalse;
695 entry->seekable_stream=MagickTrue;
696 entry->thread_support=NoThreadSupport;
697#if defined(MAGICKCORE_JP2_DELEGATE)
698 entry->decoder=(DecodeImageHandler *) ReadJP2Image;
699 entry->encoder=(EncodeImageHandler *) WriteJP2Image;
700#endif
701 (void) RegisterMagickInfo(entry);
702 entry=SetMagickInfo("JPX");
703 entry->description=ConstantString("JPEG-2000 File Format Syntax");
cristy02f1fda2009-12-10 15:23:56 +0000704 entry->module=ConstantString("JP2");
cristy3ed852e2009-09-05 21:47:34 +0000705 entry->magick=(IsImageFormatHandler *) IsJPC;
706 entry->adjoin=MagickFalse;
707 entry->seekable_stream=MagickTrue;
708 entry->thread_support=NoThreadSupport;
709#if defined(MAGICKCORE_JP2_DELEGATE)
710 entry->decoder=(DecodeImageHandler *) ReadJP2Image;
711 entry->encoder=(EncodeImageHandler *) WriteJP2Image;
712#endif
713 (void) RegisterMagickInfo(entry);
714 entry=SetMagickInfo("PGX");
715 entry->description=ConstantString("JPEG-2000 VM Format");
cristy02f1fda2009-12-10 15:23:56 +0000716 entry->module=ConstantString("JP2");
cristy3ed852e2009-09-05 21:47:34 +0000717 entry->magick=(IsImageFormatHandler *) IsJPC;
718 entry->adjoin=MagickFalse;
719 entry->seekable_stream=MagickTrue;
720 entry->thread_support=NoThreadSupport;
721#if defined(MAGICKCORE_JP2_DELEGATE)
722 entry->decoder=(DecodeImageHandler *) ReadJP2Image;
723#endif
724 (void) RegisterMagickInfo(entry);
725#if defined(MAGICKCORE_JP2_DELEGATE)
cristy1e09ca22009-12-27 18:04:58 +0000726 if (instantiate_jp2 == MagickFalse)
727 {
728 jas_init();
729 instantiate_jp2=MagickTrue;
730 }
cristy3ed852e2009-09-05 21:47:34 +0000731#endif
732 return(MagickImageCoderSignature);
733}
734
735/*
736%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
737% %
738% %
739% %
740% U n r e g i s t e r J P 2 I m a g e %
741% %
742% %
743% %
744%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
745%
746% UnregisterJP2Image() removes format registrations made by the JP2 module
747% from the list of supported formats.
748%
749% The format of the UnregisterJP2Image method is:
750%
751% UnregisterJP2Image(void)
752%
753*/
754ModuleExport void UnregisterJP2Image(void)
755{
cristy3ed852e2009-09-05 21:47:34 +0000756 (void) UnregisterMagickInfo("PGX");
cristy02f1fda2009-12-10 15:23:56 +0000757 (void) UnregisterMagickInfo("J2C");
758 (void) UnregisterMagickInfo("JPC");
759 (void) UnregisterMagickInfo("JP2");
cristy3ed852e2009-09-05 21:47:34 +0000760#if defined(MAGICKCORE_JP2_DELEGATE)
cristy1e09ca22009-12-27 18:04:58 +0000761 if (instantiate_jp2 != MagickFalse)
762 {
cristy992729d2010-01-01 15:45:06 +0000763 jas_cleanup();
cristy1e09ca22009-12-27 18:04:58 +0000764 instantiate_jp2=MagickFalse;
765 }
cristy3ed852e2009-09-05 21:47:34 +0000766#endif
767}
768
769#if defined(MAGICKCORE_JP2_DELEGATE)
770/*
771%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
772% %
773% %
774% %
775% W r i t e J P 2 I m a g e %
776% %
777% %
778% %
779%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780%
781% WriteJP2Image() writes an image in the JPEG 2000 image format.
782%
783% JP2 support originally written by Nathan Brown, nathanbrown@letu.edu
784%
785% The format of the WriteJP2Image method is:
786%
787% MagickBooleanType WriteJP2Image(const ImageInfo *image_info,Image *image)
788%
789% A description of each parameter follows.
790%
791% o image_info: the image info.
792%
793% o image: The image.
794%
795*/
796static MagickBooleanType WriteJP2Image(const ImageInfo *image_info,Image *image)
797{
798 char
799 *key,
800 magick[MaxTextExtent],
801 *options;
802
803 const char
804 *option;
805
cristy3ed852e2009-09-05 21:47:34 +0000806 jas_image_cmptparm_t
807 component_info[4];
808
809 jas_image_t
810 *jp2_image;
811
812 jas_matrix_t
813 *pixels[4];
814
815 jas_stream_t
816 *jp2_stream;
817
818 MagickBooleanType
819 status;
820
cristy18f8ee42009-09-30 01:04:36 +0000821 QuantumAny
822 range;
823
cristy4c08aed2011-07-01 19:47:50 +0000824 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000825 *p;
826
cristybb503372010-05-27 20:51:26 +0000827 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000828 i,
829 x;
830
cristybb503372010-05-27 20:51:26 +0000831 size_t
cristy3ed852e2009-09-05 21:47:34 +0000832 number_components;
833
cristy524222d2011-04-25 00:37:06 +0000834 ssize_t
835 format,
836 y;
837
cristy3ed852e2009-09-05 21:47:34 +0000838 /*
839 Open image file.
840 */
841 assert(image_info != (const ImageInfo *) NULL);
842 assert(image_info->signature == MagickSignature);
843 assert(image != (Image *) NULL);
844 assert(image->signature == MagickSignature);
845 if (image->debug != MagickFalse)
846 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
847 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
848 if (status == MagickFalse)
849 return(status);
850 /*
glennrpbca49a22011-07-01 12:18:22 +0000851 Initialize JPEG 2000 API.
cristy3ed852e2009-09-05 21:47:34 +0000852 */
cristy510d06a2011-07-06 23:43:54 +0000853 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000854 (void) TransformImageColorspace(image,RGBColorspace);
855 jp2_stream=JP2StreamManager(image);
856 if (jp2_stream == (jas_stream_t *) NULL)
857 ThrowWriterException(DelegateError,"UnableToManageJP2Stream");
858 number_components=image->matte ? 4UL : 3UL;
859 if ((image_info->type != TrueColorType) &&
cristy4c08aed2011-07-01 19:47:50 +0000860 (IsImageGray(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000861 number_components=1;
862 if ((image->columns != (unsigned int) image->columns) ||
863 (image->rows != (unsigned int) image->rows))
864 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
cristy30faca22009-09-29 13:49:52 +0000865 (void) ResetMagickMemory(&component_info,0,sizeof(component_info));
cristybb503372010-05-27 20:51:26 +0000866 for (i=0; i < (ssize_t) number_components; i++)
cristy3ed852e2009-09-05 21:47:34 +0000867 {
868 component_info[i].tlx=0;
869 component_info[i].tly=0;
870 component_info[i].hstep=1;
871 component_info[i].vstep=1;
872 component_info[i].width=(unsigned int) image->columns;
873 component_info[i].height=(unsigned int) image->rows;
cristy30faca22009-09-29 13:49:52 +0000874 component_info[i].prec=(int) MagickMax(MagickMin(image->depth,16),2);
cristy3ed852e2009-09-05 21:47:34 +0000875 component_info[i].sgnd=MagickFalse;
876 }
877 jp2_image=jas_image_create((int) number_components,component_info,
878 JAS_CLRSPC_UNKNOWN);
879 if (jp2_image == (jas_image_t *) NULL)
880 ThrowWriterException(DelegateError,"UnableToCreateImage");
881 if (number_components == 1)
882 {
883 /*
884 sRGB Grayscale.
885 */
886 jas_image_setclrspc(jp2_image,JAS_CLRSPC_SGRAY);
887 jas_image_setcmpttype(jp2_image,0,
888 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
889 }
890 else
891 {
892 /*
893 sRGB.
894 */
cristy3ed852e2009-09-05 21:47:34 +0000895 jas_image_setclrspc(jp2_image,JAS_CLRSPC_SRGB);
896 jas_image_setcmpttype(jp2_image,0,
cristy722af032009-12-04 01:50:06 +0000897 (jas_image_cmpttype_t) JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
cristy3ed852e2009-09-05 21:47:34 +0000898 jas_image_setcmpttype(jp2_image,1,
cristy722af032009-12-04 01:50:06 +0000899 (jas_image_cmpttype_t) JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
cristy3ed852e2009-09-05 21:47:34 +0000900 jas_image_setcmpttype(jp2_image,2,
cristy722af032009-12-04 01:50:06 +0000901 (jas_image_cmpttype_t) JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
cristy30faca22009-09-29 13:49:52 +0000902 if (number_components == 4)
903 jas_image_setcmpttype(jp2_image,3,JAS_IMAGE_CT_OPACITY);
cristy3ed852e2009-09-05 21:47:34 +0000904 }
905 /*
906 Convert to JPEG 2000 pixels.
907 */
cristybb503372010-05-27 20:51:26 +0000908 for (i=0; i < (ssize_t) number_components; i++)
cristy3ed852e2009-09-05 21:47:34 +0000909 {
910 pixels[i]=jas_matrix_create(1,(int) image->columns);
911 if (pixels[i] == (jas_matrix_t *) NULL)
912 {
913 for (x=0; x < i; x++)
914 jas_matrix_destroy(pixels[x]);
915 jas_image_destroy(jp2_image);
916 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
917 }
918 }
cristybb503372010-05-27 20:51:26 +0000919 range=GetQuantumRange((size_t) component_info[0].prec);
920 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000921 {
922 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +0000923 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000924 break;
cristybb503372010-05-27 20:51:26 +0000925 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000926 {
927 if (number_components == 1)
cristy722af032009-12-04 01:50:06 +0000928 jas_matrix_setv(pixels[0],x,(jas_seqent_t) ScaleQuantumToAny(
cristy4c08aed2011-07-01 19:47:50 +0000929 GetPixelIntensity(image,p),range));
cristy3ed852e2009-09-05 21:47:34 +0000930 else
931 {
cristy4c08aed2011-07-01 19:47:50 +0000932 jas_matrix_setv(pixels[0],x,(jas_seqent_t) ScaleQuantumToAny(
933 GetPixelRed(image,p),range));
934 jas_matrix_setv(pixels[1],x,(jas_seqent_t) ScaleQuantumToAny(
935 GetPixelGreen(image,p),range));
936 jas_matrix_setv(pixels[2],x,(jas_seqent_t) ScaleQuantumToAny(
937 GetPixelBlue(image,p),range));
cristy3ed852e2009-09-05 21:47:34 +0000938 if (number_components > 3)
cristy4c08aed2011-07-01 19:47:50 +0000939 jas_matrix_setv(pixels[3],x,(jas_seqent_t) ScaleQuantumToAny(
940 GetPixelAlpha(image,p),range));
cristy3ed852e2009-09-05 21:47:34 +0000941 }
cristy4c08aed2011-07-01 19:47:50 +0000942 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000943 }
cristybb503372010-05-27 20:51:26 +0000944 for (i=0; i < (ssize_t) number_components; i++)
cristy3ed852e2009-09-05 21:47:34 +0000945 (void) jas_image_writecmpt(jp2_image,(short) i,0,(unsigned int) y,
946 (unsigned int) image->columns,1,pixels[i]);
cristycee97112010-05-28 00:44:52 +0000947 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristy524222d2011-04-25 00:37:06 +0000948 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000949 if (status == MagickFalse)
950 break;
951 }
952 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
cristy02f1fda2009-12-10 15:23:56 +0000953 if (LocaleCompare(magick,"J2C") == 0)
954 (void) CopyMagickString(magick,"JPC",MaxTextExtent);
cristy3ed852e2009-09-05 21:47:34 +0000955 LocaleLower(magick);
956 format=jas_image_strtofmt(magick);
957 options=(char *) NULL;
958 ResetImageOptionIterator(image_info);
959 key=GetNextImageOption(image_info);
cristy722af032009-12-04 01:50:06 +0000960 for ( ; key != (char *) NULL; key=GetNextImageOption(image_info))
cristy3ed852e2009-09-05 21:47:34 +0000961 {
962 option=GetImageOption(image_info,key);
cristy722af032009-12-04 01:50:06 +0000963 if (option == (const char *) NULL)
964 continue;
965 if (LocaleNCompare(key,"jp2:",4) == 0)
cristy3ed852e2009-09-05 21:47:34 +0000966 {
cristy722af032009-12-04 01:50:06 +0000967 (void) ConcatenateString(&options,key+4);
968 if (*option != '\0')
cristy3ed852e2009-09-05 21:47:34 +0000969 {
cristy722af032009-12-04 01:50:06 +0000970 (void) ConcatenateString(&options,"=");
971 (void) ConcatenateString(&options,option);
cristy3ed852e2009-09-05 21:47:34 +0000972 }
cristy722af032009-12-04 01:50:06 +0000973 (void) ConcatenateString(&options," ");
974 }
cristy3ed852e2009-09-05 21:47:34 +0000975 }
976 option=GetImageOption(image_info,"jp2:rate");
977 if ((option == (const char *) NULL) &&
978 (image_info->compression != LosslessJPEGCompression) &&
979 (image->quality != UndefinedCompressionQuality) &&
cristy30faca22009-09-29 13:49:52 +0000980 ((double) image->quality <= 99.5) &&
981 ((image->rows*image->columns) > 2500))
cristy3ed852e2009-09-05 21:47:34 +0000982 {
983 char
984 option[MaxTextExtent];
985
986 double
987 alpha,
988 header_size,
989 number_pixels,
990 rate,
991 target_size;
992
993 alpha=115.0-image->quality;
994 rate=100.0/(alpha*alpha);
995 header_size=550.0;
996 header_size+=(number_components-1)*142;
997 number_pixels=(double) image->rows*image->columns*number_components*
998 (GetImageQuantumDepth(image,MagickTrue)/8);
999 target_size=(number_pixels*rate)+header_size;
1000 rate=target_size/number_pixels;
cristyb51dff52011-05-19 16:55:47 +00001001 (void) FormatLocaleString(option,MaxTextExtent,"rate=%g",rate);
cristy3ed852e2009-09-05 21:47:34 +00001002 (void) ConcatenateString(&options,option);
1003 }
1004 status=jas_image_encode(jp2_image,jp2_stream,format,options) != 0 ?
1005 MagickTrue : MagickFalse;
1006 (void) jas_stream_close(jp2_stream);
cristybb503372010-05-27 20:51:26 +00001007 for (i=0; i < (ssize_t) number_components; i++)
cristy3ed852e2009-09-05 21:47:34 +00001008 jas_matrix_destroy(pixels[i]);
1009 jas_image_destroy(jp2_image);
1010 if (status != MagickFalse)
1011 ThrowWriterException(DelegateError,"UnableToEncodeImageFile");
1012 return(MagickTrue);
1013}
1014#endif