5751029 [rkeene@sledge /home/rkeene/devel/old/stereograph-0.16]$ cat -n gfxio.c
  1 /* Stereograph 0.16, 12/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 #include "globals.h"
 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         if(verbose) printf("FAILED;\n"); else fprintf(stderr, "loading gfx data...FAILED;\n");
 37         fprintf(stderr, "cannot open file '%s'!\n", file_name);
 38         return -1;
 39     } else {
 40         a = Read_TARGA(ifile, gfx);
 41     }
 42     fclose(ifile);
 43     ifile = NULL;
 44     return a;
 45 }
 46 
 47 
 48 int Write_Gfx_File (char *file_name, struct GFX_DATA *gfx)
 49 {
 50     int a;
 51     FILE *ofile = NULL;
 52 
 53     if(verbose) printf("saving '%s' (%i*%i)...", file_name, gfx->Width, gfx->Height);
 54     if(!file_name)
 55         return Write_PIPE(stdout, gfx);
 56     else {
 57         ofile = fopen(file_name, "w+");
 58 
 59         if(ofile == NULL) {
 60             if(verbose) printf("FAILED;\n"); else fprintf(stderr, "writing gfx data...FAILED;\n");
 61             fprintf(stderr, "cannot create file %s!\n", file_name);
 62             return -1;
 63         } else
 64             a = Write_TARGA(ofile, gfx);
 65         fclose(ofile);
 66         ofile = NULL;
 67         return a;
 68     }
 69 }
 70 
 71 
 72 int Read_TARGA (FILE *ifile, struct GFX_DATA *gfx)
 73 {
 74     /* TARGA uses the Intel format for integer coding (low byte first) */
 75 
 76     unsigned char header[20];
 77     int x, y, *palette, c, l, s, z;
 78 
 79     /* reading all header information */
 80     if(fread(header, sizeof(char), 18, ifile) != 18) {
 81         if(verbose) printf("FAILED!\n"); else fprintf(stderr, "reading gfx data...FAILED;\n");
 82         fprintf(stderr, "cannot read header! (file corrupt?)\n");
 83         return -2;
 84     }
 85 
 86     /* general measurements */
 87     gfx->Width = (int)header[12] + (int)header[13]*256;
 88     gfx->Height = (int)header[14] + (int)header[15]*256;
 89     gfx->Data = (int*)malloc(gfx->Width*gfx->Height*sizeof(int));
 90     if(!gfx->Data) {
 91         if(verbose) printf("FAILED;\n"); else fprintf(stderr, "reading gfx data...FAILED;\n");
 92         fprintf(stderr, "error allocating memory for image dimensions of %i * %i pixels in 32 bit\n", gfx->Width,
	gfx->Height);
 93         fprintf(stderr, "debug - header follows:\n");
 94         for(x = 0; x < 20; x += 4)
 95             fprintf(stderr, " %i, %i, %i, %i;\n", (int)header[x], (int)header[x + 1], (int)header[x + 2], (int)header[x
	+ 3]);
 96         return -3;
 97     }
 98 
 99     /* reading image identification field */
