cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % M M AAA PPPP % |
| 7 | % MM MM A A P P % |
| 8 | % M M M AAAAA PPPP % |
| 9 | % M M A A P % |
| 10 | % M M A A P % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Image Colormaps As An Image File. % |
| 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 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 20 | % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
| 43 | #include "MagickCore/attribute.h" |
| 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
| 46 | #include "MagickCore/cache.h" |
| 47 | #include "MagickCore/color.h" |
| 48 | #include "MagickCore/color-private.h" |
| 49 | #include "MagickCore/colormap.h" |
| 50 | #include "MagickCore/colormap-private.h" |
| 51 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 52 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 53 | #include "MagickCore/exception.h" |
| 54 | #include "MagickCore/exception-private.h" |
| 55 | #include "MagickCore/histogram.h" |
| 56 | #include "MagickCore/image.h" |
| 57 | #include "MagickCore/image-private.h" |
| 58 | #include "MagickCore/list.h" |
| 59 | #include "MagickCore/magick.h" |
| 60 | #include "MagickCore/memory_.h" |
| 61 | #include "MagickCore/pixel-accessor.h" |
| 62 | #include "MagickCore/quantum-private.h" |
| 63 | #include "MagickCore/static.h" |
| 64 | #include "MagickCore/statistic.h" |
| 65 | #include "MagickCore/string_.h" |
| 66 | #include "MagickCore/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | Forward declarations. |
| 70 | */ |
| 71 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 72 | WriteMAPImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 76 | % % |
| 77 | % % |
| 78 | % % |
| 79 | % R e a d M A P I m a g e % |
| 80 | % % |
| 81 | % % |
| 82 | % % |
| 83 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 84 | % |
| 85 | % ReadMAPImage() reads an image of raw RGB colormap and colormap index |
| 86 | % bytes and returns it. It allocates the memory necessary for the new Image |
| 87 | % structure and returns a pointer to the new image. |
| 88 | % |
| 89 | % The format of the ReadMAPImage method is: |
| 90 | % |
| 91 | % Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 92 | % |
| 93 | % A description of each parameter follows: |
| 94 | % |
| 95 | % o image_info: the image info. |
| 96 | % |
| 97 | % o exception: return any errors or warnings in this structure. |
| 98 | % |
| 99 | */ |
| 100 | static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 101 | { |
| 102 | Image |
| 103 | *image; |
| 104 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 105 | MagickBooleanType |
| 106 | status; |
| 107 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 108 | Quantum |
| 109 | index; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 110 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 111 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 112 | x; |
| 113 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 114 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 115 | *q; |
| 116 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 117 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 118 | i; |
| 119 | |
| 120 | register unsigned char |
| 121 | *p; |
| 122 | |
| 123 | size_t |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 124 | depth, |
| 125 | packet_size, |
| 126 | quantum; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 127 | |
| 128 | ssize_t |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 129 | count, |
| 130 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 131 | |
| 132 | unsigned char |
| 133 | *colormap, |
| 134 | *pixels; |
| 135 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 136 | /* |
| 137 | Open image file. |
| 138 | */ |
| 139 | assert(image_info != (const ImageInfo *) NULL); |
| 140 | assert(image_info->signature == MagickSignature); |
| 141 | if (image_info->debug != MagickFalse) |
| 142 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 143 | image_info->filename); |
| 144 | assert(exception != (ExceptionInfo *) NULL); |
| 145 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 146 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 147 | if ((image->columns == 0) || (image->rows == 0)) |
| 148 | ThrowReaderException(OptionError,"MustSpecifyImageSize"); |
| 149 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 150 | if (status == MagickFalse) |
| 151 | { |
| 152 | image=DestroyImageList(image); |
| 153 | return((Image *) NULL); |
| 154 | } |
| 155 | /* |
| 156 | Initialize image structure. |
| 157 | */ |
| 158 | image->storage_class=PseudoClass; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 159 | status=AcquireImageColormap(image,(size_t) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 160 | (image->offset != 0 ? image->offset : 256),exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 161 | if (status == MagickFalse) |
| 162 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 163 | depth=GetImageQuantumDepth(image,MagickTrue); |
| 164 | packet_size=(size_t) (depth/8); |
| 165 | pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size* |
| 166 | sizeof(*pixels)); |
| 167 | packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL); |
| 168 | colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size* |
| 169 | sizeof(*colormap)); |
| 170 | if ((pixels == (unsigned char *) NULL) || |
| 171 | (colormap == (unsigned char *) NULL)) |
| 172 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 173 | /* |
| 174 | Read image colormap. |
| 175 | */ |
| 176 | count=ReadBlob(image,packet_size*image->colors,colormap); |
| 177 | if (count != (ssize_t) (packet_size*image->colors)) |
| 178 | ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile"); |
| 179 | p=colormap; |
| 180 | if (image->depth <= 8) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 181 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 182 | { |
| 183 | image->colormap[i].red=ScaleCharToQuantum(*p++); |
| 184 | image->colormap[i].green=ScaleCharToQuantum(*p++); |
| 185 | image->colormap[i].blue=ScaleCharToQuantum(*p++); |
| 186 | } |
| 187 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 188 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 189 | { |
| 190 | quantum=(*p++ << 8); |
| 191 | quantum|=(*p++); |
| 192 | image->colormap[i].red=(Quantum) quantum; |
| 193 | quantum=(*p++ << 8); |
| 194 | quantum|=(*p++); |
| 195 | image->colormap[i].green=(Quantum) quantum; |
| 196 | quantum=(*p++ << 8); |
| 197 | quantum|=(*p++); |
| 198 | image->colormap[i].blue=(Quantum) quantum; |
| 199 | } |
| 200 | colormap=(unsigned char *) RelinquishMagickMemory(colormap); |
| 201 | if (image_info->ping != MagickFalse) |
| 202 | { |
| 203 | (void) CloseBlob(image); |
| 204 | return(GetFirstImageInList(image)); |
| 205 | } |
| 206 | /* |
| 207 | Read image pixels. |
| 208 | */ |
| 209 | packet_size=(size_t) (depth/8); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 210 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 211 | { |
| 212 | p=pixels; |
| 213 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 214 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 215 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 216 | count=ReadBlob(image,(size_t) packet_size*image->columns,pixels); |
| 217 | if (count != (ssize_t) (packet_size*image->columns)) |
| 218 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 219 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 220 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 221 | index=ConstrainColormapIndex(image,*p,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 222 | p++; |
| 223 | if (image->colors > 256) |
| 224 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 225 | index=ConstrainColormapIndex(image,((size_t) index << 8)+(*p), |
| 226 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 227 | p++; |
| 228 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 229 | SetPixelIndex(image,index,q); |
cristy | 803640d | 2011-11-17 02:11:32 +0000 | [diff] [blame] | 230 | SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 231 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 232 | } |
| 233 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 234 | break; |
| 235 | } |
| 236 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 237 | if (y < (ssize_t) image->rows) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 238 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 239 | image->filename); |
| 240 | (void) CloseBlob(image); |
| 241 | return(GetFirstImageInList(image)); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 246 | % % |
| 247 | % % |
| 248 | % % |
| 249 | % R e g i s t e r M A P I m a g e % |
| 250 | % % |
| 251 | % % |
| 252 | % % |
| 253 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 254 | % |
| 255 | % RegisterMAPImage() adds attributes for the MAP image format to |
| 256 | % the list of supported formats. The attributes include the image format |
| 257 | % tag, a method to read and/or write the format, whether the format |
| 258 | % supports the saving of more than one frame to the same file or blob, |
| 259 | % whether the format supports native in-memory I/O, and a brief |
| 260 | % description of the format. |
| 261 | % |
| 262 | % The format of the RegisterMAPImage method is: |
| 263 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 264 | % size_t RegisterMAPImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 265 | % |
| 266 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 267 | ModuleExport size_t RegisterMAPImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 268 | { |
| 269 | MagickInfo |
| 270 | *entry; |
| 271 | |
| 272 | entry=SetMagickInfo("MAP"); |
| 273 | entry->decoder=(DecodeImageHandler *) ReadMAPImage; |
| 274 | entry->encoder=(EncodeImageHandler *) WriteMAPImage; |
| 275 | entry->adjoin=MagickFalse; |
| 276 | entry->format_type=ExplicitFormatType; |
| 277 | entry->raw=MagickTrue; |
| 278 | entry->endian_support=MagickTrue; |
| 279 | entry->description=ConstantString("Colormap intensities and indices"); |
| 280 | entry->module=ConstantString("MAP"); |
| 281 | (void) RegisterMagickInfo(entry); |
| 282 | return(MagickImageCoderSignature); |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 287 | % % |
| 288 | % % |
| 289 | % % |
| 290 | % U n r e g i s t e r M A P I m a g e % |
| 291 | % % |
| 292 | % % |
| 293 | % % |
| 294 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 295 | % |
| 296 | % UnregisterMAPImage() removes format registrations made by the |
| 297 | % MAP module from the list of supported formats. |
| 298 | % |
| 299 | % The format of the UnregisterMAPImage method is: |
| 300 | % |
| 301 | % UnregisterMAPImage(void) |
| 302 | % |
| 303 | */ |
| 304 | ModuleExport void UnregisterMAPImage(void) |
| 305 | { |
| 306 | (void) UnregisterMagickInfo("MAP"); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 311 | % % |
| 312 | % % |
| 313 | % % |
| 314 | % W r i t e M A P I m a g e % |
| 315 | % % |
| 316 | % % |
| 317 | % % |
| 318 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 319 | % |
| 320 | % WriteMAPImage() writes an image to a file as red, green, and blue |
| 321 | % colormap bytes followed by the colormap indexes. |
| 322 | % |
| 323 | % The format of the WriteMAPImage method is: |
| 324 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 325 | % MagickBooleanType WriteMAPImage(const ImageInfo *image_info, |
| 326 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 327 | % |
| 328 | % A description of each parameter follows. |
| 329 | % |
| 330 | % o image_info: the image info. |
| 331 | % |
| 332 | % o image: The image. |
| 333 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 334 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 335 | % |
| 336 | */ |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 337 | static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image, |
| 338 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 339 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 340 | MagickBooleanType |
| 341 | status; |
| 342 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 343 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 344 | *p; |
| 345 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 346 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 347 | i, |
| 348 | x; |
| 349 | |
| 350 | register unsigned char |
| 351 | *q; |
| 352 | |
| 353 | size_t |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 354 | depth, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 355 | packet_size; |
| 356 | |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 357 | ssize_t |
| 358 | y; |
| 359 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 360 | unsigned char |
| 361 | *colormap, |
| 362 | *pixels; |
| 363 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 364 | /* |
| 365 | Open output image file. |
| 366 | */ |
| 367 | assert(image_info != (const ImageInfo *) NULL); |
| 368 | assert(image_info->signature == MagickSignature); |
| 369 | assert(image != (Image *) NULL); |
| 370 | assert(image->signature == MagickSignature); |
| 371 | if (image->debug != MagickFalse) |
| 372 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 373 | assert(exception != (ExceptionInfo *) NULL); |
| 374 | assert(exception->signature == MagickSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 375 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 376 | if (status == MagickFalse) |
| 377 | return(status); |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame^] | 378 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 379 | /* |
| 380 | Allocate colormap. |
| 381 | */ |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 382 | if (IsPaletteImage(image,exception) == MagickFalse) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 383 | (void) SetImageType(image,PaletteType,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 384 | depth=GetImageQuantumDepth(image,MagickTrue); |
| 385 | packet_size=(size_t) (depth/8); |
| 386 | pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size* |
| 387 | sizeof(*pixels)); |
| 388 | packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL); |
| 389 | colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size* |
| 390 | sizeof(*colormap)); |
| 391 | if ((pixels == (unsigned char *) NULL) || |
| 392 | (colormap == (unsigned char *) NULL)) |
| 393 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 394 | /* |
| 395 | Write colormap to file. |
| 396 | */ |
| 397 | q=colormap; |
| 398 | if (image->depth <= 8) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 399 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 400 | { |
| 401 | *q++=(unsigned char) image->colormap[i].red; |
| 402 | *q++=(unsigned char) image->colormap[i].green; |
| 403 | *q++=(unsigned char) image->colormap[i].blue; |
| 404 | } |
| 405 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 406 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 407 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 408 | *q++=(unsigned char) ((size_t) image->colormap[i].red >> 8); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 409 | *q++=(unsigned char) image->colormap[i].red; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 410 | *q++=(unsigned char) ((size_t) image->colormap[i].green >> 8); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 411 | *q++=(unsigned char) image->colormap[i].green; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 412 | *q++=(unsigned char) ((size_t) image->colormap[i].blue >> 8); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 413 | *q++=(unsigned char) image->colormap[i].blue; |
| 414 | } |
| 415 | (void) WriteBlob(image,packet_size*image->colors,colormap); |
| 416 | colormap=(unsigned char *) RelinquishMagickMemory(colormap); |
| 417 | /* |
| 418 | Write image pixels to file. |
| 419 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 420 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 421 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 422 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 423 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 424 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 425 | q=pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 426 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 427 | { |
| 428 | if (image->colors > 256) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 429 | *q++=(unsigned char) ((size_t) GetPixelIndex(image,p) >> 8); |
| 430 | *q++=(unsigned char) GetPixelIndex(image,p); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 431 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 432 | } |
| 433 | (void) WriteBlob(image,(size_t) (q-pixels),pixels); |
| 434 | } |
| 435 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 436 | (void) CloseBlob(image); |
| 437 | return(status); |
| 438 | } |