Difference between revisions of "Targa.c"

From Organic Design wiki
(put test at bottom - no function prototypes)
(need int on each parameter)
Line 6: Line 6:
  
 
// Create a new targa image in memory
 
// Create a new targa image in memory
char* targa(int width, height) {
+
char* targa(int width, int height) {
 
char* image = malloc(18+width*height*4);
 
char* image = malloc(18+width*height*4);
 
image[12] = width & 0xff00 >> 8;
 
image[12] = width & 0xff00 >> 8;
Line 17: Line 17:
 
}
 
}
  
char* getOffset(char* image, int x,y) {
+
char* getOffset(char* image, int x, int y) {
 
int width = (int)((image[12]<<8)+image[13]);
 
int width = (int)((image[12]<<8)+image[13]);
 
return image+18+4*(x+y*width);
 
return image+18+4*(x+y*width);
 
}
 
}
  
int getPixel(char* image, int x,y) {
+
int getPixel(char* image, int x, int y) {
 
return *getOffset(image,x,y);
 
return *getOffset(image,x,y);
 
}
 
}
  
void setPixel(char* image, int x,y,colour) {
+
void setPixel(char* image, int x, int y, int colour) {
 
int i, col[3], a = colour >> 24;
 
int i, col[3], a = colour >> 24;
 
int col[0] = (colour & 0xff0000) >> 16;
 
int col[0] = (colour & 0xff0000) >> 16;
Line 36: Line 36:
 
}
 
}
  
void fillRect(char* image, int x1,y1,x2,y2,colour) {
+
void fillRect(char* image, int x1, int y1, int x2 int ,y2, int colour) {
 
int x,y;
 
int x,y;
 
for (y=y1; y<y2; y++)
 
for (y=y1; y<y2; y++)

Revision as of 11:36, 3 July 2006

// Licenced under LGPL: www.gnu.org/copyleft/lesser.html

  1. include <stdlib.h>
  2. include <stdio.h>
  3. include <string.h>

main() {

// Create a new targa image in memory char* targa(int width, int 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, int y) { int width = (int)((image[12]<<8)+image[13]); 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; 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, 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); } }


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