1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                                                                             %
7 %                            M   M   AAA    CCCC                              %
8 %                            MM MM  A   A  C                                  %
9 %                            M M M  AAAAA  C                                  %
10 %                            M   M  A   A  C                                  %
11 %                            M   M  A   A   CCCC                              %
12 %                                                                             %
13 %                                                                             %
14 %                         Read MacPaint Image Format                          %
15 %                                                                             %
16 %                              Software Design                                %
17 %                                   Cristy                                    %
18 %                                 July 1992                                   %
19 %                                                                             %
20 %                                                                             %
21 %  Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization      %
22 %  dedicated to making software imaging solutions freely available.           %
23 %                                                                             %
24 %  You may not use this file except in compliance with the License.  You may  %
25 %  obtain a copy of the License at                                            %
26 %                                                                             %
27 %    https://imagemagick.org/script/license.php                               %
28 %                                                                             %
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.                                             %
34 %                                                                             %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 %
38 */
39 
40 /*
41   Include declarations.
42 */
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"
63 
64 /*
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %                                                                             %
67 %                                                                             %
68 %                                                                             %
69 %   R e a d M A C I m a g e                                                   %
70 %                                                                             %
71 %                                                                             %
72 %                                                                             %
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 %
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.
78 %
79 %  The format of the ReadMACImage method is:
80 %
81 %      Image *ReadMACImage(const ImageInfo *image_info,ExceptionInfo *exception)
82 %
83 %  A description of each parameter follows:
84 %
85 %    o image_info: the image info.
86 %
87 %    o exception: return any errors or warnings in this structure.
88 %
89 */
ReadMACImage(const ImageInfo * image_info,ExceptionInfo * exception)90 static Image *ReadMACImage(const ImageInfo *image_info,ExceptionInfo *exception)
91 {
92   Image
93     *image;
94 
95   MagickBooleanType
96     status;
97 
98   Quantum
99     *q;
100 
101   ssize_t
102     x;
103 
104   unsigned char
105     *p;
106 
107   size_t
108     length;
109 
110   ssize_t
111     offset,
112     y;
113 
114   unsigned char
115     count,
116     bit,
117     byte,
118     *pixels;
119 
120   /*
121     Open image file.
122   */
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)
133     {
134       image=DestroyImageList(image);
135       return((Image *) NULL);
136     }
137   /*
138     Read MAC X image.
139   */
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");
146   image->columns=576;
147   image->rows=720;
148   image->depth=1;
149   if (AcquireImageColormap(image,2,exception) == MagickFalse)
150     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
151   if (image_info->ping != MagickFalse)
152     {
153       (void) CloseBlob(image);
154       return(GetFirstImageInList(image));
155     }
156   status=SetImageExtent(image,image->columns,image->rows,exception);
157   if (status == MagickFalse)
158     return(DestroyImageList(image));
159   status=ResetImagePixels(image,exception);
160   if (status == MagickFalse)
161     return(DestroyImageList(image));
162   /*
163     Convert MAC raster image to pixel packets.
164   */
165   length=(image->columns+7)/8;
166   pixels=(unsigned char *) AcquireQuantumMemory(length+257,sizeof(*pixels));
167   if (pixels == (unsigned char *) NULL)
168     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
169   (void) memset(pixels,0,(length+257)*sizeof(*pixels));
170   p=pixels;
171   offset=0;
172   for (y=0; y < (ssize_t) image->rows; )
173   {
174     count=(unsigned char) ReadBlobByte(image);
175     if (EOFBlob(image) != MagickFalse)
176       break;
177     if ((count <= 0) || (count >= 128))
178       {
179         byte=(unsigned char) (~ReadBlobByte(image));
180         count=(~count)+2;
181         while (count != 0)
182         {
183           *p++=byte;
184           offset++;
185           count--;
186           if (offset >= (ssize_t) length)
187             {
188               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
189               if (q == (Quantum *) NULL)
190                 break;
191               p=pixels;
192               bit=0;
193               byte=0;
194               for (x=0; x < (ssize_t) image->columns; x++)
195               {
196                 if (bit == 0)
197                   byte=(*p++);
198                 SetPixelIndex(image,((byte & 0x80) != 0 ? 0x01 : 0x00),q);
199                 bit++;
200                 byte<<=1;
201                 if (bit == 8)
202                   bit=0;
203                 q+=GetPixelChannels(image);
204               }
205               if (SyncAuthenticPixels(image,exception) == MagickFalse)
206                 break;
207               offset=0;
208               p=pixels;
209               y++;
210             }
211         }
212         continue;
213       }
214     count++;
215     while (count != 0)
216     {
217       byte=(unsigned char) (~ReadBlobByte(image));
218       *p++=byte;
219       offset++;
220       count--;
221       if (offset >= (ssize_t) length)
222         {
223           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
224           if (q == (Quantum *) NULL)
225             break;
226           p=pixels;
227           bit=0;
228           byte=0;
229           for (x=0; x < (ssize_t) image->columns; x++)
230           {
231             if (bit == 0)
232               byte=(*p++);
233             SetPixelIndex(image,((byte & 0x80) != 0 ? 0x01 : 0x00),q);
234             bit++;
235             byte<<=1;
236             if (bit == 8)
237               bit=0;
238             q+=GetPixelChannels(image);
239           }
240           if (SyncAuthenticPixels(image,exception) == MagickFalse)
241             break;
242           offset=0;
243           p=pixels;
244           y++;
245         }
246     }
247   }
248   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
249   (void) SyncImage(image,exception);
250   (void) CloseBlob(image);
251   return(GetFirstImageInList(image));
252 }
253 
254 /*
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 %                                                                             %
257 %                                                                             %
258 %                                                                             %
259 %   R e g i s t e r M A C I m a g e                                           %
260 %                                                                             %
261 %                                                                             %
262 %                                                                             %
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 %
265 %  RegisterMACImage() adds attributes for the MAC X image format to the list
266 %  of supported formats.  The attributes include the image format tag, a
267 %  method to read and/or write the format, whether the format supports the
268 %  saving of more than one frame to the same file or blob, whether the format
269 %  supports native in-memory I/O, and a brief description of the format.
270 %
271 %  The format of the RegisterMACImage method is:
272 %
273 %      size_t RegisterMACImage(void)
274 %
275 */
RegisterMACImage(void)276 ModuleExport size_t RegisterMACImage(void)
277 {
278   MagickInfo
279     *entry;
280 
281   entry=AcquireMagickInfo("MAC","MAC","MAC Paint");
282   entry->decoder=(DecodeImageHandler *) ReadMACImage;
283   (void) RegisterMagickInfo(entry);
284   return(MagickImageCoderSignature);
285 }
286 
287 /*
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 %                                                                             %
290 %                                                                             %
291 %                                                                             %
292 %   U n r e g i s t e r M A C I m a g e                                       %
293 %                                                                             %
294 %                                                                             %
295 %                                                                             %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 %
298 %  UnregisterMACImage() removes format registrations made by the
299 %  MAC module from the list of supported formats.
300 %
301 %  The format of the UnregisterMACImage method is:
302 %
303 %      UnregisterMACImage(void)
304 %
305 */
UnregisterMACImage(void)306 ModuleExport void UnregisterMACImage(void)
307 {
308   (void) UnregisterMagickInfo("MAC");
309 }
310