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