2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write X Windows System Bitmap Format %
20 % Copyright 1999-2014 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 % http://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 int XBMInteger(Image *image,short int *hex_digits)
145 c=ReadBlobByte(image);
152 if (isxdigit(c) != MagickFalse)
154 value=(int) ((size_t) value << 4)+hex_digits[c];
158 if ((hex_digits[c]) < 0 && (flag != 0))
164 static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
167 buffer[MaxTextExtent],
183 register unsigned char
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,exception);
219 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
220 if (status == MagickFalse)
222 image=DestroyImageList(image);
223 return((Image *) NULL);
226 Read X bitmap header.
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))
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))
240 image->columns=width;
243 image->storage_class=PseudoClass;
246 Scan until hex digits.
249 while (ReadBlobString(image,buffer) != (char *) NULL)
251 if (sscanf(buffer,"static short %s = {",name) == 1)
254 if (sscanf(buffer,"static unsigned char %s = {",name) == 1)
257 if (sscanf(buffer,"static char %s = {",name) == 1)
261 p=(unsigned char *) strrchr(name,'_');
262 if (p == (unsigned char *) NULL)
263 p=(unsigned char *) name;
266 if (LocaleCompare("bits[]",(char *) p) == 0)
269 if ((image->columns == 0) || (image->rows == 0) ||
270 (EOFBlob(image) != MagickFalse))
271 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
273 Initialize image structure.
275 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
276 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
280 image->colormap[0].red=QuantumRange;
281 image->colormap[0].green=QuantumRange;
282 image->colormap[0].blue=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)
288 (void) CloseBlob(image);
289 return(GetFirstImageInList(image));
292 Initialize hex values.
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);
326 if (((image->columns % 16) != 0) && ((image->columns % 16) < 9) &&
329 bytes_per_line=(image->columns+7)/8+padding;
330 length=(size_t) image->rows;
331 data=(unsigned char *) AcquireQuantumMemory(length,bytes_per_line*
333 if (data == (unsigned char *) NULL)
334 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
337 for (i=0; i < (ssize_t) (bytes_per_line*image->rows); (i+=2))
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);
345 for (i=0; i < (ssize_t) (bytes_per_line*image->rows); i++)
347 value=(size_t) XBMInteger(image,hex_digits);
348 *p++=(unsigned char) value;
351 Convert X bitmap image to pixel packets.
354 for (y=0; y < (ssize_t) image->rows; y++)
356 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
357 if (q == (Quantum *) NULL)
361 for (x=0; x < (ssize_t) image->columns; x++)
364 byte=(size_t) (*p++);
365 SetPixelIndex(image,(Quantum) ((byte & 0x01) != 0 ? 0x01 : 0x00),q);
370 q+=GetPixelChannels(image);
372 if (SyncAuthenticPixels(image,exception) == MagickFalse)
374 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
376 if (status == MagickFalse)
379 data=(unsigned char *) RelinquishMagickMemory(data);
380 (void) SyncImage(image,exception);
381 (void) CloseBlob(image);
382 return(GetFirstImageInList(image));
386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 % R e g i s t e r X B M I m a g e %
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
403 % The format of the RegisterXBMImage method is:
405 % size_t RegisterXBMImage(void)
408 ModuleExport size_t RegisterXBMImage(void)
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);
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 % U n r e g i s t e r X B M I m a g e %
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 % UnregisterXBMImage() removes format registrations made by the
437 % XBM module from the list of supported formats.
439 % The format of the UnregisterXBMImage method is:
441 % UnregisterXBMImage(void)
444 ModuleExport void UnregisterXBMImage(void)
446 (void) UnregisterMagickInfo("XBM");
450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454 % W r i t e X B M I m a g e %
458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460 % WriteXBMImage() writes an image to a file in the X bitmap format.
462 % The format of the WriteXBMImage method is:
464 % MagickBooleanType WriteXBMImage(const ImageInfo *image_info,
465 % Image *image,ExceptionInfo *exception)
467 % A description of each parameter follows.
469 % o image_info: the image info.
471 % o image: The image.
473 % o exception: return any errors or warnings in this structure.
476 static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
477 ExceptionInfo *exception)
480 basename[MaxTextExtent],
481 buffer[MaxTextExtent];
486 register const Quantum
501 Open output image file.
503 assert(image_info != (const ImageInfo *) NULL);
504 assert(image_info->signature == MagickSignature);
505 assert(image != (Image *) NULL);
506 assert(image->signature == MagickSignature);
507 if (image->debug != MagickFalse)
508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
509 assert(exception != (ExceptionInfo *) NULL);
510 assert(exception->signature == MagickSignature);
511 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
512 if (status == MagickFalse)
514 (void) TransformImageColorspace(image,sRGBColorspace,exception);
516 Write X bitmap header.
518 GetPathComponent(image->filename,BasePath,basename);
519 (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_width %.20g\n",
520 basename,(double) image->columns);
521 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
522 (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_height %.20g\n",
523 basename,(double) image->rows);
524 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
525 (void) FormatLocaleString(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);
531 Convert MIFF to X bitmap pixels.
533 (void) SetImageType(image,BilevelType,exception);
539 (void) CopyMagickString(buffer," ",MaxTextExtent);
540 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
541 for (y=0; y < (ssize_t) image->rows; y++)
543 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
544 if (p == (const Quantum *) NULL)
546 for (x=0; x < (ssize_t) image->columns; x++)
549 if (GetPixelLuma(image,p) < (QuantumRange/2))
555 Write a bitmap byte to the image file.
557 (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ",
558 (unsigned int) (byte & 0xff));
559 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
563 (void) CopyMagickString(buffer,"\n ",MaxTextExtent);
564 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
570 p+=GetPixelChannels(image);
575 Write a bitmap byte to the image file.
578 (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ",
579 (unsigned int) (byte & 0xff));
580 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
584 (void) CopyMagickString(buffer,"\n ",MaxTextExtent);
585 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
591 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
593 if (status == MagickFalse)
596 (void) CopyMagickString(buffer,"};\n",MaxTextExtent);
597 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
598 (void) CloseBlob(image);