2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % M M M O O N N N O O %
13 % Read/Write Raw Bi-Level Bitmap Format %
20 % Copyright 1999-2010 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 "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"
66 static MagickBooleanType
67 WriteMONOImage(const ImageInfo *,Image *);
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % R e a d M O N O I m a g e %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
84 % The format of the ReadMONOImage method is:
86 % Image *ReadMONOImage(const ImageInfo *image_info,
87 % ExceptionInfo *exception)
89 % A description of each parameter follows:
91 % o image_info: the image info.
93 % o exception: return any errors or warnings in this structure.
96 static Image *ReadMONOImage(const ImageInfo *image_info,
97 ExceptionInfo *exception)
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)
140 image=DestroyImageList(image);
141 return((Image *) NULL);
143 for (i=0; i < image->offset; i++)
144 if (ReadBlobByte(image) == EOF)
146 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
151 Initialize image colormap.
154 if (AcquireImageColormap(image,2) == MagickFalse)
155 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
156 if (image_info->ping != MagickFalse)
158 (void) CloseBlob(image);
159 return(GetFirstImageInList(image));
162 Convert bi-level image to pixel packets.
164 for (y=0; y < (ssize_t) image->rows; y++)
166 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
167 if (q == (PixelPacket *) NULL)
169 indexes=GetAuthenticIndexQueue(image);
172 for (x=0; x < (ssize_t) image->columns; x++)
175 byte=(size_t) ReadBlobByte(image);
176 if (image_info->endian == LSBEndian)
177 indexes[x]=(IndexPacket) (((byte & 0x01) != 0) ? 0x00 : 0x01);
179 indexes[x]=(IndexPacket) (((byte & 0x01) != 0) ? 0x01 : 0x00);
185 if (SyncAuthenticPixels(image,exception) == MagickFalse)
187 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
189 if (status == MagickFalse)
192 (void) SyncImage(image);
193 if (EOFBlob(image) != MagickFalse)
194 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
196 (void) CloseBlob(image);
197 return(GetFirstImageInList(image));
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 % R e g i s t e r M O N O I m a g e %
209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
218 % The format of the RegisterMONOImage method is:
220 % size_t RegisterMONOImage(void)
223 ModuleExport size_t RegisterMONOImage(void)
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);
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 % U n r e g i s t e r M O N O I m a g e %
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % UnregisterMONOImage() removes format registrations made by the
252 % MONO module from the list of supported formats.
254 % The format of the UnregisterMONOImage method is:
256 % UnregisterMONOImage(void)
259 ModuleExport void UnregisterMONOImage(void)
261 (void) UnregisterMagickInfo("MONO");
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 % W r i t e M O N O I m a g e %
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % WriteMONOImage() writes an image of raw bits in LSB order to a file.
277 % The format of the WriteMONOImage method is:
279 % MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
282 % A description of each parameter follows.
284 % o image_info: the image info.
286 % o image: The image.
289 static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
298 register const IndexPacket
301 register const PixelPacket
312 Open output image file.
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)
323 if (image->colorspace != RGBColorspace)
324 (void) TransformImageColorspace(image,RGBColorspace);
326 Convert image to a bi-level image.
328 (void) SetImageType(image,BilevelType);
329 for (y=0; y < (ssize_t) image->rows; y++)
331 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
332 if (p == (const PixelPacket *) NULL)
334 indexes=GetVirtualIndexQueue(image);
337 for (x=0; x < (ssize_t) image->columns; x++)
340 if (image->endian == LSBEndian)
342 if (PixelIntensity(p) < ((Quantum) QuantumRange/2.0))
346 if (PixelIntensity(p) >= ((Quantum) QuantumRange/2.0))
351 (void) WriteBlobByte(image,(unsigned char) byte);
358 (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit)));
359 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
361 if (status == MagickFalse)
364 (void) CloseBlob(image);