root / ImageMagick / trunk / coders / mono.c

Revision 12035, 12.6 kB (checked in by cristy, 6 weeks ago)
Line 
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3%                                                                             %
4%                                                                             %
5%                                                                             %
6%                         M   M   OOO   N   N   OOO                           %
7%                         MM MM  O   O  NN  N  O   O                          %
8%                         M M M  O   O  N N N  O   O                          %
9%                         M   M  O   O  N  NN  O   O                          %
10%                         M   M   OOO   N   N   OOO                           %
11%                                                                             %
12%                                                                             %
13%                   Read/Write Raw Bi-Level Bitmap Format.                    %
14%                                                                             %
15%                              Software Design                                %
16%                                John Cristy                                  %
17%                                 July 1992                                   %
18%                                                                             %
19%                                                                             %
20%  Copyright 1999-2008 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/color-private.h"
46#include "magick/colorspace.h"
47#include "magick/exception.h"
48#include "magick/exception-private.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  WriteMONOImage(const ImageInfo *,Image *);
66
67/*
68%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69%                                                                             %
70%                                                                             %
71%                                                                             %
72%   R e a d M O N O I m a g e                                                 %
73%                                                                             %
74%                                                                             %
75%                                                                             %
76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77%
78%  ReadMONOImage() reads an image of raw bites in LSB order and returns
79%  it.  It allocates the memory necessary for the new Image structure and
80%  returns a pointer to the new image.
81%
82%  The format of the ReadMONOImage method is:
83%
84%      Image *ReadMONOImage(const ImageInfo *image_info,
85%        ExceptionInfo *exception)
86%
87%  A description of each parameter follows:
88%
89%    o image_info: the image info.
90%
91%    o exception: return any errors or warnings in this structure.
92%
93*/
94static Image *ReadMONOImage(const ImageInfo *image_info,
95  ExceptionInfo *exception)
96{
97  Image
98    *image;
99
100  long
101    y;
102
103  MagickBooleanType
104    status;
105
106  register IndexPacket
107    *indexes;
108
109  register long
110    x;
111
112  register PixelPacket
113    *q;
114
115  register long
116    i;
117
118  unsigned long
119    bit,
120    byte;
121
122  /*
123    Open image file.
124  */
125  assert(image_info != (const ImageInfo *) NULL);
126  assert(image_info->signature == MagickSignature);
127  if (image_info->debug != MagickFalse)
128    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
129      image_info->filename);
130  assert(exception != (ExceptionInfo *) NULL);
131  assert(exception->signature == MagickSignature);
132  image=AcquireImage(image_info);
133  if ((image->columns == 0) || (image->rows == 0))
134    ThrowReaderException(OptionError,"MustSpecifyImageSize");
135  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
136  if (status == MagickFalse)
137    {
138      image=DestroyImageList(image);
139      return((Image *) NULL);
140    }
141  for (i=0; i < image->offset; i++)
142    if (ReadBlobByte(image) == EOF)
143      {
144        ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
145          image->filename);
146        break;
147      }
148  /*
149    Initialize image colormap.
150  */
151  image->depth=1;
152  if (AcquireImageColormap(image,2) == MagickFalse)
153    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
154  if (image_info->ping != MagickFalse)
155    {
156      (void) CloseBlob(image);
157      return(GetFirstImageInList(image));
158    }
159  /*
160    Convert bi-level image to pixel packets.
161  */
162  if (SetImageExtent(image,0,0) == MagickFalse)
163    {
164      InheritException(exception,&image->exception);
165      return(DestroyImageList(image));
166    }
167  for (y=0; y < (long) image->rows; y++)
168  {
169    q=SetImagePixels(image,0,y,image->columns,1);
170    if (q == (PixelPacket *) NULL)
171      break;
172    indexes=GetIndexes(image);
173    bit=0;
174    byte=0;
175    for (x=0; x < (long) image->columns; x++)
176    {
177      if (bit == 0)
178        byte=(unsigned long) ReadBlobByte(image);
179      if (image_info->endian == LSBEndian)
180        indexes[x]=(IndexPacket) (((byte & 0x01) != 0) ? 0x00 : 0x01);
181      else
182        indexes[x]=(IndexPacket) (((byte & 0x01) != 0) ? 0x01 : 0x00);
183      bit++;
184      if (bit == 8)
185        bit=0;
186      byte>>=1;
187    }
188    if (SyncImagePixels(image) == MagickFalse)
189      break;
190    status=SetImageProgress(image,LoadImageTag,y,image->rows);
191    if (status == MagickFalse)
192      break;
193  }
194  (void) SyncImage(image);
195  if (EOFBlob(image) != MagickFalse)
196    ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
197      image->filename);
198  (void) CloseBlob(image);
199  return(GetFirstImageInList(image));
200}
201
202/*
203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204%                                                                             %
205%                                                                             %
206%                                                                             %
207%   R e g i s t e r M O N O I m a g e                                         %
208%                                                                             %
209%                                                                             %
210%                                                                             %
211%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212%
213%  RegisterMONOImage() adds attributes for the MONO image format to
214%  the list of supported formats.  The attributes include the image format
215%  tag, a method to read and/or write the format, whether the format
216%  supports the saving of more than one frame to the same file or blob,
217%  whether the format supports native in-memory I/O, and a brief
218%  description of the format.
219%
220%  The format of the RegisterMONOImage method is:
221%
222%      unsigned long RegisterMONOImage(void)
223%
224*/
225ModuleExport unsigned long RegisterMONOImage(void)
226{
227  MagickInfo
228    *entry;
229
230  entry=SetMagickInfo("MONO");
231  entry->decoder=(DecodeImageHandler *) ReadMONOImage;
232  entry->encoder=(EncodeImageHandler *) WriteMONOImage;
233  entry->raw=MagickTrue;
234  entry->endian_support=MagickTrue;
235  entry->adjoin=MagickFalse;
236  entry->description=ConstantString("Raw bi-level bitmap");
237  entry->module=ConstantString("MONO");
238  (void) RegisterMagickInfo(entry);
239  return(MagickImageCoderSignature);
240}
241
242/*
243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244%                                                                             %
245%                                                                             %
246%                                                                             %
247%   U n r e g i s t e r M O N O I m a g e                                     %
248%                                                                             %
249%                                                                             %
250%                                                                             %
251%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252%
253%  UnregisterMONOImage() removes format registrations made by the
254%  MONO module from the list of supported formats.
255%
256%  The format of the UnregisterMONOImage method is:
257%
258%      UnregisterMONOImage(void)
259%
260*/
261ModuleExport void UnregisterMONOImage(void)
262{
263  (void) UnregisterMagickInfo("MONO");
264}
265
266/*
267%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268%                                                                             %
269%                                                                             %
270%                                                                             %
271%   W r i t e M O N O I m a g e                                               %
272%                                                                             %
273%                                                                             %
274%                                                                             %
275%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276%
277%  WriteMONOImage() writes an image of raw bits in LSB order to a file.
278%
279%  The format of the WriteMONOImage method is:
280%
281%      MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
282%        Image *image)
283%
284%  A description of each parameter follows.
285%
286%    o image_info: the image info.
287%
288%    o image:  The image.
289%
290*/
291static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
292  Image *image)
293{
294  long
295    y;
296
297  MagickBooleanType
298    status;
299
300  register IndexPacket
301    *indexes;
302
303  register const PixelPacket
304    *p;
305
306  register long
307    x;
308
309  unsigned long
310    bit,
311    byte;
312
313  /*
314    Open output image file.
315  */
316  assert(image_info != (const ImageInfo *) NULL);
317  assert(image_info->signature == MagickSignature);
318  assert(image != (Image *) NULL);
319  assert(image->signature == MagickSignature);
320  if (image->debug != MagickFalse)
321    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
322  status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
323  if (status == MagickFalse)
324    return(status);
325  if (image->colorspace != RGBColorspace)
326    (void) SetImageColorspace(image,RGBColorspace);
327  /*
328    Convert image to a bi-level image.
329  */
330  (void) SetImageType(image,BilevelType);
331  for (y=0; y < (long) image->rows; y++)
332  {
333    p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
334    if (p == (const PixelPacket *) NULL)
335      break;
336    indexes=GetIndexes(image);
337    bit=0;
338    byte=0;
339    for (x=0; x < (long) image->columns; x++)
340    {
341      byte>>=1;
342      if (image->endian == LSBEndian)
343        {
344          if (PixelIntensity(p) < ((Quantum) QuantumRange/2.0))
345            byte|=0x80;
346        }
347      else
348        if (PixelIntensity(p) >= ((Quantum) QuantumRange/2.0))
349          byte|=0x80;
350      bit++;
351      if (bit == 8)
352        {
353          (void) WriteBlobByte(image,(unsigned char) byte);
354          bit=0;
355          byte=0;
356        }
357      p++;
358    }
359    if (bit != 0)
360      (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit)));
361    status=SetImageProgress(image,SaveImageTag,y,image->rows);
362    if (status == MagickFalse)
363      break;
364  }
365  (void) CloseBlob(image);
366  return(MagickTrue);
367}
Note: See TracBrowser for help on using the browser.