2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Image Colormaps As An Image File. %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
52 #include "MagickCore/colorspace-private.h"
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"
71 static MagickBooleanType
72 WriteMAPImage(const ImageInfo *,Image *,ExceptionInfo *);
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 % R e a d M A P I m a g e %
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
89 % The format of the ReadMAPImage method is:
91 % Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
93 % A description of each parameter follows:
95 % o image_info: the image info.
97 % o exception: return any errors or warnings in this structure.
100 static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
120 register unsigned char
139 assert(image_info != (const ImageInfo *) NULL);
140 assert(image_info->signature == MagickCoreSignature);
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 == MagickCoreSignature);
146 image=AcquireImage(image_info,exception);
147 if ((image->columns == 0) || (image->rows == 0))
148 ThrowReaderException(OptionError,"MustSpecifyImageSize");
149 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
150 if (status == MagickFalse)
152 image=DestroyImageList(image);
153 return((Image *) NULL);
156 Initialize image structure.
158 image->storage_class=PseudoClass;
159 status=AcquireImageColormap(image,(size_t)
160 (image->offset != 0 ? image->offset : 256),exception);
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*
167 packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
168 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
170 if ((pixels == (unsigned char *) NULL) ||
171 (colormap == (unsigned char *) NULL))
172 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
176 count=ReadBlob(image,packet_size*image->colors,colormap);
177 if (count != (ssize_t) (packet_size*image->colors))
178 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
180 if (image->depth <= 8)
181 for (i=0; i < (ssize_t) image->colors; i++)
183 image->colormap[i].red=ScaleCharToQuantum(*p++);
184 image->colormap[i].green=ScaleCharToQuantum(*p++);
185 image->colormap[i].blue=ScaleCharToQuantum(*p++);
188 for (i=0; i < (ssize_t) image->colors; i++)
192 image->colormap[i].red=(Quantum) quantum;
195 image->colormap[i].green=(Quantum) quantum;
198 image->colormap[i].blue=(Quantum) quantum;
200 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
201 if (image_info->ping != MagickFalse)
203 (void) CloseBlob(image);
204 return(GetFirstImageInList(image));
206 status=SetImageExtent(image,image->columns,image->rows,exception);
207 if (status == MagickFalse)
208 return(DestroyImageList(image));
212 packet_size=(size_t) (depth/8);
213 for (y=0; y < (ssize_t) image->rows; y++)
216 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
217 if (q == (Quantum *) NULL)
219 count=ReadBlob(image,(size_t) packet_size*image->columns,pixels);
220 if (count != (ssize_t) (packet_size*image->columns))
222 for (x=0; x < (ssize_t) image->columns; x++)
224 index=ConstrainColormapIndex(image,*p,exception);
226 if (image->colors > 256)
228 index=ConstrainColormapIndex(image,((size_t) index << 8)+(*p),
232 SetPixelIndex(image,index,q);
233 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
234 q+=GetPixelChannels(image);
236 if (SyncAuthenticPixels(image,exception) == MagickFalse)
239 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
240 if (y < (ssize_t) image->rows)
241 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
243 (void) CloseBlob(image);
244 return(GetFirstImageInList(image));
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 % R e g i s t e r M A P I m a g e %
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 % RegisterMAPImage() adds attributes for the MAP image format to
259 % the list of supported formats. The attributes include the image format
260 % tag, a method to read and/or write the format, whether the format
261 % supports the saving of more than one frame to the same file or blob,
262 % whether the format supports native in-memory I/O, and a brief
263 % description of the format.
265 % The format of the RegisterMAPImage method is:
267 % size_t RegisterMAPImage(void)
270 ModuleExport size_t RegisterMAPImage(void)
275 entry=AcquireMagickInfo("MAP","MAP","Colormap intensities and indices");
276 entry->decoder=(DecodeImageHandler *) ReadMAPImage;
277 entry->encoder=(EncodeImageHandler *) WriteMAPImage;
278 entry->flags^=CoderAdjoinFlag;
279 entry->format_type=ExplicitFormatType;
280 entry->flags|=CoderRawSupportFlag;
281 entry->flags|=CoderEndianSupportFlag;
282 (void) RegisterMagickInfo(entry);
283 return(MagickImageCoderSignature);
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % U n r e g i s t e r M A P I m a g e %
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % UnregisterMAPImage() removes format registrations made by the
298 % MAP module from the list of supported formats.
300 % The format of the UnregisterMAPImage method is:
302 % UnregisterMAPImage(void)
305 ModuleExport void UnregisterMAPImage(void)
307 (void) UnregisterMagickInfo("MAP");
311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 % W r i t e M A P I m a g e %
319 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 % WriteMAPImage() writes an image to a file as red, green, and blue
322 % colormap bytes followed by the colormap indexes.
324 % The format of the WriteMAPImage method is:
326 % MagickBooleanType WriteMAPImage(const ImageInfo *image_info,
327 % Image *image,ExceptionInfo *exception)
329 % A description of each parameter follows.
331 % o image_info: the image info.
333 % o image: The image.
335 % o exception: return any errors or warnings in this structure.
338 static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image,
339 ExceptionInfo *exception)
344 register const Quantum
351 register unsigned char
366 Open output image file.
368 assert(image_info != (const ImageInfo *) NULL);
369 assert(image_info->signature == MagickCoreSignature);
370 assert(image != (Image *) NULL);
371 assert(image->signature == MagickCoreSignature);
372 if (image->debug != MagickFalse)
373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
374 assert(exception != (ExceptionInfo *) NULL);
375 assert(exception->signature == MagickCoreSignature);
376 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
377 if (status == MagickFalse)
379 (void) TransformImageColorspace(image,sRGBColorspace,exception);
383 if (IsPaletteImage(image) == MagickFalse)
384 (void) SetImageType(image,PaletteType,exception);
385 depth=GetImageQuantumDepth(image,MagickTrue);
386 packet_size=(size_t) (depth/8);
387 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
389 packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
390 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
392 if ((pixels == (unsigned char *) NULL) ||
393 (colormap == (unsigned char *) NULL))
394 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
396 Write colormap to file.
400 if (image->colors <= 256)
401 for (i=0; i < (ssize_t) image->colors; i++)
403 *q++=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
404 *q++=(unsigned char) ScaleQuantumToChar(image->colormap[i].green);
405 *q++=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
408 for (i=0; i < (ssize_t) image->colors; i++)
410 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].red) >> 8);
411 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].red) & 0xff);
412 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].green) >> 8);
413 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].green) & 0xff);;
414 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].blue) >> 8);
415 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].blue) & 0xff);
417 (void) WriteBlob(image,packet_size*image->colors,colormap);
418 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
420 Write image pixels to file.
422 for (y=0; y < (ssize_t) image->rows; y++)
424 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
425 if (p == (const Quantum *) NULL)
428 for (x=0; x < (ssize_t) image->columns; x++)
430 if (image->colors > 256)
431 *q++=(unsigned char) ((size_t) GetPixelIndex(image,p) >> 8);
432 *q++=(unsigned char) GetPixelIndex(image,p);
433 p+=GetPixelChannels(image);
435 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
437 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
438 (void) CloseBlob(image);