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 < (long) 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 < (long) image->columns; x++)
175 byte=(unsigned long) 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,y,image->rows);
188 if (status == MagickFalse)
191 (void) SyncImage(image);
192 if (EOFBlob(image) != MagickFalse)
193 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
195 (void) CloseBlob(image);
196 return(GetFirstImageInList(image));
200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204 % R e g i s t e r M O N O I m a g e %
208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210 % RegisterMONOImage() adds attributes for the MONO image format to
211 % the list of supported formats. The attributes include the image format
212 % tag, a method to read and/or write the format, whether the format
213 % supports the saving of more than one frame to the same file or blob,
214 % whether the format supports native in-memory I/O, and a brief
215 % description of the format.
217 % The format of the RegisterMONOImage method is:
219 % unsigned long RegisterMONOImage(void)
222 ModuleExport unsigned long RegisterMONOImage(void)
227 entry=SetMagickInfo("MONO");
228 entry->decoder=(DecodeImageHandler *) ReadMONOImage;
229 entry->encoder=(EncodeImageHandler *) WriteMONOImage;
230 entry->raw=MagickTrue;
231 entry->endian_support=MagickTrue;
232 entry->adjoin=MagickFalse;
233 entry->description=ConstantString("Raw bi-level bitmap");
234 entry->module=ConstantString("MONO");
235 (void) RegisterMagickInfo(entry);
236 return(MagickImageCoderSignature);
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 % U n r e g i s t e r M O N O I m a g e %
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % UnregisterMONOImage() removes format registrations made by the
251 % MONO module from the list of supported formats.
253 % The format of the UnregisterMONOImage method is:
255 % UnregisterMONOImage(void)
258 ModuleExport void UnregisterMONOImage(void)
260 (void) UnregisterMagickInfo("MONO");
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 % W r i t e M O N O I m a g e %
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % WriteMONOImage() writes an image of raw bits in LSB order to a file.
276 % The format of the WriteMONOImage method is:
278 % MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
281 % A description of each parameter follows.
283 % o image_info: the image info.
285 % o image: The image.
288 static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
297 register const IndexPacket
300 register const PixelPacket
311 Open output image file.
313 assert(image_info != (const ImageInfo *) NULL);
314 assert(image_info->signature == MagickSignature);
315 assert(image != (Image *) NULL);
316 assert(image->signature == MagickSignature);
317 if (image->debug != MagickFalse)
318 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
319 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
320 if (status == MagickFalse)
322 if (image->colorspace != RGBColorspace)
323 (void) TransformImageColorspace(image,RGBColorspace);
325 Convert image to a bi-level image.
327 (void) SetImageType(image,BilevelType);
328 for (y=0; y < (long) image->rows; y++)
330 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
331 if (p == (const PixelPacket *) NULL)
333 indexes=GetVirtualIndexQueue(image);
336 for (x=0; x < (long) image->columns; x++)
339 if (image->endian == LSBEndian)
341 if (PixelIntensity(p) < ((Quantum) QuantumRange/2.0))
345 if (PixelIntensity(p) >= ((Quantum) QuantumRange/2.0))
350 (void) WriteBlobByte(image,(unsigned char) byte);
357 (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit)));
358 status=SetImageProgress(image,SaveImageTag,y,image->rows);
359 if (status == MagickFalse)
362 (void) CloseBlob(image);