-
Notifications
You must be signed in to change notification settings - Fork 0
/
tga_image.cpp
151 lines (136 loc) · 4.68 KB
/
tga_image.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// http://www.paulbourke.net/dataformats/tga/
#include "tga_image.h"
void tga_32bit_image::MergeBytes(pixel *pxl, unsigned char *p, int bytes)
{
if (bytes == 4) {
pxl->r = p[2];
pxl->g = p[1];
pxl->b = p[0];
pxl->a = p[3];
} else if (bytes == 3) {
pxl->r = p[2];
pxl->g = p[1];
pxl->b = p[0];
pxl->a = 0;
} else if (bytes == 2) {
pxl->r = (p[1] & 0x7c) << 1;
pxl->g = ((p[1] & 0x03) << 6) | ((p[0] & 0xe0) >> 2);
pxl->b = (p[0] & 0x1f) << 3;
pxl->a = (p[1] & 0x80);
}
}
void tga_32bit_image::save(const char *const filename)
{
FILE *fptr;
/* Write the result as a uncompressed TGA */
if ((fptr = fopen(filename,"w")) == NULL) {
fprintf(stderr,"Failed to open outputfile\n");
exit(-1);
}
putc(0,fptr);
putc(0,fptr);
putc(2,fptr); /* uncompressed RGB */
putc(0,fptr); putc(0,fptr);
putc(0,fptr); putc(0,fptr);
putc(0,fptr);
putc(0,fptr); putc(0,fptr); /* X origin */
putc(0,fptr); putc(0,fptr); /* y origin */
putc((hdr.width & 0x00FF),fptr);
putc((hdr.width & 0xFF00) / 256,fptr);
putc((hdr.height & 0x00FF),fptr);
putc((hdr.height & 0xFF00) / 256,fptr);
putc(32,fptr);
putc(0,fptr);
for (size_t i=0;i<hdr.height*hdr.width;i++) {
putc(pixels[i].b,fptr);
putc(pixels[i].g,fptr);
putc(pixels[i].r,fptr);
putc(pixels[i].a,fptr);
}
fclose(fptr);
}
void tga_32bit_image::load(const char *const filename)
{
int n=0,i,j;
int bytes2read,skipover = 0;
unsigned char p[5];
FILE *fptr;
/* Open the file */
if ((fptr = fopen(filename,"r")) == NULL) {
fprintf(stderr,"File open failed\n");
return;
}
/* Display the header fields */
hdr.idlength = fgetc(fptr);
hdr.colourmaptype = fgetc(fptr);
hdr.datatypecode = fgetc(fptr);
fread(&hdr.colourmaporigin,2,1,fptr);
fread(&hdr.colourmaplength,2,1,fptr);
hdr.colourmapdepth = fgetc(fptr);
fread(&hdr.x_origin,2,1,fptr);
fread(&hdr.y_origin,2,1,fptr);
fread(&hdr.width,2,1,fptr);
fread(&hdr.height,2,1,fptr);
hdr.bitsperpixel = fgetc(fptr);
hdr.imagedescriptor = fgetc(fptr);
pixels.resize(hdr.width*hdr.height);
for (i=0;i<hdr.width*hdr.height;i++) {
pixels[i].r = 0;
pixels[i].g = 0;
pixels[i].b = 0;
pixels[i].a = 0;
}
/* Skip over unnecessary stuff */
skipover += hdr.idlength;
skipover += hdr.colourmaptype * hdr.colourmaplength;
// fprintf(stderr,"Skip over %d bytes\n",skipover);
fseek(fptr,skipover,SEEK_CUR);
/* Read the image */
bytes2read = hdr.bitsperpixel / 8;
while (n < hdr.width * hdr.height) {
if (hdr.datatypecode == 2) { /* Uncompressed */
if (fread(p,1,bytes2read,fptr) != bytes2read) {
// fprintf(stderr,"Unexpected end of file at pixel %d\n",i);
return;
}
MergeBytes(&(pixels[n]),p,bytes2read);
n++;
} else if (hdr.datatypecode == 10) { /* Compressed */
if (fread(p,1,bytes2read+1,fptr) != bytes2read+1) {
// fprintf(stderr,"Unexpected end of file at pixel %d\n",i);
return;
}
j = p[0] & 0x7f;
MergeBytes(&(pixels[n]),&(p[1]),bytes2read);
n++;
if (p[0] & 0x80) { /* RLE chunk */
for (i=0;i<j;i++) {
MergeBytes(&(pixels[n]),&(p[1]),bytes2read);
n++;
}
} else { /* Normal chunk */
for (i=0;i<j;i++) {
if (fread(p,1,bytes2read,fptr) != bytes2read) {
// fprintf(stderr,"Unexpected end of file at pixel %d\n",i);
return;
}
MergeBytes(&(pixels[n]),p,bytes2read);
n++;
}
}
}
}
fclose(fptr);
}
void tga_32bit_image::add_colour_noise(double scale)
{
for(size_t i = 0; i < pixels.size(); i++)
{
unsigned char noise = rand()%256;
pixels[i].r = (pixels[i].r + noise*scale) / (1.0 + scale);
noise = rand()%256;
pixels[i].g = (pixels[i].g + noise*scale) / (1.0 + scale);
noise = rand()%256;
pixels[i].b = (pixels[i].b + noise*scale) / (1.0 + scale);
}
}