]> granicus.if.org Git - imagemagick/blob - coders/xbm.c
Horizon validity (anti-aliased) added to Plane2Cylinder
[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-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 \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   MagickBooleanType
171     status;
172
173   register IndexPacket
174     *indexes;
175
176   register ssize_t
177     i,
178     x;
179
180   register PixelPacket
181     *q;
182
183   register unsigned char
184     *p;
185
186   short int
187     hex_digits[256];
188
189   size_t
190     bit,
191     byte,
192     bytes_per_line,
193     length,
194     padding,
195     value,
196     version;
197
198   ssize_t
199     y;
200
201   unsigned char
202     *data;
203
204   unsigned long
205     height,
206     width;
207
208   /*
209     Open image file.
210   */
211   assert(image_info != (const ImageInfo *) NULL);
212   assert(image_info->signature == MagickSignature);
213   if (image_info->debug != MagickFalse)
214     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
215       image_info->filename);
216   assert(exception != (ExceptionInfo *) NULL);
217   assert(exception->signature == MagickSignature);
218   image=AcquireImage(image_info);
219   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
220   if (status == MagickFalse)
221     {
222       image=DestroyImageList(image);
223       return((Image *) NULL);
224     }
225   /*
226     Read X bitmap header.
227   */
228   width=0;
229   height=0;
230   while (ReadBlobString(image,buffer) != (char *) NULL)
231     if (sscanf(buffer,"#define %s %lu",name,&width) == 2)
232       if ((strlen(name) >= 6) &&
233           (LocaleCompare(name+strlen(name)-6,"_width") == 0))
234         break;
235   while (ReadBlobString(image,buffer) != (char *) NULL)
236     if (sscanf(buffer,"#define %s %lu",name,&height) == 2)
237       if ((strlen(name) >= 7) &&
238           (LocaleCompare(name+strlen(name)-7,"_height") == 0))
239         break;
240   image->columns=width;
241   image->rows=height;
242   image->depth=8;
243   image->storage_class=PseudoClass;
244   image->colors=2;
245   /*
246     Scan until hex digits.
247   */
248   version=11;
249   while (ReadBlobString(image,buffer) != (char *) NULL)
250   {
251     if (sscanf(buffer,"static short %s = {",name) == 1)
252       version=10;
253     else
254       if (sscanf(buffer,"static unsigned char %s = {",name) == 1)
255         version=11;
256       else
257         if (sscanf(buffer,"static char %s = {",name) == 1)
258           version=11;
259         else
260           continue;
261     p=(unsigned char *) strrchr(name,'_');
262     if (p == (unsigned char *) NULL)
263       p=(unsigned char *) name;
264     else
265       p++;
266     if (LocaleCompare("bits[]",(char *) p) == 0)
267       break;
268   }
269   if ((image->columns == 0) || (image->rows == 0) ||
270       (EOFBlob(image) != MagickFalse))
271     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
272   /*
273     Initialize image structure.
274   */
275   if (AcquireImageColormap(image,image->colors) == MagickFalse)
276     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
277   /*
278     Initialize colormap.
279   */
280   image->colormap[0].red=(Quantum) QuantumRange;
281   image->colormap[0].green=(Quantum) QuantumRange;
282   image->colormap[0].blue=(Quantum) QuantumRange;
283   image->colormap[1].red=(Quantum) 0;
284   image->colormap[1].green=(Quantum) 0;
285   image->colormap[1].blue=(Quantum) 0;
286   if (image_info->ping != MagickFalse)
287     {
288       (void) CloseBlob(image);
289       return(GetFirstImageInList(image));
290     }
291   /*
292     Initialize hex values.
293   */
294   hex_digits[(int) '0']=0;
295   hex_digits[(int) '1']=1;
296   hex_digits[(int) '2']=2;
297   hex_digits[(int) '3']=3;
298   hex_digits[(int) '4']=4;
299   hex_digits[(int) '5']=5;
300   hex_digits[(int) '6']=6;
301   hex_digits[(int) '7']=7;
302   hex_digits[(int) '8']=8;
303   hex_digits[(int) '9']=9;
304   hex_digits[(int) 'A']=10;
305   hex_digits[(int) 'B']=11;
306   hex_digits[(int) 'C']=12;
307   hex_digits[(int) 'D']=13;
308   hex_digits[(int) 'E']=14;
309   hex_digits[(int) 'F']=15;
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) 'x']=0;
317   hex_digits[(int) ' ']=(-1);
318   hex_digits[(int) ',']=(-1);
319   hex_digits[(int) '}']=(-1);
320   hex_digits[(int) '\n']=(-1);
321   hex_digits[(int) '\t']=(-1);
322   /*
323     Read hex image data.
324   */
325   padding=0;
326   if (((image->columns % 16) != 0) &&
327       ((image->columns % 16) < 9) && (version == 10))
328     padding=1;
329   bytes_per_line=(image->columns+7)/8+padding;
330   length=(size_t) image->rows;
331   data=(unsigned char *) AcquireQuantumMemory(length,bytes_per_line*
332     sizeof(*data));
333   if (data == (unsigned char *) NULL)
334     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
335   p=data;
336   if (version == 10)
337     for (i=0; i < (ssize_t) (bytes_per_line*image->rows); (i+=2))
338     {
339       value=(size_t) XBMInteger(image,hex_digits);
340       *p++=(unsigned char) value;
341       if ((padding == 0) || (((i+2) % bytes_per_line) != 0))
342         *p++=(unsigned char) (value >> 8);
343     }
344   else
345     for (i=0; i < (ssize_t) (bytes_per_line*image->rows); i++)
346     {
347       value=(size_t) XBMInteger(image,hex_digits);
348       *p++=(unsigned char) value;
349     }
350   /*
351     Convert X bitmap image to pixel packets.
352   */
353   p=data;
354   for (y=0; y < (ssize_t) image->rows; y++)
355   {
356     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
357     if (q == (PixelPacket *) NULL)
358       break;
359     indexes=GetAuthenticIndexQueue(image);
360     bit=0;
361     byte=0;
362     for (x=0; x < (ssize_t) image->columns; x++)
363     {
364       if (bit == 0)
365         byte=(size_t) (*p++);
366       SetIndexPixelComponent(indexes+x,(byte & 0x01) != 0 ? 0x01 : 0x00);
367       bit++;
368       byte>>=1;
369       if (bit == 8)
370         bit=0;
371     }
372     if (SyncAuthenticPixels(image,exception) == MagickFalse)
373       break;
374     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
375       image->rows);
376     if (status == MagickFalse)
377       break;
378   }
379   data=(unsigned char *) RelinquishMagickMemory(data);
380   (void) SyncImage(image);
381   (void) CloseBlob(image);
382   return(GetFirstImageInList(image));
383 }
384 \f
385 /*
386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387 %                                                                             %
388 %                                                                             %
389 %                                                                             %
390 %   R e g i s t e r X B M I m a g e                                           %
391 %                                                                             %
392 %                                                                             %
393 %                                                                             %
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395 %
396 %  RegisterXBMImage() adds attributes for the XBM image format to
397 %  the list of supported formats.  The attributes include the image format
398 %  tag, a method to read and/or write the format, whether the format
399 %  supports the saving of more than one frame to the same file or blob,
400 %  whether the format supports native in-memory I/O, and a brief
401 %  description of the format.
402 %
403 %  The format of the RegisterXBMImage method is:
404 %
405 %      size_t RegisterXBMImage(void)
406 %
407 */
408 ModuleExport size_t RegisterXBMImage(void)
409 {
410   MagickInfo
411     *entry;
412
413   entry=SetMagickInfo("XBM");
414   entry->decoder=(DecodeImageHandler *) ReadXBMImage;
415   entry->encoder=(EncodeImageHandler *) WriteXBMImage;
416   entry->magick=(IsImageFormatHandler *) IsXBM;
417   entry->adjoin=MagickFalse;
418   entry->description=ConstantString(
419     "X Windows system bitmap (black and white)");
420   entry->module=ConstantString("XBM");
421   (void) RegisterMagickInfo(entry);
422   return(MagickImageCoderSignature);
423 }
424 \f
425 /*
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 %                                                                             %
428 %                                                                             %
429 %                                                                             %
430 %   U n r e g i s t e r X B M I m a g e                                       %
431 %                                                                             %
432 %                                                                             %
433 %                                                                             %
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 %
436 %  UnregisterXBMImage() removes format registrations made by the
437 %  XBM module from the list of supported formats.
438 %
439 %  The format of the UnregisterXBMImage method is:
440 %
441 %      UnregisterXBMImage(void)
442 %
443 */
444 ModuleExport void UnregisterXBMImage(void)
445 {
446   (void) UnregisterMagickInfo("XBM");
447 }
448 \f
449 /*
450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451 %                                                                             %
452 %                                                                             %
453 %                                                                             %
454 %   W r i t e X B M I m a g e                                                 %
455 %                                                                             %
456 %                                                                             %
457 %                                                                             %
458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459 %
460 %  Procedure WriteXBMImage() writes an image to a file in the X bitmap format.
461 %
462 %  The format of the WriteXBMImage method is:
463 %
464 %      MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image)
465 %
466 %  A description of each parameter follows.
467 %
468 %    o image_info: the image info.
469 %
470 %    o image:  The image.
471 %
472 %
473 */
474 static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image)
475 {
476   char
477     basename[MaxTextExtent],
478     buffer[MaxTextExtent];
479
480   MagickBooleanType
481     status;
482
483   register const PixelPacket
484     *p;
485
486   register ssize_t
487     x;
488
489   size_t
490     bit,
491     byte;
492
493   ssize_t
494     count,
495     y;
496
497   /*
498     Open output image file.
499   */
500   assert(image_info != (const ImageInfo *) NULL);
501   assert(image_info->signature == MagickSignature);
502   assert(image != (Image *) NULL);
503   assert(image->signature == MagickSignature);
504   if (image->debug != MagickFalse)
505     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
506   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
507   if (status == MagickFalse)
508     return(status);
509   if (image->colorspace != RGBColorspace)
510     (void) TransformImageColorspace(image,RGBColorspace);
511   /*
512     Write X bitmap header.
513   */
514   GetPathComponent(image->filename,BasePath,basename);
515   (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_width %.20g\n",
516     basename,(double) image->columns);
517   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
518   (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_height %.20g\n",
519     basename,(double) image->rows);
520   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
521   (void) FormatLocaleString(buffer,MaxTextExtent,
522     "static char %s_bits[] = {\n",basename);
523   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
524   (void) CopyMagickString(buffer," ",MaxTextExtent);
525   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
526   /*
527     Convert MIFF to X bitmap pixels.
528   */
529   (void) SetImageType(image,BilevelType);
530   bit=0;
531   byte=0;
532   count=0;
533   x=0;
534   y=0;
535   (void) CopyMagickString(buffer," ",MaxTextExtent);
536   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
537   for (y=0; y < (ssize_t) image->rows; y++)
538   {
539     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
540     if (p == (const PixelPacket *) NULL)
541       break;
542     for (x=0; x < (ssize_t) image->columns; x++)
543     {
544       byte>>=1;
545       if (PixelIntensity(p) < ((MagickRealType) QuantumRange/2.0))
546         byte|=0x80;
547       bit++;
548       if (bit == 8)
549         {
550           /*
551             Write a bitmap byte to the image file.
552           */
553           (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ",
554             (unsigned int) (byte & 0xff));
555           (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
556           count++;
557           if (count == 12)
558             {
559               (void) CopyMagickString(buffer,"\n  ",MaxTextExtent);
560               (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
561               count=0;
562             };
563           bit=0;
564           byte=0;
565         }
566         p++;
567       }
568     if (bit != 0)
569       {
570         /*
571           Write a bitmap byte to the image file.
572         */
573         byte>>=(8-bit);
574         (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ",
575           (unsigned int) (byte & 0xff));
576         (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
577         count++;
578         if (count == 12)
579           {
580             (void) CopyMagickString(buffer,"\n  ",MaxTextExtent);
581             (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
582             count=0;
583           };
584         bit=0;
585         byte=0;
586       };
587     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
588       image->rows);
589     if (status == MagickFalse)
590       break;
591   }
592   (void) CopyMagickString(buffer,"};\n",MaxTextExtent);
593   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
594   (void) CloseBlob(image);
595   return(MagickTrue);
596 }