Difference between revisions of "Targa.c"

From Organic Design wiki
m
m
 
(69 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{legacy}}
 +
<source lang="c">
 +
// Licenced under LGPL: www.gnu.org/copyleft/lesser.html
 +
#include <stdlib.h>
 +
#include <stdio.h>
 +
#include <string.h>
 
main() {
 
main() {
  
// Default Targa image header
+
// Create a new targa image in memory
typedef struct {
+
char* newImage(int width, int height) {
char idlength;
+
char* image = malloc(18+4*width*height+26);
char  colourmaptype;
+
strcpy(image+18+4*width*height+8,"TRUEVISION-XFILE.");
char  datatypecode;
+
image[2] = 2;
short int colourmaporigin;
+
image[12] = width & 0xff;
short int colourmaplength;
+
image[13] = (width & 0xff00) >> 8;
char  colourmapdepth;
+
image[14] = height & 0xff;
short int x_origin;
+
image[15] = (height & 0xff00) >> 8;
short int y_origin;
+
image[16] = ' ';
short width;
+
image[17] = 8;
short height;
+
return image;
char  bitsperpixel;
+
}
char  imagedescriptor;
 
} targaHeader;
 
 
 
char* image = targa( 100, 200 );
 
fillRect( image, 10, 20, 30, 40, 0xff0000ff );
 
 
 
  
 +
// Return complete byte size of passed image
 +
int getSize(unsigned char* image) {
 +
int width = (image[13]<<8)+image[12];
 +
int height = (image[15]<<8)+image[14];
 +
return 18+4*width*height+26;
 +
}
  
// Create a new targa image in memory
+
// Return address of pixel x,y in passed image
char* targa(short int width, height) {
+
char* getOffset(unsigned char* image, int x, int y) {
targaHeader* image;
+
int width = (image[13]<<8)+image[12];
image = malloc(sizeOf(targaHeader)+width*height*4);
+
return image+18+4*(x+y*width);
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) {
+
// Return the content of pixel x,y for the passed image
targaHeader* header = (targaHeader*)image;
+
int getPixel(char* image, int x, int y) {
char* addr = image+sizeOf(header)+4*(x+header->width*y);
+
return *getOffset(image,x,y);
return *addr;
 
 
}
 
}
  
void setPixel(char* image, short int x, y, int colour) {
+
// Set pixel x,y to passed colour allowing for alpha
 +
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;
 +
unsigned char* pix = getOffset(image,x,y);
 +
for (i = 0; i < 3; i++)
 +
pix[i] = (a*col[i]+(255-a)*pix[i])/255;
 
}
 
}
  
void fillRect( char* image, short int x1,y1,x2,y2, int colour) {
+
// Fill a rect in passed image using passed bounds and 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++)
for (x=x1; x<x2; x++)
+
for (x = x1; x < x2; x++)
 
setPixel(image,x,y,colour);
 
setPixel(image,x,y,colour);
 
}
 
}
 +
 +
 +
// Create test a image
 +
char* image = newImage(200,200);
 +
fillRect(image,50,50,100,100,0xff0000ff);
 +
fillRect(image,75,75,175,175,0x80ff0000);
 +
fillRect(image,60,60,165,165,0x40ff00ff);
 +
 +
// Write test image to file and convert to jpeg
 +
int fd = creat("/var/www/wiki/targa.tga",0x755);
 +
write(fd,image,getSize(image));
 +
system("cjpeg -quality 100 /var/www/wiki/targa.tga > /var/www/wiki/targa.jpg");
 +
 
}
 
}
 +
</source>
 +
[[Category:C]]

Latest revision as of 15:26, 6 July 2015

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, now this page is for historic record only.
// 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 = malloc(18+4*width*height+26);
		strcpy(image+18+4*width*height+8,"TRUEVISION-XFILE.");
		image[2] = 2;
		image[12] = width & 0xff;
		image[13] = (width & 0xff00) >> 8;
		image[14] = height & 0xff;
		image[15] = (height & 0xff00) >> 8;
		image[16] = ' ';
		image[17] = 8;
		return image;
		}

	// Return complete byte size of passed image
	int getSize(unsigned char* image) {
		int width = (image[13]<<8)+image[12];
		int height = (image[15]<<8)+image[14];
		return 18+4*width*height+26;
		}

	// Return address of pixel x,y in passed image
	char* getOffset(unsigned char* image, int x, int y) {
		int width = (image[13]<<8)+image[12];
		return image+18+4*(x+y*width);
		}

	// Return the content of pixel x,y for the passed image
	int getPixel(char* image, int x, int y) {
		return *getOffset(image,x,y);
		}

	// Set pixel x,y to passed colour allowing for alpha
	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;
		unsigned char* pix = getOffset(image,x,y);
		for (i = 0; i < 3; i++)
			pix[i] = (a*col[i]+(255-a)*pix[i])/255;
		}

	// Fill a rect in passed image using passed bounds and colour
	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 a image
	char* image = newImage(200,200);
	fillRect(image,50,50,100,100,0xff0000ff);
	fillRect(image,75,75,175,175,0x80ff0000);
	fillRect(image,60,60,165,165,0x40ff00ff);

	// Write test image to file and convert to jpeg
	int fd = creat("/var/www/wiki/targa.tga",0x755);
	write(fd,image,getSize(image));
	system("cjpeg -quality 100 /var/www/wiki/targa.tga > /var/www/wiki/targa.jpg");

	}