2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % W W W BBBB M M M PPPP %
13 % Read/Write Wireless Bitmap (level 0) Image 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 #include "magick/studio.h"
42 #include "magick/blob.h"
43 #include "magick/blob-private.h"
44 #include "magick/cache.h"
45 #include "magick/color-private.h"
46 #include "magick/colormap.h"
47 #include "magick/colorspace.h"
48 #include "magick/exception.h"
49 #include "magick/exception-private.h"
50 #include "magick/image.h"
51 #include "magick/image-private.h"
52 #include "magick/list.h"
53 #include "magick/magick.h"
54 #include "magick/memory_.h"
55 #include "magick/monitor.h"
56 #include "magick/monitor-private.h"
57 #include "magick/quantum-private.h"
58 #include "magick/static.h"
59 #include "magick/string_.h"
60 #include "magick/module.h"
65 static MagickBooleanType
66 WriteWBMPImage(const ImageInfo *,Image *);
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 % R e a d W B M P I m a g e %
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 % ReadWBMPImage() reads a WBMP (level 0) image file and returns it. It
80 % allocates the memory necessary for the new Image structure and returns a
81 % pointer to the new image.
83 % ReadWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
85 % The format of the ReadWBMPImage method is:
87 % Image *ReadWBMPImage(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.
98 static MagickBooleanType WBMPReadInteger(Image *image,size_t *value)
106 byte=ReadBlobByte(image);
110 *value|=(unsigned int) (byte & 0x7f);
111 } while (byte & 0x80);
115 static Image *ReadWBMPImage(const ImageInfo *image_info,
116 ExceptionInfo *exception)
148 assert(image_info != (const ImageInfo *) NULL);
149 assert(image_info->signature == MagickSignature);
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 == MagickSignature);
155 image=AcquireImage(image_info);
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 (AcquireImageColormap(image,2) == MagickFalse)
179 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
180 if (image_info->ping != MagickFalse)
182 (void) CloseBlob(image);
183 return(GetFirstImageInList(image));
186 Convert bi-level image to pixel packets.
188 for (y=0; y < (ssize_t) image->rows; y++)
190 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
191 if (q == (PixelPacket *) NULL)
193 indexes=GetAuthenticIndexQueue(image);
196 for (x=0; x < (ssize_t) image->columns; x++)
200 byte=ReadBlobByte(image);
202 ThrowReaderException(CorruptImageError,"CorruptImage");
204 indexes[x]=(IndexPacket) ((byte & (0x01 << (7-bit))) ? 1 : 0);
209 if (SyncAuthenticPixels(image,exception) == MagickFalse)
211 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
213 if (status == MagickFalse)
216 (void) SyncImage(image);
217 if (EOFBlob(image) != MagickFalse)
218 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
220 (void) CloseBlob(image);
221 return(GetFirstImageInList(image));
225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229 % R e g i s t e r W B M P I m a g e %
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 % RegisterWBMPImage() adds attributes for the WBMP image format to
236 % the list of supported formats. The attributes include the image format
237 % tag, a method to read and/or write the format, whether the format
238 % supports the saving of more than one frame to the same file or blob,
239 % whether the format supports native in-memory I/O, and a brief
240 % description of the format.
242 % The format of the RegisterWBMPImage method is:
244 % size_t RegisterWBMPImage(void)
247 ModuleExport size_t RegisterWBMPImage(void)
252 entry=SetMagickInfo("WBMP");
253 entry->decoder=(DecodeImageHandler *) ReadWBMPImage;
254 entry->encoder=(EncodeImageHandler *) WriteWBMPImage;
255 entry->adjoin=MagickFalse;
256 entry->description=ConstantString("Wireless Bitmap (level 0) image");
257 entry->module=ConstantString("WBMP");
258 (void) RegisterMagickInfo(entry);
259 return(MagickImageCoderSignature);
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 % U n r e g i s t e r W B M P I m a g e %
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 % UnregisterWBMPImage() removes format registrations made by the
274 % WBMP module from the list of supported formats.
276 % The format of the UnregisterWBMPImage method is:
278 % UnregisterWBMPImage(void)
281 ModuleExport void UnregisterWBMPImage(void)
283 (void) UnregisterMagickInfo("WBMP");
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % W r i t e W B M P I m a g e %
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % WriteWBMPImage() writes an image to a file in the Wireless Bitmap
298 % (level 0) image format.
300 % WriteWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
302 % The format of the WriteWBMPImage method is:
304 % MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
307 % A description of each parameter follows.
309 % o image_info: the image info.
311 % o image: The image.
316 static void WBMPWriteInteger(Image *image,const size_t value)
333 for (i=4; i >= 0; i--)
335 octet=(unsigned char) ((value >> bits) & 0x7f);
336 if ((flag == 0) && (octet != 0))
341 buffer[4-i]=octet | (i && (flag || octet))*(0x01 << 7);
344 (void) WriteBlob(image,(size_t) n,buffer+5-n);
347 static MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
356 register const IndexPacket
359 register const PixelPacket
370 Open output image file.
372 assert(image_info != (const ImageInfo *) NULL);
373 assert(image_info->signature == MagickSignature);
374 assert(image != (Image *) NULL);
375 assert(image->signature == MagickSignature);
376 if (image->debug != MagickFalse)
377 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
378 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
379 if (status == MagickFalse)
381 if (image->colorspace != RGBColorspace)
382 (void) TransformImageColorspace(image,RGBColorspace);
384 Convert image to a bi-level image.
386 (void) SetImageType(image,BilevelType);
387 (void) WriteBlobMSBShort(image,0);
388 WBMPWriteInteger(image,image->columns);
389 WBMPWriteInteger(image,image->rows);
390 for (y=0; y < (ssize_t) image->rows; y++)
392 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
393 if (p == (const PixelPacket *) NULL)
395 indexes=GetVirtualIndexQueue(image);
398 for (x=0; x < (ssize_t) image->columns; x++)
400 if (PixelIntensity(p) >= ((MagickRealType) QuantumRange/2.0))
401 byte|=0x1 << (7-bit);
405 (void) WriteBlobByte(image,byte);
412 (void) WriteBlobByte(image,byte);
413 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
415 if (status == MagickFalse)
418 (void) CloseBlob(image);