cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % JJJJJ PPPP EEEEE GGGG % |
| 7 | % J P P E G % |
| 8 | % J PPPP EEE G GG % |
| 9 | % J J P E G G % |
| 10 | % JJJ P EEEEE GGG % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write JPEG Image Format % |
| 14 | % % |
| 15 | % Software Design % |
cristy | 5328a4c | 2013-12-03 11:32:13 +0000 | [diff] [blame] | 16 | % John Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 5328a4c | 2013-12-03 11:32:13 +0000 | [diff] [blame] | 20 | % Copyright 1999-2013 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 | % This software is based in part on the work of the Independent JPEG Group. |
| 37 | % See ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz for copyright and |
| 38 | % licensing restrictions. Blob support contributed by Glenn Randers-Pehrson. |
| 39 | % |
| 40 | % |
| 41 | */ |
dirk | 29dd80e | 2013-10-31 23:11:11 +0000 | [diff] [blame] | 42 | |
| 43 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 44 | /* |
| 45 | Include declarations. |
| 46 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 47 | #include "MagickCore/studio.h" |
cristy | c634861 | 2013-04-05 14:24:24 +0000 | [diff] [blame] | 48 | #include "MagickCore/artifact.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 49 | #include "MagickCore/attribute.h" |
| 50 | #include "MagickCore/blob.h" |
| 51 | #include "MagickCore/blob-private.h" |
| 52 | #include "MagickCore/cache.h" |
| 53 | #include "MagickCore/color.h" |
| 54 | #include "MagickCore/colormap-private.h" |
| 55 | #include "MagickCore/color-private.h" |
| 56 | #include "MagickCore/colormap.h" |
| 57 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 58 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 59 | #include "MagickCore/constitute.h" |
| 60 | #include "MagickCore/exception.h" |
| 61 | #include "MagickCore/exception-private.h" |
| 62 | #include "MagickCore/geometry.h" |
| 63 | #include "MagickCore/image.h" |
| 64 | #include "MagickCore/image-private.h" |
| 65 | #include "MagickCore/list.h" |
| 66 | #include "MagickCore/log.h" |
| 67 | #include "MagickCore/magick.h" |
| 68 | #include "MagickCore/memory_.h" |
| 69 | #include "MagickCore/module.h" |
| 70 | #include "MagickCore/monitor.h" |
| 71 | #include "MagickCore/monitor-private.h" |
| 72 | #include "MagickCore/option.h" |
cristy | 53b6cc3 | 2014-06-25 13:13:33 +0000 | [diff] [blame] | 73 | #include "MagickCore/option-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 74 | #include "MagickCore/pixel-accessor.h" |
| 75 | #include "MagickCore/profile.h" |
| 76 | #include "MagickCore/property.h" |
| 77 | #include "MagickCore/quantum-private.h" |
| 78 | #include "MagickCore/resource_.h" |
cristy | 191ba5c | 2014-03-16 21:26:40 +0000 | [diff] [blame] | 79 | #include "MagickCore/semaphore.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 80 | #include "MagickCore/splay-tree.h" |
| 81 | #include "MagickCore/static.h" |
| 82 | #include "MagickCore/string_.h" |
| 83 | #include "MagickCore/string-private.h" |
cristy | e40005d | 2012-03-23 12:18:45 +0000 | [diff] [blame] | 84 | #include "MagickCore/token.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 85 | #include "MagickCore/utility.h" |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 86 | #include "MagickCore/xml-tree.h" |
| 87 | #include "MagickCore/xml-tree-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 88 | #include <setjmp.h> |
| 89 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 90 | #define JPEG_INTERNAL_OPTIONS |
cristy | 07a3cca | 2012-12-10 13:09:10 +0000 | [diff] [blame] | 91 | #if defined(__MINGW32__) || defined(__MINGW64__) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 92 | # define XMD_H 1 /* Avoid conflicting typedef for INT32 */ |
| 93 | #endif |
| 94 | #undef HAVE_STDLIB_H |
| 95 | #include "jpeglib.h" |
| 96 | #include "jerror.h" |
| 97 | #endif |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 98 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 99 | /* |
| 100 | Define declarations. |
| 101 | */ |
| 102 | #define ICC_MARKER (JPEG_APP0+2) |
| 103 | #define ICC_PROFILE "ICC_PROFILE" |
| 104 | #define IPTC_MARKER (JPEG_APP0+13) |
| 105 | #define XML_MARKER (JPEG_APP0+1) |
cristy | eb9759e | 2012-06-07 23:06:29 +0000 | [diff] [blame] | 106 | #define MaxBufferExtent 16384 |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 107 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 108 | /* |
| 109 | Typedef declarations. |
| 110 | */ |
| 111 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 112 | typedef struct _DestinationManager |
| 113 | { |
| 114 | struct jpeg_destination_mgr |
| 115 | manager; |
| 116 | |
| 117 | Image |
| 118 | *image; |
| 119 | |
| 120 | JOCTET |
| 121 | *buffer; |
| 122 | } DestinationManager; |
| 123 | |
| 124 | typedef struct _ErrorManager |
| 125 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 126 | ExceptionInfo |
| 127 | *exception; |
| 128 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 129 | Image |
| 130 | *image; |
| 131 | |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 132 | MagickBooleanType |
| 133 | finished; |
| 134 | |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 135 | StringInfo |
| 136 | *profile; |
| 137 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 138 | jmp_buf |
| 139 | error_recovery; |
| 140 | } ErrorManager; |
| 141 | |
| 142 | typedef struct _SourceManager |
| 143 | { |
| 144 | struct jpeg_source_mgr |
| 145 | manager; |
| 146 | |
| 147 | Image |
| 148 | *image; |
| 149 | |
| 150 | JOCTET |
| 151 | *buffer; |
| 152 | |
| 153 | boolean |
| 154 | start_of_blob; |
| 155 | } SourceManager; |
| 156 | #endif |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 157 | |
| 158 | typedef struct _QuantizationTable |
| 159 | { |
| 160 | char |
| 161 | *slot, |
| 162 | *description; |
| 163 | |
| 164 | size_t |
| 165 | width, |
| 166 | height; |
| 167 | |
cristy | 043f3f3 | 2012-03-02 17:37:28 +0000 | [diff] [blame] | 168 | double |
| 169 | divisor; |
| 170 | |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 171 | unsigned int |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 172 | *levels; |
| 173 | } QuantizationTable; |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 174 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 175 | /* |
| 176 | Forward declarations. |
| 177 | */ |
| 178 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 179 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 180 | WriteJPEGImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 181 | #endif |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 182 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 183 | /* |
| 184 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 185 | % % |
| 186 | % % |
| 187 | % % |
| 188 | % I s J P E G % |
| 189 | % % |
| 190 | % % |
| 191 | % % |
| 192 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 193 | % |
| 194 | % IsJPEG() returns MagickTrue if the image format type, identified by the |
| 195 | % magick string, is JPEG. |
| 196 | % |
| 197 | % The format of the IsJPEG method is: |
| 198 | % |
| 199 | % MagickBooleanType IsJPEG(const unsigned char *magick,const size_t length) |
| 200 | % |
| 201 | % A description of each parameter follows: |
| 202 | % |
| 203 | % o magick: compare image format pattern against these bytes. |
| 204 | % |
| 205 | % o length: Specifies the length of the magick string. |
| 206 | % |
| 207 | */ |
| 208 | static MagickBooleanType IsJPEG(const unsigned char *magick,const size_t length) |
| 209 | { |
| 210 | if (length < 3) |
| 211 | return(MagickFalse); |
| 212 | if (memcmp(magick,"\377\330\377",3) == 0) |
| 213 | return(MagickTrue); |
| 214 | return(MagickFalse); |
| 215 | } |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 216 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 217 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 218 | /* |
| 219 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 220 | % % |
| 221 | % % |
| 222 | % % |
| 223 | % R e a d J P E G I m a g e % |
| 224 | % % |
| 225 | % % |
| 226 | % % |
| 227 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 228 | % |
| 229 | % ReadJPEGImage() reads a JPEG image file and returns it. It allocates |
| 230 | % the memory necessary for the new Image structure and returns a pointer to |
| 231 | % the new image. |
| 232 | % |
| 233 | % The format of the ReadJPEGImage method is: |
| 234 | % |
| 235 | % Image *ReadJPEGImage(const ImageInfo *image_info, |
| 236 | % ExceptionInfo *exception) |
| 237 | % |
| 238 | % A description of each parameter follows: |
| 239 | % |
| 240 | % o image_info: the image info. |
| 241 | % |
| 242 | % o exception: return any errors or warnings in this structure. |
| 243 | % |
| 244 | */ |
| 245 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 246 | static boolean FillInputBuffer(j_decompress_ptr cinfo) |
| 247 | { |
| 248 | SourceManager |
| 249 | *source; |
| 250 | |
| 251 | source=(SourceManager *) cinfo->src; |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 252 | source->manager.bytes_in_buffer=(size_t) ReadBlob(source->image, |
| 253 | MaxBufferExtent,source->buffer); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 254 | if (source->manager.bytes_in_buffer == 0) |
| 255 | { |
cristy | 453b820 | 2012-06-07 22:57:52 +0000 | [diff] [blame] | 256 | if (source->start_of_blob != FALSE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 257 | ERREXIT(cinfo,JERR_INPUT_EMPTY); |
| 258 | WARNMS(cinfo,JWRN_JPEG_EOF); |
| 259 | source->buffer[0]=(JOCTET) 0xff; |
| 260 | source->buffer[1]=(JOCTET) JPEG_EOI; |
| 261 | source->manager.bytes_in_buffer=2; |
| 262 | } |
| 263 | source->manager.next_input_byte=source->buffer; |
| 264 | source->start_of_blob=FALSE; |
| 265 | return(TRUE); |
| 266 | } |
| 267 | |
| 268 | static int GetCharacter(j_decompress_ptr jpeg_info) |
| 269 | { |
| 270 | if (jpeg_info->src->bytes_in_buffer == 0) |
| 271 | (void) (*jpeg_info->src->fill_input_buffer)(jpeg_info); |
| 272 | jpeg_info->src->bytes_in_buffer--; |
| 273 | return((int) GETJOCTET(*jpeg_info->src->next_input_byte++)); |
| 274 | } |
| 275 | |
| 276 | static void InitializeSource(j_decompress_ptr cinfo) |
| 277 | { |
| 278 | SourceManager |
| 279 | *source; |
| 280 | |
| 281 | source=(SourceManager *) cinfo->src; |
| 282 | source->start_of_blob=TRUE; |
| 283 | } |
| 284 | |
cristy | e8dd130 | 2009-11-11 02:45:03 +0000 | [diff] [blame] | 285 | static MagickBooleanType IsITUFaxImage(const Image *image) |
| 286 | { |
| 287 | const StringInfo |
| 288 | *profile; |
| 289 | |
| 290 | const unsigned char |
| 291 | *datum; |
| 292 | |
cristy | ace6aa4 | 2009-11-11 03:17:33 +0000 | [diff] [blame] | 293 | profile=GetImageProfile(image,"8bim"); |
cristy | e8dd130 | 2009-11-11 02:45:03 +0000 | [diff] [blame] | 294 | if (profile == (const StringInfo *) NULL) |
| 295 | return(MagickFalse); |
| 296 | if (GetStringInfoLength(profile) < 5) |
| 297 | return(MagickFalse); |
| 298 | datum=GetStringInfoDatum(profile); |
| 299 | if ((datum[0] == 0x47) && (datum[1] == 0x33) && (datum[2] == 0x46) && |
| 300 | (datum[3] == 0x41) && (datum[4] == 0x58)) |
| 301 | return(MagickTrue); |
| 302 | return(MagickFalse); |
| 303 | } |
| 304 | |
cristy | 437c393 | 2011-10-12 18:03:46 +0000 | [diff] [blame] | 305 | static void JPEGErrorHandler(j_common_ptr jpeg_info) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 306 | { |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 307 | char |
| 308 | message[JMSG_LENGTH_MAX]; |
| 309 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 310 | ErrorManager |
| 311 | *error_manager; |
| 312 | |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 313 | ExceptionInfo |
| 314 | *exception; |
| 315 | |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 316 | Image |
| 317 | *image; |
| 318 | |
| 319 | *message='\0'; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 320 | error_manager=(ErrorManager *) jpeg_info->client_data; |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 321 | image=error_manager->image; |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 322 | exception=error_manager->exception; |
cristy | 86f3354 | 2011-05-21 22:58:33 +0000 | [diff] [blame] | 323 | (jpeg_info->err->format_message)(jpeg_info,message); |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 324 | if (image->debug != MagickFalse) |
cristy | 86f3354 | 2011-05-21 22:58:33 +0000 | [diff] [blame] | 325 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 326 | "[%s] JPEG Trace: \"%s\"",image->filename,message); |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 327 | if (error_manager->finished != MagickFalse) |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 328 | (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageWarning, |
| 329 | (char *) message,"`%s'",image->filename); |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 330 | else |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 331 | (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError, |
| 332 | (char *) message,"`%s'",image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 333 | longjmp(error_manager->error_recovery,1); |
| 334 | } |
| 335 | |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 336 | static MagickBooleanType JPEGWarningHandler(j_common_ptr jpeg_info,int level) |
| 337 | { |
cristy | f162e01 | 2012-03-28 12:54:33 +0000 | [diff] [blame] | 338 | #define JPEGExcessiveWarnings 1000 |
| 339 | |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 340 | char |
| 341 | message[JMSG_LENGTH_MAX]; |
| 342 | |
| 343 | ErrorManager |
| 344 | *error_manager; |
| 345 | |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 346 | ExceptionInfo |
| 347 | *exception; |
| 348 | |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 349 | Image |
| 350 | *image; |
| 351 | |
| 352 | *message='\0'; |
| 353 | error_manager=(ErrorManager *) jpeg_info->client_data; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 354 | exception=error_manager->exception; |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 355 | image=error_manager->image; |
| 356 | if (level < 0) |
| 357 | { |
| 358 | /* |
| 359 | Process warning message. |
| 360 | */ |
| 361 | (jpeg_info->err->format_message)(jpeg_info,message); |
cristy | f162e01 | 2012-03-28 12:54:33 +0000 | [diff] [blame] | 362 | if (jpeg_info->err->num_warnings++ > JPEGExcessiveWarnings) |
| 363 | JPEGErrorHandler(jpeg_info); |
cristy | 8043fb3 | 2012-06-28 16:14:12 +0000 | [diff] [blame] | 364 | ThrowBinaryException(CorruptImageWarning,(char *) message, |
| 365 | image->filename); |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 366 | } |
| 367 | else |
| 368 | if ((image->debug != MagickFalse) && |
| 369 | (level >= jpeg_info->err->trace_level)) |
| 370 | { |
| 371 | /* |
| 372 | Process trace message. |
| 373 | */ |
| 374 | (jpeg_info->err->format_message)(jpeg_info,message); |
| 375 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 376 | "[%s] JPEG Trace: \"%s\"",image->filename,message); |
| 377 | } |
| 378 | return(MagickTrue); |
| 379 | } |
| 380 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 381 | static boolean ReadComment(j_decompress_ptr jpeg_info) |
| 382 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 383 | ErrorManager |
| 384 | *error_manager; |
| 385 | |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 386 | ExceptionInfo |
| 387 | *exception; |
| 388 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 389 | Image |
| 390 | *image; |
| 391 | |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 392 | register unsigned char |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 393 | *p; |
| 394 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 395 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 396 | i; |
| 397 | |
| 398 | size_t |
| 399 | length; |
| 400 | |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 401 | StringInfo |
| 402 | *comment; |
| 403 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 404 | /* |
| 405 | Determine length of comment. |
| 406 | */ |
| 407 | error_manager=(ErrorManager *) jpeg_info->client_data; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 408 | exception=error_manager->exception; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 409 | image=error_manager->image; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 410 | length=(size_t) ((size_t) GetCharacter(jpeg_info) << 8); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 411 | length+=GetCharacter(jpeg_info); |
dirk | 896a34e | 2014-09-18 10:57:30 +0000 | [diff] [blame] | 412 | if (length <= 2) |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 413 | return(TRUE); |
dirk | 896a34e | 2014-09-18 10:57:30 +0000 | [diff] [blame] | 414 | length-=2; |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 415 | comment=BlobToStringInfo((const void *) NULL,length); |
| 416 | if (comment == (StringInfo *) NULL) |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 417 | { |
| 418 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 419 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 420 | return(FALSE); |
| 421 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 422 | /* |
| 423 | Read comment. |
| 424 | */ |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 425 | error_manager->profile=comment; |
| 426 | p=GetStringInfoDatum(comment); |
| 427 | for (i=0; i < (ssize_t) GetStringInfoLength(comment); i++) |
| 428 | *p++=(unsigned char) GetCharacter(jpeg_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 429 | *p='\0'; |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 430 | error_manager->profile=NULL; |
| 431 | p=GetStringInfoDatum(comment); |
| 432 | (void) SetImageProperty(image,"comment",(const char *) p,exception); |
| 433 | comment=DestroyStringInfo(comment); |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 434 | return(TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static boolean ReadICCProfile(j_decompress_ptr jpeg_info) |
| 438 | { |
| 439 | char |
| 440 | magick[12]; |
| 441 | |
| 442 | ErrorManager |
| 443 | *error_manager; |
| 444 | |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 445 | ExceptionInfo |
| 446 | *exception; |
| 447 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 448 | Image |
| 449 | *image; |
| 450 | |
| 451 | MagickBooleanType |
| 452 | status; |
| 453 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 454 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 455 | i; |
| 456 | |
| 457 | register unsigned char |
| 458 | *p; |
| 459 | |
| 460 | size_t |
| 461 | length; |
| 462 | |
| 463 | StringInfo |
| 464 | *icc_profile, |
| 465 | *profile; |
| 466 | |
| 467 | /* |
| 468 | Read color profile. |
| 469 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 470 | length=(size_t) ((size_t) GetCharacter(jpeg_info) << 8); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 471 | length+=(size_t) GetCharacter(jpeg_info); |
| 472 | length-=2; |
| 473 | if (length <= 14) |
| 474 | { |
| 475 | while (length-- > 0) |
| 476 | (void) GetCharacter(jpeg_info); |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 477 | return(TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 478 | } |
| 479 | for (i=0; i < 12; i++) |
| 480 | magick[i]=(char) GetCharacter(jpeg_info); |
| 481 | if (LocaleCompare(magick,ICC_PROFILE) != 0) |
| 482 | { |
| 483 | /* |
| 484 | Not a ICC profile, return. |
| 485 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 486 | for (i=0; i < (ssize_t) (length-12); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 487 | (void) GetCharacter(jpeg_info); |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 488 | return(TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 489 | } |
| 490 | (void) GetCharacter(jpeg_info); /* id */ |
| 491 | (void) GetCharacter(jpeg_info); /* markers */ |
| 492 | length-=14; |
| 493 | error_manager=(ErrorManager *) jpeg_info->client_data; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 494 | exception=error_manager->exception; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 495 | image=error_manager->image; |
cristy | 8723e4b | 2011-09-01 13:11:19 +0000 | [diff] [blame] | 496 | profile=BlobToStringInfo((const void *) NULL,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 497 | if (profile == (StringInfo *) NULL) |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 498 | { |
| 499 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 500 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 501 | return(FALSE); |
| 502 | } |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 503 | error_manager->profile=profile; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 504 | p=GetStringInfoDatum(profile); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 505 | for (i=(ssize_t) GetStringInfoLength(profile)-1; i >= 0; i--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 506 | *p++=(unsigned char) GetCharacter(jpeg_info); |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 507 | error_manager->profile=NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 508 | icc_profile=(StringInfo *) GetImageProfile(image,"icc"); |
| 509 | if (icc_profile != (StringInfo *) NULL) |
| 510 | { |
| 511 | ConcatenateStringInfo(icc_profile,profile); |
| 512 | profile=DestroyStringInfo(profile); |
| 513 | } |
| 514 | else |
| 515 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 516 | status=SetImageProfile(image,"icc",profile,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 517 | profile=DestroyStringInfo(profile); |
| 518 | if (status == MagickFalse) |
cristy | 9bb7c84 | 2014-06-17 23:42:24 +0000 | [diff] [blame] | 519 | { |
| 520 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 521 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 522 | return(FALSE); |
| 523 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 524 | } |
| 525 | if (image->debug != MagickFalse) |
| 526 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 527 | "Profile: ICC, %.20g bytes",(double) length); |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 528 | return(TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static boolean ReadIPTCProfile(j_decompress_ptr jpeg_info) |
| 532 | { |
| 533 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 534 | magick[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 535 | |
| 536 | ErrorManager |
| 537 | *error_manager; |
| 538 | |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 539 | ExceptionInfo |
| 540 | *exception; |
| 541 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 542 | Image |
| 543 | *image; |
| 544 | |
| 545 | MagickBooleanType |
| 546 | status; |
| 547 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 548 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 549 | i; |
| 550 | |
| 551 | register unsigned char |
| 552 | *p; |
| 553 | |
| 554 | size_t |
| 555 | length; |
| 556 | |
| 557 | StringInfo |
| 558 | *iptc_profile, |
| 559 | *profile; |
| 560 | |
| 561 | /* |
| 562 | Determine length of binary data stored here. |
| 563 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 564 | length=(size_t) ((size_t) GetCharacter(jpeg_info) << 8); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 565 | length+=(size_t) GetCharacter(jpeg_info); |
| 566 | length-=2; |
| 567 | if (length <= 14) |
| 568 | { |
| 569 | while (length-- > 0) |
| 570 | (void) GetCharacter(jpeg_info); |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 571 | return(TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 572 | } |
| 573 | /* |
| 574 | Validate that this was written as a Photoshop resource format slug. |
| 575 | */ |
| 576 | for (i=0; i < 10; i++) |
| 577 | magick[i]=(char) GetCharacter(jpeg_info); |
| 578 | magick[10]='\0'; |
cristy | 4d0ca34 | 2014-05-01 00:42:09 +0000 | [diff] [blame] | 579 | length-=10; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 580 | if (length <= 10) |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 581 | return(TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 582 | if (LocaleCompare(magick,"Photoshop ") != 0) |
| 583 | { |
| 584 | /* |
| 585 | Not a IPTC profile, return. |
| 586 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 587 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 588 | (void) GetCharacter(jpeg_info); |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 589 | return(TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 590 | } |
| 591 | /* |
| 592 | Remove the version number. |
| 593 | */ |
| 594 | for (i=0; i < 4; i++) |
| 595 | (void) GetCharacter(jpeg_info); |
cristy | 3589219 | 2014-05-26 12:04:36 +0000 | [diff] [blame] | 596 | if (length <= 11) |
cristy | 58a749e | 2014-05-25 17:36:53 +0000 | [diff] [blame] | 597 | return(TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 598 | length-=4; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 599 | error_manager=(ErrorManager *) jpeg_info->client_data; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 600 | exception=error_manager->exception; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 601 | image=error_manager->image; |
cristy | 8723e4b | 2011-09-01 13:11:19 +0000 | [diff] [blame] | 602 | profile=BlobToStringInfo((const void *) NULL,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 603 | if (profile == (StringInfo *) NULL) |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 604 | { |
| 605 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 606 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 607 | return(FALSE); |
| 608 | } |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 609 | error_manager->profile=profile; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 610 | p=GetStringInfoDatum(profile); |
cristy | 109e557 | 2010-09-16 18:38:17 +0000 | [diff] [blame] | 611 | for (i=0; i < (ssize_t) GetStringInfoLength(profile); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 612 | *p++=(unsigned char) GetCharacter(jpeg_info); |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 613 | error_manager->profile=NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 614 | iptc_profile=(StringInfo *) GetImageProfile(image,"8bim"); |
| 615 | if (iptc_profile != (StringInfo *) NULL) |
| 616 | { |
| 617 | ConcatenateStringInfo(iptc_profile,profile); |
| 618 | profile=DestroyStringInfo(profile); |
| 619 | } |
| 620 | else |
| 621 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 622 | status=SetImageProfile(image,"8bim",profile,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 623 | profile=DestroyStringInfo(profile); |
| 624 | if (status == MagickFalse) |
cristy | 9bb7c84 | 2014-06-17 23:42:24 +0000 | [diff] [blame] | 625 | { |
| 626 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 627 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 628 | return(FALSE); |
| 629 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 630 | } |
| 631 | if (image->debug != MagickFalse) |
| 632 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 633 | "Profile: iptc, %.20g bytes",(double) length); |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 634 | return(TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | static boolean ReadProfile(j_decompress_ptr jpeg_info) |
| 638 | { |
| 639 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 640 | name[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 641 | |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 642 | const StringInfo |
| 643 | *previous_profile; |
| 644 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 645 | ErrorManager |
| 646 | *error_manager; |
| 647 | |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 648 | ExceptionInfo |
| 649 | *exception; |
| 650 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 651 | Image |
| 652 | *image; |
| 653 | |
| 654 | int |
| 655 | marker; |
| 656 | |
| 657 | MagickBooleanType |
| 658 | status; |
| 659 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 660 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 661 | i; |
| 662 | |
| 663 | register unsigned char |
| 664 | *p; |
| 665 | |
| 666 | size_t |
| 667 | length; |
| 668 | |
| 669 | StringInfo |
| 670 | *profile; |
| 671 | |
| 672 | /* |
| 673 | Read generic profile. |
| 674 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 675 | length=(size_t) ((size_t) GetCharacter(jpeg_info) << 8); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 676 | length+=(size_t) GetCharacter(jpeg_info); |
| 677 | if (length <= 2) |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 678 | return(TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 679 | length-=2; |
| 680 | marker=jpeg_info->unread_marker-JPEG_APP0; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 681 | (void) FormatLocaleString(name,MagickPathExtent,"APP%d",marker); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 682 | error_manager=(ErrorManager *) jpeg_info->client_data; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 683 | exception=error_manager->exception; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 684 | image=error_manager->image; |
cristy | 8723e4b | 2011-09-01 13:11:19 +0000 | [diff] [blame] | 685 | profile=BlobToStringInfo((const void *) NULL,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 686 | if (profile == (StringInfo *) NULL) |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 687 | { |
| 688 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 689 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 690 | return(FALSE); |
| 691 | } |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 692 | error_manager->profile=profile; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 693 | p=GetStringInfoDatum(profile); |
cristy | 08f255a | 2011-08-17 01:23:06 +0000 | [diff] [blame] | 694 | for (i=0; i < (ssize_t) GetStringInfoLength(profile); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 695 | *p++=(unsigned char) GetCharacter(jpeg_info); |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 696 | error_manager->profile=NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 697 | if (marker == 1) |
| 698 | { |
| 699 | p=GetStringInfoDatum(profile); |
| 700 | if ((length > 4) && (LocaleNCompare((char *) p,"exif",4) == 0)) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 701 | (void) CopyMagickString(name,"exif",MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 702 | if ((length > 5) && (LocaleNCompare((char *) p,"http:",5) == 0)) |
| 703 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 704 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 705 | j; |
| 706 | |
| 707 | /* |
| 708 | Extract namespace from XMP profile. |
| 709 | */ |
| 710 | p=GetStringInfoDatum(profile); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 711 | for (j=0; j < (ssize_t) GetStringInfoLength(profile); j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 712 | { |
| 713 | if (*p == '\0') |
| 714 | break; |
| 715 | p++; |
| 716 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 717 | if (j < (ssize_t) GetStringInfoLength(profile)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 718 | (void) DestroyStringInfo(SplitStringInfo(profile,(size_t) (j+1))); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 719 | (void) CopyMagickString(name,"xmp",MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 720 | } |
| 721 | } |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 722 | previous_profile=GetImageProfile(image,name); |
| 723 | if (previous_profile != (const StringInfo *) NULL) |
cristy | 08f255a | 2011-08-17 01:23:06 +0000 | [diff] [blame] | 724 | { |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 725 | size_t |
cristy | 05c0c9a | 2011-09-05 23:16:13 +0000 | [diff] [blame] | 726 | length; |
| 727 | |
| 728 | length=GetStringInfoLength(profile); |
cristy | 08f255a | 2011-08-17 01:23:06 +0000 | [diff] [blame] | 729 | SetStringInfoLength(profile,GetStringInfoLength(profile)+ |
| 730 | GetStringInfoLength(previous_profile)); |
cristy | 83ab3d8 | 2011-09-06 12:05:51 +0000 | [diff] [blame] | 731 | (void) memmove(GetStringInfoDatum(profile)+ |
| 732 | GetStringInfoLength(previous_profile),GetStringInfoDatum(profile), |
| 733 | length); |
cristy | 08f255a | 2011-08-17 01:23:06 +0000 | [diff] [blame] | 734 | (void) memcpy(GetStringInfoDatum(profile), |
| 735 | GetStringInfoDatum(previous_profile), |
| 736 | GetStringInfoLength(previous_profile)); |
| 737 | } |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 738 | status=SetImageProfile(image,name,profile,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 739 | profile=DestroyStringInfo(profile); |
| 740 | if (status == MagickFalse) |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 741 | { |
| 742 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 743 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 744 | return(FALSE); |
| 745 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 746 | if (image->debug != MagickFalse) |
| 747 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 748 | "Profile: %s, %.20g bytes",name,(double) length); |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 749 | return(TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 750 | } |
| 751 | |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 752 | static void SkipInputData(j_decompress_ptr cinfo,long number_bytes) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 753 | { |
| 754 | SourceManager |
| 755 | *source; |
| 756 | |
| 757 | if (number_bytes <= 0) |
| 758 | return; |
| 759 | source=(SourceManager *) cinfo->src; |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 760 | while (number_bytes > (long) source->manager.bytes_in_buffer) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 761 | { |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 762 | number_bytes-=(long) source->manager.bytes_in_buffer; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 763 | (void) FillInputBuffer(cinfo); |
| 764 | } |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 765 | source->manager.next_input_byte+=number_bytes; |
| 766 | source->manager.bytes_in_buffer-=number_bytes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | static void TerminateSource(j_decompress_ptr cinfo) |
| 770 | { |
cristy | df0d90e | 2011-12-12 01:03:55 +0000 | [diff] [blame] | 771 | (void) cinfo; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | static void JPEGSourceManager(j_decompress_ptr cinfo,Image *image) |
| 775 | { |
| 776 | SourceManager |
| 777 | *source; |
| 778 | |
| 779 | cinfo->src=(struct jpeg_source_mgr *) (*cinfo->mem->alloc_small) |
| 780 | ((j_common_ptr) cinfo,JPOOL_IMAGE,sizeof(SourceManager)); |
| 781 | source=(SourceManager *) cinfo->src; |
| 782 | source->buffer=(JOCTET *) (*cinfo->mem->alloc_small) |
| 783 | ((j_common_ptr) cinfo,JPOOL_IMAGE,MaxBufferExtent*sizeof(JOCTET)); |
| 784 | source=(SourceManager *) cinfo->src; |
| 785 | source->manager.init_source=InitializeSource; |
| 786 | source->manager.fill_input_buffer=FillInputBuffer; |
| 787 | source->manager.skip_input_data=SkipInputData; |
| 788 | source->manager.resync_to_restart=jpeg_resync_to_restart; |
| 789 | source->manager.term_source=TerminateSource; |
| 790 | source->manager.bytes_in_buffer=0; |
| 791 | source->manager.next_input_byte=NULL; |
| 792 | source->image=image; |
| 793 | } |
| 794 | |
| 795 | static void JPEGSetImageQuality(struct jpeg_decompress_struct *jpeg_info, |
dirk | 881355a | 2013-08-19 19:56:50 +0000 | [diff] [blame] | 796 | Image *image, ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 797 | { |
| 798 | image->quality=UndefinedCompressionQuality; |
| 799 | #if defined(D_PROGRESSIVE_SUPPORTED) |
| 800 | if (image->compression == LosslessJPEGCompression) |
| 801 | { |
dirk | 29dd80e | 2013-10-31 23:11:11 +0000 | [diff] [blame] | 802 | image->quality=100; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 803 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 804 | "Quality: 100 (lossless)"); |
| 805 | } |
| 806 | else |
| 807 | #endif |
| 808 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 809 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 810 | j, |
| 811 | qvalue, |
| 812 | sum; |
| 813 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 814 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 815 | i; |
| 816 | |
| 817 | /* |
| 818 | Determine the JPEG compression quality from the quantization tables. |
| 819 | */ |
| 820 | sum=0; |
| 821 | for (i=0; i < NUM_QUANT_TBLS; i++) |
| 822 | { |
| 823 | if (jpeg_info->quant_tbl_ptrs[i] != NULL) |
| 824 | for (j=0; j < DCTSIZE2; j++) |
| 825 | sum+=jpeg_info->quant_tbl_ptrs[i]->quantval[j]; |
dirk | 881355a | 2013-08-19 19:56:50 +0000 | [diff] [blame] | 826 | } |
| 827 | if ((jpeg_info->quant_tbl_ptrs[0] != NULL) && |
| 828 | (jpeg_info->quant_tbl_ptrs[1] != NULL)) |
| 829 | { |
| 830 | ssize_t |
| 831 | hash[101] = |
| 832 | { |
| 833 | 1020, 1015, 932, 848, 780, 735, 702, 679, 660, 645, |
| 834 | 632, 623, 613, 607, 600, 594, 589, 585, 581, 571, |
| 835 | 555, 542, 529, 514, 494, 474, 457, 439, 424, 410, |
| 836 | 397, 386, 373, 364, 351, 341, 334, 324, 317, 309, |
| 837 | 299, 294, 287, 279, 274, 267, 262, 257, 251, 247, |
| 838 | 243, 237, 232, 227, 222, 217, 213, 207, 202, 198, |
| 839 | 192, 188, 183, 177, 173, 168, 163, 157, 153, 148, |
| 840 | 143, 139, 132, 128, 125, 119, 115, 108, 104, 99, |
| 841 | 94, 90, 84, 79, 74, 70, 64, 59, 55, 49, |
| 842 | 45, 40, 34, 30, 25, 20, 15, 11, 6, 4, |
| 843 | 0 |
| 844 | }, |
| 845 | sums[101] = |
| 846 | { |
| 847 | 32640, 32635, 32266, 31495, 30665, 29804, 29146, 28599, 28104, |
| 848 | 27670, 27225, 26725, 26210, 25716, 25240, 24789, 24373, 23946, |
| 849 | 23572, 22846, 21801, 20842, 19949, 19121, 18386, 17651, 16998, |
| 850 | 16349, 15800, 15247, 14783, 14321, 13859, 13535, 13081, 12702, |
| 851 | 12423, 12056, 11779, 11513, 11135, 10955, 10676, 10392, 10208, |
| 852 | 9928, 9747, 9564, 9369, 9193, 9017, 8822, 8639, 8458, |
| 853 | 8270, 8084, 7896, 7710, 7527, 7347, 7156, 6977, 6788, |
| 854 | 6607, 6422, 6236, 6054, 5867, 5684, 5495, 5305, 5128, |
| 855 | 4945, 4751, 4638, 4442, 4248, 4065, 3888, 3698, 3509, |
| 856 | 3326, 3139, 2957, 2775, 2586, 2405, 2216, 2037, 1846, |
| 857 | 1666, 1483, 1297, 1109, 927, 735, 554, 375, 201, |
| 858 | 128, 0 |
| 859 | }; |
| 860 | |
| 861 | qvalue=(ssize_t) (jpeg_info->quant_tbl_ptrs[0]->quantval[2]+ |
| 862 | jpeg_info->quant_tbl_ptrs[0]->quantval[53]+ |
| 863 | jpeg_info->quant_tbl_ptrs[1]->quantval[0]+ |
| 864 | jpeg_info->quant_tbl_ptrs[1]->quantval[DCTSIZE2-1]); |
| 865 | for (i=0; i < 100; i++) |
| 866 | { |
| 867 | if ((qvalue < hash[i]) && (sum < sums[i])) |
| 868 | continue; |
| 869 | if (((qvalue <= hash[i]) && (sum <= sums[i])) || (i >= 50)) |
dirk | 29dd80e | 2013-10-31 23:11:11 +0000 | [diff] [blame] | 870 | image->quality=(size_t) i+1; |
dirk | 881355a | 2013-08-19 19:56:50 +0000 | [diff] [blame] | 871 | if (image->debug != MagickFalse) |
| 872 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 873 | "Quality: %.20g (%s)",(double) i+1,(qvalue <= hash[i]) && |
| 874 | (sum <= sums[i]) ? "exact" : "approximate"); |
| 875 | break; |
| 876 | } |
| 877 | } |
| 878 | else |
| 879 | if (jpeg_info->quant_tbl_ptrs[0] != NULL) |
| 880 | { |
| 881 | ssize_t |
| 882 | hash[101] = |
| 883 | { |
| 884 | 510, 505, 422, 380, 355, 338, 326, 318, 311, 305, |
| 885 | 300, 297, 293, 291, 288, 286, 284, 283, 281, 280, |
| 886 | 279, 278, 277, 273, 262, 251, 243, 233, 225, 218, |
| 887 | 211, 205, 198, 193, 186, 181, 177, 172, 168, 164, |
| 888 | 158, 156, 152, 148, 145, 142, 139, 136, 133, 131, |
| 889 | 129, 126, 123, 120, 118, 115, 113, 110, 107, 105, |
| 890 | 102, 100, 97, 94, 92, 89, 87, 83, 81, 79, |
| 891 | 76, 74, 70, 68, 66, 63, 61, 57, 55, 52, |
| 892 | 50, 48, 44, 42, 39, 37, 34, 31, 29, 26, |
| 893 | 24, 21, 18, 16, 13, 11, 8, 6, 3, 2, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 894 | 0 |
dirk | 881355a | 2013-08-19 19:56:50 +0000 | [diff] [blame] | 895 | }, |
| 896 | sums[101] = |
| 897 | { |
| 898 | 16320, 16315, 15946, 15277, 14655, 14073, 13623, 13230, 12859, |
| 899 | 12560, 12240, 11861, 11456, 11081, 10714, 10360, 10027, 9679, |
| 900 | 9368, 9056, 8680, 8331, 7995, 7668, 7376, 7084, 6823, |
| 901 | 6562, 6345, 6125, 5939, 5756, 5571, 5421, 5240, 5086, |
| 902 | 4976, 4829, 4719, 4616, 4463, 4393, 4280, 4166, 4092, |
| 903 | 3980, 3909, 3835, 3755, 3688, 3621, 3541, 3467, 3396, |
| 904 | 3323, 3247, 3170, 3096, 3021, 2952, 2874, 2804, 2727, |
| 905 | 2657, 2583, 2509, 2437, 2362, 2290, 2211, 2136, 2068, |
| 906 | 1996, 1915, 1858, 1773, 1692, 1620, 1552, 1477, 1398, |
| 907 | 1326, 1251, 1179, 1109, 1031, 961, 884, 814, 736, |
| 908 | 667, 592, 518, 441, 369, 292, 221, 151, 86, |
| 909 | 64, 0 |
| 910 | }; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 911 | |
dirk | 881355a | 2013-08-19 19:56:50 +0000 | [diff] [blame] | 912 | qvalue=(ssize_t) (jpeg_info->quant_tbl_ptrs[0]->quantval[2]+ |
| 913 | jpeg_info->quant_tbl_ptrs[0]->quantval[53]); |
| 914 | for (i=0; i < 100; i++) |
| 915 | { |
| 916 | if ((qvalue < hash[i]) && (sum < sums[i])) |
| 917 | continue; |
| 918 | if (((qvalue <= hash[i]) && (sum <= sums[i])) || (i >= 50)) |
dirk | 29dd80e | 2013-10-31 23:11:11 +0000 | [diff] [blame] | 919 | image->quality=(size_t)i+1; |
dirk | 881355a | 2013-08-19 19:56:50 +0000 | [diff] [blame] | 920 | if (image->debug != MagickFalse) |
| 921 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 922 | "Quality: %.20g (%s)",(double) i+1,(qvalue <= hash[i]) && |
| 923 | (sum <= sums[i]) ? "exact" : "approximate"); |
| 924 | break; |
| 925 | } |
| 926 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 930 | static void JPEGSetImageSamplingFactor(struct jpeg_decompress_struct *jpeg_info, Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 931 | { |
| 932 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 933 | sampling_factor[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 934 | |
| 935 | switch (jpeg_info->out_color_space) |
| 936 | { |
| 937 | case JCS_CMYK: |
| 938 | { |
| 939 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Colorspace: CMYK"); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 940 | (void) FormatLocaleString(sampling_factor,MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 941 | "%dx%d,%dx%d,%dx%d,%dx%d",jpeg_info->comp_info[0].h_samp_factor, |
| 942 | jpeg_info->comp_info[0].v_samp_factor, |
| 943 | jpeg_info->comp_info[1].h_samp_factor, |
| 944 | jpeg_info->comp_info[1].v_samp_factor, |
| 945 | jpeg_info->comp_info[2].h_samp_factor, |
| 946 | jpeg_info->comp_info[2].v_samp_factor, |
| 947 | jpeg_info->comp_info[3].h_samp_factor, |
| 948 | jpeg_info->comp_info[3].v_samp_factor); |
cristy | 787d435 | 2010-03-06 13:55:58 +0000 | [diff] [blame] | 949 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 950 | } |
| 951 | case JCS_GRAYSCALE: |
| 952 | { |
| 953 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 954 | "Colorspace: GRAYSCALE"); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 955 | (void) FormatLocaleString(sampling_factor,MagickPathExtent,"%dx%d", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 956 | jpeg_info->comp_info[0].h_samp_factor, |
| 957 | jpeg_info->comp_info[0].v_samp_factor); |
| 958 | break; |
| 959 | } |
| 960 | case JCS_RGB: |
| 961 | { |
| 962 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Colorspace: RGB"); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 963 | (void) FormatLocaleString(sampling_factor,MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 964 | "%dx%d,%dx%d,%dx%d",jpeg_info->comp_info[0].h_samp_factor, |
| 965 | jpeg_info->comp_info[0].v_samp_factor, |
| 966 | jpeg_info->comp_info[1].h_samp_factor, |
| 967 | jpeg_info->comp_info[1].v_samp_factor, |
| 968 | jpeg_info->comp_info[2].h_samp_factor, |
| 969 | jpeg_info->comp_info[2].v_samp_factor); |
| 970 | break; |
| 971 | } |
| 972 | default: |
| 973 | { |
| 974 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Colorspace: %d", |
| 975 | jpeg_info->out_color_space); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 976 | (void) FormatLocaleString(sampling_factor,MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 977 | "%dx%d,%dx%d,%dx%d,%dx%d",jpeg_info->comp_info[0].h_samp_factor, |
| 978 | jpeg_info->comp_info[0].v_samp_factor, |
| 979 | jpeg_info->comp_info[1].h_samp_factor, |
| 980 | jpeg_info->comp_info[1].v_samp_factor, |
| 981 | jpeg_info->comp_info[2].h_samp_factor, |
| 982 | jpeg_info->comp_info[2].v_samp_factor, |
| 983 | jpeg_info->comp_info[3].h_samp_factor, |
| 984 | jpeg_info->comp_info[3].v_samp_factor); |
| 985 | break; |
| 986 | } |
| 987 | } |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 988 | (void) SetImageProperty(image,"jpeg:sampling-factor",sampling_factor, |
| 989 | exception); |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 990 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Sampling Factors: %s", |
| 991 | sampling_factor); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static Image *ReadJPEGImage(const ImageInfo *image_info, |
| 995 | ExceptionInfo *exception) |
| 996 | { |
| 997 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 998 | value[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 999 | |
cristy | caec74e | 2009-09-14 02:20:04 +0000 | [diff] [blame] | 1000 | const char |
cristy | 1115121 | 2009-09-14 13:10:15 +0000 | [diff] [blame] | 1001 | *option; |
cristy | caec74e | 2009-09-14 02:20:04 +0000 | [diff] [blame] | 1002 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1003 | ErrorManager |
| 1004 | error_manager; |
| 1005 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1006 | Image |
| 1007 | *image; |
| 1008 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1009 | JSAMPLE |
cristy | 75fc68f | 2012-10-08 16:26:00 +0000 | [diff] [blame] | 1010 | *volatile jpeg_pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1011 | |
| 1012 | JSAMPROW |
| 1013 | scanline[1]; |
| 1014 | |
| 1015 | MagickBooleanType |
| 1016 | debug, |
| 1017 | status; |
| 1018 | |
| 1019 | MagickSizeType |
| 1020 | number_pixels; |
| 1021 | |
cristy | 22646e2 | 2013-06-23 16:34:03 +0000 | [diff] [blame] | 1022 | MemoryInfo |
| 1023 | *memory_info; |
| 1024 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1025 | Quantum |
| 1026 | index; |
| 1027 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1028 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1029 | i; |
| 1030 | |
| 1031 | struct jpeg_decompress_struct |
| 1032 | jpeg_info; |
| 1033 | |
| 1034 | struct jpeg_error_mgr |
| 1035 | jpeg_error; |
| 1036 | |
| 1037 | register JSAMPLE |
| 1038 | *p; |
| 1039 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1040 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1041 | units; |
| 1042 | |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 1043 | ssize_t |
| 1044 | y; |
| 1045 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1046 | /* |
| 1047 | Open image file. |
| 1048 | */ |
| 1049 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame^] | 1050 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1051 | if (image_info->debug != MagickFalse) |
| 1052 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 1053 | image_info->filename); |
| 1054 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame^] | 1055 | assert(exception->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1056 | debug=IsEventLogging(); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1057 | (void) debug; |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 1058 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1059 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 1060 | if (status == MagickFalse) |
| 1061 | { |
| 1062 | image=DestroyImageList(image); |
| 1063 | return((Image *) NULL); |
| 1064 | } |
| 1065 | /* |
| 1066 | Initialize JPEG parameters. |
| 1067 | */ |
cristy | 9104497 | 2011-04-22 14:21:16 +0000 | [diff] [blame] | 1068 | (void) ResetMagickMemory(&error_manager,0,sizeof(error_manager)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1069 | (void) ResetMagickMemory(&jpeg_info,0,sizeof(jpeg_info)); |
| 1070 | (void) ResetMagickMemory(&jpeg_error,0,sizeof(jpeg_error)); |
| 1071 | jpeg_info.err=jpeg_std_error(&jpeg_error); |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 1072 | jpeg_info.err->emit_message=(void (*)(j_common_ptr,int)) JPEGWarningHandler; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1073 | jpeg_info.err->error_exit=(void (*)(j_common_ptr)) JPEGErrorHandler; |
cristy | 72f87f8 | 2013-06-23 16:43:05 +0000 | [diff] [blame] | 1074 | memory_info=(MemoryInfo *) NULL; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1075 | error_manager.exception=exception; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1076 | error_manager.image=image; |
| 1077 | if (setjmp(error_manager.error_recovery) != 0) |
| 1078 | { |
| 1079 | jpeg_destroy_decompress(&jpeg_info); |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 1080 | if (error_manager.profile != (StringInfo *) NULL) |
| 1081 | error_manager.profile=DestroyStringInfo(error_manager.profile); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1082 | (void) CloseBlob(image); |
| 1083 | number_pixels=(MagickSizeType) image->columns*image->rows; |
| 1084 | if (number_pixels != 0) |
| 1085 | return(GetFirstImageInList(image)); |
| 1086 | return(DestroyImage(image)); |
| 1087 | } |
| 1088 | jpeg_info.client_data=(void *) &error_manager; |
| 1089 | jpeg_create_decompress(&jpeg_info); |
| 1090 | JPEGSourceManager(&jpeg_info,image); |
| 1091 | jpeg_set_marker_processor(&jpeg_info,JPEG_COM,ReadComment); |
cristy | 00db021 | 2014-06-25 23:01:19 +0000 | [diff] [blame] | 1092 | option=GetImageOption(image_info,"profile:skip"); |
cristy | 53b6cc3 | 2014-06-25 13:13:33 +0000 | [diff] [blame] | 1093 | if (IsOptionMember("ICC",option) == MagickFalse) |
cristy | 9056b87 | 2014-06-24 17:30:15 +0000 | [diff] [blame] | 1094 | jpeg_set_marker_processor(&jpeg_info,ICC_MARKER,ReadICCProfile); |
cristy | 53b6cc3 | 2014-06-25 13:13:33 +0000 | [diff] [blame] | 1095 | if (IsOptionMember("IPTC",option) == MagickFalse) |
cristy | 9056b87 | 2014-06-24 17:30:15 +0000 | [diff] [blame] | 1096 | jpeg_set_marker_processor(&jpeg_info,IPTC_MARKER,ReadIPTCProfile); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1097 | for (i=1; i < 16; i++) |
| 1098 | if ((i != 2) && (i != 13) && (i != 14)) |
cristy | 53b6cc3 | 2014-06-25 13:13:33 +0000 | [diff] [blame] | 1099 | if (IsOptionMember("APP",option) == MagickFalse) |
cristy | 9056b87 | 2014-06-24 17:30:15 +0000 | [diff] [blame] | 1100 | jpeg_set_marker_processor(&jpeg_info,(int) (JPEG_APP0+i),ReadProfile); |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 1101 | i=(ssize_t) jpeg_read_header(&jpeg_info,TRUE); |
cristy | 53215c8 | 2009-09-19 16:32:36 +0000 | [diff] [blame] | 1102 | if ((image_info->colorspace == YCbCrColorspace) || |
| 1103 | (image_info->colorspace == Rec601YCbCrColorspace) || |
| 1104 | (image_info->colorspace == Rec709YCbCrColorspace)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1105 | jpeg_info.out_color_space=JCS_YCbCr; |
| 1106 | /* |
| 1107 | Set image resolution. |
| 1108 | */ |
| 1109 | units=0; |
| 1110 | if ((jpeg_info.saw_JFIF_marker != 0) && (jpeg_info.X_density != 1) && |
| 1111 | (jpeg_info.Y_density != 1)) |
| 1112 | { |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1113 | image->resolution.x=(double) jpeg_info.X_density; |
| 1114 | image->resolution.y=(double) jpeg_info.Y_density; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1115 | units=(size_t) jpeg_info.density_unit; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1116 | } |
| 1117 | if (units == 1) |
| 1118 | image->units=PixelsPerInchResolution; |
| 1119 | if (units == 2) |
| 1120 | image->units=PixelsPerCentimeterResolution; |
| 1121 | number_pixels=(MagickSizeType) image->columns*image->rows; |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1122 | option=GetImageOption(image_info,"jpeg:size"); |
cristy | 7e7d8e9 | 2014-10-25 22:24:51 +0000 | [diff] [blame] | 1123 | if ((option != (const char *) NULL) && |
| 1124 | (jpeg_info.out_color_space != JCS_YCbCr)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1125 | { |
| 1126 | double |
| 1127 | scale_factor; |
| 1128 | |
cristy | caec74e | 2009-09-14 02:20:04 +0000 | [diff] [blame] | 1129 | GeometryInfo |
| 1130 | geometry_info; |
| 1131 | |
cristy | 0adb4f9 | 2009-11-28 18:08:51 +0000 | [diff] [blame] | 1132 | MagickStatusType |
cristy | caec74e | 2009-09-14 02:20:04 +0000 | [diff] [blame] | 1133 | flags; |
| 1134 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1135 | /* |
cristy | caec74e | 2009-09-14 02:20:04 +0000 | [diff] [blame] | 1136 | Scale the image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1137 | */ |
cristy | 1115121 | 2009-09-14 13:10:15 +0000 | [diff] [blame] | 1138 | flags=ParseGeometry(option,&geometry_info); |
cristy | caec74e | 2009-09-14 02:20:04 +0000 | [diff] [blame] | 1139 | if ((flags & SigmaValue) == 0) |
| 1140 | geometry_info.sigma=geometry_info.rho; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1141 | jpeg_calc_output_dimensions(&jpeg_info); |
| 1142 | image->magick_columns=jpeg_info.output_width; |
| 1143 | image->magick_rows=jpeg_info.output_height; |
cristy | caec74e | 2009-09-14 02:20:04 +0000 | [diff] [blame] | 1144 | scale_factor=1.0; |
| 1145 | if (geometry_info.rho != 0.0) |
| 1146 | scale_factor=jpeg_info.output_width/geometry_info.rho; |
| 1147 | if ((geometry_info.sigma != 0.0) && |
| 1148 | (scale_factor > (jpeg_info.output_height/geometry_info.sigma))) |
| 1149 | scale_factor=jpeg_info.output_height/geometry_info.sigma; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1150 | jpeg_info.scale_num=1U; |
| 1151 | jpeg_info.scale_denom=(unsigned int) scale_factor; |
| 1152 | jpeg_calc_output_dimensions(&jpeg_info); |
| 1153 | if (image->debug != MagickFalse) |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1154 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1155 | "Scale factor: %.20g",(double) scale_factor); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1156 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1157 | #if (JPEG_LIB_VERSION >= 61) && defined(D_PROGRESSIVE_SUPPORTED) |
| 1158 | #if defined(D_LOSSLESS_SUPPORTED) |
| 1159 | image->interlace=jpeg_info.process == JPROC_PROGRESSIVE ? |
| 1160 | JPEGInterlace : NoInterlace; |
| 1161 | image->compression=jpeg_info.process == JPROC_LOSSLESS ? |
| 1162 | LosslessJPEGCompression : JPEGCompression; |
| 1163 | if (jpeg_info.data_precision > 8) |
| 1164 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1165 | "12-bit JPEG not supported. Reducing pixel data to 8 bits","`%s'", |
| 1166 | image->filename); |
| 1167 | if (jpeg_info.data_precision == 16) |
| 1168 | jpeg_info.data_precision=12; |
| 1169 | #else |
| 1170 | image->interlace=jpeg_info.progressive_mode != 0 ? JPEGInterlace : |
| 1171 | NoInterlace; |
| 1172 | image->compression=JPEGCompression; |
| 1173 | #endif |
| 1174 | #else |
| 1175 | image->compression=JPEGCompression; |
| 1176 | image->interlace=JPEGInterlace; |
| 1177 | #endif |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1178 | option=GetImageOption(image_info,"jpeg:colors"); |
anthony | 4190655 | 2011-10-07 12:15:27 +0000 | [diff] [blame] | 1179 | if (option != (const char *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1180 | { |
cristy | b4bb39c | 2012-02-21 18:45:54 +0000 | [diff] [blame] | 1181 | /* |
| 1182 | Let the JPEG library quantize the image. |
| 1183 | */ |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 1184 | jpeg_info.quantize_colors=TRUE; |
anthony | 4190655 | 2011-10-07 12:15:27 +0000 | [diff] [blame] | 1185 | jpeg_info.desired_number_of_colors=(int) StringToUnsignedLong(option); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1186 | } |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1187 | option=GetImageOption(image_info,"jpeg:block-smoothing"); |
cristy | 0d73718 | 2013-04-04 00:22:57 +0000 | [diff] [blame] | 1188 | if (option != (const char *) NULL) |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 1189 | jpeg_info.do_block_smoothing=IsStringTrue(option) != MagickFalse ? TRUE : |
| 1190 | FALSE; |
cristy | b4bb39c | 2012-02-21 18:45:54 +0000 | [diff] [blame] | 1191 | jpeg_info.dct_method=JDCT_FLOAT; |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1192 | option=GetImageOption(image_info,"jpeg:dct-method"); |
cristy | 787d435 | 2010-03-06 13:55:58 +0000 | [diff] [blame] | 1193 | if (option != (const char *) NULL) |
| 1194 | switch (*option) |
| 1195 | { |
| 1196 | case 'D': |
| 1197 | case 'd': |
| 1198 | { |
| 1199 | if (LocaleCompare(option,"default") == 0) |
| 1200 | jpeg_info.dct_method=JDCT_DEFAULT; |
| 1201 | break; |
| 1202 | } |
| 1203 | case 'F': |
| 1204 | case 'f': |
| 1205 | { |
| 1206 | if (LocaleCompare(option,"fastest") == 0) |
| 1207 | jpeg_info.dct_method=JDCT_FASTEST; |
| 1208 | if (LocaleCompare(option,"float") == 0) |
| 1209 | jpeg_info.dct_method=JDCT_FLOAT; |
| 1210 | break; |
| 1211 | } |
| 1212 | case 'I': |
| 1213 | case 'i': |
| 1214 | { |
| 1215 | if (LocaleCompare(option,"ifast") == 0) |
| 1216 | jpeg_info.dct_method=JDCT_IFAST; |
| 1217 | if (LocaleCompare(option,"islow") == 0) |
| 1218 | jpeg_info.dct_method=JDCT_ISLOW; |
| 1219 | break; |
| 1220 | } |
| 1221 | } |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1222 | option=GetImageOption(image_info,"jpeg:fancy-upsampling"); |
cristy | 0d73718 | 2013-04-04 00:22:57 +0000 | [diff] [blame] | 1223 | if (option != (const char *) NULL) |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 1224 | jpeg_info.do_fancy_upsampling=IsStringTrue(option) != MagickFalse ? TRUE : |
| 1225 | FALSE; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1226 | (void) jpeg_start_decompress(&jpeg_info); |
| 1227 | image->columns=jpeg_info.output_width; |
| 1228 | image->rows=jpeg_info.output_height; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1229 | image->depth=(size_t) jpeg_info.data_precision; |
cristy | 93ff2cc | 2012-05-13 21:03:53 +0000 | [diff] [blame] | 1230 | switch (jpeg_info.out_color_space) |
| 1231 | { |
| 1232 | case JCS_RGB: |
| 1233 | default: |
| 1234 | { |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 1235 | (void) SetImageColorspace(image,sRGBColorspace,exception); |
cristy | 93ff2cc | 2012-05-13 21:03:53 +0000 | [diff] [blame] | 1236 | break; |
| 1237 | } |
| 1238 | case JCS_GRAYSCALE: |
| 1239 | { |
cristy | 1a45be7 | 2013-04-07 00:16:24 +0000 | [diff] [blame] | 1240 | (void) SetImageColorspace(image,GRAYColorspace,exception); |
cristy | 93ff2cc | 2012-05-13 21:03:53 +0000 | [diff] [blame] | 1241 | break; |
| 1242 | } |
| 1243 | case JCS_YCbCr: |
| 1244 | { |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 1245 | (void) SetImageColorspace(image,YCbCrColorspace,exception); |
cristy | 93ff2cc | 2012-05-13 21:03:53 +0000 | [diff] [blame] | 1246 | break; |
| 1247 | } |
| 1248 | case JCS_CMYK: |
| 1249 | { |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 1250 | (void) SetImageColorspace(image,CMYKColorspace,exception); |
cristy | 93ff2cc | 2012-05-13 21:03:53 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | } |
| 1253 | } |
cristy | 36fc550 | 2012-05-22 11:32:23 +0000 | [diff] [blame] | 1254 | if (IsITUFaxImage(image) != MagickFalse) |
| 1255 | { |
cristy | f551aee | 2012-09-27 21:45:23 +0000 | [diff] [blame] | 1256 | (void) SetImageColorspace(image,LabColorspace,exception); |
cristy | 36fc550 | 2012-05-22 11:32:23 +0000 | [diff] [blame] | 1257 | jpeg_info.out_color_space=JCS_YCbCr; |
| 1258 | } |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1259 | option=GetImageOption(image_info,"jpeg:colors"); |
anthony | 4190655 | 2011-10-07 12:15:27 +0000 | [diff] [blame] | 1260 | if (option != (const char *) NULL) |
| 1261 | if (AcquireImageColormap(image,StringToUnsignedLong(option),exception) |
| 1262 | == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1263 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1264 | if ((jpeg_info.output_components == 1) && |
| 1265 | (jpeg_info.quantize_colors == MagickFalse)) |
| 1266 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1267 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1268 | colors; |
| 1269 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1270 | colors=(size_t) GetQuantumRange(image->depth)+1; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1271 | if (AcquireImageColormap(image,colors,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1272 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1273 | } |
| 1274 | if (image->debug != MagickFalse) |
| 1275 | { |
| 1276 | if (image->interlace != NoInterlace) |
| 1277 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1278 | "Interlace: progressive"); |
| 1279 | else |
| 1280 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1281 | "Interlace: nonprogressive"); |
| 1282 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Data precision: %d", |
| 1283 | (int) jpeg_info.data_precision); |
| 1284 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %dx%d", |
| 1285 | (int) jpeg_info.output_width,(int) jpeg_info.output_height); |
| 1286 | } |
dirk | 881355a | 2013-08-19 19:56:50 +0000 | [diff] [blame] | 1287 | JPEGSetImageQuality(&jpeg_info,image,exception); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1288 | JPEGSetImageSamplingFactor(&jpeg_info,image,exception); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1289 | (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1290 | jpeg_info.out_color_space); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1291 | (void) SetImageProperty(image,"jpeg:colorspace",value,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1292 | if (image_info->ping != MagickFalse) |
| 1293 | { |
| 1294 | jpeg_destroy_decompress(&jpeg_info); |
| 1295 | (void) CloseBlob(image); |
| 1296 | return(GetFirstImageInList(image)); |
| 1297 | } |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 1298 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 1299 | if (status == MagickFalse) |
cristy | 457f81d | 2015-02-01 18:13:23 +0000 | [diff] [blame] | 1300 | { |
| 1301 | jpeg_destroy_decompress(&jpeg_info); |
| 1302 | return(DestroyImageList(image)); |
| 1303 | } |
| 1304 | if ((jpeg_info.output_components != 1) && |
| 1305 | (jpeg_info.output_components != 3) && (jpeg_info.output_components != 4)) |
| 1306 | { |
| 1307 | jpeg_destroy_decompress(&jpeg_info); |
| 1308 | ThrowReaderException(CorruptImageError,"ImageTypeNotSupported"); |
| 1309 | } |
cristy | 22646e2 | 2013-06-23 16:34:03 +0000 | [diff] [blame] | 1310 | memory_info=AcquireVirtualMemory((size_t) image->columns, |
| 1311 | jpeg_info.output_components*sizeof(*jpeg_pixels)); |
| 1312 | if (memory_info == (MemoryInfo *) NULL) |
cristy | ee1bdaa | 2013-07-16 16:45:03 +0000 | [diff] [blame] | 1313 | { |
| 1314 | jpeg_destroy_decompress(&jpeg_info); |
| 1315 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1316 | } |
cristy | 22646e2 | 2013-06-23 16:34:03 +0000 | [diff] [blame] | 1317 | jpeg_pixels=(JSAMPLE *) GetVirtualMemoryBlob(memory_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1318 | /* |
| 1319 | Convert JPEG pixels to pixel packets. |
| 1320 | */ |
| 1321 | if (setjmp(error_manager.error_recovery) != 0) |
| 1322 | { |
cristy | 22646e2 | 2013-06-23 16:34:03 +0000 | [diff] [blame] | 1323 | if (memory_info != (MemoryInfo *) NULL) |
| 1324 | memory_info=RelinquishVirtualMemory(memory_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1325 | jpeg_destroy_decompress(&jpeg_info); |
| 1326 | (void) CloseBlob(image); |
| 1327 | number_pixels=(MagickSizeType) image->columns*image->rows; |
| 1328 | if (number_pixels != 0) |
| 1329 | return(GetFirstImageInList(image)); |
| 1330 | return(DestroyImage(image)); |
| 1331 | } |
| 1332 | if (jpeg_info.quantize_colors != MagickFalse) |
| 1333 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1334 | image->colors=(size_t) jpeg_info.actual_number_of_colors; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1335 | if (jpeg_info.out_color_space == JCS_GRAYSCALE) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1336 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1337 | { |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 1338 | image->colormap[i].red=(double) ScaleCharToQuantum( |
| 1339 | jpeg_info.colormap[0][i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1340 | image->colormap[i].green=image->colormap[i].red; |
| 1341 | image->colormap[i].blue=image->colormap[i].red; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1342 | image->colormap[i].alpha=OpaqueAlpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1343 | } |
| 1344 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1345 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1346 | { |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 1347 | image->colormap[i].red=(double) ScaleCharToQuantum( |
| 1348 | jpeg_info.colormap[0][i]); |
| 1349 | image->colormap[i].green=(double) ScaleCharToQuantum( |
| 1350 | jpeg_info.colormap[1][i]); |
| 1351 | image->colormap[i].blue=(double) ScaleCharToQuantum( |
| 1352 | jpeg_info.colormap[2][i]); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1353 | image->colormap[i].alpha=OpaqueAlpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1354 | } |
| 1355 | } |
| 1356 | scanline[0]=(JSAMPROW) jpeg_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1357 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1358 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1359 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1360 | x; |
| 1361 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1362 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1363 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1364 | |
| 1365 | if (jpeg_read_scanlines(&jpeg_info,scanline,1) != 1) |
| 1366 | { |
| 1367 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 1368 | CorruptImageWarning,"SkipToSyncByte","`%s'",image->filename); |
| 1369 | continue; |
| 1370 | } |
| 1371 | p=jpeg_pixels; |
| 1372 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1373 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1374 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1375 | if (jpeg_info.data_precision > 8) |
| 1376 | { |
cristy | 5328a4c | 2013-12-03 11:32:13 +0000 | [diff] [blame] | 1377 | unsigned short |
| 1378 | scale; |
| 1379 | |
cristy | 477b9fd | 2013-12-03 16:52:31 +0000 | [diff] [blame] | 1380 | scale=65535U/GetQuantumRange(jpeg_info.data_precision); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1381 | if (jpeg_info.output_components == 1) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1382 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1383 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1384 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1385 | pixel; |
| 1386 | |
cristy | 5328a4c | 2013-12-03 11:32:13 +0000 | [diff] [blame] | 1387 | pixel=(size_t) (scale*GETJSAMPLE(*p)); |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1388 | index=ConstrainColormapIndex(image,pixel,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1389 | SetPixelIndex(image,index,q); |
cristy | 11a06d3 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1390 | SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1391 | p++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1392 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1393 | } |
| 1394 | else |
| 1395 | if (image->colorspace != CMYKColorspace) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1396 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1397 | { |
cristy | 217ddd6 | 2013-12-06 23:27:09 +0000 | [diff] [blame] | 1398 | SetPixelRed(image,ScaleShortToQuantum(scale*GETJSAMPLE(*p++)), |
| 1399 | q); |
| 1400 | SetPixelGreen(image,ScaleShortToQuantum(scale*GETJSAMPLE(*p++)), |
| 1401 | q); |
| 1402 | SetPixelBlue(image,ScaleShortToQuantum(scale*GETJSAMPLE(*p++)), |
| 1403 | q); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1404 | SetPixelAlpha(image,OpaqueAlpha,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1405 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1406 | } |
| 1407 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1408 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1409 | { |
cristy | 217ddd6 | 2013-12-06 23:27:09 +0000 | [diff] [blame] | 1410 | SetPixelCyan(image,QuantumRange-ScaleShortToQuantum(scale* |
| 1411 | GETJSAMPLE(*p++)),q); |
| 1412 | SetPixelMagenta(image,QuantumRange-ScaleShortToQuantum(scale* |
| 1413 | GETJSAMPLE(*p++)),q); |
| 1414 | SetPixelYellow(image,QuantumRange-ScaleShortToQuantum(scale* |
| 1415 | GETJSAMPLE(*p++)),q); |
| 1416 | SetPixelBlack(image,QuantumRange-ScaleShortToQuantum(scale* |
| 1417 | GETJSAMPLE(*p++)),q); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1418 | SetPixelAlpha(image,OpaqueAlpha,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1419 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | else |
| 1423 | if (jpeg_info.output_components == 1) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1424 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1425 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1426 | index=ConstrainColormapIndex(image,(size_t) GETJSAMPLE(*p),exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1427 | SetPixelIndex(image,index,q); |
cristy | 11a06d3 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1428 | SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1429 | p++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1430 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1431 | } |
| 1432 | else |
| 1433 | if (image->colorspace != CMYKColorspace) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1434 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1435 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1436 | SetPixelRed(image,ScaleCharToQuantum((unsigned char) |
| 1437 | GETJSAMPLE(*p++)),q); |
| 1438 | SetPixelGreen(image,ScaleCharToQuantum((unsigned char) |
| 1439 | GETJSAMPLE(*p++)),q); |
| 1440 | SetPixelBlue(image,ScaleCharToQuantum((unsigned char) |
| 1441 | GETJSAMPLE(*p++)),q); |
| 1442 | SetPixelAlpha(image,OpaqueAlpha,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1443 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1444 | } |
| 1445 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1446 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1447 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1448 | SetPixelCyan(image,QuantumRange-ScaleCharToQuantum( |
| 1449 | (unsigned char) GETJSAMPLE(*p++)),q); |
| 1450 | SetPixelMagenta(image,QuantumRange-ScaleCharToQuantum( |
| 1451 | (unsigned char) GETJSAMPLE(*p++)),q); |
| 1452 | SetPixelYellow(image,QuantumRange-ScaleCharToQuantum( |
| 1453 | (unsigned char) GETJSAMPLE(*p++)),q); |
| 1454 | SetPixelBlack(image,QuantumRange-ScaleCharToQuantum( |
| 1455 | (unsigned char) GETJSAMPLE(*p++)),q); |
| 1456 | SetPixelAlpha(image,OpaqueAlpha,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1457 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1458 | } |
| 1459 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 1460 | break; |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 1461 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 1462 | image->rows); |
| 1463 | if (status == MagickFalse) |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 1464 | { |
| 1465 | jpeg_abort_decompress(&jpeg_info); |
| 1466 | break; |
| 1467 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1468 | } |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 1469 | if (status != MagickFalse) |
| 1470 | { |
| 1471 | error_manager.finished=MagickTrue; |
| 1472 | if (setjmp(error_manager.error_recovery) == 0) |
| 1473 | (void) jpeg_finish_decompress(&jpeg_info); |
| 1474 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1475 | /* |
| 1476 | Free jpeg resources. |
| 1477 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1478 | jpeg_destroy_decompress(&jpeg_info); |
cristy | 22646e2 | 2013-06-23 16:34:03 +0000 | [diff] [blame] | 1479 | memory_info=RelinquishVirtualMemory(memory_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1480 | (void) CloseBlob(image); |
| 1481 | return(GetFirstImageInList(image)); |
| 1482 | } |
| 1483 | #endif |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 1484 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1485 | /* |
| 1486 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1487 | % % |
| 1488 | % % |
| 1489 | % % |
| 1490 | % R e g i s t e r J P E G I m a g e % |
| 1491 | % % |
| 1492 | % % |
| 1493 | % % |
| 1494 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1495 | % |
| 1496 | % RegisterJPEGImage() adds properties for the JPEG image format to |
| 1497 | % the list of supported formats. The properties include the image format |
| 1498 | % tag, a method to read and/or write the format, whether the format |
| 1499 | % supports the saving of more than one frame to the same file or blob, |
| 1500 | % whether the format supports native in-memory I/O, and a brief |
| 1501 | % description of the format. |
| 1502 | % |
| 1503 | % The format of the RegisterJPEGImage method is: |
| 1504 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1505 | % size_t RegisterJPEGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1506 | % |
| 1507 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1508 | ModuleExport size_t RegisterJPEGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1509 | { |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 1510 | #define JPEGDescription "Joint Photographic Experts Group JFIF format" |
| 1511 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1512 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1513 | version[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1514 | |
| 1515 | MagickInfo |
| 1516 | *entry; |
| 1517 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1518 | *version='\0'; |
| 1519 | #if defined(JPEG_LIB_VERSION) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1520 | (void) FormatLocaleString(version,MagickPathExtent,"%d",JPEG_LIB_VERSION); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1521 | #endif |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 1522 | entry=AcquireMagickInfo("JPEG","JPE",JPEGDescription); |
cristy | 8e62a80 | 2014-12-27 22:43:35 +0000 | [diff] [blame] | 1523 | #if (JPEG_LIB_VERSION < 80) && !defined(LIBJPEG_TURBO_VERSION) |
cristy | b338b71 | 2014-11-27 14:06:34 +0000 | [diff] [blame] | 1524 | entry->thread_support=NoThreadSupport; |
| 1525 | #endif |
| 1526 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 1527 | entry->decoder=(DecodeImageHandler *) ReadJPEGImage; |
| 1528 | entry->encoder=(EncodeImageHandler *) WriteJPEGImage; |
| 1529 | #endif |
| 1530 | entry->magick=(IsImageFormatHandler *) IsJPEG; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 1531 | entry->flags^=CoderAdjoinFlag; |
| 1532 | entry->flags^=CoderUseExtensionFlag; |
cristy | b338b71 | 2014-11-27 14:06:34 +0000 | [diff] [blame] | 1533 | if (*version != '\0') |
| 1534 | entry->version=ConstantString(version); |
| 1535 | entry->mime_type=ConstantString("image/jpeg"); |
cristy | b338b71 | 2014-11-27 14:06:34 +0000 | [diff] [blame] | 1536 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 1537 | entry=AcquireMagickInfo("JPEG","JPEG",JPEGDescription); |
cristy | 8e62a80 | 2014-12-27 22:43:35 +0000 | [diff] [blame] | 1538 | #if (JPEG_LIB_VERSION < 80) && !defined(LIBJPEG_TURBO_VERSION) |
cristy | 61c382d | 2014-09-17 11:18:11 +0000 | [diff] [blame] | 1539 | entry->thread_support=NoThreadSupport; |
| 1540 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1541 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 1542 | entry->decoder=(DecodeImageHandler *) ReadJPEGImage; |
| 1543 | entry->encoder=(EncodeImageHandler *) WriteJPEGImage; |
| 1544 | #endif |
| 1545 | entry->magick=(IsImageFormatHandler *) IsJPEG; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 1546 | entry->flags^=CoderAdjoinFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1547 | if (*version != '\0') |
| 1548 | entry->version=ConstantString(version); |
cristy | 4a6ab48 | 2013-08-09 01:29:35 +0000 | [diff] [blame] | 1549 | entry->mime_type=ConstantString("image/jpeg"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1550 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 1551 | entry=AcquireMagickInfo("JPEG","JPG",JPEGDescription); |
cristy | 8e62a80 | 2014-12-27 22:43:35 +0000 | [diff] [blame] | 1552 | #if (JPEG_LIB_VERSION < 80) && !defined(LIBJPEG_TURBO_VERSION) |
cristy | 61c382d | 2014-09-17 11:18:11 +0000 | [diff] [blame] | 1553 | entry->thread_support=NoThreadSupport; |
| 1554 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1555 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 1556 | entry->decoder=(DecodeImageHandler *) ReadJPEGImage; |
| 1557 | entry->encoder=(EncodeImageHandler *) WriteJPEGImage; |
| 1558 | #endif |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 1559 | entry->flags^=CoderAdjoinFlag; |
| 1560 | entry->flags^=CoderUseExtensionFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1561 | if (*version != '\0') |
| 1562 | entry->version=ConstantString(version); |
cristy | 4a6ab48 | 2013-08-09 01:29:35 +0000 | [diff] [blame] | 1563 | entry->mime_type=ConstantString("image/jpeg"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1564 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 1565 | entry=AcquireMagickInfo("JPEG","JPS",JPEGDescription); |
cristy | a663a4f | 2015-01-23 12:11:57 +0000 | [diff] [blame] | 1566 | #if (JPEG_LIB_VERSION < 80) && !defined(LIBJPEG_TURBO_VERSION) |
| 1567 | entry->thread_support=NoThreadSupport; |
| 1568 | #endif |
| 1569 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 1570 | entry->decoder=(DecodeImageHandler *) ReadJPEGImage; |
| 1571 | entry->encoder=(EncodeImageHandler *) WriteJPEGImage; |
| 1572 | #endif |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 1573 | entry->flags^=CoderAdjoinFlag; |
| 1574 | entry->flags^=CoderUseExtensionFlag; |
cristy | a663a4f | 2015-01-23 12:11:57 +0000 | [diff] [blame] | 1575 | if (*version != '\0') |
| 1576 | entry->version=ConstantString(version); |
| 1577 | entry->mime_type=ConstantString("image/jpeg"); |
cristy | a663a4f | 2015-01-23 12:11:57 +0000 | [diff] [blame] | 1578 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 1579 | entry=AcquireMagickInfo("JPEG","PJPEG",JPEGDescription); |
cristy | 8e62a80 | 2014-12-27 22:43:35 +0000 | [diff] [blame] | 1580 | #if (JPEG_LIB_VERSION < 80) && !defined(LIBJPEG_TURBO_VERSION) |
cristy | 61c382d | 2014-09-17 11:18:11 +0000 | [diff] [blame] | 1581 | entry->thread_support=NoThreadSupport; |
| 1582 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1583 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 1584 | entry->decoder=(DecodeImageHandler *) ReadJPEGImage; |
| 1585 | entry->encoder=(EncodeImageHandler *) WriteJPEGImage; |
| 1586 | #endif |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 1587 | entry->flags^=CoderAdjoinFlag; |
| 1588 | entry->flags^=CoderUseExtensionFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1589 | if (*version != '\0') |
| 1590 | entry->version=ConstantString(version); |
cristy | 4a6ab48 | 2013-08-09 01:29:35 +0000 | [diff] [blame] | 1591 | entry->mime_type=ConstantString("image/jpeg"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1592 | (void) RegisterMagickInfo(entry); |
| 1593 | return(MagickImageCoderSignature); |
| 1594 | } |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 1595 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1596 | /* |
| 1597 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1598 | % % |
| 1599 | % % |
| 1600 | % % |
| 1601 | % U n r e g i s t e r J P E G I m a g e % |
| 1602 | % % |
| 1603 | % % |
| 1604 | % % |
| 1605 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1606 | % |
| 1607 | % UnregisterJPEGImage() removes format registrations made by the |
| 1608 | % JPEG module from the list of supported formats. |
| 1609 | % |
| 1610 | % The format of the UnregisterJPEGImage method is: |
| 1611 | % |
| 1612 | % UnregisterJPEGImage(void) |
| 1613 | % |
| 1614 | */ |
| 1615 | ModuleExport void UnregisterJPEGImage(void) |
| 1616 | { |
| 1617 | (void) UnregisterMagickInfo("PJPG"); |
cristy | a663a4f | 2015-01-23 12:11:57 +0000 | [diff] [blame] | 1618 | (void) UnregisterMagickInfo("JPS"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1619 | (void) UnregisterMagickInfo("JPG"); |
cristy | a663a4f | 2015-01-23 12:11:57 +0000 | [diff] [blame] | 1620 | (void) UnregisterMagickInfo("JPEG"); |
cristy | b338b71 | 2014-11-27 14:06:34 +0000 | [diff] [blame] | 1621 | (void) UnregisterMagickInfo("JPE"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1622 | } |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 1623 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1624 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 1625 | /* |
| 1626 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1627 | % % |
| 1628 | % % |
| 1629 | % % |
| 1630 | % W r i t e J P E G I m a g e % |
| 1631 | % % |
| 1632 | % % |
| 1633 | % % |
| 1634 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1635 | % |
| 1636 | % WriteJPEGImage() writes a JPEG image file and returns it. It |
| 1637 | % allocates the memory necessary for the new Image structure and returns a |
| 1638 | % pointer to the new image. |
| 1639 | % |
| 1640 | % The format of the WriteJPEGImage method is: |
| 1641 | % |
cristy | 9104497 | 2011-04-22 14:21:16 +0000 | [diff] [blame] | 1642 | % MagickBooleanType WriteJPEGImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1643 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1644 | % |
| 1645 | % A description of each parameter follows: |
| 1646 | % |
| 1647 | % o image_info: the image info. |
| 1648 | % |
| 1649 | % o jpeg_image: The image. |
| 1650 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1651 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1652 | % |
| 1653 | */ |
| 1654 | |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 1655 | static QuantizationTable *DestroyQuantizationTable(QuantizationTable *table) |
| 1656 | { |
| 1657 | assert(table != (QuantizationTable *) NULL); |
| 1658 | if (table->slot != (char *) NULL) |
| 1659 | table->slot=DestroyString(table->slot); |
| 1660 | if (table->description != (char *) NULL) |
| 1661 | table->description=DestroyString(table->description); |
| 1662 | if (table->levels != (unsigned int *) NULL) |
| 1663 | table->levels=(unsigned int *) RelinquishMagickMemory(table->levels); |
| 1664 | table=(QuantizationTable *) RelinquishMagickMemory(table); |
| 1665 | return(table); |
| 1666 | } |
| 1667 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1668 | static boolean EmptyOutputBuffer(j_compress_ptr cinfo) |
| 1669 | { |
| 1670 | DestinationManager |
| 1671 | *destination; |
| 1672 | |
| 1673 | destination=(DestinationManager *) cinfo->dest; |
| 1674 | destination->manager.free_in_buffer=(size_t) WriteBlob(destination->image, |
| 1675 | MaxBufferExtent,destination->buffer); |
| 1676 | if (destination->manager.free_in_buffer != MaxBufferExtent) |
| 1677 | ERREXIT(cinfo,JERR_FILE_WRITE); |
| 1678 | destination->manager.next_output_byte=destination->buffer; |
| 1679 | return(TRUE); |
| 1680 | } |
| 1681 | |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 1682 | static QuantizationTable *GetQuantizationTable(const char *filename, |
| 1683 | const char *slot,ExceptionInfo *exception) |
| 1684 | { |
| 1685 | char |
| 1686 | *p, |
| 1687 | *xml; |
| 1688 | |
| 1689 | const char |
| 1690 | *attribute, |
| 1691 | *content; |
| 1692 | |
| 1693 | double |
| 1694 | value; |
| 1695 | |
| 1696 | register ssize_t |
| 1697 | i; |
| 1698 | |
| 1699 | ssize_t |
| 1700 | j; |
| 1701 | |
| 1702 | QuantizationTable |
| 1703 | *table; |
| 1704 | |
cristy | 1fcc8b6 | 2012-03-02 17:25:55 +0000 | [diff] [blame] | 1705 | size_t |
| 1706 | length; |
| 1707 | |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 1708 | XMLTreeInfo |
| 1709 | *description, |
| 1710 | *levels, |
| 1711 | *quantization_tables, |
| 1712 | *table_iterator; |
| 1713 | |
| 1714 | (void) LogMagickEvent(ConfigureEvent,GetMagickModule(), |
| 1715 | "Loading quantization tables \"%s\" ...",filename); |
| 1716 | table=(QuantizationTable *) NULL; |
cristy | 3a5987c | 2013-11-07 14:18:46 +0000 | [diff] [blame] | 1717 | xml=FileToString(filename,~0UL,exception); |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 1718 | if (xml == (char *) NULL) |
| 1719 | return(table); |
| 1720 | quantization_tables=NewXMLTree(xml,exception); |
| 1721 | if (quantization_tables == (XMLTreeInfo *) NULL) |
| 1722 | { |
| 1723 | xml=DestroyString(xml); |
| 1724 | return(table); |
| 1725 | } |
| 1726 | for (table_iterator=GetXMLTreeChild(quantization_tables,"table"); |
| 1727 | table_iterator != (XMLTreeInfo *) NULL; |
| 1728 | table_iterator=GetNextXMLTreeTag(table_iterator)) |
| 1729 | { |
| 1730 | attribute=GetXMLTreeAttribute(table_iterator,"slot"); |
| 1731 | if ((attribute != (char *) NULL) && (LocaleCompare(slot,attribute) == 0)) |
| 1732 | break; |
| 1733 | attribute=GetXMLTreeAttribute(table_iterator,"alias"); |
| 1734 | if ((attribute != (char *) NULL) && (LocaleCompare(slot,attribute) == 0)) |
| 1735 | break; |
| 1736 | } |
| 1737 | if (table_iterator == (XMLTreeInfo *) NULL) |
| 1738 | { |
| 1739 | xml=DestroyString(xml); |
| 1740 | return(table); |
| 1741 | } |
| 1742 | description=GetXMLTreeChild(table_iterator,"description"); |
| 1743 | if (description == (XMLTreeInfo *) NULL) |
| 1744 | { |
| 1745 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1746 | "XmlMissingElement", "<description>, slot \"%s\"",slot); |
| 1747 | quantization_tables=DestroyXMLTree(quantization_tables); |
| 1748 | xml=DestroyString(xml); |
| 1749 | return(table); |
| 1750 | } |
| 1751 | levels=GetXMLTreeChild(table_iterator,"levels"); |
| 1752 | if (levels == (XMLTreeInfo *) NULL) |
| 1753 | { |
| 1754 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1755 | "XmlMissingElement", "<levels>, slot \"%s\"", slot); |
| 1756 | quantization_tables=DestroyXMLTree(quantization_tables); |
| 1757 | xml=DestroyString(xml); |
| 1758 | return(table); |
| 1759 | } |
| 1760 | table=(QuantizationTable *) AcquireMagickMemory(sizeof(*table)); |
| 1761 | if (table == (QuantizationTable *) NULL) |
| 1762 | ThrowFatalException(ResourceLimitFatalError, |
| 1763 | "UnableToAcquireQuantizationTable"); |
| 1764 | table->slot=(char *) NULL; |
| 1765 | table->description=(char *) NULL; |
| 1766 | table->levels=(unsigned int *) NULL; |
| 1767 | attribute=GetXMLTreeAttribute(table_iterator,"slot"); |
| 1768 | if (attribute != (char *) NULL) |
| 1769 | table->slot=ConstantString(attribute); |
| 1770 | content=GetXMLTreeContent(description); |
| 1771 | if (content != (char *) NULL) |
| 1772 | table->description=ConstantString(content); |
| 1773 | attribute=GetXMLTreeAttribute(levels,"width"); |
| 1774 | if (attribute == (char *) NULL) |
| 1775 | { |
| 1776 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1777 | "XmlMissingAttribute", "<levels width>, slot \"%s\"",slot); |
| 1778 | quantization_tables=DestroyXMLTree(quantization_tables); |
| 1779 | table=DestroyQuantizationTable(table); |
| 1780 | xml=DestroyString(xml); |
| 1781 | return(table); |
| 1782 | } |
| 1783 | table->width=StringToUnsignedLong(attribute); |
| 1784 | if (table->width == 0) |
| 1785 | { |
| 1786 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1787 | "XmlInvalidAttribute", "<levels width>, table \"%s\"",slot); |
| 1788 | quantization_tables=DestroyXMLTree(quantization_tables); |
| 1789 | table=DestroyQuantizationTable(table); |
| 1790 | xml=DestroyString(xml); |
| 1791 | return(table); |
| 1792 | } |
| 1793 | attribute=GetXMLTreeAttribute(levels,"height"); |
| 1794 | if (attribute == (char *) NULL) |
| 1795 | { |
| 1796 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1797 | "XmlMissingAttribute", "<levels height>, table \"%s\"",slot); |
| 1798 | quantization_tables=DestroyXMLTree(quantization_tables); |
| 1799 | table=DestroyQuantizationTable(table); |
| 1800 | xml=DestroyString(xml); |
| 1801 | return(table); |
| 1802 | } |
| 1803 | table->height=StringToUnsignedLong(attribute); |
| 1804 | if (table->height == 0) |
| 1805 | { |
| 1806 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1807 | "XmlInvalidAttribute", "<levels height>, table \"%s\"",slot); |
| 1808 | quantization_tables=DestroyXMLTree(quantization_tables); |
| 1809 | table=DestroyQuantizationTable(table); |
| 1810 | xml=DestroyString(xml); |
| 1811 | return(table); |
| 1812 | } |
| 1813 | attribute=GetXMLTreeAttribute(levels,"divisor"); |
| 1814 | if (attribute == (char *) NULL) |
| 1815 | { |
| 1816 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1817 | "XmlMissingAttribute", "<levels divisor>, table \"%s\"",slot); |
| 1818 | quantization_tables=DestroyXMLTree(quantization_tables); |
| 1819 | table=DestroyQuantizationTable(table); |
| 1820 | xml=DestroyString(xml); |
| 1821 | return(table); |
| 1822 | } |
cristy | 043f3f3 | 2012-03-02 17:37:28 +0000 | [diff] [blame] | 1823 | table->divisor=InterpretLocaleValue(attribute,(char **) NULL); |
| 1824 | if (table->divisor == 0.0) |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 1825 | { |
| 1826 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1827 | "XmlInvalidAttribute", "<levels divisor>, table \"%s\"",slot); |
| 1828 | quantization_tables=DestroyXMLTree(quantization_tables); |
| 1829 | table=DestroyQuantizationTable(table); |
| 1830 | xml=DestroyString(xml); |
| 1831 | return(table); |
| 1832 | } |
| 1833 | content=GetXMLTreeContent(levels); |
| 1834 | if (content == (char *) NULL) |
| 1835 | { |
| 1836 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1837 | "XmlMissingContent", "<levels>, table \"%s\"",slot); |
| 1838 | quantization_tables=DestroyXMLTree(quantization_tables); |
| 1839 | table=DestroyQuantizationTable(table); |
| 1840 | xml=DestroyString(xml); |
| 1841 | return(table); |
| 1842 | } |
cristy | 1fcc8b6 | 2012-03-02 17:25:55 +0000 | [diff] [blame] | 1843 | length=(size_t) table->width*table->height; |
| 1844 | if (length < 64) |
| 1845 | length=64; |
cristy | 092006b | 2012-03-02 17:26:02 +0000 | [diff] [blame] | 1846 | table->levels=(unsigned int *) AcquireQuantumMemory(length, |
cristy | 1fcc8b6 | 2012-03-02 17:25:55 +0000 | [diff] [blame] | 1847 | sizeof(*table->levels)); |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 1848 | if (table->levels == (unsigned int *) NULL) |
| 1849 | ThrowFatalException(ResourceLimitFatalError, |
| 1850 | "UnableToAcquireQuantizationTable"); |
| 1851 | for (i=0; i < (ssize_t) (table->width*table->height); i++) |
| 1852 | { |
cristy | 043f3f3 | 2012-03-02 17:37:28 +0000 | [diff] [blame] | 1853 | table->levels[i]=(unsigned int) (InterpretLocaleValue(content,&p)/ |
| 1854 | table->divisor+0.5); |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 1855 | while (isspace((int) ((unsigned char) *p)) != 0) |
| 1856 | p++; |
| 1857 | if (*p == ',') |
| 1858 | p++; |
| 1859 | content=p; |
| 1860 | } |
cristy | 043f3f3 | 2012-03-02 17:37:28 +0000 | [diff] [blame] | 1861 | value=InterpretLocaleValue(content,&p); |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 1862 | (void) value; |
| 1863 | if (p != content) |
| 1864 | { |
| 1865 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1866 | "XmlInvalidContent", "<level> too many values, table \"%s\"",slot); |
| 1867 | quantization_tables=DestroyXMLTree(quantization_tables); |
| 1868 | table=DestroyQuantizationTable(table); |
| 1869 | xml=DestroyString(xml); |
| 1870 | return(table); |
| 1871 | } |
cristy | 043f3f3 | 2012-03-02 17:37:28 +0000 | [diff] [blame] | 1872 | for (j=i; j < 64; j++) |
| 1873 | table->levels[j]=table->levels[j-1]; |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 1874 | quantization_tables=DestroyXMLTree(quantization_tables); |
| 1875 | xml=DestroyString(xml); |
| 1876 | return(table); |
| 1877 | } |
| 1878 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1879 | static void InitializeDestination(j_compress_ptr cinfo) |
| 1880 | { |
| 1881 | DestinationManager |
| 1882 | *destination; |
| 1883 | |
| 1884 | destination=(DestinationManager *) cinfo->dest; |
| 1885 | destination->buffer=(JOCTET *) (*cinfo->mem->alloc_small) |
| 1886 | ((j_common_ptr) cinfo,JPOOL_IMAGE,MaxBufferExtent*sizeof(JOCTET)); |
| 1887 | destination->manager.next_output_byte=destination->buffer; |
| 1888 | destination->manager.free_in_buffer=MaxBufferExtent; |
| 1889 | } |
| 1890 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1891 | static void TerminateDestination(j_compress_ptr cinfo) |
| 1892 | { |
| 1893 | DestinationManager |
| 1894 | *destination; |
| 1895 | |
| 1896 | destination=(DestinationManager *) cinfo->dest; |
| 1897 | if ((MaxBufferExtent-(int) destination->manager.free_in_buffer) > 0) |
| 1898 | { |
| 1899 | ssize_t |
| 1900 | count; |
| 1901 | |
| 1902 | count=WriteBlob(destination->image,MaxBufferExtent- |
| 1903 | destination->manager.free_in_buffer,destination->buffer); |
| 1904 | if (count != (ssize_t) |
| 1905 | (MaxBufferExtent-destination->manager.free_in_buffer)) |
| 1906 | ERREXIT(cinfo,JERR_FILE_WRITE); |
| 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | static void WriteProfile(j_compress_ptr jpeg_info,Image *image) |
| 1911 | { |
| 1912 | const char |
| 1913 | *name; |
| 1914 | |
| 1915 | const StringInfo |
| 1916 | *profile; |
| 1917 | |
| 1918 | MagickBooleanType |
| 1919 | iptc; |
| 1920 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1921 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1922 | i; |
| 1923 | |
| 1924 | size_t |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 1925 | length, |
| 1926 | tag_length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1927 | |
| 1928 | StringInfo |
| 1929 | *custom_profile; |
| 1930 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1931 | /* |
| 1932 | Save image profile as a APP marker. |
| 1933 | */ |
| 1934 | iptc=MagickFalse; |
| 1935 | custom_profile=AcquireStringInfo(65535L); |
| 1936 | ResetImageProfileIterator(image); |
| 1937 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
| 1938 | { |
cristy | 109e557 | 2010-09-16 18:38:17 +0000 | [diff] [blame] | 1939 | register unsigned char |
| 1940 | *p; |
| 1941 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1942 | profile=GetImageProfile(image,name); |
cristy | 109e557 | 2010-09-16 18:38:17 +0000 | [diff] [blame] | 1943 | p=GetStringInfoDatum(custom_profile); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1944 | if (LocaleCompare(name,"EXIF") == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1945 | for (i=0; i < (ssize_t) GetStringInfoLength(profile); i+=65533L) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1946 | { |
| 1947 | length=MagickMin(GetStringInfoLength(profile)-i,65533L); |
| 1948 | jpeg_write_marker(jpeg_info,XML_MARKER,GetStringInfoDatum(profile)+i, |
| 1949 | (unsigned int) length); |
| 1950 | } |
| 1951 | if (LocaleCompare(name,"ICC") == 0) |
| 1952 | { |
| 1953 | register unsigned char |
| 1954 | *p; |
| 1955 | |
cristy | 8ba82ae | 2013-05-29 14:19:49 +0000 | [diff] [blame] | 1956 | tag_length=strlen(ICC_PROFILE); |
cristy | 35ce5c3 | 2010-09-16 23:16:02 +0000 | [diff] [blame] | 1957 | p=GetStringInfoDatum(custom_profile); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1958 | (void) CopyMagickMemory(p,ICC_PROFILE,tag_length); |
cristy | c3696d4 | 2013-07-16 20:18:37 +0000 | [diff] [blame] | 1959 | p[tag_length]='\0'; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1960 | for (i=0; i < (ssize_t) GetStringInfoLength(profile); i+=65519L) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1961 | { |
| 1962 | length=MagickMin(GetStringInfoLength(profile)-i,65519L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1963 | p[12]=(unsigned char) ((i/65519L)+1); |
| 1964 | p[13]=(unsigned char) (GetStringInfoLength(profile)/65519L+1); |
cristy | c1381ed | 2013-06-28 18:10:23 +0000 | [diff] [blame] | 1965 | (void) CopyMagickMemory(p+tag_length+3,GetStringInfoDatum(profile)+i, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1966 | length); |
| 1967 | jpeg_write_marker(jpeg_info,ICC_MARKER,GetStringInfoDatum( |
cristy | c1381ed | 2013-06-28 18:10:23 +0000 | [diff] [blame] | 1968 | custom_profile),(unsigned int) (length+tag_length+3)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1969 | } |
| 1970 | } |
| 1971 | if (((LocaleCompare(name,"IPTC") == 0) || |
| 1972 | (LocaleCompare(name,"8BIM") == 0)) && (iptc == MagickFalse)) |
| 1973 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1974 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1975 | roundup; |
| 1976 | |
| 1977 | iptc=MagickTrue; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1978 | for (i=0; i < (ssize_t) GetStringInfoLength(profile); i+=65500L) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1979 | { |
| 1980 | length=MagickMin(GetStringInfoLength(profile)-i,65500L); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1981 | roundup=(size_t) (length & 0x01); |
cristy | 109e557 | 2010-09-16 18:38:17 +0000 | [diff] [blame] | 1982 | if (LocaleNCompare((char *) GetStringInfoDatum(profile),"8BIM",4) == 0) |
| 1983 | { |
| 1984 | (void) memcpy(p,"Photoshop 3.0 ",14); |
| 1985 | tag_length=14; |
| 1986 | } |
| 1987 | else |
| 1988 | { |
| 1989 | (void) CopyMagickMemory(p,"Photoshop 3.0 8BIM\04\04\0\0\0\0",24); |
| 1990 | tag_length=26; |
| 1991 | p[24]=(unsigned char) (length >> 8); |
| 1992 | p[25]=(unsigned char) (length & 0xff); |
| 1993 | } |
| 1994 | p[13]=0x00; |
| 1995 | (void) memcpy(p+tag_length,GetStringInfoDatum(profile)+i,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1996 | if (roundup != 0) |
| 1997 | p[length+tag_length]='\0'; |
| 1998 | jpeg_write_marker(jpeg_info,IPTC_MARKER,GetStringInfoDatum( |
| 1999 | custom_profile),(unsigned int) (length+tag_length+roundup)); |
| 2000 | } |
| 2001 | } |
| 2002 | if (LocaleCompare(name,"XMP") == 0) |
| 2003 | { |
| 2004 | StringInfo |
| 2005 | *xmp_profile; |
| 2006 | |
| 2007 | /* |
| 2008 | Add namespace to XMP profile. |
| 2009 | */ |
cristy | 0615f0e | 2011-10-12 11:36:46 +0000 | [diff] [blame] | 2010 | xmp_profile=StringToStringInfo("http://ns.adobe.com/xap/1.0/ "); |
cristy | 30841e6 | 2014-05-19 00:45:15 +0000 | [diff] [blame] | 2011 | if (xmp_profile != (StringInfo *) NULL) |
cristy | 8418c7e | 2014-05-18 18:38:26 +0000 | [diff] [blame] | 2012 | { |
cristy | c08a0aa | 2014-05-25 23:30:09 +0000 | [diff] [blame] | 2013 | if (profile != (StringInfo *) NULL) |
| 2014 | ConcatenateStringInfo(xmp_profile,profile); |
cristy | 8418c7e | 2014-05-18 18:38:26 +0000 | [diff] [blame] | 2015 | GetStringInfoDatum(xmp_profile)[28]='\0'; |
| 2016 | for (i=0; i < (ssize_t) GetStringInfoLength(xmp_profile); i+=65533L) |
| 2017 | { |
| 2018 | length=MagickMin(GetStringInfoLength(xmp_profile)-i,65533L); |
| 2019 | jpeg_write_marker(jpeg_info,XML_MARKER, |
| 2020 | GetStringInfoDatum(xmp_profile)+i,(unsigned int) length); |
| 2021 | } |
| 2022 | xmp_profile=DestroyStringInfo(xmp_profile); |
| 2023 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2024 | } |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2025 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2026 | "%s profile: %.20g bytes",name,(double) GetStringInfoLength(profile)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2027 | name=GetNextImageProfile(image); |
| 2028 | } |
| 2029 | custom_profile=DestroyStringInfo(custom_profile); |
| 2030 | } |
| 2031 | |
| 2032 | static void JPEGDestinationManager(j_compress_ptr cinfo,Image * image) |
| 2033 | { |
| 2034 | DestinationManager |
| 2035 | *destination; |
| 2036 | |
| 2037 | cinfo->dest=(struct jpeg_destination_mgr *) (*cinfo->mem->alloc_small) |
| 2038 | ((j_common_ptr) cinfo,JPOOL_IMAGE,sizeof(DestinationManager)); |
| 2039 | destination=(DestinationManager *) cinfo->dest; |
| 2040 | destination->manager.init_destination=InitializeDestination; |
| 2041 | destination->manager.empty_output_buffer=EmptyOutputBuffer; |
| 2042 | destination->manager.term_destination=TerminateDestination; |
| 2043 | destination->image=image; |
| 2044 | } |
| 2045 | |
| 2046 | static char **SamplingFactorToList(const char *text) |
| 2047 | { |
| 2048 | char |
| 2049 | **textlist; |
| 2050 | |
| 2051 | register char |
| 2052 | *q; |
| 2053 | |
| 2054 | register const char |
| 2055 | *p; |
| 2056 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2057 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2058 | i; |
| 2059 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2060 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2061 | lines; |
| 2062 | |
| 2063 | if (text == (char *) NULL) |
| 2064 | return((char **) NULL); |
| 2065 | /* |
| 2066 | Convert string to an ASCII list. |
| 2067 | */ |
| 2068 | lines=1; |
| 2069 | for (p=text; *p != '\0'; p++) |
| 2070 | if (*p == ',') |
| 2071 | lines++; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 2072 | textlist=(char **) AcquireQuantumMemory((size_t) lines+MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2073 | sizeof(*textlist)); |
| 2074 | if (textlist == (char **) NULL) |
| 2075 | ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText"); |
| 2076 | p=text; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2077 | for (i=0; i < (ssize_t) lines; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2078 | { |
| 2079 | for (q=(char *) p; *q != '\0'; q++) |
| 2080 | if (*q == ',') |
| 2081 | break; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 2082 | textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2083 | sizeof(*textlist[i])); |
| 2084 | if (textlist[i] == (char *) NULL) |
| 2085 | ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText"); |
| 2086 | (void) CopyMagickString(textlist[i],p,(size_t) (q-p+1)); |
| 2087 | if (*q == '\r') |
| 2088 | q++; |
| 2089 | p=q+1; |
| 2090 | } |
| 2091 | textlist[i]=(char *) NULL; |
| 2092 | return(textlist); |
| 2093 | } |
| 2094 | |
| 2095 | static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2096 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2097 | { |
| 2098 | const char |
| 2099 | *option, |
| 2100 | *sampling_factor, |
| 2101 | *value; |
| 2102 | |
| 2103 | ErrorManager |
| 2104 | error_manager; |
| 2105 | |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 2106 | Image |
| 2107 | *volatile volatile_image; |
| 2108 | |
cristy | 21749e9 | 2012-02-18 02:29:21 +0000 | [diff] [blame] | 2109 | int |
cristy | 7b8ed29 | 2013-05-18 01:13:56 +0000 | [diff] [blame] | 2110 | colorspace, |
cristy | 21749e9 | 2012-02-18 02:29:21 +0000 | [diff] [blame] | 2111 | quality; |
| 2112 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2113 | JSAMPLE |
cristy | 75fc68f | 2012-10-08 16:26:00 +0000 | [diff] [blame] | 2114 | *volatile jpeg_pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2115 | |
| 2116 | JSAMPROW |
| 2117 | scanline[1]; |
| 2118 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2119 | MagickBooleanType |
| 2120 | status; |
| 2121 | |
cristy | 22646e2 | 2013-06-23 16:34:03 +0000 | [diff] [blame] | 2122 | MemoryInfo |
| 2123 | *memory_info; |
| 2124 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2125 | register JSAMPLE |
| 2126 | *q; |
| 2127 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2128 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2129 | i; |
| 2130 | |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 2131 | ssize_t |
| 2132 | y; |
| 2133 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2134 | struct jpeg_compress_struct |
| 2135 | jpeg_info; |
| 2136 | |
| 2137 | struct jpeg_error_mgr |
| 2138 | jpeg_error; |
| 2139 | |
cristy | 5328a4c | 2013-12-03 11:32:13 +0000 | [diff] [blame] | 2140 | unsigned short |
| 2141 | scale; |
| 2142 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2143 | /* |
| 2144 | Open image file. |
| 2145 | */ |
| 2146 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame^] | 2147 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2148 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame^] | 2149 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2150 | if (image->debug != MagickFalse) |
| 2151 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2152 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame^] | 2153 | assert(exception->signature == MagickCoreSignature); |
cristy | a663a4f | 2015-01-23 12:11:57 +0000 | [diff] [blame] | 2154 | if ((LocaleCompare(image_info->magick,"JPS") == 0) && |
| 2155 | (image->next != (Image *) NULL)) |
| 2156 | image=AppendImages(image,MagickFalse,exception); |
cristy | 73ef4eb | 2015-01-23 14:31:57 +0000 | [diff] [blame] | 2157 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
| 2158 | if (status == MagickFalse) |
| 2159 | return(status); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2160 | /* |
| 2161 | Initialize JPEG parameters. |
| 2162 | */ |
cristy | 9104497 | 2011-04-22 14:21:16 +0000 | [diff] [blame] | 2163 | (void) ResetMagickMemory(&error_manager,0,sizeof(error_manager)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2164 | (void) ResetMagickMemory(&jpeg_info,0,sizeof(jpeg_info)); |
| 2165 | (void) ResetMagickMemory(&jpeg_error,0,sizeof(jpeg_error)); |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 2166 | volatile_image=image; |
| 2167 | jpeg_info.client_data=(void *) volatile_image; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2168 | jpeg_info.err=jpeg_std_error(&jpeg_error); |
cristy | d28b1dd | 2011-05-14 20:30:38 +0000 | [diff] [blame] | 2169 | jpeg_info.err->emit_message=(void (*)(j_common_ptr,int)) JPEGWarningHandler; |
cristy | 9104497 | 2011-04-22 14:21:16 +0000 | [diff] [blame] | 2170 | jpeg_info.err->error_exit=(void (*)(j_common_ptr)) JPEGErrorHandler; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2171 | error_manager.exception=exception; |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 2172 | error_manager.image=volatile_image; |
cristy | 72f87f8 | 2013-06-23 16:43:05 +0000 | [diff] [blame] | 2173 | memory_info=(MemoryInfo *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2174 | if (setjmp(error_manager.error_recovery) != 0) |
| 2175 | { |
| 2176 | jpeg_destroy_compress(&jpeg_info); |
cristy | 86b6e55 | 2015-06-06 13:25:16 +0000 | [diff] [blame] | 2177 | (void) CloseBlob(volatile_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2178 | return(MagickFalse); |
| 2179 | } |
| 2180 | jpeg_info.client_data=(void *) &error_manager; |
| 2181 | jpeg_create_compress(&jpeg_info); |
| 2182 | JPEGDestinationManager(&jpeg_info,image); |
| 2183 | if ((image->columns != (unsigned int) image->columns) || |
| 2184 | (image->rows != (unsigned int) image->rows)) |
| 2185 | ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit"); |
| 2186 | jpeg_info.image_width=(unsigned int) image->columns; |
| 2187 | jpeg_info.image_height=(unsigned int) image->rows; |
| 2188 | jpeg_info.input_components=3; |
| 2189 | jpeg_info.data_precision=8; |
| 2190 | jpeg_info.in_color_space=JCS_RGB; |
| 2191 | switch (image->colorspace) |
| 2192 | { |
| 2193 | case CMYKColorspace: |
| 2194 | { |
| 2195 | jpeg_info.input_components=4; |
| 2196 | jpeg_info.in_color_space=JCS_CMYK; |
| 2197 | break; |
| 2198 | } |
| 2199 | case YCbCrColorspace: |
| 2200 | case Rec601YCbCrColorspace: |
| 2201 | case Rec709YCbCrColorspace: |
| 2202 | { |
| 2203 | jpeg_info.in_color_space=JCS_YCbCr; |
| 2204 | break; |
| 2205 | } |
| 2206 | case GRAYColorspace: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2207 | { |
cristy | e935555 | 2014-01-16 13:35:05 +0000 | [diff] [blame] | 2208 | if (image_info->type == TrueColorType) |
| 2209 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2210 | jpeg_info.input_components=1; |
| 2211 | jpeg_info.in_color_space=JCS_GRAYSCALE; |
| 2212 | break; |
| 2213 | } |
| 2214 | default: |
cristy | 9f39678 | 2009-12-21 01:37:45 +0000 | [diff] [blame] | 2215 | { |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 2216 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | e935555 | 2014-01-16 13:35:05 +0000 | [diff] [blame] | 2217 | if (image_info->type == TrueColorType) |
| 2218 | break; |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 2219 | if (SetImageGray(image,exception) != MagickFalse) |
cristy | e935555 | 2014-01-16 13:35:05 +0000 | [diff] [blame] | 2220 | { |
| 2221 | jpeg_info.input_components=1; |
| 2222 | jpeg_info.in_color_space=JCS_GRAYSCALE; |
| 2223 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2224 | break; |
cristy | 9f39678 | 2009-12-21 01:37:45 +0000 | [diff] [blame] | 2225 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2226 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2227 | jpeg_set_defaults(&jpeg_info); |
cristy | 1136909 | 2013-02-03 22:38:57 +0000 | [diff] [blame] | 2228 | if (jpeg_info.in_color_space == JCS_CMYK) |
| 2229 | jpeg_set_colorspace(&jpeg_info,JCS_YCCK); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2230 | if ((jpeg_info.data_precision != 12) && (image->depth <= 8)) |
| 2231 | jpeg_info.data_precision=8; |
| 2232 | else |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 2233 | jpeg_info.data_precision=BITS_IN_JSAMPLE; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2234 | if (image->debug != MagickFalse) |
| 2235 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 2236 | "Image resolution: %.20g,%.20g",floor(image->resolution.x+0.5), |
| 2237 | floor(image->resolution.y+0.5)); |
| 2238 | if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2239 | { |
| 2240 | /* |
| 2241 | Set image resolution. |
| 2242 | */ |
cristy | df87801 | 2014-01-12 23:39:40 +0000 | [diff] [blame] | 2243 | jpeg_info.write_JFIF_header=TRUE; |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 2244 | jpeg_info.X_density=(UINT16) floor(image->resolution.x+0.5); |
| 2245 | jpeg_info.Y_density=(UINT16) floor(image->resolution.y+0.5); |
cristy | 0c22063 | 2013-09-25 22:10:28 +0000 | [diff] [blame] | 2246 | /* |
| 2247 | Set image resolution units. |
| 2248 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2249 | if (image->units == PixelsPerInchResolution) |
| 2250 | jpeg_info.density_unit=(UINT8) 1; |
| 2251 | if (image->units == PixelsPerCentimeterResolution) |
| 2252 | jpeg_info.density_unit=(UINT8) 2; |
| 2253 | } |
cristy | 97cb3bf | 2012-02-21 18:31:20 +0000 | [diff] [blame] | 2254 | jpeg_info.dct_method=JDCT_FLOAT; |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 2255 | option=GetImageOption(image_info,"jpeg:dct-method"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2256 | if (option != (const char *) NULL) |
| 2257 | switch (*option) |
| 2258 | { |
| 2259 | case 'D': |
| 2260 | case 'd': |
| 2261 | { |
| 2262 | if (LocaleCompare(option,"default") == 0) |
| 2263 | jpeg_info.dct_method=JDCT_DEFAULT; |
| 2264 | break; |
| 2265 | } |
| 2266 | case 'F': |
| 2267 | case 'f': |
| 2268 | { |
| 2269 | if (LocaleCompare(option,"fastest") == 0) |
| 2270 | jpeg_info.dct_method=JDCT_FASTEST; |
| 2271 | if (LocaleCompare(option,"float") == 0) |
| 2272 | jpeg_info.dct_method=JDCT_FLOAT; |
| 2273 | break; |
| 2274 | } |
| 2275 | case 'I': |
| 2276 | case 'i': |
| 2277 | { |
| 2278 | if (LocaleCompare(option,"ifast") == 0) |
| 2279 | jpeg_info.dct_method=JDCT_IFAST; |
| 2280 | if (LocaleCompare(option,"islow") == 0) |
| 2281 | jpeg_info.dct_method=JDCT_ISLOW; |
| 2282 | break; |
| 2283 | } |
| 2284 | } |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 2285 | option=GetImageOption(image_info,"jpeg:optimize-coding"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2286 | if (option != (const char *) NULL) |
cristy | b9d0e7e | 2014-11-02 18:14:44 +0000 | [diff] [blame] | 2287 | jpeg_info.optimize_coding=IsStringTrue(option) != MagickFalse ? TRUE : |
| 2288 | FALSE; |
cristy | 7d31f6e | 2014-11-11 13:07:58 +0000 | [diff] [blame] | 2289 | else |
| 2290 | { |
| 2291 | MagickSizeType |
| 2292 | length; |
| 2293 | |
| 2294 | length=(MagickSizeType) jpeg_info.input_components*image->columns* |
| 2295 | image->rows*sizeof(JSAMPLE); |
| 2296 | if (length == (MagickSizeType) ((size_t) length)) |
| 2297 | { |
| 2298 | /* |
| 2299 | Perform optimization only if available memory resources permit it. |
| 2300 | */ |
| 2301 | status=AcquireMagickResource(MemoryResource,length); |
| 2302 | RelinquishMagickResource(MemoryResource,length); |
| 2303 | jpeg_info.optimize_coding=status == MagickFalse ? FALSE : TRUE; |
| 2304 | } |
| 2305 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2306 | #if (JPEG_LIB_VERSION >= 61) && defined(C_PROGRESSIVE_SUPPORTED) |
| 2307 | if ((LocaleCompare(image_info->magick,"PJPEG") == 0) || |
| 2308 | (image_info->interlace != NoInterlace)) |
| 2309 | { |
| 2310 | if (image->debug != MagickFalse) |
| 2311 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2312 | "Interlace: progressive"); |
| 2313 | jpeg_simple_progression(&jpeg_info); |
| 2314 | } |
| 2315 | else |
| 2316 | if (image->debug != MagickFalse) |
| 2317 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2318 | "Interlace: non-progressive"); |
| 2319 | #else |
| 2320 | if (image->debug != MagickFalse) |
| 2321 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2322 | "Interlace: nonprogressive"); |
| 2323 | #endif |
dirk | 871abab | 2013-11-03 13:21:16 +0000 | [diff] [blame] | 2324 | quality=92; |
cristy | 2a95040 | 2014-03-12 17:57:14 +0000 | [diff] [blame] | 2325 | if ((image_info->compression != LosslessJPEGCompression) && |
| 2326 | (image->quality <= 100)) |
| 2327 | { |
| 2328 | if (image->quality != UndefinedCompressionQuality) |
| 2329 | quality=(int) image->quality; |
| 2330 | if (image->debug != MagickFalse) |
| 2331 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Quality: %.20g", |
| 2332 | (double) image->quality); |
| 2333 | } |
| 2334 | else |
| 2335 | { |
| 2336 | #if !defined(C_LOSSLESS_SUPPORTED) |
| 2337 | quality=100; |
| 2338 | if (image->debug != MagickFalse) |
| 2339 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Quality: 100"); |
| 2340 | #else |
| 2341 | if (image->quality < 100) |
| 2342 | (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning, |
| 2343 | "LosslessToLossyJPEGConversion",image->filename); |
| 2344 | else |
| 2345 | { |
| 2346 | int |
| 2347 | point_transform, |
| 2348 | predictor; |
| 2349 | |
| 2350 | predictor=image->quality/100; /* range 1-7 */ |
| 2351 | point_transform=image->quality % 20; /* range 0-15 */ |
| 2352 | jpeg_simple_lossless(&jpeg_info,predictor,point_transform); |
| 2353 | if (image->debug != MagickFalse) |
| 2354 | { |
| 2355 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2356 | "Compression: lossless"); |
| 2357 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2358 | "Predictor: %d",predictor); |
| 2359 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2360 | "Point Transform: %d",point_transform); |
| 2361 | } |
| 2362 | } |
| 2363 | #endif |
| 2364 | } |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 2365 | option=GetImageOption(image_info,"jpeg:extent"); |
cristy | 0adb4f9 | 2009-11-28 18:08:51 +0000 | [diff] [blame] | 2366 | if (option != (const char *) NULL) |
| 2367 | { |
| 2368 | Image |
| 2369 | *jpeg_image; |
| 2370 | |
| 2371 | ImageInfo |
| 2372 | *jpeg_info; |
| 2373 | |
| 2374 | jpeg_info=CloneImageInfo(image_info); |
cristy | e0823d5 | 2013-11-03 21:50:10 +0000 | [diff] [blame] | 2375 | jpeg_info->blob=NULL; |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2376 | jpeg_image=CloneImage(image,0,0,MagickTrue,exception); |
cristy | 0adb4f9 | 2009-11-28 18:08:51 +0000 | [diff] [blame] | 2377 | if (jpeg_image != (Image *) NULL) |
| 2378 | { |
| 2379 | MagickSizeType |
| 2380 | extent; |
| 2381 | |
| 2382 | size_t |
cristy | 87e73ab | 2010-02-12 01:59:12 +0000 | [diff] [blame] | 2383 | maximum, |
| 2384 | minimum; |
cristy | 0adb4f9 | 2009-11-28 18:08:51 +0000 | [diff] [blame] | 2385 | |
| 2386 | /* |
| 2387 | Search for compression quality that does not exceed image extent. |
| 2388 | */ |
| 2389 | jpeg_info->quality=0; |
cristy | d6495f9 | 2011-12-01 19:36:52 +0000 | [diff] [blame] | 2390 | extent=(MagickSizeType) SiPrefixToDoubleInterval(option,100.0); |
cristy | 0adb4f9 | 2009-11-28 18:08:51 +0000 | [diff] [blame] | 2391 | (void) DeleteImageOption(jpeg_info,"jpeg:extent"); |
cristy | 2e5309e | 2013-04-27 14:48:31 +0000 | [diff] [blame] | 2392 | (void) DeleteImageArtifact(jpeg_image,"jpeg:extent"); |
cristy | 4e8b2ed | 2015-03-06 19:54:18 +0000 | [diff] [blame] | 2393 | maximum=image->quality; |
| 2394 | if (maximum < 2) |
| 2395 | maximum=2; |
dirk | cf84524 | 2013-08-11 21:36:19 +0000 | [diff] [blame] | 2396 | for (minimum=2; minimum < maximum; ) |
cristy | 0adb4f9 | 2009-11-28 18:08:51 +0000 | [diff] [blame] | 2397 | { |
cristy | 3975f40 | 2014-02-26 14:36:33 +0000 | [diff] [blame] | 2398 | (void) AcquireUniqueFilename(jpeg_image->filename); |
| 2399 | jpeg_image->quality=minimum+(maximum-minimum+1)/2; |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2400 | status=WriteJPEGImage(jpeg_info,jpeg_image,exception); |
cristy | c8d69b1 | 2010-02-13 19:06:26 +0000 | [diff] [blame] | 2401 | if (GetBlobSize(jpeg_image) <= extent) |
cristy | 3975f40 | 2014-02-26 14:36:33 +0000 | [diff] [blame] | 2402 | minimum=jpeg_image->quality+1; |
cristy | 0adb4f9 | 2009-11-28 18:08:51 +0000 | [diff] [blame] | 2403 | else |
cristy | 3975f40 | 2014-02-26 14:36:33 +0000 | [diff] [blame] | 2404 | maximum=jpeg_image->quality-1; |
| 2405 | (void) RelinquishUniqueFileResource(jpeg_image->filename); |
cristy | 0adb4f9 | 2009-11-28 18:08:51 +0000 | [diff] [blame] | 2406 | } |
cristy | 58d4fe1 | 2015-02-07 11:53:05 +0000 | [diff] [blame] | 2407 | quality=(int) minimum-1; |
cristy | 0adb4f9 | 2009-11-28 18:08:51 +0000 | [diff] [blame] | 2408 | jpeg_image=DestroyImage(jpeg_image); |
| 2409 | } |
| 2410 | jpeg_info=DestroyImageInfo(jpeg_info); |
| 2411 | } |
cristy | 9bb7c84 | 2014-06-17 23:42:24 +0000 | [diff] [blame] | 2412 | jpeg_set_quality(&jpeg_info,quality,TRUE); |
cristy | 16e7472 | 2012-07-01 13:01:38 +0000 | [diff] [blame] | 2413 | #if (JPEG_LIB_VERSION >= 70) |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 2414 | option=GetImageOption(image_info,"quality"); |
cristy | 16e7472 | 2012-07-01 13:01:38 +0000 | [diff] [blame] | 2415 | if (option != (const char *) NULL) |
| 2416 | { |
| 2417 | GeometryInfo |
| 2418 | geometry_info; |
| 2419 | |
| 2420 | int |
| 2421 | flags; |
| 2422 | |
| 2423 | /* |
| 2424 | Set quality scaling for luminance and chrominance separately. |
| 2425 | */ |
| 2426 | flags=ParseGeometry(option,&geometry_info); |
| 2427 | if (((flags & RhoValue) != 0) && ((flags & SigmaValue) != 0)) |
| 2428 | { |
| 2429 | jpeg_info.q_scale_factor[0]=jpeg_quality_scaling((int) |
| 2430 | (geometry_info.rho+0.5)); |
| 2431 | jpeg_info.q_scale_factor[1]=jpeg_quality_scaling((int) |
| 2432 | (geometry_info.sigma+0.5)); |
cristy | 9bb7c84 | 2014-06-17 23:42:24 +0000 | [diff] [blame] | 2433 | jpeg_default_qtables(&jpeg_info,TRUE); |
cristy | 16e7472 | 2012-07-01 13:01:38 +0000 | [diff] [blame] | 2434 | } |
| 2435 | } |
| 2436 | #endif |
cristy | 7b8ed29 | 2013-05-18 01:13:56 +0000 | [diff] [blame] | 2437 | colorspace=jpeg_info.in_color_space; |
| 2438 | value=GetImageOption(image_info,"jpeg:colorspace"); |
glennrp | 2a7dbb1 | 2012-09-20 12:48:41 +0000 | [diff] [blame] | 2439 | if (value == (char *) NULL) |
cristy | 7b8ed29 | 2013-05-18 01:13:56 +0000 | [diff] [blame] | 2440 | value=GetImageProperty(image,"jpeg:colorspace",exception); |
cristy | efb04e2 | 2012-09-17 23:29:25 +0000 | [diff] [blame] | 2441 | if (value != (char *) NULL) |
cristy | 7b8ed29 | 2013-05-18 01:13:56 +0000 | [diff] [blame] | 2442 | colorspace=StringToInteger(value); |
| 2443 | sampling_factor=(const char *) NULL; |
| 2444 | if (colorspace == jpeg_info.in_color_space) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2445 | { |
cristy | 7b8ed29 | 2013-05-18 01:13:56 +0000 | [diff] [blame] | 2446 | value=GetImageOption(image_info,"jpeg:sampling-factor"); |
| 2447 | if (value == (char *) NULL) |
| 2448 | value=GetImageProperty(image,"jpeg:sampling-factor",exception); |
| 2449 | if (value != (char *) NULL) |
| 2450 | { |
| 2451 | sampling_factor=value; |
| 2452 | if (image->debug != MagickFalse) |
| 2453 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2454 | " Input sampling-factors=%s",sampling_factor); |
| 2455 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2456 | } |
cristy | 7b8ed29 | 2013-05-18 01:13:56 +0000 | [diff] [blame] | 2457 | value=GetImageOption(image_info,"jpeg:sampling-factor"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2458 | if (image_info->sampling_factor != (char *) NULL) |
| 2459 | sampling_factor=image_info->sampling_factor; |
| 2460 | if (sampling_factor == (const char *) NULL) |
| 2461 | { |
cristy | c243f62 | 2015-02-21 17:20:34 +0000 | [diff] [blame] | 2462 | if (quality >= 90) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2463 | for (i=0; i < MAX_COMPONENTS; i++) |
| 2464 | { |
| 2465 | jpeg_info.comp_info[i].h_samp_factor=1; |
| 2466 | jpeg_info.comp_info[i].v_samp_factor=1; |
| 2467 | } |
| 2468 | } |
| 2469 | else |
| 2470 | { |
| 2471 | char |
| 2472 | **factors; |
| 2473 | |
| 2474 | GeometryInfo |
| 2475 | geometry_info; |
| 2476 | |
| 2477 | MagickStatusType |
| 2478 | flags; |
| 2479 | |
| 2480 | /* |
| 2481 | Set sampling factor. |
| 2482 | */ |
| 2483 | i=0; |
| 2484 | factors=SamplingFactorToList(sampling_factor); |
| 2485 | if (factors != (char **) NULL) |
| 2486 | { |
| 2487 | for (i=0; i < MAX_COMPONENTS; i++) |
| 2488 | { |
| 2489 | if (factors[i] == (char *) NULL) |
| 2490 | break; |
| 2491 | flags=ParseGeometry(factors[i],&geometry_info); |
| 2492 | if ((flags & SigmaValue) == 0) |
| 2493 | geometry_info.sigma=geometry_info.rho; |
| 2494 | jpeg_info.comp_info[i].h_samp_factor=(int) geometry_info.rho; |
| 2495 | jpeg_info.comp_info[i].v_samp_factor=(int) geometry_info.sigma; |
| 2496 | factors[i]=(char *) RelinquishMagickMemory(factors[i]); |
| 2497 | } |
| 2498 | factors=(char **) RelinquishMagickMemory(factors); |
| 2499 | } |
| 2500 | for ( ; i < MAX_COMPONENTS; i++) |
| 2501 | { |
| 2502 | jpeg_info.comp_info[i].h_samp_factor=1; |
| 2503 | jpeg_info.comp_info[i].v_samp_factor=1; |
| 2504 | } |
| 2505 | } |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 2506 | option=GetImageOption(image_info,"jpeg:q-table"); |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 2507 | if (option != (const char *) NULL) |
cristy | 1445b32 | 2012-02-19 18:57:30 +0000 | [diff] [blame] | 2508 | { |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 2509 | QuantizationTable |
| 2510 | *table; |
cristy | b4bb39c | 2012-02-21 18:45:54 +0000 | [diff] [blame] | 2511 | |
cristy | 1445b32 | 2012-02-19 18:57:30 +0000 | [diff] [blame] | 2512 | /* |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 2513 | Custom quantization tables. |
cristy | 1445b32 | 2012-02-19 18:57:30 +0000 | [diff] [blame] | 2514 | */ |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 2515 | table=GetQuantizationTable(option,"0",exception); |
| 2516 | if (table != (QuantizationTable *) NULL) |
| 2517 | { |
cristy | ae453b7 | 2013-04-21 22:24:20 +0000 | [diff] [blame] | 2518 | for (i=0; i < MAX_COMPONENTS; i++) |
cristy | d2fe6d1 | 2013-04-21 22:29:56 +0000 | [diff] [blame] | 2519 | jpeg_info.comp_info[i].quant_tbl_no=0; |
| 2520 | jpeg_add_quant_table(&jpeg_info,0,table->levels, |
| 2521 | jpeg_quality_scaling(quality),0); |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 2522 | table=DestroyQuantizationTable(table); |
| 2523 | } |
| 2524 | table=GetQuantizationTable(option,"1",exception); |
| 2525 | if (table != (QuantizationTable *) NULL) |
| 2526 | { |
cristy | ae453b7 | 2013-04-21 22:24:20 +0000 | [diff] [blame] | 2527 | for (i=1; i < MAX_COMPONENTS; i++) |
cristy | d2fe6d1 | 2013-04-21 22:29:56 +0000 | [diff] [blame] | 2528 | jpeg_info.comp_info[i].quant_tbl_no=1; |
| 2529 | jpeg_add_quant_table(&jpeg_info,1,table->levels, |
| 2530 | jpeg_quality_scaling(quality),0); |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 2531 | table=DestroyQuantizationTable(table); |
| 2532 | } |
| 2533 | table=GetQuantizationTable(option,"2",exception); |
| 2534 | if (table != (QuantizationTable *) NULL) |
| 2535 | { |
cristy | ae453b7 | 2013-04-21 22:24:20 +0000 | [diff] [blame] | 2536 | for (i=2; i < MAX_COMPONENTS; i++) |
cristy | d2fe6d1 | 2013-04-21 22:29:56 +0000 | [diff] [blame] | 2537 | jpeg_info.comp_info[i].quant_tbl_no=2; |
| 2538 | jpeg_add_quant_table(&jpeg_info,2,table->levels, |
| 2539 | jpeg_quality_scaling(quality),0); |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 2540 | table=DestroyQuantizationTable(table); |
| 2541 | } |
| 2542 | table=GetQuantizationTable(option,"3",exception); |
| 2543 | if (table != (QuantizationTable *) NULL) |
| 2544 | { |
cristy | ae453b7 | 2013-04-21 22:24:20 +0000 | [diff] [blame] | 2545 | for (i=3; i < MAX_COMPONENTS; i++) |
cristy | d2fe6d1 | 2013-04-21 22:29:56 +0000 | [diff] [blame] | 2546 | jpeg_info.comp_info[i].quant_tbl_no=3; |
| 2547 | jpeg_add_quant_table(&jpeg_info,3,table->levels, |
| 2548 | jpeg_quality_scaling(quality),0); |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 2549 | table=DestroyQuantizationTable(table); |
| 2550 | } |
cristy | 1445b32 | 2012-02-19 18:57:30 +0000 | [diff] [blame] | 2551 | } |
cristy | 9bb7c84 | 2014-06-17 23:42:24 +0000 | [diff] [blame] | 2552 | jpeg_start_compress(&jpeg_info,TRUE); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2553 | if (image->debug != MagickFalse) |
| 2554 | { |
| 2555 | if (image->storage_class == PseudoClass) |
| 2556 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2557 | "Storage class: PseudoClass"); |
| 2558 | else |
| 2559 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2560 | "Storage class: DirectClass"); |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2561 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Depth: %.20g", |
| 2562 | (double) image->depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2563 | if (image->colors != 0) |
| 2564 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2565 | "Number of colors: %.20g",(double) image->colors); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2566 | else |
| 2567 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2568 | "Number of colors: unspecified"); |
| 2569 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2570 | "JPEG data precision: %d",(int) jpeg_info.data_precision); |
| 2571 | switch (image->colorspace) |
| 2572 | { |
| 2573 | case CMYKColorspace: |
| 2574 | { |
| 2575 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2576 | "Storage class: DirectClass"); |
| 2577 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2578 | "Colorspace: CMYK"); |
| 2579 | break; |
| 2580 | } |
| 2581 | case YCbCrColorspace: |
| 2582 | case Rec601YCbCrColorspace: |
| 2583 | case Rec709YCbCrColorspace: |
| 2584 | { |
| 2585 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2586 | "Colorspace: YCbCr"); |
| 2587 | break; |
| 2588 | } |
| 2589 | default: |
| 2590 | break; |
| 2591 | } |
| 2592 | switch (image->colorspace) |
| 2593 | { |
| 2594 | case CMYKColorspace: |
| 2595 | { |
| 2596 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2597 | "Colorspace: CMYK"); |
| 2598 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2599 | "Sampling factors: %dx%d,%dx%d,%dx%d,%dx%d", |
| 2600 | jpeg_info.comp_info[0].h_samp_factor, |
| 2601 | jpeg_info.comp_info[0].v_samp_factor, |
| 2602 | jpeg_info.comp_info[1].h_samp_factor, |
| 2603 | jpeg_info.comp_info[1].v_samp_factor, |
| 2604 | jpeg_info.comp_info[2].h_samp_factor, |
| 2605 | jpeg_info.comp_info[2].v_samp_factor, |
| 2606 | jpeg_info.comp_info[3].h_samp_factor, |
| 2607 | jpeg_info.comp_info[3].v_samp_factor); |
| 2608 | break; |
| 2609 | } |
| 2610 | case GRAYColorspace: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2611 | { |
| 2612 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2613 | "Colorspace: GRAY"); |
| 2614 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2615 | "Sampling factors: %dx%d",jpeg_info.comp_info[0].h_samp_factor, |
| 2616 | jpeg_info.comp_info[0].v_samp_factor); |
| 2617 | break; |
| 2618 | } |
cristy | ed8d785 | 2013-08-01 22:44:22 +0000 | [diff] [blame] | 2619 | case sRGBColorspace: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2620 | case RGBColorspace: |
| 2621 | { |
| 2622 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | feed8be | 2013-08-01 22:45:13 +0000 | [diff] [blame] | 2623 | "Image colorspace is RGB"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2624 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2625 | "Sampling factors: %dx%d,%dx%d,%dx%d", |
| 2626 | jpeg_info.comp_info[0].h_samp_factor, |
| 2627 | jpeg_info.comp_info[0].v_samp_factor, |
| 2628 | jpeg_info.comp_info[1].h_samp_factor, |
| 2629 | jpeg_info.comp_info[1].v_samp_factor, |
| 2630 | jpeg_info.comp_info[2].h_samp_factor, |
| 2631 | jpeg_info.comp_info[2].v_samp_factor); |
| 2632 | break; |
| 2633 | } |
| 2634 | case YCbCrColorspace: |
| 2635 | case Rec601YCbCrColorspace: |
| 2636 | case Rec709YCbCrColorspace: |
| 2637 | { |
| 2638 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2639 | "Colorspace: YCbCr"); |
| 2640 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2641 | "Sampling factors: %dx%d,%dx%d,%dx%d", |
| 2642 | jpeg_info.comp_info[0].h_samp_factor, |
| 2643 | jpeg_info.comp_info[0].v_samp_factor, |
| 2644 | jpeg_info.comp_info[1].h_samp_factor, |
| 2645 | jpeg_info.comp_info[1].v_samp_factor, |
| 2646 | jpeg_info.comp_info[2].h_samp_factor, |
| 2647 | jpeg_info.comp_info[2].v_samp_factor); |
| 2648 | break; |
| 2649 | } |
| 2650 | default: |
| 2651 | { |
| 2652 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Colorspace: %d", |
| 2653 | image->colorspace); |
| 2654 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2655 | "Sampling factors: %dx%d,%dx%d,%dx%d,%dx%d", |
| 2656 | jpeg_info.comp_info[0].h_samp_factor, |
| 2657 | jpeg_info.comp_info[0].v_samp_factor, |
| 2658 | jpeg_info.comp_info[1].h_samp_factor, |
| 2659 | jpeg_info.comp_info[1].v_samp_factor, |
| 2660 | jpeg_info.comp_info[2].h_samp_factor, |
| 2661 | jpeg_info.comp_info[2].v_samp_factor, |
| 2662 | jpeg_info.comp_info[3].h_samp_factor, |
| 2663 | jpeg_info.comp_info[3].v_samp_factor); |
| 2664 | break; |
| 2665 | } |
| 2666 | } |
| 2667 | } |
| 2668 | /* |
| 2669 | Write JPEG profiles. |
| 2670 | */ |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2671 | value=GetImageProperty(image,"comment",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2672 | if (value != (char *) NULL) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2673 | for (i=0; i < (ssize_t) strlen(value); i+=65533L) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2674 | jpeg_write_marker(&jpeg_info,JPEG_COM,(unsigned char *) value+i, |
| 2675 | (unsigned int) MagickMin((size_t) strlen(value+i),65533L)); |
| 2676 | if (image->profiles != (void *) NULL) |
| 2677 | WriteProfile(&jpeg_info,image); |
| 2678 | /* |
| 2679 | Convert MIFF to JPEG raster pixels. |
| 2680 | */ |
cristy | 22646e2 | 2013-06-23 16:34:03 +0000 | [diff] [blame] | 2681 | memory_info=AcquireVirtualMemory((size_t) image->columns, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2682 | jpeg_info.input_components*sizeof(*jpeg_pixels)); |
cristy | 22646e2 | 2013-06-23 16:34:03 +0000 | [diff] [blame] | 2683 | if (memory_info == (MemoryInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2684 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 22646e2 | 2013-06-23 16:34:03 +0000 | [diff] [blame] | 2685 | jpeg_pixels=(JSAMPLE *) GetVirtualMemoryBlob(memory_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2686 | if (setjmp(error_manager.error_recovery) != 0) |
| 2687 | { |
| 2688 | jpeg_destroy_compress(&jpeg_info); |
cristy | 22646e2 | 2013-06-23 16:34:03 +0000 | [diff] [blame] | 2689 | if (memory_info != (MemoryInfo *) NULL) |
| 2690 | memory_info=RelinquishVirtualMemory(memory_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2691 | (void) CloseBlob(image); |
| 2692 | return(MagickFalse); |
| 2693 | } |
| 2694 | scanline[0]=(JSAMPROW) jpeg_pixels; |
cristy | 477b9fd | 2013-12-03 16:52:31 +0000 | [diff] [blame] | 2695 | scale=65535U/GetQuantumRange(jpeg_info.data_precision); |
cristy | 31db6c1 | 2013-12-04 15:17:30 +0000 | [diff] [blame] | 2696 | if (scale == 0) |
| 2697 | scale=1; |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2698 | if (jpeg_info.data_precision <= 8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2699 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2700 | if ((jpeg_info.in_color_space == JCS_RGB) || |
| 2701 | (jpeg_info.in_color_space == JCS_YCbCr)) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2702 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2703 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2704 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2705 | *p; |
| 2706 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2707 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2708 | x; |
| 2709 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2710 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2711 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2712 | break; |
| 2713 | q=jpeg_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2714 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2715 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2716 | *q++=(JSAMPLE) ScaleQuantumToChar(GetPixelRed(image,p)); |
| 2717 | *q++=(JSAMPLE) ScaleQuantumToChar(GetPixelGreen(image,p)); |
| 2718 | *q++=(JSAMPLE) ScaleQuantumToChar(GetPixelBlue(image,p)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2719 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2720 | } |
| 2721 | (void) jpeg_write_scanlines(&jpeg_info,scanline,1); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 2722 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 2723 | image->rows); |
| 2724 | if (status == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2725 | break; |
| 2726 | } |
| 2727 | else |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2728 | if (jpeg_info.in_color_space == JCS_GRAYSCALE) |
cristy | d032322 | 2013-04-07 16:13:21 +0000 | [diff] [blame] | 2729 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2730 | { |
cristy | d032322 | 2013-04-07 16:13:21 +0000 | [diff] [blame] | 2731 | register const Quantum |
| 2732 | *p; |
| 2733 | |
| 2734 | register ssize_t |
| 2735 | x; |
| 2736 | |
| 2737 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| 2738 | if (p == (const Quantum *) NULL) |
| 2739 | break; |
| 2740 | q=jpeg_pixels; |
| 2741 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2742 | { |
cristy | d032322 | 2013-04-07 16:13:21 +0000 | [diff] [blame] | 2743 | *q++=(JSAMPLE) ScaleQuantumToChar(ClampToQuantum(GetPixelLuma( |
| 2744 | image,p))); |
| 2745 | p+=GetPixelChannels(image); |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2746 | } |
cristy | d032322 | 2013-04-07 16:13:21 +0000 | [diff] [blame] | 2747 | (void) jpeg_write_scanlines(&jpeg_info,scanline,1); |
| 2748 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 2749 | image->rows); |
| 2750 | if (status == MagickFalse) |
| 2751 | break; |
| 2752 | } |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2753 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2754 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2755 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2756 | register const Quantum |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2757 | *p; |
| 2758 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2759 | register ssize_t |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2760 | x; |
| 2761 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2762 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2763 | if (p == (const Quantum *) NULL) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2764 | break; |
| 2765 | q=jpeg_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2766 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2767 | { |
| 2768 | /* |
| 2769 | Convert DirectClass packets to contiguous CMYK scanlines. |
| 2770 | */ |
| 2771 | *q++=(JSAMPLE) (ScaleQuantumToChar((Quantum) (QuantumRange- |
cristy | 679eaad | 2013-02-03 22:35:12 +0000 | [diff] [blame] | 2772 | GetPixelCyan(image,p)))); |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2773 | *q++=(JSAMPLE) (ScaleQuantumToChar((Quantum) (QuantumRange- |
cristy | 679eaad | 2013-02-03 22:35:12 +0000 | [diff] [blame] | 2774 | GetPixelMagenta(image,p)))); |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2775 | *q++=(JSAMPLE) (ScaleQuantumToChar((Quantum) (QuantumRange- |
cristy | 679eaad | 2013-02-03 22:35:12 +0000 | [diff] [blame] | 2776 | GetPixelYellow(image,p)))); |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2777 | *q++=(JSAMPLE) (ScaleQuantumToChar((Quantum) (QuantumRange- |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2778 | GetPixelBlack(image,p)))); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2779 | p+=GetPixelChannels(image); |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2780 | } |
| 2781 | (void) jpeg_write_scanlines(&jpeg_info,scanline,1); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 2782 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 2783 | image->rows); |
| 2784 | if (status == MagickFalse) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2785 | break; |
| 2786 | } |
| 2787 | } |
| 2788 | else |
| 2789 | if (jpeg_info.in_color_space == JCS_GRAYSCALE) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2790 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2791 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2792 | register const Quantum |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2793 | *p; |
| 2794 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2795 | register ssize_t |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2796 | x; |
| 2797 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2798 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2799 | if (p == (const Quantum *) NULL) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2800 | break; |
| 2801 | q=jpeg_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2802 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2803 | { |
cristy | 217ddd6 | 2013-12-06 23:27:09 +0000 | [diff] [blame] | 2804 | *q++=(JSAMPLE) (ScaleQuantumToShort(ClampToQuantum(GetPixelLuma(image, |
| 2805 | p)))/scale); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2806 | p+=GetPixelChannels(image); |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2807 | } |
| 2808 | (void) jpeg_write_scanlines(&jpeg_info,scanline,1); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 2809 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 2810 | image->rows); |
| 2811 | if (status == MagickFalse) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2812 | break; |
| 2813 | } |
| 2814 | else |
| 2815 | if ((jpeg_info.in_color_space == JCS_RGB) || |
| 2816 | (jpeg_info.in_color_space == JCS_YCbCr)) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2817 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2818 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2819 | register const Quantum |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2820 | *p; |
| 2821 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2822 | register ssize_t |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2823 | x; |
| 2824 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2825 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2826 | if (p == (const Quantum *) NULL) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2827 | break; |
| 2828 | q=jpeg_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2829 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2830 | { |
cristy | 5328a4c | 2013-12-03 11:32:13 +0000 | [diff] [blame] | 2831 | *q++=(JSAMPLE) (ScaleQuantumToShort(GetPixelRed(image,p))/scale); |
| 2832 | *q++=(JSAMPLE) (ScaleQuantumToShort(GetPixelGreen(image,p))/scale); |
| 2833 | *q++=(JSAMPLE) (ScaleQuantumToShort(GetPixelBlue(image,p))/scale); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2834 | p+=GetPixelChannels(image); |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2835 | } |
| 2836 | (void) jpeg_write_scanlines(&jpeg_info,scanline,1); |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 2837 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 2838 | image->rows); |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2839 | if (status == MagickFalse) |
| 2840 | break; |
| 2841 | } |
| 2842 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2843 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2844 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2845 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2846 | *p; |
| 2847 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2848 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2849 | x; |
| 2850 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2851 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2852 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2853 | break; |
| 2854 | q=jpeg_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2855 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2856 | { |
| 2857 | /* |
| 2858 | Convert DirectClass packets to contiguous CMYK scanlines. |
| 2859 | */ |
cristy | 217ddd6 | 2013-12-06 23:27:09 +0000 | [diff] [blame] | 2860 | *q++=(JSAMPLE) (ScaleQuantumToShort(QuantumRange-GetPixelRed( |
| 2861 | image,p))/scale); |
| 2862 | *q++=(JSAMPLE) (ScaleQuantumToShort(QuantumRange-GetPixelGreen( |
| 2863 | image,p))/scale); |
| 2864 | *q++=(JSAMPLE) (ScaleQuantumToShort(QuantumRange-GetPixelBlue( |
| 2865 | image,p))/scale); |
| 2866 | *q++=(JSAMPLE) (ScaleQuantumToShort(QuantumRange-GetPixelBlack( |
| 2867 | image,p))/scale); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2868 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2869 | } |
| 2870 | (void) jpeg_write_scanlines(&jpeg_info,scanline,1); |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 2871 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 2872 | image->rows); |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 2873 | if (status == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2874 | break; |
| 2875 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2876 | if (y == (ssize_t) image->rows) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2877 | jpeg_finish_compress(&jpeg_info); |
| 2878 | /* |
| 2879 | Relinquish resources. |
| 2880 | */ |
| 2881 | jpeg_destroy_compress(&jpeg_info); |
cristy | 22646e2 | 2013-06-23 16:34:03 +0000 | [diff] [blame] | 2882 | memory_info=RelinquishVirtualMemory(memory_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2883 | (void) CloseBlob(image); |
| 2884 | return(MagickTrue); |
| 2885 | } |
| 2886 | #endif |