2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Image Colormaps As An Image File. %
20 % Copyright 1999-2011 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 % http://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/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/histogram.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/pixel-accessor.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/statistic.h"
64 #include "MagickCore/string_.h"
65 #include "MagickCore/module.h"
70 static MagickBooleanType
71 WriteMAPImage(const ImageInfo *,Image *);
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % R e a d M A P I m a g e %
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % ReadMAPImage() reads an image of raw RGB colormap and colormap index
85 % bytes and returns it. It allocates the memory necessary for the new Image
86 % structure and returns a pointer to the new image.
88 % The format of the ReadMAPImage method is:
90 % Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
92 % A description of each parameter follows:
94 % o image_info: the image info.
96 % o exception: return any errors or warnings in this structure.
99 static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
119 register unsigned char
138 assert(image_info != (const ImageInfo *) NULL);
139 assert(image_info->signature == MagickSignature);
140 if (image_info->debug != MagickFalse)
141 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
142 image_info->filename);
143 assert(exception != (ExceptionInfo *) NULL);
144 assert(exception->signature == MagickSignature);
145 image=AcquireImage(image_info);
146 if ((image->columns == 0) || (image->rows == 0))
147 ThrowReaderException(OptionError,"MustSpecifyImageSize");
148 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
149 if (status == MagickFalse)
151 image=DestroyImageList(image);
152 return((Image *) NULL);
155 Initialize image structure.
157 image->storage_class=PseudoClass;
158 status=AcquireImageColormap(image,(size_t)
159 (image->offset != 0 ? image->offset : 256));
160 if (status == MagickFalse)
161 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
162 depth=GetImageQuantumDepth(image,MagickTrue);
163 packet_size=(size_t) (depth/8);
164 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
166 packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
167 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
169 if ((pixels == (unsigned char *) NULL) ||
170 (colormap == (unsigned char *) NULL))
171 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
175 count=ReadBlob(image,packet_size*image->colors,colormap);
176 if (count != (ssize_t) (packet_size*image->colors))
177 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
179 if (image->depth <= 8)
180 for (i=0; i < (ssize_t) image->colors; i++)
182 image->colormap[i].red=ScaleCharToQuantum(*p++);
183 image->colormap[i].green=ScaleCharToQuantum(*p++);
184 image->colormap[i].blue=ScaleCharToQuantum(*p++);
187 for (i=0; i < (ssize_t) image->colors; i++)
191 image->colormap[i].red=(Quantum) quantum;
194 image->colormap[i].green=(Quantum) quantum;
197 image->colormap[i].blue=(Quantum) quantum;
199 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
200 if (image_info->ping != MagickFalse)
202 (void) CloseBlob(image);
203 return(GetFirstImageInList(image));
208 packet_size=(size_t) (depth/8);
209 for (y=0; y < (ssize_t) image->rows; y++)
212 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
213 if (q == (const Quantum *) NULL)
215 count=ReadBlob(image,(size_t) packet_size*image->columns,pixels);
216 if (count != (ssize_t) (packet_size*image->columns))
218 for (x=0; x < (ssize_t) image->columns; x++)
220 index=ConstrainColormapIndex(image,*p);
222 if (image->colors > 256)
224 index=ConstrainColormapIndex(image,((size_t) index << 8)+(*p));
227 SetPixelIndex(image,index,q);
228 SetPixelPacket(image,image->colormap+(ssize_t) index,q);
229 q+=GetPixelChannels(image);
231 if (SyncAuthenticPixels(image,exception) == MagickFalse)
234 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
235 if (y < (ssize_t) image->rows)
236 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
238 (void) CloseBlob(image);
239 return(GetFirstImageInList(image));
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % R e g i s t e r M A P I m a g e %
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 % RegisterMAPImage() adds attributes for the MAP image format to
254 % the list of supported formats. The attributes include the image format
255 % tag, a method to read and/or write the format, whether the format
256 % supports the saving of more than one frame to the same file or blob,
257 % whether the format supports native in-memory I/O, and a brief
258 % description of the format.
260 % The format of the RegisterMAPImage method is:
262 % size_t RegisterMAPImage(void)
265 ModuleExport size_t RegisterMAPImage(void)
270 entry=SetMagickInfo("MAP");
271 entry->decoder=(DecodeImageHandler *) ReadMAPImage;
272 entry->encoder=(EncodeImageHandler *) WriteMAPImage;
273 entry->adjoin=MagickFalse;
274 entry->format_type=ExplicitFormatType;
275 entry->raw=MagickTrue;
276 entry->endian_support=MagickTrue;
277 entry->description=ConstantString("Colormap intensities and indices");
278 entry->module=ConstantString("MAP");
279 (void) RegisterMagickInfo(entry);
280 return(MagickImageCoderSignature);
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 % U n r e g i s t e r M A P I m a g e %
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % UnregisterMAPImage() removes format registrations made by the
295 % MAP module from the list of supported formats.
297 % The format of the UnregisterMAPImage method is:
299 % UnregisterMAPImage(void)
302 ModuleExport void UnregisterMAPImage(void)
304 (void) UnregisterMagickInfo("MAP");
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 % W r i t e M A P I m a g e %
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 % WriteMAPImage() writes an image to a file as red, green, and blue
319 % colormap bytes followed by the colormap indexes.
321 % The format of the WriteMAPImage method is:
323 % MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image)
325 % A description of each parameter follows.
327 % o image_info: the image info.
329 % o image: The image.
333 static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image)
338 register const Quantum
345 register unsigned char
360 Open output image file.
362 assert(image_info != (const ImageInfo *) NULL);
363 assert(image_info->signature == MagickSignature);
364 assert(image != (Image *) NULL);
365 assert(image->signature == MagickSignature);
366 if (image->debug != MagickFalse)
367 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
368 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
369 if (status == MagickFalse)
371 if (image->colorspace != RGBColorspace)
372 (void) TransformImageColorspace(image,RGBColorspace);
376 if (IsPaletteImage(image,&image->exception) == MagickFalse)
377 (void) SetImageType(image,PaletteType);
378 depth=GetImageQuantumDepth(image,MagickTrue);
379 packet_size=(size_t) (depth/8);
380 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
382 packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
383 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
385 if ((pixels == (unsigned char *) NULL) ||
386 (colormap == (unsigned char *) NULL))
387 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
389 Write colormap to file.
392 if (image->depth <= 8)
393 for (i=0; i < (ssize_t) image->colors; i++)
395 *q++=(unsigned char) image->colormap[i].red;
396 *q++=(unsigned char) image->colormap[i].green;
397 *q++=(unsigned char) image->colormap[i].blue;
400 for (i=0; i < (ssize_t) image->colors; i++)
402 *q++=(unsigned char) ((size_t) image->colormap[i].red >> 8);
403 *q++=(unsigned char) image->colormap[i].red;
404 *q++=(unsigned char) ((size_t) image->colormap[i].green >> 8);
405 *q++=(unsigned char) image->colormap[i].green;
406 *q++=(unsigned char) ((size_t) image->colormap[i].blue >> 8);
407 *q++=(unsigned char) image->colormap[i].blue;
409 (void) WriteBlob(image,packet_size*image->colors,colormap);
410 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
412 Write image pixels to file.
414 for (y=0; y < (ssize_t) image->rows; y++)
416 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
417 if (p == (const Quantum *) NULL)
420 for (x=0; x < (ssize_t) image->columns; x++)
422 if (image->colors > 256)
423 *q++=(unsigned char) ((size_t) GetPixelIndex(image,p) >> 8);
424 *q++=(unsigned char) GetPixelIndex(image,p);
425 p+=GetPixelChannels(image);
427 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
429 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
430 (void) CloseBlob(image);