]> granicus.if.org Git - imagemagick/blob - coders/map.c
(no commit message)
[imagemagick] / coders / map.c
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                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
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 \f
39 /*
40   Include declarations.
41 */
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"
65 \f
66 /*
67   Forward declarations.
68 */
69 static MagickBooleanType
70   WriteMAPImage(const ImageInfo *,Image *);
71 \f
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 */
98 static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
99 {
100   Image
101     *image;
102
103   IndexPacket
104     index;
105
106   long
107     y;
108
109   MagickBooleanType
110     status;
111
112   register IndexPacket
113     *indexes;
114
115   register long
116     x;
117
118   register PixelPacket
119     *q;
120
121   register long
122     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
137   unsigned long
138     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;
164   status=AcquireImageColormap(image,(unsigned long)
165     (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)
186     for (i=0; i < (long) image->colors; i++)
187     {
188       image->colormap[i].red=ScaleCharToQuantum(*p++);
189       image->colormap[i].green=ScaleCharToQuantum(*p++);
190       image->colormap[i].blue=ScaleCharToQuantum(*p++);
191     }
192   else
193     for (i=0; i < (long) image->colors; i++)
194     {
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);
215   for (y=0; y < (long) image->rows; y++)
216   {
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;
225     for (x=0; x < (long) image->columns; x++)
226     {
227       index=ConstrainColormapIndex(image,*p);
228       p++;
229       if (image->colors > 256)
230         {
231           index=ConstrainColormapIndex(image,((unsigned long) index << 8)+(*p));
232           p++;
233         }
234       indexes[x]=(IndexPacket) index;
235       *q++=image->colormap[(long) index];
236     }
237     if (SyncAuthenticPixels(image,exception) == MagickFalse)
238       break;
239   }
240   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
241   if (y < (long) image->rows)
242     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
243       image->filename);
244   (void) CloseBlob(image);
245   return(GetFirstImageInList(image));
246 }
247 \f
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 %
268 %      unsigned long RegisterMAPImage(void)
269 %
270 */
271 ModuleExport unsigned long RegisterMAPImage(void)
272 {
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 \f
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 */
308 ModuleExport void UnregisterMAPImage(void)
309 {
310   (void) UnregisterMagickInfo("MAP");
311 }
312 \f
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 */
339 static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image)
340 {
341   long
342     y;
343
344   MagickBooleanType
345     status;
346
347   register const IndexPacket
348     *indexes;
349
350   register const PixelPacket
351     *p;
352
353   register long
354     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
367   unsigned long
368     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)
404     for (i=0; i < (long) image->colors; i++)
405     {
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
411     for (i=0; i < (long) image->colors; i++)
412     {
413       *q++=(unsigned char) ((unsigned long) image->colormap[i].red >> 8);
414       *q++=(unsigned char) image->colormap[i].red;
415       *q++=(unsigned char) ((unsigned long) image->colormap[i].green >> 8);
416       *q++=(unsigned char) image->colormap[i].green;
417       *q++=(unsigned char) ((unsigned long) image->colormap[i].blue >> 8);
418       *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   */
425   for (y=0; y < (long) image->rows; y++)
426   {
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;
432     for (x=0; x < (long) image->columns; x++)
433     {
434       if (image->colors > 256)
435         *q++=(unsigned char) ((unsigned long) indexes[x] >> 8);
436       *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 }