2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % W W W BBBB M M M PPPP %
13 % Read/Write Wireless Bitmap (level 0) Image Format %
20 % Copyright 1999-2018 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 % https://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/attribute.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/colorspace-private.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/image.h"
53 #include "MagickCore/image-private.h"
54 #include "MagickCore/list.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/monitor.h"
58 #include "MagickCore/monitor-private.h"
59 #include "MagickCore/pixel-accessor.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/module.h"
68 static MagickBooleanType
69 WriteWBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % R e a d W B M P I m a g e %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % ReadWBMPImage() reads a WBMP (level 0) image file and returns it. It
83 % allocates the memory necessary for the new Image structure and returns a
84 % pointer to the new image.
86 % ReadWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
88 % The format of the ReadWBMPImage method is:
90 % Image *ReadWBMPImage(const ImageInfo *image_info,
91 % ExceptionInfo *exception)
93 % A description of each parameter follows:
95 % o image_info: the image info.
97 % o exception: return any errors or warnings in this structure.
101 static MagickBooleanType WBMPReadInteger(Image *image,size_t *value)
109 byte=ReadBlobByte(image);
113 *value|=(unsigned int) (byte & 0x7f);
114 } while (byte & 0x80);
118 static Image *ReadWBMPImage(const ImageInfo *image_info,
119 ExceptionInfo *exception)
148 assert(image_info != (const ImageInfo *) NULL);
149 assert(image_info->signature == MagickCoreSignature);
150 if (image_info->debug != MagickFalse)
151 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
152 image_info->filename);
153 assert(exception != (ExceptionInfo *) NULL);
154 assert(exception->signature == MagickCoreSignature);
155 image=AcquireImage(image_info,exception);
156 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
157 if (status == MagickFalse)
159 image=DestroyImageList(image);
160 return((Image *) NULL);
162 if (ReadBlob(image,2,(unsigned char *) &header) == 0)
163 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
165 ThrowReaderException(CoderError,"OnlyLevelZerofilesSupported");
167 Initialize image structure.
169 if (WBMPReadInteger(image,&image->columns) == MagickFalse)
170 ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
171 if (WBMPReadInteger(image,&image->rows) == MagickFalse)
172 ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
173 if ((image->columns == 0) || (image->rows == 0))
174 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
175 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
176 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
178 if (image_info->ping != MagickFalse)
180 (void) CloseBlob(image);
181 return(GetFirstImageInList(image));
183 status=SetImageExtent(image,image->columns,image->rows,exception);
184 if (status == MagickFalse)
185 return(DestroyImageList(image));
186 (void) SetImageBackgroundColor(image,exception);
187 if (AcquireImageColormap(image,2,exception) == MagickFalse)
188 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
190 Convert bi-level image to pixel packets.
192 for (y=0; y < (ssize_t) image->rows; y++)
194 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
195 if (q == (Quantum *) NULL)
199 for (x=0; x < (ssize_t) image->columns; x++)
203 byte=ReadBlobByte(image);
205 ThrowReaderException(CorruptImageError,"CorruptImage");
207 SetPixelIndex(image,(byte & (0x01 << (7-bit))) ? 1 : 0,q);
211 q+=GetPixelChannels(image);
213 if (SyncAuthenticPixels(image,exception) == MagickFalse)
215 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
217 if (status == MagickFalse)
220 (void) SyncImage(image,exception);
221 if (EOFBlob(image) != MagickFalse)
222 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
224 (void) CloseBlob(image);
225 return(GetFirstImageInList(image));
229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 % R e g i s t e r W B M P I m a g e %
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 % RegisterWBMPImage() adds attributes for the WBMP image format to
240 % the list of supported formats. The attributes include the image format
241 % tag, a method to read and/or write the format, whether the format
242 % supports the saving of more than one frame to the same file or blob,
243 % whether the format supports native in-memory I/O, and a brief
244 % description of the format.
246 % The format of the RegisterWBMPImage method is:
248 % size_t RegisterWBMPImage(void)
251 ModuleExport size_t RegisterWBMPImage(void)
256 entry=AcquireMagickInfo("WBMP","WBMP","Wireless Bitmap (level 0) image");
257 entry->decoder=(DecodeImageHandler *) ReadWBMPImage;
258 entry->encoder=(EncodeImageHandler *) WriteWBMPImage;
259 entry->flags^=CoderAdjoinFlag;
260 (void) RegisterMagickInfo(entry);
261 return(MagickImageCoderSignature);
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 % U n r e g i s t e r W B M P I m a g e %
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % UnregisterWBMPImage() removes format registrations made by the
276 % WBMP module from the list of supported formats.
278 % The format of the UnregisterWBMPImage method is:
280 % UnregisterWBMPImage(void)
283 ModuleExport void UnregisterWBMPImage(void)
285 (void) UnregisterMagickInfo("WBMP");
289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 % W r i t e W B M P I m a g e %
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 % WriteWBMPImage() writes an image to a file in the Wireless Bitmap
300 % (level 0) image format.
302 % WriteWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
304 % The format of the WriteWBMPImage method is:
306 % MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
307 % Image *image,ExceptionInfo *exception)
309 % A description of each parameter follows.
311 % o image_info: the image info.
313 % o image: The image.
315 % o exception: return any errors or warnings in this structure.
319 static void WBMPWriteInteger(Image *image,const size_t value)
336 for (i=4; i >= 0; i--)
338 octet=(unsigned char) ((value >> bits) & 0x7f);
339 if ((flag == 0) && (octet != 0))
344 buffer[4-i]=octet | (i && (flag || octet))*(0x01 << 7);
347 (void) WriteBlob(image,(size_t) n,buffer+5-n);
350 static MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
351 Image *image,ExceptionInfo *exception)
356 register const Quantum
370 Open output image file.
372 assert(image_info != (const ImageInfo *) NULL);
373 assert(image_info->signature == MagickCoreSignature);
374 assert(image != (Image *) NULL);
375 assert(image->signature == MagickCoreSignature);
376 if (image->debug != MagickFalse)
377 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
378 assert(exception != (ExceptionInfo *) NULL);
379 assert(exception->signature == MagickCoreSignature);
380 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
381 if (status == MagickFalse)
383 (void) TransformImageColorspace(image,sRGBColorspace,exception);
385 Convert image to a bi-level image.
387 (void) SetImageType(image,BilevelType,exception);
388 (void) WriteBlobMSBShort(image,0);
389 WBMPWriteInteger(image,image->columns);
390 WBMPWriteInteger(image,image->rows);
391 for (y=0; y < (ssize_t) image->rows; y++)
393 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
394 if (p == (const Quantum *) NULL)
398 for (x=0; x < (ssize_t) image->columns; x++)
400 if (GetPixelLuma(image,p) >= (QuantumRange/2.0))
401 byte|=0x1 << (7-bit);
405 (void) WriteBlobByte(image,byte);
409 p+=GetPixelChannels(image);
412 (void) WriteBlobByte(image,byte);
413 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
415 if (status == MagickFalse)
418 (void) CloseBlob(image);