16 #define TACHYON_INTERNAL 1 29 int createtgafile(
char *name,
unsigned short width,
unsigned short height) {
33 filesize = 3*width*height + 18 - 10;
38 ofp=fopen(name,
"w+b");
41 sprintf(msgtxt,
"Cannot create %s for output!", name);
59 fputc((width & 0xff), ofp);
60 fputc(((width >> 8) & 0xff), ofp);
61 fputc((height & 0xff), ofp);
62 fputc(((height >> 8) & 0xff), ofp);
66 fseek(ofp, filesize, 0);
67 fprintf(ofp,
"9876543210");
79 tga->
ofp=fopen(filename,
"r+b");
80 if (tga->
ofp == NULL) {
82 sprintf(msgtxt,
"Cannot open %s for output!", filename);
88 fseek(tga->
ofp, 12, 0);
98 int stopx,
int stopy,
unsigned char * buffer) {
99 int x, y, totalx, totaly, xbytes, widthbytes, regionstart;
100 unsigned char * bufpos;
101 int filepos, numbytes;
103 unsigned char * fixbuf;
105 totalx = stopx - startx + 1;
106 totaly = stopy - starty + 1;
108 widthbytes = tga->
width*3;
109 fixbuf = (
unsigned char *) malloc(xbytes);
110 if (fixbuf == NULL) {
115 regionstart = 18 + (startx-1)*3 + widthbytes*(tga->
height-starty-totaly+1);
116 if (totalx == tga->
width) {
119 fseek(tga->
ofp, filepos, 0);
126 for (y=0; y<totaly; y++) {
127 bufpos=buffer + xbytes*(totaly-y-1);
128 for (x=0; x<xbytes; x+=3) {
129 fixbuf[x ] = bufpos[x + 2];
130 fixbuf[x + 1] = bufpos[x + 1];
131 fixbuf[x + 2] = bufpos[x ];
133 numbytes = fwrite(fixbuf, 1, xbytes, tga->
ofp);
134 if (numbytes != xbytes) {
136 sprintf(msgtxt,
"File write problem, %d bytes written.", numbytes);
143 for (y=0; y<totaly; y++) {
144 bufpos=buffer + xbytes*(totaly-y-1);
145 filepos=regionstart + widthbytes*y;
148 fseek(tga->
ofp, filepos, 0);
150 for (x=0; x<xbytes; x+=3) {
151 fixbuf[x ] = bufpos[x + 2];
152 fixbuf[x + 1] = bufpos[x + 1];
153 fixbuf[x + 2] = bufpos[x ];
156 numbytes = fwrite(fixbuf, 1, xbytes, tga->
ofp);
157 if (numbytes != xbytes) {
159 sprintf(msgtxt,
"File write problem, %d bytes written.", numbytes);
182 int readtga(
char * name,
int * xres,
int * yres,
unsigned char **imgdata) {
183 int format, width, height, w1, w2, h1, h2, depth, flags;
184 int imgsize, bytesread, i, tmp;
187 ifp=fopen(name,
"r");
212 if ((format != 2) || (depth != 24)) {
218 width = ((w2 << 8) | w1);
219 height = ((h2 << 8) | h1);
221 imgsize = 3 * width * height;
222 *imgdata = malloc(imgsize);
223 bytesread = fread(*imgdata, 1, imgsize, ifp);
228 int rowsize = 3 * width;
229 unsigned char * copytmp;
231 copytmp = malloc(rowsize);
233 for (i=0; i<height / 2; i++) {
234 memcpy(copytmp, &((*imgdata)[rowsize*i]), rowsize);
235 memcpy(&(*imgdata)[rowsize*i], &(*imgdata)[rowsize*(height - 1 - i)], rowsize);
236 memcpy(&(*imgdata)[rowsize*(height - 1 - i)], copytmp, rowsize);
244 for (i=0; i<imgsize; i+=3) {
246 (*imgdata)[i] = (*imgdata)[i+2];
247 (*imgdata)[i+2] = tmp;
253 if (bytesread != imgsize)
260 int writetga(
char * name,
int xres,
int yres,
unsigned char *imgdata) {
264 rc =
createtgafile(name, (
unsigned short) xres, (
unsigned short) yres);
#define IMAGEUNSUP
the image file is an unsupported format
void closetgafile(void *voidhandle)
void rt_ui_message(int level, char *msg)
void * opentgafile(char *filename)
Tachyon cross-platform timers, special math function wrappers, and RNGs.
int readtga(char *name, int *xres, int *yres, unsigned char **imgdata)
#define IMAGENOERR
no error
#define IMAGEREADERR
failed read, short reads etc
#define IMAGEWRITEERR
failed write, short writes etc
void writetgaregion(void *voidhandle, int startx, int starty, int stopx, int stopy, unsigned char *buffer)
int writetga(char *name, int xres, int yres, unsigned char *imgdata)
Tachyon public API function prototypes and declarations used to drive the ray tracing engine...
#define IMAGEBADFILE
can't find or can't open the file
int createtgafile(char *name, unsigned short width, unsigned short height)