100     for(x = 0; x < header[0]; x++)
101         getc(ifile);
102 
103     /* reading palette data */
104     if((header[1] != 0) && ((header[5] + header[6]*256) > 0) && (((header[3] + header[4]*256) + (header[5] +
	header[6]*256)) <= 256)) {
105         palette = (int*)malloc(((header[3] + header[4]*256) + (header[5] + header[6]*256)) * sizeof(int));
106         for(x = header[3] + header[4]*256; (x < (header[5] + header[6]*256)); x++)
107             Read_TARGA_RGB(ifile, (int)header[7], NULL, palette + x);
108     } else
109         palette = NULL;
110 
111     if(((header[2] == 2) && (header[16] >= 16)) || (((header[2] == 1) || header[2] == 3) && (header[16] == 8))) {
112     /* type 1: 8 bit/palette, uncompressed; type 2: 16 bit++, uncompressed; type 3: 8 bit monocrome, uncompressed; */
113         if(header[17] & 32) {
114             for(y = 0; y < gfx->Height; y++) {
115                 if(header[17] & 16)
116                     for(x = gfx->Width - 1; x >= 0; x--)
117                         Read_TARGA_RGB(ifile, (int)header[16], palette, gfx->Data + (y * gfx->Width) + x);
118                 else
119                     for(x = 0; x < gfx->Width; x++)
120                         Read_TARGA_RGB(ifile, (int)header[16], palette, gfx->Data + (y * gfx->Width) + x);
121             }
122         } else {
123             for(y = gfx->Height - 1; y >= 0; y--) {
124                 if(header[17] & 16)
125                     for(x = gfx->Width - 1; x >= 0; x--)
126                         Read_TARGA_RGB(ifile, (int)header[16], palette, gfx->Data + (y * gfx->Width) + x);
127                 else
128                     for(x = 0; x < gfx->Width; x++)
129                         Read_TARGA_RGB(ifile, (int)header[16], palette, gfx->Data + (y * gfx->Width) + x);
130             }
131         }
132         if(verbose) printf("gfx data read (%i*%i);\n", gfx->Width, gfx->Height);
133         free(palette);
134         return 0;
135     } else if(((header[2] == 10) && (header[16] >= 16)) || (((header[2] == 9) || header[2] == 11) && (header[16] == 8)))
	{
136     /* type 9: 8 bit/palette RLE; type 10: 16 bit++, RLE; type 11: 8 bit monocrome, RLE; */
137         for(z = 0; ((l = getc(ifile)) != EOF) && (z < (gfx->Height * gfx->Width)); ) {
138             if(l & 128) { /* compressed data follows */
139                 l &= 127; /* extracting length */
140                 Read_TARGA_RGB(ifile, (int)header[16], palette, &c);
141                 for(s = 0; (s <= l); s++) {
142                     x = z % gfx->Width;
143                     y = (z - x) / gfx->Width;
144                     if(header[17] & 16)
145                         x = gfx->Width - 1 - x;
146                     if(!(header[17] & 32))
147                         y = gfx->Height - 1 - y;
148                     if((x < gfx->Width) && (y < gfx->Height) && (x >= 0) && (y >= 0))
149                         gfx->Data[(y * gfx->Width) + x] = c;
150                     else
151                         fprintf(stderr, "(%i, %i) => error\n", x, y);
152                     z++;
153                 }
154             } else { /* uncompressed data follows */
155                 for(s = 0; (s <= l); s++) {
156                     x = z % gfx->Width;
157                     y = (z - x) / gfx->Width;
158                     if(header[17] & 16)
159                         x = gfx->Width - 1 - x;
160                     if(!(header[17] & 32))
161                         y = gfx->Height - 1 - y;
162                     if((x < gfx->Width) && (y < gfx->Height) && (x >= 0) && (y >= 0))
163                         Read_TARGA_RGB(ifile, (int)header[16], palette, gfx->Data + (y * gfx->Width) + x);
164                     else
165                         fprintf(stderr, "(%i, %i) => error\n", x, y);
166                     z++;
167                 }
168             }
169         }
170         if(verbose) printf("gfx data read (%i*%i);\n", gfx->Width, gfx->Height);
171         free(palette);
172         return 0;
173     } else {
174         if(verbose) printf("FAILED;\n"); else fprintf(stderr, "reading gfx data...FAILED;\n");
175         fprintf(stderr, "Unsupported Targa Data Format %i@%i;\nOnly TARGA types 1, 2, 3 (uncompressed) and 9, 10, 11
	(RLE) are supported;\n", header[2], header[16]);
176         free(palette);
177         return -1;
178     }
179 }
180 
181 
182 int Read_TARGA_RGB(FILE *ifile, int bits, int *palette, int *c) {
183     static int a, r, g, b, z1, z2;
184     a = 0;
185     if(bits == 8) {
186         if(palette)
187             (*c) = palette[(int)getc(ifile)];
188         else
189             (*c) = (int)getc(ifile) * 65793;  /* 65793 = (1+256+65536) */
190     } else if(bits == 16) {
191             z1 = getc(ifile);
192             z2 = getc(ifile);
193             r = (int)((255.0/31.0) * (float)((z1 & 124) >> 2) );  /* 124 = 64 + 32 + 16 + 8 + 4 */
194             g = (int)((255.0/31.0) * (float)(((z1 & 3) << 3) | ((z2 & 224) >> 5)) );  /* 224 = 128 + 64 + 32 */
195             b = (int)((255.0/31.0) * (float)(z2 & 31) );
196             (*c) = r + (g << 8) + (b << 16);
197             a = z1 & 128;
198         } else {
199             r = getc(ifile);
200             g = getc(ifile);
201             b = getc(ifile);
202             (*c) = r + (g << 8) + (b << 16);
203             if(bits == 32)
204                 a = getc(ifile);
205         }
206     return a; /* attribute (alpha/transparency information, 32 bit only) */
207 }
208 
209 /* writes all gfx data to stdout, preceded by the resolution of gfx data - everything 32 bit wide */
210 int Write_PIPE (FILE *pp, struct GFX_DATA *gfx)
211 {
212     fwrite(&(gfx->Width), sizeof(int), 1, pp);
213     fwrite(&(gfx->Height), sizeof(int), 1, pp);
214     fwrite(gfx->Data, sizeof(int), (gfx->Width) * (gfx->Height), pp);
215     return 0;
216 }
217 
218 int Write_TARGA (FILE *ofile, struct GFX_DATA *gfx)
219 {
220     int a, x, y;
221     /* standard TARGA header consists of 18 bytes */
222     /* id tag length */
223     putc(strlen("created by stereograph"), ofile);
224     /* color pallette, yes/no */
225     putc(0, ofile);
226     /* TARGA type 2: RGB 24 bit, no pallette */
227     putc(2, ofile);
228     /* 5 following bytes contain only pallette information */
229     putc(0, ofile);
230     putc(0, ofile);
231     putc(0, ofile);
232     putc(0, ofile);
233     putc(0, ofile);
234     /* x offset */
235     putc(0, ofile);
236     putc(0, ofile);
237     /* y offset */
238     putc(0, ofile);
239     putc(0, ofile);
240     /* width, low byte first */
241     putc(gfx->Width & 255, ofile);
242     putc((gfx->Width >> 8) & 255, ofile);
243     /* height */
244     putc(gfx->Height & 255, ofile);
245     putc((gfx->Height >> 8) & 255, ofile);
246     /* bits per pixel */
247     putc(32-8, ofile);
248     /* Image Descriptor Flags */
249     putc(0, ofile);
250     fwrite("created by stereograph", sizeof(char), strlen("created by stereograph"), ofile);
251     /* data follows */
252     a = 0;
253     for(y = gfx->Height - 1; (y >= 0) && (a != EOF); y--)
254         for(x = 0; (x < gfx->Width) && (a != EOF); x++) {
255             putc(gfx->Data[y * gfx->Width + x] & 255, ofile);
256             putc((gfx->Data[y * gfx->Width + x] >> 8) & 255, ofile);
257             a = putc((gfx->Data[y * gfx->Width + x] >> 16) & 255, ofile);
258         }
259     if(a != EOF) {
260         if(verbose) printf("completed;\n");
261         return 0;
262     } else {
263         if(verbose) printf("FAILED;\n"); else fprintf(stderr, "writing gfx data...FAILED;\n");
264             fprintf(stderr, "cannot write to file! (disk full?)\n");
265         return -1;
266     }
267 }

gfxio.c handles graphics I/O for the program.
5751030 [rkeene@sledge /home/rkeene/devel/old/stereograph-0.16]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2000-04-12 11:46:17