2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write X Windows System Bitmap Format %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
70 static MagickBooleanType
71 WriteXBMImage(const ImageInfo *,Image *,ExceptionInfo *);
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % IsXBM() returns MagickTrue if the image format type, identified by the
85 % magick string, is XBM.
87 % The format of the IsXBM method is:
89 % MagickBooleanType IsXBM(const unsigned char *magick,const size_t length)
91 % A description of each parameter follows:
93 % o magick: compare image format pattern against these bytes.
95 % o length: Specifies the length of the magick string.
98 static MagickBooleanType IsXBM(const unsigned char *magick,const size_t length)
102 if (memcmp(magick,"#define",7) == 0)
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 % R e a d X B M I m a g e %
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
122 % The format of the ReadXBMImage method is:
124 % Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
126 % A description of each parameter follows:
128 % o image_info: the image info.
130 % o exception: return any errors or warnings in this structure.
134 static unsigned int XBMInteger(Image *image,short int *hex_digits)
143 Skip any leading whitespace.
147 c=ReadBlobByte(image);
150 } while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
157 if (value > (unsigned int) (INT_MAX/10))
161 if (value > (unsigned int) (INT_MAX-hex_digits[c]))
163 value+=hex_digits[c];
164 c=ReadBlobByte(image);
167 } while (hex_digits[c] >= 0);
171 static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
174 buffer[MagickPathExtent],
175 name[MagickPathExtent];
190 register unsigned char
216 assert(image_info != (const ImageInfo *) NULL);
217 assert(image_info->signature == MagickCoreSignature);
218 if (image_info->debug != MagickFalse)
219 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
220 image_info->filename);
221 assert(exception != (ExceptionInfo *) NULL);
222 assert(exception->signature == MagickCoreSignature);
223 image=AcquireImage(image_info,exception);
224 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
225 if (status == MagickFalse)
227 image=DestroyImageList(image);
228 return((Image *) NULL);
231 Read X bitmap header.
235 while (ReadBlobString(image,buffer) != (char *) NULL)
236 if (sscanf(buffer,"#define %32s %u",name,&width) == 2)
237 if ((strlen(name) >= 6) &&
238 (LocaleCompare(name+strlen(name)-6,"_width") == 0))
240 while (ReadBlobString(image,buffer) != (char *) NULL)
241 if (sscanf(buffer,"#define %32s %u",name,&height) == 2)
242 if ((strlen(name) >= 7) &&
243 (LocaleCompare(name+strlen(name)-7,"_height") == 0))
245 image->columns=width;
248 image->storage_class=PseudoClass;
251 Scan until hex digits.
254 while (ReadBlobString(image,buffer) != (char *) NULL)
256 if (sscanf(buffer,"static short %32s = {",name) == 1)
259 if (sscanf(buffer,"static unsigned char %32s = {",name) == 1)
262 if (sscanf(buffer,"static char %32s = {",name) == 1)
266 p=(unsigned char *) strrchr(name,'_');
267 if (p == (unsigned char *) NULL)
268 p=(unsigned char *) name;
271 if (LocaleCompare("bits[]",(char *) p) == 0)
274 if ((image->columns == 0) || (image->rows == 0) ||
275 (EOFBlob(image) != MagickFalse))
276 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
278 Initialize image structure.
280 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
281 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
285 image->colormap[0].red=QuantumRange;
286 image->colormap[0].green=QuantumRange;
287 image->colormap[0].blue=QuantumRange;
288 image->colormap[1].red=(Quantum) 0;
289 image->colormap[1].green=(Quantum) 0;
290 image->colormap[1].blue=(Quantum) 0;
291 if (image_info->ping != MagickFalse)
293 (void) CloseBlob(image);
294 return(GetFirstImageInList(image));
296 status=SetImageExtent(image,image->columns,image->rows,exception);
297 if (status == MagickFalse)
298 return(DestroyImageList(image));
300 Initialize hex values.
302 hex_digits[(int) '0']=0;
303 hex_digits[(int) '1']=1;
304 hex_digits[(int) '2']=2;
305 hex_digits[(int) '3']=3;
306 hex_digits[(int) '4']=4;
307 hex_digits[(int) '5']=5;
308 hex_digits[(int) '6']=6;
309 hex_digits[(int) '7']=7;
310 hex_digits[(int) '8']=8;
311 hex_digits[(int) '9']=9;
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) 'a']=10;
319 hex_digits[(int) 'b']=11;
320 hex_digits[(int) 'c']=12;
321 hex_digits[(int) 'd']=13;
322 hex_digits[(int) 'e']=14;
323 hex_digits[(int) 'f']=15;
324 hex_digits[(int) 'x']=0;
325 hex_digits[(int) ' ']=(-1);
326 hex_digits[(int) ',']=(-1);
327 hex_digits[(int) '}']=(-1);
328 hex_digits[(int) '\n']=(-1);
329 hex_digits[(int) '\t']=(-1);
334 if (((image->columns % 16) != 0) && ((image->columns % 16) < 9) &&
337 bytes_per_line=(unsigned int) (image->columns+7)/8+padding;
338 length=(unsigned int) image->rows;
339 data=(unsigned char *) AcquireQuantumMemory(length,bytes_per_line*
341 if (data == (unsigned char *) NULL)
342 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
345 for (i=0; i < (ssize_t) (bytes_per_line*image->rows); (i+=2))
347 value=XBMInteger(image,hex_digits);
348 *p++=(unsigned char) value;
349 if ((padding == 0) || (((i+2) % bytes_per_line) != 0))
350 *p++=(unsigned char) (value >> 8);
353 for (i=0; i < (ssize_t) (bytes_per_line*image->rows); i++)
355 value=XBMInteger(image,hex_digits);
356 *p++=(unsigned char) value;
359 Convert X bitmap image to pixel packets.
362 for (y=0; y < (ssize_t) image->rows; y++)
364 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
365 if (q == (Quantum *) NULL)
369 for (x=0; x < (ssize_t) image->columns; x++)
372 byte=(size_t) (*p++);
373 SetPixelIndex(image,(Quantum) ((byte & 0x01) != 0 ? 0x01 : 0x00),q);
378 q+=GetPixelChannels(image);
380 if (SyncAuthenticPixels(image,exception) == MagickFalse)
382 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
384 if (status == MagickFalse)
387 data=(unsigned char *) RelinquishMagickMemory(data);
388 (void) SyncImage(image,exception);
389 (void) CloseBlob(image);
390 return(GetFirstImageInList(image));
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 % R e g i s t e r X B M I m a g e %
402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 % RegisterXBMImage() adds attributes for the XBM image format to
405 % the list of supported formats. The attributes include the image format
406 % tag, a method to read and/or write the format, whether the format
407 % supports the saving of more than one frame to the same file or blob,
408 % whether the format supports native in-memory I/O, and a brief
409 % description of the format.
411 % The format of the RegisterXBMImage method is:
413 % size_t RegisterXBMImage(void)
416 ModuleExport size_t RegisterXBMImage(void)
421 entry=AcquireMagickInfo("XBM","XBM",
422 "X Windows system bitmap (black and white)");
423 entry->decoder=(DecodeImageHandler *) ReadXBMImage;
424 entry->encoder=(EncodeImageHandler *) WriteXBMImage;
425 entry->magick=(IsImageFormatHandler *) IsXBM;
426 entry->flags^=CoderAdjoinFlag;
427 (void) RegisterMagickInfo(entry);
428 return(MagickImageCoderSignature);
432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 % U n r e g i s t e r X B M I m a g e %
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 % UnregisterXBMImage() removes format registrations made by the
443 % XBM module from the list of supported formats.
445 % The format of the UnregisterXBMImage method is:
447 % UnregisterXBMImage(void)
450 ModuleExport void UnregisterXBMImage(void)
452 (void) UnregisterMagickInfo("XBM");
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460 % W r i t e X B M I m a g e %
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 % WriteXBMImage() writes an image to a file in the X bitmap format.
468 % The format of the WriteXBMImage method is:
470 % MagickBooleanType WriteXBMImage(const ImageInfo *image_info,
471 % Image *image,ExceptionInfo *exception)
473 % A description of each parameter follows.
475 % o image_info: the image info.
477 % o image: The image.
479 % o exception: return any errors or warnings in this structure.
482 static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
483 ExceptionInfo *exception)
486 basename[MagickPathExtent],
487 buffer[MagickPathExtent];
492 register const Quantum
507 Open output image file.
509 assert(image_info != (const ImageInfo *) NULL);
510 assert(image_info->signature == MagickCoreSignature);
511 assert(image != (Image *) NULL);
512 assert(image->signature == MagickCoreSignature);
513 if (image->debug != MagickFalse)
514 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
515 assert(exception != (ExceptionInfo *) NULL);
516 assert(exception->signature == MagickCoreSignature);
517 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
518 if (status == MagickFalse)
520 (void) TransformImageColorspace(image,sRGBColorspace,exception);
522 Write X bitmap header.
524 GetPathComponent(image->filename,BasePath,basename);
525 (void) FormatLocaleString(buffer,MagickPathExtent,"#define %s_width %.20g\n",
526 basename,(double) image->columns);
527 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
528 (void) FormatLocaleString(buffer,MagickPathExtent,"#define %s_height %.20g\n",
529 basename,(double) image->rows);
530 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
531 (void) FormatLocaleString(buffer,MagickPathExtent,
532 "static char %s_bits[] = {\n",basename);
533 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
534 (void) CopyMagickString(buffer," ",MagickPathExtent);
535 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
537 Convert MIFF to X bitmap pixels.
539 (void) SetImageType(image,BilevelType,exception);
545 (void) CopyMagickString(buffer," ",MagickPathExtent);
546 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
547 for (y=0; y < (ssize_t) image->rows; y++)
549 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
550 if (p == (const Quantum *) NULL)
552 for (x=0; x < (ssize_t) image->columns; x++)
555 if (GetPixelLuma(image,p) < (QuantumRange/2))
561 Write a bitmap byte to the image file.
563 (void) FormatLocaleString(buffer,MagickPathExtent,"0x%02X, ",
564 (unsigned int) (byte & 0xff));
565 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
569 (void) CopyMagickString(buffer,"\n ",MagickPathExtent);
570 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
576 p+=GetPixelChannels(image);
581 Write a bitmap byte to the image file.
584 (void) FormatLocaleString(buffer,MagickPathExtent,"0x%02X, ",
585 (unsigned int) (byte & 0xff));
586 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
590 (void) CopyMagickString(buffer,"\n ",MagickPathExtent);
591 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
597 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
599 if (status == MagickFalse)
602 (void) CopyMagickString(buffer,"};\n",MagickPathExtent);
603 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
604 (void) CloseBlob(image);