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