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 "magick/studio.h"
43 #include "magick/attribute.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/colormap.h"
50 #include "magick/colormap-private.h"
51 #include "magick/colorspace.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/histogram.h"
55 #include "magick/image.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/quantum-private.h"
61 #include "magick/static.h"
62 #include "magick/statistic.h"
63 #include "magick/string_.h"
64 #include "magick/module.h"
69 static MagickBooleanType
70 WriteMAPImage(const ImageInfo *,Image *);
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % R e a d M A P I m a g e %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 % ReadMAPImage() reads an image of raw RGB colormap and colormap index
84 % bytes and returns it. It allocates the memory necessary for the new Image
85 % structure and returns a pointer to the new image.
87 % The format of the ReadMAPImage method is:
89 % Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
91 % A description of each parameter follows:
93 % o image_info: the image info.
95 % o exception: return any errors or warnings in this structure.
98 static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
121 register unsigned char
140 assert(image_info != (const ImageInfo *) NULL);
141 assert(image_info->signature == MagickSignature);
142 if (image_info->debug != MagickFalse)
143 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
144 image_info->filename);
145 assert(exception != (ExceptionInfo *) NULL);
146 assert(exception->signature == MagickSignature);
147 image=AcquireImage(image_info);
148 if ((image->columns == 0) || (image->rows == 0))
149 ThrowReaderException(OptionError,"MustSpecifyImageSize");
150 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
151 if (status == MagickFalse)
153 image=DestroyImageList(image);
154 return((Image *) NULL);
157 Initialize image structure.
159 image->storage_class=PseudoClass;
160 status=AcquireImageColormap(image,(size_t)
161 (image->offset != 0 ? image->offset : 256));
162 if (status == MagickFalse)
163 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
164 depth=GetImageQuantumDepth(image,MagickTrue);
165 packet_size=(size_t) (depth/8);
166 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
168 packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
169 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
171 if ((pixels == (unsigned char *) NULL) ||
172 (colormap == (unsigned char *) NULL))
173 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
177 count=ReadBlob(image,packet_size*image->colors,colormap);
178 if (count != (ssize_t) (packet_size*image->colors))
179 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
181 if (image->depth <= 8)
182 for (i=0; i < (ssize_t) image->colors; i++)
184 image->colormap[i].red=ScaleCharToQuantum(*p++);
185 image->colormap[i].green=ScaleCharToQuantum(*p++);
186 image->colormap[i].blue=ScaleCharToQuantum(*p++);
189 for (i=0; i < (ssize_t) image->colors; i++)
193 image->colormap[i].red=(Quantum) quantum;
196 image->colormap[i].green=(Quantum) quantum;
199 image->colormap[i].blue=(Quantum) quantum;
201 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
202 if (image_info->ping != MagickFalse)
204 (void) CloseBlob(image);
205 return(GetFirstImageInList(image));
210 packet_size=(size_t) (depth/8);
211 for (y=0; y < (ssize_t) image->rows; y++)
214 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
215 if (q == (PixelPacket *) NULL)
217 indexes=GetAuthenticIndexQueue(image);
218 count=ReadBlob(image,(size_t) packet_size*image->columns,pixels);
219 if (count != (ssize_t) (packet_size*image->columns))
221 for (x=0; x < (ssize_t) image->columns; x++)
223 index=ConstrainColormapIndex(image,*p);
225 if (image->colors > 256)
227 index=ConstrainColormapIndex(image,((size_t) index << 8)+(*p));
230 SetIndexPixelComponent(indexes+x,index);
231 SetRedPixelComponent(q,image->colormap[(ssize_t) index].red);
232 SetGreenPixelComponent(q,image->colormap[(ssize_t) index].green);
233 SetBluePixelComponent(q,image->colormap[(ssize_t) index].blue);
235 if (SyncAuthenticPixels(image,exception) == MagickFalse)
238 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
239 if (y < (ssize_t) image->rows)
240 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
242 (void) CloseBlob(image);
243 return(GetFirstImageInList(image));
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % R e g i s t e r M A P I m a g e %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % RegisterMAPImage() adds attributes for the MAP image format to
258 % the list of supported formats. The attributes include the image format
259 % tag, a method to read and/or write the format, whether the format
260 % supports the saving of more than one frame to the same file or blob,
261 % whether the format supports native in-memory I/O, and a brief
262 % description of the format.
264 % The format of the RegisterMAPImage method is:
266 % size_t RegisterMAPImage(void)
269 ModuleExport size_t RegisterMAPImage(void)
274 entry=SetMagickInfo("MAP");
275 entry->decoder=(DecodeImageHandler *) ReadMAPImage;
276 entry->encoder=(EncodeImageHandler *) WriteMAPImage;
277 entry->adjoin=MagickFalse;
278 entry->format_type=ExplicitFormatType;
279 entry->raw=MagickTrue;
280 entry->endian_support=MagickTrue;
281 entry->description=ConstantString("Colormap intensities and indices");
282 entry->module=ConstantString("MAP");
283 (void) RegisterMagickInfo(entry);
284 return(MagickImageCoderSignature);
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 % U n r e g i s t e r M A P I m a g e %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % UnregisterMAPImage() removes format registrations made by the
299 % MAP module from the list of supported formats.
301 % The format of the UnregisterMAPImage method is:
303 % UnregisterMAPImage(void)
306 ModuleExport void UnregisterMAPImage(void)
308 (void) UnregisterMagickInfo("MAP");
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 % W r i t e M A P I m a g e %
320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 % WriteMAPImage() writes an image to a file as red, green, and blue
323 % colormap bytes followed by the colormap indexes.
325 % The format of the WriteMAPImage method is:
327 % MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image)
329 % A description of each parameter follows.
331 % o image_info: the image info.
333 % o image: The image.
337 static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image)
342 register const IndexPacket
345 register const PixelPacket
352 register unsigned char
367 Open output image file.
369 assert(image_info != (const ImageInfo *) NULL);
370 assert(image_info->signature == MagickSignature);
371 assert(image != (Image *) NULL);
372 assert(image->signature == MagickSignature);
373 if (image->debug != MagickFalse)
374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
375 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
376 if (status == MagickFalse)
378 if (image->colorspace != RGBColorspace)
379 (void) TransformImageColorspace(image,RGBColorspace);
383 if (IsPaletteImage(image,&image->exception) == MagickFalse)
384 (void) SetImageType(image,PaletteType);
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.
399 if (image->depth <= 8)
400 for (i=0; i < (ssize_t) image->colors; i++)
402 *q++=(unsigned char) image->colormap[i].red;
403 *q++=(unsigned char) image->colormap[i].green;
404 *q++=(unsigned char) image->colormap[i].blue;
407 for (i=0; i < (ssize_t) image->colors; i++)
409 *q++=(unsigned char) ((size_t) image->colormap[i].red >> 8);
410 *q++=(unsigned char) image->colormap[i].red;
411 *q++=(unsigned char) ((size_t) image->colormap[i].green >> 8);
412 *q++=(unsigned char) image->colormap[i].green;
413 *q++=(unsigned char) ((size_t) image->colormap[i].blue >> 8);
414 *q++=(unsigned char) image->colormap[i].blue;
416 (void) WriteBlob(image,packet_size*image->colors,colormap);
417 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
419 Write image pixels to file.
421 for (y=0; y < (ssize_t) image->rows; y++)
423 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
424 if (p == (const PixelPacket *) NULL)
426 indexes=GetVirtualIndexQueue(image);
428 for (x=0; x < (ssize_t) image->columns; x++)
430 if (image->colors > 256)
431 *q++=(unsigned char) ((size_t) GetIndexPixelComponent(indexes+x) >> 8);
432 *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
434 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
436 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
437 (void) CloseBlob(image);