]> granicus.if.org Git - imagemagick/blob - coders/mac.c
(no commit message)
[imagemagick] / coders / mac.c
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 %                                John Cristy                                  %
18 %                                 July 1992                                   %
19 %                                                                             %
20 %                                                                             %
21 %  Copyright 1999-2011 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 %    http://www.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 \f
40 /*
41   Include declarations.
42 */
43 #include "magick/studio.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/colormap.h"
48 #include "magick/colorspace.h"
49 #include "magick/exception.h"
50 #include "magick/exception-private.h"
51 #include "magick/image.h"
52 #include "magick/image-private.h"
53 #include "magick/list.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/monitor.h"
57 #include "magick/monitor-private.h"
58 #include "magick/quantum-private.h"
59 #include "magick/static.h"
60 #include "magick/string_.h"
61 #include "magick/module.h"
62 \f
63 /*
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %                                                                             %
66 %                                                                             %
67 %                                                                             %
68 %   R e a d M A C I m a g e                                                   %
69 %                                                                             %
70 %                                                                             %
71 %                                                                             %
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %
74 %  ReadMACImage() reads an MacPaint image file and returns it.  It
75 %  allocates the memory necessary for the new Image structure and returns a
76 %  pointer to the new image.
77 %
78 %  The format of the ReadMACImage method is:
79 %
80 %      Image *ReadMACImage(const ImageInfo *image_info,ExceptionInfo *exception)
81 %
82 %  A description of each parameter follows:
83 %
84 %    o image_info: the image info.
85 %
86 %    o exception: return any errors or warnings in this structure.
87 %
88 */
89 static Image *ReadMACImage(const ImageInfo *image_info,ExceptionInfo *exception)
90 {
91   Image
92     *image;
93
94   MagickBooleanType
95     status;
96
97   register IndexPacket
98     *indexes;
99
100   register PixelPacket
101     *q;
102
103   register ssize_t
104     x;
105
106   register unsigned char
107     *p;
108
109   size_t
110     length;
111
112   ssize_t
113     offset,
114     y;
115
116   unsigned char
117     count,
118     bit,
119     byte,
120     *pixels;
121
122   /*
123     Open image file.
124   */
125   assert(image_info != (const ImageInfo *) NULL);
126   assert(image_info->signature == MagickSignature);
127   if (image_info->debug != MagickFalse)
128     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
129       image_info->filename);
130   assert(exception != (ExceptionInfo *) NULL);
131   assert(exception->signature == MagickSignature);
132   image=AcquireImage(image_info);
133   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
134   if (status == MagickFalse)
135     {
136       image=DestroyImageList(image);
137       return((Image *) NULL);
138     }
139   /*
140     Read MAC X image.
141   */
142   length=ReadBlobLSBShort(image);
143   if ((length & 0xff) != 0)
144     ThrowReaderException(CorruptImageError,"CorruptImage");
145   for (x=0; x < (ssize_t) 638; x++)
146     if (ReadBlobByte(image) == EOF)
147       ThrowReaderException(CorruptImageError,"CorruptImage");
148   image->columns=576;
149   image->rows=720;
150   image->depth=1;
151   if (AcquireImageColormap(image,2) == MagickFalse)
152     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
153   if (image_info->ping != MagickFalse)
154     {
155       (void) CloseBlob(image);
156       return(GetFirstImageInList(image));
157     }
158   /*
159     Convert MAC raster image to pixel packets.
160   */
161   length=(image->columns+7)/8;
162   pixels=(unsigned char *) AcquireQuantumMemory(length+1,sizeof(*pixels));
163   if (pixels == (unsigned char *) NULL) 
164     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
165   p=pixels;
166   offset=0;
167   for (y=0; y < (ssize_t) image->rows; )
168   {
169     count=(unsigned char) ReadBlobByte(image);
170     if (EOFBlob(image) != MagickFalse)
171       break;
172     if ((count <= 0) || (count >= 128))
173       {
174         byte=(unsigned char) (~ReadBlobByte(image));
175         count=(~count)+2;
176         while (count != 0)
177         {
178           *p++=byte;
179           offset++;
180           count--;
181           if (offset >= (ssize_t) length)
182             {
183               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
184               if (q == (PixelPacket *) NULL)
185                 break;
186               indexes=GetAuthenticIndexQueue(image);
187               p=pixels;
188               bit=0;
189               byte=0;
190               for (x=0; x < (ssize_t) image->columns; x++)
191               {
192                 if (bit == 0)
193                   byte=(*p++);
194                 indexes[x]=(IndexPacket) ((byte & 0x80) != 0 ? 0x01 : 0x00);
195                 bit++;
196                 byte<<=1;
197                 if (bit == 8)
198                   bit=0;
199               }
200               if (SyncAuthenticPixels(image,exception) == MagickFalse)
201                 break;
202               offset=0;
203               p=pixels;
204               y++;
205             }
206         }
207         continue;
208       }
209     count++;
210     while (count != 0)
211     {
212       byte=(unsigned char) (~ReadBlobByte(image));
213       *p++=byte;
214       offset++;
215       count--;
216       if (offset >= (ssize_t) length)
217         {
218           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
219           if (q == (PixelPacket *) NULL)
220             break;
221           indexes=GetAuthenticIndexQueue(image);
222           p=pixels;
223           bit=0;
224           byte=0;
225           for (x=0; x < (ssize_t) image->columns; x++)
226           {
227             if (bit == 0)
228               byte=(*p++);
229             indexes[x]=(IndexPacket) ((byte & 0x80) != 0 ? 0x01 : 0x00);
230             bit++;
231             byte<<=1;
232             if (bit == 8)
233               bit=0;
234           }
235           if (SyncAuthenticPixels(image,exception) == MagickFalse)
236             break;
237           offset=0;
238           p=pixels;
239           y++;
240         }
241     }
242   }
243   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
244   (void) CloseBlob(image);
245   return(GetFirstImageInList(image));
246 }
247 \f
248 /*
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 %                                                                             %
251 %                                                                             %
252 %                                                                             %
253 %   R e g i s t e r M A C I m a g e                                           %
254 %                                                                             %
255 %                                                                             %
256 %                                                                             %
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 %
259 %  RegisterMACImage() adds attributes for the MAC X image format to the list
260 %  of supported formats.  The attributes include the image format tag, a
261 %  method to read and/or write the format, whether the format supports the
262 %  saving of more than one frame to the same file or blob, whether the format
263 %  supports native in-memory I/O, and a brief description of the format.
264 %
265 %  The format of the RegisterMACImage method is:
266 %
267 %      size_t RegisterMACImage(void)
268 %
269 */
270 ModuleExport size_t RegisterMACImage(void)
271 {
272   MagickInfo
273     *entry;
274
275   entry=SetMagickInfo("MAC");
276   entry->decoder=(DecodeImageHandler *) ReadMACImage;
277   entry->description=ConstantString("MAC Paint");
278   entry->module=ConstantString("MAC");
279   (void) RegisterMagickInfo(entry);
280   return(MagickImageCoderSignature);
281 }
282 \f
283 /*
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 %                                                                             %
286 %                                                                             %
287 %                                                                             %
288 %   U n r e g i s t e r M A C I m a g e                                       %
289 %                                                                             %
290 %                                                                             %
291 %                                                                             %
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 %
294 %  UnregisterMACImage() removes format registrations made by the
295 %  MAC module from the list of supported formats.
296 %
297 %  The format of the UnregisterMACImage method is:
298 %
299 %      UnregisterMACImage(void)
300 %
301 */
302 ModuleExport void UnregisterMACImage(void)
303 {
304   (void) UnregisterMagickInfo("MAC");
305 }