cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP SSSSS 33333 % |
| 7 | % P P SS 33 % |
| 8 | % PPPP SSS 333 % |
| 9 | % P SS 33 % |
| 10 | % P SSSSS 33333 % |
| 11 | % % |
| 12 | % % |
| 13 | % Write Postscript Level III Format % |
| 14 | % % |
| 15 | % Software Design % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % Lars Ruben Skyum % |
| 18 | % July 1992 % |
| 19 | % % |
| 20 | % % |
Cristy | 7ce65e7 | 2015-12-12 18:03:16 -0500 | [diff] [blame] | 21 | % Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 22 | % dedicated to making software imaging solutions freely available. % |
| 23 | % % |
| 24 | % You may not use this file except in compliance with the License. You may % |
| 25 | % obtain a copy of the License at % |
| 26 | % % |
| 27 | % http://www.imagemagick.org/script/license.php % |
| 28 | % % |
| 29 | % Unless required by applicable law or agreed to in writing, software % |
| 30 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 31 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 32 | % See the License for the specific language governing permissions and % |
| 33 | % limitations under the License. % |
| 34 | % % |
| 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 36 | % |
| 37 | % |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | Include declarations. |
| 42 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 43 | #include "MagickCore/studio.h" |
cristy | 76ce6e1 | 2013-04-05 14:33:38 +0000 | [diff] [blame] | 44 | #include "MagickCore/artifact.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 45 | #include "MagickCore/attribute.h" |
| 46 | #include "MagickCore/blob.h" |
| 47 | #include "MagickCore/blob-private.h" |
| 48 | #include "MagickCore/cache.h" |
cristy | 500d6b9 | 2012-03-05 19:12:49 +0000 | [diff] [blame] | 49 | #include "MagickCore/channel.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 50 | #include "MagickCore/color.h" |
| 51 | #include "MagickCore/color-private.h" |
| 52 | #include "MagickCore/compress.h" |
| 53 | #include "MagickCore/constitute.h" |
| 54 | #include "MagickCore/draw.h" |
| 55 | #include "MagickCore/exception.h" |
| 56 | #include "MagickCore/exception-private.h" |
| 57 | #include "MagickCore/geometry.h" |
| 58 | #include "MagickCore/image.h" |
| 59 | #include "MagickCore/image-private.h" |
| 60 | #include "MagickCore/list.h" |
| 61 | #include "MagickCore/magick.h" |
| 62 | #include "MagickCore/memory_.h" |
| 63 | #include "MagickCore/monitor.h" |
| 64 | #include "MagickCore/monitor-private.h" |
| 65 | #include "MagickCore/option.h" |
| 66 | #include "MagickCore/pixel-accessor.h" |
| 67 | #include "MagickCore/property.h" |
| 68 | #include "MagickCore/quantum-private.h" |
| 69 | #include "MagickCore/resource_.h" |
| 70 | #include "MagickCore/static.h" |
| 71 | #include "MagickCore/string_.h" |
| 72 | #include "MagickCore/module.h" |
| 73 | #include "MagickCore/token.h" |
| 74 | #include "MagickCore/utility.h" |
| 75 | #include "MagickCore/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | Define declarations. |
| 79 | */ |
| 80 | #define PS3_NoCompression "0" |
| 81 | #define PS3_FaxCompression "1" |
| 82 | #define PS3_JPEGCompression "2" |
| 83 | #define PS3_LZWCompression "3" |
| 84 | #define PS3_RLECompression "4" |
| 85 | #define PS3_ZipCompression "5" |
| 86 | |
| 87 | #define PS3_RGBColorspace "0" |
| 88 | #define PS3_CMYKColorspace "1" |
| 89 | |
| 90 | #define PS3_DirectClass "0" |
| 91 | #define PS3_PseudoClass "1" |
cristy | 8097586 | 2009-09-25 14:34:31 +0000 | [diff] [blame] | 92 | |
| 93 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
cristy | 8097586 | 2009-09-25 14:34:31 +0000 | [diff] [blame] | 94 | #define CCITTParam "-1" |
| 95 | #else |
| 96 | #define CCITTParam "0" |
| 97 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | Forward declarations. |
| 101 | */ |
| 102 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 103 | WritePS3Image(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 107 | % % |
| 108 | % % |
| 109 | % % |
| 110 | % R e g i s t e r P S 3 I m a g e % |
| 111 | % % |
| 112 | % % |
| 113 | % % |
| 114 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 115 | % |
| 116 | % RegisterPS3Image() adds properties for the PS3 image format to the list of |
| 117 | % supported formats. The properties include the image format tag, a method to |
| 118 | % read and/or write the format, whether the format supports the saving of more |
| 119 | % than one frame to the same file or blob, whether the format supports native |
| 120 | % in-memory I/O, and a brief description of the format. |
| 121 | % |
| 122 | % The format of the RegisterPS3Image method is: |
| 123 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 124 | % size_t RegisterPS3Image(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 125 | % |
| 126 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 127 | ModuleExport size_t RegisterPS3Image(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 128 | { |
| 129 | MagickInfo |
| 130 | *entry; |
| 131 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 132 | entry=AcquireMagickInfo("PS3","EPS3","Level III Encapsulated PostScript"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 133 | entry->encoder=(EncodeImageHandler *) WritePS3Image; |
cristy | 5aefbeb | 2013-08-09 12:13:32 +0000 | [diff] [blame] | 134 | entry->mime_type=ConstantString("application/postscript"); |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 135 | entry->flags|=CoderSeekableStreamFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 136 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 137 | entry=AcquireMagickInfo("PS3","PS3","Level III PostScript"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 138 | entry->encoder=(EncodeImageHandler *) WritePS3Image; |
cristy | 5aefbeb | 2013-08-09 12:13:32 +0000 | [diff] [blame] | 139 | entry->mime_type=ConstantString("application/postscript"); |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 140 | entry->flags|=CoderSeekableStreamFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 141 | (void) RegisterMagickInfo(entry); |
| 142 | return(MagickImageCoderSignature); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 147 | % % |
| 148 | % % |
| 149 | % % |
| 150 | % U n r e g i s t e r P S 3 I m a g e % |
| 151 | % % |
| 152 | % % |
| 153 | % % |
| 154 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 155 | % |
| 156 | % UnregisterPS3Image() removes format registrations made by the PS3 module |
| 157 | % from the list of supported formats. |
| 158 | % |
| 159 | % The format of the UnregisterPS3Image method is: |
| 160 | % |
| 161 | % UnregisterPS3Image(void) |
| 162 | % |
| 163 | */ |
| 164 | ModuleExport void UnregisterPS3Image(void) |
| 165 | { |
| 166 | (void) UnregisterMagickInfo("EPS3"); |
| 167 | (void) UnregisterMagickInfo("PS3"); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 172 | % % |
| 173 | % % |
| 174 | % % |
| 175 | % W r i t e P S 3 I m a g e % |
| 176 | % % |
| 177 | % % |
| 178 | % % |
| 179 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 180 | % |
| 181 | % WritePS3Image() translates an image to encapsulated Postscript Level III |
| 182 | % for printing. If the supplied geometry is null, the image is centered on |
| 183 | % the Postscript page. Otherwise, the image is positioned as specified by the |
| 184 | % geometry. |
| 185 | % |
| 186 | % The format of the WritePS3Image method is: |
| 187 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 188 | % MagickBooleanType WritePS3Image(const ImageInfo *image_info, |
| 189 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 190 | % |
| 191 | % A description of each parameter follows: |
| 192 | % |
| 193 | % o image_info: Specifies a pointer to a ImageInfo structure. |
| 194 | % |
| 195 | % o image: the image. |
| 196 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 197 | % o exception: return any errors or warnings in this structure. |
| 198 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 199 | */ |
| 200 | |
cristy | 47b838c | 2009-09-19 16:09:30 +0000 | [diff] [blame] | 201 | static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 202 | Image *image,Image *inject_image,ExceptionInfo *exception) |
cristy | 47b838c | 2009-09-19 16:09:30 +0000 | [diff] [blame] | 203 | { |
cristy | 47b838c | 2009-09-19 16:09:30 +0000 | [diff] [blame] | 204 | Image |
cristy | 8097586 | 2009-09-25 14:34:31 +0000 | [diff] [blame] | 205 | *group4_image; |
cristy | 47b838c | 2009-09-19 16:09:30 +0000 | [diff] [blame] | 206 | |
| 207 | ImageInfo |
| 208 | *write_info; |
| 209 | |
cristy | 47b838c | 2009-09-19 16:09:30 +0000 | [diff] [blame] | 210 | MagickBooleanType |
| 211 | status; |
| 212 | |
cristy | 8097586 | 2009-09-25 14:34:31 +0000 | [diff] [blame] | 213 | size_t |
| 214 | length; |
cristy | 47b838c | 2009-09-19 16:09:30 +0000 | [diff] [blame] | 215 | |
| 216 | unsigned char |
cristy | 8097586 | 2009-09-25 14:34:31 +0000 | [diff] [blame] | 217 | *group4; |
cristy | 47b838c | 2009-09-19 16:09:30 +0000 | [diff] [blame] | 218 | |
cristy | 42751fe | 2009-10-05 00:15:50 +0000 | [diff] [blame] | 219 | status=MagickTrue; |
cristy | 47b838c | 2009-09-19 16:09:30 +0000 | [diff] [blame] | 220 | write_info=CloneImageInfo(image_info); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 221 | (void) CopyMagickString(write_info->filename,"GROUP4:",MagickPathExtent); |
| 222 | (void) CopyMagickString(write_info->magick,"GROUP4",MagickPathExtent); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 223 | group4_image=CloneImage(inject_image,0,0,MagickTrue,exception); |
cristy | 8097586 | 2009-09-25 14:34:31 +0000 | [diff] [blame] | 224 | if (group4_image == (Image *) NULL) |
| 225 | return(MagickFalse); |
| 226 | group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 227 | exception); |
cristy | 8097586 | 2009-09-25 14:34:31 +0000 | [diff] [blame] | 228 | group4_image=DestroyImage(group4_image); |
| 229 | if (group4 == (unsigned char *) NULL) |
| 230 | return(MagickFalse); |
cristy | 47b838c | 2009-09-19 16:09:30 +0000 | [diff] [blame] | 231 | write_info=DestroyImageInfo(write_info); |
cristy | 8097586 | 2009-09-25 14:34:31 +0000 | [diff] [blame] | 232 | if (WriteBlob(image,length,group4) != (ssize_t) length) |
| 233 | status=MagickFalse; |
| 234 | group4=(unsigned char *) RelinquishMagickMemory(group4); |
| 235 | return(status); |
cristy | 47b838c | 2009-09-19 16:09:30 +0000 | [diff] [blame] | 236 | } |
cristy | 8097586 | 2009-09-25 14:34:31 +0000 | [diff] [blame] | 237 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 238 | static MagickBooleanType SerializeImage(const ImageInfo *image_info, |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 239 | Image *image,MemoryInfo **pixel_info,size_t *length,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 240 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 241 | MagickBooleanType |
| 242 | status; |
| 243 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 244 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 245 | *p; |
| 246 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 247 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 248 | x; |
| 249 | |
| 250 | register unsigned char |
| 251 | *q; |
| 252 | |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 253 | ssize_t |
| 254 | y; |
| 255 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 256 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 257 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 258 | if (image->debug != MagickFalse) |
| 259 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 260 | status=MagickTrue; |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 261 | *length=(image->colorspace == CMYKColorspace ? 4 : 3)*(size_t) |
| 262 | image->columns*image->rows; |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 263 | *pixel_info=AcquireVirtualMemory(*length,sizeof(*q)); |
| 264 | if (*pixel_info == (MemoryInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 265 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 266 | q=(unsigned char *) GetVirtualMemoryBlob(*pixel_info); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 267 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 268 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 269 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 270 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 271 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 272 | if (image->colorspace != CMYKColorspace) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 273 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 274 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 275 | *q++=ScaleQuantumToChar(GetPixelRed(image,p)); |
| 276 | *q++=ScaleQuantumToChar(GetPixelGreen(image,p)); |
| 277 | *q++=ScaleQuantumToChar(GetPixelBlue(image,p)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 278 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 279 | } |
| 280 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 281 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 282 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 283 | *q++=ScaleQuantumToChar(GetPixelRed(image,p)); |
| 284 | *q++=ScaleQuantumToChar(GetPixelGreen(image,p)); |
| 285 | *q++=ScaleQuantumToChar(GetPixelBlue(image,p)); |
| 286 | *q++=ScaleQuantumToChar(GetPixelBlack(image,p)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 287 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 288 | } |
| 289 | if (image->previous == (Image *) NULL) |
| 290 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 291 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 292 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 293 | if (status == MagickFalse) |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | if (status == MagickFalse) |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 298 | *pixel_info=RelinquishVirtualMemory(*pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 299 | return(status); |
| 300 | } |
| 301 | |
| 302 | static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info, |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 303 | Image *image,MemoryInfo **pixel_info,size_t *length,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 304 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 305 | MagickBooleanType |
| 306 | status; |
| 307 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 308 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 309 | *p; |
| 310 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 311 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 312 | x; |
| 313 | |
| 314 | register unsigned char |
| 315 | *q; |
| 316 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 317 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 318 | pack, |
| 319 | padded_columns; |
| 320 | |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 321 | ssize_t |
| 322 | y; |
| 323 | |
| 324 | unsigned char |
| 325 | code, |
| 326 | bit; |
| 327 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 328 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 329 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 330 | if (image->debug != MagickFalse) |
| 331 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 332 | status=MagickTrue; |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 333 | pack=SetImageMonochrome(image,exception) == MagickFalse ? 1UL : 8UL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 334 | padded_columns=((image->columns+pack-1)/pack)*pack; |
| 335 | *length=(size_t) padded_columns*image->rows/pack; |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 336 | *pixel_info=AcquireVirtualMemory(*length,sizeof(*q)); |
| 337 | if (*pixel_info == (MemoryInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 338 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 339 | q=(unsigned char *) GetVirtualMemoryBlob(*pixel_info); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 340 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 341 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 342 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 343 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 344 | break; |
| 345 | if (pack == 1) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 346 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 347 | { |
cristy | d032322 | 2013-04-07 16:13:21 +0000 | [diff] [blame] | 348 | *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(image,p))); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 349 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 350 | } |
| 351 | else |
| 352 | { |
| 353 | code='\0'; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 354 | for (x=0; x < (ssize_t) padded_columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 355 | { |
| 356 | bit=(unsigned char) 0x00; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 357 | if (x < (ssize_t) image->columns) |
cristy | d032322 | 2013-04-07 16:13:21 +0000 | [diff] [blame] | 358 | bit=(unsigned char) (GetPixelLuma(image,p) == TransparentAlpha ? |
| 359 | 0x01 : 0x00); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 360 | code=(code << 1)+bit; |
| 361 | if (((x+1) % pack) == 0) |
| 362 | { |
| 363 | *q++=code; |
| 364 | code='\0'; |
| 365 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 366 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 369 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 370 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 371 | if (status == MagickFalse) |
| 372 | break; |
| 373 | } |
| 374 | if (status == MagickFalse) |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 375 | *pixel_info=RelinquishVirtualMemory(*pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 376 | return(status); |
| 377 | } |
| 378 | |
| 379 | static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info, |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 380 | Image *image,MemoryInfo **pixel_info,size_t *length,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 381 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 382 | MagickBooleanType |
| 383 | status; |
| 384 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 385 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 386 | *p; |
| 387 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 388 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 389 | x; |
| 390 | |
| 391 | register unsigned char |
| 392 | *q; |
| 393 | |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 394 | ssize_t |
| 395 | y; |
| 396 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 397 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 398 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 399 | if (image->debug != MagickFalse) |
| 400 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 401 | status=MagickTrue; |
| 402 | *length=(size_t) image->columns*image->rows; |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 403 | *pixel_info=AcquireVirtualMemory(*length,sizeof(*q)); |
| 404 | if (*pixel_info == (MemoryInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 405 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 406 | q=(unsigned char *) GetVirtualMemoryBlob(*pixel_info); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 407 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 408 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 409 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 410 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 411 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 412 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 413 | { |
| 414 | *q++=(unsigned char) GetPixelIndex(image,p); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 415 | p+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 416 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 417 | if (image->previous == (Image *) NULL) |
| 418 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 419 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 420 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 421 | if (status == MagickFalse) |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | if (status == MagickFalse) |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 426 | *pixel_info=RelinquishVirtualMemory(*pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 427 | return(status); |
| 428 | } |
| 429 | |
| 430 | static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 431 | Image *image,const CompressionType compression,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 432 | { |
| 433 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 434 | buffer[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 435 | |
| 436 | Image |
| 437 | *mask_image; |
| 438 | |
| 439 | MagickBooleanType |
| 440 | status; |
| 441 | |
| 442 | MagickOffsetType |
| 443 | offset, |
| 444 | start, |
| 445 | stop; |
| 446 | |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 447 | MemoryInfo |
| 448 | *pixel_info; |
| 449 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 450 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 451 | i; |
| 452 | |
| 453 | size_t |
| 454 | length; |
| 455 | |
| 456 | unsigned char |
| 457 | *pixels; |
| 458 | |
| 459 | assert(image_info != (ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 460 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 461 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 462 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 463 | if (image->debug != MagickFalse) |
| 464 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 465 | assert(image->alpha_trait != UndefinedPixelTrait); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 466 | status=MagickTrue; |
| 467 | /* |
| 468 | Note BeginData DSC comment for update later. |
| 469 | */ |
| 470 | start=TellBlob(image); |
cristy | ca5add7 | 2015-04-04 23:58:14 +0000 | [diff] [blame] | 471 | if (start < 0) |
| 472 | ThrowWriterException(CorruptImageError,"ImproperImageHeader"); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 473 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 474 | "%%%%BeginData:%13ld %s Bytes\n",0L,compression == NoCompression ? |
| 475 | "ASCII" : "BINARY"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 476 | (void) WriteBlobString(image,buffer); |
| 477 | stop=TellBlob(image); |
cristy | ca5add7 | 2015-04-04 23:58:14 +0000 | [diff] [blame] | 478 | if (stop < 0) |
| 479 | ThrowWriterException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 480 | /* |
| 481 | Only lossless compressions for the mask. |
| 482 | */ |
| 483 | switch (compression) |
| 484 | { |
| 485 | case NoCompression: |
| 486 | default: |
| 487 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 488 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 489 | "currentfile %.20g %.20g "PS3_NoCompression" ByteStreamDecodeFilter\n", |
| 490 | (double) image->columns,(double) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 491 | break; |
| 492 | } |
| 493 | case FaxCompression: |
| 494 | case Group4Compression: |
| 495 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 496 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 497 | "currentfile %.20g %.20g "PS3_FaxCompression" ByteStreamDecodeFilter\n", |
| 498 | (double) image->columns,(double) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 499 | break; |
| 500 | } |
| 501 | case LZWCompression: |
| 502 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 503 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 504 | "currentfile %.20g %.20g "PS3_LZWCompression" ByteStreamDecodeFilter\n", |
| 505 | (double) image->columns,(double) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 506 | break; |
| 507 | } |
| 508 | case RLECompression: |
| 509 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 510 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 511 | "currentfile %.20g %.20g "PS3_RLECompression" ByteStreamDecodeFilter\n", |
| 512 | (double) image->columns,(double) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 513 | break; |
| 514 | } |
| 515 | case ZipCompression: |
| 516 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 517 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 518 | "currentfile %.20g %.20g "PS3_ZipCompression" ByteStreamDecodeFilter\n", |
| 519 | (double) image->columns,(double) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 520 | break; |
| 521 | } |
| 522 | } |
| 523 | (void) WriteBlobString(image,buffer); |
| 524 | (void) WriteBlobString(image,"/ReusableStreamDecode filter\n"); |
cristy | dbeca02 | 2012-01-11 17:07:34 +0000 | [diff] [blame] | 525 | mask_image=SeparateImage(image,AlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 526 | if (mask_image == (Image *) NULL) |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 527 | ThrowWriterException(CoderError,exception->reason); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 528 | (void) SetImageType(mask_image,BilevelType,exception); |
| 529 | (void) SetImageType(mask_image,PaletteType,exception); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 530 | mask_image->alpha_trait=UndefinedPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 531 | pixels=(unsigned char *) NULL; |
| 532 | length=0; |
| 533 | switch (compression) |
| 534 | { |
| 535 | case NoCompression: |
| 536 | default: |
| 537 | { |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 538 | status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 539 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 540 | if (status == MagickFalse) |
| 541 | break; |
| 542 | Ascii85Initialize(image); |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 543 | pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 544 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 545 | Ascii85Encode(image,pixels[i]); |
| 546 | Ascii85Flush(image); |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 547 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 548 | break; |
| 549 | } |
| 550 | case FaxCompression: |
| 551 | case Group4Compression: |
| 552 | { |
| 553 | if ((compression == FaxCompression) || |
| 554 | (LocaleCompare(CCITTParam,"0") == 0)) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 555 | status=HuffmanEncodeImage(image_info,image,mask_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 556 | else |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 557 | status=Huffman2DEncodeImage(image_info,image,mask_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 558 | break; |
| 559 | } |
| 560 | case LZWCompression: |
| 561 | { |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 562 | status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 563 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 564 | if (status == MagickFalse) |
| 565 | break; |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 566 | pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 567 | status=LZWEncodeImage(image,length,pixels,exception); |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 568 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 569 | break; |
| 570 | } |
| 571 | case RLECompression: |
| 572 | { |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 573 | status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 574 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 575 | if (status == MagickFalse) |
| 576 | break; |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 577 | pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 578 | status=PackbitsEncodeImage(image,length,pixels,exception); |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 579 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 580 | break; |
| 581 | } |
| 582 | case ZipCompression: |
| 583 | { |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 584 | status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 585 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 586 | if (status == MagickFalse) |
| 587 | break; |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 588 | pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 589 | status=ZLIBEncodeImage(image,length,pixels,exception); |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 590 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | } |
| 594 | mask_image=DestroyImage(mask_image); |
| 595 | (void) WriteBlobByte(image,'\n'); |
| 596 | length=(size_t) (TellBlob(image)-stop); |
| 597 | stop=TellBlob(image); |
cristy | ca5add7 | 2015-04-04 23:58:14 +0000 | [diff] [blame] | 598 | if (stop < 0) |
| 599 | ThrowWriterException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 600 | offset=SeekBlob(image,start,SEEK_SET); |
| 601 | if (offset < 0) |
| 602 | ThrowWriterException(CorruptImageError,"ImproperImageHeader"); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 603 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 604 | "%%%%BeginData:%13ld %s Bytes\n",(long) length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 605 | compression == NoCompression ? "ASCII" : "BINARY"); |
| 606 | (void) WriteBlobString(image,buffer); |
| 607 | offset=SeekBlob(image,stop,SEEK_SET); |
| 608 | if (offset < 0) |
| 609 | ThrowWriterException(CorruptImageError,"ImproperImageHeader"); |
| 610 | (void) WriteBlobString(image,"%%EndData\n"); |
| 611 | (void) WriteBlobString(image, "/mask_stream exch def\n"); |
| 612 | return(status); |
| 613 | } |
| 614 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 615 | static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image, |
| 616 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 617 | { |
| 618 | static const char |
Cristy | 629f805 | 2015-09-14 07:48:08 -0400 | [diff] [blame] | 619 | *const PostscriptProlog[]= |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 620 | { |
| 621 | "/ByteStreamDecodeFilter", |
| 622 | "{", |
| 623 | " /z exch def", |
| 624 | " /r exch def", |
| 625 | " /c exch def", |
| 626 | " z "PS3_NoCompression" eq { /ASCII85Decode filter } if", |
| 627 | " z "PS3_FaxCompression" eq", |
| 628 | " {", |
| 629 | " <<", |
| 630 | " /K "CCITTParam, |
| 631 | " /Columns c", |
| 632 | " /Rows r", |
| 633 | " >>", |
| 634 | " /CCITTFaxDecode filter", |
| 635 | " } if", |
| 636 | " z "PS3_JPEGCompression" eq { /DCTDecode filter } if", |
| 637 | " z "PS3_LZWCompression" eq { /LZWDecode filter } if", |
| 638 | " z "PS3_RLECompression" eq { /RunLengthDecode filter } if", |
| 639 | " z "PS3_ZipCompression" eq { /FlateDecode filter } if", |
| 640 | "} bind def", |
| 641 | "", |
| 642 | "/DirectClassImageDict", |
| 643 | "{", |
| 644 | " colorspace "PS3_RGBColorspace" eq", |
| 645 | " {", |
| 646 | " /DeviceRGB setcolorspace", |
| 647 | " <<", |
| 648 | " /ImageType 1", |
| 649 | " /Width columns", |
| 650 | " /Height rows", |
| 651 | " /BitsPerComponent 8", |
| 652 | " /DataSource pixel_stream", |
| 653 | " /MultipleDataSources false", |
| 654 | " /ImageMatrix [columns 0 0 rows neg 0 rows]", |
| 655 | " /Decode [0 1 0 1 0 1]", |
| 656 | " >>", |
| 657 | " }", |
| 658 | " {", |
| 659 | " /DeviceCMYK setcolorspace", |
| 660 | " <<", |
| 661 | " /ImageType 1", |
| 662 | " /Width columns", |
| 663 | " /Height rows", |
| 664 | " /BitsPerComponent 8", |
| 665 | " /DataSource pixel_stream", |
| 666 | " /MultipleDataSources false", |
| 667 | " /ImageMatrix [columns 0 0 rows neg 0 rows]", |
| 668 | " /Decode", |
| 669 | " compression "PS3_JPEGCompression" eq", |
| 670 | " { [1 0 1 0 1 0 1 0] }", |
| 671 | " { [0 1 0 1 0 1 0 1] }", |
| 672 | " ifelse", |
| 673 | " >>", |
| 674 | " }", |
| 675 | " ifelse", |
| 676 | "} bind def", |
| 677 | "", |
| 678 | "/PseudoClassImageDict", |
| 679 | "{", |
| 680 | " % Colors in colormap image.", |
| 681 | " currentfile buffer readline pop", |
| 682 | " token pop /colors exch def pop", |
| 683 | " colors 0 eq", |
| 684 | " {", |
| 685 | " % Depth of grayscale image.", |
| 686 | " currentfile buffer readline pop", |
| 687 | " token pop /bits exch def pop", |
| 688 | " /DeviceGray setcolorspace", |
| 689 | " <<", |
| 690 | " /ImageType 1", |
| 691 | " /Width columns", |
| 692 | " /Height rows", |
| 693 | " /BitsPerComponent bits", |
| 694 | " /Decode [0 1]", |
| 695 | " /ImageMatrix [columns 0 0 rows neg 0 rows]", |
| 696 | " /DataSource pixel_stream", |
| 697 | " >>", |
| 698 | " }", |
| 699 | " {", |
| 700 | " % RGB colormap.", |
| 701 | " /colormap colors 3 mul string def", |
| 702 | " compression "PS3_NoCompression" eq", |
| 703 | " { currentfile /ASCII85Decode filter colormap readstring pop pop }", |
| 704 | " { currentfile colormap readstring pop pop }", |
| 705 | " ifelse", |
| 706 | " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace", |
| 707 | " <<", |
| 708 | " /ImageType 1", |
| 709 | " /Width columns", |
| 710 | " /Height rows", |
| 711 | " /BitsPerComponent 8", |
| 712 | " /Decode [0 255]", |
| 713 | " /ImageMatrix [columns 0 0 rows neg 0 rows]", |
| 714 | " /DataSource pixel_stream", |
| 715 | " >>", |
| 716 | " }", |
| 717 | " ifelse", |
| 718 | "} bind def", |
| 719 | "", |
| 720 | "/NonMaskedImageDict", |
| 721 | "{", |
| 722 | " class "PS3_PseudoClass" eq", |
| 723 | " { PseudoClassImageDict }", |
| 724 | " { DirectClassImageDict }", |
| 725 | " ifelse", |
| 726 | "} bind def", |
| 727 | "", |
| 728 | "/MaskedImageDict", |
| 729 | "{", |
| 730 | " <<", |
| 731 | " /ImageType 3", |
| 732 | " /InterleaveType 3", |
| 733 | " /DataDict NonMaskedImageDict", |
| 734 | " /MaskDict", |
| 735 | " <<", |
| 736 | " /ImageType 1", |
| 737 | " /Width columns", |
| 738 | " /Height rows", |
| 739 | " /BitsPerComponent 1", |
| 740 | " /DataSource mask_stream", |
| 741 | " /MultipleDataSources false", |
| 742 | " /ImageMatrix [ columns 0 0 rows neg 0 rows]", |
| 743 | " /Decode [ 0 1 ]", |
| 744 | " >>", |
| 745 | " >>", |
| 746 | "} bind def", |
| 747 | "", |
| 748 | "/ClipImage", |
| 749 | "{} def", |
| 750 | "", |
| 751 | "/DisplayImage", |
| 752 | "{", |
cristy | 2cde86a | 2012-07-28 23:19:42 +0000 | [diff] [blame] | 753 | " gsave", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 754 | " /buffer 512 string def", |
| 755 | " % Translation.", |
| 756 | " currentfile buffer readline pop", |
| 757 | " token pop /x exch def", |
| 758 | " token pop /y exch def pop", |
| 759 | " x y translate", |
| 760 | " % Image size and font size.", |
| 761 | " currentfile buffer readline pop", |
| 762 | " token pop /x exch def", |
| 763 | " token pop /y exch def pop", |
| 764 | " currentfile buffer readline pop", |
| 765 | " token pop /pointsize exch def pop", |
Cristy | 629f805 | 2015-09-14 07:48:08 -0400 | [diff] [blame] | 766 | (const char *) NULL |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 767 | }, |
Cristy | 629f805 | 2015-09-14 07:48:08 -0400 | [diff] [blame] | 768 | *const PostscriptEpilog[]= |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 769 | { |
| 770 | " x y scale", |
| 771 | " % Clipping path.", |
| 772 | " currentfile buffer readline pop", |
| 773 | " token pop /clipped exch def pop", |
| 774 | " % Showpage.", |
| 775 | " currentfile buffer readline pop", |
| 776 | " token pop /sp exch def pop", |
| 777 | " % Image pixel size.", |
| 778 | " currentfile buffer readline pop", |
| 779 | " token pop /columns exch def", |
| 780 | " token pop /rows exch def pop", |
| 781 | " % Colorspace (RGB/CMYK).", |
| 782 | " currentfile buffer readline pop", |
| 783 | " token pop /colorspace exch def pop", |
| 784 | " % Transparency.", |
| 785 | " currentfile buffer readline pop", |
| 786 | " token pop /alpha exch def pop", |
| 787 | " % Stencil mask?", |
| 788 | " currentfile buffer readline pop", |
| 789 | " token pop /stencil exch def pop", |
| 790 | " % Image class (direct/pseudo).", |
| 791 | " currentfile buffer readline pop", |
| 792 | " token pop /class exch def pop", |
| 793 | " % Compression type.", |
| 794 | " currentfile buffer readline pop", |
| 795 | " token pop /compression exch def pop", |
| 796 | " % Clip and render.", |
| 797 | " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def", |
| 798 | " clipped { ClipImage } if", |
| 799 | " alpha stencil not and", |
| 800 | " { MaskedImageDict mask_stream resetfile }", |
| 801 | " { NonMaskedImageDict }", |
| 802 | " ifelse", |
| 803 | " stencil { 0 setgray imagemask } { image } ifelse", |
cristy | 2cde86a | 2012-07-28 23:19:42 +0000 | [diff] [blame] | 804 | " grestore", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 805 | " sp { showpage } if", |
| 806 | "} bind def", |
Cristy | 629f805 | 2015-09-14 07:48:08 -0400 | [diff] [blame] | 807 | (const char *) NULL |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 808 | }; |
| 809 | |
| 810 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 811 | buffer[MagickPathExtent], |
| 812 | date[MagickPathExtent], |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 813 | **labels, |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 814 | page_geometry[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 815 | |
| 816 | CompressionType |
| 817 | compression; |
| 818 | |
| 819 | const char |
| 820 | *option, |
Cristy | 629f805 | 2015-09-14 07:48:08 -0400 | [diff] [blame] | 821 | *const *q, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 822 | *value; |
| 823 | |
| 824 | double |
| 825 | pointsize; |
| 826 | |
| 827 | GeometryInfo |
| 828 | geometry_info; |
| 829 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 830 | MagickBooleanType |
| 831 | status; |
| 832 | |
| 833 | MagickOffsetType |
| 834 | offset, |
| 835 | scene, |
| 836 | start, |
| 837 | stop; |
| 838 | |
| 839 | MagickStatusType |
| 840 | flags; |
| 841 | |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 842 | MemoryInfo |
| 843 | *pixel_info; |
| 844 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 845 | PointInfo |
| 846 | delta, |
| 847 | resolution, |
| 848 | scale; |
| 849 | |
| 850 | RectangleInfo |
| 851 | geometry, |
| 852 | media_info, |
| 853 | page_info; |
| 854 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 855 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 856 | i; |
| 857 | |
| 858 | SegmentInfo |
| 859 | bounds; |
| 860 | |
| 861 | size_t |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 862 | length, |
| 863 | page, |
| 864 | pixel, |
| 865 | text_size; |
| 866 | |
| 867 | ssize_t |
| 868 | j; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 869 | |
| 870 | time_t |
| 871 | timer; |
| 872 | |
| 873 | unsigned char |
| 874 | *pixels; |
| 875 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 876 | /* |
| 877 | Open output image file. |
| 878 | */ |
| 879 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 880 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 881 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 882 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 883 | if (image->debug != MagickFalse) |
| 884 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 885 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 886 | assert(exception->signature == MagickCoreSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 887 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 888 | if (status == MagickFalse) |
| 889 | return(MagickFalse); |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 890 | compression=image->compression; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 891 | if (image_info->compression != UndefinedCompression) |
| 892 | compression=image_info->compression; |
| 893 | switch (compression) |
| 894 | { |
| 895 | case FaxCompression: |
| 896 | case Group4Compression: |
| 897 | { |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 898 | if ((SetImageMonochrome(image,exception) == MagickFalse) || |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 899 | (image->alpha_trait != UndefinedPixelTrait)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 900 | compression=RLECompression; |
| 901 | break; |
| 902 | } |
| 903 | #if !defined(MAGICKCORE_JPEG_DELEGATE) |
| 904 | case JPEGCompression: |
| 905 | { |
| 906 | compression=RLECompression; |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 907 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 908 | MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)", |
| 909 | image->filename); |
| 910 | break; |
| 911 | } |
| 912 | #endif |
| 913 | #if !defined(MAGICKCORE_ZLIB_DELEGATE) |
| 914 | case ZipCompression: |
| 915 | { |
| 916 | compression=RLECompression; |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 917 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 918 | MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)", |
| 919 | image->filename); |
| 920 | break; |
| 921 | } |
| 922 | #endif |
| 923 | default: |
| 924 | break; |
| 925 | } |
| 926 | (void) ResetMagickMemory(&bounds,0,sizeof(bounds)); |
| 927 | page=0; |
| 928 | scene=0; |
| 929 | do |
| 930 | { |
| 931 | /* |
| 932 | Scale relative to dots-per-inch. |
| 933 | */ |
| 934 | delta.x=DefaultResolution; |
| 935 | delta.y=DefaultResolution; |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 936 | resolution.x=image->resolution.x; |
| 937 | resolution.y=image->resolution.y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 938 | if ((resolution.x == 0.0) || (resolution.y == 0.0)) |
| 939 | { |
| 940 | flags=ParseGeometry(PSDensityGeometry,&geometry_info); |
| 941 | resolution.x=geometry_info.rho; |
| 942 | resolution.y=geometry_info.sigma; |
| 943 | if ((flags & SigmaValue) == 0) |
| 944 | resolution.y=resolution.x; |
| 945 | } |
| 946 | if (image_info->density != (char *) NULL) |
| 947 | { |
| 948 | flags=ParseGeometry(image_info->density,&geometry_info); |
| 949 | resolution.x=geometry_info.rho; |
| 950 | resolution.y=geometry_info.sigma; |
| 951 | if ((flags & SigmaValue) == 0) |
| 952 | resolution.y=resolution.x; |
| 953 | } |
| 954 | if (image->units == PixelsPerCentimeterResolution) |
| 955 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 956 | resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0; |
| 957 | resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 958 | } |
| 959 | SetGeometry(image,&geometry); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 960 | (void) FormatLocaleString(page_geometry,MagickPathExtent,"%.20gx%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 961 | (double) image->columns,(double) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 962 | if (image_info->page != (char *) NULL) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 963 | (void) CopyMagickString(page_geometry,image_info->page,MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 964 | else |
| 965 | if ((image->page.width != 0) && (image->page.height != 0)) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 966 | (void) FormatLocaleString(page_geometry,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 967 | "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double) |
| 968 | image->page.height,(double) image->page.x,(double) image->page.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 969 | else |
| 970 | if ((image->gravity != UndefinedGravity) && |
| 971 | (LocaleCompare(image_info->magick,"PS") == 0)) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 972 | (void) CopyMagickString(page_geometry,PSPageGeometry,MagickPathExtent); |
| 973 | (void) ConcatenateMagickString(page_geometry,">",MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 974 | (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y, |
| 975 | &geometry.width,&geometry.height); |
| 976 | scale.x=(double) (geometry.width*delta.x)/resolution.x; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 977 | geometry.width=(size_t) floor(scale.x+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 978 | scale.y=(double) (geometry.height*delta.y)/resolution.y; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 979 | geometry.height=(size_t) floor(scale.y+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 980 | (void) ParseAbsoluteGeometry(page_geometry,&media_info); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 981 | (void) ParseGravityGeometry(image,page_geometry,&page_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 982 | if (image->gravity != UndefinedGravity) |
| 983 | { |
| 984 | geometry.x=(-page_info.x); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 985 | geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 986 | } |
| 987 | pointsize=12.0; |
| 988 | if (image_info->pointsize != 0.0) |
| 989 | pointsize=image_info->pointsize; |
| 990 | text_size=0; |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 991 | value=GetImageProperty(image,"label",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 992 | if (value != (const char *) NULL) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 993 | text_size=(size_t) (MultilineCensus(value)*pointsize+12); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 994 | page++; |
| 995 | if (page == 1) |
| 996 | { |
| 997 | /* |
| 998 | Postscript header on the first page. |
| 999 | */ |
| 1000 | if (LocaleCompare(image_info->magick,"PS3") == 0) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1001 | (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1002 | else |
| 1003 | (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n", |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1004 | MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1005 | (void) WriteBlobString(image,buffer); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1006 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1007 | "%%%%Creator: ImageMagick %s\n",MagickLibVersionText); |
| 1008 | (void) WriteBlobString(image,buffer); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1009 | (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Title: %s\n", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1010 | image->filename); |
| 1011 | (void) WriteBlobString(image,buffer); |
| 1012 | timer=time((time_t *) NULL); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1013 | (void) FormatMagickTime(timer,MagickPathExtent,date); |
| 1014 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1015 | "%%%%CreationDate: %s\n",date); |
| 1016 | (void) WriteBlobString(image,buffer); |
| 1017 | bounds.x1=(double) geometry.x; |
| 1018 | bounds.y1=(double) geometry.y; |
| 1019 | bounds.x2=(double) geometry.x+scale.x; |
| 1020 | bounds.y2=(double) geometry.y+scale.y+text_size; |
| 1021 | if ((image_info->adjoin != MagickFalse) && |
| 1022 | (GetNextImageInList(image) != (Image *) NULL)) |
| 1023 | { |
| 1024 | (void) WriteBlobString(image,"%%BoundingBox: (atend)\n"); |
| 1025 | (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n"); |
| 1026 | } |
| 1027 | else |
| 1028 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1029 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e4cc1a1 | 2012-09-03 23:04:46 +0000 | [diff] [blame] | 1030 | "%%%%BoundingBox: %g %g %g %g\n",ceil(bounds.x1-0.5), |
cristy | 8071c47 | 2012-09-24 12:41:06 +0000 | [diff] [blame] | 1031 | ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1032 | (void) WriteBlobString(image,buffer); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1033 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 1034 | "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1, |
cristy | 8cd5b31 | 2010-01-07 01:10:24 +0000 | [diff] [blame] | 1035 | bounds.y1,bounds.x2,bounds.y2); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1036 | (void) WriteBlobString(image,buffer); |
| 1037 | if (image->colorspace == CMYKColorspace) |
| 1038 | (void) WriteBlobString(image, |
| 1039 | "%%DocumentProcessColors: Cyan Magenta Yellow Black\n"); |
| 1040 | else |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 1041 | if (SetImageGray(image,exception) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1042 | (void) WriteBlobString(image, |
| 1043 | "%%DocumentProcessColors: Black\n"); |
| 1044 | } |
| 1045 | /* |
| 1046 | Font resources |
| 1047 | */ |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1048 | value=GetImageProperty(image,"label",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1049 | if (value != (const char *) NULL) |
| 1050 | (void) WriteBlobString(image, |
| 1051 | "%%DocumentNeededResources: font Helvetica\n"); |
| 1052 | (void) WriteBlobString(image,"%%LanguageLevel: 3\n"); |
| 1053 | /* |
| 1054 | Pages, orientation and order. |
| 1055 | */ |
| 1056 | if (LocaleCompare(image_info->magick,"PS3") != 0) |
| 1057 | (void) WriteBlobString(image,"%%Pages: 1\n"); |
| 1058 | else |
| 1059 | { |
| 1060 | (void) WriteBlobString(image,"%%Orientation: Portrait\n"); |
| 1061 | (void) WriteBlobString(image,"%%PageOrder: Ascend\n"); |
| 1062 | if (image_info->adjoin == MagickFalse) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1063 | (void) CopyMagickString(buffer,"%%Pages: 1\n",MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1064 | else |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1065 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1066 | "%%%%Pages: %.20g\n",(double) GetImageListLength(image)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1067 | (void) WriteBlobString(image,buffer); |
| 1068 | } |
cristy | a5079b3 | 2014-01-29 21:07:10 +0000 | [diff] [blame] | 1069 | if (image->colorspace == CMYKColorspace) |
| 1070 | (void) WriteBlobString(image, |
| 1071 | "%%DocumentProcessColors: Cyan Magenta Yellow Black\n"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1072 | (void) WriteBlobString(image,"%%EndComments\n"); |
| 1073 | /* |
| 1074 | The static postscript procedures prolog. |
| 1075 | */ |
| 1076 | (void)WriteBlobString(image,"%%BeginProlog\n"); |
| 1077 | for (q=PostscriptProlog; *q; q++) |
| 1078 | { |
| 1079 | (void) WriteBlobString(image,*q); |
| 1080 | (void) WriteBlobByte(image,'\n'); |
| 1081 | } |
| 1082 | /* |
| 1083 | One label line for each line in label string. |
| 1084 | */ |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1085 | value=GetImageProperty(image,"label",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1086 | if (value != (const char *) NULL) |
| 1087 | { |
| 1088 | (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica " |
| 1089 | " findfont pointsize scalefont setfont\n"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1090 | for (i=(ssize_t) MultilineCensus(value)-1; i >= 0; i--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1091 | { |
| 1092 | (void) WriteBlobString(image, |
| 1093 | " currentfile buffer readline pop token pop\n"); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1094 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 1095 | " 0 y %g add moveto show pop\n",i*pointsize+12); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1096 | (void) WriteBlobString(image,buffer); |
| 1097 | } |
| 1098 | } |
| 1099 | /* |
| 1100 | The static postscript procedures epilog. |
| 1101 | */ |
| 1102 | for (q=PostscriptEpilog; *q; q++) |
| 1103 | { |
| 1104 | (void) WriteBlobString(image,*q); |
| 1105 | (void) WriteBlobByte(image,'\n'); |
| 1106 | } |
| 1107 | (void)WriteBlobString(image,"%%EndProlog\n"); |
| 1108 | } |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1109 | (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Page: 1 %.20g\n", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1110 | (double) page); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1111 | (void) WriteBlobString(image,buffer); |
| 1112 | /* |
| 1113 | Page bounding box. |
| 1114 | */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1115 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1116 | "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x, |
| 1117 | (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+ |
| 1118 | (double) (geometry.height+text_size)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1119 | (void) WriteBlobString(image,buffer); |
| 1120 | /* |
| 1121 | Page process colors if not RGB. |
| 1122 | */ |
| 1123 | if (image->colorspace == CMYKColorspace) |
| 1124 | (void) WriteBlobString(image, |
| 1125 | "%%PageProcessColors: Cyan Magenta Yellow Black\n"); |
| 1126 | else |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 1127 | if (SetImageGray(image,exception) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1128 | (void) WriteBlobString(image,"%%PageProcessColors: Black\n"); |
| 1129 | /* |
| 1130 | Adjust document bounding box to bound page bounding box. |
| 1131 | */ |
| 1132 | if ((double) geometry.x < bounds.x1) |
| 1133 | bounds.x1=(double) geometry.x; |
| 1134 | if ((double) geometry.y < bounds.y1) |
| 1135 | bounds.y1=(double) geometry.y; |
| 1136 | if ((double) (geometry.x+scale.x) > bounds.x2) |
| 1137 | bounds.x2=(double) geometry.x+scale.x; |
| 1138 | if ((double) (geometry.y+scale.y+text_size) > bounds.y2) |
| 1139 | bounds.y2=(double) geometry.y+scale.y+text_size; |
| 1140 | /* |
| 1141 | Page font resource if there's a label. |
| 1142 | */ |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1143 | value=GetImageProperty(image,"label",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1144 | if (value != (const char *) NULL) |
| 1145 | (void) WriteBlobString(image,"%%PageResources: font Helvetica\n"); |
| 1146 | /* |
| 1147 | PS clipping path from Photoshop clipping path. |
| 1148 | */ |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1149 | if ((image->read_mask != MagickFalse) || |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1150 | (LocaleNCompare("8BIM:",image->magick_filename,5) != 0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1151 | (void) WriteBlobString(image,"/ClipImage {} def\n"); |
| 1152 | else |
| 1153 | { |
| 1154 | const char |
| 1155 | *value; |
| 1156 | |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1157 | value=GetImageProperty(image,image->magick_filename,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1158 | if (value == (const char *) NULL) |
| 1159 | return(MagickFalse); |
| 1160 | (void) WriteBlobString(image,value); |
| 1161 | (void) WriteBlobByte(image,'\n'); |
| 1162 | } |
| 1163 | /* |
| 1164 | Push a dictionary for our own def's if this an EPS. |
| 1165 | */ |
| 1166 | if (LocaleCompare(image_info->magick,"PS3") != 0) |
| 1167 | (void) WriteBlobString(image,"userdict begin\n"); |
| 1168 | /* |
| 1169 | Image mask. |
| 1170 | */ |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 1171 | if ((image->alpha_trait != UndefinedPixelTrait) && |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1172 | (WritePS3MaskImage(image_info,image,compression,exception) == MagickFalse)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1173 | { |
| 1174 | (void) CloseBlob(image); |
| 1175 | return(MagickFalse); |
| 1176 | } |
| 1177 | /* |
| 1178 | Remember position of BeginData comment so we can update it. |
| 1179 | */ |
| 1180 | start=TellBlob(image); |
cristy | ca5add7 | 2015-04-04 23:58:14 +0000 | [diff] [blame] | 1181 | if (start < 0) |
| 1182 | ThrowWriterException(CorruptImageError,"ImproperImageHeader"); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1183 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1184 | "%%%%BeginData:%13ld %s Bytes\n",0L, |
| 1185 | compression == NoCompression ? "ASCII" : "BINARY"); |
| 1186 | (void) WriteBlobString(image,buffer); |
| 1187 | stop=TellBlob(image); |
cristy | ca5add7 | 2015-04-04 23:58:14 +0000 | [diff] [blame] | 1188 | if (stop < 0) |
| 1189 | ThrowWriterException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1190 | (void) WriteBlobString(image,"DisplayImage\n"); |
| 1191 | /* |
| 1192 | Translate, scale, and font point size. |
| 1193 | */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1194 | (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n%g %g\n%g\n", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1195 | (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1196 | (void) WriteBlobString(image,buffer); |
| 1197 | /* |
| 1198 | Output labels. |
| 1199 | */ |
| 1200 | labels=(char **) NULL; |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1201 | value=GetImageProperty(image,"label",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1202 | if (value != (const char *) NULL) |
| 1203 | labels=StringToList(value); |
| 1204 | if (labels != (char **) NULL) |
| 1205 | { |
| 1206 | for (i=0; labels[i] != (char *) NULL; i++) |
| 1207 | { |
| 1208 | if (compression != NoCompression) |
| 1209 | { |
| 1210 | for (j=0; labels[i][j] != '\0'; j++) |
| 1211 | (void) WriteBlobByte(image,(unsigned char) labels[i][j]); |
| 1212 | (void) WriteBlobByte(image,'\n'); |
| 1213 | } |
| 1214 | else |
| 1215 | { |
| 1216 | (void) WriteBlobString(image,"<~"); |
| 1217 | Ascii85Initialize(image); |
| 1218 | for (j=0; labels[i][j] != '\0'; j++) |
| 1219 | Ascii85Encode(image,(unsigned char) labels[i][j]); |
| 1220 | Ascii85Flush(image); |
| 1221 | } |
| 1222 | labels[i]=DestroyString(labels[i]); |
| 1223 | } |
| 1224 | labels=(char **) RelinquishMagickMemory(labels); |
| 1225 | } |
| 1226 | /* |
| 1227 | Photoshop clipping path active? |
| 1228 | */ |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1229 | if ((image->read_mask != MagickFalse) && |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1230 | (LocaleNCompare("8BIM:",image->magick_filename,5) == 0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1231 | (void) WriteBlobString(image,"true\n"); |
| 1232 | else |
| 1233 | (void) WriteBlobString(image,"false\n"); |
| 1234 | /* |
| 1235 | Showpage for non-EPS. |
| 1236 | */ |
| 1237 | (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ? |
| 1238 | "true\n" : "false\n"); |
| 1239 | /* |
| 1240 | Image columns, rows, and color space. |
| 1241 | */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1242 | (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n%s\n", |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1243 | (double) image->columns,(double) image->rows,image->colorspace == |
| 1244 | CMYKColorspace ? PS3_CMYKColorspace : PS3_RGBColorspace); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1245 | (void) WriteBlobString(image,buffer); |
| 1246 | /* |
| 1247 | Masked image? |
| 1248 | */ |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 1249 | (void) WriteBlobString(image,image->alpha_trait != UndefinedPixelTrait ? |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1250 | "true\n" : "false\n"); |
| 1251 | /* |
| 1252 | Render with imagemask operator? |
| 1253 | */ |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1254 | option=GetImageOption(image_info,"ps3:imagemask"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1255 | (void) WriteBlobString(image,((option != (const char *) NULL) && |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 1256 | (SetImageMonochrome(image,exception) != MagickFalse)) ? |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1257 | "true\n" : "false\n"); |
| 1258 | /* |
| 1259 | Output pixel data. |
| 1260 | */ |
| 1261 | pixels=(unsigned char *) NULL; |
| 1262 | length=0; |
| 1263 | if ((image_info->type != TrueColorType) && |
cristy | def23e5 | 2015-01-22 11:52:01 +0000 | [diff] [blame] | 1264 | (image_info->type != TrueColorAlphaType) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1265 | (image_info->type != ColorSeparationType) && |
cristy | def23e5 | 2015-01-22 11:52:01 +0000 | [diff] [blame] | 1266 | (image_info->type != ColorSeparationAlphaType) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1267 | (image->colorspace != CMYKColorspace) && |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 1268 | ((SetImageGray(image,exception) != MagickFalse) || |
| 1269 | (SetImageMonochrome(image,exception) != MagickFalse))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1270 | { |
| 1271 | /* |
| 1272 | Gray images. |
| 1273 | */ |
| 1274 | (void) WriteBlobString(image,PS3_PseudoClass"\n"); |
| 1275 | switch (compression) |
| 1276 | { |
| 1277 | case NoCompression: |
| 1278 | default: |
| 1279 | { |
| 1280 | (void) WriteBlobString(image,PS3_NoCompression"\n"); |
| 1281 | break; |
| 1282 | } |
| 1283 | case FaxCompression: |
| 1284 | case Group4Compression: |
| 1285 | { |
| 1286 | (void) WriteBlobString(image,PS3_FaxCompression"\n"); |
| 1287 | break; |
| 1288 | } |
| 1289 | case JPEGCompression: |
| 1290 | { |
| 1291 | (void) WriteBlobString(image,PS3_JPEGCompression"\n"); |
| 1292 | break; |
| 1293 | } |
| 1294 | case LZWCompression: |
| 1295 | { |
| 1296 | (void) WriteBlobString(image,PS3_LZWCompression"\n"); |
| 1297 | break; |
| 1298 | } |
| 1299 | case RLECompression: |
| 1300 | { |
| 1301 | (void) WriteBlobString(image,PS3_RLECompression"\n"); |
| 1302 | break; |
| 1303 | } |
| 1304 | case ZipCompression: |
| 1305 | { |
| 1306 | (void) WriteBlobString(image,PS3_ZipCompression"\n"); |
| 1307 | break; |
| 1308 | } |
| 1309 | } |
| 1310 | /* |
| 1311 | Number of colors -- 0 for single component non-color mapped data. |
| 1312 | */ |
| 1313 | (void) WriteBlobString(image,"0\n"); |
| 1314 | /* |
| 1315 | 1 bit or 8 bit components? |
| 1316 | */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1317 | (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n", |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 1318 | SetImageMonochrome(image,exception) != MagickFalse ? 1 : 8); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1319 | (void) WriteBlobString(image,buffer); |
| 1320 | /* |
| 1321 | Image data. |
| 1322 | */ |
| 1323 | if (compression == JPEGCompression) |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1324 | status=InjectImageBlob(image_info,image,image,"jpeg",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1325 | else |
| 1326 | if ((compression == FaxCompression) || |
| 1327 | (compression == Group4Compression)) |
| 1328 | { |
| 1329 | if (LocaleCompare(CCITTParam,"0") == 0) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1330 | status=HuffmanEncodeImage(image_info,image,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1331 | else |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1332 | status=Huffman2DEncodeImage(image_info,image,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1333 | } |
| 1334 | else |
| 1335 | { |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 1336 | status=SerializeImageChannel(image_info,image,&pixel_info,&length, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1337 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1338 | if (status == MagickFalse) |
| 1339 | { |
| 1340 | (void) CloseBlob(image); |
| 1341 | return(MagickFalse); |
| 1342 | } |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 1343 | pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1344 | switch (compression) |
| 1345 | { |
| 1346 | case NoCompression: |
| 1347 | default: |
| 1348 | { |
| 1349 | Ascii85Initialize(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1350 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1351 | Ascii85Encode(image,pixels[i]); |
| 1352 | Ascii85Flush(image); |
| 1353 | status=MagickTrue; |
| 1354 | break; |
| 1355 | } |
| 1356 | case LZWCompression: |
| 1357 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1358 | status=LZWEncodeImage(image,length,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1359 | break; |
| 1360 | } |
| 1361 | case RLECompression: |
| 1362 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1363 | status=PackbitsEncodeImage(image,length,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1364 | break; |
| 1365 | } |
| 1366 | case ZipCompression: |
| 1367 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1368 | status=ZLIBEncodeImage(image,length,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1369 | break; |
| 1370 | } |
| 1371 | } |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 1372 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | else |
| 1376 | if ((image->storage_class == DirectClass) || (image->colors > 256) || |
| 1377 | (compression == JPEGCompression)) |
| 1378 | { |
| 1379 | /* |
| 1380 | Truecolor image. |
| 1381 | */ |
| 1382 | (void) WriteBlobString(image,PS3_DirectClass"\n"); |
| 1383 | switch (compression) |
| 1384 | { |
| 1385 | case NoCompression: |
| 1386 | default: |
| 1387 | { |
| 1388 | (void) WriteBlobString(image,PS3_NoCompression"\n"); |
| 1389 | break; |
| 1390 | } |
| 1391 | case RLECompression: |
| 1392 | { |
| 1393 | (void) WriteBlobString(image,PS3_RLECompression"\n"); |
| 1394 | break; |
| 1395 | } |
| 1396 | case JPEGCompression: |
| 1397 | { |
| 1398 | (void) WriteBlobString(image,PS3_JPEGCompression"\n"); |
| 1399 | break; |
| 1400 | } |
| 1401 | case LZWCompression: |
| 1402 | { |
| 1403 | (void) WriteBlobString(image,PS3_LZWCompression"\n"); |
| 1404 | break; |
| 1405 | } |
| 1406 | case ZipCompression: |
| 1407 | { |
| 1408 | (void) WriteBlobString(image,PS3_ZipCompression"\n"); |
| 1409 | break; |
| 1410 | } |
| 1411 | } |
| 1412 | /* |
| 1413 | Image data. |
| 1414 | */ |
| 1415 | if (compression == JPEGCompression) |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1416 | status=InjectImageBlob(image_info,image,image,"jpeg",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1417 | else |
| 1418 | { |
| 1419 | /* |
| 1420 | Stream based compressions. |
| 1421 | */ |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 1422 | status=SerializeImage(image_info,image,&pixel_info,&length, |
| 1423 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1424 | if (status == MagickFalse) |
| 1425 | { |
| 1426 | (void) CloseBlob(image); |
| 1427 | return(MagickFalse); |
| 1428 | } |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 1429 | pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1430 | switch (compression) |
| 1431 | { |
| 1432 | case NoCompression: |
| 1433 | default: |
| 1434 | { |
| 1435 | Ascii85Initialize(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1436 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1437 | Ascii85Encode(image,pixels[i]); |
| 1438 | Ascii85Flush(image); |
| 1439 | status=MagickTrue; |
| 1440 | break; |
| 1441 | } |
| 1442 | case RLECompression: |
| 1443 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1444 | status=PackbitsEncodeImage(image,length,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1445 | break; |
| 1446 | } |
| 1447 | case LZWCompression: |
| 1448 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1449 | status=LZWEncodeImage(image,length,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1450 | break; |
| 1451 | } |
| 1452 | case ZipCompression: |
| 1453 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1454 | status=ZLIBEncodeImage(image,length,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1455 | break; |
| 1456 | } |
| 1457 | } |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 1458 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | else |
| 1462 | { |
| 1463 | /* |
| 1464 | Colormapped images. |
| 1465 | */ |
| 1466 | (void) WriteBlobString(image,PS3_PseudoClass"\n"); |
| 1467 | switch (compression) |
| 1468 | { |
| 1469 | case NoCompression: |
| 1470 | default: |
| 1471 | { |
| 1472 | (void) WriteBlobString(image,PS3_NoCompression"\n"); |
| 1473 | break; |
| 1474 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1475 | case RLECompression: |
| 1476 | { |
| 1477 | (void) WriteBlobString(image,PS3_RLECompression"\n"); |
| 1478 | break; |
| 1479 | } |
| 1480 | case LZWCompression: |
| 1481 | { |
| 1482 | (void) WriteBlobString(image,PS3_LZWCompression"\n"); |
| 1483 | break; |
| 1484 | } |
| 1485 | case ZipCompression: |
| 1486 | { |
| 1487 | (void) WriteBlobString(image,PS3_ZipCompression"\n"); |
| 1488 | break; |
| 1489 | } |
| 1490 | } |
| 1491 | /* |
| 1492 | Number of colors in color map. |
| 1493 | */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1494 | (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1495 | (double) image->colors); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1496 | (void) WriteBlobString(image,buffer); |
| 1497 | /* |
| 1498 | Color map - uncompressed. |
| 1499 | */ |
| 1500 | if ((compression != NoCompression) && |
| 1501 | (compression != UndefinedCompression)) |
| 1502 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1503 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1504 | { |
| 1505 | pixel=ScaleQuantumToChar(image->colormap[i].red); |
| 1506 | (void) WriteBlobByte(image,(unsigned char) pixel); |
| 1507 | pixel=ScaleQuantumToChar(image->colormap[i].green); |
| 1508 | (void) WriteBlobByte(image,(unsigned char) pixel); |
| 1509 | pixel=ScaleQuantumToChar(image->colormap[i].blue); |
| 1510 | (void) WriteBlobByte(image,(unsigned char) pixel); |
| 1511 | } |
| 1512 | } |
| 1513 | else |
| 1514 | { |
| 1515 | Ascii85Initialize(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1516 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1517 | { |
| 1518 | pixel=ScaleQuantumToChar(image->colormap[i].red); |
| 1519 | Ascii85Encode(image,(unsigned char) pixel); |
| 1520 | pixel=ScaleQuantumToChar(image->colormap[i].green); |
| 1521 | Ascii85Encode(image,(unsigned char) pixel); |
| 1522 | pixel=ScaleQuantumToChar(image->colormap[i].blue); |
| 1523 | Ascii85Encode(image,(unsigned char) pixel); |
| 1524 | } |
| 1525 | Ascii85Flush(image); |
| 1526 | } |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 1527 | status=SerializeImageIndexes(image_info,image,&pixel_info,&length, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1528 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1529 | if (status == MagickFalse) |
| 1530 | { |
| 1531 | (void) CloseBlob(image); |
| 1532 | return(MagickFalse); |
| 1533 | } |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 1534 | pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1535 | switch (compression) |
| 1536 | { |
| 1537 | case NoCompression: |
| 1538 | default: |
| 1539 | { |
| 1540 | Ascii85Initialize(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1541 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1542 | Ascii85Encode(image,pixels[i]); |
| 1543 | Ascii85Flush(image); |
| 1544 | status=MagickTrue; |
| 1545 | break; |
| 1546 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1547 | case RLECompression: |
| 1548 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1549 | status=PackbitsEncodeImage(image,length,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1550 | break; |
| 1551 | } |
| 1552 | case LZWCompression: |
| 1553 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1554 | status=LZWEncodeImage(image,length,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1555 | break; |
| 1556 | } |
| 1557 | case ZipCompression: |
| 1558 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1559 | status=ZLIBEncodeImage(image,length,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1560 | break; |
| 1561 | } |
| 1562 | } |
cristy | b64823d | 2013-06-30 20:58:24 +0000 | [diff] [blame] | 1563 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1564 | } |
| 1565 | (void) WriteBlobByte(image,'\n'); |
| 1566 | if (status == MagickFalse) |
| 1567 | { |
| 1568 | (void) CloseBlob(image); |
| 1569 | return(MagickFalse); |
| 1570 | } |
| 1571 | /* |
| 1572 | Update BeginData now that we know the data size. |
| 1573 | */ |
| 1574 | length=(size_t) (TellBlob(image)-stop); |
| 1575 | stop=TellBlob(image); |
cristy | ca5add7 | 2015-04-04 23:58:14 +0000 | [diff] [blame] | 1576 | if (stop < 0) |
| 1577 | ThrowWriterException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1578 | offset=SeekBlob(image,start,SEEK_SET); |
| 1579 | if (offset < 0) |
| 1580 | ThrowWriterException(CorruptImageError,"ImproperImageHeader"); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1581 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 1582 | "%%%%BeginData:%13ld %s Bytes\n",(long) length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1583 | compression == NoCompression ? "ASCII" : "BINARY"); |
| 1584 | (void) WriteBlobString(image,buffer); |
| 1585 | offset=SeekBlob(image,stop,SEEK_SET); |
| 1586 | (void) WriteBlobString(image,"%%EndData\n"); |
| 1587 | /* |
| 1588 | End private dictionary if this an EPS. |
| 1589 | */ |
| 1590 | if (LocaleCompare(image_info->magick,"PS3") != 0) |
| 1591 | (void) WriteBlobString(image,"end\n"); |
| 1592 | (void) WriteBlobString(image,"%%PageTrailer\n"); |
| 1593 | if (GetNextImageInList(image) == (Image *) NULL) |
| 1594 | break; |
| 1595 | image=SyncNextImageInList(image); |
| 1596 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 1597 | GetImageListLength(image)); |
| 1598 | if (status == MagickFalse) |
| 1599 | break; |
| 1600 | } while (image_info->adjoin != MagickFalse); |
| 1601 | (void) WriteBlobString(image,"%%Trailer\n"); |
| 1602 | if (page > 1) |
| 1603 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1604 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e4cc1a1 | 2012-09-03 23:04:46 +0000 | [diff] [blame] | 1605 | "%%%%BoundingBox: %g %g %g %g\n",ceil(bounds.x1-0.5), |
cristy | 8071c47 | 2012-09-24 12:41:06 +0000 | [diff] [blame] | 1606 | ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1607 | (void) WriteBlobString(image,buffer); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1608 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e4cc1a1 | 2012-09-03 23:04:46 +0000 | [diff] [blame] | 1609 | "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,bounds.x2, |
| 1610 | bounds.y2); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1611 | (void) WriteBlobString(image,buffer); |
| 1612 | } |
| 1613 | (void) WriteBlobString(image,"%%EOF\n"); |
| 1614 | (void) CloseBlob(image); |
| 1615 | return(MagickTrue); |
| 1616 | } |