cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | % % |
cristy | 7e41fe8 | 2010-12-04 23:12:08 +0000 | [diff] [blame] | 20 | % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
| 43 | #include "MagickCore/blob.h" |
| 44 | #include "MagickCore/blob-private.h" |
| 45 | #include "MagickCore/cache.h" |
| 46 | #include "MagickCore/color-private.h" |
| 47 | #include "MagickCore/colormap.h" |
| 48 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 49 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 50 | #include "MagickCore/exception.h" |
| 51 | #include "MagickCore/exception-private.h" |
| 52 | #include "MagickCore/image.h" |
| 53 | #include "MagickCore/image-private.h" |
| 54 | #include "MagickCore/list.h" |
| 55 | #include "MagickCore/magick.h" |
| 56 | #include "MagickCore/memory_.h" |
| 57 | #include "MagickCore/monitor.h" |
| 58 | #include "MagickCore/monitor-private.h" |
| 59 | #include "MagickCore/pixel-accessor.h" |
| 60 | #include "MagickCore/quantum-private.h" |
| 61 | #include "MagickCore/static.h" |
| 62 | #include "MagickCore/string_.h" |
| 63 | #include "MagickCore/module.h" |
| 64 | #include "MagickCore/utility.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | Forward declarations. |
| 68 | */ |
| 69 | static MagickBooleanType |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 70 | WriteXBMImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 74 | % % |
| 75 | % % |
| 76 | % % |
| 77 | % I s X B M % |
| 78 | % % |
| 79 | % % |
| 80 | % % |
| 81 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 82 | % |
| 83 | % IsXBM() returns MagickTrue if the image format type, identified by the |
| 84 | % magick string, is XBM. |
| 85 | % |
| 86 | % The format of the IsXBM method is: |
| 87 | % |
| 88 | % MagickBooleanType IsXBM(const unsigned char *magick,const size_t length) |
| 89 | % |
| 90 | % A description of each parameter follows: |
| 91 | % |
| 92 | % o magick: compare image format pattern against these bytes. |
| 93 | % |
| 94 | % o length: Specifies the length of the magick string. |
| 95 | % |
| 96 | */ |
| 97 | static MagickBooleanType IsXBM(const unsigned char *magick,const size_t length) |
| 98 | { |
| 99 | if (length < 7) |
| 100 | return(MagickFalse); |
| 101 | if (memcmp(magick,"#define",7) == 0) |
| 102 | return(MagickTrue); |
| 103 | return(MagickFalse); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 108 | % % |
| 109 | % % |
| 110 | % % |
| 111 | % R e a d X B M I m a g e % |
| 112 | % % |
| 113 | % % |
| 114 | % % |
| 115 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 116 | % |
| 117 | % ReadXBMImage() reads an X11 bitmap image file and returns it. It |
| 118 | % allocates the memory necessary for the new Image structure and returns a |
| 119 | % pointer to the new image. |
| 120 | % |
| 121 | % The format of the ReadXBMImage method is: |
| 122 | % |
| 123 | % Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 124 | % |
| 125 | % A description of each parameter follows: |
| 126 | % |
| 127 | % o image_info: the image info. |
| 128 | % |
| 129 | % o exception: return any errors or warnings in this structure. |
| 130 | % |
| 131 | */ |
| 132 | |
| 133 | static int XBMInteger(Image *image,short int *hex_digits) |
| 134 | { |
| 135 | int |
| 136 | c, |
| 137 | flag, |
| 138 | value; |
| 139 | |
| 140 | value=0; |
| 141 | flag=0; |
| 142 | for ( ; ; ) |
| 143 | { |
| 144 | c=ReadBlobByte(image); |
| 145 | if (c == EOF) |
| 146 | { |
| 147 | value=(-1); |
| 148 | break; |
| 149 | } |
| 150 | c&=0xff; |
| 151 | if (isxdigit(c) != MagickFalse) |
| 152 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 153 | value=(int) ((size_t) value << 4)+hex_digits[c]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 154 | flag++; |
| 155 | continue; |
| 156 | } |
| 157 | if ((hex_digits[c]) < 0 && (flag != 0)) |
| 158 | break; |
| 159 | } |
| 160 | return(value); |
| 161 | } |
| 162 | |
| 163 | static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 164 | { |
| 165 | char |
| 166 | buffer[MaxTextExtent], |
| 167 | name[MaxTextExtent]; |
| 168 | |
| 169 | Image |
| 170 | *image; |
| 171 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 172 | MagickBooleanType |
| 173 | status; |
| 174 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 175 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 176 | i, |
| 177 | x; |
| 178 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 179 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 180 | *q; |
| 181 | |
| 182 | register unsigned char |
| 183 | *p; |
| 184 | |
| 185 | short int |
| 186 | hex_digits[256]; |
| 187 | |
| 188 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 189 | bit, |
| 190 | byte, |
| 191 | bytes_per_line, |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 192 | length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 193 | padding, |
| 194 | value, |
| 195 | version; |
| 196 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 197 | ssize_t |
| 198 | y; |
| 199 | |
| 200 | unsigned char |
| 201 | *data; |
| 202 | |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 203 | unsigned long |
| 204 | height, |
| 205 | width; |
| 206 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 207 | /* |
| 208 | Open image file. |
| 209 | */ |
| 210 | assert(image_info != (const ImageInfo *) NULL); |
| 211 | assert(image_info->signature == MagickSignature); |
| 212 | if (image_info->debug != MagickFalse) |
| 213 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 214 | image_info->filename); |
| 215 | assert(exception != (ExceptionInfo *) NULL); |
| 216 | assert(exception->signature == MagickSignature); |
| 217 | image=AcquireImage(image_info); |
| 218 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 219 | if (status == MagickFalse) |
| 220 | { |
| 221 | image=DestroyImageList(image); |
| 222 | return((Image *) NULL); |
| 223 | } |
| 224 | /* |
| 225 | Read X bitmap header. |
| 226 | */ |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 227 | width=0; |
| 228 | height=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 229 | while (ReadBlobString(image,buffer) != (char *) NULL) |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 230 | if (sscanf(buffer,"#define %s %lu",name,&width) == 2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 231 | if ((strlen(name) >= 6) && |
| 232 | (LocaleCompare(name+strlen(name)-6,"_width") == 0)) |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 233 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 234 | while (ReadBlobString(image,buffer) != (char *) NULL) |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 235 | if (sscanf(buffer,"#define %s %lu",name,&height) == 2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 236 | if ((strlen(name) >= 7) && |
| 237 | (LocaleCompare(name+strlen(name)-7,"_height") == 0)) |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 238 | break; |
| 239 | image->columns=width; |
cristy | 9768b93 | 2010-11-19 17:11:01 +0000 | [diff] [blame] | 240 | image->rows=height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 241 | image->depth=8; |
| 242 | image->storage_class=PseudoClass; |
| 243 | image->colors=2; |
| 244 | /* |
| 245 | Scan until hex digits. |
| 246 | */ |
| 247 | version=11; |
| 248 | while (ReadBlobString(image,buffer) != (char *) NULL) |
| 249 | { |
| 250 | if (sscanf(buffer,"static short %s = {",name) == 1) |
| 251 | version=10; |
| 252 | else |
| 253 | if (sscanf(buffer,"static unsigned char %s = {",name) == 1) |
| 254 | version=11; |
| 255 | else |
| 256 | if (sscanf(buffer,"static char %s = {",name) == 1) |
| 257 | version=11; |
| 258 | else |
| 259 | continue; |
| 260 | p=(unsigned char *) strrchr(name,'_'); |
| 261 | if (p == (unsigned char *) NULL) |
| 262 | p=(unsigned char *) name; |
| 263 | else |
| 264 | p++; |
| 265 | if (LocaleCompare("bits[]",(char *) p) == 0) |
| 266 | break; |
| 267 | } |
| 268 | if ((image->columns == 0) || (image->rows == 0) || |
| 269 | (EOFBlob(image) != MagickFalse)) |
| 270 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 271 | /* |
| 272 | Initialize image structure. |
| 273 | */ |
| 274 | if (AcquireImageColormap(image,image->colors) == MagickFalse) |
| 275 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 276 | /* |
| 277 | Initialize colormap. |
| 278 | */ |
| 279 | image->colormap[0].red=(Quantum) QuantumRange; |
| 280 | image->colormap[0].green=(Quantum) QuantumRange; |
| 281 | image->colormap[0].blue=(Quantum) QuantumRange; |
| 282 | image->colormap[1].red=(Quantum) 0; |
| 283 | image->colormap[1].green=(Quantum) 0; |
| 284 | image->colormap[1].blue=(Quantum) 0; |
| 285 | if (image_info->ping != MagickFalse) |
| 286 | { |
| 287 | (void) CloseBlob(image); |
| 288 | return(GetFirstImageInList(image)); |
| 289 | } |
| 290 | /* |
| 291 | Initialize hex values. |
| 292 | */ |
| 293 | hex_digits[(int) '0']=0; |
| 294 | hex_digits[(int) '1']=1; |
| 295 | hex_digits[(int) '2']=2; |
| 296 | hex_digits[(int) '3']=3; |
| 297 | hex_digits[(int) '4']=4; |
| 298 | hex_digits[(int) '5']=5; |
| 299 | hex_digits[(int) '6']=6; |
| 300 | hex_digits[(int) '7']=7; |
| 301 | hex_digits[(int) '8']=8; |
| 302 | hex_digits[(int) '9']=9; |
| 303 | hex_digits[(int) 'A']=10; |
| 304 | hex_digits[(int) 'B']=11; |
| 305 | hex_digits[(int) 'C']=12; |
| 306 | hex_digits[(int) 'D']=13; |
| 307 | hex_digits[(int) 'E']=14; |
| 308 | hex_digits[(int) 'F']=15; |
| 309 | hex_digits[(int) 'a']=10; |
| 310 | hex_digits[(int) 'b']=11; |
| 311 | hex_digits[(int) 'c']=12; |
| 312 | hex_digits[(int) 'd']=13; |
| 313 | hex_digits[(int) 'e']=14; |
| 314 | hex_digits[(int) 'f']=15; |
| 315 | hex_digits[(int) 'x']=0; |
| 316 | hex_digits[(int) ' ']=(-1); |
| 317 | hex_digits[(int) ',']=(-1); |
| 318 | hex_digits[(int) '}']=(-1); |
| 319 | hex_digits[(int) '\n']=(-1); |
| 320 | hex_digits[(int) '\t']=(-1); |
| 321 | /* |
| 322 | Read hex image data. |
| 323 | */ |
| 324 | padding=0; |
| 325 | if (((image->columns % 16) != 0) && |
| 326 | ((image->columns % 16) < 9) && (version == 10)) |
| 327 | padding=1; |
| 328 | bytes_per_line=(image->columns+7)/8+padding; |
| 329 | length=(size_t) image->rows; |
| 330 | data=(unsigned char *) AcquireQuantumMemory(length,bytes_per_line* |
| 331 | sizeof(*data)); |
| 332 | if (data == (unsigned char *) NULL) |
| 333 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 334 | p=data; |
| 335 | if (version == 10) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 336 | for (i=0; i < (ssize_t) (bytes_per_line*image->rows); (i+=2)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 337 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 338 | value=(size_t) XBMInteger(image,hex_digits); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 339 | *p++=(unsigned char) value; |
| 340 | if ((padding == 0) || (((i+2) % bytes_per_line) != 0)) |
| 341 | *p++=(unsigned char) (value >> 8); |
| 342 | } |
| 343 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 344 | for (i=0; i < (ssize_t) (bytes_per_line*image->rows); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 345 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 346 | value=(size_t) XBMInteger(image,hex_digits); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 347 | *p++=(unsigned char) value; |
| 348 | } |
| 349 | /* |
| 350 | Convert X bitmap image to pixel packets. |
| 351 | */ |
| 352 | p=data; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 353 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 354 | { |
| 355 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 356 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 357 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 358 | bit=0; |
| 359 | byte=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 360 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 361 | { |
| 362 | if (bit == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 363 | byte=(size_t) (*p++); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 364 | SetPixelIndex(image,(byte & 0x01) != 0 ? 0x01 : 0x00,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 365 | bit++; |
| 366 | byte>>=1; |
| 367 | if (bit == 8) |
| 368 | bit=0; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 369 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 370 | } |
| 371 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 372 | break; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 373 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 374 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 375 | if (status == MagickFalse) |
| 376 | break; |
| 377 | } |
| 378 | data=(unsigned char *) RelinquishMagickMemory(data); |
| 379 | (void) SyncImage(image); |
| 380 | (void) CloseBlob(image); |
| 381 | return(GetFirstImageInList(image)); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 386 | % % |
| 387 | % % |
| 388 | % % |
| 389 | % R e g i s t e r X B M I m a g e % |
| 390 | % % |
| 391 | % % |
| 392 | % % |
| 393 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 394 | % |
| 395 | % RegisterXBMImage() adds attributes for the XBM image format to |
| 396 | % the list of supported formats. The attributes include the image format |
| 397 | % tag, a method to read and/or write the format, whether the format |
| 398 | % supports the saving of more than one frame to the same file or blob, |
| 399 | % whether the format supports native in-memory I/O, and a brief |
| 400 | % description of the format. |
| 401 | % |
| 402 | % The format of the RegisterXBMImage method is: |
| 403 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 404 | % size_t RegisterXBMImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 405 | % |
| 406 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 407 | ModuleExport size_t RegisterXBMImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 408 | { |
| 409 | MagickInfo |
| 410 | *entry; |
| 411 | |
| 412 | entry=SetMagickInfo("XBM"); |
| 413 | entry->decoder=(DecodeImageHandler *) ReadXBMImage; |
| 414 | entry->encoder=(EncodeImageHandler *) WriteXBMImage; |
| 415 | entry->magick=(IsImageFormatHandler *) IsXBM; |
| 416 | entry->adjoin=MagickFalse; |
| 417 | entry->description=ConstantString( |
| 418 | "X Windows system bitmap (black and white)"); |
| 419 | entry->module=ConstantString("XBM"); |
| 420 | (void) RegisterMagickInfo(entry); |
| 421 | return(MagickImageCoderSignature); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 426 | % % |
| 427 | % % |
| 428 | % % |
| 429 | % U n r e g i s t e r X B M I m a g e % |
| 430 | % % |
| 431 | % % |
| 432 | % % |
| 433 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 434 | % |
| 435 | % UnregisterXBMImage() removes format registrations made by the |
| 436 | % XBM module from the list of supported formats. |
| 437 | % |
| 438 | % The format of the UnregisterXBMImage method is: |
| 439 | % |
| 440 | % UnregisterXBMImage(void) |
| 441 | % |
| 442 | */ |
| 443 | ModuleExport void UnregisterXBMImage(void) |
| 444 | { |
| 445 | (void) UnregisterMagickInfo("XBM"); |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 450 | % % |
| 451 | % % |
| 452 | % % |
| 453 | % W r i t e X B M I m a g e % |
| 454 | % % |
| 455 | % % |
| 456 | % % |
| 457 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 458 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 459 | % WriteXBMImage() writes an image to a file in the X bitmap format. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 460 | % |
| 461 | % The format of the WriteXBMImage method is: |
| 462 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 463 | % MagickBooleanType WriteXBMImage(const ImageInfo *image_info, |
| 464 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 465 | % |
| 466 | % A description of each parameter follows. |
| 467 | % |
| 468 | % o image_info: the image info. |
| 469 | % |
| 470 | % o image: The image. |
| 471 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 472 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 473 | % |
| 474 | */ |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 475 | static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image, |
| 476 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 477 | { |
| 478 | char |
| 479 | basename[MaxTextExtent], |
| 480 | buffer[MaxTextExtent]; |
| 481 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 482 | MagickBooleanType |
| 483 | status; |
| 484 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 485 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 486 | *p; |
| 487 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 488 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 489 | x; |
| 490 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 491 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 492 | bit, |
| 493 | byte; |
| 494 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 495 | ssize_t |
| 496 | count, |
| 497 | y; |
| 498 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 499 | /* |
| 500 | Open output image file. |
| 501 | */ |
| 502 | assert(image_info != (const ImageInfo *) NULL); |
| 503 | assert(image_info->signature == MagickSignature); |
| 504 | assert(image != (Image *) NULL); |
| 505 | assert(image->signature == MagickSignature); |
| 506 | if (image->debug != MagickFalse) |
| 507 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 508 | assert(exception != (ExceptionInfo *) NULL); |
| 509 | assert(exception->signature == MagickSignature); |
| 510 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 511 | if (status == MagickFalse) |
| 512 | return(status); |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 513 | if (IsRGBColorspace(image->colorspace) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 514 | (void) TransformImageColorspace(image,RGBColorspace); |
| 515 | /* |
| 516 | Write X bitmap header. |
| 517 | */ |
| 518 | GetPathComponent(image->filename,BasePath,basename); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 519 | (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_width %.20g\n", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 520 | basename,(double) image->columns); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 521 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 522 | (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_height %.20g\n", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 523 | basename,(double) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 524 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 525 | (void) FormatLocaleString(buffer,MaxTextExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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); |
| 530 | /* |
| 531 | Convert MIFF to X bitmap pixels. |
| 532 | */ |
| 533 | (void) SetImageType(image,BilevelType); |
| 534 | bit=0; |
| 535 | byte=0; |
| 536 | count=0; |
| 537 | x=0; |
| 538 | y=0; |
| 539 | (void) CopyMagickString(buffer," ",MaxTextExtent); |
| 540 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 541 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 542 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 543 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 544 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 545 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 546 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 547 | { |
| 548 | byte>>=1; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 549 | if (GetPixelIntensity(image,p) < ((MagickRealType) QuantumRange/2.0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 550 | byte|=0x80; |
| 551 | bit++; |
| 552 | if (bit == 8) |
| 553 | { |
| 554 | /* |
| 555 | Write a bitmap byte to the image file. |
| 556 | */ |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 557 | (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 558 | (unsigned int) (byte & 0xff)); |
| 559 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 560 | count++; |
| 561 | if (count == 12) |
| 562 | { |
| 563 | (void) CopyMagickString(buffer,"\n ",MaxTextExtent); |
| 564 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 565 | count=0; |
| 566 | }; |
| 567 | bit=0; |
| 568 | byte=0; |
| 569 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 570 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 571 | } |
| 572 | if (bit != 0) |
| 573 | { |
| 574 | /* |
| 575 | Write a bitmap byte to the image file. |
| 576 | */ |
| 577 | byte>>=(8-bit); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 578 | (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 579 | (unsigned int) (byte & 0xff)); |
| 580 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 581 | count++; |
| 582 | if (count == 12) |
| 583 | { |
| 584 | (void) CopyMagickString(buffer,"\n ",MaxTextExtent); |
| 585 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 586 | count=0; |
| 587 | }; |
| 588 | bit=0; |
| 589 | byte=0; |
| 590 | }; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 591 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 592 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 593 | if (status == MagickFalse) |
| 594 | break; |
| 595 | } |
| 596 | (void) CopyMagickString(buffer,"};\n",MaxTextExtent); |
| 597 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 598 | (void) CloseBlob(image); |
| 599 | return(MagickTrue); |
| 600 | } |