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