blob: 4204818f30d3b23fb2fd8e8170ff60d88990ed1b [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% W W BBBB M M PPPP %
7% W W B B MM MM P P %
8% W W W BBBB M M M PPPP %
9% WW WW B B M M P %
10% W W BBBB M M P %
11% %
12% %
13% Read/Write Wireless Bitmap (level 0) Image Format %
14% %
15% Software Design %
16% John Cristy %
17% January 2000 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38/*
39 Include declarations.
40*/
cristy4c08aed2011-07-01 19:47:50 +000041#include "MagickCore/studio.h"
cristyd9ecd042012-06-17 18:26:12 +000042#include "MagickCore/attribute.h"
cristy4c08aed2011-07-01 19:47:50 +000043#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"
cristy510d06a2011-07-06 23:43:54 +000049#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000050#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"
cristy3ed852e2009-09-05 21:47:34 +000064
65/*
66 Forward declarations.
67*/
68static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000069 WriteWBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000070
71/*
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73% %
74% %
75% %
76% R e a d W B M P I m a g e %
77% %
78% %
79% %
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81%
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.
85%
86% ReadWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
87%
88% The format of the ReadWBMPImage method is:
89%
90% Image *ReadWBMPImage(const ImageInfo *image_info,
91% ExceptionInfo *exception)
92%
93% A description of each parameter follows:
94%
95% o image_info: the image info.
96%
97% o exception: return any errors or warnings in this structure.
98%
99*/
100
cristybb503372010-05-27 20:51:26 +0000101static MagickBooleanType WBMPReadInteger(Image *image,size_t *value)
cristy3ed852e2009-09-05 21:47:34 +0000102{
103 int
104 byte;
105
106 *value=0;
107 do
108 {
109 byte=ReadBlobByte(image);
110 if (byte == EOF)
111 return(MagickFalse);
112 *value<<=7;
113 *value|=(unsigned int) (byte & 0x7f);
114 } while (byte & 0x80);
115 return(MagickTrue);
116}
117
118static Image *ReadWBMPImage(const ImageInfo *image_info,
119 ExceptionInfo *exception)
120{
121 Image
122 *image;
123
124 int
125 byte;
126
cristy3ed852e2009-09-05 21:47:34 +0000127 MagickBooleanType
128 status;
129
cristybb503372010-05-27 20:51:26 +0000130 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000131 x;
132
cristy4c08aed2011-07-01 19:47:50 +0000133 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000134 *q;
135
cristyc6da28e2011-04-28 01:41:35 +0000136 ssize_t
137 y;
138
cristy3ed852e2009-09-05 21:47:34 +0000139 unsigned char
140 bit;
141
142 unsigned short
143 header;
144
145 /*
146 Open image file.
147 */
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);
cristy9950d572011-10-01 18:22:35 +0000155 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000156 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
157 if (status == MagickFalse)
158 {
159 image=DestroyImageList(image);
160 return((Image *) NULL);
161 }
162 if (ReadBlob(image,2,(unsigned char *) &header) == 0)
163 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
164 if (header != 0)
165 ThrowReaderException(CoderError,"OnlyLevelZerofilesSupported");
166 /*
167 Initialize image structure.
168 */
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");
cristyd4297022010-09-16 22:59:09 +0000175 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
176 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
177 image->filename);
cristy018f07f2011-09-04 21:15:19 +0000178 if (AcquireImageColormap(image,2,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000179 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
180 if (image_info->ping != MagickFalse)
181 {
182 (void) CloseBlob(image);
183 return(GetFirstImageInList(image));
184 }
185 /*
186 Convert bi-level image to pixel packets.
187 */
cristybb503372010-05-27 20:51:26 +0000188 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000189 {
190 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000191 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000192 break;
cristy3ed852e2009-09-05 21:47:34 +0000193 bit=0;
194 byte=0;
cristybb503372010-05-27 20:51:26 +0000195 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000196 {
197 if (bit == 0)
198 {
199 byte=ReadBlobByte(image);
200 if (byte == EOF)
201 ThrowReaderException(CorruptImageError,"CorruptImage");
202 }
cristy4c08aed2011-07-01 19:47:50 +0000203 SetPixelIndex(image,(byte & (0x01 << (7-bit))) ? 1 : 0,q);
cristy3ed852e2009-09-05 21:47:34 +0000204 bit++;
205 if (bit == 8)
206 bit=0;
cristyed231572011-07-14 02:18:59 +0000207 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000208 }
209 if (SyncAuthenticPixels(image,exception) == MagickFalse)
210 break;
cristycee97112010-05-28 00:44:52 +0000211 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
212 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000213 if (status == MagickFalse)
214 break;
215 }
cristyea1a8aa2011-10-20 13:24:06 +0000216 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000217 if (EOFBlob(image) != MagickFalse)
218 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
219 image->filename);
220 (void) CloseBlob(image);
221 return(GetFirstImageInList(image));
222}
223
224/*
225%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226% %
227% %
228% %
229% R e g i s t e r W B M P I m a g e %
230% %
231% %
232% %
233%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234%
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.
241%
242% The format of the RegisterWBMPImage method is:
243%
cristybb503372010-05-27 20:51:26 +0000244% size_t RegisterWBMPImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000245%
246*/
cristybb503372010-05-27 20:51:26 +0000247ModuleExport size_t RegisterWBMPImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000248{
249 MagickInfo
250 *entry;
251
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);
260}
261
262/*
263%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264% %
265% %
266% %
267% U n r e g i s t e r W B M P I m a g e %
268% %
269% %
270% %
271%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272%
273% UnregisterWBMPImage() removes format registrations made by the
274% WBMP module from the list of supported formats.
275%
276% The format of the UnregisterWBMPImage method is:
277%
278% UnregisterWBMPImage(void)
279%
280*/
281ModuleExport void UnregisterWBMPImage(void)
282{
283 (void) UnregisterMagickInfo("WBMP");
284}
285
286/*
287%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288% %
289% %
290% %
291% W r i t e W B M P I m a g e %
292% %
293% %
294% %
295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296%
297% WriteWBMPImage() writes an image to a file in the Wireless Bitmap
298% (level 0) image format.
299%
300% WriteWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
301%
302% The format of the WriteWBMPImage method is:
303%
304% MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +0000305% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000306%
307% A description of each parameter follows.
308%
309% o image_info: the image info.
310%
311% o image: The image.
312%
cristy3a37efd2011-08-28 20:31:03 +0000313% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +0000314%
315*/
316
cristybb503372010-05-27 20:51:26 +0000317static void WBMPWriteInteger(Image *image,const size_t value)
cristy3ed852e2009-09-05 21:47:34 +0000318{
319 int
320 bits,
321 flag,
322 n;
323
cristybb503372010-05-27 20:51:26 +0000324 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000325 i;
326
327 unsigned char
328 buffer[5],
329 octet;
330
331 n=1;
332 bits=28;
333 flag=MagickFalse;
334 for (i=4; i >= 0; i--)
335 {
336 octet=(unsigned char) ((value >> bits) & 0x7f);
337 if ((flag == 0) && (octet != 0))
338 {
339 flag=MagickTrue;
340 n=i+1;
341 }
342 buffer[4-i]=octet | (i && (flag || octet))*(0x01 << 7);
343 bits-=7;
344 }
345 (void) WriteBlob(image,(size_t) n,buffer+5-n);
346}
347
348static MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +0000349 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000350{
cristy3ed852e2009-09-05 21:47:34 +0000351 MagickBooleanType
352 status;
353
cristy4c08aed2011-07-01 19:47:50 +0000354 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000355 *p;
356
cristybb503372010-05-27 20:51:26 +0000357 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000358 x;
359
cristyc6da28e2011-04-28 01:41:35 +0000360 ssize_t
361 y;
362
cristy3ed852e2009-09-05 21:47:34 +0000363 unsigned char
364 bit,
365 byte;
366
367 /*
368 Open output image file.
369 */
370 assert(image_info != (const ImageInfo *) NULL);
371 assert(image_info->signature == MagickSignature);
372 assert(image != (Image *) NULL);
373 assert(image->signature == MagickSignature);
374 if (image->debug != MagickFalse)
375 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000376 assert(exception != (ExceptionInfo *) NULL);
377 assert(exception->signature == MagickSignature);
378 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000379 if (status == MagickFalse)
380 return(status);
cristyd9ecd042012-06-17 18:26:12 +0000381 if ((IssRGBColorspace(image->colorspace) == MagickFalse) &&
382 (IsImageGray(image,exception) == MagickFalse))
cristy8d951092012-02-08 18:54:56 +0000383 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000384 /*
385 Convert image to a bi-level image.
386 */
cristy018f07f2011-09-04 21:15:19 +0000387 (void) SetImageType(image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000388 (void) WriteBlobMSBShort(image,0);
389 WBMPWriteInteger(image,image->columns);
390 WBMPWriteInteger(image,image->rows);
cristybb503372010-05-27 20:51:26 +0000391 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000392 {
cristy3a37efd2011-08-28 20:31:03 +0000393 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000394 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000395 break;
cristy3ed852e2009-09-05 21:47:34 +0000396 bit=0;
397 byte=0;
cristybb503372010-05-27 20:51:26 +0000398 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000399 {
cristy4c08aed2011-07-01 19:47:50 +0000400 if (GetPixelIntensity(image,p) >= ((MagickRealType) QuantumRange/2.0))
cristy3ed852e2009-09-05 21:47:34 +0000401 byte|=0x1 << (7-bit);
402 bit++;
403 if (bit == 8)
404 {
405 (void) WriteBlobByte(image,byte);
406 bit=0;
407 byte=0;
408 }
cristyed231572011-07-14 02:18:59 +0000409 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000410 }
411 if (bit != 0)
412 (void) WriteBlobByte(image,byte);
cristycee97112010-05-28 00:44:52 +0000413 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000414 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000415 if (status == MagickFalse)
416 break;
417 }
418 (void) CloseBlob(image);
419 return(MagickTrue);
420}