2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Palm Pixmap. %
17 % Christopher R. Hawks %
20 % Copyright 1999-2004 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Based on pnmtopalm by Bill Janssen and ppmtobmp by Ian Goldberg.
44 #include "MagickCore/studio.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/colormap.h"
51 #include "MagickCore/colormap-private.h"
52 #include "MagickCore/color-private.h"
53 #include "MagickCore/colorspace.h"
54 #include "MagickCore/colorspace-private.h"
55 #include "MagickCore/constitute.h"
56 #include "MagickCore/exception.h"
57 #include "MagickCore/histogram.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/paint.h"
66 #include "MagickCore/pixel-accessor.h"
67 #include "MagickCore/property.h"
68 #include "MagickCore/quantize.h"
69 #include "MagickCore/quantum-private.h"
70 #include "MagickCore/static.h"
71 #include "MagickCore/string_.h"
72 #include "MagickCore/module.h"
73 #include "MagickCore/utility.h"
78 #define PALM_IS_COMPRESSED_FLAG 0x8000
79 #define PALM_HAS_COLORMAP_FLAG 0x4000
80 #define PALM_HAS_FOUR_BYTE_FIELD 0x0200
81 #define PALM_HAS_TRANSPARENCY_FLAG 0x2000
82 #define PALM_IS_INDIRECT 0x1000
83 #define PALM_IS_FOR_SCREEN 0x0800
84 #define PALM_IS_DIRECT_COLOR 0x0400
85 #define PALM_COMPRESSION_SCANLINE 0x00
86 #define PALM_COMPRESSION_RLE 0x01
87 #define PALM_COMPRESSION_NONE 0xFF
90 The 256 color system palette for Palm Computing Devices.
92 static const unsigned char
95 {255, 255,255}, {255, 204,255}, {255, 153,255}, {255, 102,255},
96 {255, 51,255}, {255, 0,255}, {255, 255,204}, {255, 204,204},
97 {255, 153,204}, {255, 102,204}, {255, 51,204}, {255, 0,204},
98 {255, 255,153}, {255, 204,153}, {255, 153,153}, {255, 102,153},
99 {255, 51,153}, {255, 0,153}, {204, 255,255}, {204, 204,255},
100 {204, 153,255}, {204, 102,255}, {204, 51,255}, {204, 0,255},
101 {204, 255,204}, {204, 204,204}, {204, 153,204}, {204, 102,204},
102 {204, 51,204}, {204, 0,204}, {204, 255,153}, {204, 204,153},
103 {204, 153,153}, {204, 102,153}, {204, 51,153}, {204, 0,153},
104 {153, 255,255}, {153, 204,255}, {153, 153,255}, {153, 102,255},
105 {153, 51,255}, {153, 0,255}, {153, 255,204}, {153, 204,204},
106 {153, 153,204}, {153, 102,204}, {153, 51,204}, {153, 0,204},
107 {153, 255,153}, {153, 204,153}, {153, 153,153}, {153, 102,153},
108 {153, 51,153}, {153, 0,153}, {102, 255,255}, {102, 204,255},
109 {102, 153,255}, {102, 102,255}, {102, 51,255}, {102, 0,255},
110 {102, 255,204}, {102, 204,204}, {102, 153,204}, {102, 102,204},
111 {102, 51,204}, {102, 0,204}, {102, 255,153}, {102, 204,153},
112 {102, 153,153}, {102, 102,153}, {102, 51,153}, {102, 0,153},
113 { 51, 255,255}, { 51, 204,255}, { 51, 153,255}, { 51, 102,255},
114 { 51, 51,255}, { 51, 0,255}, { 51, 255,204}, { 51, 204,204},
115 { 51, 153,204}, { 51, 102,204}, { 51, 51,204}, { 51, 0,204},
116 { 51, 255,153}, { 51, 204,153}, { 51, 153,153}, { 51, 102,153},
117 { 51, 51,153}, { 51, 0,153}, { 0, 255,255}, { 0, 204,255},
118 { 0, 153,255}, { 0, 102,255}, { 0, 51,255}, { 0, 0,255},
119 { 0, 255,204}, { 0, 204,204}, { 0, 153,204}, { 0, 102,204},
120 { 0, 51,204}, { 0, 0,204}, { 0, 255,153}, { 0, 204,153},
121 { 0, 153,153}, { 0, 102,153}, { 0, 51,153}, { 0, 0,153},
122 {255, 255,102}, {255, 204,102}, {255, 153,102}, {255, 102,102},
123 {255, 51,102}, {255, 0,102}, {255, 255, 51}, {255, 204, 51},
124 {255, 153, 51}, {255, 102, 51}, {255, 51, 51}, {255, 0, 51},
125 {255, 255, 0}, {255, 204, 0}, {255, 153, 0}, {255, 102, 0},
126 {255, 51, 0}, {255, 0, 0}, {204, 255,102}, {204, 204,102},
127 {204, 153,102}, {204, 102,102}, {204, 51,102}, {204, 0,102},
128 {204, 255, 51}, {204, 204, 51}, {204, 153, 51}, {204, 102, 51},
129 {204, 51, 51}, {204, 0, 51}, {204, 255, 0}, {204, 204, 0},
130 {204, 153, 0}, {204, 102, 0}, {204, 51, 0}, {204, 0, 0},
131 {153, 255,102}, {153, 204,102}, {153, 153,102}, {153, 102,102},
132 {153, 51,102}, {153, 0,102}, {153, 255, 51}, {153, 204, 51},
133 {153, 153, 51}, {153, 102, 51}, {153, 51, 51}, {153, 0, 51},
134 {153, 255, 0}, {153, 204, 0}, {153, 153, 0}, {153, 102, 0},
135 {153, 51, 0}, {153, 0, 0}, {102, 255,102}, {102, 204,102},
136 {102, 153,102}, {102, 102,102}, {102, 51,102}, {102, 0,102},
137 {102, 255, 51}, {102, 204, 51}, {102, 153, 51}, {102, 102, 51},
138 {102, 51, 51}, {102, 0, 51}, {102, 255, 0}, {102, 204, 0},
139 {102, 153, 0}, {102, 102, 0}, {102, 51, 0}, {102, 0, 0},
140 { 51, 255,102}, { 51, 204,102}, { 51, 153,102}, { 51, 102,102},
141 { 51, 51,102}, { 51, 0,102}, { 51, 255, 51}, { 51, 204, 51},
142 { 51, 153, 51}, { 51, 102, 51}, { 51, 51, 51}, { 51, 0, 51},
143 { 51, 255, 0}, { 51, 204, 0}, { 51, 153, 0}, { 51, 102, 0},
144 { 51, 51, 0}, { 51, 0, 0}, { 0, 255,102}, { 0, 204,102},
145 { 0, 153,102}, { 0, 102,102}, { 0, 51,102}, { 0, 0,102},
146 { 0, 255, 51}, { 0, 204, 51}, { 0, 153, 51}, { 0, 102, 51},
147 { 0, 51, 51}, { 0, 0, 51}, { 0, 255, 0}, { 0, 204, 0},
148 { 0, 153, 0}, { 0, 102, 0}, { 0, 51, 0}, { 17, 17, 17},
149 { 34, 34, 34}, { 68, 68, 68}, { 85, 85, 85}, {119, 119,119},
150 {136, 136,136}, {170, 170,170}, {187, 187,187}, {221, 221,221},
151 {238, 238,238}, {192, 192,192}, {128, 0, 0}, {128, 0,128},
152 { 0, 128, 0}, { 0, 128,128}, { 0, 0, 0}, { 0, 0, 0},
153 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
154 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
155 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
156 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
157 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
158 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}
162 Forward declarations.
164 static MagickBooleanType
165 WritePALMImage(const ImageInfo *,Image *,ExceptionInfo *);
168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 % F i n d C o l o r %
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 % FindColor() returns the index of the matching entry from PalmPalette for a
181 % The format of the FindColor method is:
183 % int FindColor(PixelInfo *pixel)
185 % A description of each parameter follows:
187 % o int: the index of the matching color or -1 if not found/
189 % o pixel: a pointer to the PixelInfo to be matched.
192 static ssize_t FindColor(PixelInfo *packet)
197 for (i=0; i < 256; i++)
198 if (ScaleQuantumToChar(ClampToQuantum(packet->red)) == PalmPalette[i][0] &&
199 ScaleQuantumToChar(ClampToQuantum(packet->green)) == PalmPalette[i][1] &&
200 ScaleQuantumToChar(ClampToQuantum(packet->blue)) == PalmPalette[i][2])
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210 % R e a d P A L M I m a g e %
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 % ReadPALMImage() reads an image of raw bites in LSB order and returns it. It
217 % allocates the memory necessary for the new Image structure and returns a
218 % pointer to the new image.
220 % The format of the ReadPALMImage method is:
222 % Image *ReadPALMImage(const ImageInfo *image_info,
223 % ExceptionInfo *exception)
225 % A description of each parameter follows:
227 % o image_info: Specifies a pointer to an ImageInfo structure.
229 % o exception: return any errors or warnings in this structure.
232 static Image *ReadPALMImage(const ImageInfo *image_info,
233 ExceptionInfo *exception)
291 assert(image_info != (const ImageInfo *) NULL);
292 assert(image_info->signature == MagickCoreSignature);
293 if (image_info->debug != MagickFalse)
294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
295 image_info->filename);
296 assert(exception != (ExceptionInfo *) NULL);
297 assert(exception->signature == MagickCoreSignature);
298 image=AcquireImage(image_info,exception);
299 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
300 if (status == MagickFalse)
302 (void) DestroyImageList(image);
303 return((Image *) NULL);
308 image->columns=ReadBlobMSBShort(image);
309 image->rows=ReadBlobMSBShort(image);
310 if (EOFBlob(image) != MagickFalse)
311 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
312 if ((image->columns == 0) || (image->rows == 0))
313 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
314 status=SetImageExtent(image,image->columns,image->rows,exception);
315 if (status == MagickFalse)
316 return(DestroyImageList(image));
317 bytes_per_row=ReadBlobMSBShort(image);
318 flags=ReadBlobMSBShort(image);
319 bits_per_pixel=(size_t) ReadBlobByte(image);
320 if ((bits_per_pixel != 1) && (bits_per_pixel != 2) &&
321 (bits_per_pixel != 4) && (bits_per_pixel != 8) &&
322 (bits_per_pixel != 16))
323 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
324 version=(size_t) ReadBlobByte(image);
325 if ((version != 0) && (version != 1) && (version != 2))
326 ThrowReaderException(CorruptImageError,"FileFormatVersionMismatch");
327 nextDepthOffset=(size_t) ReadBlobMSBShort(image);
328 transparentIndex=(size_t) ReadBlobByte(image);
329 compressionType=(size_t) ReadBlobByte(image);
330 if ((compressionType != PALM_COMPRESSION_NONE) &&
331 (compressionType != PALM_COMPRESSION_SCANLINE ) &&
332 (compressionType != PALM_COMPRESSION_RLE))
333 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
334 pad=ReadBlobMSBShort(image);
337 Initialize image colormap.
340 if ((bits_per_pixel < 16) &&
341 (AcquireImageColormap(image,one << bits_per_pixel,exception) == MagickFalse))
342 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
343 GetPixelInfo(image,&transpix);
344 if (bits_per_pixel == 16) /* Direct Color */
346 redbits=(size_t) ReadBlobByte(image); /* # of bits of red */
348 greenbits=(size_t) ReadBlobByte(image); /* # of bits of green */
350 bluebits=(size_t) ReadBlobByte(image); /* # of bits of blue */
352 ReadBlobByte(image); /* reserved by Palm */
353 ReadBlobByte(image); /* reserved by Palm */
354 transpix.red=(double) (QuantumRange*ReadBlobByte(image)/31);
355 transpix.green=(double) (QuantumRange*ReadBlobByte(image)/63);
356 transpix.blue=(double) (QuantumRange*ReadBlobByte(image)/31);
358 if (bits_per_pixel == 8)
363 if (flags & PALM_HAS_COLORMAP_FLAG)
365 count=(ssize_t) ReadBlobMSBShort(image);
366 for (i=0; i < (ssize_t) count; i++)
369 index=ConstrainColormapIndex(image,255-i,exception);
370 image->colormap[index].red=(MagickRealType)
371 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
372 image->colormap[index].green=(MagickRealType)
373 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
374 image->colormap[index].blue=(MagickRealType)
375 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
379 for (i=0; i < (ssize_t) (1L << bits_per_pixel); i++)
381 index=ConstrainColormapIndex(image,255-i,exception);
382 image->colormap[index].red=(MagickRealType)
383 ScaleCharToQuantum(PalmPalette[i][0]);
384 image->colormap[index].green=(MagickRealType)
385 ScaleCharToQuantum(PalmPalette[i][1]);
386 image->colormap[index].blue=(MagickRealType)
387 ScaleCharToQuantum(PalmPalette[i][2]);
390 if (flags & PALM_IS_COMPRESSED_FLAG)
391 size=ReadBlobMSBShort(image);
393 image->storage_class=DirectClass;
394 if (bits_per_pixel < 16)
396 image->storage_class=PseudoClass;
399 if (image_info->ping != MagickFalse)
401 (void) CloseBlob(image);
404 status=SetImageExtent(image,image->columns,image->rows,exception);
405 if (status == MagickFalse)
406 return(DestroyImageList(image));
407 one_row=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row,
408 2*image->columns),sizeof(*one_row));
409 if (one_row == (unsigned char *) NULL)
410 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
411 lastrow=(unsigned char *) NULL;
412 if (compressionType == PALM_COMPRESSION_SCANLINE)
414 lastrow=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row,
415 2*image->columns),sizeof(*lastrow));
416 if (lastrow == (unsigned char *) NULL)
417 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
419 mask=(size_t) (1U << bits_per_pixel)-1;
420 for (y=0; y < (ssize_t) image->rows; y++)
422 if ((flags & PALM_IS_COMPRESSED_FLAG) == 0)
424 /* TODO move out of loop! */
425 image->compression=NoCompression;
426 count=ReadBlob(image,bytes_per_row,one_row);
427 if (count != (ssize_t) bytes_per_row)
432 if (compressionType == PALM_COMPRESSION_RLE)
434 /* TODO move out of loop! */
435 image->compression=RLECompression;
436 for (i=0; i < (ssize_t) bytes_per_row; )
438 count=(ssize_t) ReadBlobByte(image);
441 count=MagickMin(count,(ssize_t) bytes_per_row-i);
442 byte=(size_t) ReadBlobByte(image);
443 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
448 if (compressionType == PALM_COMPRESSION_SCANLINE)
453 /* TODO move out of loop! */
455 image->compression=FaxCompression;
456 for (i=0; i < (ssize_t) bytes_per_row; i+=8)
458 count=(ssize_t) ReadBlobByte(image);
461 byte=(size_t) MagickMin((ssize_t) bytes_per_row-i,8);
462 for (bit=0; bit < byte; bit++)
464 if ((y == 0) || (count & (one << (7 - bit))))
465 one_row[i+bit]=(unsigned char) ReadBlobByte(image);
467 one_row[i+bit]=lastrow[i+bit];
470 (void) CopyMagickMemory(lastrow, one_row, bytes_per_row);
474 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
475 if (q == (Quantum *) NULL)
477 if (bits_per_pixel == 16)
479 if (image->columns > (2*bytes_per_row))
481 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
482 if (compressionType == PALM_COMPRESSION_SCANLINE)
483 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
484 ThrowReaderException(CorruptImageError,"CorruptImage");
486 for (x=0; x < (ssize_t) image->columns; x++)
488 color16=(*ptr++ << 8);
490 SetPixelRed(image,(Quantum) ((QuantumRange*((color16 >> 11) &
492 SetPixelGreen(image,(Quantum) ((QuantumRange*((color16 >> 5) &
494 SetPixelBlue(image,(Quantum) ((QuantumRange*((color16 >> 0) &
496 SetPixelAlpha(image,OpaqueAlpha,q);
497 q+=GetPixelChannels(image);
502 bit=8-bits_per_pixel;
503 for (x=0; x < (ssize_t) image->columns; x++)
505 if ((size_t) (ptr-one_row) >= bytes_per_row)
507 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
508 if (compressionType == PALM_COMPRESSION_SCANLINE)
509 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
510 ThrowReaderException(CorruptImageError,"CorruptImage");
512 index=(Quantum) (mask-(((*ptr) & (mask << bit)) >> bit));
513 SetPixelIndex(image,index,q);
514 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
520 bit=8-bits_per_pixel;
522 q+=GetPixelChannels(image);
524 if (SyncAuthenticPixels(image,exception) == MagickFalse)
527 if (image->previous == (Image *) NULL)
529 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
531 if (status == MagickFalse)
535 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
537 ssize_t index=ConstrainColormapIndex(image,(ssize_t) (mask-
538 transparentIndex),exception);
539 if (bits_per_pixel != 16)
540 transpix=image->colormap[index];
541 (void) TransparentPaintImage(image,&transpix,(Quantum) TransparentAlpha,
542 MagickFalse,exception);
544 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
545 if (compressionType == PALM_COMPRESSION_SCANLINE)
546 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
547 if (EOFBlob(image) != MagickFalse)
549 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
554 Proceed to next image. Copied from coders/pnm.c
556 if (image_info->number_scenes != 0)
557 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
559 if (nextDepthOffset != 0)
564 totalOffset+=(MagickOffsetType) (nextDepthOffset*4);
565 if (totalOffset >= (MagickOffsetType) GetBlobSize(image))
566 ThrowReaderException(CorruptImageError,"ImproperImageHeader")
568 seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET);
569 if (seekNextDepth != totalOffset)
570 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
572 Allocate next image structure. Copied from coders/pnm.c
574 AcquireNextImage(image_info,image,exception);
575 if (GetNextImageInList(image) == (Image *) NULL)
577 (void) DestroyImageList(image);
578 return((Image *) NULL);
580 image=SyncNextImageInList(image);
581 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
583 if (status == MagickFalse)
586 } while (nextDepthOffset != 0);
587 (void) CloseBlob(image);
588 return(GetFirstImageInList(image));
592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
596 % R e g i s t e r P A L M I m a g e %
600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
602 % RegisterPALMImage() adds properties for the PALM image format to the list of
603 % supported formats. The properties include the image format tag, a method to
604 % read and/or write the format, whether the format supports the saving of more
605 % than one frame to the same file or blob, whether the format supports native
606 % in-memory I/O, and a brief description of the format.
608 % The format of the RegisterPALMImage method is:
610 % size_t RegisterPALMImage(void)
613 ModuleExport size_t RegisterPALMImage(void)
618 entry=AcquireMagickInfo("PALM","PALM","Palm pixmap");
619 entry->decoder=(DecodeImageHandler *) ReadPALMImage;
620 entry->encoder=(EncodeImageHandler *) WritePALMImage;
621 entry->flags|=CoderDecoderSeekableStreamFlag;
622 entry->flags|=CoderEncoderSeekableStreamFlag;
623 (void) RegisterMagickInfo(entry);
624 return(MagickImageCoderSignature);
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632 % U n r e g i s t e r P A L M I m a g e %
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 % UnregisterPALMImage() removes format registrations made by the PALM
639 % module from the list of supported formats.
641 % The format of the UnregisterPALMImage method is:
643 % UnregisterPALMImage(void)
646 ModuleExport void UnregisterPALMImage(void)
648 (void) UnregisterMagickInfo("PALM");
652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
656 % W r i t e P A L M I m a g e %
660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
662 % WritePALMImage() writes an image of raw bits in LSB order to a file.
664 % The format of the WritePALMImage method is:
666 % MagickBooleanType WritePALMImage(const ImageInfo *image_info,
667 % Image *image,ExceptionInfo *exception)
669 % A description of each parameter follows.
671 % o image_info: Specifies a pointer to an ImageInfo structure.
673 % o image: A pointer to a Image structure.
675 % o exception: return any errors or warnings in this structure.
678 static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
679 Image *image,ExceptionInfo *exception)
701 register const Quantum
734 Open output image file.
736 assert(image_info != (const ImageInfo *) NULL);
737 assert(image_info->signature == MagickCoreSignature);
738 assert(image != (Image *) NULL);
739 assert(image->signature == MagickCoreSignature);
740 if (image->debug != MagickFalse)
741 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
742 assert(exception != (ExceptionInfo *) NULL);
743 assert(exception->signature == MagickCoreSignature);
744 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
745 if (status == MagickFalse)
747 quantize_info=AcquireQuantizeInfo(image_info);
760 (void) TransformImageColorspace(image,sRGBColorspace,exception);
761 count=GetNumberColors(image,NULL,exception);
762 for (bits_per_pixel=1; (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
763 if (image_info->depth > 100)
764 bits_per_pixel=image_info->depth-100;
765 if (bits_per_pixel < 16)
766 (void) TransformImageColorspace(image,image->colorspace,exception);
767 if (bits_per_pixel < 8)
769 (void) TransformImageColorspace(image,GRAYColorspace,exception);
770 (void) SetImageType(image,PaletteType,exception);
771 (void) SortColormapByIntensity(image,exception);
773 if ((image->storage_class == PseudoClass) && (image->colors > 256))
774 (void) SetImageStorageClass(image,DirectClass,exception);
775 if (image->storage_class == PseudoClass)
776 flags|=PALM_HAS_COLORMAP_FLAG;
778 flags|=PALM_IS_DIRECT_COLOR;
779 (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */
780 (void) WriteBlobMSBShort(image,(unsigned short) image->rows); /* height */
781 bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/
783 (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row);
784 if ((image_info->compression == RLECompression) ||
785 (image_info->compression == FaxCompression))
786 flags|=PALM_IS_COMPRESSED_FLAG;
787 (void) WriteBlobMSBShort(image, flags);
788 (void) WriteBlobByte(image,(unsigned char) bits_per_pixel);
789 if (bits_per_pixel > 1)
791 if ((image_info->compression == RLECompression) ||
792 (image_info->compression == FaxCompression))
794 (void) WriteBlobByte(image,version);
795 (void) WriteBlobMSBShort(image,0); /* nextDepthOffset */
796 (void) WriteBlobByte(image,(unsigned char) transparentIndex);
797 if (image_info->compression == RLECompression)
798 (void) WriteBlobByte(image,PALM_COMPRESSION_RLE);
800 if (image_info->compression == FaxCompression)
801 (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE);
803 (void) WriteBlobByte(image,PALM_COMPRESSION_NONE);
804 (void) WriteBlobMSBShort(image,0); /* reserved */
806 if (bits_per_pixel == 16)
808 (void) WriteBlobByte(image,5); /* # of bits of red */
809 (void) WriteBlobByte(image,6); /* # of bits of green */
810 (void) WriteBlobByte(image,5); /* # of bits of blue */
811 (void) WriteBlobByte(image,0); /* reserved by Palm */
812 (void) WriteBlobMSBLong(image,0); /* no transparent color, YET */
815 if (bits_per_pixel == 8)
817 if (flags & PALM_HAS_COLORMAP_FLAG) /* Write out colormap */
819 quantize_info->dither_method=IdentifyPaletteImage(image,exception)
820 == MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
821 quantize_info->number_colors=image->colors;
822 (void) QuantizeImage(quantize_info,image,exception);
823 (void) WriteBlobMSBShort(image,(unsigned short) image->colors);
824 for (count = 0; count < image->colors; count++)
826 (void) WriteBlobByte(image,(unsigned char) count);
827 (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
828 image->colormap[count].red)));
829 (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
830 image->colormap[count].green)));
831 (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
832 image->colormap[count].blue)));
836 else /* Map colors to Palm standard colormap */
841 affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette,
843 (void) TransformImageColorspace(affinity_image,
844 affinity_image->colorspace,exception);
845 (void) RemapImage(quantize_info,image,affinity_image,exception);
846 for (y=0; y < (ssize_t) image->rows; y++)
848 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
849 if (q == (Quantum *) NULL)
851 for (x=0; x < (ssize_t) image->columns; x++)
853 SetPixelIndex(image,(Quantum) FindColor(&image->colormap[(ssize_t)
854 GetPixelIndex(image,q)]),q);
855 q+=GetPixelChannels(image);
858 affinity_image=DestroyImage(affinity_image);
861 if (flags & PALM_IS_COMPRESSED_FLAG)
862 (void) WriteBlobMSBShort(image,0); /* fill in size later */
863 lastrow=(unsigned char *) NULL;
864 if (image_info->compression == FaxCompression)
865 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
867 /* TODO check whether memory really was acquired? */
868 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
870 if (one_row == (unsigned char *) NULL)
871 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
872 for (y=0; y < (ssize_t) image->rows; y++)
875 (void) ResetMagickMemory(ptr,0,bytes_per_row);
876 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
877 if (p == (const Quantum *) NULL)
879 if (bits_per_pixel == 16)
881 for (x=0; x < (ssize_t) image->columns; x++)
883 color16=(unsigned short) ((((31*(size_t) GetPixelRed(image,p))/
884 (size_t) QuantumRange) << 11) | (((63*(size_t)
885 GetPixelGreen(image,p))/(size_t) QuantumRange) << 5) |
886 ((31*(size_t) GetPixelBlue(image,p))/(size_t) QuantumRange));
887 if (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha)
889 transpix.red=(MagickRealType) GetPixelRed(image,p);
890 transpix.green=(MagickRealType) GetPixelGreen(image,p);
891 transpix.blue=(MagickRealType) GetPixelBlue(image,p);
892 transpix.alpha=(MagickRealType) GetPixelAlpha(image,p);
893 flags|=PALM_HAS_TRANSPARENCY_FLAG;
895 *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
896 *ptr++=(unsigned char) (color16 & 0xff);
897 p+=GetPixelChannels(image);
903 bit=(unsigned char) (8-bits_per_pixel);
904 for (x=0; x < (ssize_t) image->columns; x++)
906 if (bits_per_pixel >= 8)
907 color=(unsigned char) GetPixelIndex(image,p);
909 color=(unsigned char) (GetPixelIndex(image,p)*
910 ((one << bits_per_pixel)-1)/MagickMax(1*image->colors-1,1));
913 bit-=(unsigned char) bits_per_pixel;
918 bit=(unsigned char) (8-bits_per_pixel);
920 p+=GetPixelChannels(image);
922 if ((image->columns % (8/bits_per_pixel)) != 0)
925 if (image_info->compression == RLECompression)
928 while (x < (ssize_t) bytes_per_row)
932 while ((one_row[++x] == byte) && (count < 255) &&
933 (x < (ssize_t) bytes_per_row))
935 (void) WriteBlobByte(image,(unsigned char) count);
936 (void) WriteBlobByte(image,(unsigned char) byte);
940 if (image_info->compression == FaxCompression)
946 for (x = 0; x < (ssize_t) bytes_per_row; x += 8)
949 for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
951 if ((y == 0) || (lastrow[x + bit] != one_row[x + bit]))
953 byte |= (1 << (7 - bit));
954 *tptr++ = (char) one_row[x + bit];
957 (void) WriteBlobByte(image, byte);
958 (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
960 (void) CopyMagickMemory(lastrow,one_row,bytes_per_row);
963 (void) WriteBlob(image,bytes_per_row,one_row);
965 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
967 offset=SeekBlob(image,currentOffset+6,SEEK_SET);
968 (void) WriteBlobMSBShort(image,flags);
969 offset=SeekBlob(image,currentOffset+12,SEEK_SET);
970 (void) WriteBlobByte(image,(unsigned char) transparentIndex); /* trans index */
972 if (bits_per_pixel == 16)
974 offset=SeekBlob(image,currentOffset+20,SEEK_SET);
975 (void) WriteBlobByte(image,0); /* reserved by Palm */
976 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange));
977 (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange));
978 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange));
980 if (flags & PALM_IS_COMPRESSED_FLAG) /* fill in size now */
982 offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
983 (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
984 currentOffset-offset));
986 if (one_row != (unsigned char *) NULL)
987 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
988 if (lastrow != (unsigned char *) NULL)
989 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
990 if (GetNextImageInList(image) == (Image *) NULL)
992 /* padding to 4 byte word */
993 for (cc=(GetBlobSize(image)) % 4; cc > 0; cc--)
994 (void) WriteBlobByte(image,0);
995 /* write nextDepthOffset and return to end of image */
996 offset=SeekBlob(image,currentOffset+10,SEEK_SET);
997 nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4);
998 (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
999 currentOffset=(MagickOffsetType) GetBlobSize(image);
1000 offset=SeekBlob(image,currentOffset,SEEK_SET);
1001 image=SyncNextImageInList(image);
1002 status=SetImageProgress(image,SaveImagesTag,scene++,
1003 GetImageListLength(image));
1004 if (status == MagickFalse)
1006 } while (image_info->adjoin != MagickFalse);
1007 quantize_info=DestroyQuantizeInfo(quantize_info);
1008 (void) CloseBlob(image);