2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write JPEG-2000 Image Format %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #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"
48 #include "MagickCore/colorspace-private.h"
49 #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"
68 #if defined(MAGICKCORE_JP2_DELEGATE)
69 #ifndef JAS_IMAGE_CM_GRAY
70 #define JAS_IMAGE_CM_GRAY JAS_IMAGE_CS_GRAY
72 #ifndef JAS_IMAGE_CM_RGB
73 #define JAS_IMAGE_CM_RGB JAS_IMAGE_CS_RGB
76 #define uchar unsigned char
79 #define ushort unsigned short
82 #define uint unsigned int
84 #if !defined(ssize_tssize_t)
85 #define ssize_tssize_t long long
87 #if !defined(ussize_tssize_t)
88 #define ussize_tssize_t unsigned long long
93 #undef PACKAGE_TARNAME
94 #undef PACKAGE_VERSION
95 #include "jasper/jasper.h"
98 #undef PACKAGE_TARNAME
99 #undef PACKAGE_VERSION
104 Forward declarations.
106 #if defined(MAGICKCORE_JP2_DELEGATE)
107 static MagickBooleanType
108 WriteJP2Image(const ImageInfo *,Image *,ExceptionInfo *);
110 static volatile MagickBooleanType
111 instantiate_jp2 = MagickFalse;
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % IsJP2() returns MagickTrue if the image format type, identified by the
126 % magick string, is JP2.
128 % The format of the IsJP2 method is:
130 % MagickBooleanType IsJP2(const unsigned char *magick,const size_t length)
132 % A description of each parameter follows:
134 % o magick: compare image format pattern against these bytes.
136 % o length: Specifies the length of the magick string.
139 static MagickBooleanType IsJP2(const unsigned char *magick,const size_t length)
143 if (memcmp(magick+4,"\152\120\040\040\015",5) == 0)
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 % IsJPC()() returns MagickTrue if the image format type, identified by the
160 % magick string, is JPC.
162 % The format of the IsJPC method is:
164 % MagickBooleanType IsJPC(const unsigned char *magick,const size_t length)
166 % A description of each parameter follows:
168 % o magick: compare image format pattern against these bytes.
170 % o length: Specifies the length of the magick string.
173 static MagickBooleanType IsJPC(const unsigned char *magick,const size_t length)
177 if (memcmp(magick,"\377\117",2) == 0)
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 % R e a d J P 2 I m a g e %
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
198 % JP2 support is originally written by Nathan Brown, nathanbrown@letu.edu.
200 % The format of the ReadJP2Image method is:
202 % Image *ReadJP2Image(const ImageInfo *image_info,
203 % ExceptionInfo *exception)
205 % A description of each parameter follows:
207 % o image_info: the image info.
209 % o exception: return any errors or warnings in this structure.
212 #if defined(MAGICKCORE_JP2_DELEGATE)
214 typedef struct _StreamManager
223 static int BlobRead(jas_stream_obj_t *object,char *buffer,const int length)
231 source=(StreamManager *) object;
232 count=ReadBlob(source->image,(size_t) length,(unsigned char *) buffer);
236 static int BlobWrite(jas_stream_obj_t *object,char *buffer,const int length)
244 source=(StreamManager *) object;
245 count=WriteBlob(source->image,(size_t) length,(unsigned char *) buffer);
249 static long BlobSeek(jas_stream_obj_t *object,long offset,int origin)
254 source=(StreamManager *) object;
255 return((long) SeekBlob(source->image,offset,origin));
258 static int BlobClose(jas_stream_obj_t *object)
263 source=(StreamManager *) object;
264 (void) CloseBlob(source->image);
266 source=(StreamManager *) NULL;
270 static inline size_t MagickMax(const size_t x,const size_t y)
277 static inline size_t MagickMin(const size_t x,const size_t y)
284 static jas_stream_t *JP2StreamManager(Image *image)
286 static jas_stream_ops_t
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)
316 stream->bufbase_=stream->tinybuf_;
321 stream->bufmode_=JAS_STREAM_FREEBUF | JAS_STREAM_BUFMODEMASK;
322 stream->bufsize_=JAS_STREAM_BUFSIZE;
324 stream->bufstart_=(&stream->bufbase_[JAS_STREAM_MAXPUTBACK]);
325 stream->ptr_=stream->bufstart_;
327 source=(StreamManager *) stream->obj_;
332 static Image *ReadJP2Image(const ImageInfo *image_info,ExceptionInfo *exception)
367 maximum_component_depth,
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,exception);
387 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
388 if (status == MagickFalse)
390 image=DestroyImageList(image);
391 return((Image *) NULL);
394 Initialize JPEG 2000 API.
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)
402 (void) jas_stream_close(jp2_stream);
403 ThrowReaderException(DelegateError,"UnableToDecodeImageFile");
405 switch (jas_clrspc_fam(jas_image_clrspc(jp2_image)))
407 case JAS_CLRSPC_FAM_RGB:
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))
414 (void) jas_stream_close(jp2_stream);
415 jas_image_destroy(jp2_image);
416 ThrowReaderException(CorruptImageError,"MissingImageChannel");
419 components[3]=jas_image_getcmptbytype(jp2_image,3);
420 if (components[3] > 0)
422 image->matte=MagickTrue;
427 case JAS_CLRSPC_FAM_GRAY:
429 components[0]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_GRAY_Y);
430 if (components[0] < 0)
432 (void) jas_stream_close(jp2_stream);
433 jas_image_destroy(jp2_image);
434 ThrowReaderException(CorruptImageError,"MissingImageChannel");
439 case JAS_CLRSPC_FAM_YCBCR:
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))
446 (void) jas_stream_close(jp2_stream);
447 jas_image_destroy(jp2_image);
448 ThrowReaderException(CorruptImageError,"MissingImageChannel");
451 components[3]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_UNKNOWN);
452 if (components[3] > 0)
454 image->matte=MagickTrue;
457 SetImageColorspace(image,YCbCrColorspace,exception);
462 (void) jas_stream_close(jp2_stream);
463 jas_image_destroy(jp2_image);
464 ThrowReaderException(CoderError,"ColorspaceModelIsNotSupported");
467 image->columns=jas_image_width(jp2_image);
468 image->rows=jas_image_height(jp2_image);
469 image->compression=JPEG2000Compression;
470 if (number_components == 1)
471 SetImageColorspace(image,GRAYColorspace,exception);
472 for (i=0; i < (ssize_t) number_components; i++)
478 width=(size_t) (jas_image_cmptwidth(jp2_image,components[i])*
479 jas_image_cmpthstep(jp2_image,components[i]));
480 height=(size_t) (jas_image_cmptheight(jp2_image,components[i])*
481 jas_image_cmptvstep(jp2_image,components[i]));
482 x_step[i]=(unsigned int) jas_image_cmpthstep(jp2_image,components[i]);
483 y_step[i]=(unsigned int) jas_image_cmptvstep(jp2_image,components[i]);
484 if ((width != image->columns) || (height != image->rows) ||
485 (jas_image_cmpttlx(jp2_image,components[i]) != 0) ||
486 (jas_image_cmpttly(jp2_image,components[i]) != 0) ||
487 (x_step[i] != 1) || (y_step[i] != 1) ||
488 (jas_image_cmptsgnd(jp2_image,components[i]) != MagickFalse))
490 (void) jas_stream_close(jp2_stream);
491 jas_image_destroy(jp2_image);
492 ThrowReaderException(CoderError,"IrregularChannelGeometryNotSupported");
496 Convert JPEG 2000 pixels.
498 image->matte=number_components > 3 ? MagickTrue : MagickFalse;
499 maximum_component_depth=0;
500 for (i=0; i < (ssize_t) number_components; i++)
502 maximum_component_depth=(unsigned int) MagickMax((size_t)
503 jas_image_cmptprec(jp2_image,components[i]),(size_t)
504 maximum_component_depth);
505 pixels[i]=jas_matrix_create(1,(int) (image->columns/x_step[i]));
506 if (pixels[i] == (jas_matrix_t *) NULL)
508 for (--i; i >= 0; i--)
509 jas_matrix_destroy(pixels[i]);
510 jas_image_destroy(jp2_image);
511 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
514 image->depth=maximum_component_depth;
515 if (image_info->ping != MagickFalse)
517 (void) jas_stream_close(jp2_stream);
518 jas_image_destroy(jp2_image);
519 return(GetFirstImageInList(image));
521 for (i=0; i < (ssize_t) number_components; i++)
522 range[i]=GetQuantumRange((size_t) jas_image_cmptprec(jp2_image,
524 for (y=0; y < (ssize_t) image->rows; y++)
526 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
527 if (q == (Quantum *) NULL)
529 for (i=0; i < (ssize_t) number_components; i++)
530 (void) jas_image_readcmpt(jp2_image,(short) components[i],0,
531 (jas_image_coord_t) (y/y_step[i]),(jas_image_coord_t) (image->columns/
532 x_step[i]),1,pixels[i]);
533 switch (number_components)
540 for (x=0; x < (ssize_t) image->columns; x++)
542 pixel=(QuantumAny) jas_matrix_getv(pixels[0],x/x_step[0]);
543 SetPixelGray(image,ScaleAnyToQuantum((QuantumAny) pixel,range[0]),q);
544 q+=GetPixelChannels(image);
553 for (x=0; x < (ssize_t) image->columns; x++)
555 pixel=(QuantumAny) jas_matrix_getv(pixels[0],x/x_step[0]);
556 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) pixel,range[0]),q);
557 pixel=(QuantumAny) jas_matrix_getv(pixels[1],x/x_step[1]);
558 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) pixel,range[1]),q);
559 pixel=(QuantumAny) jas_matrix_getv(pixels[2],x/x_step[2]);
560 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) pixel,range[2]),q);
561 q+=GetPixelChannels(image);
570 for (x=0; x < (ssize_t) image->columns; x++)
572 pixel=(QuantumAny) jas_matrix_getv(pixels[0],x/x_step[0]);
573 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) pixel,range[0]),q);
574 pixel=(QuantumAny) jas_matrix_getv(pixels[1],x/x_step[1]);
575 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) pixel,range[1]),q);
576 pixel=(QuantumAny) jas_matrix_getv(pixels[2],x/x_step[2]);
577 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) pixel,range[2]),q);
578 pixel=(QuantumAny) jas_matrix_getv(pixels[3],x/x_step[3]);
579 SetPixelAlpha(image,ScaleAnyToQuantum((QuantumAny) pixel,range[3]),q);
580 q+=GetPixelChannels(image);
585 if (SyncAuthenticPixels(image,exception) == MagickFalse)
587 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
589 if (status == MagickFalse)
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)
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))
614 Extract the icc profile, handle errors without much noise.
616 blob=(jas_stream_memobj_t *) icc_stream->obj_;
617 if (image->debug != MagickFalse)
618 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
619 "Profile: ICC, %.20g bytes",(double) blob->len_);
620 profile=BlobToStringInfo(blob->buf_,blob->len_);
621 if (profile == (StringInfo *) NULL)
622 ThrowReaderException(CorruptImageError,"MemoryAllocationFailed");
623 icc_profile=(StringInfo *) GetImageProfile(image,"icc");
624 if (icc_profile == (StringInfo *) NULL)
625 (void) SetImageProfile(image,"icc",profile,exception);
627 (void) ConcatenateStringInfo(icc_profile,profile);
628 profile=DestroyStringInfo(profile);
629 (void) jas_stream_close(icc_stream);
632 (void) jas_stream_close(jp2_stream);
633 jas_image_destroy(jp2_image);
634 for (i=0; i < (ssize_t) number_components; i++)
635 jas_matrix_destroy(pixels[i]);
636 return(GetFirstImageInList(image));
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 % R e g i s t e r J P 2 I m a g e %
649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 % RegisterJP2Image() adds attributes for the JP2 image format to the list of
652 % supported formats. The attributes include the image format tag, a method
653 % method to read and/or write the format, whether the format supports the
654 % saving of more than one frame to the same file or blob, whether the format
655 % supports native in-memory I/O, and a brief description of the format.
657 % The format of the RegisterJP2Image method is:
659 % size_t RegisterJP2Image(void)
662 ModuleExport size_t RegisterJP2Image(void)
667 entry=SetMagickInfo("JP2");
668 entry->description=ConstantString("JPEG-2000 File Format Syntax");
669 entry->module=ConstantString("JP2");
670 entry->magick=(IsImageFormatHandler *) IsJP2;
671 entry->adjoin=MagickFalse;
672 entry->seekable_stream=MagickTrue;
673 entry->thread_support=NoThreadSupport;
674 #if defined(MAGICKCORE_JP2_DELEGATE)
675 entry->decoder=(DecodeImageHandler *) ReadJP2Image;
676 entry->encoder=(EncodeImageHandler *) WriteJP2Image;
678 (void) RegisterMagickInfo(entry);
679 entry=SetMagickInfo("JPC");
680 entry->description=ConstantString("JPEG-2000 Code Stream Syntax");
681 entry->module=ConstantString("JP2");
682 entry->magick=(IsImageFormatHandler *) IsJPC;
683 entry->adjoin=MagickFalse;
684 entry->seekable_stream=MagickTrue;
685 entry->thread_support=NoThreadSupport;
686 #if defined(MAGICKCORE_JP2_DELEGATE)
687 entry->decoder=(DecodeImageHandler *) ReadJP2Image;
688 entry->encoder=(EncodeImageHandler *) WriteJP2Image;
690 (void) RegisterMagickInfo(entry);
691 entry=SetMagickInfo("J2C");
692 entry->description=ConstantString("JPEG-2000 Code Stream Syntax");
693 entry->module=ConstantString("JP2");
694 entry->magick=(IsImageFormatHandler *) IsJPC;
695 entry->adjoin=MagickFalse;
696 entry->seekable_stream=MagickTrue;
697 entry->thread_support=NoThreadSupport;
698 #if defined(MAGICKCORE_JP2_DELEGATE)
699 entry->decoder=(DecodeImageHandler *) ReadJP2Image;
700 entry->encoder=(EncodeImageHandler *) WriteJP2Image;
702 (void) RegisterMagickInfo(entry);
703 entry=SetMagickInfo("J2K");
704 entry->description=ConstantString("JPEG-2000 Code Stream Syntax");
705 entry->module=ConstantString("JP2");
706 entry->magick=(IsImageFormatHandler *) IsJPC;
707 entry->adjoin=MagickFalse;
708 entry->seekable_stream=MagickTrue;
709 entry->thread_support=NoThreadSupport;
710 #if defined(MAGICKCORE_JP2_DELEGATE)
711 entry->decoder=(DecodeImageHandler *) ReadJP2Image;
712 entry->encoder=(EncodeImageHandler *) WriteJP2Image;
714 (void) RegisterMagickInfo(entry);
715 entry=SetMagickInfo("JPX");
716 entry->description=ConstantString("JPEG-2000 File Format Syntax");
717 entry->module=ConstantString("JP2");
718 entry->magick=(IsImageFormatHandler *) IsJPC;
719 entry->adjoin=MagickFalse;
720 entry->seekable_stream=MagickTrue;
721 entry->thread_support=NoThreadSupport;
722 #if defined(MAGICKCORE_JP2_DELEGATE)
723 entry->decoder=(DecodeImageHandler *) ReadJP2Image;
724 entry->encoder=(EncodeImageHandler *) WriteJP2Image;
726 (void) RegisterMagickInfo(entry);
727 entry=SetMagickInfo("PGX");
728 entry->description=ConstantString("JPEG-2000 VM Format");
729 entry->module=ConstantString("JP2");
730 entry->magick=(IsImageFormatHandler *) IsJPC;
731 entry->adjoin=MagickFalse;
732 entry->seekable_stream=MagickTrue;
733 entry->thread_support=NoThreadSupport;
734 #if defined(MAGICKCORE_JP2_DELEGATE)
735 entry->decoder=(DecodeImageHandler *) ReadJP2Image;
737 (void) RegisterMagickInfo(entry);
738 #if defined(MAGICKCORE_JP2_DELEGATE)
739 if (instantiate_jp2 == MagickFalse)
742 instantiate_jp2=MagickTrue;
745 return(MagickImageCoderSignature);
749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753 % U n r e g i s t e r J P 2 I m a g e %
757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 % UnregisterJP2Image() removes format registrations made by the JP2 module
760 % from the list of supported formats.
762 % The format of the UnregisterJP2Image method is:
764 % UnregisterJP2Image(void)
767 ModuleExport void UnregisterJP2Image(void)
769 (void) UnregisterMagickInfo("PGX");
770 (void) UnregisterMagickInfo("J2K");
771 (void) UnregisterMagickInfo("J2C");
772 (void) UnregisterMagickInfo("JPC");
773 (void) UnregisterMagickInfo("JP2");
774 #if defined(MAGICKCORE_JP2_DELEGATE)
775 if (instantiate_jp2 != MagickFalse)
778 instantiate_jp2=MagickFalse;
783 #if defined(MAGICKCORE_JP2_DELEGATE)
785 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
789 % W r i t e J P 2 I m a g e %
793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
795 % WriteJP2Image() writes an image in the JPEG 2000 image format.
797 % JP2 support originally written by Nathan Brown, nathanbrown@letu.edu
799 % The format of the WriteJP2Image method is:
801 % MagickBooleanType WriteJP2Image(const ImageInfo *image_info,
802 % Image *image,ExceptionInfo *exception)
804 % A description of each parameter follows.
806 % o image_info: the image info.
808 % o image: The image.
810 % o exception: return any errors or warnings in this structure.
813 static MagickBooleanType WriteJP2Image(const ImageInfo *image_info,Image *image,
814 ExceptionInfo *exception)
818 magick[MaxTextExtent],
842 register const Quantum
859 assert(image_info != (const ImageInfo *) NULL);
860 assert(image_info->signature == MagickSignature);
861 assert(image != (Image *) NULL);
862 assert(image->signature == MagickSignature);
863 if (image->debug != MagickFalse)
864 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
865 assert(exception != (ExceptionInfo *) NULL);
866 assert(exception->signature == MagickSignature);
867 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
868 if (status == MagickFalse)
871 Initialize JPEG 2000 API.
873 if (IssRGBColorspace(image->colorspace) == MagickFalse)
874 (void) TransformImageColorspace(image,sRGBColorspace,exception);
875 jp2_stream=JP2StreamManager(image);
876 if (jp2_stream == (jas_stream_t *) NULL)
877 ThrowWriterException(DelegateError,"UnableToManageJP2Stream");
878 number_components=image->matte ? 4UL : 3UL;
879 if ((image_info->type != TrueColorType) &&
880 (IsImageGray(image,exception) != MagickFalse))
882 if ((image->columns != (unsigned int) image->columns) ||
883 (image->rows != (unsigned int) image->rows))
884 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
885 (void) ResetMagickMemory(&component_info,0,sizeof(component_info));
886 for (i=0; i < (ssize_t) number_components; i++)
888 component_info[i].tlx=0;
889 component_info[i].tly=0;
890 component_info[i].hstep=1;
891 component_info[i].vstep=1;
892 component_info[i].width=(unsigned int) image->columns;
893 component_info[i].height=(unsigned int) image->rows;
894 component_info[i].prec=(int) MagickMax(MagickMin(image->depth,16),2);
895 component_info[i].sgnd=MagickFalse;
897 jp2_image=jas_image_create((int) number_components,component_info,
899 if (jp2_image == (jas_image_t *) NULL)
900 ThrowWriterException(DelegateError,"UnableToCreateImage");
901 if (number_components == 1)
906 jas_image_setclrspc(jp2_image,JAS_CLRSPC_SGRAY);
907 jas_image_setcmpttype(jp2_image,0,
908 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
915 jas_image_setclrspc(jp2_image,JAS_CLRSPC_SRGB);
916 jas_image_setcmpttype(jp2_image,0,
917 (jas_image_cmpttype_t) JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
918 jas_image_setcmpttype(jp2_image,1,
919 (jas_image_cmpttype_t) JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
920 jas_image_setcmpttype(jp2_image,2,
921 (jas_image_cmpttype_t) JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
922 if (number_components == 4)
923 jas_image_setcmpttype(jp2_image,3,JAS_IMAGE_CT_OPACITY);
926 Convert to JPEG 2000 pixels.
928 for (i=0; i < (ssize_t) number_components; i++)
930 pixels[i]=jas_matrix_create(1,(int) image->columns);
931 if (pixels[i] == (jas_matrix_t *) NULL)
933 for (x=0; x < i; x++)
934 jas_matrix_destroy(pixels[x]);
935 jas_image_destroy(jp2_image);
936 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
939 range=GetQuantumRange((size_t) component_info[0].prec);
940 for (y=0; y < (ssize_t) image->rows; y++)
942 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
943 if (p == (const Quantum *) NULL)
945 for (x=0; x < (ssize_t) image->columns; x++)
947 if (number_components == 1)
948 jas_matrix_setv(pixels[0],x,(jas_seqent_t) ScaleQuantumToAny(
949 GetPixelIntensity(image,p),range));
952 jas_matrix_setv(pixels[0],x,(jas_seqent_t) ScaleQuantumToAny(
953 GetPixelRed(image,p),range));
954 jas_matrix_setv(pixels[1],x,(jas_seqent_t) ScaleQuantumToAny(
955 GetPixelGreen(image,p),range));
956 jas_matrix_setv(pixels[2],x,(jas_seqent_t) ScaleQuantumToAny(
957 GetPixelBlue(image,p),range));
958 if (number_components > 3)
959 jas_matrix_setv(pixels[3],x,(jas_seqent_t) ScaleQuantumToAny(
960 GetPixelAlpha(image,p),range));
962 p+=GetPixelChannels(image);
964 for (i=0; i < (ssize_t) number_components; i++)
965 (void) jas_image_writecmpt(jp2_image,(short) i,0,(unsigned int) y,
966 (unsigned int) image->columns,1,pixels[i]);
967 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
969 if (status == MagickFalse)
972 (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
973 if (LocaleCompare(magick,"J2C") == 0)
974 (void) CopyMagickString(magick,"JPC",MaxTextExtent);
976 format=jas_image_strtofmt(magick);
977 options=(char *) NULL;
978 ResetImageOptionIterator(image_info);
979 key=GetNextImageOption(image_info);
980 for ( ; key != (char *) NULL; key=GetNextImageOption(image_info))
982 option=GetImageOption(image_info,key);
983 if (option == (const char *) NULL)
985 if (LocaleNCompare(key,"jp2:",4) == 0)
987 (void) ConcatenateString(&options,key+4);
990 (void) ConcatenateString(&options,"=");
991 (void) ConcatenateString(&options,option);
993 (void) ConcatenateString(&options," ");
996 option=GetImageOption(image_info,"jp2:rate");
997 if ((option == (const char *) NULL) &&
998 (image_info->compression != LosslessJPEGCompression) &&
999 (image->quality != UndefinedCompressionQuality) &&
1000 ((double) image->quality <= 99.5) &&
1001 ((image->rows*image->columns) > 2500))
1004 option[MaxTextExtent];
1013 alpha=115.0-image->quality;
1014 rate=100.0/(alpha*alpha);
1016 header_size+=(number_components-1)*142;
1017 number_pixels=(double) image->rows*image->columns*number_components*
1018 (GetImageQuantumDepth(image,MagickTrue)/8);
1019 target_size=(number_pixels*rate)+header_size;
1020 rate=target_size/number_pixels;
1021 (void) FormatLocaleString(option,MaxTextExtent,"rate=%g",rate);
1022 (void) ConcatenateString(&options,option);
1024 status=jas_image_encode(jp2_image,jp2_stream,format,options) != 0 ?
1025 MagickTrue : MagickFalse;
1026 if (options != (char *) NULL)
1027 options=DestroyString(options);
1028 (void) jas_stream_close(jp2_stream);
1029 for (i=0; i < (ssize_t) number_components; i++)
1030 jas_matrix_destroy(pixels[i]);
1031 jas_image_destroy(jp2_image);
1032 if (status != MagickFalse)
1033 ThrowWriterException(DelegateError,"UnableToEncodeImageFile");