]> granicus.if.org Git - imagemagick/blob - coders/xbm.c
(no commit message)
[imagemagick] / coders / xbm.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            X   X  BBBB   M   M                              %
7 %                             X X   B   B  MM MM                              %
8 %                              X    BBBB   M M M                              %
9 %                             X X   B   B  M   M                              %
10 %                            X   X  BBBB   M   M                              %
11 %                                                                             %
12 %                                                                             %
13 %                  Read/Write X Windows System Bitmap Format                  %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2010 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 \f
39 /*
40   Include declarations.
41 */
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color-private.h"
47 #include "magick/colormap.h"
48 #include "magick/colorspace.h"
49 #include "magick/exception.h"
50 #include "magick/exception-private.h"
51 #include "magick/image.h"
52 #include "magick/image-private.h"
53 #include "magick/list.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/monitor.h"
57 #include "magick/monitor-private.h"
58 #include "magick/quantum-private.h"
59 #include "magick/static.h"
60 #include "magick/string_.h"
61 #include "magick/module.h"
62 #include "magick/utility.h"
63 \f
64 /*
65   Forward declarations.
66 */
67 static MagickBooleanType
68   WriteXBMImage(const ImageInfo *,Image *);
69 \f
70 /*
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %                                                                             %
73 %                                                                             %
74 %                                                                             %
75 %   I s X B M                                                                 %
76 %                                                                             %
77 %                                                                             %
78 %                                                                             %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %
81 %  IsXBM() returns MagickTrue if the image format type, identified by the
82 %  magick string, is XBM.
83 %
84 %  The format of the IsXBM method is:
85 %
86 %      MagickBooleanType IsXBM(const unsigned char *magick,const size_t length)
87 %
88 %  A description of each parameter follows:
89 %
90 %    o magick: compare image format pattern against these bytes.
91 %
92 %    o length: Specifies the length of the magick string.
93 %
94 */
95 static MagickBooleanType IsXBM(const unsigned char *magick,const size_t length)
96 {
97   if (length < 7)
98     return(MagickFalse);
99   if (memcmp(magick,"#define",7) == 0)
100     return(MagickTrue);
101   return(MagickFalse);
102 }
103 \f
104 /*
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %                                                                             %
107 %                                                                             %
108 %                                                                             %
109 %   R e a d X B M I m a g e                                                   %
110 %                                                                             %
111 %                                                                             %
112 %                                                                             %
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 %
115 %  ReadXBMImage() reads an X11 bitmap image file and returns it.  It
116 %  allocates the memory necessary for the new Image structure and returns a
117 %  pointer to the new image.
118 %
119 %  The format of the ReadXBMImage method is:
120 %
121 %      Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
122 %
123 %  A description of each parameter follows:
124 %
125 %    o image_info: the image info.
126 %
127 %    o exception: return any errors or warnings in this structure.
128 %
129 */
130
131 static int XBMInteger(Image *image,short int *hex_digits)
132 {
133   int
134     c,
135     flag,
136     value;
137
138   value=0;
139   flag=0;
140   for ( ; ; )
141   {
142     c=ReadBlobByte(image);
143     if (c == EOF)
144       {
145         value=(-1);
146         break;
147       }
148     c&=0xff;
149     if (isxdigit(c) != MagickFalse)
150       {
151         value=(int) ((size_t) value << 4)+hex_digits[c];
152         flag++;
153         continue;
154       }
155     if ((hex_digits[c]) < 0 && (flag != 0))
156       break;
157   }
158   return(value);
159 }
160
161 static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
162 {
163   char
164     buffer[MaxTextExtent],
165     name[MaxTextExtent];
166
167   Image
168     *image;
169
170   ssize_t
171     y;
172
173   MagickBooleanType
174     status;
175
176   register IndexPacket
177     *indexes;
178
179   register ssize_t
180     i,
181     x;
182
183   register PixelPacket
184     *q;
185
186   register unsigned char
187     *p;
188
189   short int
190     hex_digits[256];
191
192   size_t
193     length;
194
195   unsigned char
196     *data;
197
198   size_t
199     bit,
200     byte,
201     bytes_per_line,
202     padding,
203     value,
204     version;
205
206   unsigned long
207     height,
208     width;
209
210   /*
211     Open image file.
212   */
213   assert(image_info != (const ImageInfo *) NULL);
214   assert(image_info->signature == MagickSignature);
215   if (image_info->debug != MagickFalse)
216     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
217       image_info->filename);
218   assert(exception != (ExceptionInfo *) NULL);
219   assert(exception->signature == MagickSignature);
220   image=AcquireImage(image_info);
221   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
222   if (status == MagickFalse)
223     {
224       image=DestroyImageList(image);
225       return((Image *) NULL);
226     }
227   /*
228     Read X bitmap header.
229   */
230   width=0;
231   height=0;
232   while (ReadBlobString(image,buffer) != (char *) NULL)
233     if (sscanf(buffer,"#define %s %lu",name,&width) == 2)
234       if ((strlen(name) >= 6) &&
235           (LocaleCompare(name+strlen(name)-6,"_width") == 0))
236         break;
237   while (ReadBlobString(image,buffer) != (char *) NULL)
238     if (sscanf(buffer,"#define %s %lu",name,&height) == 2)
239       if ((strlen(name) >= 7) &&
240           (LocaleCompare(name+strlen(name)-7,"_height") == 0))
241         break;
242   image->columns=width;
243   image->rows=width;
244   image->depth=8;
245   image->storage_class=PseudoClass;
246   image->colors=2;
247   /*
248     Scan until hex digits.
249   */
250   version=11;
251   while (ReadBlobString(image,buffer) != (char *) NULL)
252   {
253     if (sscanf(buffer,"static short %s = {",name) == 1)
254       version=10;
255     else
256       if (sscanf(buffer,"static unsigned char %s = {",name) == 1)
257         version=11;
258       else
259         if (sscanf(buffer,"static char %s = {",name) == 1)
260           version=11;
261         else
262           continue;
263     p=(unsigned char *) strrchr(name,'_');
264     if (p == (unsigned char *) NULL)
265       p=(unsigned char *) name;
266     else
267       p++;
268     if (LocaleCompare("bits[]",(char *) p) == 0)
269       break;
270   }
271   if ((image->columns == 0) || (image->rows == 0) ||
272       (EOFBlob(image) != MagickFalse))
273     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
274   /*
275     Initialize image structure.
276   */
277   if (AcquireImageColormap(image,image->colors) == MagickFalse)
278     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
279   /*
280     Initialize colormap.
281   */
282   image->colormap[0].red=(Quantum) QuantumRange;
283   image->colormap[0].green=(Quantum) QuantumRange;
284   image->colormap[0].blue=(Quantum) QuantumRange;
285   image->colormap[1].red=(Quantum) 0;
286   image->colormap[1].green=(Quantum) 0;
287   image->colormap[1].blue=(Quantum) 0;
288   if (image_info->ping != MagickFalse)
289     {
290       (void) CloseBlob(image);
291       return(GetFirstImageInList(image));
292     }
293   /*
294     Initialize hex values.
295   */
296   hex_digits[(int) '0']=0;
297   hex_digits[(int) '1']=1;
298   hex_digits[(int) '2']=2;
299   hex_digits[(int) '3']=3;
300   hex_digits[(int) '4']=4;
301   hex_digits[(int) '5']=5;
302   hex_digits[(int) '6']=6;
303   hex_digits[(int) '7']=7;
304   hex_digits[(int) '8']=8;
305   hex_digits[(int) '9']=9;
306   hex_digits[(int) 'A']=10;
307   hex_digits[(int) 'B']=11;
308   hex_digits[(int) 'C']=12;
309   hex_digits[(int) 'D']=13;
310   hex_digits[(int) 'E']=14;
311   hex_digits[(int) 'F']=15;
312   hex_digits[(int) 'a']=10;
313   hex_digits[(int) 'b']=11;
314   hex_digits[(int) 'c']=12;
315   hex_digits[(int) 'd']=13;
316   hex_digits[(int) 'e']=14;
317   hex_digits[(int) 'f']=15;
318   hex_digits[(int) 'x']=0;
319   hex_digits[(int) ' ']=(-1);
320   hex_digits[(int) ',']=(-1);
321   hex_digits[(int) '}']=(-1);
322   hex_digits[(int) '\n']=(-1);
323   hex_digits[(int) '\t']=(-1);
324   /*
325     Read hex image data.
326   */
327   padding=0;
328   if (((image->columns % 16) != 0) &&
329       ((image->columns % 16) < 9) && (version == 10))
330     padding=1;
331   bytes_per_line=(image->columns+7)/8+padding;
332   length=(size_t) image->rows;
333   data=(unsigned char *) AcquireQuantumMemory(length,bytes_per_line*
334     sizeof(*data));
335   if (data == (unsigned char *) NULL)
336     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
337   p=data;
338   if (version == 10)
339     for (i=0; i < (ssize_t) (bytes_per_line*image->rows); (i+=2))
340     {
341       value=(size_t) XBMInteger(image,hex_digits);
342       *p++=(unsigned char) value;
343       if ((padding == 0) || (((i+2) % bytes_per_line) != 0))
344         *p++=(unsigned char) (value >> 8);
345     }
346   else
347     for (i=0; i < (ssize_t) (bytes_per_line*image->rows); i++)
348     {
349       value=(size_t) XBMInteger(image,hex_digits);
350       *p++=(unsigned char) value;
351     }
352   /*
353     Convert X bitmap image to pixel packets.
354   */
355   p=data;
356   for (y=0; y < (ssize_t) image->rows; y++)
357   {
358     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
359     if (q == (PixelPacket *) NULL)
360       break;
361     indexes=GetAuthenticIndexQueue(image);
362     bit=0;
363     byte=0;
364     for (x=0; x < (ssize_t) image->columns; x++)
365     {
366       if (bit == 0)
367         byte=(size_t) (*p++);
368       indexes[x]=(IndexPacket) ((byte & 0x01) != 0 ? 0x01 : 0x00);
369       bit++;
370       byte>>=1;
371       if (bit == 8)
372         bit=0;
373     }
374     if (SyncAuthenticPixels(image,exception) == MagickFalse)
375       break;
376     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
377                 image->rows);
378     if (status == MagickFalse)
379       break;
380   }
381   data=(unsigned char *) RelinquishMagickMemory(data);
382   (void) SyncImage(image);
383   (void) CloseBlob(image);
384   return(GetFirstImageInList(image));
385 }
386 \f
387 /*
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389 %                                                                             %
390 %                                                                             %
391 %                                                                             %
392 %   R e g i s t e r X B M I m a g e                                           %
393 %                                                                             %
394 %                                                                             %
395 %                                                                             %
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 %
398 %  RegisterXBMImage() adds attributes for the XBM image format to
399 %  the list of supported formats.  The attributes include the image format
400 %  tag, a method to read and/or write the format, whether the format
401 %  supports the saving of more than one frame to the same file or blob,
402 %  whether the format supports native in-memory I/O, and a brief
403 %  description of the format.
404 %
405 %  The format of the RegisterXBMImage method is:
406 %
407 %      size_t RegisterXBMImage(void)
408 %
409 */
410 ModuleExport size_t RegisterXBMImage(void)
411 {
412   MagickInfo
413     *entry;
414
415   entry=SetMagickInfo("XBM");
416   entry->decoder=(DecodeImageHandler *) ReadXBMImage;
417   entry->encoder=(EncodeImageHandler *) WriteXBMImage;
418   entry->magick=(IsImageFormatHandler *) IsXBM;
419   entry->adjoin=MagickFalse;
420   entry->description=ConstantString(
421     "X Windows system bitmap (black and white)");
422   entry->module=ConstantString("XBM");
423   (void) RegisterMagickInfo(entry);
424   return(MagickImageCoderSignature);
425 }
426 \f
427 /*
428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 %                                                                             %
430 %                                                                             %
431 %                                                                             %
432 %   U n r e g i s t e r X B M I m a g e                                       %
433 %                                                                             %
434 %                                                                             %
435 %                                                                             %
436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437 %
438 %  UnregisterXBMImage() removes format registrations made by the
439 %  XBM module from the list of supported formats.
440 %
441 %  The format of the UnregisterXBMImage method is:
442 %
443 %      UnregisterXBMImage(void)
444 %
445 */
446 ModuleExport void UnregisterXBMImage(void)
447 {
448   (void) UnregisterMagickInfo("XBM");
449 }
450 \f
451 /*
452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
453 %                                                                             %
454 %                                                                             %
455 %                                                                             %
456 %   W r i t e X B M I m a g e                                                 %
457 %                                                                             %
458 %                                                                             %
459 %                                                                             %
460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
461 %
462 %  Procedure WriteXBMImage() writes an image to a file in the X bitmap format.
463 %
464 %  The format of the WriteXBMImage method is:
465 %
466 %      MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image)
467 %
468 %  A description of each parameter follows.
469 %
470 %    o image_info: the image info.
471 %
472 %    o image:  The image.
473 %
474 %
475 */
476 static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image)
477 {
478   char
479     basename[MaxTextExtent],
480     buffer[MaxTextExtent];
481
482   ssize_t
483     y;
484
485   MagickBooleanType
486     status;
487
488   register const PixelPacket
489     *p;
490
491   register ssize_t
492     x;
493
494   ssize_t
495     count;
496
497   size_t
498     bit,
499     byte;
500
501   /*
502     Open output image file.
503   */
504   assert(image_info != (const ImageInfo *) NULL);
505   assert(image_info->signature == MagickSignature);
506   assert(image != (Image *) NULL);
507   assert(image->signature == MagickSignature);
508   if (image->debug != MagickFalse)
509     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
510   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
511   if (status == MagickFalse)
512     return(status);
513   if (image->colorspace != RGBColorspace)
514     (void) TransformImageColorspace(image,RGBColorspace);
515   /*
516     Write X bitmap header.
517   */
518   GetPathComponent(image->filename,BasePath,basename);
519   (void) FormatMagickString(buffer,MaxTextExtent,"#define %s_width %.20g\n",
520     basename,(double) image->columns);
521   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
522   (void) FormatMagickString(buffer,MaxTextExtent,"#define %s_height %.20g\n",
523     basename,(double) image->rows);
524   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
525   (void) FormatMagickString(buffer,MaxTextExtent,
526     "static char %s_bits[] = {\n",basename);
527   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
528   (void) CopyMagickString(buffer," ",MaxTextExtent);
529   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
530   /*
531     Convert MIFF to X bitmap pixels.
532   */
533   (void) SetImageType(image,BilevelType);
534   bit=0;
535   byte=0;
536   count=0;
537   x=0;
538   y=0;
539   (void) CopyMagickString(buffer," ",MaxTextExtent);
540   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
541   for (y=0; y < (ssize_t) image->rows; y++)
542   {
543     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
544     if (p == (const PixelPacket *) NULL)
545       break;
546     for (x=0; x < (ssize_t) image->columns; x++)
547     {
548       byte>>=1;
549       if (PixelIntensity(p) < ((MagickRealType) QuantumRange/2.0))
550         byte|=0x80;
551       bit++;
552       if (bit == 8)
553         {
554           /*
555             Write a bitmap byte to the image file.
556           */
557           (void) FormatMagickString(buffer,MaxTextExtent,"0x%02X, ",
558             (unsigned int) (byte & 0xff));
559           (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
560           count++;
561           if (count == 12)
562             {
563               (void) CopyMagickString(buffer,"\n  ",MaxTextExtent);
564               (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
565               count=0;
566             };
567           bit=0;
568           byte=0;
569         }
570         p++;
571       }
572     if (bit != 0)
573       {
574         /*
575           Write a bitmap byte to the image file.
576         */
577         byte>>=(8-bit);
578         (void) FormatMagickString(buffer,MaxTextExtent,"0x%02X, ",
579           (unsigned int) (byte & 0xff));
580         (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
581         count++;
582         if (count == 12)
583           {
584             (void) CopyMagickString(buffer,"\n  ",MaxTextExtent);
585             (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
586             count=0;
587           };
588         bit=0;
589         byte=0;
590       };
591     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
592                 image->rows);
593     if (status == MagickFalse)
594       break;
595   }
596   (void) CopyMagickString(buffer,"};\n",MaxTextExtent);
597   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
598   (void) CloseBlob(image);
599   return(MagickTrue);
600 }