blob: ee3335d3e1e8f7e25620ef74ba36b883a7cbbfcb [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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 %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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*/
42#include "magick/studio.h"
cristy5a2ca482009-10-14 18:24:56 +000043#include "magick/attribute.h"
cristy3ed852e2009-09-05 21:47:34 +000044#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/cache.h"
47#include "magick/color.h"
cristy3ed852e2009-09-05 21:47:34 +000048#include "magick/color-private.h"
cristye7e40552010-04-24 21:34:22 +000049#include "magick/colormap.h"
50#include "magick/colormap-private.h"
cristy3ed852e2009-09-05 21:47:34 +000051#include "magick/colorspace.h"
52#include "magick/exception.h"
53#include "magick/exception-private.h"
cristyf2e11662009-10-14 01:24:43 +000054#include "magick/histogram.h"
cristy3ed852e2009-09-05 21:47:34 +000055#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"
65
66/*
67 Forward declarations.
68*/
69static MagickBooleanType
70 WriteMAPImage(const ImageInfo *,Image *);
71
72/*
73%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74% %
75% %
76% %
77% R e a d M A P I m a g e %
78% %
79% %
80% %
81%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82%
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.
86%
87% The format of the ReadMAPImage method is:
88%
89% Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
90%
91% A description of each parameter follows:
92%
93% o image_info: the image info.
94%
95% o exception: return any errors or warnings in this structure.
96%
97*/
98static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
99{
100 Image
101 *image;
102
103 IndexPacket
104 index;
105
cristybb503372010-05-27 20:51:26 +0000106 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000107 y;
108
109 MagickBooleanType
110 status;
111
112 register IndexPacket
113 *indexes;
114
cristybb503372010-05-27 20:51:26 +0000115 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000116 x;
117
118 register PixelPacket
119 *q;
120
cristybb503372010-05-27 20:51:26 +0000121 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000122 i;
123
124 register unsigned char
125 *p;
126
127 size_t
128 packet_size;
129
130 ssize_t
131 count;
132
133 unsigned char
134 *colormap,
135 *pixels;
136
cristybb503372010-05-27 20:51:26 +0000137 size_t
cristy3ed852e2009-09-05 21:47:34 +0000138 depth,
139 quantum;
140
141 /*
142 Open image file.
143 */
144 assert(image_info != (const ImageInfo *) NULL);
145 assert(image_info->signature == MagickSignature);
146 if (image_info->debug != MagickFalse)
147 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
148 image_info->filename);
149 assert(exception != (ExceptionInfo *) NULL);
150 assert(exception->signature == MagickSignature);
151 image=AcquireImage(image_info);
152 if ((image->columns == 0) || (image->rows == 0))
153 ThrowReaderException(OptionError,"MustSpecifyImageSize");
154 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
155 if (status == MagickFalse)
156 {
157 image=DestroyImageList(image);
158 return((Image *) NULL);
159 }
160 /*
161 Initialize image structure.
162 */
163 image->storage_class=PseudoClass;
cristybb503372010-05-27 20:51:26 +0000164 status=AcquireImageColormap(image,(size_t)
cristy3ed852e2009-09-05 21:47:34 +0000165 (image->offset != 0 ? image->offset : 256));
166 if (status == MagickFalse)
167 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
168 depth=GetImageQuantumDepth(image,MagickTrue);
169 packet_size=(size_t) (depth/8);
170 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
171 sizeof(*pixels));
172 packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
173 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
174 sizeof(*colormap));
175 if ((pixels == (unsigned char *) NULL) ||
176 (colormap == (unsigned char *) NULL))
177 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
178 /*
179 Read image colormap.
180 */
181 count=ReadBlob(image,packet_size*image->colors,colormap);
182 if (count != (ssize_t) (packet_size*image->colors))
183 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
184 p=colormap;
185 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +0000186 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000187 {
188 image->colormap[i].red=ScaleCharToQuantum(*p++);
189 image->colormap[i].green=ScaleCharToQuantum(*p++);
190 image->colormap[i].blue=ScaleCharToQuantum(*p++);
191 }
192 else
cristybb503372010-05-27 20:51:26 +0000193 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000194 {
195 quantum=(*p++ << 8);
196 quantum|=(*p++);
197 image->colormap[i].red=(Quantum) quantum;
198 quantum=(*p++ << 8);
199 quantum|=(*p++);
200 image->colormap[i].green=(Quantum) quantum;
201 quantum=(*p++ << 8);
202 quantum|=(*p++);
203 image->colormap[i].blue=(Quantum) quantum;
204 }
205 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
206 if (image_info->ping != MagickFalse)
207 {
208 (void) CloseBlob(image);
209 return(GetFirstImageInList(image));
210 }
211 /*
212 Read image pixels.
213 */
214 packet_size=(size_t) (depth/8);
cristybb503372010-05-27 20:51:26 +0000215 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000216 {
217 p=pixels;
218 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
219 if (q == (PixelPacket *) NULL)
220 break;
221 indexes=GetAuthenticIndexQueue(image);
222 count=ReadBlob(image,(size_t) packet_size*image->columns,pixels);
223 if (count != (ssize_t) (packet_size*image->columns))
224 break;
cristybb503372010-05-27 20:51:26 +0000225 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000226 {
227 index=ConstrainColormapIndex(image,*p);
228 p++;
229 if (image->colors > 256)
230 {
cristybb503372010-05-27 20:51:26 +0000231 index=ConstrainColormapIndex(image,((size_t) index << 8)+(*p));
cristy3ed852e2009-09-05 21:47:34 +0000232 p++;
233 }
234 indexes[x]=(IndexPacket) index;
cristybb503372010-05-27 20:51:26 +0000235 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +0000236 }
237 if (SyncAuthenticPixels(image,exception) == MagickFalse)
238 break;
239 }
240 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
cristybb503372010-05-27 20:51:26 +0000241 if (y < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +0000242 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
243 image->filename);
244 (void) CloseBlob(image);
245 return(GetFirstImageInList(image));
246}
247
248/*
249%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250% %
251% %
252% %
253% R e g i s t e r M A P I m a g e %
254% %
255% %
256% %
257%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258%
259% RegisterMAPImage() adds attributes for the MAP image format to
260% the list of supported formats. The attributes include the image format
261% tag, a method to read and/or write the format, whether the format
262% supports the saving of more than one frame to the same file or blob,
263% whether the format supports native in-memory I/O, and a brief
264% description of the format.
265%
266% The format of the RegisterMAPImage method is:
267%
cristybb503372010-05-27 20:51:26 +0000268% size_t RegisterMAPImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000269%
270*/
cristybb503372010-05-27 20:51:26 +0000271ModuleExport size_t RegisterMAPImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000272{
273 MagickInfo
274 *entry;
275
276 entry=SetMagickInfo("MAP");
277 entry->decoder=(DecodeImageHandler *) ReadMAPImage;
278 entry->encoder=(EncodeImageHandler *) WriteMAPImage;
279 entry->adjoin=MagickFalse;
280 entry->format_type=ExplicitFormatType;
281 entry->raw=MagickTrue;
282 entry->endian_support=MagickTrue;
283 entry->description=ConstantString("Colormap intensities and indices");
284 entry->module=ConstantString("MAP");
285 (void) RegisterMagickInfo(entry);
286 return(MagickImageCoderSignature);
287}
288
289/*
290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291% %
292% %
293% %
294% U n r e g i s t e r M A P I m a g e %
295% %
296% %
297% %
298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299%
300% UnregisterMAPImage() removes format registrations made by the
301% MAP module from the list of supported formats.
302%
303% The format of the UnregisterMAPImage method is:
304%
305% UnregisterMAPImage(void)
306%
307*/
308ModuleExport void UnregisterMAPImage(void)
309{
310 (void) UnregisterMagickInfo("MAP");
311}
312
313/*
314%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315% %
316% %
317% %
318% W r i t e M A P I m a g e %
319% %
320% %
321% %
322%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323%
324% WriteMAPImage() writes an image to a file as red, green, and blue
325% colormap bytes followed by the colormap indexes.
326%
327% The format of the WriteMAPImage method is:
328%
329% MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image)
330%
331% A description of each parameter follows.
332%
333% o image_info: the image info.
334%
335% o image: The image.
336%
337%
338*/
339static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image)
340{
cristybb503372010-05-27 20:51:26 +0000341 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000342 y;
343
344 MagickBooleanType
345 status;
346
347 register const IndexPacket
348 *indexes;
349
350 register const PixelPacket
351 *p;
352
cristybb503372010-05-27 20:51:26 +0000353 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000354 i,
355 x;
356
357 register unsigned char
358 *q;
359
360 size_t
361 packet_size;
362
363 unsigned char
364 *colormap,
365 *pixels;
366
cristybb503372010-05-27 20:51:26 +0000367 size_t
cristy3ed852e2009-09-05 21:47:34 +0000368 depth;
369
370 /*
371 Open output image file.
372 */
373 assert(image_info != (const ImageInfo *) NULL);
374 assert(image_info->signature == MagickSignature);
375 assert(image != (Image *) NULL);
376 assert(image->signature == MagickSignature);
377 if (image->debug != MagickFalse)
378 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
379 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
380 if (status == MagickFalse)
381 return(status);
382 if (image->colorspace != RGBColorspace)
383 (void) TransformImageColorspace(image,RGBColorspace);
384 /*
385 Allocate colormap.
386 */
387 if (IsPaletteImage(image,&image->exception) == MagickFalse)
388 (void) SetImageType(image,PaletteType);
389 depth=GetImageQuantumDepth(image,MagickTrue);
390 packet_size=(size_t) (depth/8);
391 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
392 sizeof(*pixels));
393 packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
394 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
395 sizeof(*colormap));
396 if ((pixels == (unsigned char *) NULL) ||
397 (colormap == (unsigned char *) NULL))
398 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
399 /*
400 Write colormap to file.
401 */
402 q=colormap;
403 if (image->depth <= 8)
cristybb503372010-05-27 20:51:26 +0000404 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000405 {
406 *q++=(unsigned char) image->colormap[i].red;
407 *q++=(unsigned char) image->colormap[i].green;
408 *q++=(unsigned char) image->colormap[i].blue;
409 }
410 else
cristybb503372010-05-27 20:51:26 +0000411 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000412 {
cristybb503372010-05-27 20:51:26 +0000413 *q++=(unsigned char) ((size_t) image->colormap[i].red >> 8);
cristy3ed852e2009-09-05 21:47:34 +0000414 *q++=(unsigned char) image->colormap[i].red;
cristybb503372010-05-27 20:51:26 +0000415 *q++=(unsigned char) ((size_t) image->colormap[i].green >> 8);
cristy3ed852e2009-09-05 21:47:34 +0000416 *q++=(unsigned char) image->colormap[i].green;
cristybb503372010-05-27 20:51:26 +0000417 *q++=(unsigned char) ((size_t) image->colormap[i].blue >> 8);
cristy3ed852e2009-09-05 21:47:34 +0000418 *q++=(unsigned char) image->colormap[i].blue;
419 }
420 (void) WriteBlob(image,packet_size*image->colors,colormap);
421 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
422 /*
423 Write image pixels to file.
424 */
cristybb503372010-05-27 20:51:26 +0000425 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000426 {
427 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
428 if (p == (const PixelPacket *) NULL)
429 break;
430 indexes=GetVirtualIndexQueue(image);
431 q=pixels;
cristybb503372010-05-27 20:51:26 +0000432 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000433 {
434 if (image->colors > 256)
cristybb503372010-05-27 20:51:26 +0000435 *q++=(unsigned char) ((size_t) indexes[x] >> 8);
cristy3ed852e2009-09-05 21:47:34 +0000436 *q++=(unsigned char) indexes[x];
437 }
438 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
439 }
440 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
441 (void) CloseBlob(image);
442 return(status);
443}