2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % W W W BBBB M M M PPPP %
13 % Read/Write Wireless Bitmap (level 0) Image Format %
20 % Copyright 1999-2009 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/colorspace.h"
47 #include "magick/exception.h"
48 #include "magick/exception-private.h"
49 #include "magick/image.h"
50 #include "magick/image-private.h"
51 #include "magick/list.h"
52 #include "magick/magick.h"
53 #include "magick/memory_.h"
54 #include "magick/monitor.h"
55 #include "magick/monitor-private.h"
56 #include "magick/quantum-private.h"
57 #include "magick/static.h"
58 #include "magick/string_.h"
59 #include "magick/module.h"
64 static MagickBooleanType
65 WriteWBMPImage(const ImageInfo *,Image *);
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % R e a d W B M P I m a g e %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % ReadWBMPImage() reads a WBMP (level 0) image file and returns it. It
79 % allocates the memory necessary for the new Image structure and returns a
80 % pointer to the new image.
82 % ReadWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
84 % The format of the ReadWBMPImage method is:
86 % Image *ReadWBMPImage(const ImageInfo *image_info,
87 % ExceptionInfo *exception)
89 % A description of each parameter follows:
91 % o image_info: the image info.
93 % o exception: return any errors or warnings in this structure.
97 static MagickBooleanType WBMPReadInteger(Image *image,unsigned long *value)
105 byte=ReadBlobByte(image);
109 *value|=(unsigned int) (byte & 0x7f);
110 } while (byte & 0x80);
114 static Image *ReadWBMPImage(const ImageInfo *image_info,
115 ExceptionInfo *exception)
150 assert(image_info != (const ImageInfo *) NULL);
151 assert(image_info->signature == MagickSignature);
152 if (image_info->debug != MagickFalse)
153 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
154 image_info->filename);
155 assert(exception != (ExceptionInfo *) NULL);
156 assert(exception->signature == MagickSignature);
157 image=AcquireImage(image_info);
158 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
159 if (status == MagickFalse)
161 image=DestroyImageList(image);
162 return((Image *) NULL);
164 if (ReadBlob(image,2,(unsigned char *) &header) == 0)
165 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
167 ThrowReaderException(CoderError,"OnlyLevelZerofilesSupported");
169 Initialize image structure.
171 if (WBMPReadInteger(image,&image->columns) == MagickFalse)
172 ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
173 if (WBMPReadInteger(image,&image->rows) == MagickFalse)
174 ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
175 if ((image->columns == 0) || (image->rows == 0))
176 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
177 for (i=0; i < image->offset; i++)
178 if (ReadBlobByte(image) == EOF)
180 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
184 if (AcquireImageColormap(image,2) == MagickFalse)
185 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
186 if (image_info->ping != MagickFalse)
188 (void) CloseBlob(image);
189 return(GetFirstImageInList(image));
192 Convert bi-level image to pixel packets.
194 for (y=0; y < (long) image->rows; y++)
196 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
197 if (q == (PixelPacket *) NULL)
199 indexes=GetAuthenticIndexQueue(image);
202 for (x=0; x < (long) image->columns; x++)
206 byte=ReadBlobByte(image);
208 ThrowReaderException(CorruptImageError,"CorruptImage");
210 indexes[x]=(IndexPacket) ((byte & (0x01 << (7-bit))) ? 1 : 0);
215 if (SyncAuthenticPixels(image,exception) == MagickFalse)
217 status=SetImageProgress(image,LoadImageTag,y,image->rows);
218 if (status == MagickFalse)
221 (void) SyncImage(image);
222 if (EOFBlob(image) != MagickFalse)
223 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
225 (void) CloseBlob(image);
226 return(GetFirstImageInList(image));
230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 % R e g i s t e r W B M P I m a g e %
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % RegisterWBMPImage() adds attributes for the WBMP image format to
241 % the list of supported formats. The attributes include the image format
242 % tag, a method to read and/or write the format, whether the format
243 % supports the saving of more than one frame to the same file or blob,
244 % whether the format supports native in-memory I/O, and a brief
245 % description of the format.
247 % The format of the RegisterWBMPImage method is:
249 % unsigned long RegisterWBMPImage(void)
252 ModuleExport unsigned long RegisterWBMPImage(void)
257 entry=SetMagickInfo("WBMP");
258 entry->decoder=(DecodeImageHandler *) ReadWBMPImage;
259 entry->encoder=(EncodeImageHandler *) WriteWBMPImage;
260 entry->adjoin=MagickFalse;
261 entry->description=ConstantString("Wireless Bitmap (level 0) image");
262 entry->module=ConstantString("WBMP");
263 (void) RegisterMagickInfo(entry);
264 return(MagickImageCoderSignature);
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 % U n r e g i s t e r W B M P I m a g e %
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 % UnregisterWBMPImage() removes format registrations made by the
279 % WBMP module from the list of supported formats.
281 % The format of the UnregisterWBMPImage method is:
283 % UnregisterWBMPImage(void)
286 ModuleExport void UnregisterWBMPImage(void)
288 (void) UnregisterMagickInfo("WBMP");
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 % W r i t e W B M P I m a g e %
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 % WriteWBMPImage() writes an image to a file in the Wireless Bitmap
303 % (level 0) image format.
305 % WriteWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
307 % The format of the WriteWBMPImage method is:
309 % MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
312 % A description of each parameter follows.
314 % o image_info: the image info.
316 % o image: The image.
321 static void WBMPWriteInteger(Image *image,const unsigned long value)
338 for (i=4; i >= 0; i--)
340 octet=(unsigned char) ((value >> bits) & 0x7f);
341 if ((flag == 0) && (octet != 0))
346 buffer[4-i]=octet | (i && (flag || octet))*(0x01 << 7);
349 (void) WriteBlob(image,(size_t) n,buffer+5-n);
352 static MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
361 register const IndexPacket
364 register const PixelPacket
375 Open output image file.
377 assert(image_info != (const ImageInfo *) NULL);
378 assert(image_info->signature == MagickSignature);
379 assert(image != (Image *) NULL);
380 assert(image->signature == MagickSignature);
381 if (image->debug != MagickFalse)
382 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
383 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
384 if (status == MagickFalse)
386 if (image->colorspace != RGBColorspace)
387 (void) TransformImageColorspace(image,RGBColorspace);
389 Convert image to a bi-level image.
391 (void) SetImageType(image,BilevelType);
392 (void) WriteBlobMSBShort(image,0);
393 WBMPWriteInteger(image,image->columns);
394 WBMPWriteInteger(image,image->rows);
395 for (y=0; y < (long) image->rows; y++)
397 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
398 if (p == (const PixelPacket *) NULL)
400 indexes=GetVirtualIndexQueue(image);
403 for (x=0; x < (long) image->columns; x++)
405 if (PixelIntensity(p) >= ((MagickRealType) QuantumRange/2.0))
406 byte|=0x1 << (7-bit);
410 (void) WriteBlobByte(image,byte);
417 (void) WriteBlobByte(image,byte);
418 status=SetImageProgress(image,SaveImageTag,y,image->rows);
419 if (status == MagickFalse)
422 (void) CloseBlob(image);