blob: f915d1a589ec51190496d361734ee11c713ee62c [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"
42#include "MagickCore/blob.h"
43#include "MagickCore/blob-private.h"
44#include "MagickCore/cache.h"
45#include "MagickCore/color-private.h"
46#include "MagickCore/colormap.h"
47#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000048#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000049#include "MagickCore/exception.h"
50#include "MagickCore/exception-private.h"
51#include "MagickCore/image.h"
52#include "MagickCore/image-private.h"
53#include "MagickCore/list.h"
54#include "MagickCore/magick.h"
55#include "MagickCore/memory_.h"
56#include "MagickCore/monitor.h"
57#include "MagickCore/monitor-private.h"
58#include "MagickCore/pixel-accessor.h"
59#include "MagickCore/quantum-private.h"
60#include "MagickCore/static.h"
61#include "MagickCore/string_.h"
62#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000063
64/*
65 Forward declarations.
66*/
67static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000068 WriteWBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000069
70/*
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72% %
73% %
74% %
75% R e a d W B M P I m a g e %
76% %
77% %
78% %
79%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80%
81% ReadWBMPImage() reads a WBMP (level 0) image file and returns it. It
82% allocates the memory necessary for the new Image structure and returns a
83% pointer to the new image.
84%
85% ReadWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
86%
87% The format of the ReadWBMPImage method is:
88%
89% Image *ReadWBMPImage(const ImageInfo *image_info,
90% ExceptionInfo *exception)
91%
92% A description of each parameter follows:
93%
94% o image_info: the image info.
95%
96% o exception: return any errors or warnings in this structure.
97%
98*/
99
cristybb503372010-05-27 20:51:26 +0000100static MagickBooleanType WBMPReadInteger(Image *image,size_t *value)
cristy3ed852e2009-09-05 21:47:34 +0000101{
102 int
103 byte;
104
105 *value=0;
106 do
107 {
108 byte=ReadBlobByte(image);
109 if (byte == EOF)
110 return(MagickFalse);
111 *value<<=7;
112 *value|=(unsigned int) (byte & 0x7f);
113 } while (byte & 0x80);
114 return(MagickTrue);
115}
116
117static Image *ReadWBMPImage(const ImageInfo *image_info,
118 ExceptionInfo *exception)
119{
120 Image
121 *image;
122
123 int
124 byte;
125
cristy3ed852e2009-09-05 21:47:34 +0000126 MagickBooleanType
127 status;
128
cristybb503372010-05-27 20:51:26 +0000129 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000130 x;
131
cristy4c08aed2011-07-01 19:47:50 +0000132 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000133 *q;
134
cristyc6da28e2011-04-28 01:41:35 +0000135 ssize_t
136 y;
137
cristy3ed852e2009-09-05 21:47:34 +0000138 unsigned char
139 bit;
140
141 unsigned short
142 header;
143
144 /*
145 Open image file.
146 */
147 assert(image_info != (const ImageInfo *) NULL);
148 assert(image_info->signature == MagickSignature);
149 if (image_info->debug != MagickFalse)
150 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
151 image_info->filename);
152 assert(exception != (ExceptionInfo *) NULL);
153 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000154 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000155 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
156 if (status == MagickFalse)
157 {
158 image=DestroyImageList(image);
159 return((Image *) NULL);
160 }
161 if (ReadBlob(image,2,(unsigned char *) &header) == 0)
162 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
163 if (header != 0)
164 ThrowReaderException(CoderError,"OnlyLevelZerofilesSupported");
165 /*
166 Initialize image structure.
167 */
168 if (WBMPReadInteger(image,&image->columns) == MagickFalse)
169 ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
170 if (WBMPReadInteger(image,&image->rows) == MagickFalse)
171 ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
172 if ((image->columns == 0) || (image->rows == 0))
173 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristyd4297022010-09-16 22:59:09 +0000174 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
175 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
176 image->filename);
cristy018f07f2011-09-04 21:15:19 +0000177 if (AcquireImageColormap(image,2,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000178 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
179 if (image_info->ping != MagickFalse)
180 {
181 (void) CloseBlob(image);
182 return(GetFirstImageInList(image));
183 }
184 /*
185 Convert bi-level image to pixel packets.
186 */
cristybb503372010-05-27 20:51:26 +0000187 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000188 {
189 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000190 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000191 break;
cristy3ed852e2009-09-05 21:47:34 +0000192 bit=0;
193 byte=0;
cristybb503372010-05-27 20:51:26 +0000194 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000195 {
196 if (bit == 0)
197 {
198 byte=ReadBlobByte(image);
199 if (byte == EOF)
200 ThrowReaderException(CorruptImageError,"CorruptImage");
201 }
cristy4c08aed2011-07-01 19:47:50 +0000202 SetPixelIndex(image,(byte & (0x01 << (7-bit))) ? 1 : 0,q);
cristy3ed852e2009-09-05 21:47:34 +0000203 bit++;
204 if (bit == 8)
205 bit=0;
cristyed231572011-07-14 02:18:59 +0000206 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000207 }
208 if (SyncAuthenticPixels(image,exception) == MagickFalse)
209 break;
cristycee97112010-05-28 00:44:52 +0000210 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
211 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000212 if (status == MagickFalse)
213 break;
214 }
cristyea1a8aa2011-10-20 13:24:06 +0000215 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000216 if (EOFBlob(image) != MagickFalse)
217 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
218 image->filename);
219 (void) CloseBlob(image);
220 return(GetFirstImageInList(image));
221}
222
223/*
224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225% %
226% %
227% %
228% R e g i s t e r W B M P I m a g e %
229% %
230% %
231% %
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233%
234% RegisterWBMPImage() adds attributes for the WBMP image format to
235% the list of supported formats. The attributes include the image format
236% tag, a method to read and/or write the format, whether the format
237% supports the saving of more than one frame to the same file or blob,
238% whether the format supports native in-memory I/O, and a brief
239% description of the format.
240%
241% The format of the RegisterWBMPImage method is:
242%
cristybb503372010-05-27 20:51:26 +0000243% size_t RegisterWBMPImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000244%
245*/
cristybb503372010-05-27 20:51:26 +0000246ModuleExport size_t RegisterWBMPImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000247{
248 MagickInfo
249 *entry;
250
251 entry=SetMagickInfo("WBMP");
252 entry->decoder=(DecodeImageHandler *) ReadWBMPImage;
253 entry->encoder=(EncodeImageHandler *) WriteWBMPImage;
254 entry->adjoin=MagickFalse;
255 entry->description=ConstantString("Wireless Bitmap (level 0) image");
256 entry->module=ConstantString("WBMP");
257 (void) RegisterMagickInfo(entry);
258 return(MagickImageCoderSignature);
259}
260
261/*
262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263% %
264% %
265% %
266% U n r e g i s t e r W B M P I m a g e %
267% %
268% %
269% %
270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271%
272% UnregisterWBMPImage() removes format registrations made by the
273% WBMP module from the list of supported formats.
274%
275% The format of the UnregisterWBMPImage method is:
276%
277% UnregisterWBMPImage(void)
278%
279*/
280ModuleExport void UnregisterWBMPImage(void)
281{
282 (void) UnregisterMagickInfo("WBMP");
283}
284
285/*
286%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287% %
288% %
289% %
290% W r i t e W B M P I m a g e %
291% %
292% %
293% %
294%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295%
296% WriteWBMPImage() writes an image to a file in the Wireless Bitmap
297% (level 0) image format.
298%
299% WriteWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
300%
301% The format of the WriteWBMPImage method is:
302%
303% MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +0000304% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000305%
306% A description of each parameter follows.
307%
308% o image_info: the image info.
309%
310% o image: The image.
311%
cristy3a37efd2011-08-28 20:31:03 +0000312% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +0000313%
314*/
315
cristybb503372010-05-27 20:51:26 +0000316static void WBMPWriteInteger(Image *image,const size_t value)
cristy3ed852e2009-09-05 21:47:34 +0000317{
318 int
319 bits,
320 flag,
321 n;
322
cristybb503372010-05-27 20:51:26 +0000323 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000324 i;
325
326 unsigned char
327 buffer[5],
328 octet;
329
330 n=1;
331 bits=28;
332 flag=MagickFalse;
333 for (i=4; i >= 0; i--)
334 {
335 octet=(unsigned char) ((value >> bits) & 0x7f);
336 if ((flag == 0) && (octet != 0))
337 {
338 flag=MagickTrue;
339 n=i+1;
340 }
341 buffer[4-i]=octet | (i && (flag || octet))*(0x01 << 7);
342 bits-=7;
343 }
344 (void) WriteBlob(image,(size_t) n,buffer+5-n);
345}
346
347static MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +0000348 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000349{
cristy3ed852e2009-09-05 21:47:34 +0000350 MagickBooleanType
351 status;
352
cristy4c08aed2011-07-01 19:47:50 +0000353 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000354 *p;
355
cristybb503372010-05-27 20:51:26 +0000356 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000357 x;
358
cristyc6da28e2011-04-28 01:41:35 +0000359 ssize_t
360 y;
361
cristy3ed852e2009-09-05 21:47:34 +0000362 unsigned char
363 bit,
364 byte;
365
366 /*
367 Open output image file.
368 */
369 assert(image_info != (const ImageInfo *) NULL);
370 assert(image_info->signature == MagickSignature);
371 assert(image != (Image *) NULL);
372 assert(image->signature == MagickSignature);
373 if (image->debug != MagickFalse)
374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000375 assert(exception != (ExceptionInfo *) NULL);
376 assert(exception->signature == MagickSignature);
377 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000378 if (status == MagickFalse)
379 return(status);
cristy510d06a2011-07-06 23:43:54 +0000380 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +0000381 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000382 /*
383 Convert image to a bi-level image.
384 */
cristy018f07f2011-09-04 21:15:19 +0000385 (void) SetImageType(image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000386 (void) WriteBlobMSBShort(image,0);
387 WBMPWriteInteger(image,image->columns);
388 WBMPWriteInteger(image,image->rows);
cristybb503372010-05-27 20:51:26 +0000389 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000390 {
cristy3a37efd2011-08-28 20:31:03 +0000391 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000392 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000393 break;
cristy3ed852e2009-09-05 21:47:34 +0000394 bit=0;
395 byte=0;
cristybb503372010-05-27 20:51:26 +0000396 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000397 {
cristy4c08aed2011-07-01 19:47:50 +0000398 if (GetPixelIntensity(image,p) >= ((MagickRealType) QuantumRange/2.0))
cristy3ed852e2009-09-05 21:47:34 +0000399 byte|=0x1 << (7-bit);
400 bit++;
401 if (bit == 8)
402 {
403 (void) WriteBlobByte(image,byte);
404 bit=0;
405 byte=0;
406 }
cristyed231572011-07-14 02:18:59 +0000407 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000408 }
409 if (bit != 0)
410 (void) WriteBlobByte(image,byte);
cristycee97112010-05-28 00:44:52 +0000411 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000412 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000413 if (status == MagickFalse)
414 break;
415 }
416 (void) CloseBlob(image);
417 return(MagickTrue);
418}