]> 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   ssize_t
103     y;
104
105   MagickBooleanType
106     status;
107
108   register IndexPacket
109     *indexes;
110
111   register ssize_t
112     x;
113
114   register PixelPacket
115     *q;
116
117   register ssize_t
118     i;
119
120   size_t
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 < (ssize_t) 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 < (ssize_t) image->columns; x++)
173     {
174       if (bit == 0)
175         byte=(size_t) 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,(MagickOffsetType) y,
188                 image->rows);
189     if (status == MagickFalse)
190       break;
191   }
192   (void) SyncImage(image);
193   if (EOFBlob(image) != MagickFalse)
194     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
195       image->filename);
196   (void) CloseBlob(image);
197   return(GetFirstImageInList(image));
198 }
199 \f
200 /*
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 %                                                                             %
203 %                                                                             %
204 %                                                                             %
205 %   R e g i s t e r M O N O I m a g e                                         %
206 %                                                                             %
207 %                                                                             %
208 %                                                                             %
209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210 %
211 %  RegisterMONOImage() adds attributes for the MONO image format to
212 %  the list of supported formats.  The attributes include the image format
213 %  tag, a method to read and/or write the format, whether the format
214 %  supports the saving of more than one frame to the same file or blob,
215 %  whether the format supports native in-memory I/O, and a brief
216 %  description of the format.
217 %
218 %  The format of the RegisterMONOImage method is:
219 %
220 %      size_t RegisterMONOImage(void)
221 %
222 */
223 ModuleExport size_t RegisterMONOImage(void)
224 {
225   MagickInfo
226     *entry;
227
228   entry=SetMagickInfo("MONO");
229   entry->decoder=(DecodeImageHandler *) ReadMONOImage;
230   entry->encoder=(EncodeImageHandler *) WriteMONOImage;
231   entry->raw=MagickTrue;
232   entry->endian_support=MagickTrue;
233   entry->adjoin=MagickFalse;
234   entry->description=ConstantString("Raw bi-level bitmap");
235   entry->module=ConstantString("MONO");
236   (void) RegisterMagickInfo(entry);
237   return(MagickImageCoderSignature);
238 }
239 \f
240 /*
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 %                                                                             %
243 %                                                                             %
244 %                                                                             %
245 %   U n r e g i s t e r M O N O I m a g e                                     %
246 %                                                                             %
247 %                                                                             %
248 %                                                                             %
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 %
251 %  UnregisterMONOImage() removes format registrations made by the
252 %  MONO module from the list of supported formats.
253 %
254 %  The format of the UnregisterMONOImage method is:
255 %
256 %      UnregisterMONOImage(void)
257 %
258 */
259 ModuleExport void UnregisterMONOImage(void)
260 {
261   (void) UnregisterMagickInfo("MONO");
262 }
263 \f
264 /*
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 %                                                                             %
267 %                                                                             %
268 %                                                                             %
269 %   W r i t e M O N O I m a g e                                               %
270 %                                                                             %
271 %                                                                             %
272 %                                                                             %
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 %
275 %  WriteMONOImage() writes an image of raw bits in LSB order to a file.
276 %
277 %  The format of the WriteMONOImage method is:
278 %
279 %      MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
280 %        Image *image)
281 %
282 %  A description of each parameter follows.
283 %
284 %    o image_info: the image info.
285 %
286 %    o image:  The image.
287 %
288 */
289 static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
290   Image *image)
291 {
292   ssize_t
293     y;
294
295   MagickBooleanType
296     status;
297
298   register const IndexPacket
299     *indexes;
300
301   register const PixelPacket
302     *p;
303
304   register ssize_t
305     x;
306
307   size_t
308     bit,
309     byte;
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);
320   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
321   if (status == MagickFalse)
322     return(status);
323   if (image->colorspace != RGBColorspace)
324     (void) TransformImageColorspace(image,RGBColorspace);
325   /*
326     Convert image to a bi-level image.
327   */
328   (void) SetImageType(image,BilevelType);
329   for (y=0; y < (ssize_t) image->rows; y++)
330   {
331     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
332     if (p == (const PixelPacket *) NULL)
333       break;
334     indexes=GetVirtualIndexQueue(image);
335     bit=0;
336     byte=0;
337     for (x=0; x < (ssize_t) image->columns; x++)
338     {
339       byte>>=1;
340       if (image->endian == LSBEndian)
341         {
342           if (PixelIntensity(p) < ((Quantum) QuantumRange/2.0))
343             byte|=0x80;
344         }
345       else
346         if (PixelIntensity(p) >= ((Quantum) QuantumRange/2.0))
347           byte|=0x80;
348       bit++;
349       if (bit == 8)
350         {
351           (void) WriteBlobByte(image,(unsigned char) byte);
352           bit=0;
353           byte=0;
354         }
355       p++;
356     }
357     if (bit != 0)
358       (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit)));
359     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
360                 image->rows);
361     if (status == MagickFalse)
362       break;
363   }
364   (void) CloseBlob(image);
365   return(MagickTrue);
366 }