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 | 45ef08f | 2012-12-07 13:13:34 +0000 | [diff] [blame] | 20 | % Copyright 1999-2013 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" |
cristy | d9ecd04 | 2012-06-17 18:26:12 +0000 | [diff] [blame] | 43 | #include "MagickCore/attribute.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 50 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | Forward declarations. |
| 69 | */ |
| 70 | static MagickBooleanType |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 71 | WriteXBMImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 72 | |
| 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 | */ |
| 98 | static 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 | |
| 134 | static int XBMInteger(Image *image,short int *hex_digits) |
| 135 | { |
| 136 | int |
| 137 | c, |
| 138 | flag, |
| 139 | value; |
| 140 | |
| 141 | value=0; |
| 142 | flag=0; |
| 143 | for ( ; ; ) |
| 144 | { |
| 145 | c=ReadBlobByte(image); |
| 146 | if (c == EOF) |
| 147 | { |
| 148 | value=(-1); |
| 149 | break; |
| 150 | } |
| 151 | c&=0xff; |
| 152 | if (isxdigit(c) != MagickFalse) |
| 153 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 154 | value=(int) ((size_t) value << 4)+hex_digits[c]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 155 | flag++; |
| 156 | continue; |
| 157 | } |
| 158 | if ((hex_digits[c]) < 0 && (flag != 0)) |
| 159 | break; |
| 160 | } |
| 161 | return(value); |
| 162 | } |
| 163 | |
| 164 | static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 165 | { |
| 166 | char |
| 167 | buffer[MaxTextExtent], |
| 168 | name[MaxTextExtent]; |
| 169 | |
| 170 | Image |
| 171 | *image; |
| 172 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 173 | MagickBooleanType |
| 174 | status; |
| 175 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 176 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 177 | i, |
| 178 | x; |
| 179 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 180 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 181 | *q; |
| 182 | |
| 183 | register unsigned char |
| 184 | *p; |
| 185 | |
| 186 | short int |
| 187 | hex_digits[256]; |
| 188 | |
| 189 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 190 | bit, |
| 191 | byte, |
| 192 | bytes_per_line, |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 193 | length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 194 | padding, |
| 195 | value, |
| 196 | version; |
| 197 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 198 | ssize_t |
| 199 | y; |
| 200 | |
| 201 | unsigned char |
| 202 | *data; |
| 203 | |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 204 | unsigned long |
| 205 | height, |
| 206 | width; |
| 207 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 208 | /* |
| 209 | Open image file. |
| 210 | */ |
| 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); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 218 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 219 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 220 | if (status == MagickFalse) |
| 221 | { |
| 222 | image=DestroyImageList(image); |
| 223 | return((Image *) NULL); |
| 224 | } |
| 225 | /* |
| 226 | Read X bitmap header. |
| 227 | */ |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 228 | width=0; |
| 229 | height=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 230 | while (ReadBlobString(image,buffer) != (char *) NULL) |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 231 | if (sscanf(buffer,"#define %s %lu",name,&width) == 2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 232 | if ((strlen(name) >= 6) && |
| 233 | (LocaleCompare(name+strlen(name)-6,"_width") == 0)) |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 234 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 235 | while (ReadBlobString(image,buffer) != (char *) NULL) |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 236 | if (sscanf(buffer,"#define %s %lu",name,&height) == 2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 237 | if ((strlen(name) >= 7) && |
| 238 | (LocaleCompare(name+strlen(name)-7,"_height") == 0)) |
cristy | f1d9124 | 2010-05-28 02:23:19 +0000 | [diff] [blame] | 239 | break; |
| 240 | image->columns=width; |
cristy | 9768b93 | 2010-11-19 17:11:01 +0000 | [diff] [blame] | 241 | image->rows=height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 242 | image->depth=8; |
| 243 | image->storage_class=PseudoClass; |
| 244 | image->colors=2; |
| 245 | /* |
| 246 | Scan until hex digits. |
| 247 | */ |
| 248 | version=11; |
| 249 | while (ReadBlobString(image,buffer) != (char *) NULL) |
| 250 | { |
| 251 | if (sscanf(buffer,"static short %s = {",name) == 1) |
| 252 | version=10; |
| 253 | else |
| 254 | if (sscanf(buffer,"static unsigned char %s = {",name) == 1) |
| 255 | version=11; |
| 256 | else |
| 257 | if (sscanf(buffer,"static char %s = {",name) == 1) |
| 258 | version=11; |
| 259 | else |
| 260 | continue; |
| 261 | p=(unsigned char *) strrchr(name,'_'); |
| 262 | if (p == (unsigned char *) NULL) |
| 263 | p=(unsigned char *) name; |
| 264 | else |
| 265 | p++; |
| 266 | if (LocaleCompare("bits[]",(char *) p) == 0) |
| 267 | break; |
| 268 | } |
| 269 | if ((image->columns == 0) || (image->rows == 0) || |
| 270 | (EOFBlob(image) != MagickFalse)) |
| 271 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 272 | /* |
| 273 | Initialize image structure. |
| 274 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 275 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 276 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 277 | /* |
| 278 | Initialize colormap. |
| 279 | */ |
cristy | 6e963d8 | 2012-06-19 15:23:24 +0000 | [diff] [blame] | 280 | image->colormap[0].red=QuantumRange; |
| 281 | image->colormap[0].green=QuantumRange; |
| 282 | image->colormap[0].blue=QuantumRange; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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) |
| 287 | { |
| 288 | (void) CloseBlob(image); |
| 289 | return(GetFirstImageInList(image)); |
| 290 | } |
| 291 | /* |
| 292 | Initialize hex values. |
| 293 | */ |
| 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); |
| 322 | /* |
| 323 | Read hex image data. |
| 324 | */ |
| 325 | padding=0; |
cristy | c4fc8e7 | 2012-06-10 14:57:23 +0000 | [diff] [blame] | 326 | if (((image->columns % 16) != 0) && ((image->columns % 16) < 9) && |
| 327 | (version == 10)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 328 | padding=1; |
| 329 | bytes_per_line=(image->columns+7)/8+padding; |
| 330 | length=(size_t) image->rows; |
| 331 | data=(unsigned char *) AcquireQuantumMemory(length,bytes_per_line* |
| 332 | sizeof(*data)); |
| 333 | if (data == (unsigned char *) NULL) |
| 334 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 335 | p=data; |
| 336 | if (version == 10) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 337 | for (i=0; i < (ssize_t) (bytes_per_line*image->rows); (i+=2)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 338 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 339 | value=(size_t) XBMInteger(image,hex_digits); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 340 | *p++=(unsigned char) value; |
| 341 | if ((padding == 0) || (((i+2) % bytes_per_line) != 0)) |
| 342 | *p++=(unsigned char) (value >> 8); |
| 343 | } |
| 344 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 345 | for (i=0; i < (ssize_t) (bytes_per_line*image->rows); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 346 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 347 | value=(size_t) XBMInteger(image,hex_digits); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 348 | *p++=(unsigned char) value; |
| 349 | } |
| 350 | /* |
| 351 | Convert X bitmap image to pixel packets. |
| 352 | */ |
| 353 | p=data; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 354 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 355 | { |
| 356 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 357 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 358 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 359 | bit=0; |
| 360 | byte=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 361 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 362 | { |
| 363 | if (bit == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 364 | byte=(size_t) (*p++); |
cristy | f19c990 | 2012-06-10 14:59:10 +0000 | [diff] [blame] | 365 | SetPixelIndex(image,(Quantum) ((byte & 0x01) != 0 ? 0x01 : 0x00),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 366 | bit++; |
| 367 | byte>>=1; |
| 368 | if (bit == 8) |
| 369 | bit=0; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 370 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 371 | } |
| 372 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 373 | break; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 374 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 375 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 376 | if (status == MagickFalse) |
| 377 | break; |
| 378 | } |
| 379 | data=(unsigned char *) RelinquishMagickMemory(data); |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 380 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 381 | (void) CloseBlob(image); |
| 382 | return(GetFirstImageInList(image)); |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 387 | % % |
| 388 | % % |
| 389 | % % |
| 390 | % R e g i s t e r X B M I m a g e % |
| 391 | % % |
| 392 | % % |
| 393 | % % |
| 394 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 395 | % |
| 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. |
| 402 | % |
| 403 | % The format of the RegisterXBMImage method is: |
| 404 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 405 | % size_t RegisterXBMImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 406 | % |
| 407 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 408 | ModuleExport size_t RegisterXBMImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 409 | { |
| 410 | MagickInfo |
| 411 | *entry; |
| 412 | |
| 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); |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 427 | % % |
| 428 | % % |
| 429 | % % |
| 430 | % U n r e g i s t e r X B M I m a g e % |
| 431 | % % |
| 432 | % % |
| 433 | % % |
| 434 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 435 | % |
| 436 | % UnregisterXBMImage() removes format registrations made by the |
| 437 | % XBM module from the list of supported formats. |
| 438 | % |
| 439 | % The format of the UnregisterXBMImage method is: |
| 440 | % |
| 441 | % UnregisterXBMImage(void) |
| 442 | % |
| 443 | */ |
| 444 | ModuleExport void UnregisterXBMImage(void) |
| 445 | { |
| 446 | (void) UnregisterMagickInfo("XBM"); |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 451 | % % |
| 452 | % % |
| 453 | % % |
| 454 | % W r i t e X B M I m a g e % |
| 455 | % % |
| 456 | % % |
| 457 | % % |
| 458 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 459 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 460 | % WriteXBMImage() writes an image to a file in the X bitmap format. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 461 | % |
| 462 | % The format of the WriteXBMImage method is: |
| 463 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 464 | % MagickBooleanType WriteXBMImage(const ImageInfo *image_info, |
| 465 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 466 | % |
| 467 | % A description of each parameter follows. |
| 468 | % |
| 469 | % o image_info: the image info. |
| 470 | % |
| 471 | % o image: The image. |
| 472 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 473 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 474 | % |
| 475 | */ |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 476 | static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image, |
| 477 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 478 | { |
| 479 | char |
| 480 | basename[MaxTextExtent], |
| 481 | buffer[MaxTextExtent]; |
| 482 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 483 | MagickBooleanType |
| 484 | status; |
| 485 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 486 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 487 | *p; |
| 488 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 489 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 490 | x; |
| 491 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 492 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 493 | bit, |
| 494 | byte; |
| 495 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 496 | ssize_t |
| 497 | count, |
| 498 | y; |
| 499 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 500 | /* |
| 501 | Open output image file. |
| 502 | */ |
| 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); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 509 | assert(exception != (ExceptionInfo *) NULL); |
| 510 | assert(exception->signature == MagickSignature); |
| 511 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 512 | if (status == MagickFalse) |
| 513 | return(status); |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 514 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
cristy | 8d95109 | 2012-02-08 18:54:56 +0000 | [diff] [blame] | 515 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 516 | /* |
| 517 | Write X bitmap header. |
| 518 | */ |
| 519 | GetPathComponent(image->filename,BasePath,basename); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 520 | (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_width %.20g\n", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 521 | basename,(double) image->columns); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 522 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 523 | (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_height %.20g\n", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 524 | basename,(double) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 525 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 526 | (void) FormatLocaleString(buffer,MaxTextExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 527 | "static char %s_bits[] = {\n",basename); |
| 528 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 529 | (void) CopyMagickString(buffer," ",MaxTextExtent); |
| 530 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 531 | /* |
| 532 | Convert MIFF to X bitmap pixels. |
| 533 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 534 | (void) SetImageType(image,BilevelType,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 535 | bit=0; |
| 536 | byte=0; |
| 537 | count=0; |
| 538 | x=0; |
| 539 | y=0; |
| 540 | (void) CopyMagickString(buffer," ",MaxTextExtent); |
| 541 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 542 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 543 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 544 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 545 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 546 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 547 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 548 | { |
| 549 | byte>>=1; |
cristy | f19c990 | 2012-06-10 14:59:10 +0000 | [diff] [blame] | 550 | if (GetPixelIntensity(image,p) < (QuantumRange/2)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 551 | byte|=0x80; |
| 552 | bit++; |
| 553 | if (bit == 8) |
| 554 | { |
| 555 | /* |
| 556 | Write a bitmap byte to the image file. |
| 557 | */ |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 558 | (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 559 | (unsigned int) (byte & 0xff)); |
| 560 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 561 | count++; |
| 562 | if (count == 12) |
| 563 | { |
| 564 | (void) CopyMagickString(buffer,"\n ",MaxTextExtent); |
| 565 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 566 | count=0; |
| 567 | }; |
| 568 | bit=0; |
| 569 | byte=0; |
| 570 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 571 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 572 | } |
| 573 | if (bit != 0) |
| 574 | { |
| 575 | /* |
| 576 | Write a bitmap byte to the image file. |
| 577 | */ |
| 578 | byte>>=(8-bit); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 579 | (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 580 | (unsigned int) (byte & 0xff)); |
| 581 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 582 | count++; |
| 583 | if (count == 12) |
| 584 | { |
| 585 | (void) CopyMagickString(buffer,"\n ",MaxTextExtent); |
| 586 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 587 | count=0; |
| 588 | }; |
| 589 | bit=0; |
| 590 | byte=0; |
| 591 | }; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 592 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 593 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 594 | if (status == MagickFalse) |
| 595 | break; |
| 596 | } |
| 597 | (void) CopyMagickString(buffer,"};\n",MaxTextExtent); |
| 598 | (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer); |
| 599 | (void) CloseBlob(image); |
| 600 | return(MagickTrue); |
| 601 | } |