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 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
|
/****************************************************************************
* targa.cpp
*
* This module contains the code to read and write the Targa output file
* format.
*
* from Persistence of Vision(tm) Ray Tracer version 3.6.
* Copyright 1991-2003 Persistence of Vision Team
* Copyright 2003-2004 Persistence of Vision Raytracer Pty. Ltd.
*---------------------------------------------------------------------------
* NOTICE: This source code file is provided so that users may experiment
* with enhancements to POV-Ray and to port the software to platforms other
* than those supported by the POV-Ray developers. There are strict rules
* regarding how you are permitted to use this file. These rules are contained
* in the distribution and derivative versions licenses which should have been
* provided with this file.
*
* These licences may be found online, linked from the end-user license
* agreement that is located at http://www.povray.org/povlegal.html
*---------------------------------------------------------------------------
* This program is based on the popular DKB raytracer version 2.12.
* DKBTrace was originally written by David K. Buck.
* DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
*---------------------------------------------------------------------------
* $File: //depot/povray/3.6-release/source/targa.cpp $
* $Revision: #3 $
* $Change: 3032 $
* $DateTime: 2004/08/02 18:43:41 $
* $Author: chrisc $
* $Log$
*****************************************************************************/
/****************************************************************************
*
* Explanation:
*
* -
*
* ---
*
* May 1994 : Support for 24-bit RLE Targa output files added: John Baily
* and David Payne.
*
* Jul 1994 : Resume trace support and minor algorithm fix (one more still
* needed, see comments in Write_Targa_Line); resume will force
* Targa format to match the original trace format -- the T or C
* format flag is adjusted as necessary: Charles Marslett,
*
* Jun 1995 : Added support for 32-bit Targa input and output files.
* The alpha channel has a value of 0 for 100% transparency
* and a value of 255 for 0% transparency. [DB]
*
*****************************************************************************/
#include "frame.h"
#include "povray.h"
#include "targa.h"
#include "optout.h"
#include "pov_util.h"
#include "povmsend.h"
#include "colour.h"
BEGIN_POV_NAMESPACE
/*****************************************************************************
* Local preprocessor defines
******************************************************************************/
/*****************************************************************************
* Local typedefs
******************************************************************************/
typedef struct pix
{
DBL blue, green, red, transm;
} pix;
/*****************************************************************************
* Local variables
******************************************************************************/
/*****************************************************************************
* Static functions
******************************************************************************/
static void convert_targa_color (IMAGE_COLOUR *, unsigned, unsigned char *);
/*****************************************************************************
*
* FUNCTION
*
* Open_Targa_File
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* Jun 1995 : Added code for 32 bit Targa files. [DB]
*
* Sept 1995: Modified header output for Targa files. [AED]
*
******************************************************************************/
Targa_Image::Targa_Image(char *name, int w, int h, int m, int l)
{
unsigned char tgaheader[18];
mode = m;
filename = name;
in_file = NULL;
out_file = NULL;
width = w ;
height = h ;
line_number = l;
switch (mode)
{
case READ_MODE:
/* We can't resume from stdout. */
if (opts.Options & TO_STDOUT || (in_file = New_Checked_IStream(name, POV_File_Image_Targa)) == NULL)
return;
/* Read targa header information. */
if (!in_file->read((char *)tgaheader, 18))
return;
/* Decipher the header information */
switch (tgaheader[2])
{
case 2 : opts.OutputFormat = 'T'; break;
case 10 : opts.OutputFormat = 'C'; break;
default : return;
}
switch (tgaheader[16])
{
case 24 : break;
case 32 : opts.Options |= OUTPUT_ALPHA; break;
default : return;
}
/* First_Column set to x offset. Bytes 8, 9 */
opts.First_Column = tgaheader[8] + (tgaheader[9] << 8);
/* First line set to y offset. Bytes 10, 11 */
opts.First_Line = line_number = tgaheader[10] + (tgaheader[11]<<8);
width = tgaheader[12] + (tgaheader[13] << 8);
height = tgaheader[14] + (tgaheader[15] << 8);
if (w != width || h != height)
Error ("Targa file dimensions do not match render resolution.");
Send_Progress("Resuming interrupted trace", PROGRESS_RESUMING_INTERRUPTED_TRACE);
break;
case WRITE_MODE:
if (opts.Options & TO_STDOUT)
{
out_file = New_OStream("stdout", POV_File_Image_Targa, false);
}
else if ((out_file = New_Checked_OStream(name, POV_File_Image_Targa, false)) == NULL)
{
return;
}
/* Output TGA file header info */
out_file->Write_Byte(0); /* Byte 0 - Length of Image ID field */
out_file->Write_Byte(0); /* Byte 1 - Color map type (0 is no color map) */
switch(opts.OutputFormat) /* Byte 2 - TGA file type */
{
case 't': /* Uncompressed True-Color Image */
case 'T':
case 's':
case 'S': out_file->Write_Byte(2); break;
case 'c': /* Run-length compressed True-Color Image */
case 'C': out_file->Write_Byte(10); break;
}
out_file->Write_Byte(0); /* Byte 3 - Index of first color map entry LSB */
out_file->Write_Byte(0); /* Byte 4 - Index of first color map entry MSB */
out_file->Write_Byte(0); /* Byte 5 - Color map length LSB */
out_file->Write_Byte(0); /* Byte 6 - Color map legth MSB */
out_file->Write_Byte(0); /* Byte 7 - Color map size */
/* x origin set to "First_Column" Bytes 8, 9 */
out_file->Write_Byte(opts.First_Column % 256);
out_file->Write_Byte(opts.First_Column / 256);
/* y origin set to "First_Line" Bytes 10, 11 */
out_file->Write_Byte(opts.First_Line % 256);
out_file->Write_Byte(opts.First_Line / 256);
/* write width and height Bytes 12 - 15 */
out_file->Write_Byte(w % 256);
out_file->Write_Byte(w / 256);
out_file->Write_Byte(h % 256);
out_file->Write_Byte(h / 256);
/* We write 24 or 32 bits/pixel (16 million colors and alpha channel)
* and also store the orientation and Alpha channel depth. Bytes 16, 17.
*/
if (opts.Options & OUTPUT_ALPHA)
{
out_file->Write_Byte(32); /* 32 bits/pixel (BGRA) */
out_file->Write_Byte(0x28); /* Data starts at top left, 8 bits Alpha */
}
else
{
out_file->Write_Byte(24); /* 24 bits/pixel (BGR) */
out_file->Write_Byte(0x20); /* Data starts at top left, 0 bits Alpha */
}
/* TGA file Image ID field data would go here (up to 255 chars) */
width = w;
height = h;
break;
case APPEND_MODE:
if (opts.Options & TO_STDOUT)
{
out_file = New_OStream("stdout", POV_File_Image_Targa, true);
}
else if ((out_file = New_Checked_OStream(name, POV_File_Image_Targa, true)) == NULL)
{
return;
}
break;
}
valid = true;
}
/*****************************************************************************
*
* FUNCTION
*
* Close_Targa_File
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
Targa_Image::~Targa_Image()
{
/* Close the input file (if open) */
if(in_file != NULL)
delete in_file;
/* Close the output file (if open) */
if(out_file != NULL)
{
out_file->flush();
delete out_file;
}
in_file = NULL;
out_file = NULL;
}
/*****************************************************************************
*
* FUNCTION
*
* Write_Targa_Pixel
*
* INPUT
*
* handle - Current file handel
* r, g, b, f - Color values to write
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* Dan Farmer
*
* DESCRIPTION :
*
* Moves redundant code to a single function. Adding 16 bit grayscale
* conditional code to each occurance of writing a pixel was getting a bit
* too wordy.
*
* CHANGES
*
* Jun 1995 : Added code for 32 bit Targa files. [DB]
*
* Sept 1995: Modified handling of Alpha channel to use only opts.Options
* Sept 1995: Modified handling of grayscale to use only opts.Options
*
******************************************************************************/
void Targa_Image::Write_Pixel(DBL b, DBL g, DBL r, DBL f)
{
unsigned int gray;
if (opts.Options & HF_GRAY_16)
{
/* Ouput heightfield in POV red/green format */
gray = (int)floor((GREY_SCALE3(r,g,b)) * 65535.0);
out_file->Write_Byte(0);
out_file->Write_Byte(gray & 0xFF);
if (!out_file->Write_Byte((gray >> 8) & 0xFF))
Error("Cannot write TGA output data to %s.",filename);
}
else
{
/* Normal 24/32 bit pixel coloring */
out_file->Write_Byte((int) floor(b * 255.0));
out_file->Write_Byte((int) floor(g * 255.0));
if (!out_file->Write_Byte((int) floor(r * 255.0)))
Error("Cannot write TGA output data to %s.",filename);
if (opts.Options & OUTPUT_ALPHA)
{
if (!out_file->Write_Byte((int) floor((1.0 - f) * 255.0)))
Error("Cannot write TGA output data to %s.",filename);
}
}
}
/*****************************************************************************
*
* FUNCTION
*
* Write_Targa_Line
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* Jun 1995 : Added code for 32 bit Targa files. [DB]
*
******************************************************************************/
void Targa_Image::Write_Line(COLOUR *line_data)
{
if(valid == false)
Error("Cannot access output image file.");
register int x;
int ptype, cnt, llen, startx;
bool writenow;
pix curpix, nexpix;
switch (opts.OutputFormat)
{
case 't':
case 'T':
case 's':
case 'S':
for (x = 0; x < width; x++)
{
Write_Pixel(line_data[x][pBLUE], line_data[x][pGREEN], line_data[x][pRED], line_data[x][pTRANSM]);
}
break;
case 'c':
case 'C':
llen = width;
startx = 0;
cnt = 1;
curpix.blue = line_data[startx][pBLUE];
curpix.green = line_data[startx][pGREEN];
curpix.red = line_data[startx][pRED];
curpix.transm = line_data[startx][pTRANSM];
ptype = 0;
writenow = false;
for (;;)
{
nexpix.blue = line_data[startx+cnt][pBLUE];
nexpix.green = line_data[startx+cnt][pGREEN];
nexpix.red = line_data[startx+cnt][pRED];
nexpix.transm = line_data[startx+cnt][pTRANSM];
if ((nexpix.red == curpix.red) && (nexpix.blue == curpix.blue) &&
(nexpix.green == curpix.green) && (nexpix.transm == curpix.transm))
{
if (ptype == 0)
{
cnt++;
if ((cnt >= 128) || ((startx + cnt) >= llen))
{
writenow = true;
}
}
else
{
cnt--;
writenow = true;
}
}
else
{
if ((ptype == 1) || (cnt <= 1))
{
ptype = 1;
curpix = nexpix;
cnt++;
if ((cnt >= 128) || ((startx + cnt) >= llen))
{
writenow = true;
}
}
else
{
writenow = true;
}
}
if (writenow)
{
/* This test SHOULD be unnecessary! However, it isn't! [CWM] */
if (startx + cnt > llen)
{
cnt = llen - startx;
}
if (ptype == 0)
{
out_file->Write_Byte((int) ((cnt - 1) | 0x80));
Write_Pixel(curpix.blue, curpix.green, curpix.red, curpix.transm);
curpix = nexpix;
}
else
{
out_file->Write_Byte((int) cnt - 1);
for (x = 0; x < cnt; x++)
{
Write_Pixel(line_data[startx+x][pBLUE], line_data[startx+x][pGREEN], line_data[startx+x][pRED], line_data[startx+x][pTRANSM]);
}
}
startx = startx + cnt;
writenow = false;
ptype = 0;
cnt = 1;
if (startx >= llen)
{
break; /* Exit while */
}
}
}
break; /* End of case */
}
out_file->flush();
}
/*****************************************************************************
*
* FUNCTION
*
* Read_Targa_Line
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* Jun 1995 : Added code for 32 bit Targa files. [DB]
*
******************************************************************************/
int Targa_Image::Read_Line(COLOUR *line_data)
{
if(valid == false)
Error("Cannot access output image file.");
int x, data, cnt;
DBL rdata, gdata, bdata, fdata;
switch (opts.OutputFormat)
{
case 't':
case 'T':
case 's':
case 'S':
for (x = 0; x < width; x++)
{
/* Read the BLUE data byte. If EOF is reached on the first
* character read, then this line hasn't been rendered yet.
* Return 0. If an EOF occurs somewhere within the line, this
* is an error - return -1.
*/
if ((data = in_file->Read_Byte ()) == EOF)
{
if (x == 0)
{
return 0;
}
else
{
return -1;
}
}
line_data[x][pBLUE] = (DBL) data / 255.0;
/* Read the GREEN data byte. */
if ((data = in_file->Read_Byte ()) == EOF)
{
return -1;
}
line_data[x][pGREEN] = (DBL) data / 255.0;
/* Read the RED data byte. */
if ((data = in_file->Read_Byte ()) == EOF)
{
return -1;
}
line_data[x][pRED] = (DBL) data / 255.0;
/* Read alpha channel. */
if (opts.Options & OUTPUT_ALPHA)
{
if ((data = in_file->Read_Byte ()) == EOF)
{
return -1;
}
line_data[x][pTRANSM] = 1.0 - (DBL)data / 255.0;
}
else
{
line_data[x][pTRANSM] = 0.0;
}
}
break;
case 'c':
case 'C':
cnt = 0;
do
{
if ((data = in_file->Read_Byte ()) == EOF)
{
if (cnt == 0)
return 0;
else
return -1;
}
if (data & 0x80)
{
x = data & 0x7F;
if ((data = in_file->Read_Byte ()) == EOF)
return -1;
bdata = (DBL) data / 255.0;
if ((data = in_file->Read_Byte ()) == EOF)
return -1;
gdata = (DBL) data / 255.0;
if ((data = in_file->Read_Byte ()) == EOF)
return -1;
rdata = (DBL) data / 255.0;
/* Read alpha channel if any. */
if (opts.Options & OUTPUT_ALPHA)
{
if ((data = in_file->Read_Byte ()) == EOF)
return -1;
fdata = 1.0 - (DBL)data / 255.0;
}
else
{
fdata = 0.0;
}
do
{
line_data[cnt][pBLUE] = bdata;
line_data[cnt][pGREEN] = gdata;
line_data[cnt][pRED] = rdata;
line_data[cnt][pTRANSM] = fdata;
cnt++;
}
while (--x != -1);
}
else
{
x = data & 0x7F;
do
{
if ((data = in_file->Read_Byte ()) == EOF)
return -1;
bdata = (DBL) data / 255.0;
if ((data = in_file->Read_Byte ()) == EOF)
return -1;
gdata = (DBL) data / 255.0;
if ((data = in_file->Read_Byte ()) == EOF)
return -1;
rdata = (DBL) data / 255.0;
/* Read alpha channel if any. */
if (opts.Options & OUTPUT_ALPHA)
{
if ((data = in_file->Read_Byte ()) == EOF)
return -1;
fdata = 1.0 - (DBL)data / 255.0;
}
else
{
fdata = 0.0;
}
line_data[cnt][pBLUE] = bdata;
line_data[cnt][pGREEN] = gdata;
line_data[cnt][pRED] = rdata;
line_data[cnt][pTRANSM] = fdata;
cnt++;
}
while (--x != -1);
}
}
while (cnt < width);
if (cnt != width)
{
return -1;
}
break;
}
line_number++;
return 1;
}
/*****************************************************************************
*
* FUNCTION
*
* convert_targa_color
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
static void convert_targa_color(IMAGE_COLOUR *tcolor, unsigned pixelsize, unsigned char *bytes)
{
switch (pixelsize)
{
case 1:
tcolor->Red = bytes[0];
tcolor->Green = bytes[0];
tcolor->Blue = bytes[0];
tcolor->Filter = 0;
tcolor->Transmit = 0;
break;
case 2:
tcolor->Red = ((bytes[1] & 0x7c) << 1);
tcolor->Green = (((bytes[1] & 0x03) << 3) | ((bytes[0] & 0xe0) >> 5)) << 3;
tcolor->Blue = (bytes[0] & 0x1f) << 3;
tcolor->Filter = 0;
tcolor->Transmit = 255 - (bytes[1] & 0x80 ? 255 : 0);
break;
case 3:
tcolor->Red = bytes[2];
tcolor->Green = bytes[1];
tcolor->Blue = bytes[0];
tcolor->Filter = 0;
tcolor->Transmit = 0;
break;
case 4:
tcolor->Red = bytes[2];
tcolor->Green = bytes[1];
tcolor->Blue = bytes[0];
tcolor->Filter = 0;
tcolor->Transmit = 255 - bytes[3];
break;
default:
Error("Bad pixelsize in TGA color.");
}
}
/*****************************************************************************
*
* FUNCTION
*
* Read_Targa_Image
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* Reads a Targa image into an RGB image buffer. Handles 8, 16, 24, 32 bit
* formats. Raw or color mapped. Simple raster and RLE compressed pixel
* encoding. Right side up or upside down orientations.
*
* CHANGES
*
* Jun 1995 : Added code for 32 bit Targa files. [DB]
*
******************************************************************************/
void Read_Targa_Image(IMAGE *Image, char *name)
{
int h;
int temp;
unsigned i, j, k;
unsigned char cflag = 0;
unsigned char *map_line = NULL;
unsigned char bytes[4];
unsigned char tgaheader[18];
unsigned char idbuf[256];
unsigned ftype, idlen, cmlen, cmsiz, psize, orien;
unsigned width, height;
IStream *filep;
IMAGE8_LINE *line_data = NULL;
IMAGE_COLOUR *cmap;
IMAGE_COLOUR pixel;
/* Start by trying to open the file */
if ((filep = Locate_File(name,POV_File_Image_Targa,NULL,true)) == NULL)
Error ("Cannot open TGA image.");
if (!filep->read((char *)tgaheader, 18))
Error ("Cannot reading header of TGA image.");
/* Decipher the header information */
idlen = tgaheader[ 0];
ftype = tgaheader[ 2];
cmlen = tgaheader[ 5] + (tgaheader[ 6] << 8);
cmsiz = tgaheader[ 7] / 8;
width = tgaheader[12] + (tgaheader[13] << 8);
height = tgaheader[14] + (tgaheader[15] << 8);
psize = tgaheader[16] / 8;
orien = tgaheader[17] & 0x20; /* Right side up ? */
Image->iwidth = width;
Image->iheight = height;
Image->width = (DBL)width;
Image->height = (DBL)height;
Image->Colour_Map_Size = cmlen;
Image->Colour_Map = NULL;
/* Determine if this is a supported Targa type */
if (ftype == 9 || ftype == 10 || ftype == 11)
{
cflag = 1;
}
else
{
if (ftype == 1 || ftype == 2 || ftype == 3)
{
cflag = 0;
}
else
{
Error("Unsupported file type %d in TGA image.", ftype);
}
}
/* Skip over the picture ID information */
if(idlen > 0)
{
if (!filep->read((char *)idbuf, idlen))
Error ("Cannot read header from TGA image.");
}
/* Read in the the color map (if any) */
if (cmlen > 0)
{
if (psize != 1)
{
Error("Unsupported color map bit depth (%d bpp) in TGA image.",
psize * 8);
}
cmap = (IMAGE_COLOUR *)POV_MALLOC(cmlen * sizeof(IMAGE_COLOUR), "TGA color map");
for (i = 0; i < cmlen; i++)
{
for (j = 0; j < cmsiz; j++)
{
if ((temp = filep->Read_Byte ()) == EOF)
{
Error("Cannot read color map from TGA image.");
}
else
{
bytes[j] = (unsigned char)temp;
}
}
convert_targa_color(&cmap[i], cmsiz, bytes);
}
Image->Colour_Map = cmap;
}
else
{
Image->Colour_Map = NULL;
}
/* Allocate the buffer for the image */
if (cmlen > 0)
{
Image->data.map_lines = (unsigned char **)POV_MALLOC(height * sizeof(unsigned char *), "TGA image");
}
else
{
Image->data.rgb8_lines = (IMAGE8_LINE *)POV_MALLOC(height * sizeof(IMAGE8_LINE), "TGA image");
}
for (i = 0; i < height; i++)
{
k = width * sizeof(unsigned char);
if (cmlen > 0)
{
map_line = (unsigned char *)POV_MALLOC(k, "TGA image line");
Image->data.map_lines[i] = map_line;
}
else
{
line_data = &Image->data.rgb8_lines[i];
line_data->red = (unsigned char *)POV_MALLOC(k, "TGA image line");
line_data->green = (unsigned char *)POV_MALLOC(k, "TGA image line");
line_data->blue = (unsigned char *)POV_MALLOC(k, "TGA image line");
if (psize > 3)
{
line_data->transm = (unsigned char *)POV_MALLOC(k, "TGA image line");
}
else
{
line_data->transm = (unsigned char *)NULL;
}
}
}
/* Read the image into the buffer */
if (cflag)
{
/* RLE compressed images */
if (cmlen > 0)
{
if (orien)
{
map_line = Image->data.map_lines[0];
}
else
{
map_line = Image->data.map_lines[height-1];
}
}
else
{
if (orien)
{
line_data = &Image->data.rgb8_lines[0];
}
else
{
line_data = &Image->data.rgb8_lines[height-1];
}
}
i = 0; /* row counter */
j = 0; /* column counter */
while (i < height)
{
/* Grab a header */
if ((h = filep->Read_Byte ()) == EOF)
{
Error("Cannot read data from TGA image.");
}
if (h & 0x80)
{
/* Repeat buffer */
h &= 0x7F;
for (k = 0; k < psize; k++)
{
if ((temp = filep->Read_Byte ()) == EOF)
{
Error("Cannot read data from TGA image.");
}
else
{
bytes[k] = (unsigned char)temp;
}
}
if (cmlen == 0)
{
convert_targa_color(&pixel, psize, bytes);
}
for (; h >= 0; h--)
{
if (cmlen > 0)
{
map_line[j] = bytes[0];
}
else
{
line_data->red[j] = (unsigned char)pixel.Red;
line_data->green[j] = (unsigned char)pixel.Green;
line_data->blue[j] = (unsigned char)pixel.Blue;
if (psize > 3)
{
line_data->transm[j] = (unsigned char)pixel.Transmit;
}
}
if (++j == width)
{
i++;
if (cmlen > 0)
{
if (orien)
{
map_line = Image->data.map_lines[i];
}
else
{
map_line = Image->data.map_lines[height-i-1];
}
}
else
{
line_data += (orien ? 1 : -1);
}
j = 0;
}
}
}
else
{
/* Copy buffer */
for (; h >= 0; h--)
{
for (k = 0; k < psize ; k++)
{
if ((temp = filep->Read_Byte ()) == EOF)
{
Error("Cannot reading data from TGA image.");
}
else
{
bytes[k] = (unsigned char)temp;
}
}
if (cmlen > 0)
{
map_line[j] = bytes[0];
}
else
{
convert_targa_color(&pixel, psize, bytes);
line_data->red[j] = (unsigned char)pixel.Red;
line_data->green[j] = (unsigned char)pixel.Green;
line_data->blue[j] = (unsigned char)pixel.Blue;
if (psize > 3)
{
line_data->transm[j] = (unsigned char)pixel.Transmit;
}
}
if (++j == width)
{
i++;
if (cmlen > 0)
{
if (orien)
{
map_line = Image->data.map_lines[i];
}
else
{
map_line = Image->data.map_lines[height-i-1];
}
}
else
{
line_data += (orien ? 1 : -1);
}
j = 0;
}
}
}
}
}
else
{
/* Simple raster image file, read in all of the pixels */
if (cmlen == 0)
{
if (orien)
{
line_data = &Image->data.rgb8_lines[0];
}
else
{
line_data = &Image->data.rgb8_lines[height-1];
}
}
for (i = 0; i < height; i++)
{
if (cmlen > 0)
{
if (orien)
{
map_line = Image->data.map_lines[i];
}
else
{
map_line = Image->data.map_lines[height-i-1];
}
}
for (j = 0; j < width; j++)
{
for (k = 0; k < psize; k++)
{
if ((temp = filep->Read_Byte ()) == EOF)
{
Error("Cannot read data from TGA image.");
}
else
{
bytes[k] = (unsigned char)temp;
}
}
if (cmlen > 0)
{
map_line[j] = bytes[0];
}
else
{
convert_targa_color(&pixel, psize, bytes);
line_data->red[j] = (unsigned char)pixel.Red;
line_data->green[j] = (unsigned char)pixel.Green;
line_data->blue[j] = (unsigned char)pixel.Blue;
if (psize > 3)
{
line_data->transm[j] = (unsigned char)pixel.Transmit;
}
}
}
if (cmlen == 0)
{
line_data += (orien ? 1 : -1);
}
}
}
/* Any data following the image is ignored. */
/* Close the image file */
delete filep;
}
END_POV_NAMESPACE
|