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 % Cristy %
17 % July 1992 %
18 % %
19 % %
20 % Copyright 1999-2019 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 % https://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 "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/colorspace-private.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/histogram.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/magick.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/quantum-private.h"
63 #include "MagickCore/static.h"
64 #include "MagickCore/statistic.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/module.h"
67
68 /*
69 Forward declarations.
70 */
71 static MagickBooleanType
72 WriteMAPImage(const ImageInfo *,Image *,ExceptionInfo *);
73
74 /*
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % %
77 % %
78 % %
79 % R e a d M A P I m a g e %
80 % %
81 % %
82 % %
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %
85 % ReadMAPImage() reads an image of raw RGB colormap and colormap index
86 % bytes and returns it. It allocates the memory necessary for the new Image
87 % structure and returns a pointer to the new image.
88 %
89 % The format of the ReadMAPImage method is:
90 %
91 % Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
92 %
93 % A description of each parameter follows:
94 %
95 % o image_info: the image info.
96 %
97 % o exception: return any errors or warnings in this structure.
98 %
99 */
ReadMAPImage(const ImageInfo * image_info,ExceptionInfo * exception)100 static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
101 {
102 Image
103 *image;
104
105 MagickBooleanType
106 status;
107
108 Quantum
109 index;
110
111 register ssize_t
112 x;
113
114 register Quantum
115 *q;
116
117 register ssize_t
118 i;
119
120 register unsigned char
121 *p;
122
123 size_t
124 depth,
125 packet_size,
126 quantum;
127
128 ssize_t
129 count,
130 y;
131
132 unsigned char
133 *colormap,
134 *pixels;
135
136 /*
137 Open image file.
138 */
139 assert(image_info != (const ImageInfo *) NULL);
140 assert(image_info->signature == MagickCoreSignature);
141 if (image_info->debug != MagickFalse)
142 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
143 image_info->filename);
144 assert(exception != (ExceptionInfo *) NULL);
145 assert(exception->signature == MagickCoreSignature);
146 image=AcquireImage(image_info,exception);
147 if ((image->columns == 0) || (image->rows == 0))
148 ThrowReaderException(OptionError,"MustSpecifyImageSize");
149 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
150 if (status == MagickFalse)
151 {
152 image=DestroyImageList(image);
153 return((Image *) NULL);
154 }
155 /*
156 Initialize image structure.
157 */
158 image->storage_class=PseudoClass;
159 status=AcquireImageColormap(image,(size_t)
160 (image->offset != 0 ? image->offset : 256),exception);
161 if (status == MagickFalse)
162 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
163 depth=GetImageQuantumDepth(image,MagickTrue);
164 packet_size=(size_t) (depth/8);
165 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
166 sizeof(*pixels));
167 packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
168 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
169 sizeof(*colormap));
170 if ((pixels == (unsigned char *) NULL) ||
171 (colormap == (unsigned char *) NULL))
172 {
173 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
174 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
175 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
176 }
177 /*
178 Read image colormap.
179 */
180 count=ReadBlob(image,packet_size*image->colors,colormap);
181 if (count != (ssize_t) (packet_size*image->colors))
182 {
183 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
184 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
185 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
186 }
187 p=colormap;
188 if (image->depth <= 8)
189 for (i=0; i < (ssize_t) image->colors; i++)
190 {
191 image->colormap[i].red=ScaleCharToQuantum(*p++);
192 image->colormap[i].green=ScaleCharToQuantum(*p++);
193 image->colormap[i].blue=ScaleCharToQuantum(*p++);
194 }
195 else
196 for (i=0; i < (ssize_t) image->colors; i++)
197 {
198 quantum=(*p++ << 8);
199 quantum|=(*p++);
200 image->colormap[i].red=(Quantum) quantum;
201 quantum=(*p++ << 8);
202 quantum|=(*p++);
203 image->colormap[i].green=(Quantum) quantum;
204 quantum=(*p++ << 8);
205 quantum|=(*p++);
206 image->colormap[i].blue=(Quantum) quantum;
207 }
208 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
209 if (image_info->ping != MagickFalse)
210 {
211 (void) CloseBlob(image);
212 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
213 return(GetFirstImageInList(image));
214 }
215 status=SetImageExtent(image,image->columns,image->rows,exception);
216 if (status == MagickFalse)
217 {
218 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
219 return(DestroyImageList(image));
220 }
221 /*
222 Read image pixels.
223 */
224 packet_size=(size_t) (depth/8);
225 for (y=0; y < (ssize_t) image->rows; y++)
226 {
227 p=pixels;
228 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
229 if (q == (Quantum *) NULL)
230 break;
231 count=ReadBlob(image,(size_t) packet_size*image->columns,pixels);
232 if (count != (ssize_t) (packet_size*image->columns))
233 break;
234 for (x=0; x < (ssize_t) image->columns; x++)
235 {
236 index=ConstrainColormapIndex(image,*p,exception);
237 p++;
238 if (image->colors > 256)
239 {
240 index=ConstrainColormapIndex(image,((size_t) index << 8)+(*p),
241 exception);
242 p++;
243 }
244 SetPixelIndex(image,index,q);
245 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
246 q+=GetPixelChannels(image);
247 }
248 if (SyncAuthenticPixels(image,exception) == MagickFalse)
249 break;
250 }
251 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
252 if (y < (ssize_t) image->rows)
253 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
254 image->filename);
255 (void) CloseBlob(image);
256 return(GetFirstImageInList(image));
257 }
258
259 /*
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 % %
262 % %
263 % %
264 % R e g i s t e r M A P I m a g e %
265 % %
266 % %
267 % %
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 %
270 % RegisterMAPImage() adds attributes for the MAP image format to
271 % the list of supported formats. The attributes include the image format
272 % tag, a method to read and/or write the format, whether the format
273 % supports the saving of more than one frame to the same file or blob,
274 % whether the format supports native in-memory I/O, and a brief
275 % description of the format.
276 %
277 % The format of the RegisterMAPImage method is:
278 %
279 % size_t RegisterMAPImage(void)
280 %
281 */
RegisterMAPImage(void)282 ModuleExport size_t RegisterMAPImage(void)
283 {
284 MagickInfo
285 *entry;
286
287 entry=AcquireMagickInfo("MAP","MAP","Colormap intensities and indices");
288 entry->decoder=(DecodeImageHandler *) ReadMAPImage;
289 entry->encoder=(EncodeImageHandler *) WriteMAPImage;
290 entry->flags^=CoderAdjoinFlag;
291 entry->format_type=ExplicitFormatType;
292 entry->flags|=CoderRawSupportFlag;
293 entry->flags|=CoderEndianSupportFlag;
294 (void) RegisterMagickInfo(entry);
295 return(MagickImageCoderSignature);
296 }
297
298 /*
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 % %
301 % %
302 % %
303 % U n r e g i s t e r M A P I m a g e %
304 % %
305 % %
306 % %
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 %
309 % UnregisterMAPImage() removes format registrations made by the
310 % MAP module from the list of supported formats.
311 %
312 % The format of the UnregisterMAPImage method is:
313 %
314 % UnregisterMAPImage(void)
315 %
316 */
UnregisterMAPImage(void)317 ModuleExport void UnregisterMAPImage(void)
318 {
319 (void) UnregisterMagickInfo("MAP");
320 }
321
322 /*
323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 % %
325 % %
326 % %
327 % W r i t e M A P I m a g e %
328 % %
329 % %
330 % %
331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 %
333 % WriteMAPImage() writes an image to a file as red, green, and blue
334 % colormap bytes followed by the colormap indexes.
335 %
336 % The format of the WriteMAPImage method is:
337 %
338 % MagickBooleanType WriteMAPImage(const ImageInfo *image_info,
339 % Image *image,ExceptionInfo *exception)
340 %
341 % A description of each parameter follows.
342 %
343 % o image_info: the image info.
344 %
345 % o image: The image.
346 %
347 % o exception: return any errors or warnings in this structure.
348 %
349 */
WriteMAPImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)350 static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image,
351 ExceptionInfo *exception)
352 {
353 MagickBooleanType
354 status;
355
356 register const Quantum
357 *p;
358
359 register ssize_t
360 i,
361 x;
362
363 register unsigned char
364 *q;
365
366 size_t
367 depth,
368 packet_size;
369
370 ssize_t
371 y;
372
373 unsigned char
374 *colormap,
375 *pixels;
376
377 /*
378 Open output image file.
379 */
380 assert(image_info != (const ImageInfo *) NULL);
381 assert(image_info->signature == MagickCoreSignature);
382 assert(image != (Image *) NULL);
383 assert(image->signature == MagickCoreSignature);
384 if (image->debug != MagickFalse)
385 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
386 assert(exception != (ExceptionInfo *) NULL);
387 assert(exception->signature == MagickCoreSignature);
388 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
389 if (status == MagickFalse)
390 return(status);
391 (void) TransformImageColorspace(image,sRGBColorspace,exception);
392 /*
393 Allocate colormap.
394 */
395 if (SetImageType(image,PaletteType,exception) == MagickFalse)
396 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
397 depth=GetImageQuantumDepth(image,MagickTrue);
398 packet_size=(size_t) (depth/8);
399 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
400 sizeof(*pixels));
401 packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
402 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size*
403 sizeof(*colormap));
404 if ((pixels == (unsigned char *) NULL) ||
405 (colormap == (unsigned char *) NULL))
406 {
407 if (colormap != (unsigned char *) NULL)
408 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
409 if (pixels != (unsigned char *) NULL)
410 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
411 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
412 }
413 /*
414 Write colormap to file.
415 */
416 q=colormap;
417 if (image->colors <= 256)
418 for (i=0; i < (ssize_t) image->colors; i++)
419 {
420 *q++=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
421 *q++=(unsigned char) ScaleQuantumToChar(image->colormap[i].green);
422 *q++=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
423 }
424 else
425 for (i=0; i < (ssize_t) image->colors; i++)
426 {
427 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].red) >> 8);
428 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].red) & 0xff);
429 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].green) >> 8);
430 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].green) &
431 0xff);
432 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].blue) >> 8);
433 *q++=(unsigned char) (ScaleQuantumToShort(image->colormap[i].blue) &
434 0xff);
435 }
436 (void) WriteBlob(image,packet_size*image->colors,colormap);
437 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
438 /*
439 Write image pixels to file.
440 */
441 for (y=0; y < (ssize_t) image->rows; y++)
442 {
443 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
444 if (p == (const Quantum *) NULL)
445 break;
446 q=pixels;
447 for (x=0; x < (ssize_t) image->columns; x++)
448 {
449 if (image->colors > 256)
450 *q++=(unsigned char) ((size_t) GetPixelIndex(image,p) >> 8);
451 *q++=(unsigned char) GetPixelIndex(image,p);
452 p+=GetPixelChannels(image);
453 }
454 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
455 }
456 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
457 (void) CloseBlob(image);
458 return(status);
459 }
460