2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % M M M O O N N N O O %
13 % Read/Write Raw Bi-Level Bitmap Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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/colorspace-private.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/image.h"
53 #include "MagickCore/image-private.h"
54 #include "MagickCore/list.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/monitor.h"
58 #include "MagickCore/monitor-private.h"
59 #include "MagickCore/pixel-accessor.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/module.h"
68 static MagickBooleanType
69 WriteMONOImage(const ImageInfo *,Image *,ExceptionInfo *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % R e a d M O N O I m a g e %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % ReadMONOImage() reads an image of raw bites in LSB order and returns
83 % it. It allocates the memory necessary for the new Image structure and
84 % returns a pointer to the new image.
86 % The format of the ReadMONOImage method is:
88 % Image *ReadMONOImage(const ImageInfo *image_info,
89 % ExceptionInfo *exception)
91 % A description of each parameter follows:
93 % o image_info: the image info.
95 % o exception: return any errors or warnings in this structure.
98 static Image *ReadMONOImage(const ImageInfo *image_info,
99 ExceptionInfo *exception)
123 assert(image_info != (const ImageInfo *) NULL);
124 assert(image_info->signature == MagickSignature);
125 if (image_info->debug != MagickFalse)
126 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
127 image_info->filename);
128 assert(exception != (ExceptionInfo *) NULL);
129 assert(exception->signature == MagickSignature);
130 image=AcquireImage(image_info,exception);
131 if ((image->columns == 0) || (image->rows == 0))
132 ThrowReaderException(OptionError,"MustSpecifyImageSize");
133 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
134 if (status == MagickFalse)
136 image=DestroyImageList(image);
137 return((Image *) NULL);
139 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
140 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
143 Initialize image colormap.
146 if (AcquireImageColormap(image,2,exception) == MagickFalse)
147 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
148 if (image_info->ping != MagickFalse)
150 (void) CloseBlob(image);
151 return(GetFirstImageInList(image));
154 Convert bi-level image to pixel packets.
156 for (y=0; y < (ssize_t) image->rows; y++)
158 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
159 if (q == (Quantum *) NULL)
163 for (x=0; x < (ssize_t) image->columns; x++)
166 byte=(size_t) ReadBlobByte(image);
167 if (image_info->endian == LSBEndian)
168 SetPixelIndex(image,((byte & 0x01) != 0) ? 0x00 : 0x01,q);
170 SetPixelIndex(image,((byte & 0x01) != 0) ? 0x01 : 0x00,q);
175 q+=GetPixelChannels(image);
177 if (SyncAuthenticPixels(image,exception) == MagickFalse)
179 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
181 if (status == MagickFalse)
184 (void) SyncImage(image);
185 if (EOFBlob(image) != MagickFalse)
186 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
188 (void) CloseBlob(image);
189 return(GetFirstImageInList(image));
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 % R e g i s t e r M O N O I m a g e %
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 % RegisterMONOImage() adds attributes for the MONO image format to
204 % the list of supported formats. The attributes include the image format
205 % tag, a method to read and/or write the format, whether the format
206 % supports the saving of more than one frame to the same file or blob,
207 % whether the format supports native in-memory I/O, and a brief
208 % description of the format.
210 % The format of the RegisterMONOImage method is:
212 % size_t RegisterMONOImage(void)
215 ModuleExport size_t RegisterMONOImage(void)
220 entry=SetMagickInfo("MONO");
221 entry->decoder=(DecodeImageHandler *) ReadMONOImage;
222 entry->encoder=(EncodeImageHandler *) WriteMONOImage;
223 entry->raw=MagickTrue;
224 entry->endian_support=MagickTrue;
225 entry->adjoin=MagickFalse;
226 entry->description=ConstantString("Raw bi-level bitmap");
227 entry->module=ConstantString("MONO");
228 (void) RegisterMagickInfo(entry);
229 return(MagickImageCoderSignature);
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 % U n r e g i s t e r M O N O I m a g e %
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 % UnregisterMONOImage() removes format registrations made by the
244 % MONO module from the list of supported formats.
246 % The format of the UnregisterMONOImage method is:
248 % UnregisterMONOImage(void)
251 ModuleExport void UnregisterMONOImage(void)
253 (void) UnregisterMagickInfo("MONO");
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 % W r i t e M O N O I m a g e %
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 % WriteMONOImage() writes an image of raw bits in LSB order to a file.
269 % The format of the WriteMONOImage method is:
271 % MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
272 % Image *image,ExceptionInfo *exception)
274 % A description of each parameter follows.
276 % o image_info: the image info.
278 % o image: The image.
280 % o exception: return any errors or warnings in this structure.
283 static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
284 Image *image,ExceptionInfo *exception)
289 register const Quantum
303 Open output image file.
305 assert(image_info != (const ImageInfo *) NULL);
306 assert(image_info->signature == MagickSignature);
307 assert(image != (Image *) NULL);
308 assert(image->signature == MagickSignature);
309 if (image->debug != MagickFalse)
310 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
311 assert(exception != (ExceptionInfo *) NULL);
312 assert(exception->signature == MagickSignature);
313 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
314 if (status == MagickFalse)
316 if (IsRGBColorspace(image->colorspace) == MagickFalse)
317 (void) TransformImageColorspace(image,RGBColorspace);
319 Convert image to a bi-level image.
321 (void) SetImageType(image,BilevelType,exception);
322 for (y=0; y < (ssize_t) image->rows; y++)
324 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
325 if (p == (const Quantum *) NULL)
329 for (x=0; x < (ssize_t) image->columns; x++)
332 if (image->endian == LSBEndian)
334 if (GetPixelIntensity(image,p) < ((Quantum) QuantumRange/2.0))
338 if (GetPixelIntensity(image,p) >= ((Quantum) QuantumRange/2.0))
343 (void) WriteBlobByte(image,(unsigned char) byte);
347 p+=GetPixelChannels(image);
350 (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit)));
351 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
353 if (status == MagickFalse)
356 (void) CloseBlob(image);