blob: 06de5e68869f448371a6c16891740648f0589a9c [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"
43#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/colormap.h"
46#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000047#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000048#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"
cristy3ed852e2009-09-05 21:47:34 +000062
63/*
64 Forward declarations.
65*/
66static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000067 WriteFAXImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000068
69/*
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71% %
72% %
73% %
74% I s F A X %
75% %
76% %
77% %
78%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79%
80% IsFAX() returns MagickTrue if the image format type, identified by the
81% magick string, is FAX.
82%
83% The format of the IsFAX method is:
84%
85% MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
86%
87% A description of each parameter follows:
88%
89% o magick: compare image format pattern against these bytes.
90%
91% o length: Specifies the length of the magick string.
92%
93%
94*/
95static MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
96{
97 if (length < 4)
98 return(MagickFalse);
99 if (LocaleNCompare((char *) magick,"DFAX",4) == 0)
100 return(MagickTrue);
101 return(MagickFalse);
102}
103
104/*
105%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106% %
107% %
108% %
109% R e a d F A X I m a g e %
110% %
111% %
112% %
113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114%
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.
118%
119% The format of the ReadFAXImage method is:
120%
121% Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
122%
123% A description of each parameter follows:
124%
125% o image_info: the image info.
126%
127% o exception: return any errors or warnings in this structure.
128%
129*/
130static Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
131{
132 Image
133 *image;
134
135 MagickBooleanType
136 status;
137
138 /*
139 Open image file.
140 */
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);
cristy9950d572011-10-01 18:22:35 +0000148 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000149 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
150 if (status == MagickFalse)
151 {
152 image=DestroyImageList(image);
153 return((Image *) NULL);
154 }
155 /*
156 Initialize image structure.
157 */
158 image->storage_class=PseudoClass;
159 if (image->columns == 0)
160 image->columns=2592;
161 if (image->rows == 0)
162 image->rows=3508;
163 image->depth=8;
cristy018f07f2011-09-04 21:15:19 +0000164 if (AcquireImageColormap(image,2,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000165 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
166 /*
167 Monochrome colormap.
168 */
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)
176 {
177 (void) CloseBlob(image);
178 return(GetFirstImageInList(image));
179 }
cristy018f07f2011-09-04 21:15:19 +0000180 status=HuffmanDecodeImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000181 if (status == MagickFalse)
182 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
183 if (EOFBlob(image) != MagickFalse)
184 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
185 image->filename);
186 (void) CloseBlob(image);
187 return(GetFirstImageInList(image));
188}
189
190/*
191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192% %
193% %
194% %
195% R e g i s t e r F A X I m a g e %
196% %
197% %
198% %
199%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200%
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.
207%
208% The format of the RegisterFAXImage method is:
209%
cristybb503372010-05-27 20:51:26 +0000210% size_t RegisterFAXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000211%
212*/
cristybb503372010-05-27 20:51:26 +0000213ModuleExport size_t RegisterFAXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000214{
215 MagickInfo
216 *entry;
217
218 static const char
219 *Note=
220 {
cristyc4a5dd22010-01-02 02:41:21 +0000221 "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"
cristy3ed852e2009-09-05 21:47:34 +0000225 };
226
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);
244}
245
246/*
247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248% %
249% %
250% %
251% U n r e g i s t e r F A X I m a g e %
252% %
253% %
254% %
255%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256%
257% UnregisterFAXImage() removes format registrations made by the
258% FAX module from the list of supported formats.
259%
260% The format of the UnregisterFAXImage method is:
261%
262% UnregisterFAXImage(void)
263%
264*/
265ModuleExport void UnregisterFAXImage(void)
266{
267 (void) UnregisterMagickInfo("FAX");
268 (void) UnregisterMagickInfo("G3");
269}
270
271/*
272%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273% %
274% %
275% %
276% W r i t e F A X I m a g e %
277% %
278% %
279% %
280%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281%
282% WriteFAXImage() writes an image to a file in 1 dimensional Huffman encoded
283% format.
284%
285% The format of the WriteFAXImage method is:
286%
cristy1e178e72011-08-28 19:44:34 +0000287% MagickBooleanType WriteFAXImage(const ImageInfo *image_info,
288% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000289%
290% A description of each parameter follows.
291%
292% o image_info: the image info.
293%
294% o image: The image.
295%
cristy1e178e72011-08-28 19:44:34 +0000296% o exception: return any errors or warnings in this structure.
297%
cristy3ed852e2009-09-05 21:47:34 +0000298*/
cristy1e178e72011-08-28 19:44:34 +0000299static MagickBooleanType WriteFAXImage(const ImageInfo *image_info,Image *image,
300 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000301{
302 ImageInfo
303 *write_info;
304
305 MagickBooleanType
306 status;
307
308 MagickOffsetType
309 scene;
310
311 /*
312 Open output image file.
313 */
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);
cristy3a37efd2011-08-28 20:31:03 +0000320 assert(exception != (ExceptionInfo *) NULL);
321 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000322 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000323 if (status == MagickFalse)
324 return(status);
325 write_info=CloneImageInfo(image_info);
326 (void) CopyMagickString(write_info->magick,"FAX",MaxTextExtent);
327 scene=0;
328 do
329 {
330 /*
331 Convert MIFF to monochrome.
332 */
cristy510d06a2011-07-06 23:43:54 +0000333 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +0000334 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy018f07f2011-09-04 21:15:19 +0000335 status=HuffmanEncodeImage(write_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000336 if (GetNextImageInList(image) == (Image *) NULL)
337 break;
338 image=SyncNextImageInList(image);
339 status=SetImageProgress(image,SaveImagesTag,scene++,
340 GetImageListLength(image));
341 if (status == MagickFalse)
342 break;
343 } while (write_info->adjoin != MagickFalse);
344 write_info=DestroyImageInfo(write_info);
345 (void) CloseBlob(image);
346 return(status);
347}