]> granicus.if.org Git - imagemagick/blob - coders/fax.c
(no commit message)
[imagemagick] / coders / fax.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            FFFFF   AAA   X   X                              %
7 %                            F      A   A   X X                               %
8 %                            FFF    AAAAA    X                                %
9 %                            F      A   A   X X                               %
10 %                            F      A   A  X   X                              %
11 %                                                                             %
12 %                                                                             %
13 %                   Read/Write Group 3 Fax Image Format                       %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2010 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/colormap.h"
46 #include "magick/colorspace.h"
47 #include "magick/exception.h"
48 #include "magick/exception-private.h"
49 #include "magick/compress.h"
50 #include "magick/image.h"
51 #include "magick/image-private.h"
52 #include "magick/list.h"
53 #include "magick/magick.h"
54 #include "magick/memory_.h"
55 #include "magick/monitor.h"
56 #include "magick/monitor-private.h"
57 #include "magick/quantum-private.h"
58 #include "magick/static.h"
59 #include "magick/string_.h"
60 #include "magick/module.h"
61 \f
62 /*
63   Forward declarations.
64 */
65 static MagickBooleanType
66   WriteFAXImage(const ImageInfo *,Image *);
67 \f
68 /*
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 %                                                                             %
71 %                                                                             %
72 %                                                                             %
73 %   I s F A X                                                                 %
74 %                                                                             %
75 %                                                                             %
76 %                                                                             %
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 %
79 %  IsFAX() returns MagickTrue if the image format type, identified by the
80 %  magick string, is FAX.
81 %
82 %  The format of the IsFAX method is:
83 %
84 %      MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
85 %
86 %  A description of each parameter follows:
87 %
88 %    o magick: compare image format pattern against these bytes.
89 %
90 %    o length: Specifies the length of the magick string.
91 %
92 %
93 */
94 static MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
95 {
96   if (length < 4)
97     return(MagickFalse);
98   if (LocaleNCompare((char *) magick,"DFAX",4) == 0)
99     return(MagickTrue);
100   return(MagickFalse);
101 }
102 \f
103 /*
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 %                                                                             %
106 %                                                                             %
107 %                                                                             %
108 %   R e a d F A X I m a g e                                                   %
109 %                                                                             %
110 %                                                                             %
111 %                                                                             %
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 %
114 %  ReadFAXImage() reads a Group 3 FAX image file and returns it.  It
115 %  allocates the memory necessary for the new Image structure and returns a
116 %  pointer to the new image.
117 %
118 %  The format of the ReadFAXImage method is:
119 %
120 %      Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
121 %
122 %  A description of each parameter follows:
123 %
124 %    o image_info: the image info.
125 %
126 %    o exception: return any errors or warnings in this structure.
127 %
128 */
129 static Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
130 {
131   Image
132     *image;
133
134   MagickBooleanType
135     status;
136
137   /*
138     Open image file.
139   */
140   assert(image_info != (const ImageInfo *) NULL);
141   assert(image_info->signature == MagickSignature);
142   if (image_info->debug != MagickFalse)
143     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
144       image_info->filename);
145   assert(exception != (ExceptionInfo *) NULL);
146   assert(exception->signature == MagickSignature);
147   image=AcquireImage(image_info);
148   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
149   if (status == MagickFalse)
150     {
151       image=DestroyImageList(image);
152       return((Image *) NULL);
153     }
154   /*
155     Initialize image structure.
156   */
157   image->storage_class=PseudoClass;
158   if (image->columns == 0)
159     image->columns=2592;
160   if (image->rows == 0)
161     image->rows=3508;
162   image->depth=8;
163   if (AcquireImageColormap(image,2) == MagickFalse)
164     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
165   /*
166     Monochrome colormap.
167   */
168   image->colormap[0].red=(Quantum) QuantumRange;
169   image->colormap[0].green=(Quantum) QuantumRange;
170   image->colormap[0].blue=(Quantum) QuantumRange;
171   image->colormap[1].red=(Quantum) 0;
172   image->colormap[1].green=(Quantum) 0;
173   image->colormap[1].blue=(Quantum) 0;
174   if (image_info->ping != MagickFalse)
175     {
176       (void) CloseBlob(image);
177       return(GetFirstImageInList(image));
178     }
179   status=HuffmanDecodeImage(image);
180   if (status == MagickFalse)
181     ThrowReaderException(CorruptImageError,"UnableToReadImageData");
182   if (EOFBlob(image) != MagickFalse)
183     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
184       image->filename);
185   (void) CloseBlob(image);
186   return(GetFirstImageInList(image));
187 }
188 \f
189 /*
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 %                                                                             %
192 %                                                                             %
193 %                                                                             %
194 %   R e g i s t e r F A X I m a g e                                           %
195 %                                                                             %
196 %                                                                             %
197 %                                                                             %
198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199 %
200 %  RegisterFAXImage() adds attributes for the FAX image format to
201 %  the list of supported formats.  The attributes include the image format
202 %  tag, a method to read and/or write the format, whether the format
203 %  supports the saving of more than one frame to the same file or blob,
204 %  whether the format supports native in-memory I/O, and a brief
205 %  description of the format.
206 %
207 %  The format of the RegisterFAXImage method is:
208 %
209 %      size_t RegisterFAXImage(void)
210 %
211 */
212 ModuleExport size_t RegisterFAXImage(void)
213 {
214   MagickInfo
215     *entry;
216
217   static const char
218     *Note=
219     {
220       "FAX machines use non-square pixels which are 1.5 times wider than\n"
221       "they are tall but computer displays use square pixels, therefore\n"
222       "FAX images may appear to be narrow unless they are explicitly\n"
223       "resized using a geometry of \"150x100%\".\n"
224     };
225
226   entry=SetMagickInfo("FAX");
227   entry->decoder=(DecodeImageHandler *) ReadFAXImage;
228   entry->encoder=(EncodeImageHandler *) WriteFAXImage;
229   entry->magick=(IsImageFormatHandler *) IsFAX;
230   entry->description=ConstantString("Group 3 FAX");
231   entry->note=ConstantString(Note);
232   entry->module=ConstantString("FAX");
233   (void) RegisterMagickInfo(entry);
234   entry=SetMagickInfo("G3");
235   entry->decoder=(DecodeImageHandler *) ReadFAXImage;
236   entry->encoder=(EncodeImageHandler *) WriteFAXImage;
237   entry->magick=(IsImageFormatHandler *) IsFAX;
238   entry->adjoin=MagickFalse;
239   entry->description=ConstantString("Group 3 FAX");
240   entry->module=ConstantString("FAX");
241   (void) RegisterMagickInfo(entry);
242   return(MagickImageCoderSignature);
243 }
244 \f
245 /*
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 %                                                                             %
248 %                                                                             %
249 %                                                                             %
250 %   U n r e g i s t e r F A X I m a g e                                       %
251 %                                                                             %
252 %                                                                             %
253 %                                                                             %
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 %
256 %  UnregisterFAXImage() removes format registrations made by the
257 %  FAX module from the list of supported formats.
258 %
259 %  The format of the UnregisterFAXImage method is:
260 %
261 %      UnregisterFAXImage(void)
262 %
263 */
264 ModuleExport void UnregisterFAXImage(void)
265 {
266   (void) UnregisterMagickInfo("FAX");
267   (void) UnregisterMagickInfo("G3");
268 }
269 \f
270 /*
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 %                                                                             %
273 %                                                                             %
274 %                                                                             %
275 %   W r i t e F A X I m a g e                                                 %
276 %                                                                             %
277 %                                                                             %
278 %                                                                             %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 %
281 %  WriteFAXImage() writes an image to a file in 1 dimensional Huffman encoded
282 %  format.
283 %
284 %  The format of the WriteFAXImage method is:
285 %
286 %      MagickBooleanType WriteFAXImage(const ImageInfo *image_info,Image *image)
287 %
288 %  A description of each parameter follows.
289 %
290 %    o image_info: the image info.
291 %
292 %    o image:  The image.
293 %
294 */
295 static MagickBooleanType WriteFAXImage(const ImageInfo *image_info,Image *image)
296 {
297   ImageInfo
298     *write_info;
299
300   MagickBooleanType
301     status;
302
303   MagickOffsetType
304     scene;
305
306   /*
307     Open output image file.
308   */
309   assert(image_info != (const ImageInfo *) NULL);
310   assert(image_info->signature == MagickSignature);
311   assert(image != (Image *) NULL);
312   assert(image->signature == MagickSignature);
313   if (image->debug != MagickFalse)
314     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
315   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
316   if (status == MagickFalse)
317     return(status);
318   write_info=CloneImageInfo(image_info);
319   (void) CopyMagickString(write_info->magick,"FAX",MaxTextExtent);
320   scene=0;
321   do
322   {
323     /*
324       Convert MIFF to monochrome.
325     */
326     if (image->colorspace != RGBColorspace)
327       (void) TransformImageColorspace(image,RGBColorspace);
328     status=HuffmanEncodeImage(write_info,image,image);
329     if (GetNextImageInList(image) == (Image *) NULL)
330       break;
331     image=SyncNextImageInList(image);
332     status=SetImageProgress(image,SaveImagesTag,scene++,
333       GetImageListLength(image));
334     if (status == MagickFalse)
335       break;
336   } while (write_info->adjoin != MagickFalse);
337   write_info=DestroyImageInfo(write_info);
338   (void) CloseBlob(image);
339   return(status);
340 }