]> 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-2011 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 "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"
66 \f
67 /*
68   Forward declarations.
69 */
70 static MagickBooleanType
71   WriteMAPImage(const ImageInfo *,Image *);
72 \f
73 /*
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 %                                                                             %
76 %                                                                             %
77 %                                                                             %
78 %   R e a d M A P I m a g e                                                   %
79 %                                                                             %
80 %                                                                             %
81 %                                                                             %
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 %
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.
87 %
88 %  The format of the ReadMAPImage method is:
89 %
90 %      Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
91 %
92 %  A description of each parameter follows:
93 %
94 %    o image_info: the image info.
95 %
96 %    o exception: return any errors or warnings in this structure.
97 %
98 */
99 static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
100 {
101   Image
102     *image;
103
104   MagickBooleanType
105     status;
106
107   Quantum
108     index;
109
110   register ssize_t
111     x;
112
113   register Quantum
114     *q;
115
116   register ssize_t
117     i;
118
119   register unsigned char
120     *p;
121
122   size_t
123     depth,
124     packet_size,
125     quantum;
126
127   ssize_t
128     count,
129     y;
130
131   unsigned char
132     *colormap,
133     *pixels;
134
135   /*
136     Open image file.
137   */
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)
150     {
151       image=DestroyImageList(image);
152       return((Image *) NULL);
153     }
154   /*
155     Initialize image structure.
156   */
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*
165     sizeof(*pixels));
166   packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
167   colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
168     sizeof(*colormap));
169   if ((pixels == (unsigned char *) NULL) ||
170       (colormap == (unsigned char *) NULL))
171     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
172   /*
173     Read image colormap.
174   */
175   count=ReadBlob(image,packet_size*image->colors,colormap);
176   if (count != (ssize_t) (packet_size*image->colors))
177     ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
178   p=colormap;
179   if (image->depth <= 8)
180     for (i=0; i < (ssize_t) image->colors; i++)
181     {
182       image->colormap[i].red=ScaleCharToQuantum(*p++);
183       image->colormap[i].green=ScaleCharToQuantum(*p++);
184       image->colormap[i].blue=ScaleCharToQuantum(*p++);
185     }
186   else
187     for (i=0; i < (ssize_t) image->colors; i++)
188     {
189       quantum=(*p++ << 8);
190       quantum|=(*p++);
191       image->colormap[i].red=(Quantum) quantum;
192       quantum=(*p++ << 8);
193       quantum|=(*p++);
194       image->colormap[i].green=(Quantum) quantum;
195       quantum=(*p++ << 8);
196       quantum|=(*p++);
197       image->colormap[i].blue=(Quantum) quantum;
198     }
199   colormap=(unsigned char *) RelinquishMagickMemory(colormap);
200   if (image_info->ping != MagickFalse)
201     {
202       (void) CloseBlob(image);
203       return(GetFirstImageInList(image));
204     }
205   /*
206     Read image pixels.
207   */
208   packet_size=(size_t) (depth/8);
209   for (y=0; y < (ssize_t) image->rows; y++)
210   {
211     p=pixels;
212     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
213     if (q == (const Quantum *) NULL)
214       break;
215     count=ReadBlob(image,(size_t) packet_size*image->columns,pixels);
216     if (count != (ssize_t) (packet_size*image->columns))
217       break;
218     for (x=0; x < (ssize_t) image->columns; x++)
219     {
220       index=ConstrainColormapIndex(image,*p);
221       p++;
222       if (image->colors > 256)
223         {
224           index=ConstrainColormapIndex(image,((size_t) index << 8)+(*p));
225           p++;
226         }
227       SetPixelIndex(image,index,q);
228       SetPixelPacket(image,image->colormap+(ssize_t) index,q);
229       q+=GetPixelChannels(image);
230     }
231     if (SyncAuthenticPixels(image,exception) == MagickFalse)
232       break;
233   }
234   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
235   if (y < (ssize_t) image->rows)
236     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
237       image->filename);
238   (void) CloseBlob(image);
239   return(GetFirstImageInList(image));
240 }
241 \f
242 /*
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 %                                                                             %
245 %                                                                             %
246 %                                                                             %
247 %   R e g i s t e r M A P I m a g e                                           %
248 %                                                                             %
249 %                                                                             %
250 %                                                                             %
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 %
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.
259 %
260 %  The format of the RegisterMAPImage method is:
261 %
262 %      size_t RegisterMAPImage(void)
263 %
264 */
265 ModuleExport size_t RegisterMAPImage(void)
266 {
267   MagickInfo
268     *entry;
269
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);
281 }
282 \f
283 /*
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 %                                                                             %
286 %                                                                             %
287 %                                                                             %
288 %   U n r e g i s t e r M A P I m a g e                                       %
289 %                                                                             %
290 %                                                                             %
291 %                                                                             %
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 %
294 %  UnregisterMAPImage() removes format registrations made by the
295 %  MAP module from the list of supported formats.
296 %
297 %  The format of the UnregisterMAPImage method is:
298 %
299 %      UnregisterMAPImage(void)
300 %
301 */
302 ModuleExport void UnregisterMAPImage(void)
303 {
304   (void) UnregisterMagickInfo("MAP");
305 }
306 \f
307 /*
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 %                                                                             %
310 %                                                                             %
311 %                                                                             %
312 %   W r i t e M A P I m a g e                                                 %
313 %                                                                             %
314 %                                                                             %
315 %                                                                             %
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 %
318 %  WriteMAPImage() writes an image to a file as red, green, and blue
319 %  colormap bytes followed by the colormap indexes.
320 %
321 %  The format of the WriteMAPImage method is:
322 %
323 %      MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image)
324 %
325 %  A description of each parameter follows.
326 %
327 %    o image_info: the image info.
328 %
329 %    o image:  The image.
330 %
331 %
332 */
333 static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image)
334 {
335   MagickBooleanType
336     status;
337
338   register const Quantum
339     *p;
340
341   register ssize_t
342     i,
343     x;
344
345   register unsigned char
346     *q;
347
348   size_t
349     depth,
350     packet_size;
351
352   ssize_t
353     y;
354
355   unsigned char
356     *colormap,
357     *pixels;
358
359   /*
360     Open output image file.
361   */
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)
370     return(status);
371   if (image->colorspace != RGBColorspace)
372     (void) TransformImageColorspace(image,RGBColorspace);
373   /*
374     Allocate colormap.
375   */
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*
381     sizeof(*pixels));
382   packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
383   colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
384     sizeof(*colormap));
385   if ((pixels == (unsigned char *) NULL) ||
386       (colormap == (unsigned char *) NULL))
387     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
388   /*
389     Write colormap to file.
390   */
391   q=colormap;
392   if (image->depth <= 8)
393     for (i=0; i < (ssize_t) image->colors; i++)
394     {
395       *q++=(unsigned char) image->colormap[i].red;
396       *q++=(unsigned char) image->colormap[i].green;
397       *q++=(unsigned char) image->colormap[i].blue;
398     }
399   else
400     for (i=0; i < (ssize_t) image->colors; i++)
401     {
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;
408     }
409   (void) WriteBlob(image,packet_size*image->colors,colormap);
410   colormap=(unsigned char *) RelinquishMagickMemory(colormap);
411   /*
412     Write image pixels to file.
413   */
414   for (y=0; y < (ssize_t) image->rows; y++)
415   {
416     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
417     if (p == (const Quantum *) NULL)
418       break;
419     q=pixels;
420     for (x=0; x < (ssize_t) image->columns; x++)
421     {
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);
426     }
427     (void) WriteBlob(image,(size_t) (q-pixels),pixels);
428   }
429   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
430   (void) CloseBlob(image);
431   return(status);
432 }