1 /* Stereograph 0.15, 10/04/2000; 2 * Graphics I/O functions; 3 * Copyright (c) 2000 by Fabian Januszewski <fabian.linux@januszewski.de> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 */ 19 20 21 #include <stdio.h> 22 #include <stdlib.h> 23 24 #include "renderer.h" 25 #include "gfxio.h" 26 27 28 int Read_Gfx_File (char *file_name, struct GFX_DATA *gfx) 29 { 30 int a; 31 FILE *ifile = NULL; 32 33 ifile = fopen(file_name, "r"); 34 35 if(ifile == NULL) { 36 printf("FAILED;\ncannot open file '%s'!\n", file_name); 37 return -1; 38 } else { 39 a = Read_TARGA(ifile, gfx); 40 } 41 fclose(ifile); 42 ifile = NULL; 43 return a; 44 } 45 46 47 int Read_TARGA (FILE *ifile, struct GFX_DATA *gfx) 48 { 49 /* TARGA uses the Intel format for integer coding (low byte first) */ 50 51 unsigned char header[20]; 52 int x, y, *palette, c, l, s, z; 53 54 /* reading all header information */ 55 if(fread(header, sizeof(char), 18, ifile) != 18) { 56 printf("FAILED!\ncannot read file.\n"); 57 return -2; 58 } 59 60 /* general measurements */ 61 gfx->Width = (int)header[12] + (int)header[13]*256; 62 gfx->Height = (int)header[14] + (int)header[15]*256; 63 gfx->Data = (int*)malloc(gfx->Width*gfx->Height*sizeof(int)); 64 if(!gfx->Data) { 65 printf("FAILED;\nerror allocating memory for image dimensions of %i * %i pixels in 32 bit\n", gfx->Width, gfx->Height); 66 printf("debug - header follows:\n"); 67 for(x = 0; x < 20; x += 4) 68 printf(" %i, %i, %i, %i;\n", (int)header[x], (int)header[x + 1], (int)header[x + 2], (int)header[x + 3]); 69 return -3; 70 } 71 72 /* reading image identification field */ 73 for(x = 0; x < header[0]; x++) 74 getc(ifile); 75 76 /* reading palette data */ 77 if((header[1] != 0) && ((header[5] + header[6]*256) > 0) && (((header[3] + header[4]*256) + (header[5] + header[6]*256)) <= 256)) { 78 palette = (int*)malloc(((header[3] + header[4]*256) + (header[5] + header[6]*256)) * sizeof(int)); 79 for(x = header[3] + header[4]*256; (x < (header[5] + header[6]*256)); x++) 80 Read_TARGA_RGB(ifile, (int)header[7], NULL, palette + x); 81 } else 82 palette = NULL; 83 84 if(((header[2] == 2) && (header[16] >= 16)) || (((header[2] == 1) || header[2] == 3) && (header[16] == 8))) { 85 /* type 1: 8 bit/palette, uncompressed; type 2: 16 bit++, uncompressed; type 3: 8 bit monocrome, uncompressed; */ 86 if(header[17] & 32) { 87 for(y = 0; y < gfx->Height; y++) { 88 if(header[17] & 16) 89 for(x = gfx->Width - 1; x >= 0; x--) 90 Read_TARGA_RGB(ifile, (int)header[16], palette, gfx->Data + (y * gfx->Width) + x); 91 else 92 for(x = 0; x < gfx->Width; x++) 93 Read_TARGA_RGB(ifile, (int)header[16], palette, gfx->Data + (y * gfx->Width) + x); 94 } 95 } else { 96 for(y = gfx->Height - 1; y >= 0; y--) { 97 if(header[17] & 16) 98 for(x = gfx->Width - 1; x >= 0; x--) 99 Read_TARGA_RGB(ifile, (int)header[16], palette, gfx->Data + (y * gfx->Width) + x); 100 else 101 for(x = 0; x < gfx->Width; x++) 102 Read_TARGA_RGB(ifile, (int)header[16], palette, gfx->Data + (y * gfx->Width) + x); 103 } 104 } 105 printf("gfx data read (%i*%i);\n", gfx->Width, gfx->Height); 106 free(palette); 107 return 0; 108 } else if(((header[2] == 10) && (header[16] >= 16)) || (((header[2] == 9) || header[2] == 11) && (header[16] == 8))) { 109 /* type 9: 8 bit/palette RLE; type 10: 16 bit++, RLE; type 11: 8 bit monocrome, RLE; */ 110 for(z = 0; ((l = getc(ifile)) != EOF) && (z < (gfx->Height * gfx->Width)); ) { 111 if(l & 128) { /* compressed data follows */ 112 l &= 127; /* extracting length */ 113 Read_TARGA_RGB(ifile, (int)header[16], palette, &c); 114 for(s = 0; (s <= l); s++) { 115 x = z % gfx->Width; 116 y = (z - x) / gfx->Width; 117 if(header[17] & 16) 118 x = gfx->Width - 1 - x; 119 if(!(header[17] & 32)) 120 y = gfx->Height - 1 - y; 121 if((x < gfx->Width) && (y < gfx->Height) && (x >= 0) && (y >= 0)) 122 gfx->Data[(y * gfx->Width) + x] = c; 123 else 124 printf("(%i, %i) => error\n", x, y); 125 z++; 126 } 127 } else { /* uncompressed data follows */ 128 for(s = 0; (s <= l); s++) { 129 x = z % gfx->Width; 130 y = (z - x) / gfx->Width; 131 if(header[17] & 16) 132 x = gfx->Width - 1 - x; 133 if(!(header[17] & 32)) 134 y = gfx->Height - 1 - y; 135 if((x < gfx->Width) && (y < gfx->Height) && (x >= 0) && (y >= 0)) 136 Read_TARGA_RGB(ifile, (int)header[16], palette, gfx->Data + (y * gfx->Width) + x); 137 else 138 printf("(%i, %i) => error\n", x, y); 139 z++; 140 } 141 } 142 } 143 printf("gfx data read (%i*%i);\n", gfx->Width, gfx->Height); 144 free(palette); 145 return 0; 146 } else { 147 printf("FAILED;\nUnsupported Targa Data Format %i@%i;\nOnly TARGA types 1, 2, 3 (uncompressed) and 9, 10, 11 (RLE) are supported;\n", header[2], header[16]); 148 free(palette); 149 return -1; 150 } 151 } 152 153 154 int Read_TARGA_RGB(FILE *ifile, int bits, int *palette, int *c) { 155 static int a, r, g, b, z1, z2; 156 a = 0; 157 if(bits == 8) { 158 if(palette) 159 (*c) = palette[(int)getc(ifile)]; 160 else 161 (*c) = (int)getc(ifile) * 65793; /* 65793 = (1+256+65536) */ 162 } else if(bits == 16) { 163 z1 = getc(ifile); 164 z2 = getc(ifile); 165 r = (int)((255.0/31.0) * (float)((z1 & 124) >> 2) ); /* 124 = 64 + 32 + 16 + 8 + 4 */ 166 g = (int)((255.0/31.0) * (float)(((z1 & 3) << 3) | ((z2 & 224) >> 5)) ); /* 224 = 128 + 64 + 32 */ 167 b = (int)((255.0/31.0) * (float)(z2 & 31) ); 168 (*c) = r + (g << 8) + (b << 16); 169 a = z1 & 128; 170 } else { 171 r = getc(ifile); 172 g = getc(ifile); 173 b = getc(ifile); 174 (*c) = r + (g << 8) + (b << 16); 175 if(bits == 32) 176 a = getc(ifile); 177 } 178 return a; /* attribute (alpha/transparency information, 32 bit only) */ 179 } 180 181 int Write_TARGA (char *file_name, struct GFX_DATA *gfx) 182 { 183 FILE *ofile = NULL; 184 int a, x, y; 185 186 ofile = fopen(file_name, "w+"); 187 188 if(ofile == NULL) { 189 printf("FAILED;\ncannot create file %s!\n", file_name); 190 return -1; 191 } else { 192 printf("saving '%s' (%i*%i)...", file_name, gfx->Width, gfx->Height); 193 /* standard TARGA header consists of 18 bytes */ 194 /* id tag length */ 195 putc(strlen("created by stereograph"), ofile); 196 /* color pallette, yes/no */ 197 putc(0, ofile); 198 /* TARGA type 2: RGB 24 bit, no pallette */ 199 putc(2, ofile); 200 /* 5 following bytes contain only pallette information */ 201 putc(0, ofile); 202 putc(0, ofile); 203 putc(0, ofile); 204 putc(0, ofile); 205 putc(0, ofile); 206 /* x offset */ 207 putc(0, ofile); 208 putc(0, ofile); 209 /* y offset */ 210 putc(0, ofile); 211 putc(0, ofile); 212 /* width, low byte first */ 213 putc(gfx->Width & 255, ofile); 214 putc((gfx->Width >> 8) & 255, ofile); 215 /* height */ 216 putc(gfx->Height & 255, ofile); 217 putc((gfx->Height >> 8) & 255, ofile); 218 /* bits per pixel */ 219 putc(32-8, ofile); 220 /* Image Descriptor Flags */ 221 putc(0, ofile); 222 fwrite("created by stereograph", sizeof(char), strlen("created by stereograph"), ofile); 223 /* data follows */ 224 a = 0; 225 for(y = gfx->Height - 1; (y >= 0) && (a != EOF); y--) 226 for(x = 0; (x < gfx->Width) && (a != EOF); x++) { 227 putc(gfx->Data[y * gfx->Width + x] & 255, ofile); 228 putc((gfx->Data[y * gfx->Width + x] >> 8) & 255, ofile); 229 a = putc((gfx->Data[y * gfx->Width + x] >> 16) & 255, ofile); 230 } 231 fclose(ofile); 232 if(a != EOF) { 233 printf("completed;\n"); 234 return 0; 235 } else { 236 printf("FAILED;\n"); 237 return -1; 238 } 239 240 } 241 } gfxio.c handles graphics I/O for the program. |