1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
|
/*
% Copyright (C) 2003-2022 GraphicsMagick Group
% Copyright (C) 2002 ImageMagick Studio
% Copyright 1991-1999 E. I. du Pont de Nemours and Company
%
% This program is covered by multiple licenses, which are described in
% Copyright.txt. You should have received a copy of Copyright.txt with this
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% X X BBBB M M %
% X X B B MM MM %
% X BBBB M M M %
% X X B B M M %
% X X BBBB M M %
% %
% %
% Read/Write X Windows System Bitmap Format. %
% %
% %
% Software Design %
% John Cristy %
% July 1992 %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
/*
Include declarations.
*/
#include "magick/studio.h"
#include "magick/blob.h"
#include "magick/colormap.h"
#include "magick/magick.h"
#include "magick/monitor.h"
#include "magick/pixel_cache.h"
#include "magick/utility.h"
/*
Forward declarations.
*/
static unsigned int
WriteXBMImage(const ImageInfo *,Image *);
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% I s X B M %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Method IsXBM returns True if the image format type, identified by the
% magick string, is XBM.
%
% The format of the IsXBM method is:
%
% unsigned int IsXBM(const unsigned char *magick,const size_t length)
%
% A description of each parameter follows:
%
% o status: Method IsXBM returns True if the image format type is XBM.
%
% o magick: This string is generally the first few bytes of an image file
% or blob.
%
% o length: Specifies the length of the magick string.
%
%
*/
static unsigned int IsXBM(const unsigned char *magick,const size_t length)
{
if (length < 7)
return(False);
if (LocaleNCompare((char *) magick,"#define",7) == 0)
return(True);
return(False);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e a d X B M I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Method ReadXBMImage reads an X11 bitmap image file and returns it. It
% allocates the memory necessary for the new Image structure and returns a
% pointer to the new image.
%
% The format of the ReadXBMImage method is:
%
% Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image: Method ReadXBMImage returns a pointer to the image after
% reading. A null image is returned if there is a memory shortage or
% if the image cannot be read.
%
% o image_info: Specifies a pointer to a ImageInfo structure.
%
% o exception: return any errors or warnings in this structure.
%
%
*/
static int XBMInteger(Image *image,const unsigned int max_digits, short int *hex_digits)
{
unsigned int
digits;
int
c,
value;
/*
Read hex value in form 0xhh or 0xhhhh from text which may look
like ", 0x7f". FIXME: This implementation is non-validating.
*/
value=0;
digits=0U;
for ( digits=0U; digits <= max_digits+1; )
{
c=ReadBlobByte(image);
if (c == EOF)
{
value=(-1);
break;
}
c&=0xff;
if (isxdigit(c))
{
value=(value << 4)+hex_digits[c];
digits++;
continue;
}
if ((hex_digits[c]) < 0 && digits)
break;
}
/* The '0' in '0x' is currently counted as a digit */
if (digits > max_digits+1)
value=(-1);
return(value);
}
static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
{
char
buffer[MaxTextExtent],
name[MaxTextExtent];
Image
*image;
register IndexPacket
*indexes;
register size_t
bytes_per_line,
i;
unsigned long
x,
y;
long
columns_signed,
rows_signed;
register PixelPacket
*q;
register unsigned char
*p;
unsigned char
*data;
unsigned int
bit,
byte,
padding,
version;
int
value;
short int
hex_digits[256];
MagickPassFail
status;
/*
Open image file.
*/
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickSignature);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
image=AllocateImage(image_info);
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
if (status == False)
ThrowReaderException(FileOpenError,UnableToOpenFile,image);
/*
Read X bitmap header.
*/
(void) memset(buffer,0,sizeof(buffer));
name[0]='\0';
columns_signed=0;
rows_signed=0;
while (ReadBlobString(image,buffer) != (char *) NULL)
if (sscanf(buffer,"#define %s %ld",name,&columns_signed) == 2)
if ((strlen(name) >= 6) &&
(LocaleCompare(name+strlen(name)-6,"_width") == 0))
break;
while (ReadBlobString(image,buffer) != (char *) NULL)
if (sscanf(buffer,"#define %s %ld",name,&rows_signed) == 2)
if ((strlen(name) >= 7) &&
(LocaleCompare(name+strlen(name)-7,"_height") == 0))
break;
if (EOFBlob(image))
ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
if ((columns_signed <= 0) || (rows_signed <= 0))
ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
image->columns=(unsigned long) columns_signed;
image->rows=(unsigned long) rows_signed;
image->depth=8;
image->storage_class=PseudoClass;
image->colors=2;
/*
Scan until hex digits.
*/
version=11;
while (ReadBlobString(image,buffer) != (char *) NULL)
{
if (sscanf(buffer,"static short %s = {",name) == 1)
version=10;
else
if (sscanf(buffer,"static unsigned char %s = {",name) == 1)
version=11;
else
if (sscanf(buffer,"static char %s = {",name) == 1)
version=11;
else
continue;
p=(unsigned char *) strrchr(name,'_');
if (p == (unsigned char *) NULL)
p=(unsigned char *) name;
else
p++;
if (LocaleCompare("bits[]",(char *) p) == 0)
break;
}
if (EOFBlob(image))
ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
/*
Initialize image structure.
*/
if (!AllocateImageColormap(image,image->colors))
ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
/*
Initialize colormap.
*/
image->colormap[0].red=MaxRGB;
image->colormap[0].green=MaxRGB;
image->colormap[0].blue=MaxRGB;
image->colormap[1].red=0;
image->colormap[1].green=0;
image->colormap[1].blue=0;
if (image_info->ping)
{
CloseBlob(image);
StopTimer(&image->timer);
return(image);
}
if (CheckImagePixelLimits(image, exception) != MagickPass)
ThrowReaderException(ResourceLimitError,ImagePixelLimitExceeded,image);
/*
Allocate temporary storage for X bitmap image
*/
padding=0;
if ((image->columns % 16) && ((image->columns % 16) < 9) && (version == 10))
padding=1;
bytes_per_line=((size_t) image->columns+7)/8+padding;
data=MagickAllocateResourceLimitedArray(unsigned char *,image->rows,bytes_per_line);
if (data == (unsigned char *) NULL)
ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
/*
Initialize hex values.
*/
for (i = 0; i < sizeof(hex_digits)/sizeof(hex_digits[0]); i++)
hex_digits[i]=(-1);
hex_digits['0']=0;
hex_digits['1']=1;
hex_digits['2']=2;
hex_digits['3']=3;
hex_digits['4']=4;
hex_digits['5']=5;
hex_digits['6']=6;
hex_digits['7']=7;
hex_digits['8']=8;
hex_digits['9']=9;
hex_digits['A']=10;
hex_digits['B']=11;
hex_digits['C']=12;
hex_digits['D']=13;
hex_digits['E']=14;
hex_digits['F']=15;
hex_digits['a']=10;
hex_digits['b']=11;
hex_digits['c']=12;
hex_digits['d']=13;
hex_digits['e']=14;
hex_digits['f']=15;
hex_digits['x']=0;
hex_digits[' ']=(-1);
hex_digits[',']=(-1);
hex_digits['}']=(-1);
hex_digits['\n']=(-1);
hex_digits['\t']=(-1);
/*
Read hex image data.
*/
p=data;
if (version == 10)
for (i=0; i < (bytes_per_line*image->rows); (i+=2))
{
value=XBMInteger(image,4,hex_digits);
if (value < 0)
{
MagickFreeResourceLimitedMemory(data);
ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
}
*p++=(unsigned char) value;
if (!padding || ((i+2) % bytes_per_line))
*p++=(unsigned char) (value >> 8);
}
else
for (i=0; i < (bytes_per_line*image->rows); i++)
{
value=XBMInteger(image,2,hex_digits);
if (value < 0)
{
MagickFreeResourceLimitedMemory(data);
ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
}
*p++=(unsigned char) value;
}
/*
Convert X bitmap image to pixel packets.
*/
p=data;
for (y=0; y < image->rows; y++)
{
q=SetImagePixels(image,0,y,image->columns,1);
if (q == (PixelPacket *) NULL)
break;
indexes=AccessMutableIndexes(image);
bit=0U;
byte=0U;
for (x=0; x < image->columns; x++)
{
if (bit == 0U)
byte=(*p++);
indexes[x]=byte & 0x01 ? 0x01 : 0x00;
bit++;
byte>>=1U;
if (bit == 8U)
bit=0U;
}
if (!SyncImagePixels(image))
break;
if (QuantumTick(y,image->rows))
if (!MagickMonitorFormatted(y,image->rows,exception,
LoadImageText,image->filename,
image->columns,image->rows))
break;
}
MagickFreeResourceLimitedMemory(data);
(void) SyncImage(image);
CloseBlob(image);
StopTimer(&image->timer);
return(image);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e g i s t e r X B M I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Method RegisterXBMImage adds attributes for the XBM image format to
% the list of supported formats. The attributes include the image format
% tag, a method to read and/or write the format, whether the format
% supports the saving of more than one frame to the same file or blob,
% whether the format supports native in-memory I/O, and a brief
% description of the format.
%
% The format of the RegisterXBMImage method is:
%
% RegisterXBMImage(void)
%
*/
ModuleExport void RegisterXBMImage(void)
{
MagickInfo
*entry;
entry=SetMagickInfo("XBM");
entry->decoder=(DecoderHandler) ReadXBMImage;
entry->encoder=(EncoderHandler) WriteXBMImage;
entry->magick=(MagickHandler) IsXBM;
entry->adjoin=False;
entry->description="X Windows system bitmap (black/white)";
entry->module="XBM";
(void) RegisterMagickInfo(entry);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% U n r e g i s t e r X B M I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Method UnregisterXBMImage removes format registrations made by the
% XBM module from the list of supported formats.
%
% The format of the UnregisterXBMImage method is:
%
% UnregisterXBMImage(void)
%
*/
ModuleExport void UnregisterXBMImage(void)
{
(void) UnregisterMagickInfo("XBM");
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% W r i t e X B M I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Procedure WriteXBMImage writes an image to a file in the X bitmap format.
%
% The format of the WriteXBMImage method is:
%
% unsigned int WriteXBMImage(const ImageInfo *image_info,Image *image)
%
% A description of each parameter follows.
%
% o status: Method WriteXBMImage return True if the image is written.
% False is returned is there is a memory shortage or if the image file
% fails to write.
%
% o image_info: Specifies a pointer to a ImageInfo structure.
%
% o image: A pointer to an Image structure.
%
%
*/
static unsigned int WriteXBMImage(const ImageInfo *image_info,Image *image)
{
char
basename[MaxTextExtent],
buffer[MaxTextExtent];
int
y;
register const PixelPacket
*p;
register long
x;
register const IndexPacket
*indexes;
unsigned char
bit,
byte,
polarity;
unsigned int
status;
unsigned long
count;
/*
Open output image file.
*/
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickSignature);
assert(image != (Image *) NULL);
assert(image->signature == MagickSignature);
status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
if (status == False)
ThrowWriterException(FileOpenError,UnableToOpenFile,image);
(void) TransformColorspace(image,RGBColorspace);
/*
Write X bitmap header.
*/
GetPathComponent(image->filename,BasePath,basename);
FormatString(buffer,"#define %.1024s_width %lu\n",basename,image->columns);
(void) WriteBlob(image,strlen(buffer),buffer);
FormatString(buffer,"#define %.1024s_height %lu\n",basename,image->rows);
(void) WriteBlob(image,strlen(buffer),buffer);
FormatString(buffer,"static char %.1024s_bits[] = {\n",basename);
(void) WriteBlob(image,strlen(buffer),buffer);
(void) strlcpy(buffer," ",sizeof(buffer));
(void) WriteBlob(image,strlen(buffer),buffer);
/*
Convert MIFF to X bitmap pixels.
*/
(void) SetImageType(image,BilevelType);
polarity=(PixelIntensityToQuantum(&image->colormap[0]) < (MaxRGB/2));
if (image->colors == 2)
polarity=(PixelIntensityToQuantum(&image->colormap[0]) <
PixelIntensityToQuantum(&image->colormap[1]));
bit=0;
byte=0;
count=0;
x=0;
y=0;
(void) strlcpy(buffer," ",sizeof(buffer));
(void) WriteBlob(image,strlen(buffer),buffer);
for (y=0; y < (long) image->rows; y++)
{
p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
if (p == (const PixelPacket *) NULL)
break;
indexes=AccessImmutableIndexes(image);
for (x=0; x < (long) image->columns; x++)
{
byte>>=1;
if (indexes[x] != polarity)
byte|=0x80;
bit++;
if (bit == 8)
{
/*
Write a bitmap byte to the image file.
*/
FormatString(buffer,"0x%02X, ",(unsigned int) (byte & 0xff));
(void) WriteBlob(image,strlen(buffer),buffer);
count++;
if (count == 12)
{
(void) strlcpy(buffer,"\n ",sizeof(buffer));
(void) WriteBlob(image,strlen(buffer),buffer);
count=0;
};
bit=0;
byte=0;
}
p++;
}
if (bit != 0)
{
/*
Write a bitmap byte to the image file.
*/
byte>>=(8-bit);
FormatString(buffer,"0x%02X, ",(unsigned int) (byte & 0xff));
(void) WriteBlob(image,strlen(buffer),buffer);
count++;
if (count == 12)
{
(void) strlcpy(buffer,"\n ",sizeof(buffer));
(void) WriteBlob(image,strlen(buffer),buffer);
count=0;
};
bit=0;
byte=0;
};
if (QuantumTick(y,image->rows))
if (!MagickMonitorFormatted(y,image->rows,&image->exception,
SaveImageText,image->filename,
image->columns,image->rows))
break;
}
(void) strlcpy(buffer,"};\n",sizeof(buffer));
(void) WriteBlob(image,strlen(buffer),buffer);
status &= CloseBlob(image);
return(status);
}
|