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)
151 assert(image_info != (const ImageInfo *) NULL);
152 assert(image_info->signature == MagickSignature);
153 if (image_info->debug != MagickFalse)
154 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
155 image_info->filename);
156 assert(exception != (ExceptionInfo *) NULL);
157 assert(exception->signature == MagickSignature);
158 image=AcquireImage(image_info);
159 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
160 if (status == MagickFalse)
162 image=DestroyImageList(image);
163 return((Image *) NULL);
165 if (ReadBlob(image,2,(unsigned char *) &header) == 0)
166 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
168 ThrowReaderException(CoderError,"OnlyLevelZerofilesSupported");
170 Initialize image structure.
172 if (WBMPReadInteger(image,&image->columns) == MagickFalse)
173 ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
174 if (WBMPReadInteger(image,&image->rows) == MagickFalse)
175 ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
176 if ((image->columns == 0) || (image->rows == 0))
177 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
178 for (i=0; i < image->offset; i++)
179 if (ReadBlobByte(image) == EOF)
181 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
185 if (AcquireImageColormap(image,2) == MagickFalse)
186 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
187 if (image_info->ping != MagickFalse)
189 (void) CloseBlob(image);
190 return(GetFirstImageInList(image));
193 Convert bi-level image to pixel packets.
195 for (y=0; y < (ssize_t) image->rows; y++)
197 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
198 if (q == (PixelPacket *) NULL)
200 indexes=GetAuthenticIndexQueue(image);
203 for (x=0; x < (ssize_t) image->columns; x++)
207 byte=ReadBlobByte(image);
209 ThrowReaderException(CorruptImageError,"CorruptImage");
211 indexes[x]=(IndexPacket) ((byte & (0x01 << (7-bit))) ? 1 : 0);
216 if (SyncAuthenticPixels(image,exception) == MagickFalse)
218 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
220 if (status == MagickFalse)
223 (void) SyncImage(image);
224 if (EOFBlob(image) != MagickFalse)
225 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
227 (void) CloseBlob(image);
228 return(GetFirstImageInList(image));
232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 % R e g i s t e r W B M P I m a g e %
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 % RegisterWBMPImage() adds attributes for the WBMP image format to
243 % the list of supported formats. The attributes include the image format
244 % tag, a method to read and/or write the format, whether the format
245 % supports the saving of more than one frame to the same file or blob,
246 % whether the format supports native in-memory I/O, and a brief
247 % description of the format.
249 % The format of the RegisterWBMPImage method is:
251 % size_t RegisterWBMPImage(void)
254 ModuleExport size_t RegisterWBMPImage(void)
259 entry=SetMagickInfo("WBMP");
260 entry->decoder=(DecodeImageHandler *) ReadWBMPImage;
261 entry->encoder=(EncodeImageHandler *) WriteWBMPImage;
262 entry->adjoin=MagickFalse;
263 entry->description=ConstantString("Wireless Bitmap (level 0) image");
264 entry->module=ConstantString("WBMP");
265 (void) RegisterMagickInfo(entry);
266 return(MagickImageCoderSignature);
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % U n r e g i s t e r W B M P I m a g e %
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 % UnregisterWBMPImage() removes format registrations made by the
281 % WBMP module from the list of supported formats.
283 % The format of the UnregisterWBMPImage method is:
285 % UnregisterWBMPImage(void)
288 ModuleExport void UnregisterWBMPImage(void)
290 (void) UnregisterMagickInfo("WBMP");
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % W r i t e W B M P I m a g e %
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 % WriteWBMPImage() writes an image to a file in the Wireless Bitmap
305 % (level 0) image format.
307 % WriteWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
309 % The format of the WriteWBMPImage method is:
311 % MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
314 % A description of each parameter follows.
316 % o image_info: the image info.
318 % o image: The image.
323 static void WBMPWriteInteger(Image *image,const size_t value)
340 for (i=4; i >= 0; i--)
342 octet=(unsigned char) ((value >> bits) & 0x7f);
343 if ((flag == 0) && (octet != 0))
348 buffer[4-i]=octet | (i && (flag || octet))*(0x01 << 7);
351 (void) WriteBlob(image,(size_t) n,buffer+5-n);
354 static MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
363 register const IndexPacket
366 register const PixelPacket
377 Open output image file.
379 assert(image_info != (const ImageInfo *) NULL);
380 assert(image_info->signature == MagickSignature);
381 assert(image != (Image *) NULL);
382 assert(image->signature == MagickSignature);
383 if (image->debug != MagickFalse)
384 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
385 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
386 if (status == MagickFalse)
388 if (image->colorspace != RGBColorspace)
389 (void) TransformImageColorspace(image,RGBColorspace);
391 Convert image to a bi-level image.
393 (void) SetImageType(image,BilevelType);
394 (void) WriteBlobMSBShort(image,0);
395 WBMPWriteInteger(image,image->columns);
396 WBMPWriteInteger(image,image->rows);
397 for (y=0; y < (ssize_t) image->rows; y++)
399 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
400 if (p == (const PixelPacket *) NULL)
402 indexes=GetVirtualIndexQueue(image);
405 for (x=0; x < (ssize_t) image->columns; x++)
407 if (PixelIntensity(p) >= ((MagickRealType) QuantumRange/2.0))
408 byte|=0x1 << (7-bit);
412 (void) WriteBlobByte(image,byte);
419 (void) WriteBlobByte(image,byte);
420 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
422 if (status == MagickFalse)
425 (void) CloseBlob(image);