]> granicus.if.org Git - imagemagick/blob - coders/wbmp.c
(no commit message)
[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 "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"
61 \f
62 /*
63   Forward declarations.
64 */
65 static MagickBooleanType
66   WriteWBMPImage(const ImageInfo *,Image *);
67 \f
68 /*
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 %                                                                             %
71 %                                                                             %
72 %                                                                             %
73 %   R e a d W B M P I m a g e                                                 %
74 %                                                                             %
75 %                                                                             %
76 %                                                                             %
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 %
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.
82 %
83 %  ReadWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
84 %
85 %  The format of the ReadWBMPImage method is:
86 %
87 %      Image *ReadWBMPImage(const ImageInfo *image_info,
88 %        ExceptionInfo *exception)
89 %
90 %  A description of each parameter follows:
91 %
92 %    o image_info: the image info.
93 %
94 %    o exception: return any errors or warnings in this structure.
95 %
96 */
97
98 static MagickBooleanType WBMPReadInteger(Image *image,size_t *value)
99 {
100   int
101     byte;
102
103   *value=0;
104   do
105   {
106     byte=ReadBlobByte(image);
107     if (byte == EOF)
108       return(MagickFalse);
109     *value<<=7;
110     *value|=(unsigned int) (byte & 0x7f);
111   } while (byte & 0x80);
112   return(MagickTrue);
113 }
114
115 static Image *ReadWBMPImage(const ImageInfo *image_info,
116   ExceptionInfo *exception)
117 {
118   Image
119     *image;
120
121   int
122     byte;
123
124   ssize_t
125     y;
126
127   MagickBooleanType
128     status;
129
130   register IndexPacket
131     *indexes;
132
133   register ssize_t
134     x;
135
136   register PixelPacket
137     *q;
138
139   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);
155   image=AcquireImage(image_info);
156   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");
175   if (DiscardBlobBytes(image,image->offset) == MagickFalse)
176     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
177       image->filename);
178   if (AcquireImageColormap(image,2) == MagickFalse)
179     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   */
188   for (y=0; y < (ssize_t) image->rows; y++)
189   {
190     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
191     if (q == (PixelPacket *) NULL)
192       break;
193     indexes=GetAuthenticIndexQueue(image);
194     bit=0;
195     byte=0;
196     for (x=0; x < (ssize_t) image->columns; x++)
197     {
198       if (bit == 0)
199         {
200           byte=ReadBlobByte(image);
201           if (byte == EOF)
202             ThrowReaderException(CorruptImageError,"CorruptImage");
203         }
204       indexes[x]=(IndexPacket) ((byte & (0x01 << (7-bit))) ? 1 : 0);
205       bit++;
206       if (bit == 8)
207         bit=0;
208     }
209     if (SyncAuthenticPixels(image,exception) == MagickFalse)
210       break;
211     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
212                 image->rows);
213     if (status == MagickFalse)
214       break;
215   }
216   (void) SyncImage(image);
217   if (EOFBlob(image) != MagickFalse)
218     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
219       image->filename);
220   (void) CloseBlob(image);
221   return(GetFirstImageInList(image));
222 }
223 \f
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 %
244 %      size_t RegisterWBMPImage(void)
245 %
246 */
247 ModuleExport size_t RegisterWBMPImage(void)
248 {
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 \f
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 */
281 ModuleExport void UnregisterWBMPImage(void)
282 {
283   (void) UnregisterMagickInfo("WBMP");
284 }
285 \f
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,
305 %        Image *image)
306 %
307 %  A description of each parameter follows.
308 %
309 %    o image_info: the image info.
310 %
311 %    o image:  The image.
312 %
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)
349 {
350   ssize_t
351     y;
352
353   MagickBooleanType
354     status;
355
356   register const PixelPacket
357     *p;
358
359   register ssize_t
360     x;
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   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
376   if (status == MagickFalse)
377     return(status);
378   if (image->colorspace != RGBColorspace)
379     (void) TransformImageColorspace(image,RGBColorspace);
380   /*
381     Convert image to a bi-level image.
382   */
383   (void) SetImageType(image,BilevelType);
384   (void) WriteBlobMSBShort(image,0);
385   WBMPWriteInteger(image,image->columns);
386   WBMPWriteInteger(image,image->rows);
387   for (y=0; y < (ssize_t) image->rows; y++)
388   {
389     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
390     if (p == (const PixelPacket *) NULL)
391       break;
392     bit=0;
393     byte=0;
394     for (x=0; x < (ssize_t) image->columns; x++)
395     {
396       if (PixelIntensity(p) >= ((MagickRealType) QuantumRange/2.0))
397         byte|=0x1 << (7-bit);
398       bit++;
399       if (bit == 8)
400         {
401           (void) WriteBlobByte(image,byte);
402           bit=0;
403           byte=0;
404         }
405       p++;
406     }
407     if (bit != 0)
408       (void) WriteBlobByte(image,byte);
409     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
410                 image->rows);
411     if (status == MagickFalse)
412       break;
413   }
414   (void) CloseBlob(image);
415   return(MagickTrue);
416 }