]> 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-2011 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   size_t
118     bit,
119     byte;
120
121   /*
122     Open image file.
123   */
124   assert(image_info != (const ImageInfo *) NULL);
125   assert(image_info->signature == MagickSignature);
126   if (image_info->debug != MagickFalse)
127     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
128       image_info->filename);
129   assert(exception != (ExceptionInfo *) NULL);
130   assert(exception->signature == MagickSignature);
131   image=AcquireImage(image_info);
132   if ((image->columns == 0) || (image->rows == 0))
133     ThrowReaderException(OptionError,"MustSpecifyImageSize");
134   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
135   if (status == MagickFalse)
136     {
137       image=DestroyImageList(image);
138       return((Image *) NULL);
139     }
140   if (DiscardBlobBytes(image,image->offset) == MagickFalse)
141     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
142       image->filename);
143   /*
144     Initialize image colormap.
145   */
146   image->depth=1;
147   if (AcquireImageColormap(image,2) == MagickFalse)
148     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
149   if (image_info->ping != MagickFalse)
150     {
151       (void) CloseBlob(image);
152       return(GetFirstImageInList(image));
153     }
154   /*
155     Convert bi-level image to pixel packets.
156   */
157   for (y=0; y < (ssize_t) image->rows; y++)
158   {
159     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
160     if (q == (PixelPacket *) NULL)
161       break;
162     indexes=GetAuthenticIndexQueue(image);
163     bit=0;
164     byte=0;
165     for (x=0; x < (ssize_t) image->columns; x++)
166     {
167       if (bit == 0)
168         byte=(size_t) ReadBlobByte(image);
169       if (image_info->endian == LSBEndian)
170         indexes[x]=(IndexPacket) (((byte & 0x01) != 0) ? 0x00 : 0x01);
171       else
172         indexes[x]=(IndexPacket) (((byte & 0x01) != 0) ? 0x01 : 0x00);
173       bit++;
174       if (bit == 8)
175         bit=0;
176       byte>>=1;
177     }
178     if (SyncAuthenticPixels(image,exception) == MagickFalse)
179       break;
180     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
181                 image->rows);
182     if (status == MagickFalse)
183       break;
184   }
185   (void) SyncImage(image);
186   if (EOFBlob(image) != MagickFalse)
187     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
188       image->filename);
189   (void) CloseBlob(image);
190   return(GetFirstImageInList(image));
191 }
192 \f
193 /*
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195 %                                                                             %
196 %                                                                             %
197 %                                                                             %
198 %   R e g i s t e r M O N O I m a g e                                         %
199 %                                                                             %
200 %                                                                             %
201 %                                                                             %
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 %
204 %  RegisterMONOImage() adds attributes for the MONO image format to
205 %  the list of supported formats.  The attributes include the image format
206 %  tag, a method to read and/or write the format, whether the format
207 %  supports the saving of more than one frame to the same file or blob,
208 %  whether the format supports native in-memory I/O, and a brief
209 %  description of the format.
210 %
211 %  The format of the RegisterMONOImage method is:
212 %
213 %      size_t RegisterMONOImage(void)
214 %
215 */
216 ModuleExport size_t RegisterMONOImage(void)
217 {
218   MagickInfo
219     *entry;
220
221   entry=SetMagickInfo("MONO");
222   entry->decoder=(DecodeImageHandler *) ReadMONOImage;
223   entry->encoder=(EncodeImageHandler *) WriteMONOImage;
224   entry->raw=MagickTrue;
225   entry->endian_support=MagickTrue;
226   entry->adjoin=MagickFalse;
227   entry->description=ConstantString("Raw bi-level bitmap");
228   entry->module=ConstantString("MONO");
229   (void) RegisterMagickInfo(entry);
230   return(MagickImageCoderSignature);
231 }
232 \f
233 /*
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 %                                                                             %
236 %                                                                             %
237 %                                                                             %
238 %   U n r e g i s t e r M O N O I m a g e                                     %
239 %                                                                             %
240 %                                                                             %
241 %                                                                             %
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 %
244 %  UnregisterMONOImage() removes format registrations made by the
245 %  MONO module from the list of supported formats.
246 %
247 %  The format of the UnregisterMONOImage method is:
248 %
249 %      UnregisterMONOImage(void)
250 %
251 */
252 ModuleExport void UnregisterMONOImage(void)
253 {
254   (void) UnregisterMagickInfo("MONO");
255 }
256 \f
257 /*
258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 %                                                                             %
260 %                                                                             %
261 %                                                                             %
262 %   W r i t e M O N O I m a g e                                               %
263 %                                                                             %
264 %                                                                             %
265 %                                                                             %
266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 %
268 %  WriteMONOImage() writes an image of raw bits in LSB order to a file.
269 %
270 %  The format of the WriteMONOImage method is:
271 %
272 %      MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
273 %        Image *image)
274 %
275 %  A description of each parameter follows.
276 %
277 %    o image_info: the image info.
278 %
279 %    o image:  The image.
280 %
281 */
282 static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
283   Image *image)
284 {
285   ssize_t
286     y;
287
288   MagickBooleanType
289     status;
290
291   register const IndexPacket
292     *indexes;
293
294   register const PixelPacket
295     *p;
296
297   register ssize_t
298     x;
299
300   size_t
301     bit,
302     byte;
303
304   /*
305     Open output image file.
306   */
307   assert(image_info != (const ImageInfo *) NULL);
308   assert(image_info->signature == MagickSignature);
309   assert(image != (Image *) NULL);
310   assert(image->signature == MagickSignature);
311   if (image->debug != MagickFalse)
312     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
313   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
314   if (status == MagickFalse)
315     return(status);
316   if (image->colorspace != RGBColorspace)
317     (void) TransformImageColorspace(image,RGBColorspace);
318   /*
319     Convert image to a bi-level image.
320   */
321   (void) SetImageType(image,BilevelType);
322   for (y=0; y < (ssize_t) image->rows; y++)
323   {
324     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
325     if (p == (const PixelPacket *) NULL)
326       break;
327     indexes=GetVirtualIndexQueue(image);
328     bit=0;
329     byte=0;
330     for (x=0; x < (ssize_t) image->columns; x++)
331     {
332       byte>>=1;
333       if (image->endian == LSBEndian)
334         {
335           if (PixelIntensity(p) < ((Quantum) QuantumRange/2.0))
336             byte|=0x80;
337         }
338       else
339         if (PixelIntensity(p) >= ((Quantum) QuantumRange/2.0))
340           byte|=0x80;
341       bit++;
342       if (bit == 8)
343         {
344           (void) WriteBlobByte(image,(unsigned char) byte);
345           bit=0;
346           byte=0;
347         }
348       p++;
349     }
350     if (bit != 0)
351       (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit)));
352     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
353                 image->rows);
354     if (status == MagickFalse)
355       break;
356   }
357   (void) CloseBlob(image);
358   return(MagickTrue);
359 }