| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | #include "magick/studio.h" |
|---|
| 43 | #include "magick/blob.h" |
|---|
| 44 | #include "magick/blob-private.h" |
|---|
| 45 | #include "magick/color.h" |
|---|
| 46 | #include "magick/constitute.h" |
|---|
| 47 | #include "magick/draw.h" |
|---|
| 48 | #include "magick/exception.h" |
|---|
| 49 | #include "magick/exception-private.h" |
|---|
| 50 | #include "magick/delegate.h" |
|---|
| 51 | #include "magick/geometry.h" |
|---|
| 52 | #include "magick/histogram.h" |
|---|
| 53 | #include "magick/image.h" |
|---|
| 54 | #include "magick/image-private.h" |
|---|
| 55 | #include "magick/list.h" |
|---|
| 56 | #include "magick/magick.h" |
|---|
| 57 | #include "magick/memory_.h" |
|---|
| 58 | #include "magick/monitor.h" |
|---|
| 59 | #include "magick/monitor-private.h" |
|---|
| 60 | #include "magick/quantize.h" |
|---|
| 61 | #include "magick/resource_.h" |
|---|
| 62 | #include "magick/quantum-private.h" |
|---|
| 63 | #include "magick/static.h" |
|---|
| 64 | #include "magick/string_.h" |
|---|
| 65 | #include "magick/module.h" |
|---|
| 66 | #include "magick/transform.h" |
|---|
| 67 | #include "magick/utility.h" |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | typedef struct _EPTInfo |
|---|
| 73 | { |
|---|
| 74 | unsigned long |
|---|
| 75 | magick; |
|---|
| 76 | |
|---|
| 77 | MagickOffsetType |
|---|
| 78 | postscript_offset, |
|---|
| 79 | tiff_offset; |
|---|
| 80 | |
|---|
| 81 | size_t |
|---|
| 82 | postscript_length, |
|---|
| 83 | tiff_length; |
|---|
| 84 | |
|---|
| 85 | unsigned char |
|---|
| 86 | *postscript, |
|---|
| 87 | *tiff; |
|---|
| 88 | } EPTInfo; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | static MagickBooleanType |
|---|
| 94 | WriteEPTImage(const ImageInfo *,Image *); |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | static MagickBooleanType IsEPT(const unsigned char *magick,const size_t length) |
|---|
| 122 | { |
|---|
| 123 | if (length < 4) |
|---|
| 124 | return(MagickFalse); |
|---|
| 125 | if (memcmp(magick,"\305\320\323\306",4) == 0) |
|---|
| 126 | return(MagickTrue); |
|---|
| 127 | return(MagickFalse); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | static Image *ReadEPTImage(const ImageInfo *image_info,ExceptionInfo *exception) |
|---|
| 158 | { |
|---|
| 159 | EPTInfo |
|---|
| 160 | ept_info; |
|---|
| 161 | |
|---|
| 162 | Image |
|---|
| 163 | *image; |
|---|
| 164 | |
|---|
| 165 | ImageInfo |
|---|
| 166 | *read_info; |
|---|
| 167 | |
|---|
| 168 | ssize_t |
|---|
| 169 | count; |
|---|
| 170 | |
|---|
| 171 | MagickBooleanType |
|---|
| 172 | status; |
|---|
| 173 | |
|---|
| 174 | MagickOffsetType |
|---|
| 175 | offset; |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | assert(image_info != (const ImageInfo *) NULL); |
|---|
| 181 | assert(image_info->signature == MagickSignature); |
|---|
| 182 | if (image_info->debug != MagickFalse) |
|---|
| 183 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
|---|
| 184 | image_info->filename); |
|---|
| 185 | assert(exception != (ExceptionInfo *) NULL); |
|---|
| 186 | assert(exception->signature == MagickSignature); |
|---|
| 187 | image=AcquireImage(image_info); |
|---|
| 188 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
|---|
| 189 | if (status == MagickFalse) |
|---|
| 190 | { |
|---|
| 191 | image=DestroyImageList(image); |
|---|
| 192 | return((Image *) NULL); |
|---|
| 193 | } |
|---|
| 194 | ept_info.magick=ReadBlobLSBLong(image); |
|---|
| 195 | if (ept_info.magick != 0xc6d3d0c5ul) |
|---|
| 196 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
|---|
| 197 | ept_info.postscript_offset=(MagickOffsetType) ReadBlobLSBLong(image); |
|---|
| 198 | ept_info.postscript_length=ReadBlobLSBLong(image); |
|---|
| 199 | (void) ReadBlobLSBLong(image); |
|---|
| 200 | (void) ReadBlobLSBLong(image); |
|---|
| 201 | ept_info.tiff_offset=(MagickOffsetType) ReadBlobLSBLong(image); |
|---|
| 202 | ept_info.tiff_length=ReadBlobLSBLong(image); |
|---|
| 203 | (void) ReadBlobLSBShort(image); |
|---|
| 204 | ept_info.postscript=(unsigned char *) AcquireQuantumMemory( |
|---|
| 205 | ept_info.postscript_length,sizeof(*ept_info.postscript)); |
|---|
| 206 | if (ept_info.postscript == (unsigned char *) NULL) |
|---|
| 207 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
|---|
| 208 | ept_info.tiff=(unsigned char *) AcquireQuantumMemory(ept_info.tiff_length, |
|---|
| 209 | sizeof(*ept_info.tiff)); |
|---|
| 210 | if ((ept_info.tiff_length != 0) && (ept_info.tiff == (unsigned char *) NULL)) |
|---|
| 211 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
|---|
| 212 | offset=SeekBlob(image,ept_info.tiff_offset,SEEK_SET); |
|---|
| 213 | if (offset < 0) |
|---|
| 214 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
|---|
| 215 | count=ReadBlob(image,ept_info.tiff_length,ept_info.tiff); |
|---|
| 216 | if (count != (ssize_t) (ept_info.tiff_length)) |
|---|
| 217 | ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile"); |
|---|
| 218 | offset=SeekBlob(image,ept_info.postscript_offset,SEEK_SET); |
|---|
| 219 | if (offset < 0) |
|---|
| 220 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
|---|
| 221 | count=ReadBlob(image,ept_info.postscript_length,ept_info.postscript); |
|---|
| 222 | if (count != (ssize_t) (ept_info.postscript_length)) |
|---|
| 223 | ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile"); |
|---|
| 224 | (void) CloseBlob(image); |
|---|
| 225 | image=DestroyImage(image); |
|---|
| 226 | read_info=CloneImageInfo(image_info); |
|---|
| 227 | (void) CopyMagickString(read_info->magick,"EPS",MaxTextExtent); |
|---|
| 228 | image=BlobToImage(read_info,ept_info.postscript,ept_info.postscript_length, |
|---|
| 229 | exception); |
|---|
| 230 | if (image == (Image *) NULL) |
|---|
| 231 | { |
|---|
| 232 | (void) CopyMagickString(read_info->magick,"TIFF",MaxTextExtent); |
|---|
| 233 | image=BlobToImage(read_info,ept_info.tiff,ept_info.tiff_length,exception); |
|---|
| 234 | } |
|---|
| 235 | read_info=DestroyImageInfo(read_info); |
|---|
| 236 | if (image != (Image *) NULL) |
|---|
| 237 | (void) CopyMagickString(image->filename,image_info->filename,MaxTextExtent); |
|---|
| 238 | ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff); |
|---|
| 239 | ept_info.postscript=(unsigned char *) RelinquishMagickMemory( |
|---|
| 240 | ept_info.postscript); |
|---|
| 241 | return(image); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | ModuleExport unsigned long RegisterEPTImage(void) |
|---|
| 268 | { |
|---|
| 269 | MagickInfo |
|---|
| 270 | *entry; |
|---|
| 271 | |
|---|
| 272 | entry=SetMagickInfo("EPT"); |
|---|
| 273 | entry->decoder=(DecodeImageHandler *) ReadEPTImage; |
|---|
| 274 | entry->encoder=(EncodeImageHandler *) WriteEPTImage; |
|---|
| 275 | entry->magick=(IsImageFormatHandler *) IsEPT; |
|---|
| 276 | entry->adjoin=MagickFalse; |
|---|
| 277 | entry->blob_support=MagickFalse; |
|---|
| 278 | entry->description=ConstantString( |
|---|
| 279 | "Encapsulated PostScript with TIFF preview"); |
|---|
| 280 | entry->module=ConstantString("EPT"); |
|---|
| 281 | (void) RegisterMagickInfo(entry); |
|---|
| 282 | entry=SetMagickInfo("EPT2"); |
|---|
| 283 | entry->decoder=(DecodeImageHandler *) ReadEPTImage; |
|---|
| 284 | entry->encoder=(EncodeImageHandler *) WriteEPTImage; |
|---|
| 285 | entry->magick=(IsImageFormatHandler *) IsEPT; |
|---|
| 286 | entry->adjoin=MagickFalse; |
|---|
| 287 | entry->blob_support=MagickFalse; |
|---|
| 288 | entry->description=ConstantString( |
|---|
| 289 | "Encapsulated PostScript Level II with TIFF preview"); |
|---|
| 290 | entry->module=ConstantString("EPT"); |
|---|
| 291 | (void) RegisterMagickInfo(entry); |
|---|
| 292 | entry=SetMagickInfo("EPT3"); |
|---|
| 293 | entry->decoder=(DecodeImageHandler *) ReadEPTImage; |
|---|
| 294 | entry->encoder=(EncodeImageHandler *) WriteEPTImage; |
|---|
| 295 | entry->magick=(IsImageFormatHandler *) IsEPT; |
|---|
| 296 | entry->blob_support=MagickFalse; |
|---|
| 297 | entry->description=ConstantString( |
|---|
| 298 | "Encapsulated PostScript Level III with TIFF preview"); |
|---|
| 299 | entry->module=ConstantString("EPT"); |
|---|
| 300 | (void) RegisterMagickInfo(entry); |
|---|
| 301 | return(MagickImageCoderSignature); |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | ModuleExport void UnregisterEPTImage(void) |
|---|
| 324 | { |
|---|
| 325 | (void) UnregisterMagickInfo("EPT"); |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image) |
|---|
| 354 | { |
|---|
| 355 | char |
|---|
| 356 | filename[MaxTextExtent]; |
|---|
| 357 | |
|---|
| 358 | EPTInfo |
|---|
| 359 | ept_info; |
|---|
| 360 | |
|---|
| 361 | Image |
|---|
| 362 | *write_image; |
|---|
| 363 | |
|---|
| 364 | ImageInfo |
|---|
| 365 | *write_info; |
|---|
| 366 | |
|---|
| 367 | MagickBooleanType |
|---|
| 368 | status; |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | assert(image_info != (const ImageInfo *) NULL); |
|---|
| 374 | assert(image_info->signature == MagickSignature); |
|---|
| 375 | assert(image != (Image *) NULL); |
|---|
| 376 | assert(image->signature == MagickSignature); |
|---|
| 377 | if (image->debug != MagickFalse) |
|---|
| 378 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
|---|
| 379 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); |
|---|
| 380 | if (status == MagickFalse) |
|---|
| 381 | return(status); |
|---|
| 382 | write_image=CloneImage(image,0,0,MagickTrue,&image->exception); |
|---|
| 383 | if (write_image == (Image *) NULL) |
|---|
| 384 | return(MagickFalse); |
|---|
| 385 | write_info=CloneImageInfo(image_info); |
|---|
| 386 | (void) CopyMagickString(write_info->magick,"EPS",MaxTextExtent); |
|---|
| 387 | if (LocaleCompare(image_info->magick,"EPT2") == 0) |
|---|
| 388 | (void) CopyMagickString(write_info->magick,"EPS2",MaxTextExtent); |
|---|
| 389 | if (LocaleCompare(image_info->magick,"EPT3") == 0) |
|---|
| 390 | (void) CopyMagickString(write_info->magick,"EPS3",MaxTextExtent); |
|---|
| 391 | (void) ResetMagickMemory(&ept_info,0,sizeof(ept_info)); |
|---|
| 392 | ept_info.magick=0xc6d3d0c5ul; |
|---|
| 393 | ept_info.postscript=(unsigned char *) ImageToBlob(write_info,write_image, |
|---|
| 394 | &ept_info.postscript_length,&image->exception); |
|---|
| 395 | write_image=DestroyImage(write_image); |
|---|
| 396 | write_info=DestroyImageInfo(write_info); |
|---|
| 397 | if (ept_info.postscript == (void *) NULL) |
|---|
| 398 | return(MagickFalse); |
|---|
| 399 | write_image=CloneImage(image,0,0,MagickTrue,&image->exception); |
|---|
| 400 | if (write_image == (Image *) NULL) |
|---|
| 401 | return(MagickFalse); |
|---|
| 402 | write_info=CloneImageInfo(image_info); |
|---|
| 403 | (void) CopyMagickString(write_info->magick,"TIFF",MaxTextExtent); |
|---|
| 404 | (void) FormatMagickString(filename,MaxTextExtent,"tiff:%.1024s", |
|---|
| 405 | write_info->filename); |
|---|
| 406 | (void) CopyMagickString(write_info->filename,filename,MaxTextExtent); |
|---|
| 407 | (void) TransformImage(&write_image,(char *) NULL,"512x512>"); |
|---|
| 408 | if ((write_image->storage_class == DirectClass) || |
|---|
| 409 | (write_image->colors > 256)) |
|---|
| 410 | { |
|---|
| 411 | QuantizeInfo |
|---|
| 412 | quantize_info; |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | GetQuantizeInfo(&quantize_info); |
|---|
| 418 | quantize_info.dither=IsPaletteImage(write_image,&image->exception) == |
|---|
| 419 | MagickFalse ? MagickTrue : MagickFalse; |
|---|
| 420 | (void) QuantizeImage(&quantize_info,write_image); |
|---|
| 421 | } |
|---|
| 422 | write_info->compression=NoCompression; |
|---|
| 423 | ept_info.tiff=(unsigned char *) ImageToBlob(write_info,write_image, |
|---|
| 424 | &ept_info.tiff_length,&image->exception); |
|---|
| 425 | write_image=DestroyImage(write_image); |
|---|
| 426 | write_info=DestroyImageInfo(write_info); |
|---|
| 427 | if (ept_info.tiff == (void *) NULL) |
|---|
| 428 | { |
|---|
| 429 | ept_info.postscript=(unsigned char *) RelinquishMagickMemory( |
|---|
| 430 | ept_info.postscript); |
|---|
| 431 | return(MagickFalse); |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | (void) WriteBlobLSBLong(image,(unsigned int) ept_info.magick); |
|---|
| 437 | (void) WriteBlobLSBLong(image,30); |
|---|
| 438 | (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length); |
|---|
| 439 | (void) WriteBlobLSBLong(image,0); |
|---|
| 440 | (void) WriteBlobLSBLong(image,0); |
|---|
| 441 | (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length+30); |
|---|
| 442 | (void) WriteBlobLSBLong(image,(unsigned int) ept_info.tiff_length); |
|---|
| 443 | (void) WriteBlobLSBShort(image,0xffff); |
|---|
| 444 | (void) WriteBlob(image,ept_info.postscript_length,ept_info.postscript); |
|---|
| 445 | (void) WriteBlob(image,ept_info.tiff_length,ept_info.tiff); |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | ept_info.postscript=(unsigned char *) RelinquishMagickMemory( |
|---|
| 450 | ept_info.postscript); |
|---|
| 451 | ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff); |
|---|
| 452 | (void) CloseBlob(image); |
|---|
| 453 | return(MagickTrue); |
|---|
| 454 | } |
|---|