root/ImageMagick/trunk/coders/fax.c

Revision 1, 13.1 KB (checked in by cristy, 3 months ago)


Line 
1/*
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%                                                                             %
20%  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
21%  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*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/colorspace.h"
46#include "magick/exception.h"
47#include "magick/exception-private.h"
48#include "magick/compress.h"
49#include "magick/image.h"
50#include "magick/image-private.h"
51#include "magick/list.h"
52#include "magick/magick.h"
53#include "magick/memory_.h"
54#include "magick/monitor.h"
55#include "magick/monitor-private.h"
56#include "magick/quantum-private.h"
57#include "magick/static.h"
58#include "magick/string_.h"
59#include "magick/module.h"
60
61/*
62  Forward declarations.
63*/
64static MagickBooleanType
65  WriteFAXImage(const ImageInfo *,Image *);
66
67/*
68%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69%                                                                             %
70%                                                                             %
71%                                                                             %
72%   I s F A X                                                                 %
73%                                                                             %
74%                                                                             %
75%                                                                             %
76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77%
78%  IsFAX() returns MagickTrue if the image format type, identified by the
79%  magick string, is FAX.
80%
81%  The format of the IsFAX method is:
82%
83%      MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
84%
85%  A description of each parameter follows:
86%
87%    o magick: compare image format pattern against these bytes.
88%
89%    o length: Specifies the length of the magick string.
90%
91%
92*/
93static MagickBooleanType IsFAX(const unsigned char *magick,const size_t length)
94{
95  if (length < 4)
96    return(MagickFalse);
97  if (LocaleNCompare((char *) magick,"DFAX",4) == 0)
98    return(MagickTrue);
99  return(MagickFalse);
100}
101
102/*
103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104%                                                                             %
105%                                                                             %
106%                                                                             %
107%   R e a d F A X I m a g e                                                   %
108%                                                                             %
109%                                                                             %
110%                                                                             %
111%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112%
113%  ReadFAXImage() reads a Group 3 FAX image file and returns it.  It
114%  allocates the memory necessary for the new Image structure and returns a
115%  pointer to the new image.
116%
117%  The format of the ReadFAXImage method is:
118%
119%      Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
120%
121%  A description of each parameter follows:
122%
123%    o image_info: the image info.
124%
125%    o exception: return any errors or warnings in this structure.
126%
127*/
128static Image *ReadFAXImage(const ImageInfo *image_info,ExceptionInfo *exception)
129{
130  Image
131    *image;
132
133  MagickBooleanType
134    status;
135
136  /*
137    Open image file.
138  */
139  assert(image_info != (const ImageInfo *) NULL);
140  assert(image_info->signature == MagickSignature);
141  if (image_info->debug != MagickFalse)
142    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
143      image_info->filename);
144  assert(exception != (ExceptionInfo *) NULL);
145  assert(exception->signature == MagickSignature);
146  image=AcquireImage(image_info);
147  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
148  if (status == MagickFalse)
149    {
150      image=DestroyImageList(image);
151      return((Image *) NULL);
152    }
153  /*
154    Initialize image structure.
155  */
156  image->storage_class=PseudoClass;
157  if (image->columns == 0)
158    image->columns=2592;
159  if (image->rows == 0)
160    image->rows=3508;
161  image->depth=8;
162  if (AcquireImageColormap(image,2) == MagickFalse)
163    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
164  /*
165    Monochrome colormap.
166  */
167  image->colormap[0].red=(Quantum) QuantumRange;
168  image->colormap[0].green=(Quantum) QuantumRange;
169  image->colormap[0].blue=(Quantum) QuantumRange;
170  image->colormap[1].red=(Quantum) 0;
171  image->colormap[1].green=(Quantum) 0;
172  image->colormap[1].blue=(Quantum) 0;
173  if (image_info->ping != MagickFalse)
174    {
175      (void) CloseBlob(image);
176      return(GetFirstImageInList(image));
177    }
178  status=HuffmanDecodeImage(image);
179  if (status == MagickFalse)
180    ThrowReaderException(CorruptImageError,"UnableToReadImageData");
181  if (EOFBlob(image) != MagickFalse)
182    ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
183      image->filename);
184  (void) CloseBlob(image);
185  return(GetFirstImageInList(image));
186}
187
188/*
189%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190%                                                                             %
191%                                                                             %
192%                                                                             %
193%   R e g i s t e r F A X I m a g e                                           %
194%                                                                             %
195%                                                                             %
196%                                                                             %
197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198%
199%  RegisterFAXImage() adds attributes for the FAX image format to
200%  the list of supported formats.  The attributes include the image format
201%  tag, a method to read and/or write the format, whether the format
202%  supports the saving of more than one frame to the same file or blob,
203%  whether the format supports native in-memory I/O, and a brief
204%  description of the format.
205%
206%  The format of the RegisterFAXImage method is:
207%
208%      unsigned long RegisterFAXImage(void)
209%
210*/
211ModuleExport unsigned long RegisterFAXImage(void)
212{
213  MagickInfo
214    *entry;
215
216  static const char
217    *Note=
218    {
219      "FAX machines use non-square pixels which are 1.5 times wider than they\n"
220      "are tall but computer displays use square pixels, therefore FAX images\n"
221      "may appear to be narrow unless they are explicitly resized using a\n"
222      "geometry of \"150x100%\".\n"
223    };
224
225  entry=SetMagickInfo("FAX");
226  entry->decoder=(DecodeImageHandler *) ReadFAXImage;
227  entry->encoder=(EncodeImageHandler *) WriteFAXImage;
228  entry->magick=(IsImageFormatHandler *) IsFAX;
229  entry->description=ConstantString("Group 3 FAX");
230  entry->note=ConstantString(Note);
231  entry->module=ConstantString("FAX");
232  (void) RegisterMagickInfo(entry);
233  entry=SetMagickInfo("G3");
234  entry->decoder=(DecodeImageHandler *) ReadFAXImage;
235  entry->encoder=(EncodeImageHandler *) WriteFAXImage;
236  entry->magick=(IsImageFormatHandler *) IsFAX;
237  entry->adjoin=MagickFalse;
238  entry->description=ConstantString("Group 3 FAX");
239  entry->module=ConstantString("FAX");
240  (void) RegisterMagickInfo(entry);
241  return(MagickImageCoderSignature);
242}
243
244/*
245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246%                                                                             %
247%                                                                             %
248%                                                                             %
249%   U n r e g i s t e r F A X I m a g e                                       %
250%                                                                             %
251%                                                                             %
252%                                                                             %
253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254%
255%  UnregisterFAXImage() removes format registrations made by the
256%  FAX module from the list of supported formats.
257%
258%  The format of the UnregisterFAXImage method is:
259%
260%      UnregisterFAXImage(void)
261%
262*/
263ModuleExport void UnregisterFAXImage(void)
264{
265  (void) UnregisterMagickInfo("FAX");
266  (void) UnregisterMagickInfo("G3");
267}
268
269/*
270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271%                                                                             %
272%                                                                             %
273%                                                                             %
274%   W r i t e F A X I m a g e                                                 %
275%                                                                             %
276%                                                                             %
277%                                                                             %
278%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279%
280%  WriteFAXImage() writes an image to a file in 1 dimensional Huffman encoded
281%  format.
282%
283%  The format of the WriteFAXImage method is:
284%
285%      MagickBooleanType WriteFAXImage(const ImageInfo *image_info,Image *image)
286%
287%  A description of each parameter follows.
288%
289%    o image_info: the image info.
290%
291%    o image:  The image.
292%
293*/
294static MagickBooleanType WriteFAXImage(const ImageInfo *image_info,Image *image)
295{
296  ImageInfo
297    *write_info;
298
299  MagickBooleanType
300    status;
301
302  MagickOffsetType
303    scene;
304
305  /*
306    Open output image file.
307  */
308  assert(image_info != (const ImageInfo *) NULL);
309  assert(image_info->signature == MagickSignature);
310  assert(image != (Image *) NULL);
311  assert(image->signature == MagickSignature);
312  if (image->debug != MagickFalse)
313    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
314  status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
315  if (status == MagickFalse)
316    return(status);
317  write_info=CloneImageInfo(image_info);
318  (void) CopyMagickString(write_info->magick,"FAX",MaxTextExtent);
319  scene=0;
320  do
321  {
322    /*
323      Convert MIFF to monochrome.
324    */
325    if (image->colorspace != RGBColorspace)
326      (void) TransformImageColorspace(image,RGBColorspace);
327    status=HuffmanEncodeImage(write_info,image,image);
328    if (GetNextImageInList(image) == (Image *) NULL)
329      break;
330    image=SyncNextImageInList(image);
331    status=SetImageProgress(image,SaveImagesTag,scene++,
332      GetImageListLength(image));
333    if (status == MagickFalse)
334      break;
335  } while (write_info->adjoin != MagickFalse);
336  write_info=DestroyImageInfo(write_info);
337  (void) CloseBlob(image);
338  return(status);
339}
Note: See TracBrowser for help on using the browser.