blob: 3b3b314ddb430a2a0eeec0f88227908fb2436d16 [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
cristyb56bb242014-11-25 17:12:48 +000020% Copyright 1999-2015 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
cristyd9ecd042012-06-17 18:26:12 +000043#include "MagickCore/attribute.h"
cristy4c08aed2011-07-01 19:47:50 +000044#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"
cristy510d06a2011-07-06 23:43:54 +000050#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#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"
cristy3ed852e2009-09-05 21:47:34 +000066
67/*
68 Forward declarations.
69*/
70static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000071 WriteXBMImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000072
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*/
98static 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
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
cristy920046a2014-12-15 01:52:11 +0000134static unsigned int XBMInteger(Image *image,short int *hex_digits)
135{
cristy3ed852e2009-09-05 21:47:34 +0000136 int
cristy920046a2014-12-15 01:52:11 +0000137 c;
138
139 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000140 value;
cristy920046a2014-12-15 01:52:11 +0000141
142 /*
143 Skip any leading whitespace.
144 */
145 do
146 {
cristy3ed852e2009-09-05 21:47:34 +0000147 c=ReadBlobByte(image);
148 if (c == EOF)
cristy920046a2014-12-15 01:52:11 +0000149 return(0);
cristyd178f432014-12-15 11:03:40 +0000150 } while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
cristy920046a2014-12-15 01:52:11 +0000151 /*
152 Evaluate number.
153 */
154 value=0;
155 do
156 {
157 if (value > (unsigned int) (INT_MAX/10))
cristy3ed852e2009-09-05 21:47:34 +0000158 break;
cristy920046a2014-12-15 01:52:11 +0000159 value*=16;
160 c&=0xff;
cristy40a5c452014-12-15 22:13:06 +0000161 if (value > (unsigned int) (INT_MAX-hex_digits[c]))
cristy920046a2014-12-15 01:52:11 +0000162 break;
163 value+=hex_digits[c];
164 c=ReadBlobByte(image);
cristyd178f432014-12-15 11:03:40 +0000165 if (c == EOF)
166 return(0);
cristy920046a2014-12-15 01:52:11 +0000167 } while (hex_digits[c] >= 0);
cristy3ed852e2009-09-05 21:47:34 +0000168 return(value);
169}
170
171static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
172{
173 char
174 buffer[MaxTextExtent],
175 name[MaxTextExtent];
176
177 Image
178 *image;
179
cristy3ed852e2009-09-05 21:47:34 +0000180 MagickBooleanType
181 status;
182
cristybb503372010-05-27 20:51:26 +0000183 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000184 i,
185 x;
186
cristy4c08aed2011-07-01 19:47:50 +0000187 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000188 *q;
189
190 register unsigned char
191 *p;
192
193 short int
194 hex_digits[256];
195
cristyc6da28e2011-04-28 01:41:35 +0000196 ssize_t
197 y;
198
199 unsigned char
200 *data;
201
cristy920046a2014-12-15 01:52:11 +0000202 unsigned int
203 bit,
204 byte,
205 bytes_per_line,
cristyf1d91242010-05-28 02:23:19 +0000206 height,
cristy920046a2014-12-15 01:52:11 +0000207 length,
208 padding,
209 value,
210 version,
cristyf1d91242010-05-28 02:23:19 +0000211 width;
212
cristy3ed852e2009-09-05 21:47:34 +0000213 /*
214 Open image file.
215 */
216 assert(image_info != (const ImageInfo *) NULL);
217 assert(image_info->signature == MagickSignature);
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 == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000223 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000224 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
225 if (status == MagickFalse)
226 {
227 image=DestroyImageList(image);
228 return((Image *) NULL);
229 }
230 /*
231 Read X bitmap header.
232 */
cristyf1d91242010-05-28 02:23:19 +0000233 width=0;
234 height=0;
cristy3ed852e2009-09-05 21:47:34 +0000235 while (ReadBlobString(image,buffer) != (char *) NULL)
cristy920046a2014-12-15 01:52:11 +0000236 if (sscanf(buffer,"#define %s %u",name,&width) == 2)
cristy3ed852e2009-09-05 21:47:34 +0000237 if ((strlen(name) >= 6) &&
238 (LocaleCompare(name+strlen(name)-6,"_width") == 0))
cristyf1d91242010-05-28 02:23:19 +0000239 break;
cristy3ed852e2009-09-05 21:47:34 +0000240 while (ReadBlobString(image,buffer) != (char *) NULL)
cristy920046a2014-12-15 01:52:11 +0000241 if (sscanf(buffer,"#define %s %u",name,&height) == 2)
cristy3ed852e2009-09-05 21:47:34 +0000242 if ((strlen(name) >= 7) &&
243 (LocaleCompare(name+strlen(name)-7,"_height") == 0))
cristyf1d91242010-05-28 02:23:19 +0000244 break;
245 image->columns=width;
cristy9768b932010-11-19 17:11:01 +0000246 image->rows=height;
cristy3ed852e2009-09-05 21:47:34 +0000247 image->depth=8;
248 image->storage_class=PseudoClass;
249 image->colors=2;
250 /*
251 Scan until hex digits.
252 */
253 version=11;
254 while (ReadBlobString(image,buffer) != (char *) NULL)
255 {
256 if (sscanf(buffer,"static short %s = {",name) == 1)
257 version=10;
258 else
259 if (sscanf(buffer,"static unsigned char %s = {",name) == 1)
260 version=11;
261 else
262 if (sscanf(buffer,"static char %s = {",name) == 1)
263 version=11;
264 else
265 continue;
266 p=(unsigned char *) strrchr(name,'_');
267 if (p == (unsigned char *) NULL)
268 p=(unsigned char *) name;
269 else
270 p++;
271 if (LocaleCompare("bits[]",(char *) p) == 0)
272 break;
273 }
274 if ((image->columns == 0) || (image->rows == 0) ||
275 (EOFBlob(image) != MagickFalse))
276 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
277 /*
278 Initialize image structure.
279 */
cristy018f07f2011-09-04 21:15:19 +0000280 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000281 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
282 /*
283 Initialize colormap.
284 */
cristy6e963d82012-06-19 15:23:24 +0000285 image->colormap[0].red=QuantumRange;
286 image->colormap[0].green=QuantumRange;
287 image->colormap[0].blue=QuantumRange;
cristy3ed852e2009-09-05 21:47:34 +0000288 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)
292 {
293 (void) CloseBlob(image);
294 return(GetFirstImageInList(image));
295 }
cristyacabb842014-12-14 23:36:33 +0000296 status=SetImageExtent(image,image->columns,image->rows,exception);
297 if (status == MagickFalse)
298 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000299 /*
300 Initialize hex values.
301 */
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);
330 /*
331 Read hex image data.
332 */
333 padding=0;
cristyc4fc8e72012-06-10 14:57:23 +0000334 if (((image->columns % 16) != 0) && ((image->columns % 16) < 9) &&
335 (version == 10))
cristy3ed852e2009-09-05 21:47:34 +0000336 padding=1;
dirk39c51eb2014-12-27 01:01:27 +0000337 bytes_per_line=(unsigned int) (image->columns+7)/8+padding;
338 length=(unsigned int) image->rows;
cristy3ed852e2009-09-05 21:47:34 +0000339 data=(unsigned char *) AcquireQuantumMemory(length,bytes_per_line*
340 sizeof(*data));
341 if (data == (unsigned char *) NULL)
342 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
343 p=data;
344 if (version == 10)
cristybb503372010-05-27 20:51:26 +0000345 for (i=0; i < (ssize_t) (bytes_per_line*image->rows); (i+=2))
cristy3ed852e2009-09-05 21:47:34 +0000346 {
cristy920046a2014-12-15 01:52:11 +0000347 value=XBMInteger(image,hex_digits);
cristy3ed852e2009-09-05 21:47:34 +0000348 *p++=(unsigned char) value;
349 if ((padding == 0) || (((i+2) % bytes_per_line) != 0))
350 *p++=(unsigned char) (value >> 8);
351 }
352 else
cristybb503372010-05-27 20:51:26 +0000353 for (i=0; i < (ssize_t) (bytes_per_line*image->rows); i++)
cristy3ed852e2009-09-05 21:47:34 +0000354 {
cristy920046a2014-12-15 01:52:11 +0000355 value=XBMInteger(image,hex_digits);
cristy3ed852e2009-09-05 21:47:34 +0000356 *p++=(unsigned char) value;
357 }
358 /*
359 Convert X bitmap image to pixel packets.
360 */
361 p=data;
cristybb503372010-05-27 20:51:26 +0000362 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000363 {
364 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000365 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000366 break;
cristy3ed852e2009-09-05 21:47:34 +0000367 bit=0;
368 byte=0;
cristybb503372010-05-27 20:51:26 +0000369 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000370 {
371 if (bit == 0)
cristybb503372010-05-27 20:51:26 +0000372 byte=(size_t) (*p++);
cristyf19c9902012-06-10 14:59:10 +0000373 SetPixelIndex(image,(Quantum) ((byte & 0x01) != 0 ? 0x01 : 0x00),q);
cristy3ed852e2009-09-05 21:47:34 +0000374 bit++;
375 byte>>=1;
376 if (bit == 8)
377 bit=0;
cristyed231572011-07-14 02:18:59 +0000378 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000379 }
380 if (SyncAuthenticPixels(image,exception) == MagickFalse)
381 break;
cristycee97112010-05-28 00:44:52 +0000382 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000383 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000384 if (status == MagickFalse)
385 break;
386 }
387 data=(unsigned char *) RelinquishMagickMemory(data);
cristyea1a8aa2011-10-20 13:24:06 +0000388 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000389 (void) CloseBlob(image);
390 return(GetFirstImageInList(image));
391}
392
393/*
394%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395% %
396% %
397% %
398% R e g i s t e r X B M I m a g e %
399% %
400% %
401% %
402%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403%
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.
410%
411% The format of the RegisterXBMImage method is:
412%
cristybb503372010-05-27 20:51:26 +0000413% size_t RegisterXBMImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000414%
415*/
cristybb503372010-05-27 20:51:26 +0000416ModuleExport size_t RegisterXBMImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000417{
418 MagickInfo
419 *entry;
420
421 entry=SetMagickInfo("XBM");
422 entry->decoder=(DecodeImageHandler *) ReadXBMImage;
423 entry->encoder=(EncodeImageHandler *) WriteXBMImage;
424 entry->magick=(IsImageFormatHandler *) IsXBM;
dirk08e9a112015-02-22 01:51:41 +0000425 entry->flags^=CoderAdjoinFlag;
cristy3ed852e2009-09-05 21:47:34 +0000426 entry->description=ConstantString(
427 "X Windows system bitmap (black and white)");
428 entry->module=ConstantString("XBM");
429 (void) RegisterMagickInfo(entry);
430 return(MagickImageCoderSignature);
431}
432
433/*
434%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435% %
436% %
437% %
438% U n r e g i s t e r X B M I m a g e %
439% %
440% %
441% %
442%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
443%
444% UnregisterXBMImage() removes format registrations made by the
445% XBM module from the list of supported formats.
446%
447% The format of the UnregisterXBMImage method is:
448%
449% UnregisterXBMImage(void)
450%
451*/
452ModuleExport void UnregisterXBMImage(void)
453{
454 (void) UnregisterMagickInfo("XBM");
455}
456
457/*
458%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459% %
460% %
461% %
462% W r i t e X B M I m a g e %
463% %
464% %
465% %
466%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467%
cristy3a37efd2011-08-28 20:31:03 +0000468% WriteXBMImage() writes an image to a file in the X bitmap format.
cristy3ed852e2009-09-05 21:47:34 +0000469%
470% The format of the WriteXBMImage method is:
471%
cristy3a37efd2011-08-28 20:31:03 +0000472% MagickBooleanType WriteXBMImage(const ImageInfo *image_info,
473% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000474%
475% A description of each parameter follows.
476%
477% o image_info: the image info.
478%
479% o image: The image.
480%
cristy3a37efd2011-08-28 20:31:03 +0000481% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +0000482%
483*/
cristy3a37efd2011-08-28 20:31:03 +0000484static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
485 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000486{
487 char
488 basename[MaxTextExtent],
489 buffer[MaxTextExtent];
490
cristy3ed852e2009-09-05 21:47:34 +0000491 MagickBooleanType
492 status;
493
cristy4c08aed2011-07-01 19:47:50 +0000494 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000495 *p;
496
cristybb503372010-05-27 20:51:26 +0000497 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000498 x;
499
cristybb503372010-05-27 20:51:26 +0000500 size_t
cristy3ed852e2009-09-05 21:47:34 +0000501 bit,
502 byte;
503
cristyc6da28e2011-04-28 01:41:35 +0000504 ssize_t
505 count,
506 y;
507
cristy3ed852e2009-09-05 21:47:34 +0000508 /*
509 Open output image file.
510 */
511 assert(image_info != (const ImageInfo *) NULL);
512 assert(image_info->signature == MagickSignature);
513 assert(image != (Image *) NULL);
514 assert(image->signature == MagickSignature);
515 if (image->debug != MagickFalse)
516 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000517 assert(exception != (ExceptionInfo *) NULL);
518 assert(exception->signature == MagickSignature);
519 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000520 if (status == MagickFalse)
521 return(status);
cristyaf8d3912014-02-21 14:50:33 +0000522 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000523 /*
524 Write X bitmap header.
525 */
526 GetPathComponent(image->filename,BasePath,basename);
cristyb51dff52011-05-19 16:55:47 +0000527 (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_width %.20g\n",
cristye8c25f92010-06-03 00:53:06 +0000528 basename,(double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +0000529 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
cristyb51dff52011-05-19 16:55:47 +0000530 (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_height %.20g\n",
cristye8c25f92010-06-03 00:53:06 +0000531 basename,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000532 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
cristyb51dff52011-05-19 16:55:47 +0000533 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000534 "static char %s_bits[] = {\n",basename);
535 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
536 (void) CopyMagickString(buffer," ",MaxTextExtent);
537 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
538 /*
539 Convert MIFF to X bitmap pixels.
540 */
cristy018f07f2011-09-04 21:15:19 +0000541 (void) SetImageType(image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000542 bit=0;
543 byte=0;
544 count=0;
545 x=0;
546 y=0;
547 (void) CopyMagickString(buffer," ",MaxTextExtent);
548 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
cristybb503372010-05-27 20:51:26 +0000549 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000550 {
cristy3a37efd2011-08-28 20:31:03 +0000551 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000552 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000553 break;
cristybb503372010-05-27 20:51:26 +0000554 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000555 {
556 byte>>=1;
cristyd0323222013-04-07 16:13:21 +0000557 if (GetPixelLuma(image,p) < (QuantumRange/2))
cristy3ed852e2009-09-05 21:47:34 +0000558 byte|=0x80;
559 bit++;
560 if (bit == 8)
561 {
562 /*
563 Write a bitmap byte to the image file.
564 */
cristyb51dff52011-05-19 16:55:47 +0000565 (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ",
cristy3ed852e2009-09-05 21:47:34 +0000566 (unsigned int) (byte & 0xff));
567 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
568 count++;
569 if (count == 12)
570 {
571 (void) CopyMagickString(buffer,"\n ",MaxTextExtent);
572 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
573 count=0;
574 };
575 bit=0;
576 byte=0;
577 }
cristyed231572011-07-14 02:18:59 +0000578 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000579 }
580 if (bit != 0)
581 {
582 /*
583 Write a bitmap byte to the image file.
584 */
585 byte>>=(8-bit);
cristyb51dff52011-05-19 16:55:47 +0000586 (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ",
cristy3ed852e2009-09-05 21:47:34 +0000587 (unsigned int) (byte & 0xff));
588 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
589 count++;
590 if (count == 12)
591 {
592 (void) CopyMagickString(buffer,"\n ",MaxTextExtent);
593 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
594 count=0;
595 };
596 bit=0;
597 byte=0;
598 };
cristycee97112010-05-28 00:44:52 +0000599 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000600 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000601 if (status == MagickFalse)
602 break;
603 }
604 (void) CopyMagickString(buffer,"};\n",MaxTextExtent);
605 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
606 (void) CloseBlob(image);
607 return(MagickTrue);
608}