Difference between revisions of "Targa.c"

From Organic Design wiki
m
(redo targa method and add alpha handling)
Line 1: Line 1:
 
main() {
 
main() {
  
// Default Targa image header
+
char* image = targa(100,200);
typedef struct {
+
fillRect(image,10,20,30,40,0xff0000ff);
char  idlength;
+
fillRect(image,20,30,40,50,0x80ff0000);
char  colourmaptype;
 
char  datatypecode;
 
short int colourmaporigin;
 
short int colourmaplength;
 
char  colourmapdepth;
 
short int x_origin;
 
short int y_origin;
 
short width;
 
short height;
 
char  bitsperpixel;
 
char  imagedescriptor;
 
} targaHeader;
 
  
char* image = targa( 100, 200 );
+
// Create a new targa image in memory
fillRect( image, 10, 20, 30, 40, 0xff0000ff );
+
char* targa(int width, height) {
 +
char* image = malloc(18+width*height*4);
 +
image[12] = width & 0xff00 >> 8;
 +
image[13] = width & 0xff;
 +
image[14] = height & 0xff00 >> 8;
 +
image[15] = height & 0xff;
 +
image[16] = 8;
 +
image[17] = ' ';
 +
return image;
 +
}
  
 
+
char* getOffset(char* image, int x,y) {
 
+
int width = (int)((image[12]<<8)+image[13]);
// Create a new targa image in memory
+
return image+18+4*(x+y*width);
char* targa(short int width, height) {
 
targaHeader* image;
 
image = malloc(sizeOf(targaHeader)+width*height*4);
 
image = {0,0,0,0,0,0,0,0,width,height,8,' '};
 
return (char*)image;
 
 
}
 
}
  
void getPixel(char* image, short int x, y, int colour) {
+
int getPixel(char* image, int x,y) {
targaHeader* header = (targaHeader*)image;
+
return *getOffset(image,x,y);
char* addr = image+sizeOf(header)+4*(x+header->width*y);
 
return *addr;
 
 
}
 
}
  
void setPixel(char* image, short int x, y, int colour) {
+
void setPixel(char* image, int x,y,colour) {
 +
int i, col[3], a = colour >> 24;
 +
int col[0] = (colour & 0xff0000) >> 16;
 +
int col[1] = (colour & 0xff00) >> 8;
 +
int 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, short int x1,y1,x2,y2, int colour) {
+
void fillRect(char* image, int x1,y1,x2,y2,colour) {
 
int x,y;
 
int x,y;
 
for (y=y1; y<y2; y++)
 
for (y=y1; y<y2; y++)

Revision as of 11:29, 3 July 2006

main() {

char* image = targa(100,200); fillRect(image,10,20,30,40,0xff0000ff); fillRect(image,20,30,40,50,0x80ff0000);

// Create a new targa image in memory char* targa(int width, height) { char* image = malloc(18+width*height*4); image[12] = width & 0xff00 >> 8; image[13] = width & 0xff; image[14] = height & 0xff00 >> 8; image[15] = height & 0xff; image[16] = 8; image[17] = ' '; return image; }

char* getOffset(char* image, int x,y) { int width = (int)((image[12]<<8)+image[13]); return image+18+4*(x+y*width); }

int getPixel(char* image, int x,y) { return *getOffset(image,x,y); }

void setPixel(char* image, int x,y,colour) { int i, col[3], a = colour >> 24; int col[0] = (colour & 0xff0000) >> 16; int col[1] = (colour & 0xff00) >> 8; int 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,y1,x2,y2,colour) { int x,y; for (y=y1; y<y2; y++) for (x=x1; x<x2; x++) setPixel(image,x,y,colour); } }