2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % W W W BBBB M M M PPPP %
13 % Read/Write Wireless Bitmap (level 0) Image 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 #include "MagickCore/studio.h"
42 #include "MagickCore/blob.h"
43 #include "MagickCore/blob-private.h"
44 #include "MagickCore/cache.h"
45 #include "MagickCore/color-private.h"
46 #include "MagickCore/colormap.h"
47 #include "MagickCore/colorspace.h"
48 #include "MagickCore/colorspace-private.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 WriteWBMPImage(const ImageInfo *,Image *);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % R e a d W B M P I m a g e %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % ReadWBMPImage() reads a WBMP (level 0) image file and returns it. It
82 % allocates the memory necessary for the new Image structure and returns a
83 % pointer to the new image.
85 % ReadWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
87 % The format of the ReadWBMPImage method is:
89 % Image *ReadWBMPImage(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.
100 static MagickBooleanType WBMPReadInteger(Image *image,size_t *value)
108 byte=ReadBlobByte(image);
112 *value|=(unsigned int) (byte & 0x7f);
113 } while (byte & 0x80);
117 static Image *ReadWBMPImage(const ImageInfo *image_info,
118 ExceptionInfo *exception)
147 assert(image_info != (const ImageInfo *) NULL);
148 assert(image_info->signature == MagickSignature);
149 if (image_info->debug != MagickFalse)
150 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
151 image_info->filename);
152 assert(exception != (ExceptionInfo *) NULL);
153 assert(exception->signature == MagickSignature);
154 image=AcquireImage(image_info);
155 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
156 if (status == MagickFalse)
158 image=DestroyImageList(image);
159 return((Image *) NULL);
161 if (ReadBlob(image,2,(unsigned char *) &header) == 0)
162 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
164 ThrowReaderException(CoderError,"OnlyLevelZerofilesSupported");
166 Initialize image structure.
168 if (WBMPReadInteger(image,&image->columns) == MagickFalse)
169 ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
170 if (WBMPReadInteger(image,&image->rows) == MagickFalse)
171 ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
172 if ((image->columns == 0) || (image->rows == 0))
173 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
174 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
175 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
177 if (AcquireImageColormap(image,2) == MagickFalse)
178 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
179 if (image_info->ping != MagickFalse)
181 (void) CloseBlob(image);
182 return(GetFirstImageInList(image));
185 Convert bi-level image to pixel packets.
187 for (y=0; y < (ssize_t) image->rows; y++)
189 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
190 if (q == (const Quantum *) NULL)
194 for (x=0; x < (ssize_t) image->columns; x++)
198 byte=ReadBlobByte(image);
200 ThrowReaderException(CorruptImageError,"CorruptImage");
202 SetPixelIndex(image,(byte & (0x01 << (7-bit))) ? 1 : 0,q);
206 q+=GetPixelChannels(image);
208 if (SyncAuthenticPixels(image,exception) == MagickFalse)
210 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
212 if (status == MagickFalse)
215 (void) SyncImage(image);
216 if (EOFBlob(image) != MagickFalse)
217 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
219 (void) CloseBlob(image);
220 return(GetFirstImageInList(image));
224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 % R e g i s t e r W B M P I m a g e %
232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 % RegisterWBMPImage() adds attributes for the WBMP image format to
235 % the list of supported formats. The attributes include the image format
236 % tag, a method to read and/or write the format, whether the format
237 % supports the saving of more than one frame to the same file or blob,
238 % whether the format supports native in-memory I/O, and a brief
239 % description of the format.
241 % The format of the RegisterWBMPImage method is:
243 % size_t RegisterWBMPImage(void)
246 ModuleExport size_t RegisterWBMPImage(void)
251 entry=SetMagickInfo("WBMP");
252 entry->decoder=(DecodeImageHandler *) ReadWBMPImage;
253 entry->encoder=(EncodeImageHandler *) WriteWBMPImage;
254 entry->adjoin=MagickFalse;
255 entry->description=ConstantString("Wireless Bitmap (level 0) image");
256 entry->module=ConstantString("WBMP");
257 (void) RegisterMagickInfo(entry);
258 return(MagickImageCoderSignature);
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 % U n r e g i s t e r W B M P I m a g e %
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 % UnregisterWBMPImage() removes format registrations made by the
273 % WBMP module from the list of supported formats.
275 % The format of the UnregisterWBMPImage method is:
277 % UnregisterWBMPImage(void)
280 ModuleExport void UnregisterWBMPImage(void)
282 (void) UnregisterMagickInfo("WBMP");
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 % W r i t e W B M P I m a g e %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 % WriteWBMPImage() writes an image to a file in the Wireless Bitmap
297 % (level 0) image format.
299 % WriteWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
301 % The format of the WriteWBMPImage method is:
303 % MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
306 % A description of each parameter follows.
308 % o image_info: the image info.
310 % o image: The image.
315 static void WBMPWriteInteger(Image *image,const size_t value)
332 for (i=4; i >= 0; i--)
334 octet=(unsigned char) ((value >> bits) & 0x7f);
335 if ((flag == 0) && (octet != 0))
340 buffer[4-i]=octet | (i && (flag || octet))*(0x01 << 7);
343 (void) WriteBlob(image,(size_t) n,buffer+5-n);
346 static MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
352 register const Quantum
366 Open output image file.
368 assert(image_info != (const ImageInfo *) NULL);
369 assert(image_info->signature == MagickSignature);
370 assert(image != (Image *) NULL);
371 assert(image->signature == MagickSignature);
372 if (image->debug != MagickFalse)
373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
374 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
375 if (status == MagickFalse)
377 if (IsRGBColorspace(image->colorspace) == MagickFalse)
378 (void) TransformImageColorspace(image,RGBColorspace);
380 Convert image to a bi-level image.
382 (void) SetImageType(image,BilevelType);
383 (void) WriteBlobMSBShort(image,0);
384 WBMPWriteInteger(image,image->columns);
385 WBMPWriteInteger(image,image->rows);
386 for (y=0; y < (ssize_t) image->rows; y++)
388 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
389 if (p == (const Quantum *) NULL)
393 for (x=0; x < (ssize_t) image->columns; x++)
395 if (GetPixelIntensity(image,p) >= ((MagickRealType) QuantumRange/2.0))
396 byte|=0x1 << (7-bit);
400 (void) WriteBlobByte(image,byte);
404 p+=GetPixelChannels(image);
407 (void) WriteBlobByte(image,byte);
408 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
410 if (status == MagickFalse)
413 (void) CloseBlob(image);