/* graphics.c Chris Nevison June 24, 1997 This file contains some routines for converting doubles or matrices of doubles to pixels or matrices of pixels of Blue, Green, Red values. It also includes a function for writing a tga format header, to set up a tga graphics file. */ #include #include #include "graphics.h" void DblToPixel(double val, double minval, double maxval, Pixel * p) /* pre: minval <= val <= maxval */ /* post: p is a color (RGB values) pixel representing */ /* val, where colors range from blue for minval */ /* to green for a middle value to red for maxval. */ { double mid, q1, q3, qtrrange; mid = (maxval + minval) / 2; q1 = (mid + minval) / 2; q3 = (mid + maxval) / 2; qtrrange = q3 - mid; if((val < minval) || (maxval < val)){ printf("Value out of range in DblToPixel\n\n"); exit(-1); } else if(val < q1){ p->red = (unsigned char) 0; p->green = (unsigned char) (255 * ((val - minval)/qtrrange)); p->blue = (unsigned char) 255; } else if(val < mid){ p->red = (unsigned char) 0; p->green = (unsigned char) 255; p->blue = (unsigned char) (255 * ((mid - val)/qtrrange)); } else if(val < q3){ p->red = (unsigned char) (255 * ((val - mid)/qtrrange)); p->green = (unsigned char) 255; p->blue = (unsigned char) 0; } else if(val <= maxval){ p->red = (unsigned char) 255 ; p->green = (unsigned char) (255 * ((maxval - val)/qtrrange)); p->blue = (unsigned char) 0; } } void DMatToPMap(double * * mat, double minval, double maxval, int numrows, int numcols, Pixel * * pmap) /* pre: mat is a numrows x numcols matrix of double values, */ /* each value between minval and maxval. */ /* pmap is a numrows x numcols array of pixels, */ /* possibly uninitialized. */ /* post: Each values in pmap has been set to the color */ /* for the corresponding entry in mat. */ { int r, c; for(r = 0; r < numrows; r ++) for(c = 0; c < numcols; c++) DblToPixel(mat[r][c], minval, maxval, &pmap[r][c]); } void CreatePixelMap(int numrows, int numcols, Pixel * * * pmap) /* pre: pmap is an unitialized or NULL pointer */ /* post: pmap is initialized to a numrows x numcols array of */ /* pixels. */ { int r; *pmap = (Pixel **) malloc(numrows * sizeof(Pixel *)); for(r = 0; r < numrows; r++) (*pmap)[r] = (Pixel *) malloc(numcols * sizeof(Pixel)); } void StorePixelBlock(FILE * outfile, int numrows, int numcols, Pixel * * pmap) /* pre: outfile is open ready to append a block of Pixels. */ /* post: The values of pixels in pmap have been written to */ /* outfile in row-major order, in the order */ /* blue-green-red. */ { int r, c; for(r = 0; r < numrows; r++){ for(c = 0; c < numcols; c ++){ fprintf(outfile, "%c", pmap[r][c].blue); fprintf(outfile, "%c", pmap[r][c].green); fprintf(outfile, "%c", pmap[r][c].red); } } } void StoreTGAHeader(FILE * outfile, int height, int width) /* pre: outfile is open, empty and ready to write */ /* post: A standard header file for targa (tga) format for */ /* 24 bit color has been written to outfile. */ { int i; unsigned char header[18]; for(i = 0; i < 18; i++) header[i] = (unsigned char) 0; header[2] = (unsigned char) 2; header[12] = (unsigned char) (width % 256); header[13] = (unsigned char) (width / 256); header[14] = (unsigned char) (height % 256); header[15] = (unsigned char) (height / 256); header[16] = (unsigned char) 24; header[17] = (unsigned char) 32; for(i = 0; i < 18; i++){ fprintf(outfile, "%c", header[i]); } }