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/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"
67 static MagickBooleanType
68 WriteMONOImage(const ImageInfo *,Image *);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % R e a d M O N O I m a g e %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
85 % The format of the ReadMONOImage method is:
87 % Image *ReadMONOImage(const ImageInfo *image_info,
88 % ExceptionInfo *exception)
90 % A description of each parameter follows:
92 % o image_info: the image info.
94 % o exception: return any errors or warnings in this structure.
97 static Image *ReadMONOImage(const ImageInfo *image_info,
98 ExceptionInfo *exception)
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)
135 image=DestroyImageList(image);
136 return((Image *) NULL);
138 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
139 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
142 Initialize image colormap.
145 if (AcquireImageColormap(image,2) == MagickFalse)
146 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
147 if (image_info->ping != MagickFalse)
149 (void) CloseBlob(image);
150 return(GetFirstImageInList(image));
153 Convert bi-level image to pixel packets.
155 for (y=0; y < (ssize_t) image->rows; y++)
157 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
158 if (q == (const Quantum *) NULL)
162 for (x=0; x < (ssize_t) image->columns; x++)
165 byte=(size_t) ReadBlobByte(image);
166 if (image_info->endian == LSBEndian)
167 SetPixelIndex(image,((byte & 0x01) != 0) ? 0x00 : 0x01,q);
169 SetPixelIndex(image,((byte & 0x01) != 0) ? 0x01 : 0x00,q);
174 q+=GetPixelChannels(image);
176 if (SyncAuthenticPixels(image,exception) == MagickFalse)
178 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
180 if (status == MagickFalse)
183 (void) SyncImage(image);
184 if (EOFBlob(image) != MagickFalse)
185 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
187 (void) CloseBlob(image);
188 return(GetFirstImageInList(image));
192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196 % R e g i s t e r M O N O I m a g e %
200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
209 % The format of the RegisterMONOImage method is:
211 % size_t RegisterMONOImage(void)
214 ModuleExport size_t RegisterMONOImage(void)
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);
232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 % U n r e g i s t e r M O N O I m a g e %
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 % UnregisterMONOImage() removes format registrations made by the
243 % MONO module from the list of supported formats.
245 % The format of the UnregisterMONOImage method is:
247 % UnregisterMONOImage(void)
250 ModuleExport void UnregisterMONOImage(void)
252 (void) UnregisterMagickInfo("MONO");
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 % W r i t e M O N O I m a g e %
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 % WriteMONOImage() writes an image of raw bits in LSB order to a file.
268 % The format of the WriteMONOImage method is:
270 % MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
273 % A description of each parameter follows.
275 % o image_info: the image info.
277 % o image: The image.
280 static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
286 register const Quantum
300 Open output image file.
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)
311 if (image->colorspace != RGBColorspace)
312 (void) TransformImageColorspace(image,RGBColorspace);
314 Convert image to a bi-level image.
316 (void) SetImageType(image,BilevelType);
317 for (y=0; y < (ssize_t) image->rows; y++)
319 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
320 if (p == (const Quantum *) NULL)
324 for (x=0; x < (ssize_t) image->columns; x++)
327 if (image->endian == LSBEndian)
329 if (GetPixelIntensity(image,p) < ((Quantum) QuantumRange/2.0))
333 if (GetPixelIntensity(image,p) >= ((Quantum) QuantumRange/2.0))
338 (void) WriteBlobByte(image,(unsigned char) byte);
342 p+=GetPixelChannels(image);
345 (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit)));
346 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
348 if (status == MagickFalse)
351 (void) CloseBlob(image);