2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % M M M O O N N N O O %
13 % Read/Write Raw Bi-Level Bitmap Format %
20 % Copyright 1999-2013 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/attribute.h"
44 #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"
50 #include "MagickCore/colorspace-private.h"
51 #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"
69 static MagickBooleanType
70 WriteMONOImage(const ImageInfo *,Image *,ExceptionInfo *);
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % R e a d M O N O I m a g e %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
87 % The format of the ReadMONOImage method is:
89 % Image *ReadMONOImage(const ImageInfo *image_info,
90 % ExceptionInfo *exception)
92 % A description of each parameter follows:
94 % o image_info: the image info.
96 % o exception: return any errors or warnings in this structure.
99 static Image *ReadMONOImage(const ImageInfo *image_info,
100 ExceptionInfo *exception)
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);
131 image=AcquireImage(image_info,exception);
132 if ((image->columns == 0) || (image->rows == 0))
133 ThrowReaderException(OptionError,"MustSpecifyImageSize");
134 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
135 if (status == MagickFalse)
137 image=DestroyImageList(image);
138 return((Image *) NULL);
140 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
141 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
144 Initialize image colormap.
147 if (AcquireImageColormap(image,2,exception) == MagickFalse)
148 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
149 if (image_info->ping != MagickFalse)
151 (void) CloseBlob(image);
152 return(GetFirstImageInList(image));
155 Convert bi-level image to pixel packets.
157 for (y=0; y < (ssize_t) image->rows; y++)
159 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
160 if (q == (Quantum *) NULL)
164 for (x=0; x < (ssize_t) image->columns; x++)
167 byte=(size_t) ReadBlobByte(image);
168 if (image_info->endian == LSBEndian)
169 SetPixelIndex(image,((byte & 0x01) != 0) ? 0x00 : 0x01,q);
171 SetPixelIndex(image,((byte & 0x01) != 0) ? 0x01 : 0x00,q);
176 q+=GetPixelChannels(image);
178 if (SyncAuthenticPixels(image,exception) == MagickFalse)
180 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
182 if (status == MagickFalse)
185 (void) SyncImage(image,exception);
186 if (EOFBlob(image) != MagickFalse)
187 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
189 (void) CloseBlob(image);
190 return(GetFirstImageInList(image));
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 % R e g i s t e r M O N O I m a g e %
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
211 % The format of the RegisterMONOImage method is:
213 % size_t RegisterMONOImage(void)
216 ModuleExport size_t RegisterMONOImage(void)
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);
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 % U n r e g i s t e r M O N O I m a g e %
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 % UnregisterMONOImage() removes format registrations made by the
245 % MONO module from the list of supported formats.
247 % The format of the UnregisterMONOImage method is:
249 % UnregisterMONOImage(void)
252 ModuleExport void UnregisterMONOImage(void)
254 (void) UnregisterMagickInfo("MONO");
258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 % W r i t e M O N O I m a g e %
266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 % WriteMONOImage() writes an image of raw bits in LSB order to a file.
270 % The format of the WriteMONOImage method is:
272 % MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
273 % Image *image,ExceptionInfo *exception)
275 % A description of each parameter follows.
277 % o image_info: the image info.
279 % o image: The image.
281 % o exception: return any errors or warnings in this structure.
284 static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
285 Image *image,ExceptionInfo *exception)
290 register const Quantum
304 Open output image file.
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);
312 assert(exception != (ExceptionInfo *) NULL);
313 assert(exception->signature == MagickSignature);
314 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
315 if (status == MagickFalse)
317 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
318 (void) TransformImageColorspace(image,sRGBColorspace,exception);
320 Convert image to a bi-level image.
322 (void) SetImageType(image,BilevelType,exception);
323 for (y=0; y < (ssize_t) image->rows; y++)
325 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
326 if (p == (const Quantum *) NULL)
330 for (x=0; x < (ssize_t) image->columns; x++)
333 if (image->endian == LSBEndian)
335 if (GetPixelLuma(image,p) < (QuantumRange/2.0))
339 if (GetPixelLuma(image,p) >= (QuantumRange/2.0))
344 (void) WriteBlobByte(image,(unsigned char) byte);
348 p+=GetPixelChannels(image);
351 (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit)));
352 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
354 if (status == MagickFalse)
357 (void) CloseBlob(image);