cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 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 | % % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 15 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 16 | % Nathan Brown % |
| 17 | % June 2001 % |
| 18 | % % |
| 19 | % % |
cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 20 | % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % 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 | */ |
cristy | 061f98d | 2014-01-03 20:19:30 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
| 43 | #include "MagickCore/artifact.h" |
| 44 | #include "MagickCore/attribute.h" |
| 45 | #include "MagickCore/blob.h" |
| 46 | #include "MagickCore/blob-private.h" |
| 47 | #include "MagickCore/cache.h" |
| 48 | #include "MagickCore/colorspace.h" |
| 49 | #include "MagickCore/colorspace-private.h" |
| 50 | #include "MagickCore/color.h" |
| 51 | #include "MagickCore/color-private.h" |
| 52 | #include "MagickCore/exception.h" |
| 53 | #include "MagickCore/exception-private.h" |
| 54 | #include "MagickCore/image.h" |
| 55 | #include "MagickCore/image-private.h" |
| 56 | #include "MagickCore/list.h" |
| 57 | #include "MagickCore/magick.h" |
| 58 | #include "MagickCore/memory_.h" |
| 59 | #include "MagickCore/monitor.h" |
| 60 | #include "MagickCore/monitor-private.h" |
| 61 | #include "MagickCore/option.h" |
| 62 | #include "MagickCore/pixel-accessor.h" |
| 63 | #include "MagickCore/profile.h" |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 64 | #include "MagickCore/property.h" |
cristy | 061f98d | 2014-01-03 20:19:30 +0000 | [diff] [blame] | 65 | #include "MagickCore/quantum-private.h" |
cristy | 191ba5c | 2014-03-16 21:26:40 +0000 | [diff] [blame] | 66 | #include "MagickCore/semaphore.h" |
cristy | 061f98d | 2014-01-03 20:19:30 +0000 | [diff] [blame] | 67 | #include "MagickCore/static.h" |
| 68 | #include "MagickCore/statistic.h" |
| 69 | #include "MagickCore/string_.h" |
| 70 | #include "MagickCore/string-private.h" |
| 71 | #include "MagickCore/module.h" |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 72 | #if defined(MAGICKCORE_LIBOPENJP2_DELEGATE) |
| 73 | #include <openjpeg.h> |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 74 | #endif |
| 75 | |
| 76 | /* |
| 77 | Forward declarations. |
| 78 | */ |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 79 | #if defined(MAGICKCORE_LIBOPENJP2_DELEGATE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 80 | static MagickBooleanType |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 81 | WriteJP2Image(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 82 | #endif |
| 83 | |
| 84 | /* |
| 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 86 | % % |
| 87 | % % |
| 88 | % % |
cristy | b5a9791 | 2014-01-02 15:52:07 +0000 | [diff] [blame] | 89 | % I s J 2 K % |
| 90 | % % |
| 91 | % % |
| 92 | % % |
| 93 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 94 | % |
| 95 | % IsJ2K() returns MagickTrue if the image format type, identified by the |
| 96 | % magick string, is J2K. |
| 97 | % |
| 98 | % The format of the IsJ2K method is: |
| 99 | % |
cristy | 9f138af | 2014-02-16 12:54:17 +0000 | [diff] [blame] | 100 | % MagickBooleanType IsJ2K(const unsigned char *magick,const size_t length) |
cristy | b5a9791 | 2014-01-02 15:52:07 +0000 | [diff] [blame] | 101 | % |
| 102 | % A description of each parameter follows: |
| 103 | % |
| 104 | % o magick: compare image format pattern against these bytes. |
| 105 | % |
| 106 | % o length: Specifies the length of the magick string. |
| 107 | % |
| 108 | */ |
| 109 | static MagickBooleanType IsJ2K(const unsigned char *magick,const size_t length) |
| 110 | { |
| 111 | if (length < 4) |
| 112 | return(MagickFalse); |
| 113 | if (memcmp(magick,"\xff\x4f\xff\x51",4) == 0) |
| 114 | return(MagickTrue); |
| 115 | return(MagickFalse); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 120 | % % |
| 121 | % % |
| 122 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 123 | % I s J P 2 % |
| 124 | % % |
| 125 | % % |
| 126 | % % |
| 127 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 128 | % |
| 129 | % IsJP2() returns MagickTrue if the image format type, identified by the |
| 130 | % magick string, is JP2. |
| 131 | % |
| 132 | % The format of the IsJP2 method is: |
| 133 | % |
| 134 | % MagickBooleanType IsJP2(const unsigned char *magick,const size_t length) |
| 135 | % |
| 136 | % A description of each parameter follows: |
| 137 | % |
| 138 | % o magick: compare image format pattern against these bytes. |
| 139 | % |
| 140 | % o length: Specifies the length of the magick string. |
| 141 | % |
| 142 | */ |
| 143 | static MagickBooleanType IsJP2(const unsigned char *magick,const size_t length) |
| 144 | { |
dirk | 378c901 | 2014-02-16 10:21:39 +0000 | [diff] [blame] | 145 | if (length < 4) |
| 146 | return(MagickFalse); |
| 147 | if (memcmp(magick,"\x0d\x0a\x87\x0a",4) == 0) |
| 148 | return(MagickTrue); |
cristy | b5a9791 | 2014-01-02 15:52:07 +0000 | [diff] [blame] | 149 | if (length < 12) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 150 | return(MagickFalse); |
cristy | b5a9791 | 2014-01-02 15:52:07 +0000 | [diff] [blame] | 151 | if (memcmp(magick,"\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a",12) == 0) |
| 152 | return(MagickTrue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 153 | return(MagickFalse); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 158 | % % |
| 159 | % % |
| 160 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 161 | % R e a d J P 2 I m a g e % |
| 162 | % % |
| 163 | % % |
| 164 | % % |
| 165 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 166 | % |
| 167 | % ReadJP2Image() reads a JPEG 2000 Image file (JP2) or JPEG 2000 |
| 168 | % codestream (JPC) image file and returns it. It allocates the memory |
| 169 | % necessary for the new Image structure and returns a pointer to the new |
| 170 | % image or set of images. |
| 171 | % |
| 172 | % JP2 support is originally written by Nathan Brown, nathanbrown@letu.edu. |
| 173 | % |
| 174 | % The format of the ReadJP2Image method is: |
| 175 | % |
| 176 | % Image *ReadJP2Image(const ImageInfo *image_info, |
| 177 | % ExceptionInfo *exception) |
| 178 | % |
| 179 | % A description of each parameter follows: |
| 180 | % |
| 181 | % o image_info: the image info. |
| 182 | % |
| 183 | % o exception: return any errors or warnings in this structure. |
| 184 | % |
| 185 | */ |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 186 | #if defined(MAGICKCORE_LIBOPENJP2_DELEGATE) |
| 187 | static void JP2ErrorHandler(const char *message,void *client_data) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 188 | { |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 189 | ExceptionInfo |
| 190 | *exception; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 191 | |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 192 | exception=(ExceptionInfo *) client_data; |
| 193 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 194 | message,"`%s'","OpenJP2"); |
| 195 | } |
| 196 | |
| 197 | static OPJ_SIZE_T JP2ReadHandler(void *buffer,OPJ_SIZE_T length,void *context) |
| 198 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 199 | Image |
| 200 | *image; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 201 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 202 | ssize_t |
| 203 | count; |
| 204 | |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 205 | image=(Image *) context; |
| 206 | count=ReadBlob(image,(ssize_t) length,(unsigned char *) buffer); |
| 207 | if (count == 0) |
cristy | 10bb01b | 2014-01-05 17:00:06 +0000 | [diff] [blame] | 208 | return((OPJ_SIZE_T) -1); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 209 | return((OPJ_SIZE_T) count); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 210 | } |
| 211 | |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 212 | static OPJ_BOOL JP2SeekHandler(OPJ_OFF_T offset,void *context) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 213 | { |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 214 | Image |
| 215 | *image; |
| 216 | |
| 217 | image=(Image *) context; |
cristy | c72bbbe | 2014-01-21 22:10:48 +0000 | [diff] [blame] | 218 | return(SeekBlob(image,offset,SEEK_SET) < 0 ? OPJ_FALSE : OPJ_TRUE); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 219 | } |
| 220 | |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 221 | static OPJ_OFF_T JP2SkipHandler(OPJ_OFF_T offset,void *context) |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 222 | { |
| 223 | Image |
| 224 | *image; |
| 225 | |
| 226 | image=(Image *) context; |
cristy | c72bbbe | 2014-01-21 22:10:48 +0000 | [diff] [blame] | 227 | return(SeekBlob(image,offset,SEEK_CUR) < 0 ? -1 : offset); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void JP2WarningHandler(const char *message,void *client_data) |
| 231 | { |
| 232 | ExceptionInfo |
| 233 | *exception; |
| 234 | |
| 235 | exception=(ExceptionInfo *) client_data; |
| 236 | (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning, |
| 237 | message,"`%s'","OpenJP2"); |
| 238 | } |
| 239 | |
| 240 | static OPJ_SIZE_T JP2WriteHandler(void *buffer,OPJ_SIZE_T length,void *context) |
| 241 | { |
| 242 | Image |
| 243 | *image; |
| 244 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 245 | ssize_t |
| 246 | count; |
| 247 | |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 248 | image=(Image *) context; |
| 249 | count=WriteBlob(image,(ssize_t) length,(unsigned char *) buffer); |
| 250 | return((OPJ_SIZE_T) count); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static Image *ReadJP2Image(const ImageInfo *image_info,ExceptionInfo *exception) |
| 254 | { |
cristy | 77559f4 | 2014-01-02 18:54:04 +0000 | [diff] [blame] | 255 | const char |
| 256 | *option; |
| 257 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 258 | Image |
| 259 | *image; |
| 260 | |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 261 | int |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 262 | jp2_status; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 263 | |
| 264 | MagickBooleanType |
| 265 | status; |
| 266 | |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 267 | opj_codec_t |
| 268 | *jp2_codec; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 269 | |
cristy | 6c0f150 | 2014-01-02 13:53:25 +0000 | [diff] [blame] | 270 | opj_codestream_index_t |
| 271 | *codestream_index = (opj_codestream_index_t *) NULL; |
| 272 | |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 273 | opj_dparameters_t |
| 274 | parameters; |
| 275 | |
| 276 | opj_image_t |
| 277 | *jp2_image; |
| 278 | |
| 279 | opj_stream_t |
| 280 | *jp2_stream; |
cristy | 48ac1c3 | 2012-10-19 23:55:43 +0000 | [diff] [blame] | 281 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 282 | register ssize_t |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 283 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 284 | |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 285 | ssize_t |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 286 | y; |
| 287 | |
dirk | 378c901 | 2014-02-16 10:21:39 +0000 | [diff] [blame] | 288 | unsigned char |
| 289 | sans[4]; |
| 290 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 291 | /* |
| 292 | Open image file. |
| 293 | */ |
| 294 | assert(image_info != (const ImageInfo *) NULL); |
| 295 | assert(image_info->signature == MagickSignature); |
| 296 | if (image_info->debug != MagickFalse) |
| 297 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 298 | image_info->filename); |
| 299 | assert(exception != (ExceptionInfo *) NULL); |
| 300 | assert(exception->signature == MagickSignature); |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 301 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 302 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 303 | if (status == MagickFalse) |
| 304 | { |
| 305 | image=DestroyImageList(image); |
| 306 | return((Image *) NULL); |
| 307 | } |
| 308 | /* |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 309 | Initialize JP2 codec. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 310 | */ |
dirk | 378c901 | 2014-02-16 10:21:39 +0000 | [diff] [blame] | 311 | if (ReadBlob(image,4,sans) != 4) |
| 312 | { |
| 313 | image=DestroyImageList(image); |
| 314 | return((Image *) NULL); |
| 315 | } |
| 316 | (void) SeekBlob(image,SEEK_SET,0); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 317 | if (LocaleCompare(image_info->magick,"JPT") == 0) |
| 318 | jp2_codec=opj_create_decompress(OPJ_CODEC_JPT); |
| 319 | else |
dirk | 536acd1 | 2014-02-16 10:38:14 +0000 | [diff] [blame] | 320 | if (IsJ2K(sans,4) != MagickFalse) |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 321 | jp2_codec=opj_create_decompress(OPJ_CODEC_J2K); |
| 322 | else |
| 323 | jp2_codec=opj_create_decompress(OPJ_CODEC_JP2); |
| 324 | opj_set_warning_handler(jp2_codec,JP2WarningHandler,exception); |
| 325 | opj_set_error_handler(jp2_codec,JP2ErrorHandler,exception); |
| 326 | opj_set_default_decoder_parameters(¶meters); |
cristy | a13e367 | 2014-01-02 15:24:44 +0000 | [diff] [blame] | 327 | option=GetImageOption(image_info,"jp2:reduce-factor"); |
| 328 | if (option != (const char *) NULL) |
| 329 | parameters.cp_reduce=StringToInteger(option); |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 330 | option=GetImageOption(image_info,"jp2:quality-layers"); |
cristy | a13e367 | 2014-01-02 15:24:44 +0000 | [diff] [blame] | 331 | if (option != (const char *) NULL) |
| 332 | parameters.cp_layer=StringToInteger(option); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 333 | if (opj_setup_decoder(jp2_codec,¶meters) == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 334 | { |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 335 | opj_destroy_codec(jp2_codec); |
| 336 | ThrowReaderException(DelegateError,"UnableToManageJP2Stream"); |
| 337 | } |
cristy | 66d4056 | 2014-01-03 01:27:44 +0000 | [diff] [blame] | 338 | jp2_stream=opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE,1); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 339 | opj_stream_set_read_function(jp2_stream,JP2ReadHandler); |
| 340 | opj_stream_set_write_function(jp2_stream,JP2WriteHandler); |
| 341 | opj_stream_set_seek_function(jp2_stream,JP2SeekHandler); |
| 342 | opj_stream_set_skip_function(jp2_stream,JP2SkipHandler); |
cristy | b46443e | 2014-05-08 12:32:16 +0000 | [diff] [blame] | 343 | opj_stream_set_user_data(jp2_stream,image,NULL); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 344 | opj_stream_set_user_data_length(jp2_stream,GetBlobSize(image)); |
| 345 | if (opj_read_header(jp2_stream,jp2_codec,&jp2_image) == 0) |
| 346 | { |
cristy | 3f62cbf | 2014-04-06 13:02:24 +0000 | [diff] [blame] | 347 | opj_stream_destroy(jp2_stream); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 348 | opj_destroy_codec(jp2_codec); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 349 | ThrowReaderException(DelegateError,"UnableToDecodeImageFile"); |
| 350 | } |
dirk | 025f947 | 2014-10-25 20:06:53 +0000 | [diff] [blame^] | 351 | jp2_status=1; |
cristy | a8997c2 | 2014-01-03 00:55:02 +0000 | [diff] [blame] | 352 | if ((image->columns != 0) && (image->rows != 0)) |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 353 | { |
cristy | 56e4faf | 2014-01-02 17:59:34 +0000 | [diff] [blame] | 354 | /* |
| 355 | Extract an area from the image. |
| 356 | */ |
cristy | a8997c2 | 2014-01-03 00:55:02 +0000 | [diff] [blame] | 357 | jp2_status=opj_set_decode_area(jp2_codec,jp2_image,image->extract_info.x, |
cristy | 4f97673 | 2014-05-09 22:21:21 +0000 | [diff] [blame] | 358 | image->extract_info.y,image->extract_info.x+(ssize_t) image->columns, |
| 359 | image->extract_info.y+(ssize_t) image->rows); |
cristy | 56e4faf | 2014-01-02 17:59:34 +0000 | [diff] [blame] | 360 | if (jp2_status == 0) |
| 361 | { |
cristy | 3f62cbf | 2014-04-06 13:02:24 +0000 | [diff] [blame] | 362 | opj_stream_destroy(jp2_stream); |
cristy | 56e4faf | 2014-01-02 17:59:34 +0000 | [diff] [blame] | 363 | opj_destroy_codec(jp2_codec); |
| 364 | opj_image_destroy(jp2_image); |
| 365 | ThrowReaderException(DelegateError,"UnableToDecodeImageFile"); |
| 366 | } |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 367 | } |
cristy | 4f97673 | 2014-05-09 22:21:21 +0000 | [diff] [blame] | 368 | if (image_info->number_scenes != 0) |
| 369 | jp2_status=opj_get_decoded_tile(jp2_codec,jp2_stream,jp2_image, |
| 370 | (unsigned int) image_info->scene); |
cristy | 7a4f732 | 2014-10-04 14:41:20 +0000 | [diff] [blame] | 371 | else |
| 372 | if (image->ping == MagickFalse) |
| 373 | { |
| 374 | jp2_status=opj_decode(jp2_codec,jp2_stream,jp2_image); |
| 375 | if (jp2_status != 0) |
| 376 | jp2_status=opj_end_decompress(jp2_codec,jp2_stream); |
| 377 | } |
cristy | 4f97673 | 2014-05-09 22:21:21 +0000 | [diff] [blame] | 378 | if (jp2_status == 0) |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 379 | { |
cristy | 3f62cbf | 2014-04-06 13:02:24 +0000 | [diff] [blame] | 380 | opj_stream_destroy(jp2_stream); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 381 | opj_destroy_codec(jp2_codec); |
| 382 | opj_image_destroy(jp2_image); |
| 383 | ThrowReaderException(DelegateError,"UnableToDecodeImageFile"); |
| 384 | } |
cristy | 3f62cbf | 2014-04-06 13:02:24 +0000 | [diff] [blame] | 385 | opj_stream_destroy(jp2_stream); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 386 | for (i=0; i < (ssize_t) jp2_image->numcomps; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 387 | { |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 388 | if ((jp2_image->comps[i].dx == 0) || (jp2_image->comps[i].dy == 0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 389 | { |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 390 | opj_destroy_codec(jp2_codec); |
| 391 | opj_image_destroy(jp2_image); |
| 392 | ThrowReaderException(CoderError,"IrregularChannelGeometryNotSupported") |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 393 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 394 | } |
| 395 | /* |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 396 | Convert JP2 image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 397 | */ |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 398 | image->columns=(size_t) jp2_image->comps[0].w; |
| 399 | image->rows=(size_t) jp2_image->comps[0].h; |
cristy | b3445a0 | 2014-01-04 01:00:14 +0000 | [diff] [blame] | 400 | image->depth=jp2_image->comps[0].prec; |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 401 | image->compression=JPEG2000Compression; |
| 402 | if (jp2_image->numcomps <= 2) |
cristy | 30faca2 | 2009-09-29 13:49:52 +0000 | [diff] [blame] | 403 | { |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 404 | SetImageColorspace(image,GRAYColorspace,exception); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 405 | if (jp2_image->numcomps > 1) |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 406 | image->alpha_trait=BlendPixelTrait; |
cristy | 30faca2 | 2009-09-29 13:49:52 +0000 | [diff] [blame] | 407 | } |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 408 | if (jp2_image->numcomps > 3) |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 409 | image->alpha_trait=BlendPixelTrait; |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 410 | for (i=0; i < (ssize_t) jp2_image->numcomps; i++) |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 411 | if ((jp2_image->comps[i].dx > 1) || (jp2_image->comps[i].dy > 1)) |
cristy | 18b68a0 | 2014-02-13 12:38:47 +0000 | [diff] [blame] | 412 | SetImageColorspace(image,YUVColorspace,exception); |
cristy | e173c29 | 2014-01-02 13:43:12 +0000 | [diff] [blame] | 413 | if (jp2_image->icc_profile_buf != (unsigned char *) NULL) |
| 414 | { |
| 415 | StringInfo |
| 416 | *profile; |
| 417 | |
| 418 | profile=BlobToStringInfo(jp2_image->icc_profile_buf, |
| 419 | jp2_image->icc_profile_len); |
| 420 | if (profile != (StringInfo *) NULL) |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 421 | SetImageProfile(image,"icc",profile,exception); |
cristy | e173c29 | 2014-01-02 13:43:12 +0000 | [diff] [blame] | 422 | } |
dirk | bec9e4d | 2014-09-28 08:33:33 +0000 | [diff] [blame] | 423 | if (image->ping != MagickFalse) |
| 424 | { |
| 425 | opj_destroy_codec(jp2_codec); |
| 426 | opj_image_destroy(jp2_image); |
| 427 | opj_destroy_cstr_index(&codestream_index); |
| 428 | return(GetFirstImageInList(image)); |
| 429 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 430 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 431 | { |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 432 | register Quantum |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 433 | *restrict q; |
| 434 | |
| 435 | register ssize_t |
| 436 | x; |
| 437 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 438 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 439 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 440 | break; |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 441 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 442 | { |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 443 | register ssize_t |
| 444 | i; |
| 445 | |
| 446 | for (i=0; i < (ssize_t) jp2_image->numcomps; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 447 | { |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 448 | double |
| 449 | pixel, |
| 450 | scale; |
| 451 | |
| 452 | scale=QuantumRange/(double) ((1UL << jp2_image->comps[i].prec)-1); |
| 453 | pixel=scale*(jp2_image->comps[i].data[y/jp2_image->comps[i].dy* |
| 454 | image->columns/jp2_image->comps[i].dx+x/jp2_image->comps[i].dx]+ |
| 455 | (jp2_image->comps[i].sgnd ? 1UL << (jp2_image->comps[i].prec-1) : 0)); |
| 456 | switch (i) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 457 | { |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 458 | case 0: |
| 459 | { |
| 460 | SetPixelRed(image,ClampToQuantum(pixel),q); |
| 461 | SetPixelGreen(image,ClampToQuantum(pixel),q); |
| 462 | SetPixelBlue(image,ClampToQuantum(pixel),q); |
| 463 | SetPixelAlpha(image,OpaqueAlpha,q); |
| 464 | break; |
| 465 | } |
| 466 | case 1: |
| 467 | { |
| 468 | if (jp2_image->numcomps == 2) |
| 469 | { |
| 470 | SetPixelAlpha(image,ClampToQuantum(pixel),q); |
| 471 | break; |
| 472 | } |
| 473 | SetPixelGreen(image,ClampToQuantum(pixel),q); |
| 474 | break; |
| 475 | } |
| 476 | case 2: |
| 477 | { |
| 478 | SetPixelBlue(image,ClampToQuantum(pixel),q); |
| 479 | break; |
| 480 | } |
| 481 | case 3: |
| 482 | { |
| 483 | SetPixelAlpha(image,ClampToQuantum(pixel),q); |
| 484 | break; |
| 485 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 486 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 487 | } |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 488 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 489 | } |
| 490 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 491 | break; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 492 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 493 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 494 | if (status == MagickFalse) |
| 495 | break; |
| 496 | } |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 497 | /* |
| 498 | Free resources. |
| 499 | */ |
| 500 | opj_destroy_codec(jp2_codec); |
| 501 | opj_image_destroy(jp2_image); |
cristy | 6c0f150 | 2014-01-02 13:53:25 +0000 | [diff] [blame] | 502 | opj_destroy_cstr_index(&codestream_index); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 503 | return(GetFirstImageInList(image)); |
| 504 | } |
| 505 | #endif |
| 506 | |
| 507 | /* |
| 508 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 509 | % % |
| 510 | % % |
| 511 | % % |
| 512 | % R e g i s t e r J P 2 I m a g e % |
| 513 | % % |
| 514 | % % |
| 515 | % % |
| 516 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 517 | % |
| 518 | % RegisterJP2Image() adds attributes for the JP2 image format to the list of |
| 519 | % supported formats. The attributes include the image format tag, a method |
| 520 | % method to read and/or write the format, whether the format supports the |
| 521 | % saving of more than one frame to the same file or blob, whether the format |
| 522 | % supports native in-memory I/O, and a brief description of the format. |
| 523 | % |
| 524 | % The format of the RegisterJP2Image method is: |
| 525 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 526 | % size_t RegisterJP2Image(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 527 | % |
| 528 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 529 | ModuleExport size_t RegisterJP2Image(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 530 | { |
cristy | bcd740c | 2014-01-03 21:08:39 +0000 | [diff] [blame] | 531 | char |
| 532 | version[MaxTextExtent]; |
| 533 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 534 | MagickInfo |
| 535 | *entry; |
| 536 | |
cristy | bcd740c | 2014-01-03 21:08:39 +0000 | [diff] [blame] | 537 | *version='\0'; |
| 538 | #if defined(MAGICKCORE_LIBOPENJP2_DELEGATE) |
| 539 | (void) FormatLocaleString(version,MaxTextExtent,"%s",opj_version()); |
| 540 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 541 | entry=SetMagickInfo("JP2"); |
| 542 | entry->description=ConstantString("JPEG-2000 File Format Syntax"); |
cristy | bcd740c | 2014-01-03 21:08:39 +0000 | [diff] [blame] | 543 | if (*version != '\0') |
| 544 | entry->version=ConstantString(version); |
cristy | 5aefbeb | 2013-08-09 12:13:32 +0000 | [diff] [blame] | 545 | entry->mime_type=ConstantString("image/jp2"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 546 | entry->module=ConstantString("JP2"); |
| 547 | entry->magick=(IsImageFormatHandler *) IsJP2; |
| 548 | entry->adjoin=MagickFalse; |
| 549 | entry->seekable_stream=MagickTrue; |
| 550 | entry->thread_support=NoThreadSupport; |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 551 | #if defined(MAGICKCORE_LIBOPENJP2_DELEGATE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 552 | entry->decoder=(DecodeImageHandler *) ReadJP2Image; |
| 553 | entry->encoder=(EncodeImageHandler *) WriteJP2Image; |
| 554 | #endif |
| 555 | (void) RegisterMagickInfo(entry); |
dirk | 378c901 | 2014-02-16 10:21:39 +0000 | [diff] [blame] | 556 | entry=SetMagickInfo("J2C"); |
| 557 | entry->description=ConstantString("JPEG-2000 Code Stream Syntax"); |
| 558 | if (*version != '\0') |
| 559 | entry->version=ConstantString(version); |
| 560 | entry->mime_type=ConstantString("image/jp2"); |
| 561 | entry->module=ConstantString("JP2"); |
| 562 | entry->magick=(IsImageFormatHandler *) IsJ2K; |
| 563 | entry->adjoin=MagickFalse; |
| 564 | entry->seekable_stream=MagickTrue; |
| 565 | entry->thread_support=NoThreadSupport; |
| 566 | #if defined(MAGICKCORE_LIBOPENJP2_DELEGATE) |
| 567 | entry->decoder=(DecodeImageHandler *) ReadJP2Image; |
| 568 | entry->encoder=(EncodeImageHandler *) WriteJP2Image; |
| 569 | #endif |
| 570 | (void) RegisterMagickInfo(entry); |
cristy | 9bfeb94 | 2012-05-10 12:07:32 +0000 | [diff] [blame] | 571 | entry=SetMagickInfo("J2K"); |
| 572 | entry->description=ConstantString("JPEG-2000 Code Stream Syntax"); |
cristy | bcd740c | 2014-01-03 21:08:39 +0000 | [diff] [blame] | 573 | if (*version != '\0') |
| 574 | entry->version=ConstantString(version); |
cristy | 5aefbeb | 2013-08-09 12:13:32 +0000 | [diff] [blame] | 575 | entry->mime_type=ConstantString("image/jp2"); |
cristy | 9bfeb94 | 2012-05-10 12:07:32 +0000 | [diff] [blame] | 576 | entry->module=ConstantString("JP2"); |
cristy | b5a9791 | 2014-01-02 15:52:07 +0000 | [diff] [blame] | 577 | entry->magick=(IsImageFormatHandler *) IsJ2K; |
cristy | 9bfeb94 | 2012-05-10 12:07:32 +0000 | [diff] [blame] | 578 | entry->adjoin=MagickFalse; |
| 579 | entry->seekable_stream=MagickTrue; |
| 580 | entry->thread_support=NoThreadSupport; |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 581 | #if defined(MAGICKCORE_LIBOPENJP2_DELEGATE) |
cristy | 9bfeb94 | 2012-05-10 12:07:32 +0000 | [diff] [blame] | 582 | entry->decoder=(DecodeImageHandler *) ReadJP2Image; |
| 583 | entry->encoder=(EncodeImageHandler *) WriteJP2Image; |
| 584 | #endif |
| 585 | (void) RegisterMagickInfo(entry); |
cristy | 2267fa4 | 2014-09-12 10:27:47 +0000 | [diff] [blame] | 586 | entry=SetMagickInfo("JPM"); |
| 587 | entry->description=ConstantString("JPEG-2000 File Format Syntax"); |
| 588 | if (*version != '\0') |
| 589 | entry->version=ConstantString(version); |
| 590 | entry->mime_type=ConstantString("image/jp2"); |
| 591 | entry->module=ConstantString("JP2"); |
| 592 | entry->magick=(IsImageFormatHandler *) IsJP2; |
| 593 | entry->adjoin=MagickFalse; |
| 594 | entry->seekable_stream=MagickTrue; |
| 595 | entry->thread_support=NoThreadSupport; |
| 596 | #if defined(MAGICKCORE_LIBOPENJP2_DELEGATE) |
| 597 | entry->decoder=(DecodeImageHandler *) ReadJP2Image; |
| 598 | entry->encoder=(EncodeImageHandler *) WriteJP2Image; |
| 599 | #endif |
| 600 | (void) RegisterMagickInfo(entry); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 601 | entry=SetMagickInfo("JPT"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 602 | entry->description=ConstantString("JPEG-2000 File Format Syntax"); |
cristy | bcd740c | 2014-01-03 21:08:39 +0000 | [diff] [blame] | 603 | if (*version != '\0') |
| 604 | entry->version=ConstantString(version); |
cristy | 5aefbeb | 2013-08-09 12:13:32 +0000 | [diff] [blame] | 605 | entry->mime_type=ConstantString("image/jp2"); |
cristy | 02f1fda | 2009-12-10 15:23:56 +0000 | [diff] [blame] | 606 | entry->module=ConstantString("JP2"); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 607 | entry->magick=(IsImageFormatHandler *) IsJP2; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 608 | entry->adjoin=MagickFalse; |
| 609 | entry->seekable_stream=MagickTrue; |
| 610 | entry->thread_support=NoThreadSupport; |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 611 | #if defined(MAGICKCORE_LIBOPENJP2_DELEGATE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 612 | entry->decoder=(DecodeImageHandler *) ReadJP2Image; |
| 613 | entry->encoder=(EncodeImageHandler *) WriteJP2Image; |
| 614 | #endif |
| 615 | (void) RegisterMagickInfo(entry); |
cristy | 92f84a5 | 2014-01-02 15:10:12 +0000 | [diff] [blame] | 616 | entry=SetMagickInfo("JPC"); |
| 617 | entry->description=ConstantString("JPEG-2000 Code Stream Syntax"); |
cristy | bcd740c | 2014-01-03 21:08:39 +0000 | [diff] [blame] | 618 | if (*version != '\0') |
| 619 | entry->version=ConstantString(version); |
cristy | 92f84a5 | 2014-01-02 15:10:12 +0000 | [diff] [blame] | 620 | entry->mime_type=ConstantString("image/jp2"); |
| 621 | entry->module=ConstantString("JP2"); |
cristy | 9f138af | 2014-02-16 12:54:17 +0000 | [diff] [blame] | 622 | entry->magick=(IsImageFormatHandler *) IsJP2; |
cristy | 92f84a5 | 2014-01-02 15:10:12 +0000 | [diff] [blame] | 623 | entry->adjoin=MagickFalse; |
| 624 | entry->seekable_stream=MagickTrue; |
| 625 | entry->thread_support=NoThreadSupport; |
| 626 | #if defined(MAGICKCORE_LIBOPENJP2_DELEGATE) |
| 627 | entry->decoder=(DecodeImageHandler *) ReadJP2Image; |
| 628 | entry->encoder=(EncodeImageHandler *) WriteJP2Image; |
| 629 | #endif |
| 630 | (void) RegisterMagickInfo(entry); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 631 | return(MagickImageCoderSignature); |
| 632 | } |
| 633 | |
| 634 | /* |
| 635 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 636 | % % |
| 637 | % % |
| 638 | % % |
| 639 | % U n r e g i s t e r J P 2 I m a g e % |
| 640 | % % |
| 641 | % % |
| 642 | % % |
| 643 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 644 | % |
| 645 | % UnregisterJP2Image() removes format registrations made by the JP2 module |
| 646 | % from the list of supported formats. |
| 647 | % |
| 648 | % The format of the UnregisterJP2Image method is: |
| 649 | % |
| 650 | % UnregisterJP2Image(void) |
| 651 | % |
| 652 | */ |
| 653 | ModuleExport void UnregisterJP2Image(void) |
| 654 | { |
cristy | 92f84a5 | 2014-01-02 15:10:12 +0000 | [diff] [blame] | 655 | (void) UnregisterMagickInfo("JPC"); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 656 | (void) UnregisterMagickInfo("JPT"); |
cristy | 2267fa4 | 2014-09-12 10:27:47 +0000 | [diff] [blame] | 657 | (void) UnregisterMagickInfo("JPM"); |
cristy | 02f1fda | 2009-12-10 15:23:56 +0000 | [diff] [blame] | 658 | (void) UnregisterMagickInfo("JP2"); |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 659 | (void) UnregisterMagickInfo("J2K"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 660 | } |
| 661 | |
cristy | 2599725 | 2014-01-02 13:28:18 +0000 | [diff] [blame] | 662 | #if defined(MAGICKCORE_LIBOPENJP2_DELEGATE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 663 | /* |
| 664 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 665 | % % |
| 666 | % % |
| 667 | % % |
| 668 | % W r i t e J P 2 I m a g e % |
| 669 | % % |
| 670 | % % |
| 671 | % % |
| 672 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 673 | % |
| 674 | % WriteJP2Image() writes an image in the JPEG 2000 image format. |
| 675 | % |
| 676 | % JP2 support originally written by Nathan Brown, nathanbrown@letu.edu |
| 677 | % |
| 678 | % The format of the WriteJP2Image method is: |
| 679 | % |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 680 | % MagickBooleanType WriteJP2Image(const ImageInfo *image_info,Image *image, |
| 681 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 682 | % |
| 683 | % A description of each parameter follows. |
| 684 | % |
| 685 | % o image_info: the image info. |
| 686 | % |
| 687 | % o image: The image. |
| 688 | % |
| 689 | */ |
cristy | 6d61450 | 2014-01-05 14:27:40 +0000 | [diff] [blame] | 690 | |
| 691 | static void CinemaProfileCompliance(const opj_image_t *jp2_image, |
| 692 | opj_cparameters_t *parameters) |
| 693 | { |
| 694 | /* |
cristy | 121d660 | 2014-01-05 14:35:51 +0000 | [diff] [blame] | 695 | Digital Cinema 4K profile compliant codestream. |
cristy | 6d61450 | 2014-01-05 14:27:40 +0000 | [diff] [blame] | 696 | */ |
| 697 | parameters->tile_size_on=OPJ_FALSE; |
| 698 | parameters->cp_tdx=1; |
| 699 | parameters->cp_tdy=1; |
| 700 | parameters->tp_flag='C'; |
| 701 | parameters->tp_on=1; |
| 702 | parameters->cp_tx0=0; |
| 703 | parameters->cp_ty0=0; |
| 704 | parameters->image_offset_x0=0; |
| 705 | parameters->image_offset_y0=0; |
| 706 | parameters->cblockw_init=32; |
| 707 | parameters->cblockh_init=32; |
| 708 | parameters->csty|=0x01; |
| 709 | parameters->prog_order=OPJ_CPRL; |
| 710 | parameters->roi_compno=(-1); |
| 711 | parameters->subsampling_dx=1; |
| 712 | parameters->subsampling_dy=1; |
| 713 | parameters->irreversible=1; |
| 714 | if ((jp2_image->comps[0].w == 2048) || (jp2_image->comps[0].h == 1080)) |
| 715 | { |
| 716 | /* |
| 717 | Digital Cinema 2K. |
| 718 | */ |
| 719 | parameters->cp_cinema=OPJ_CINEMA2K_24; |
| 720 | parameters->cp_rsiz=OPJ_CINEMA2K; |
| 721 | parameters->max_comp_size=1041666; |
| 722 | if (parameters->numresolution > 6) |
| 723 | parameters->numresolution=6; |
| 724 | |
| 725 | } |
| 726 | if ((jp2_image->comps[0].w == 4096) || (jp2_image->comps[0].h == 2160)) |
| 727 | { |
| 728 | /* |
| 729 | Digital Cinema 4K. |
| 730 | */ |
| 731 | parameters->cp_cinema=OPJ_CINEMA4K_24; |
| 732 | parameters->cp_rsiz=OPJ_CINEMA4K; |
| 733 | parameters->max_comp_size=1041666; |
| 734 | if (parameters->numresolution < 1) |
| 735 | parameters->numresolution=1; |
| 736 | if (parameters->numresolution > 7) |
| 737 | parameters->numresolution=7; |
| 738 | parameters->numpocs=2; |
| 739 | parameters->POC[0].tile=1; |
| 740 | parameters->POC[0].resno0=0; |
| 741 | parameters->POC[0].compno0=0; |
| 742 | parameters->POC[0].layno1=1; |
| 743 | parameters->POC[0].resno1=parameters->numresolution-1; |
| 744 | parameters->POC[0].compno1=3; |
| 745 | parameters->POC[0].prg1=OPJ_CPRL; |
| 746 | parameters->POC[1].tile=1; |
| 747 | parameters->POC[1].resno0=parameters->numresolution-1; |
| 748 | parameters->POC[1].compno0=0; |
| 749 | parameters->POC[1].layno1=1; |
| 750 | parameters->POC[1].resno1=parameters->numresolution; |
| 751 | parameters->POC[1].compno1=3; |
| 752 | parameters->POC[1].prg1=OPJ_CPRL; |
| 753 | } |
| 754 | parameters->tcp_numlayers=1; |
| 755 | parameters->tcp_rates[0]=((float) (jp2_image->numcomps*jp2_image->comps[0].w* |
| 756 | jp2_image->comps[0].h*jp2_image->comps[0].prec))/(parameters->max_comp_size* |
| 757 | 8*jp2_image->comps[0].dx*jp2_image->comps[0].dy); |
| 758 | parameters->cp_disto_alloc=1; |
| 759 | } |
| 760 | |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 761 | static MagickBooleanType WriteJP2Image(const ImageInfo *image_info,Image *image, |
| 762 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 763 | { |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 764 | const char |
cristy | 73bbee1 | 2014-01-05 00:43:38 +0000 | [diff] [blame] | 765 | *option, |
| 766 | *property; |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 767 | |
| 768 | int |
| 769 | jp2_status; |
| 770 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 771 | MagickBooleanType |
| 772 | status; |
| 773 | |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 774 | opj_codec_t |
| 775 | *jp2_codec; |
| 776 | |
| 777 | OPJ_COLOR_SPACE |
| 778 | jp2_colorspace; |
| 779 | |
| 780 | opj_cparameters_t |
| 781 | parameters; |
| 782 | |
| 783 | opj_image_cmptparm_t |
| 784 | jp2_info[5]; |
| 785 | |
| 786 | opj_image_t |
| 787 | *jp2_image; |
| 788 | |
| 789 | opj_stream_t |
| 790 | *jp2_stream; |
| 791 | |
| 792 | register ssize_t |
| 793 | i; |
| 794 | |
| 795 | ssize_t |
| 796 | y; |
| 797 | |
| 798 | size_t |
| 799 | channels; |
| 800 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 801 | /* |
| 802 | Open image file. |
| 803 | */ |
| 804 | assert(image_info != (const ImageInfo *) NULL); |
| 805 | assert(image_info->signature == MagickSignature); |
| 806 | assert(image != (Image *) NULL); |
| 807 | assert(image->signature == MagickSignature); |
| 808 | if (image->debug != MagickFalse) |
| 809 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 810 | assert(exception != (ExceptionInfo *) NULL); |
| 811 | assert(exception->signature == MagickSignature); |
cristy | db9258a | 2014-01-03 20:26:19 +0000 | [diff] [blame] | 812 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 813 | if (status == MagickFalse) |
| 814 | return(status); |
| 815 | /* |
glennrp | bca49a2 | 2011-07-01 12:18:22 +0000 | [diff] [blame] | 816 | Initialize JPEG 2000 API. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 817 | */ |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 818 | opj_set_default_encoder_parameters(¶meters); |
| 819 | for (i=1; i < 6; i++) |
cristy | 4f97673 | 2014-05-09 22:21:21 +0000 | [diff] [blame] | 820 | if (((1U << (i+2)) > image->columns) && ((1U << (i+2)) > image->rows)) |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 821 | break; |
| 822 | parameters.numresolution=i; |
cristy | 73bbee1 | 2014-01-05 00:43:38 +0000 | [diff] [blame] | 823 | option=GetImageOption(image_info,"jp2:number-resolutions"); |
| 824 | if (option != (const char *) NULL) |
| 825 | parameters.numresolution=StringToInteger(option); |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 826 | parameters.tcp_numlayers=1; |
cristy | 6d61450 | 2014-01-05 14:27:40 +0000 | [diff] [blame] | 827 | parameters.tcp_rates[0]=0; /* lossless */ |
| 828 | parameters.cp_disto_alloc=1; |
| 829 | if (image->quality != 0) |
| 830 | { |
| 831 | parameters.tcp_distoratio[0]=(double) image->quality; |
| 832 | parameters.cp_fixed_quality=OPJ_TRUE; |
| 833 | } |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 834 | if (image_info->extract != (char *) NULL) |
| 835 | { |
| 836 | RectangleInfo |
| 837 | geometry; |
| 838 | |
| 839 | int |
| 840 | flags; |
| 841 | |
| 842 | /* |
| 843 | Set tile size. |
| 844 | */ |
| 845 | flags=ParseAbsoluteGeometry(image_info->extract,&geometry); |
cristy | dabb4d8 | 2014-10-25 17:51:16 +0000 | [diff] [blame] | 846 | parameters.cp_tdx=(int) geometry.width; |
| 847 | parameters.cp_tdy=(int) geometry.width; |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 848 | if ((flags & HeightValue) != 0) |
cristy | dabb4d8 | 2014-10-25 17:51:16 +0000 | [diff] [blame] | 849 | parameters.cp_tdy=(int) geometry.height; |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 850 | if ((flags & XValue) != 0) |
| 851 | parameters.cp_tx0=geometry.x; |
| 852 | if ((flags & YValue) != 0) |
| 853 | parameters.cp_ty0=geometry.y; |
| 854 | parameters.tile_size_on=OPJ_TRUE; |
| 855 | } |
cristy | 73bbee1 | 2014-01-05 00:43:38 +0000 | [diff] [blame] | 856 | option=GetImageOption(image_info,"jp2:quality"); |
| 857 | if (option != (const char *) NULL) |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 858 | { |
| 859 | register const char |
| 860 | *p; |
| 861 | |
| 862 | /* |
| 863 | Set quality PSNR. |
| 864 | */ |
cristy | 73bbee1 | 2014-01-05 00:43:38 +0000 | [diff] [blame] | 865 | p=option; |
cristy | 21e8485 | 2014-01-12 19:29:02 +0000 | [diff] [blame] | 866 | for (i=0; sscanf(p,"%f",¶meters.tcp_distoratio[i]) == 1; i++) |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 867 | { |
| 868 | if (i > 100) |
| 869 | break; |
| 870 | while ((*p != '\0') && (*p != ',')) |
| 871 | p++; |
| 872 | if (*p == '\0') |
| 873 | break; |
| 874 | p++; |
| 875 | } |
cristy | 21e8485 | 2014-01-12 19:29:02 +0000 | [diff] [blame] | 876 | parameters.tcp_numlayers=i+1; |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 877 | parameters.cp_fixed_quality=OPJ_TRUE; |
| 878 | } |
cristy | 5fd5a23 | 2014-01-05 14:41:50 +0000 | [diff] [blame] | 879 | option=GetImageOption(image_info,"jp2:progression-order"); |
| 880 | if (option != (const char *) NULL) |
| 881 | { |
| 882 | if (LocaleCompare(option,"LRCP") == 0) |
| 883 | parameters.prog_order=OPJ_LRCP; |
| 884 | if (LocaleCompare(option,"RLCP") == 0) |
| 885 | parameters.prog_order=OPJ_RLCP; |
| 886 | if (LocaleCompare(option,"RPCL") == 0) |
| 887 | parameters.prog_order=OPJ_RPCL; |
| 888 | if (LocaleCompare(option,"PCRL") == 0) |
| 889 | parameters.prog_order=OPJ_PCRL; |
| 890 | if (LocaleCompare(option,"CPRL") == 0) |
| 891 | parameters.prog_order=OPJ_CPRL; |
| 892 | } |
cristy | 73bbee1 | 2014-01-05 00:43:38 +0000 | [diff] [blame] | 893 | option=GetImageOption(image_info,"jp2:rate"); |
| 894 | if (option != (const char *) NULL) |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 895 | { |
| 896 | register const char |
| 897 | *p; |
| 898 | |
| 899 | /* |
| 900 | Set compression rate. |
| 901 | */ |
cristy | 73bbee1 | 2014-01-05 00:43:38 +0000 | [diff] [blame] | 902 | p=option; |
cristy | 21e8485 | 2014-01-12 19:29:02 +0000 | [diff] [blame] | 903 | for (i=0; sscanf(p,"%f",¶meters.tcp_rates[i]) == 1; i++) |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 904 | { |
cristy | f2a82ee | 2014-05-26 17:49:54 +0000 | [diff] [blame] | 905 | if (i >= 100) |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 906 | break; |
| 907 | while ((*p != '\0') && (*p != ',')) |
| 908 | p++; |
| 909 | if (*p == '\0') |
| 910 | break; |
| 911 | p++; |
| 912 | } |
cristy | 21e8485 | 2014-01-12 19:29:02 +0000 | [diff] [blame] | 913 | parameters.tcp_numlayers=i+1; |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 914 | parameters.cp_disto_alloc=OPJ_TRUE; |
| 915 | } |
cristy | 2598c74 | 2014-01-05 14:30:44 +0000 | [diff] [blame] | 916 | if (image_info->sampling_factor != (const char *) NULL) |
| 917 | (void) sscanf(image_info->sampling_factor,"%d,%d", |
| 918 | ¶meters.subsampling_dx,¶meters.subsampling_dy); |
cristy | 73bbee1 | 2014-01-05 00:43:38 +0000 | [diff] [blame] | 919 | property=GetImageProperty(image,"comment",exception); |
| 920 | if (property != (const char *) NULL) |
| 921 | parameters.cp_comment=ConstantString(property); |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 922 | channels=3; |
| 923 | jp2_colorspace=OPJ_CLRSPC_SRGB; |
| 924 | if (image->colorspace == YUVColorspace) |
| 925 | { |
| 926 | jp2_colorspace=OPJ_CLRSPC_SYCC; |
| 927 | parameters.subsampling_dx=2; |
| 928 | } |
| 929 | else |
| 930 | { |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 931 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 932 | if (IsGrayColorspace(image->colorspace) != MagickFalse) |
| 933 | { |
| 934 | channels=1; |
| 935 | jp2_colorspace=OPJ_CLRSPC_GRAY; |
| 936 | } |
| 937 | if (image->alpha_trait == BlendPixelTrait) |
| 938 | channels++; |
| 939 | } |
cristy | 6d61450 | 2014-01-05 14:27:40 +0000 | [diff] [blame] | 940 | parameters.tcp_mct=channels == 3 ? 1 : 0; |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 941 | ResetMagickMemory(jp2_info,0,sizeof(jp2_info)); |
| 942 | for (i=0; i < (ssize_t) channels; i++) |
| 943 | { |
| 944 | jp2_info[i].prec=image->depth; |
| 945 | jp2_info[i].bpp=image->depth; |
| 946 | if ((image->depth == 1) && |
| 947 | ((LocaleCompare(image_info->magick,"JPT") == 0) || |
| 948 | (LocaleCompare(image_info->magick,"JP2") == 0))) |
| 949 | { |
| 950 | jp2_info[i].prec++; /* OpenJPEG returns exception for depth @ 1 */ |
| 951 | jp2_info[i].bpp++; |
| 952 | } |
| 953 | jp2_info[i].sgnd=0; |
| 954 | jp2_info[i].dx=parameters.subsampling_dx; |
| 955 | jp2_info[i].dy=parameters.subsampling_dy; |
| 956 | jp2_info[i].w=image->columns; |
| 957 | jp2_info[i].h=image->rows; |
| 958 | } |
| 959 | jp2_image=opj_image_create(channels,jp2_info,jp2_colorspace); |
| 960 | if (jp2_image == (opj_image_t *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 961 | ThrowWriterException(DelegateError,"UnableToEncodeImageFile"); |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 962 | jp2_image->x0=parameters.image_offset_x0; |
| 963 | jp2_image->y0=parameters.image_offset_y0; |
cristy | dabb4d8 | 2014-10-25 17:51:16 +0000 | [diff] [blame] | 964 | jp2_image->x1=(unsigned int) (2*parameters.image_offset_x0+(image->columns-1)* |
| 965 | parameters.subsampling_dx+1); |
| 966 | jp2_image->y1=(unsigned int) (2*parameters.image_offset_y0+(image->rows-1)* |
| 967 | parameters.subsampling_dx+1); |
cristy | 6d61450 | 2014-01-05 14:27:40 +0000 | [diff] [blame] | 968 | if ((image->depth == 12) && |
| 969 | ((image->columns == 2048) || (image->rows == 1080) || |
| 970 | (image->columns == 4096) || (image->rows == 2160))) |
| 971 | CinemaProfileCompliance(jp2_image,¶meters); |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 972 | /* |
| 973 | Convert to JP2 pixels. |
| 974 | */ |
| 975 | for (y=0; y < (ssize_t) image->rows; y++) |
| 976 | { |
| 977 | register const Quantum |
| 978 | *p; |
| 979 | |
| 980 | ssize_t |
| 981 | x; |
| 982 | |
| 983 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| 984 | if (p == (const Quantum *) NULL) |
| 985 | break; |
| 986 | for (x=0; x < (ssize_t) image->columns; x++) |
| 987 | { |
| 988 | for (i=0; i < (ssize_t) channels; i++) |
| 989 | { |
| 990 | double |
| 991 | scale; |
| 992 | |
| 993 | register int |
| 994 | *q; |
| 995 | |
| 996 | scale=(double) ((1UL << jp2_image->comps[i].prec)-1)/QuantumRange; |
| 997 | q=jp2_image->comps[i].data+(y/jp2_image->comps[i].dy* |
| 998 | image->columns/jp2_image->comps[i].dx+x/jp2_image->comps[i].dx); |
| 999 | switch (i) |
| 1000 | { |
| 1001 | case 0: |
| 1002 | { |
| 1003 | if (jp2_colorspace == OPJ_CLRSPC_GRAY) |
| 1004 | { |
| 1005 | *q=(int) (scale*GetPixelLuma(image,p)); |
| 1006 | break; |
| 1007 | } |
| 1008 | *q=(int) (scale*GetPixelRed(image,p)); |
| 1009 | break; |
| 1010 | } |
| 1011 | case 1: |
| 1012 | { |
| 1013 | if (jp2_colorspace == OPJ_CLRSPC_GRAY) |
| 1014 | { |
| 1015 | *q=(int) (scale*GetPixelAlpha(image,p)); |
| 1016 | break; |
| 1017 | } |
| 1018 | *q=(int) (scale*GetPixelGreen(image,p)); |
| 1019 | break; |
| 1020 | } |
| 1021 | case 2: |
| 1022 | { |
| 1023 | *q=(int) (scale*GetPixelBlue(image,p)); |
| 1024 | break; |
| 1025 | } |
| 1026 | case 3: |
| 1027 | { |
| 1028 | *q=(int) (scale*GetPixelAlpha(image,p)); |
| 1029 | break; |
| 1030 | } |
| 1031 | } |
| 1032 | } |
cristy | 5ecce99 | 2014-01-12 16:01:44 +0000 | [diff] [blame] | 1033 | p+=GetPixelChannels(image); |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 1034 | } |
| 1035 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 1036 | image->rows); |
| 1037 | if (status == MagickFalse) |
| 1038 | break; |
| 1039 | } |
| 1040 | if (LocaleCompare(image_info->magick,"JPT") == 0) |
| 1041 | jp2_codec=opj_create_compress(OPJ_CODEC_JPT); |
| 1042 | else |
| 1043 | if (LocaleCompare(image_info->magick,"J2K") == 0) |
| 1044 | jp2_codec=opj_create_compress(OPJ_CODEC_J2K); |
| 1045 | else |
| 1046 | jp2_codec=opj_create_compress(OPJ_CODEC_JP2); |
| 1047 | opj_set_warning_handler(jp2_codec,JP2WarningHandler,exception); |
| 1048 | opj_set_error_handler(jp2_codec,JP2ErrorHandler,exception); |
| 1049 | opj_setup_encoder(jp2_codec,¶meters,jp2_image); |
| 1050 | jp2_stream=opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE,OPJ_FALSE); |
| 1051 | opj_stream_set_read_function(jp2_stream,JP2ReadHandler); |
| 1052 | opj_stream_set_write_function(jp2_stream,JP2WriteHandler); |
| 1053 | opj_stream_set_seek_function(jp2_stream,JP2SeekHandler); |
| 1054 | opj_stream_set_skip_function(jp2_stream,JP2SkipHandler); |
cristy | b46443e | 2014-05-08 12:32:16 +0000 | [diff] [blame] | 1055 | opj_stream_set_user_data(jp2_stream,image,NULL); |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 1056 | if (jp2_stream == (opj_stream_t *) NULL) |
| 1057 | ThrowWriterException(DelegateError,"UnableToEncodeImageFile"); |
| 1058 | jp2_status=opj_start_compress(jp2_codec,jp2_image,jp2_stream); |
| 1059 | if (jp2_status == 0) |
| 1060 | ThrowWriterException(DelegateError,"UnableToEncodeImageFile"); |
| 1061 | if ((opj_encode(jp2_codec,jp2_stream) == 0) || |
| 1062 | (opj_end_compress(jp2_codec,jp2_stream) == 0)) |
| 1063 | { |
cristy | 3f62cbf | 2014-04-06 13:02:24 +0000 | [diff] [blame] | 1064 | opj_stream_destroy(jp2_stream); |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 1065 | opj_destroy_codec(jp2_codec); |
| 1066 | opj_image_destroy(jp2_image); |
| 1067 | ThrowWriterException(DelegateError,"UnableToEncodeImageFile"); |
| 1068 | } |
| 1069 | /* |
| 1070 | Free resources. |
| 1071 | */ |
cristy | 3f62cbf | 2014-04-06 13:02:24 +0000 | [diff] [blame] | 1072 | opj_stream_destroy(jp2_stream); |
cristy | 7ea3496 | 2014-01-04 18:03:30 +0000 | [diff] [blame] | 1073 | opj_destroy_codec(jp2_codec); |
| 1074 | opj_image_destroy(jp2_image); |
| 1075 | (void) CloseBlob(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1076 | return(MagickTrue); |
| 1077 | } |
| 1078 | #endif |