]> granicus.if.org Git - imagemagick/blob - coders/art.c
(no commit message)
[imagemagick] / coders / art.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                             AAA   RRRR   TTTTT                              %
7 %                            A   A  R   R    T                                %
8 %                            AAAAA  RRRR     T                                %
9 %                            A   A  R R      T                                %
10 %                            A   A  R  R     T                                %
11 %                                                                             %
12 %                                                                             %
13 %                 Support PFS: 1st Publisher Clip Art Format                  %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 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 %    http://www.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 \f
39 /*
40   Include declarations.
41 */
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color-private.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   Forward declarations.
65 */
66 static MagickBooleanType
67   WriteARTImage(const ImageInfo *,Image *);
68 \f
69 /*
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %                                                                             %
72 %                                                                             %
73 %                                                                             %
74 %   R e a d A R T I m a g e                                                   %
75 %                                                                             %
76 %                                                                             %
77 %                                                                             %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %
80 %  ReadARTImage() reads an image of raw bits in LSB order and returns it.
81 %  It allocates the memory necessary for the new Image structure and returns
82 %  a pointer to the new image.
83 %
84 %  The format of the ReadARTImage method is:
85 %
86 %      Image *ReadARTImage(const ImageInfo *image_info,
87 %        ExceptionInfo *exception)
88 %
89 %  A description of each parameter follows:
90 %
91 %    o image_info: the image info.
92 %
93 %    o exception: return any errors or warnings in this structure.
94 %
95 */
96 static Image *ReadARTImage(const ImageInfo *image_info,ExceptionInfo *exception)
97 {
98   Image
99     *image;
100
101   QuantumInfo
102     *quantum_info;
103
104   QuantumType
105     quantum_type;
106
107   MagickBooleanType
108     status;
109
110   size_t
111     length;
112
113   ssize_t
114     count,
115     y;
116
117   unsigned char
118     *pixels;
119
120   /*
121     Open image file.
122   */
123   assert(image_info != (const ImageInfo *) NULL);
124   assert(image_info->signature == MagickSignature);
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 == MagickSignature);
130   image=AcquireImage(image_info);
131   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
132   if (status == MagickFalse)
133     {
134       image=DestroyImageList(image);
135       return((Image *) NULL);
136     }
137   image->depth=1;
138   image->endian=MSBEndian;
139   (void) ReadBlobLSBShort(image);
140   image->columns=(size_t) ReadBlobLSBShort(image);
141   (void) ReadBlobLSBShort(image);
142   image->rows=(size_t) ReadBlobLSBShort(image);
143   /*
144     Initialize image colormap.
145   */
146   if (AcquireImageColormap(image,2) == MagickFalse)
147     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
148   if (image_info->ping != MagickFalse)
149     {
150       (void) CloseBlob(image);
151       return(GetFirstImageInList(image));
152     }
153   /*
154     Convert bi-level image to pixel packets.
155   */
156   quantum_type=IndexQuantum;
157   quantum_info=AcquireQuantumInfo(image_info,image);
158   if (quantum_info == (QuantumInfo *) NULL)
159     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
160   pixels=GetQuantumPixels(quantum_info);
161   length=GetQuantumExtent(image,quantum_info,quantum_type);
162   for (y=0; y < (ssize_t) image->rows; y++)
163   {
164     register PixelPacket
165       *restrict q;
166
167     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
168     if (q == (PixelPacket *) NULL)
169       break;
170     count=ReadBlob(image,length,pixels);
171     if (count != (ssize_t) length)
172       ThrowReaderException(CorruptImageError,"UnableToReadImageData");
173     (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
174       quantum_type,pixels,exception);
175     count=ReadBlob(image,(size_t) (-(ssize_t) length) & 0x01,pixels);
176     if (SyncAuthenticPixels(image,exception) == MagickFalse)
177       break;
178     if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
179       break;
180   }
181   SetQuantumImageType(image,quantum_type);
182   quantum_info=DestroyQuantumInfo(quantum_info);
183   if (EOFBlob(image) != MagickFalse)
184     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
185       image->filename);
186   (void) CloseBlob(image);
187   return(GetFirstImageInList(image));
188 }
189 \f
190 /*
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 %                                                                             %
193 %                                                                             %
194 %                                                                             %
195 %   R e g i s t e r A R T I m a g e                                           %
196 %                                                                             %
197 %                                                                             %
198 %                                                                             %
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 %
201 %  RegisterARTImage() adds attributes for the ART image format to
202 %  the list of supported formats.  The attributes include the image format
203 %  tag, a method to read and/or write the format, whether the format
204 %  supports the saving of more than one frame to the same file or blob,
205 %  whether the format supports native in-memory I/O, and a brief
206 %  description of the format.
207 %
208 %  The format of the RegisterARTImage method is:
209 %
210 %      size_t RegisterARTImage(void)
211 %
212 */
213 ModuleExport size_t RegisterARTImage(void)
214 {
215   MagickInfo
216     *entry;
217
218   entry=SetMagickInfo("ART");
219   entry->decoder=(DecodeImageHandler *) ReadARTImage;
220   entry->encoder=(EncodeImageHandler *) WriteARTImage;
221   entry->raw=MagickTrue;
222   entry->adjoin=MagickFalse;
223   entry->description=ConstantString("PFS: 1st Publisher Clip Art");
224   entry->module=ConstantString("ART");
225   (void) RegisterMagickInfo(entry);
226   return(MagickImageCoderSignature);
227 }
228 \f
229 /*
230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231 %                                                                             %
232 %                                                                             %
233 %                                                                             %
234 %   U n r e g i s t e r A R T I m a g e                                       %
235 %                                                                             %
236 %                                                                             %
237 %                                                                             %
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 %
240 %  UnregisterARTImage() removes format registrations made by the
241 %  ART module from the list of supported formats.
242 %
243 %  The format of the UnregisterARTImage method is:
244 %
245 %      UnregisterARTImage(void)
246 %
247 */
248 ModuleExport void UnregisterARTImage(void)
249 {
250   (void) UnregisterMagickInfo("ART");
251 }
252 \f
253 /*
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 %                                                                             %
256 %                                                                             %
257 %                                                                             %
258 %   W r i t e A R T I m a g e                                                 %
259 %                                                                             %
260 %                                                                             %
261 %                                                                             %
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 %
264 %  WriteARTImage() writes an image of raw bits in LSB order to a file.
265 %
266 %  The format of the WriteARTImage method is:
267 %
268 %      MagickBooleanType WriteARTImage(const ImageInfo *image_info,Image *image)
269 %
270 %  A description of each parameter follows.
271 %
272 %    o image_info: the image info.
273 %
274 %    o image:  The image.
275 %
276 */
277 static MagickBooleanType WriteARTImage(const ImageInfo *image_info,Image *image)
278 {
279   MagickBooleanType
280     status;
281
282   QuantumInfo
283     *quantum_info;
284
285   register const PixelPacket
286     *p;
287
288   ssize_t
289     count;
290
291   size_t
292     length,
293     y;
294
295   unsigned char
296     *pixels;
297
298   /*
299     Open output image file.
300   */
301   assert(image_info != (const ImageInfo *) NULL);
302   assert(image_info->signature == MagickSignature);
303   assert(image != (Image *) NULL);
304   assert(image->signature == MagickSignature);
305   if (image->debug != MagickFalse)
306     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
307   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
308   if (status == MagickFalse)
309     return(status);
310   if ((image->columns > 65535UL) || (image->rows > 65535UL))
311     ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
312   image->endian=MSBEndian;
313   image->depth=1;
314   (void) WriteBlobLSBShort(image,0);
315   (void) WriteBlobLSBShort(image,(unsigned short) image->columns);
316   (void) WriteBlobLSBShort(image,0);
317   (void) WriteBlobLSBShort(image,(unsigned short) image->rows);
318   if (image->colorspace != RGBColorspace)
319     (void) TransformImageColorspace(image,RGBColorspace);
320   length=(image->columns+7)/8;
321   pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
322   if (pixels == (unsigned char *) NULL)
323     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
324   /*
325     Convert image to a bi-level image.
326   */
327   (void) SetImageType(image,BilevelType);
328   quantum_info=AcquireQuantumInfo(image_info,image);
329   for (y=0; y < (ssize_t) image->rows; y++)
330   {
331     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
332     if (p == (const PixelPacket *) NULL)
333       break;
334     (void) ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
335       GrayQuantum,pixels,&image->exception);
336     count=WriteBlob(image,length,pixels);
337     if (count != (ssize_t) length)
338       ThrowWriterException(CorruptImageError,"UnableToWriteImageData");
339     count=WriteBlob(image,(size_t) (-(ssize_t) length) & 0x01,pixels);
340     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
341       image->rows);
342     if (status == MagickFalse)
343       break;
344   }
345   quantum_info=DestroyQuantumInfo(quantum_info);
346   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
347   (void) CloseBlob(image);
348   return(MagickTrue);
349 }