2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14 % Read MacPaint Image Format %
21 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % https://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/colormap.h"
48 #include "MagickCore/colorspace.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/module.h"
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 % R e a d M A C I m a g e %
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % ReadMACImage() reads an MacPaint image file and returns it. It
76 % allocates the memory necessary for the new Image structure and returns a
77 % pointer to the new image.
79 % The format of the ReadMACImage method is:
81 % Image *ReadMACImage(const ImageInfo *image_info,ExceptionInfo *exception)
83 % A description of each parameter follows:
85 % o image_info: the image info.
87 % o exception: return any errors or warnings in this structure.
90 static Image *ReadMACImage(const ImageInfo *image_info,ExceptionInfo *exception)
104 register unsigned char
123 assert(image_info != (const ImageInfo *) NULL);
124 assert(image_info->signature == MagickCoreSignature);
125 if (image_info->debug != MagickFalse)
126 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
127 image_info->filename);
128 assert(exception != (ExceptionInfo *) NULL);
129 assert(exception->signature == MagickCoreSignature);
130 image=AcquireImage(image_info,exception);
131 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
132 if (status == MagickFalse)
134 image=DestroyImageList(image);
135 return((Image *) NULL);
140 length=ReadBlobLSBShort(image);
141 if ((length & 0xff) != 0)
142 ThrowReaderException(CorruptImageError,"CorruptImage");
143 for (x=0; x < (ssize_t) 638; x++)
144 if (ReadBlobByte(image) == EOF)
145 ThrowReaderException(CorruptImageError,"CorruptImage");
149 if (AcquireImageColormap(image,2,exception) == MagickFalse)
150 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
151 if (image_info->ping != MagickFalse)
153 (void) CloseBlob(image);
154 return(GetFirstImageInList(image));
156 status=SetImageExtent(image,image->columns,image->rows,exception);
157 if (status == MagickFalse)
158 return(DestroyImageList(image));
160 Convert MAC raster image to pixel packets.
162 length=(image->columns+7)/8;
163 pixels=(unsigned char *) AcquireQuantumMemory(length+1,sizeof(*pixels));
164 if (pixels == (unsigned char *) NULL)
165 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
168 for (y=0; y < (ssize_t) image->rows; )
170 count=(unsigned char) ReadBlobByte(image);
171 if (EOFBlob(image) != MagickFalse)
173 if ((count <= 0) || (count >= 128))
175 byte=(unsigned char) (~ReadBlobByte(image));
182 if (offset >= (ssize_t) length)
184 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
185 if (q == (Quantum *) NULL)
190 for (x=0; x < (ssize_t) image->columns; x++)
194 SetPixelIndex(image,((byte & 0x80) != 0 ? 0x01 : 0x00),q);
199 q+=GetPixelChannels(image);
201 if (SyncAuthenticPixels(image,exception) == MagickFalse)
213 byte=(unsigned char) (~ReadBlobByte(image));
217 if (offset >= (ssize_t) length)
219 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
220 if (q == (Quantum *) NULL)
225 for (x=0; x < (ssize_t) image->columns; x++)
229 SetPixelIndex(image,((byte & 0x80) != 0 ? 0x01 : 0x00),q);
234 q+=GetPixelChannels(image);
236 if (SyncAuthenticPixels(image,exception) == MagickFalse)
244 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
245 (void) SyncImage(image,exception);
246 (void) CloseBlob(image);
247 return(GetFirstImageInList(image));
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 % R e g i s t e r M A C I m a g e %
259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 % RegisterMACImage() adds attributes for the MAC X image format to the list
262 % of supported formats. The attributes include the image format tag, a
263 % method to read and/or write the format, whether the format supports the
264 % saving of more than one frame to the same file or blob, whether the format
265 % supports native in-memory I/O, and a brief description of the format.
267 % The format of the RegisterMACImage method is:
269 % size_t RegisterMACImage(void)
272 ModuleExport size_t RegisterMACImage(void)
277 entry=AcquireMagickInfo("MAC","MAC","MAC Paint");
278 entry->decoder=(DecodeImageHandler *) ReadMACImage;
279 (void) RegisterMagickInfo(entry);
280 return(MagickImageCoderSignature);
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 % U n r e g i s t e r M A C I m a g e %
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % UnregisterMACImage() removes format registrations made by the
295 % MAC module from the list of supported formats.
297 % The format of the UnregisterMACImage method is:
299 % UnregisterMACImage(void)
302 ModuleExport void UnregisterMACImage(void)
304 (void) UnregisterMagickInfo("MAC");