blob: dfb7ca7b7127c961087211337dc8743006dea612 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
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"
cristye7e40552010-04-24 21:34:22 +000047#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000048#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
64/*
65 Forward declarations.
66*/
67static MagickBooleanType
68 WriteXBMImage(const ImageInfo *,Image *);
69
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*/
95static 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
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
131static 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 {
cristybb503372010-05-27 20:51:26 +0000151 value=(int) ((size_t) value << 4)+hex_digits[c];
cristy3ed852e2009-09-05 21:47:34 +0000152 flag++;
153 continue;
154 }
155 if ((hex_digits[c]) < 0 && (flag != 0))
156 break;
157 }
158 return(value);
159}
160
161static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
162{
163 char
164 buffer[MaxTextExtent],
165 name[MaxTextExtent];
166
167 Image
168 *image;
169
cristybb503372010-05-27 20:51:26 +0000170 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000171 y;
172
173 MagickBooleanType
174 status;
175
176 register IndexPacket
177 *indexes;
178
cristybb503372010-05-27 20:51:26 +0000179 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000180 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
cristybb503372010-05-27 20:51:26 +0000198 size_t
cristy3ed852e2009-09-05 21:47:34 +0000199 bit,
200 byte,
201 bytes_per_line,
202 padding,
203 value,
204 version;
205
206 /*
207 Open image file.
208 */
209 assert(image_info != (const ImageInfo *) NULL);
210 assert(image_info->signature == MagickSignature);
211 if (image_info->debug != MagickFalse)
212 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
213 image_info->filename);
214 assert(exception != (ExceptionInfo *) NULL);
215 assert(exception->signature == MagickSignature);
216 image=AcquireImage(image_info);
217 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
218 if (status == MagickFalse)
219 {
220 image=DestroyImageList(image);
221 return((Image *) NULL);
222 }
223 /*
224 Read X bitmap header.
225 */
226 while (ReadBlobString(image,buffer) != (char *) NULL)
227 if (sscanf(buffer,"#define %s %lu",name,&image->columns) == 2)
228 if ((strlen(name) >= 6) &&
229 (LocaleCompare(name+strlen(name)-6,"_width") == 0))
230 break;
231 while (ReadBlobString(image,buffer) != (char *) NULL)
232 if (sscanf(buffer,"#define %s %lu",name,&image->rows) == 2)
233 if ((strlen(name) >= 7) &&
234 (LocaleCompare(name+strlen(name)-7,"_height") == 0))
235 break;
236 image->depth=8;
237 image->storage_class=PseudoClass;
238 image->colors=2;
239 /*
240 Scan until hex digits.
241 */
242 version=11;
243 while (ReadBlobString(image,buffer) != (char *) NULL)
244 {
245 if (sscanf(buffer,"static short %s = {",name) == 1)
246 version=10;
247 else
248 if (sscanf(buffer,"static unsigned char %s = {",name) == 1)
249 version=11;
250 else
251 if (sscanf(buffer,"static char %s = {",name) == 1)
252 version=11;
253 else
254 continue;
255 p=(unsigned char *) strrchr(name,'_');
256 if (p == (unsigned char *) NULL)
257 p=(unsigned char *) name;
258 else
259 p++;
260 if (LocaleCompare("bits[]",(char *) p) == 0)
261 break;
262 }
263 if ((image->columns == 0) || (image->rows == 0) ||
264 (EOFBlob(image) != MagickFalse))
265 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
266 /*
267 Initialize image structure.
268 */
269 if (AcquireImageColormap(image,image->colors) == MagickFalse)
270 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
271 /*
272 Initialize colormap.
273 */
274 image->colormap[0].red=(Quantum) QuantumRange;
275 image->colormap[0].green=(Quantum) QuantumRange;
276 image->colormap[0].blue=(Quantum) QuantumRange;
277 image->colormap[1].red=(Quantum) 0;
278 image->colormap[1].green=(Quantum) 0;
279 image->colormap[1].blue=(Quantum) 0;
280 if (image_info->ping != MagickFalse)
281 {
282 (void) CloseBlob(image);
283 return(GetFirstImageInList(image));
284 }
285 /*
286 Initialize hex values.
287 */
288 hex_digits[(int) '0']=0;
289 hex_digits[(int) '1']=1;
290 hex_digits[(int) '2']=2;
291 hex_digits[(int) '3']=3;
292 hex_digits[(int) '4']=4;
293 hex_digits[(int) '5']=5;
294 hex_digits[(int) '6']=6;
295 hex_digits[(int) '7']=7;
296 hex_digits[(int) '8']=8;
297 hex_digits[(int) '9']=9;
298 hex_digits[(int) 'A']=10;
299 hex_digits[(int) 'B']=11;
300 hex_digits[(int) 'C']=12;
301 hex_digits[(int) 'D']=13;
302 hex_digits[(int) 'E']=14;
303 hex_digits[(int) 'F']=15;
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) 'x']=0;
311 hex_digits[(int) ' ']=(-1);
312 hex_digits[(int) ',']=(-1);
313 hex_digits[(int) '}']=(-1);
314 hex_digits[(int) '\n']=(-1);
315 hex_digits[(int) '\t']=(-1);
316 /*
317 Read hex image data.
318 */
319 padding=0;
320 if (((image->columns % 16) != 0) &&
321 ((image->columns % 16) < 9) && (version == 10))
322 padding=1;
323 bytes_per_line=(image->columns+7)/8+padding;
324 length=(size_t) image->rows;
325 data=(unsigned char *) AcquireQuantumMemory(length,bytes_per_line*
326 sizeof(*data));
327 if (data == (unsigned char *) NULL)
328 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
329 p=data;
330 if (version == 10)
cristybb503372010-05-27 20:51:26 +0000331 for (i=0; i < (ssize_t) (bytes_per_line*image->rows); (i+=2))
cristy3ed852e2009-09-05 21:47:34 +0000332 {
cristybb503372010-05-27 20:51:26 +0000333 value=(size_t) XBMInteger(image,hex_digits);
cristy3ed852e2009-09-05 21:47:34 +0000334 *p++=(unsigned char) value;
335 if ((padding == 0) || (((i+2) % bytes_per_line) != 0))
336 *p++=(unsigned char) (value >> 8);
337 }
338 else
cristybb503372010-05-27 20:51:26 +0000339 for (i=0; i < (ssize_t) (bytes_per_line*image->rows); i++)
cristy3ed852e2009-09-05 21:47:34 +0000340 {
cristybb503372010-05-27 20:51:26 +0000341 value=(size_t) XBMInteger(image,hex_digits);
cristy3ed852e2009-09-05 21:47:34 +0000342 *p++=(unsigned char) value;
343 }
344 /*
345 Convert X bitmap image to pixel packets.
346 */
347 p=data;
cristybb503372010-05-27 20:51:26 +0000348 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000349 {
350 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
351 if (q == (PixelPacket *) NULL)
352 break;
353 indexes=GetAuthenticIndexQueue(image);
354 bit=0;
355 byte=0;
cristybb503372010-05-27 20:51:26 +0000356 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000357 {
358 if (bit == 0)
cristybb503372010-05-27 20:51:26 +0000359 byte=(size_t) (*p++);
cristy3ed852e2009-09-05 21:47:34 +0000360 indexes[x]=(IndexPacket) ((byte & 0x01) != 0 ? 0x01 : 0x00);
361 bit++;
362 byte>>=1;
363 if (bit == 8)
364 bit=0;
365 }
366 if (SyncAuthenticPixels(image,exception) == MagickFalse)
367 break;
368 status=SetImageProgress(image,LoadImageTag,y,image->rows);
369 if (status == MagickFalse)
370 break;
371 }
372 data=(unsigned char *) RelinquishMagickMemory(data);
373 (void) SyncImage(image);
374 (void) CloseBlob(image);
375 return(GetFirstImageInList(image));
376}
377
378/*
379%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380% %
381% %
382% %
383% R e g i s t e r X B M I m a g e %
384% %
385% %
386% %
387%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388%
389% RegisterXBMImage() adds attributes for the XBM image format to
390% the list of supported formats. The attributes include the image format
391% tag, a method to read and/or write the format, whether the format
392% supports the saving of more than one frame to the same file or blob,
393% whether the format supports native in-memory I/O, and a brief
394% description of the format.
395%
396% The format of the RegisterXBMImage method is:
397%
cristybb503372010-05-27 20:51:26 +0000398% size_t RegisterXBMImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000399%
400*/
cristybb503372010-05-27 20:51:26 +0000401ModuleExport size_t RegisterXBMImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000402{
403 MagickInfo
404 *entry;
405
406 entry=SetMagickInfo("XBM");
407 entry->decoder=(DecodeImageHandler *) ReadXBMImage;
408 entry->encoder=(EncodeImageHandler *) WriteXBMImage;
409 entry->magick=(IsImageFormatHandler *) IsXBM;
410 entry->adjoin=MagickFalse;
411 entry->description=ConstantString(
412 "X Windows system bitmap (black and white)");
413 entry->module=ConstantString("XBM");
414 (void) RegisterMagickInfo(entry);
415 return(MagickImageCoderSignature);
416}
417
418/*
419%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420% %
421% %
422% %
423% U n r e g i s t e r X B M I m a g e %
424% %
425% %
426% %
427%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428%
429% UnregisterXBMImage() removes format registrations made by the
430% XBM module from the list of supported formats.
431%
432% The format of the UnregisterXBMImage method is:
433%
434% UnregisterXBMImage(void)
435%
436*/
437ModuleExport void UnregisterXBMImage(void)
438{
439 (void) UnregisterMagickInfo("XBM");
440}
441
442/*
443%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444% %
445% %
446% %
447% W r i t e X B M I m a g e %
448% %
449% %
450% %
451%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452%
453% Procedure WriteXBMImage() writes an image to a file in the X bitmap format.
454%
455% The format of the WriteXBMImage method is:
456%
457% MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image)
458%
459% A description of each parameter follows.
460%
461% o image_info: the image info.
462%
463% o image: The image.
464%
465%
466*/
467static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image)
468{
469 char
470 basename[MaxTextExtent],
471 buffer[MaxTextExtent];
472
cristybb503372010-05-27 20:51:26 +0000473 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000474 y;
475
476 MagickBooleanType
477 status;
478
479 register const PixelPacket
480 *p;
481
cristybb503372010-05-27 20:51:26 +0000482 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000483 x;
484
485 ssize_t
486 count;
487
cristybb503372010-05-27 20:51:26 +0000488 size_t
cristy3ed852e2009-09-05 21:47:34 +0000489 bit,
490 byte;
491
492 /*
493 Open output image file.
494 */
495 assert(image_info != (const ImageInfo *) NULL);
496 assert(image_info->signature == MagickSignature);
497 assert(image != (Image *) NULL);
498 assert(image->signature == MagickSignature);
499 if (image->debug != MagickFalse)
500 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
501 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
502 if (status == MagickFalse)
503 return(status);
504 if (image->colorspace != RGBColorspace)
505 (void) TransformImageColorspace(image,RGBColorspace);
506 /*
507 Write X bitmap header.
508 */
509 GetPathComponent(image->filename,BasePath,basename);
510 (void) FormatMagickString(buffer,MaxTextExtent,"#define %s_width %lu\n",
511 basename,image->columns);
512 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
513 (void) FormatMagickString(buffer,MaxTextExtent,"#define %s_height %lu\n",
514 basename,image->rows);
515 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
516 (void) FormatMagickString(buffer,MaxTextExtent,
517 "static char %s_bits[] = {\n",basename);
518 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
519 (void) CopyMagickString(buffer," ",MaxTextExtent);
520 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
521 /*
522 Convert MIFF to X bitmap pixels.
523 */
524 (void) SetImageType(image,BilevelType);
525 bit=0;
526 byte=0;
527 count=0;
528 x=0;
529 y=0;
530 (void) CopyMagickString(buffer," ",MaxTextExtent);
531 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
cristybb503372010-05-27 20:51:26 +0000532 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000533 {
534 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
535 if (p == (const PixelPacket *) NULL)
536 break;
cristybb503372010-05-27 20:51:26 +0000537 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000538 {
539 byte>>=1;
540 if (PixelIntensity(p) < ((MagickRealType) QuantumRange/2.0))
541 byte|=0x80;
542 bit++;
543 if (bit == 8)
544 {
545 /*
546 Write a bitmap byte to the image file.
547 */
548 (void) FormatMagickString(buffer,MaxTextExtent,"0x%02X, ",
549 (unsigned int) (byte & 0xff));
550 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
551 count++;
552 if (count == 12)
553 {
554 (void) CopyMagickString(buffer,"\n ",MaxTextExtent);
555 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
556 count=0;
557 };
558 bit=0;
559 byte=0;
560 }
561 p++;
562 }
563 if (bit != 0)
564 {
565 /*
566 Write a bitmap byte to the image file.
567 */
568 byte>>=(8-bit);
569 (void) FormatMagickString(buffer,MaxTextExtent,"0x%02X, ",
570 (unsigned int) (byte & 0xff));
571 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
572 count++;
573 if (count == 12)
574 {
575 (void) CopyMagickString(buffer,"\n ",MaxTextExtent);
576 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
577 count=0;
578 };
579 bit=0;
580 byte=0;
581 };
582 status=SetImageProgress(image,SaveImageTag,y,image->rows);
583 if (status == MagickFalse)
584 break;
585 }
586 (void) CopyMagickString(buffer,"};\n",MaxTextExtent);
587 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
588 (void) CloseBlob(image);
589 return(MagickTrue);
590}