2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Group 3 Fax Image Format %
20 % Copyright 1999-2011 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 "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/colormap.h"
46 #include "MagickCore/colorspace.h"
47 #include "MagickCore/colorspace-private.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/compress.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/quantum-private.h"
59 #include "MagickCore/static.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/module.h"
66 static MagickBooleanType
67 WriteFAXImage(const ImageInfo *,Image *,ExceptionInfo *);
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % IsFAX() returns MagickTrue if the image format type, identified by the
81 % magick string, is FAX.
83 % The format of the IsFAX method is:
85 % MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
87 % A description of each parameter follows:
89 % o magick: compare image format pattern against these bytes.
91 % o length: Specifies the length of the magick string.
95 static MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
99 if (LocaleNCompare((char *) magick,"DFAX",4) == 0)
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 % R e a d F A X I m a g e %
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % ReadFAXImage() reads a Group 3 FAX image file and returns it. It
116 % allocates the memory necessary for the new Image structure and returns a
117 % pointer to the new image.
119 % The format of the ReadFAXImage method is:
121 % Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
123 % A description of each parameter follows:
125 % o image_info: the image info.
127 % o exception: return any errors or warnings in this structure.
130 static Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
141 assert(image_info != (const ImageInfo *) NULL);
142 assert(image_info->signature == MagickSignature);
143 if (image_info->debug != MagickFalse)
144 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
145 image_info->filename);
146 assert(exception != (ExceptionInfo *) NULL);
147 assert(exception->signature == MagickSignature);
148 image=AcquireImage(image_info,exception);
149 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
150 if (status == MagickFalse)
152 image=DestroyImageList(image);
153 return((Image *) NULL);
156 Initialize image structure.
158 image->storage_class=PseudoClass;
159 if (image->columns == 0)
161 if (image->rows == 0)
164 if (AcquireImageColormap(image,2,exception) == MagickFalse)
165 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
169 image->colormap[0].red=(Quantum) QuantumRange;
170 image->colormap[0].green=(Quantum) QuantumRange;
171 image->colormap[0].blue=(Quantum) QuantumRange;
172 image->colormap[1].red=(Quantum) 0;
173 image->colormap[1].green=(Quantum) 0;
174 image->colormap[1].blue=(Quantum) 0;
175 if (image_info->ping != MagickFalse)
177 (void) CloseBlob(image);
178 return(GetFirstImageInList(image));
180 status=HuffmanDecodeImage(image,exception);
181 if (status == MagickFalse)
182 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
183 if (EOFBlob(image) != MagickFalse)
184 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
186 (void) CloseBlob(image);
187 return(GetFirstImageInList(image));
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195 % R e g i s t e r F A X I m a g e %
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
201 % RegisterFAXImage() adds attributes for the FAX 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.
208 % The format of the RegisterFAXImage method is:
210 % size_t RegisterFAXImage(void)
213 ModuleExport size_t RegisterFAXImage(void)
221 "FAX machines use non-square pixels which are 1.5 times wider than\n"
222 "they are tall but computer displays use square pixels, therefore\n"
223 "FAX images may appear to be narrow unless they are explicitly\n"
224 "resized using a geometry of \"150x100%\".\n"
227 entry=SetMagickInfo("FAX");
228 entry->decoder=(DecodeImageHandler *) ReadFAXImage;
229 entry->encoder=(EncodeImageHandler *) WriteFAXImage;
230 entry->magick=(IsImageFormatHandler *) IsFAX;
231 entry->description=ConstantString("Group 3 FAX");
232 entry->note=ConstantString(Note);
233 entry->module=ConstantString("FAX");
234 (void) RegisterMagickInfo(entry);
235 entry=SetMagickInfo("G3");
236 entry->decoder=(DecodeImageHandler *) ReadFAXImage;
237 entry->encoder=(EncodeImageHandler *) WriteFAXImage;
238 entry->magick=(IsImageFormatHandler *) IsFAX;
239 entry->adjoin=MagickFalse;
240 entry->description=ConstantString("Group 3 FAX");
241 entry->module=ConstantString("FAX");
242 (void) RegisterMagickInfo(entry);
243 return(MagickImageCoderSignature);
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % U n r e g i s t e r F A X I m a g e %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % UnregisterFAXImage() removes format registrations made by the
258 % FAX module from the list of supported formats.
260 % The format of the UnregisterFAXImage method is:
262 % UnregisterFAXImage(void)
265 ModuleExport void UnregisterFAXImage(void)
267 (void) UnregisterMagickInfo("FAX");
268 (void) UnregisterMagickInfo("G3");
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 % W r i t e F A X I m a g e %
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 % WriteFAXImage() writes an image to a file in 1 dimensional Huffman encoded
285 % The format of the WriteFAXImage method is:
287 % MagickBooleanType WriteFAXImage(const ImageInfo *image_info,
288 % Image *image,ExceptionInfo *exception)
290 % A description of each parameter follows.
292 % o image_info: the image info.
294 % o image: The image.
296 % o exception: return any errors or warnings in this structure.
299 static MagickBooleanType WriteFAXImage(const ImageInfo *image_info,Image *image,
300 ExceptionInfo *exception)
312 Open output image file.
314 assert(image_info != (const ImageInfo *) NULL);
315 assert(image_info->signature == MagickSignature);
316 assert(image != (Image *) NULL);
317 assert(image->signature == MagickSignature);
318 if (image->debug != MagickFalse)
319 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
320 assert(exception != (ExceptionInfo *) NULL);
321 assert(exception->signature == MagickSignature);
322 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
323 if (status == MagickFalse)
325 write_info=CloneImageInfo(image_info);
326 (void) CopyMagickString(write_info->magick,"FAX",MaxTextExtent);
331 Convert MIFF to monochrome.
333 if (IsRGBColorspace(image->colorspace) == MagickFalse)
334 (void) TransformImageColorspace(image,RGBColorspace,exception);
335 status=HuffmanEncodeImage(write_info,image,image,exception);
336 if (GetNextImageInList(image) == (Image *) NULL)
338 image=SyncNextImageInList(image);
339 status=SetImageProgress(image,SaveImagesTag,scene++,
340 GetImageListLength(image));
341 if (status == MagickFalse)
343 } while (write_info->adjoin != MagickFalse);
344 write_info=DestroyImageInfo(write_info);
345 (void) CloseBlob(image);