]> granicus.if.org Git - imagemagick/blob - coders/map.c
Horizon validity (anti-aliased) added to Plane2Cylinder
[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 "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   MagickBooleanType
107     status;
108
109   register IndexPacket
110     *indexes;
111
112   register ssize_t
113     x;
114
115   register PixelPacket
116     *q;
117
118   register ssize_t
119     i;
120
121   register unsigned char
122     *p;
123
124   size_t
125     depth,
126     packet_size,
127     quantum;
128
129   ssize_t
130     count,
131     y;
132
133   unsigned char
134     *colormap,
135     *pixels;
136
137   /*
138     Open image file.
139   */
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)
152     {
153       image=DestroyImageList(image);
154       return((Image *) NULL);
155     }
156   /*
157     Initialize image structure.
158   */
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*
167     sizeof(*pixels));
168   packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
169   colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
170     sizeof(*colormap));
171   if ((pixels == (unsigned char *) NULL) ||
172       (colormap == (unsigned char *) NULL))
173     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
174   /*
175     Read image colormap.
176   */
177   count=ReadBlob(image,packet_size*image->colors,colormap);
178   if (count != (ssize_t) (packet_size*image->colors))
179     ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
180   p=colormap;
181   if (image->depth <= 8)
182     for (i=0; i < (ssize_t) image->colors; i++)
183     {
184       image->colormap[i].red=ScaleCharToQuantum(*p++);
185       image->colormap[i].green=ScaleCharToQuantum(*p++);
186       image->colormap[i].blue=ScaleCharToQuantum(*p++);
187     }
188   else
189     for (i=0; i < (ssize_t) image->colors; i++)
190     {
191       quantum=(*p++ << 8);
192       quantum|=(*p++);
193       image->colormap[i].red=(Quantum) quantum;
194       quantum=(*p++ << 8);
195       quantum|=(*p++);
196       image->colormap[i].green=(Quantum) quantum;
197       quantum=(*p++ << 8);
198       quantum|=(*p++);
199       image->colormap[i].blue=(Quantum) quantum;
200     }
201   colormap=(unsigned char *) RelinquishMagickMemory(colormap);
202   if (image_info->ping != MagickFalse)
203     {
204       (void) CloseBlob(image);
205       return(GetFirstImageInList(image));
206     }
207   /*
208     Read image pixels.
209   */
210   packet_size=(size_t) (depth/8);
211   for (y=0; y < (ssize_t) image->rows; y++)
212   {
213     p=pixels;
214     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
215     if (q == (PixelPacket *) NULL)
216       break;
217     indexes=GetAuthenticIndexQueue(image);
218     count=ReadBlob(image,(size_t) packet_size*image->columns,pixels);
219     if (count != (ssize_t) (packet_size*image->columns))
220       break;
221     for (x=0; x < (ssize_t) image->columns; x++)
222     {
223       index=ConstrainColormapIndex(image,*p);
224       p++;
225       if (image->colors > 256)
226         {
227           index=ConstrainColormapIndex(image,((size_t) index << 8)+(*p));
228           p++;
229         }
230       SetIndexPixelComponent(indexes+x,index);
231       SetRGBOPixelComponents(q,image->colormap+(ssize_t) index);
232     }
233     if (SyncAuthenticPixels(image,exception) == MagickFalse)
234       break;
235   }
236   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
237   if (y < (ssize_t) image->rows)
238     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
239       image->filename);
240   (void) CloseBlob(image);
241   return(GetFirstImageInList(image));
242 }
243 \f
244 /*
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 %                                                                             %
247 %                                                                             %
248 %                                                                             %
249 %   R e g i s t e r M A P I m a g e                                           %
250 %                                                                             %
251 %                                                                             %
252 %                                                                             %
253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 %
255 %  RegisterMAPImage() adds attributes for the MAP image format to
256 %  the list of supported formats.  The attributes include the image format
257 %  tag, a method to read and/or write the format, whether the format
258 %  supports the saving of more than one frame to the same file or blob,
259 %  whether the format supports native in-memory I/O, and a brief
260 %  description of the format.
261 %
262 %  The format of the RegisterMAPImage method is:
263 %
264 %      size_t RegisterMAPImage(void)
265 %
266 */
267 ModuleExport size_t RegisterMAPImage(void)
268 {
269   MagickInfo
270     *entry;
271
272   entry=SetMagickInfo("MAP");
273   entry->decoder=(DecodeImageHandler *) ReadMAPImage;
274   entry->encoder=(EncodeImageHandler *) WriteMAPImage;
275   entry->adjoin=MagickFalse;
276   entry->format_type=ExplicitFormatType;
277   entry->raw=MagickTrue;
278   entry->endian_support=MagickTrue;
279   entry->description=ConstantString("Colormap intensities and indices");
280   entry->module=ConstantString("MAP");
281   (void) RegisterMagickInfo(entry);
282   return(MagickImageCoderSignature);
283 }
284 \f
285 /*
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 %                                                                             %
288 %                                                                             %
289 %                                                                             %
290 %   U n r e g i s t e r M A P I m a g e                                       %
291 %                                                                             %
292 %                                                                             %
293 %                                                                             %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 %
296 %  UnregisterMAPImage() removes format registrations made by the
297 %  MAP module from the list of supported formats.
298 %
299 %  The format of the UnregisterMAPImage method is:
300 %
301 %      UnregisterMAPImage(void)
302 %
303 */
304 ModuleExport void UnregisterMAPImage(void)
305 {
306   (void) UnregisterMagickInfo("MAP");
307 }
308 \f
309 /*
310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 %                                                                             %
312 %                                                                             %
313 %                                                                             %
314 %   W r i t e M A P I m a g e                                                 %
315 %                                                                             %
316 %                                                                             %
317 %                                                                             %
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 %
320 %  WriteMAPImage() writes an image to a file as red, green, and blue
321 %  colormap bytes followed by the colormap indexes.
322 %
323 %  The format of the WriteMAPImage method is:
324 %
325 %      MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image)
326 %
327 %  A description of each parameter follows.
328 %
329 %    o image_info: the image info.
330 %
331 %    o image:  The image.
332 %
333 %
334 */
335 static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image)
336 {
337   MagickBooleanType
338     status;
339
340   register const IndexPacket
341     *indexes;
342
343   register const PixelPacket
344     *p;
345
346   register ssize_t
347     i,
348     x;
349
350   register unsigned char
351     *q;
352
353   size_t
354     depth,
355     packet_size;
356
357   ssize_t
358     y;
359
360   unsigned char
361     *colormap,
362     *pixels;
363
364   /*
365     Open output image file.
366   */
367   assert(image_info != (const ImageInfo *) NULL);
368   assert(image_info->signature == MagickSignature);
369   assert(image != (Image *) NULL);
370   assert(image->signature == MagickSignature);
371   if (image->debug != MagickFalse)
372     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
373   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
374   if (status == MagickFalse)
375     return(status);
376   if (image->colorspace != RGBColorspace)
377     (void) TransformImageColorspace(image,RGBColorspace);
378   /*
379     Allocate colormap.
380   */
381   if (IsPaletteImage(image,&image->exception) == MagickFalse)
382     (void) SetImageType(image,PaletteType);
383   depth=GetImageQuantumDepth(image,MagickTrue);
384   packet_size=(size_t) (depth/8);
385   pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
386     sizeof(*pixels));
387   packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
388   colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
389     sizeof(*colormap));
390   if ((pixels == (unsigned char *) NULL) ||
391       (colormap == (unsigned char *) NULL))
392     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
393   /*
394     Write colormap to file.
395   */
396   q=colormap;
397   if (image->depth <= 8)
398     for (i=0; i < (ssize_t) image->colors; i++)
399     {
400       *q++=(unsigned char) image->colormap[i].red;
401       *q++=(unsigned char) image->colormap[i].green;
402       *q++=(unsigned char) image->colormap[i].blue;
403     }
404   else
405     for (i=0; i < (ssize_t) image->colors; i++)
406     {
407       *q++=(unsigned char) ((size_t) image->colormap[i].red >> 8);
408       *q++=(unsigned char) image->colormap[i].red;
409       *q++=(unsigned char) ((size_t) image->colormap[i].green >> 8);
410       *q++=(unsigned char) image->colormap[i].green;
411       *q++=(unsigned char) ((size_t) image->colormap[i].blue >> 8);
412       *q++=(unsigned char) image->colormap[i].blue;
413     }
414   (void) WriteBlob(image,packet_size*image->colors,colormap);
415   colormap=(unsigned char *) RelinquishMagickMemory(colormap);
416   /*
417     Write image pixels to file.
418   */
419   for (y=0; y < (ssize_t) image->rows; y++)
420   {
421     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
422     if (p == (const PixelPacket *) NULL)
423       break;
424     indexes=GetVirtualIndexQueue(image);
425     q=pixels;
426     for (x=0; x < (ssize_t) image->columns; x++)
427     {
428       if (image->colors > 256)
429         *q++=(unsigned char) ((size_t) GetIndexPixelComponent(indexes+x) >> 8);
430       *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
431     }
432     (void) WriteBlob(image,(size_t) (q-pixels),pixels);
433   }
434   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
435   (void) CloseBlob(image);
436   return(status);
437 }