2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Group 3 Fax Image Format %
20 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
65 static MagickBooleanType
66 WriteFAXImage(const ImageInfo *,Image *);
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 % IsFAX() returns MagickTrue if the image format type, identified by the
80 % magick string, is FAX.
82 % The format of the IsFAX method is:
84 % MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
86 % A description of each parameter follows:
88 % o magick: compare image format pattern against these bytes.
90 % o length: Specifies the length of the magick string.
94 static MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
98 if (LocaleNCompare((char *) magick,"DFAX",4) == 0)
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 % R e a d F A X I m a g e %
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
118 % The format of the ReadFAXImage method is:
120 % Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
122 % A description of each parameter follows:
124 % o image_info: the image info.
126 % o exception: return any errors or warnings in this structure.
129 static Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
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)
151 image=DestroyImageList(image);
152 return((Image *) NULL);
155 Initialize image structure.
157 image->storage_class=PseudoClass;
158 if (image->columns == 0)
160 if (image->rows == 0)
163 if (AcquireImageColormap(image,2) == MagickFalse)
164 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
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)
176 (void) CloseBlob(image);
177 return(GetFirstImageInList(image));
179 status=HuffmanDecodeImage(image);
180 if (status == MagickFalse)
181 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
182 if (EOFBlob(image) != MagickFalse)
183 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
185 (void) CloseBlob(image);
186 return(GetFirstImageInList(image));
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 % R e g i s t e r F A X I m a g e %
198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
207 % The format of the RegisterFAXImage method is:
209 % unsigned long RegisterFAXImage(void)
212 ModuleExport unsigned long RegisterFAXImage(void)
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"
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);
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % U n r e g i s t e r F A X I m a g e %
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 % UnregisterFAXImage() removes format registrations made by the
257 % FAX module from the list of supported formats.
259 % The format of the UnregisterFAXImage method is:
261 % UnregisterFAXImage(void)
264 ModuleExport void UnregisterFAXImage(void)
266 (void) UnregisterMagickInfo("FAX");
267 (void) UnregisterMagickInfo("G3");
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % W r i t e F A X I m a g e %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 % WriteFAXImage() writes an image to a file in 1 dimensional Huffman encoded
284 % The format of the WriteFAXImage method is:
286 % MagickBooleanType WriteFAXImage(const ImageInfo *image_info,Image *image)
288 % A description of each parameter follows.
290 % o image_info: the image info.
292 % o image: The image.
295 static MagickBooleanType WriteFAXImage(const ImageInfo *image_info,Image *image)
307 Open output image file.
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)
318 write_info=CloneImageInfo(image_info);
319 (void) CopyMagickString(write_info->magick,"FAX",MaxTextExtent);
324 Convert MIFF to monochrome.
326 if (image->colorspace != RGBColorspace)
327 (void) TransformImageColorspace(image,RGBColorspace);
328 status=HuffmanEncodeImage(write_info,image,image);
329 if (GetNextImageInList(image) == (Image *) NULL)
331 image=SyncNextImageInList(image);
332 status=SetImageProgress(image,SaveImagesTag,scene++,
333 GetImageListLength(image));
334 if (status == MagickFalse)
336 } while (write_info->adjoin != MagickFalse);
337 write_info=DestroyImageInfo(write_info);
338 (void) CloseBlob(image);