Difference between revisions of "Targa.c"
m |
m |
||
Line 10: | Line 10: | ||
image[12] = width & 0xff; | image[12] = width & 0xff; | ||
image[13] = (width & 0xff00) >> 8; | image[13] = (width & 0xff00) >> 8; | ||
− | image[14] = (char)(height & 0xff); | + | image[14] = 200;//(char)(height & 0xff); |
image[15] = (height & 0xff00) >> 8; | image[15] = (height & 0xff00) >> 8; | ||
image[16] = ' '; | image[16] = ' '; |
Revision as of 13:03, 3 July 2006
// Licenced under LGPL: www.gnu.org/copyleft/lesser.html
- include <stdlib.h>
- include <stdio.h>
- include <string.h>
main() {
// Create a new targa image in memory char* newImage(int width, int height) { char* image = calloc(18+width*height*4,sizeof(char)); image[12] = width & 0xff; image[13] = (width & 0xff00) >> 8; image[14] = 200;//(char)(height & 0xff); image[15] = (height & 0xff00) >> 8; image[16] = ' '; image[17] = 8; printf("%d(%d,%d)\n",height,image[14],image[15]); return image; }
int getSize(char* image) { int width = (image[13]<<8)+image[12]; int height = (image[15]<<8)+image[14]; printf("%d(%d,%d)\n",height,image[14],image[15]); return 18+4*width*height; }
char* getOffset(char* image, int x, int y) { int width = (image[13]<<8)+image[12]; return image+18+4*(x+y*width); }
int getPixel(char* image, int x, int y) { return *getOffset(image,x,y); }
void setPixel(char* image, int x, int y, int colour) { int i, col[3], a = colour >> 24; col[0] = (colour & 0xff0000) >> 16; col[1] = (colour & 0xff00) >> 8; col[2] = colour & 0xff; char* pix = getOffset(image,x,y); for (i=1; i<4; i++) pix[i] = (char)(col[i]*a/255+pix[i]*(255-a)/255); }
void fillRect(char* image, int x1, int y1, int x2, int y2, int colour) { int x,y; for (y=y1; y<y2; y++) for (x=x1; x<x2; x++) setPixel(image,x,y,colour); }
// Create test image and write to file
char* image = newImage(100,200);
//fillRect(image,10,20,30,40,0xff0000ff);
//fillRect(image,20,30,40,50,0x80ff0000);
int fd = creat("/var/www/wiki/targa.tga",0x755);
write(fd,image,getSize(image));
}