blob: 2445c2a3518c444a2538c8508dbb9ea0686c04ed [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
cristyd9ecd042012-06-17 18:26:12 +000043#include "MagickCore/attribute.h"
cristy4c08aed2011-07-01 19:47:50 +000044#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/colormap.h"
47#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000048#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000049#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"
cristy3ed852e2009-09-05 21:47:34 +000063
64/*
65 Forward declarations.
66*/
67static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000068 WriteFAXImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000069
70/*
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72% %
73% %
74% %
75% I s F A X %
76% %
77% %
78% %
79%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80%
81% IsFAX() returns MagickTrue if the image format type, identified by the
82% magick string, is FAX.
83%
84% The format of the IsFAX method is:
85%
86% MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
87%
88% A description of each parameter follows:
89%
90% o magick: compare image format pattern against these bytes.
91%
92% o length: Specifies the length of the magick string.
93%
94%
95*/
96static MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
97{
98 if (length < 4)
99 return(MagickFalse);
100 if (LocaleNCompare((char *) magick,"DFAX",4) == 0)
101 return(MagickTrue);
102 return(MagickFalse);
103}
104
105/*
106%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107% %
108% %
109% %
110% R e a d F A X I m a g e %
111% %
112% %
113% %
114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115%
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.
119%
120% The format of the ReadFAXImage method is:
121%
122% Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
123%
124% A description of each parameter follows:
125%
126% o image_info: the image info.
127%
128% o exception: return any errors or warnings in this structure.
129%
130*/
131static Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
132{
133 Image
134 *image;
135
136 MagickBooleanType
137 status;
138
139 /*
140 Open image file.
141 */
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);
cristy9950d572011-10-01 18:22:35 +0000149 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000150 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
151 if (status == MagickFalse)
152 {
153 image=DestroyImageList(image);
154 return((Image *) NULL);
155 }
156 /*
157 Initialize image structure.
158 */
159 image->storage_class=PseudoClass;
160 if (image->columns == 0)
161 image->columns=2592;
162 if (image->rows == 0)
163 image->rows=3508;
164 image->depth=8;
cristy018f07f2011-09-04 21:15:19 +0000165 if (AcquireImageColormap(image,2,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000166 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
167 /*
168 Monochrome colormap.
169 */
cristy6e963d82012-06-19 15:23:24 +0000170 image->colormap[0].red=QuantumRange;
171 image->colormap[0].green=QuantumRange;
172 image->colormap[0].blue=QuantumRange;
cristy3ed852e2009-09-05 21:47:34 +0000173 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)
177 {
178 (void) CloseBlob(image);
179 return(GetFirstImageInList(image));
180 }
cristy018f07f2011-09-04 21:15:19 +0000181 status=HuffmanDecodeImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000182 if (status == MagickFalse)
183 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
184 if (EOFBlob(image) != MagickFalse)
185 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
186 image->filename);
187 (void) CloseBlob(image);
188 return(GetFirstImageInList(image));
189}
190
191/*
192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193% %
194% %
195% %
196% R e g i s t e r F A X I m a g e %
197% %
198% %
199% %
200%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
201%
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.
208%
209% The format of the RegisterFAXImage method is:
210%
cristybb503372010-05-27 20:51:26 +0000211% size_t RegisterFAXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000212%
213*/
cristybb503372010-05-27 20:51:26 +0000214ModuleExport size_t RegisterFAXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000215{
216 MagickInfo
217 *entry;
218
219 static const char
220 *Note=
221 {
cristyc4a5dd22010-01-02 02:41:21 +0000222 "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"
cristy3ed852e2009-09-05 21:47:34 +0000226 };
227
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);
245}
246
247/*
248%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249% %
250% %
251% %
252% U n r e g i s t e r F A X I m a g e %
253% %
254% %
255% %
256%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257%
258% UnregisterFAXImage() removes format registrations made by the
259% FAX module from the list of supported formats.
260%
261% The format of the UnregisterFAXImage method is:
262%
263% UnregisterFAXImage(void)
264%
265*/
266ModuleExport void UnregisterFAXImage(void)
267{
268 (void) UnregisterMagickInfo("FAX");
269 (void) UnregisterMagickInfo("G3");
270}
271
272/*
273%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274% %
275% %
276% %
277% W r i t e F A X I m a g e %
278% %
279% %
280% %
281%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282%
283% WriteFAXImage() writes an image to a file in 1 dimensional Huffman encoded
284% format.
285%
286% The format of the WriteFAXImage method is:
287%
cristy1e178e72011-08-28 19:44:34 +0000288% MagickBooleanType WriteFAXImage(const ImageInfo *image_info,
289% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000290%
291% A description of each parameter follows.
292%
293% o image_info: the image info.
294%
295% o image: The image.
296%
cristy1e178e72011-08-28 19:44:34 +0000297% o exception: return any errors or warnings in this structure.
298%
cristy3ed852e2009-09-05 21:47:34 +0000299*/
cristy1e178e72011-08-28 19:44:34 +0000300static MagickBooleanType WriteFAXImage(const ImageInfo *image_info,Image *image,
301 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000302{
303 ImageInfo
304 *write_info;
305
306 MagickBooleanType
307 status;
308
309 MagickOffsetType
310 scene;
311
312 /*
313 Open output image file.
314 */
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);
cristy3a37efd2011-08-28 20:31:03 +0000321 assert(exception != (ExceptionInfo *) NULL);
322 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000323 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000324 if (status == MagickFalse)
325 return(status);
326 write_info=CloneImageInfo(image_info);
327 (void) CopyMagickString(write_info->magick,"FAX",MaxTextExtent);
328 scene=0;
329 do
330 {
331 /*
332 Convert MIFF to monochrome.
333 */
cristyd9ecd042012-06-17 18:26:12 +0000334 if ((IssRGBColorspace(image->colorspace) == MagickFalse) &&
335 (IsImageGray(image,exception) == MagickFalse))
cristy8d951092012-02-08 18:54:56 +0000336 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy018f07f2011-09-04 21:15:19 +0000337 status=HuffmanEncodeImage(write_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000338 if (GetNextImageInList(image) == (Image *) NULL)
339 break;
340 image=SyncNextImageInList(image);
341 status=SetImageProgress(image,SaveImagesTag,scene++,
342 GetImageListLength(image));
343 if (status == MagickFalse)
344 break;
345 } while (write_info->adjoin != MagickFalse);
346 write_info=DestroyImageInfo(write_info);
347 (void) CloseBlob(image);
348 return(status);
349}