blob: 835c4912182efc2e389899e24158ded4916b5513 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
cristyd9ecd042012-06-17 18:26:12 +000043#include "MagickCore/attribute.h"
cristy4c08aed2011-07-01 19:47:50 +000044#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/color-private.h"
48#include "MagickCore/colormap.h"
49#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000050#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#include "MagickCore/exception.h"
52#include "MagickCore/exception-private.h"
53#include "MagickCore/image.h"
54#include "MagickCore/image-private.h"
55#include "MagickCore/list.h"
56#include "MagickCore/magick.h"
57#include "MagickCore/memory_.h"
58#include "MagickCore/monitor.h"
59#include "MagickCore/monitor-private.h"
60#include "MagickCore/pixel-accessor.h"
61#include "MagickCore/quantum-private.h"
62#include "MagickCore/static.h"
63#include "MagickCore/string_.h"
64#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000065
66/*
67 Forward declarations.
68*/
69static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000070 WriteMONOImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000071
72/*
73%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74% %
75% %
76% %
77% R e a d M O N O I m a g e %
78% %
79% %
80% %
81%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82%
83% ReadMONOImage() reads an image of raw bites in LSB order and returns
84% it. It allocates the memory necessary for the new Image structure and
85% returns a pointer to the new image.
86%
87% The format of the ReadMONOImage method is:
88%
89% Image *ReadMONOImage(const ImageInfo *image_info,
90% ExceptionInfo *exception)
91%
92% A description of each parameter follows:
93%
94% o image_info: the image info.
95%
96% o exception: return any errors or warnings in this structure.
97%
98*/
99static Image *ReadMONOImage(const ImageInfo *image_info,
100 ExceptionInfo *exception)
101{
102 Image
103 *image;
104
cristy3ed852e2009-09-05 21:47:34 +0000105 MagickBooleanType
106 status;
107
cristy4c08aed2011-07-01 19:47:50 +0000108 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000109 *q;
110
cristyaff6d802011-04-26 01:46:31 +0000111 register ssize_t
112 x;
113
cristybb503372010-05-27 20:51:26 +0000114 size_t
cristy3ed852e2009-09-05 21:47:34 +0000115 bit,
116 byte;
117
cristyaff6d802011-04-26 01:46:31 +0000118 ssize_t
119 y;
120
cristy3ed852e2009-09-05 21:47:34 +0000121 /*
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);
cristy9950d572011-10-01 18:22:35 +0000131 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000132 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 }
cristyd4297022010-09-16 22:59:09 +0000140 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
141 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
142 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000143 /*
144 Initialize image colormap.
145 */
146 image->depth=1;
cristy018f07f2011-09-04 21:15:19 +0000147 if (AcquireImageColormap(image,2,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000148 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 */
cristybb503372010-05-27 20:51:26 +0000157 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000158 {
159 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000160 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000161 break;
cristy3ed852e2009-09-05 21:47:34 +0000162 bit=0;
163 byte=0;
cristybb503372010-05-27 20:51:26 +0000164 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000165 {
166 if (bit == 0)
cristybb503372010-05-27 20:51:26 +0000167 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000168 if (image_info->endian == LSBEndian)
cristy4c08aed2011-07-01 19:47:50 +0000169 SetPixelIndex(image,((byte & 0x01) != 0) ? 0x00 : 0x01,q);
cristy3ed852e2009-09-05 21:47:34 +0000170 else
cristy4c08aed2011-07-01 19:47:50 +0000171 SetPixelIndex(image,((byte & 0x01) != 0) ? 0x01 : 0x00,q);
cristy3ed852e2009-09-05 21:47:34 +0000172 bit++;
173 if (bit == 8)
174 bit=0;
175 byte>>=1;
cristyed231572011-07-14 02:18:59 +0000176 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000177 }
178 if (SyncAuthenticPixels(image,exception) == MagickFalse)
179 break;
cristycee97112010-05-28 00:44:52 +0000180 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristy4c08aed2011-07-01 19:47:50 +0000181 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000182 if (status == MagickFalse)
183 break;
184 }
cristyea1a8aa2011-10-20 13:24:06 +0000185 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000186 if (EOFBlob(image) != MagickFalse)
187 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
188 image->filename);
189 (void) CloseBlob(image);
190 return(GetFirstImageInList(image));
191}
192
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%
cristybb503372010-05-27 20:51:26 +0000213% size_t RegisterMONOImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000214%
215*/
cristybb503372010-05-27 20:51:26 +0000216ModuleExport size_t RegisterMONOImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000217{
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
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*/
252ModuleExport void UnregisterMONOImage(void)
253{
254 (void) UnregisterMagickInfo("MONO");
255}
256
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,
cristy1e178e72011-08-28 19:44:34 +0000273% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000274%
275% A description of each parameter follows.
276%
277% o image_info: the image info.
278%
279% o image: The image.
280%
cristy1e178e72011-08-28 19:44:34 +0000281% o exception: return any errors or warnings in this structure.
282%
cristy3ed852e2009-09-05 21:47:34 +0000283*/
284static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000285 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000286{
cristy3ed852e2009-09-05 21:47:34 +0000287 MagickBooleanType
288 status;
289
cristy4c08aed2011-07-01 19:47:50 +0000290 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000291 *p;
292
cristybb503372010-05-27 20:51:26 +0000293 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000294 x;
295
cristybb503372010-05-27 20:51:26 +0000296 size_t
cristy3ed852e2009-09-05 21:47:34 +0000297 bit,
298 byte;
299
cristyaff6d802011-04-26 01:46:31 +0000300 ssize_t
301 y;
302
cristy3ed852e2009-09-05 21:47:34 +0000303 /*
304 Open output image file.
305 */
306 assert(image_info != (const ImageInfo *) NULL);
307 assert(image_info->signature == MagickSignature);
308 assert(image != (Image *) NULL);
309 assert(image->signature == MagickSignature);
310 if (image->debug != MagickFalse)
311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000312 assert(exception != (ExceptionInfo *) NULL);
313 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000314 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000315 if (status == MagickFalse)
316 return(status);
cristy3d9f5ba2012-06-26 13:37:31 +0000317 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
cristy8d951092012-02-08 18:54:56 +0000318 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000319 /*
320 Convert image to a bi-level image.
321 */
cristy018f07f2011-09-04 21:15:19 +0000322 (void) SetImageType(image,BilevelType,exception);
cristybb503372010-05-27 20:51:26 +0000323 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000324 {
cristy1e178e72011-08-28 19:44:34 +0000325 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000326 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000327 break;
cristy3ed852e2009-09-05 21:47:34 +0000328 bit=0;
329 byte=0;
cristybb503372010-05-27 20:51:26 +0000330 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000331 {
332 byte>>=1;
333 if (image->endian == LSBEndian)
334 {
cristy6e963d82012-06-19 15:23:24 +0000335 if (GetPixelIntensity(image,p) < (QuantumRange/2.0))
cristy3ed852e2009-09-05 21:47:34 +0000336 byte|=0x80;
337 }
338 else
cristy6e963d82012-06-19 15:23:24 +0000339 if (GetPixelIntensity(image,p) >= (QuantumRange/2.0))
cristy3ed852e2009-09-05 21:47:34 +0000340 byte|=0x80;
341 bit++;
342 if (bit == 8)
343 {
344 (void) WriteBlobByte(image,(unsigned char) byte);
345 bit=0;
346 byte=0;
347 }
cristyed231572011-07-14 02:18:59 +0000348 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000349 }
350 if (bit != 0)
351 (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit)));
cristycee97112010-05-28 00:44:52 +0000352 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +0000353 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000354 if (status == MagickFalse)
355 break;
356 }
357 (void) CloseBlob(image);
358 return(MagickTrue);
359}