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 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
|
/* GEM-Images
*
* dieter fiebelkorn GEM image type
*
* dieter fiebelkorn 19.12.90
*
* jimf 10.29.93 - lots of reworking to handle different byte-orders and
* 32-bit integers. no color images to test it with...
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include "image.h"
#define TRUE 1
#define FALSE 0
#define MAX_PLANES 8
#define ALL_PLANES 8
#define LEAVE {\
errorInLoad = errno;\
printf(" Short read within image data\n");\
return;\
}
#define LEAVE_ON_ERROR(func) if ((func) <= 0) LEAVE
static int vdiColorOrder = 0;
static char *depthError = "Unsupported depth (%d) in IMG-file.\n";
static unsigned int out_depthError = 0;
int errorInLoad;
typedef struct IMGHEADER
{
byte im_version[2];
byte im_headlength[2];
byte im_nplanes[2];
byte im_patlen[2];
byte im_pixwidth[2];
byte im_pixheight[2];
byte im_scanwidth[2];
byte im_nlines[2];
} IMGHEADER;
typedef struct ximg_header
{
byte im_version[2];
byte im_headlength[2];
byte im_nplanes[2];
byte im_patlen[2];
byte im_pixwidth[2];
byte im_pixheight[2];
byte im_scanwidth[2];
byte im_nlines[2];
char x_id [4]; /* must contain "XIMG" */
byte color_model[2]; /* 0 = RGB,1 = CYM,2 = HLS, etc. */
} XIMG_HEADER;
void (*transf) ();
static int vdi2pli();
#ifdef UNUSED_FUNCTIONS
static int pli2vdi();
#endif /* UNUSED_FUNCTIONS */
static void transf0 ();
static void transf1 ();
#ifdef UNUSED_FUNCTIONS
static void transf2 ();
#endif /* UNUSED_FUNCTIONS */
static void transf3 ();
static void xread_img ();
static void xread_line ();
static int ident_img ();
static Image *load_img ();
static int ident_ximg ();
static Image *load_ximg ();
IMGHEADER header;
XIMG_HEADER xheader;
unsigned int ScanByteBreite, max_planes,
pattern_len, plane, x, y, ByteNr, height;
unsigned char *ptr, Muster[ALL_PLANES][BUFSIZ], tmp[BUFSIZ];
#ifdef UNUSED_FUNCTIONS
static unsigned char *bitplane[ALL_PLANES], *lastbitplane;
#endif /* UNUSED_FUNCTIONS */
static void transf0(bitimage, plane, value, planes)
unsigned char *bitimage;
unsigned int plane;
unsigned char value;
unsigned int planes;
{
bitimage[x] = value;
x++;
}
static void transf1(bitimage, plane, value, planes)
unsigned char *bitimage;
unsigned int plane;
unsigned char value;
unsigned int planes;
{
unsigned char bit;
unsigned long off;
bit= 0x80;
off= 0;
do
{
if (bit & value)
bitimage[off + 8*x] |= (unsigned char)0x01 << plane;
bit >>= 1;
off++;
}
while (bit > 0);
x++;
}
#ifdef UNUSED_FUNCTIONS
static void transf2(bitimage, plane, value, planes)
unsigned char *bitimage;
unsigned int plane;
unsigned char value;
unsigned int planes;
{
int i;
if (bitimage != lastbitplane)
{
lastbitplane = bitimage;
for (i = 0; i < planes; i++)
bitplane[i] += ScanByteBreite + (ScanByteBreite % 2);
}
bitplane[plane][x] = value;
x++;
}
#endif /* UNUSED_FUNCTIONS */
static void transf3(bitimage, plane, value, planes)
unsigned char *bitimage;
unsigned int plane;
unsigned char value;
unsigned int planes;
{
unsigned char bit;
unsigned long off;
bit= 0x80;
off= 0;
do
{
if (bit & value)
{
if (plane < 8)
bitimage[off + 24*x+2] |= (unsigned char)0x01 << plane;
if (plane >= 8 && plane < 16)
bitimage[off + 24*x+1] |= (unsigned char)0x01 << (plane-8);
if (plane >= 16 && plane < 24)
bitimage[off + 24*x ] |= (unsigned char)0x01 << (plane-16);
}
bit >>= 1;
off+=3;
}
while (bit > 0);
x++;
}
static void xread_line (file, planes)
ZFILE *file;
int planes;
{
ByteNr= 0;
for (plane= 0; plane < planes; plane++)
{
do
xread_img (file, TRUE, planes);
while (ByteNr < ScanByteBreite);
ByteNr= 0;
x= 0;
}
}
static void xread_img (file, linie, planes)
ZFILE *file;
unsigned int linie;
int planes;
{
unsigned int wert, i, k;
unsigned char status;
wert = zgetc(file);
switch (wert)
{
case EOF:
LEAVE;
case 0x00:
wert = zgetc(file);
switch (wert)
{
case EOF:
LEAVE;
case 0x00: /* Vertical Run */
LEAVE_ON_ERROR(zread(file, tmp, 2L));
if (tmp[0] == 0xFF && !linie)
{
wert = tmp[1];
xread_line (file, planes);
}
else
{
errorInLoad = 1;
printf(" Wrong (X)IMG format!");
return;
}
if (y + wert > height)
wert = height - y;
if (planes == 1)
{
for (k= 0; k < wert; k++)
{
for (i= 0; i < ScanByteBreite; i++)
*ptr++ = Muster[0][i];
if (ScanByteBreite % 2 == 1)
*ptr++ = 0x00;
}
ByteNr = ScanByteBreite;
if (ByteNr % 2 == 1)
ByteNr++;
x = 0;
y += (wert - 1);
}
else
{
for (k= 0; k < wert; k++)
{
int plane;
for (plane= 0; plane < planes; plane++)
{
x= 0;
for (ByteNr= 0; ByteNr < ScanByteBreite; ByteNr++)
(*transf)(ptr, plane, Muster[plane][ByteNr], planes);
}
ptr += max_planes * (ScanByteBreite + (ScanByteBreite % 2));
y++;
}
x = 0;
ptr -= max_planes * (ScanByteBreite + (ScanByteBreite % 2));
y--;
}
break;
default : /* Pattern Run */
if (!linie)
{
LEAVE_ON_ERROR(zread(file, tmp, (long)pattern_len));
if (planes == 1)
{
for (k= 0; k < wert && ByteNr < ScanByteBreite; k++)
{
for (i= 0; i < pattern_len; i++)
*ptr++ = tmp[i];
ByteNr += pattern_len;
}
}
else
{
for (k= 0; k < wert && ByteNr < ScanByteBreite; k++)
{
for (i= 0; i < pattern_len; i++)
(*transf)(ptr, plane, tmp[i], planes);
ByteNr += pattern_len;
}
}
}
else
{
LEAVE_ON_ERROR(zread(file, tmp, (long)pattern_len));
for (k= 0; k < wert && ByteNr < ScanByteBreite; k++)
for (i= 0; i < pattern_len; i++)
Muster[plane][ByteNr++] = tmp[i];
}
}
break;
case 0x80:
wert = zgetc(file);
if (wert == EOF)
LEAVE;
i = wert;
if (ByteNr + i > ScanByteBreite)
i = ScanByteBreite - ByteNr;
wert -= i;
if (!linie)
{
if (planes == 1)
{
zread(file, ptr, (long)i);
ptr += i;
ByteNr += i;
if (wert)
LEAVE_ON_ERROR(zread(file, tmp, (long)wert));
}
else
{
zread(file, tmp, (long)i);
for (k = 0; k < i; k++)
{
(*transf)(ptr, plane, tmp[k], planes);
ByteNr++;
}
if (wert)
LEAVE_ON_ERROR(zread(file, tmp, (long)wert));
}
}
else
{
zread(file, &Muster[plane][ByteNr], (long)i);
ByteNr += i;
if (wert)
LEAVE_ON_ERROR(zread(file, tmp, (long)wert));
}
break;
default :
if (wert & 0x80)
status = 0xff;
else
status = 0x00;
wert &= 0x7f;
if (!linie)
{
if (planes == 1)
for (i= 0; i < wert && ByteNr < ScanByteBreite; i++)
{
*ptr++ = status;
ByteNr++;
}
else
for (i= 0; i < wert && ByteNr < ScanByteBreite; i++)
{
(*transf)(ptr, plane, status, planes);
ByteNr++;
}
}
else
for (i= 0; i < wert && ByteNr < ScanByteBreite; i++)
Muster[plane][ByteNr++] = status;
}
}
static int ident_img(name)
char *name;
{
ZFILE *file;
long size = 0;
int w, h, nplanes;
#if 0
char extention[5];
strncpy (extention, name+strlen(name)-4, 4);
extention[4] = '\0';
if (strcmp(extention, ".IMG") != 0 && strcmp(extention, ".img") != 0)
return (0);
#endif
if ((file= zopen(name)) == NULL)
{
printf(" Error reading IMG!");
return(0);
}
size = zread(file, (byte *)&header, sizeof(header));
zclose(file);
if (size < sizeof(header) || memToVal(header.im_version, 2) > 2)
return (0);
w = memToVal(header.im_scanwidth, 2);
h = memToVal(header.im_nlines, 2);
nplanes = memToVal(header.im_nplanes, 2);
if ((nplanes > 4 && nplanes != 24) || nplanes == 0)
{
if (out_depthError == 0)
printf(depthError, header.im_nplanes);
return(0);
}
if (nplanes == 1)
printf("%s is a %dx%d monochrome IMG-file\n",name , w, h);
else
if (nplanes != 24)
printf("%s is a %dx%d IMG-file with %d colors\n",name , w, h,
(1 << nplanes));
else
printf("%s is a %dx%d %d-bit IMG-file\n",name , w, h, nplanes);
return (1);
}
static Image *load_img(name)
char *name;
{
ZFILE *file;
Image *image = NULL;
unsigned long w, h, nplanes, headlength, scanwidth;
int colors;
long i, dummy;
if ((file = zopen(name)) == NULL)
{
printf(" Error reading IMG!");
return(NULL);
}
zread(file, (byte *)&header, sizeof(header));
headlength = memToVal(header.im_headlength, 2);
for (i=0; i<(headlength-(int)(sizeof(header)/2)); i++)
zread(file, (byte *)&dummy, 2L);
w = memToVal(header.im_scanwidth, 2);
w = 16L * (w / 16L + (w % 16L ? 1 : 0));
h = memToVal(header.im_nlines, 2);
pattern_len = memToVal(header.im_patlen, 2);
max_planes = 8;
nplanes = memToVal(header.im_nplanes, 2);
scanwidth = memToVal(header.im_scanwidth, 2);
if (nplanes == 1)
{
transf = transf0;
image= newBitImage(w, h);
if (!image)
{
zclose(file);
return(image);
}
plane = 0;
ptr= image->data;
x = y = ByteNr = 0;
height = (int) h;
ScanByteBreite = (scanwidth + 7) / 8;
do
{
xread_img (file, FALSE, 1);
if (ByteNr >= ScanByteBreite)
{
if (ByteNr % 2 == 1)
*ptr++ = 0x00;
ByteNr = 0;
y++;
x = 0;
}
}
while (y < height && !errorInLoad);
}
if ((nplanes > 1 && nplanes <= 8) || nplanes == 24)
{
if (nplanes <= 8)
{
image= newRGBImage(w, h, nplanes);
if (!image)
{
zclose(file);
return(image);
}
transf = transf1;
colors = 1 << nplanes;
}
else
{
image= newTrueImage(w, h);
if (!image)
{
zclose(file);
return(image);
}
transf = transf3;
colors = 0;
max_planes = 24;
}
if (nplanes >= 2 && nplanes <= 8)
{
image->rgb.red[ 0]= 0xFF00; image->rgb.green[ 0]= 0xFF00; image->rgb.blue[ 0]= 0xFF00;
image->rgb.red[ 1]= 0xFF00; image->rgb.green[ 1]= 0x0000; image->rgb.blue[ 1]= 0x0000;
image->rgb.red[ 2]= 0x0000; image->rgb.green[ 2]= 0xFF00; image->rgb.blue[ 2]= 0x0000;
image->rgb.red[ 3]= 0x0000; image->rgb.green[ 3]= 0x0000; image->rgb.blue[ 3]= 0x0000;
}
if (nplanes >= 3 && nplanes <= 8)
{
image->rgb.red[ 3]= 0xFF00; image->rgb.green[ 3]= 0xFF00; image->rgb.blue[ 3]= 0x0000;
image->rgb.red[ 4]= 0x0000; image->rgb.green[ 4]= 0x0000; image->rgb.blue[ 4]= 0xFF00;
image->rgb.red[ 5]= 0xFF00; image->rgb.green[ 5]= 0x0000; image->rgb.blue[ 5]= 0xFF00;
image->rgb.red[ 6]= 0x0000; image->rgb.green[ 6]= 0xFF00; image->rgb.blue[ 6]= 0xFF00;
image->rgb.red[ 7]= 0x0000; image->rgb.green[ 7]= 0x0000; image->rgb.blue[ 7]= 0x0000;
}
if (nplanes >= 4 && nplanes <= 8)
{
image->rgb.red[ 7]= 0xDA00; image->rgb.green[ 7]= 0xDA00; image->rgb.blue[ 7]= 0xDA00;
image->rgb.red[ 8]= 0x6D00; image->rgb.green[ 8]= 0x6D00; image->rgb.blue[ 8]= 0x6D00;
image->rgb.red[ 9]= 0xB600; image->rgb.green[ 9]= 0x0000; image->rgb.blue[ 9]= 0x0000;
image->rgb.red[10]= 0x0000; image->rgb.green[10]= 0xB600; image->rgb.blue[10]= 0x0000;
image->rgb.red[11]= 0xB600; image->rgb.green[11]= 0xB600; image->rgb.blue[11]= 0x0000;
image->rgb.red[12]= 0x0000; image->rgb.green[12]= 0x0000; image->rgb.blue[12]= 0xB600;
image->rgb.red[13]= 0xB600; image->rgb.green[13]= 0x0000; image->rgb.blue[13]= 0xB600;
image->rgb.red[14]= 0x0000; image->rgb.green[14]= 0xB600; image->rgb.blue[14]= 0xB600;
image->rgb.red[15]= 0x0000; image->rgb.green[15]= 0x0000; image->rgb.blue[15]= 0x0000;
}
/* colors 17 to 255 undefined in this kind of IMG-File */
image->rgb.used= colors;
ptr= image->data;
x = y = ByteNr = 0;
height = (int) h;
ScanByteBreite = (scanwidth + 7) / 8;
do
{
for (plane= 0; plane < nplanes && !errorInLoad; plane++)
{
do
xread_img (file, FALSE, nplanes);
while (ByteNr < ScanByteBreite && !errorInLoad);
ByteNr = 0;
x = 0;
}
if (nplanes != 1)
ptr += max_planes * (ScanByteBreite + (ScanByteBreite % 2));
y++;
}
while (y < height && !errorInLoad);
}
if (errorInLoad > 0)
{
freeImage(image);
image = NULL;
zclose(file);
return(image);
}
image->title= dupString(name);
zclose(file);
return(image);
}
static int ident_ximg(name)
char *name;
{
ZFILE *file;
long size = 0;
int w, h, nplanes;
if ((file = zopen(name)) == NULL)
{
printf(" Error reading XIMG!");
return(0);
}
size = zread(file, (byte *)&xheader, sizeof(xheader));
zclose(file);
if (size < sizeof(xheader))
return (0);
if (strncmp(xheader.x_id, "XIMG", 4)) /* || (xheader.color_model != 0 &&
xheader.color_model != 'TC')) */
return (0);
w = memToVal(xheader.im_scanwidth, 2);
h = memToVal(xheader.im_nlines, 2);
nplanes = memToVal(xheader.im_nplanes, 2);
if ((nplanes > MAX_PLANES && nplanes != 24) || nplanes == 0)
{
printf(depthError, xheader.im_nplanes);
out_depthError = 1;
return(0);
}
printf("%s\n is a %dx%d ",name, w, h);
if (nplanes != 24)
printf("RGB XIMG-file with %d colors\n", 1<<nplanes);
else
printf("24-bit XIMG-file\n");
return (1);
}
static int vdi2pli(vdi, plimax)
int vdi, plimax;
{
static char vdi2pli[] = {0, 15, 1, 2, 4, 6, 3, 5, 7, 8, 9, 10, 12, 14, 11, 13 };
if (vdi == 1)
return(plimax-1);
if (vdi >= 16)
return(vdi);
return(vdi2pli[vdi]);
}
#ifdef UNUSED_FUNCTIONS
static int pli2vdi(pli, plimax)
int pli, plimax;
{
static char pli2vdi[] = {0, 2, 3, 6, 4, 7, 5, 8, 9, 10, 11, 14, 12, 15, 13, 1 };
if (pli == plimax-1)
return(1);
if (pli >= 16)
return(pli);
return(pli2vdi[pli]);
}
#endif /* UNUSED_FUNCTIONS */
static Image *load_ximg(name)
char *name;
{
void transferRGBMap();
ZFILE *file;
Image *image = NULL;
unsigned long w, h, nplanes, scanwidth;
int color, colors;
struct RGB_LIST
{
unsigned int red;
unsigned int green;
unsigned int blue;
} rgb_list;
if ((file = zopen(name)) == NULL)
{
printf(" Error reading XIMG!");
return(NULL);
}
zread(file, (byte *)&xheader, sizeof(xheader));
w = scanwidth = memToVal(xheader.im_scanwidth, 2);
w = 16L * (w / 16L + (w % 16L ? 1 : 0));
h = memToVal(xheader.im_nlines, 2);
nplanes = memToVal(xheader.im_nplanes, 2);
pattern_len = memToVal(xheader.im_patlen, 2);
max_planes = 8;
if (nplanes == 1)
{
image= newBitImage(w, h);
if (!image)
{
zclose(file);
return(image);
}
transf = transf0;
}
if (nplanes > 1 && nplanes <= 8)
{
image= newRGBImage(w, h, nplanes);
if (!image)
{
zclose(file);
return(image);
}
transf = transf1;
}
colors = 1 << nplanes;
if (nplanes == 24)
{
image= newTrueImage(w, h);
if (!image)
{
zclose(file);
return(image);
}
transf = transf0;
nplanes = 1;
colors = 0;
max_planes =24;
}
for (color = 0; color < colors; color++)
{
zread (file, (byte *)&rgb_list, sizeof(rgb_list));
if (vdiColorOrder)
{
image->rgb.red[vdi2pli(color, colors)] = ((unsigned int) (255 * rgb_list.red / 1000)) << 8;
image->rgb.green[vdi2pli(color, colors)] = ((unsigned int) (255 * rgb_list.green / 1000)) << 8;
image->rgb.blue[vdi2pli(color, colors)] = ((unsigned int) (255 * rgb_list.blue / 1000)) << 8;
}
else
{
image->rgb.red[color] = ((unsigned int) (255 * rgb_list.red / 1000)) << 8;
image->rgb.green[color] = ((unsigned int) (255 * rgb_list.green / 1000)) << 8;
image->rgb.blue[color] = ((unsigned int) (255 * rgb_list.blue / 1000)) << 8;
}
}
if (colors != 0)
image->rgb.used= (unsigned int) 1<<nplanes;
else
image->rgb.used= 0;
ptr= image->data;
x = y = ByteNr = 0;
height = (int) h;
if (colors != 0)
ScanByteBreite = (scanwidth + 7) / 8;
else
ScanByteBreite = 24 * ((scanwidth + 7) / 8);
do
{
for (plane= 0; plane < nplanes && !errorInLoad; plane++)
{
do
xread_img (file, FALSE, nplanes);
while (ByteNr < ScanByteBreite && !errorInLoad);
if (nplanes == 1 && ByteNr % 2 == 1)
*ptr++ = 0x00;
ByteNr = 0;
x = 0;
}
if (nplanes != 1)
ptr += max_planes * (ScanByteBreite + (ScanByteBreite % 2));
y++;
}
while (y < height && !errorInLoad);
if (errorInLoad > 0)
{
freeImage(image);
image = NULL;
zclose(file);
return(image);
}
image->title= dupString(name);
zclose(file);
return(image);
}
int imgIdent(name, name2, verbose)
char *name;
char *name2;
int verbose;
{
int identified;
if ((identified = ident_ximg(name)) != 0)
return(identified);
identified = ident_img(name);
return(identified);
}
Image *imgLoad(name, name2, verbose)
char *name;
char *name2;
int verbose;
{
errorInLoad = 0;
out_depthError = 0;
if (ident_ximg(name) != 0)
return(load_ximg(name));
if (ident_img(name) != 0)
return(load_img(name));
return(NULL);
}
|