]> granicus.if.org Git - imagemagick/blob - coders/wbmp.c
1a157a0b59f918957fa1ab98e7a9c5d881cea706
[imagemagick] / coders / wbmp.c
1 /*
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 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  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 */
41 #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"
48 #include "MagickCore/colorspace-private.h"
49 #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"
63 \f
64 /*
65   Forward declarations.
66 */
67 static MagickBooleanType
68   WriteWBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
69 \f
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
100 static MagickBooleanType WBMPReadInteger(Image *image,size_t *value)
101 {
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
117 static Image *ReadWBMPImage(const ImageInfo *image_info,
118   ExceptionInfo *exception)
119 {
120   Image
121     *image;
122
123   int
124     byte;
125
126   MagickBooleanType
127     status;
128
129   register ssize_t
130     x;
131
132   register Quantum
133     *q;
134
135   ssize_t
136     y;
137
138   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);
154   image=AcquireImage(image_info);
155   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");
174   if (DiscardBlobBytes(image,image->offset) == MagickFalse)
175     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
176       image->filename);
177   if (AcquireImageColormap(image,2,exception) == MagickFalse)
178     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   */
187   for (y=0; y < (ssize_t) image->rows; y++)
188   {
189     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
190     if (q == (Quantum *) NULL)
191       break;
192     bit=0;
193     byte=0;
194     for (x=0; x < (ssize_t) image->columns; x++)
195     {
196       if (bit == 0)
197         {
198           byte=ReadBlobByte(image);
199           if (byte == EOF)
200             ThrowReaderException(CorruptImageError,"CorruptImage");
201         }
202       SetPixelIndex(image,(byte & (0x01 << (7-bit))) ? 1 : 0,q);
203       bit++;
204       if (bit == 8)
205         bit=0;
206       q+=GetPixelChannels(image);
207     }
208     if (SyncAuthenticPixels(image,exception) == MagickFalse)
209       break;
210     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
211                 image->rows);
212     if (status == MagickFalse)
213       break;
214   }
215   (void) SyncImage(image);
216   if (EOFBlob(image) != MagickFalse)
217     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
218       image->filename);
219   (void) CloseBlob(image);
220   return(GetFirstImageInList(image));
221 }
222 \f
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 %
243 %      size_t RegisterWBMPImage(void)
244 %
245 */
246 ModuleExport size_t RegisterWBMPImage(void)
247 {
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 \f
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 */
280 ModuleExport void UnregisterWBMPImage(void)
281 {
282   (void) UnregisterMagickInfo("WBMP");
283 }
284 \f
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,
304 %        Image *image,ExceptionInfo *exception)
305 %
306 %  A description of each parameter follows.
307 %
308 %    o image_info: the image info.
309 %
310 %    o image:  The image.
311 %
312 %    o exception: return any errors or warnings in this structure.
313 %
314 */
315
316 static void WBMPWriteInteger(Image *image,const size_t value)
317 {
318   int
319     bits,
320     flag,
321     n;
322
323   register ssize_t
324     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
347 static MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
348   Image *image,ExceptionInfo *exception)
349 {
350   MagickBooleanType
351     status;
352
353   register const Quantum
354     *p;
355
356   register ssize_t
357     x;
358
359   ssize_t
360     y;
361
362   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);
375   assert(exception != (ExceptionInfo *) NULL);
376   assert(exception->signature == MagickSignature);
377   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
378   if (status == MagickFalse)
379     return(status);
380   if (IsRGBColorspace(image->colorspace) == MagickFalse)
381     (void) TransformImageColorspace(image,RGBColorspace);
382   /*
383     Convert image to a bi-level image.
384   */
385   (void) SetImageType(image,BilevelType,exception);
386   (void) WriteBlobMSBShort(image,0);
387   WBMPWriteInteger(image,image->columns);
388   WBMPWriteInteger(image,image->rows);
389   for (y=0; y < (ssize_t) image->rows; y++)
390   {
391     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
392     if (p == (const Quantum *) NULL)
393       break;
394     bit=0;
395     byte=0;
396     for (x=0; x < (ssize_t) image->columns; x++)
397     {
398       if (GetPixelIntensity(image,p) >= ((MagickRealType) QuantumRange/2.0))
399         byte|=0x1 << (7-bit);
400       bit++;
401       if (bit == 8)
402         {
403           (void) WriteBlobByte(image,byte);
404           bit=0;
405           byte=0;
406         }
407       p+=GetPixelChannels(image);
408     }
409     if (bit != 0)
410       (void) WriteBlobByte(image,byte);
411     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
412       image->rows);
413     if (status == MagickFalse)
414       break;
415   }
416   (void) CloseBlob(image);
417   return(MagickTrue);
418 }