2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Group 3 Fax Image Format %
20 % Copyright 1999-2013 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/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/colormap.h"
47 #include "MagickCore/colorspace.h"
48 #include "MagickCore/colorspace-private.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/compress.h"
52 #include "MagickCore/image.h"
53 #include "MagickCore/image-private.h"
54 #include "MagickCore/list.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/monitor.h"
58 #include "MagickCore/monitor-private.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/module.h"
67 static MagickBooleanType
68 WriteFAXImage(const ImageInfo *,Image *,ExceptionInfo *);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % IsFAX() returns MagickTrue if the image format type, identified by the
82 % magick string, is FAX.
84 % The format of the IsFAX method is:
86 % MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
88 % A description of each parameter follows:
90 % o magick: compare image format pattern against these bytes.
92 % o length: Specifies the length of the magick string.
96 static MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
100 if (LocaleNCompare((char *) magick,"DFAX",4) == 0)
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % R e a d F A X I m a g e %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 % ReadFAXImage() reads a Group 3 FAX image file and returns it. It
117 % allocates the memory necessary for the new Image structure and returns a
118 % pointer to the new image.
120 % The format of the ReadFAXImage method is:
122 % Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
124 % A description of each parameter follows:
126 % o image_info: the image info.
128 % o exception: return any errors or warnings in this structure.
131 static Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
142 assert(image_info != (const ImageInfo *) NULL);
143 assert(image_info->signature == MagickSignature);
144 if (image_info->debug != MagickFalse)
145 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
146 image_info->filename);
147 assert(exception != (ExceptionInfo *) NULL);
148 assert(exception->signature == MagickSignature);
149 image=AcquireImage(image_info,exception);
150 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
151 if (status == MagickFalse)
153 image=DestroyImageList(image);
154 return((Image *) NULL);
157 Initialize image structure.
159 image->storage_class=PseudoClass;
160 if (image->columns == 0)
162 if (image->rows == 0)
165 if (AcquireImageColormap(image,2,exception) == MagickFalse)
166 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
170 image->colormap[0].red=QuantumRange;
171 image->colormap[0].green=QuantumRange;
172 image->colormap[0].blue=QuantumRange;
173 image->colormap[1].red=(Quantum) 0;
174 image->colormap[1].green=(Quantum) 0;
175 image->colormap[1].blue=(Quantum) 0;
176 if (image_info->ping != MagickFalse)
178 (void) CloseBlob(image);
179 return(GetFirstImageInList(image));
181 status=HuffmanDecodeImage(image,exception);
182 if (status == MagickFalse)
183 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
184 if (EOFBlob(image) != MagickFalse)
185 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
187 (void) CloseBlob(image);
188 return(GetFirstImageInList(image));
192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196 % R e g i s t e r F A X I m a g e %
200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 % RegisterFAXImage() adds attributes for the FAX image format to
203 % the list of supported formats. The attributes include the image format
204 % tag, a method to read and/or write the format, whether the format
205 % supports the saving of more than one frame to the same file or blob,
206 % whether the format supports native in-memory I/O, and a brief
207 % description of the format.
209 % The format of the RegisterFAXImage method is:
211 % size_t RegisterFAXImage(void)
214 ModuleExport size_t RegisterFAXImage(void)
222 "FAX machines use non-square pixels which are 1.5 times wider than\n"
223 "they are tall but computer displays use square pixels, therefore\n"
224 "FAX images may appear to be narrow unless they are explicitly\n"
225 "resized using a geometry of \"150x100%\".\n"
228 entry=SetMagickInfo("FAX");
229 entry->decoder=(DecodeImageHandler *) ReadFAXImage;
230 entry->encoder=(EncodeImageHandler *) WriteFAXImage;
231 entry->magick=(IsImageFormatHandler *) IsFAX;
232 entry->description=ConstantString("Group 3 FAX");
233 entry->note=ConstantString(Note);
234 entry->module=ConstantString("FAX");
235 (void) RegisterMagickInfo(entry);
236 entry=SetMagickInfo("G3");
237 entry->decoder=(DecodeImageHandler *) ReadFAXImage;
238 entry->encoder=(EncodeImageHandler *) WriteFAXImage;
239 entry->magick=(IsImageFormatHandler *) IsFAX;
240 entry->adjoin=MagickFalse;
241 entry->description=ConstantString("Group 3 FAX");
242 entry->module=ConstantString("FAX");
243 (void) RegisterMagickInfo(entry);
244 return(MagickImageCoderSignature);
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 % U n r e g i s t e r F A X I m a g e %
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 % UnregisterFAXImage() removes format registrations made by the
259 % FAX module from the list of supported formats.
261 % The format of the UnregisterFAXImage method is:
263 % UnregisterFAXImage(void)
266 ModuleExport void UnregisterFAXImage(void)
268 (void) UnregisterMagickInfo("FAX");
269 (void) UnregisterMagickInfo("G3");
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % W r i t e F A X I m a g e %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % WriteFAXImage() writes an image to a file in 1 dimensional Huffman encoded
286 % The format of the WriteFAXImage method is:
288 % MagickBooleanType WriteFAXImage(const ImageInfo *image_info,
289 % Image *image,ExceptionInfo *exception)
291 % A description of each parameter follows.
293 % o image_info: the image info.
295 % o image: The image.
297 % o exception: return any errors or warnings in this structure.
300 static MagickBooleanType WriteFAXImage(const ImageInfo *image_info,Image *image,
301 ExceptionInfo *exception)
313 Open output image file.
315 assert(image_info != (const ImageInfo *) NULL);
316 assert(image_info->signature == MagickSignature);
317 assert(image != (Image *) NULL);
318 assert(image->signature == MagickSignature);
319 if (image->debug != MagickFalse)
320 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
321 assert(exception != (ExceptionInfo *) NULL);
322 assert(exception->signature == MagickSignature);
323 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
324 if (status == MagickFalse)
326 write_info=CloneImageInfo(image_info);
327 (void) CopyMagickString(write_info->magick,"FAX",MaxTextExtent);
332 Convert MIFF to monochrome.
334 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
335 (void) TransformImageColorspace(image,sRGBColorspace,exception);
336 status=HuffmanEncodeImage(write_info,image,image,exception);
337 if (GetNextImageInList(image) == (Image *) NULL)
339 image=SyncNextImageInList(image);
340 status=SetImageProgress(image,SaveImagesTag,scene++,
341 GetImageListLength(image));
342 if (status == MagickFalse)
344 } while (write_info->adjoin != MagickFalse);
345 write_info=DestroyImageInfo(write_info);
346 (void) CloseBlob(image);