-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgaimage.cpp
263 lines (202 loc) · 7.61 KB
/
tgaimage.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/************************************************************************/
/* */
/* Member functions for TGAimage class */
/* */
/************************************************************************/
#include "tgaimage.h"
///////////////////////////////////////////////////////////////////////////////////
//
// Constructor
//
TGAimage::TGAimage(char* name)
{
image_data_offset = 18; // pixel data starts at 18
xsize = ysize = 0; // need to initialize them, they are 4 byte ints,
// reading 2 byte ints from file using SetRead
int length = strlen(name); // length of string argument
filename = new char[length+1]; // get memory
strcpy(filename,name); // copy argument to it
}
//////////////////////////////////////////////////////////////////////////
//
// Deconstructor
//
TGAimage::~TGAimage(void) // deconstructor
{
delete[] filename; // release memory for "filename" string
free(pix_buff);
}
/////////////////////////////////////////////////////////////////////////
//
// Create New : 'Initialises' the current image associated with this
// object ready for writing to. Effectively creating a new
// image.
//
void TGAimage::createNew(int x, int y) // have filename as parameter , not in constructor
{
outfile.open(filename, ios_base::binary); // open output file in binary mode
xsize = x; // copy image x size
ysize = y; // copy image y size
pix_buff = (char*)calloc((xsize*ysize*3), sizeof(char)); // initialize pixel buffer
}
/////////////////////////////////////////////////////////////////////////
//
// Open Existing Image
//
void TGAimage::readImage() // have filename as parameter , not in constructor
{
infile.open(filename, ios_base::binary); // open input file in binary mode
if (!(infile.is_open())) // if file is not open for read then return
return;
cout << "OPEN " << filename;
/************ get x size and y size from file *********************/
infile.seekg(12);
infile.read(reinterpret_cast<char *>(&xsize), sizeof(short)); // 2 bytes
infile.seekg(14);
infile.read(reinterpret_cast<char *>(&ysize), sizeof(short)); // 2 bytes
// check to make sure pix_buff isn't already pointing to an array, when reading in a second image - get memory leaks
pix_buff = (char*)calloc((xsize*ysize*3), sizeof(char));
infile.seekg(image_data_offset);
infile.read(pix_buff, xsize*ysize*3*sizeof(char)); // sizeof(char) = 1
cout << "readImage() xsize " << xsize << ", ysize " << ysize;
}
/////////////////////////////////////////////////////////////////////////
//
// Set Colour Of Pixel x,y
//
void TGAimage::setPixel(char red, char green, char blue, int x, int y)
{
// pixels stored | b, g, r | b, g, r | b, g, r | etc.
// check if pix_buff exists
//if (pix_buff != null)
long pos = ((y * xsize) + x) * 3;
pix_buff[pos] = blue;
pix_buff[pos+1] = green;
pix_buff[pos+2] = red;
}
//////////////////////////////////////////////////////////////////////////
//
// Get Pixel Colour
//
void TGAimage::getPixel(int& red, int& green, int& blue, int x, int y)
{
// pixels stored | b, g, r | b, g, r | b, g, r | etc.
long pos = ((y * xsize) + x) * 3;
blue = pix_buff[pos];
green = pix_buff[pos+1];
red = pix_buff[pos+2];
}
//////////////////////////////////////////////////////////////////////////
//
// Write Image
//
void TGAimage::writeImage()
{
// - pass in an array and write it to file
char value;
cout << endl << "Creating file " << filename
<< " (" << xsize << " x " << ysize << ")" << endl; // display filename
//unsigned char data;
outfile.seekp(0); //offset 0
//unsigned short int value = 0;
value = 0;
outfile.write(reinterpret_cast<const char *>(&value), sizeof(char)); //put 0 - number of chars in ID field
outfile.seekp(1); //offset 1
outfile.write(reinterpret_cast<const char *>(&value), sizeof(char)); //put 0 - no color map included
outfile.seekp(2); //offset 2
value = 2;
outfile.write(reinterpret_cast<const char *>(&value), sizeof(char)); //put 2 - image type code
//image specification
outfile.seekp(8); //offset 8
value = 0;
outfile.write(reinterpret_cast<const char *>(&value), sizeof(short)); // 2 bytes //put 0 - bottom left x co-ord
outfile.seekp(10); //offset 10
outfile.write(reinterpret_cast<const char *>(&value), sizeof(short));// 2 bytes //put 0 - bottom left y co-ord
outfile.seekp(12);
outfile.write(reinterpret_cast<const char *>(&xsize), sizeof(short));// 2 bytes put xsize - width of image in pixels
outfile.seekp(14);
outfile.write(reinterpret_cast<const char *>(&ysize), sizeof(short));// 2 bytes put ysize - height of image in pixels
outfile.seekp(16);
value = 24;
outfile.write(reinterpret_cast<const char *>(&value), sizeof(char)); //put 24 - 24 bits per pixel
outfile.seekp(17);
value = 32;
outfile.write(reinterpret_cast<const char *>(&value), sizeof(char)); //image descriptor byte
//put 00100000 = 32 or 00000100 = 4
outfile.seekp(image_data_offset); //start of this pixel's data
outfile.write(reinterpret_cast<const char *>(pix_buff), xsize*ysize*3*sizeof(char));
}
/////////////////////////////////////////////////////////////////////////
//
// Blur Existing Image
//
void TGAimage::blurImage()
{
// char r, g, b, p;
// char br, bg, bb;
// long pos;
TGAimage *blur_pic;
blur_pic = new TGAimage("blur.tga");
// char* pix = (char*)calloc((xsize*ysize*3), sizeof(char));
// char* greenpix = (char*)calloc((xsize*ysize), sizeof(char));
// char* bluepix = (char*)calloc((xsize*ysize), sizeof(char));
unsigned long filepos = image_data_offset; // position of pointer in file
blur_pic->createNew(xsize, ysize);
if (!(infile.is_open())) // if file is not open for read then return
return;
cout << "Bluring..." << endl;
// infile.seekg(filepos);
// infile.read(pix, xsize*ysize*3*sizeof(char));
// for (int y=0; y < ysize; y++)
// {
// if (y % 100 == 0)
// cout << "reading line " << y << endl;
// for (int x=0; x < xsize; x++) // Read values from file into arrays
// {
// GetPixelColour(r, g, b, x, y);
// pixels stored in this direction x ->
// pixels colours stored blue, green, red
// infile.seekg(filepos++); //start of this pixel's data
// infile.read(&p, sizeof(char)); // read pixel's blue data
/*infile.seekg(filepos++);
infile.read(&g, sizeof(char)); // read pixel's green data
infile.seekg(filepos++);
infile.read(&r, sizeof(char)); // read pixel's red data */
// pos = (y*xsize)+x;
// pix[pos] = p;
// if (pos % 10000 == 0)
// cout << "read in " << pos << " pixels" << endl;
//greenpix[pos] = g;
//bluepix[pos] = b;
// }
// }
// COMMENTS!!
/* for (int y=1; y < (ysize-1); y++)
{
if (y % 100 == 0)
cout << "line " << y << " of " << ysize << endl;
for (int x=1; x < (xsize-1); x++)
{
br = bg = bb = 0;
// COMMENTS!!
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
{
int xi = x + i;
int yj = y + j;
int pos = ((yj*xsize)+xi)*3;
// br += redpix[pos];
// bg += greenpix[pos];
// bb += bluepix[pos];
br += pix[pos+2];
bg += pix[pos+1];
bb += pix[pos];
}
br = br / 9;
bg = bg / 9;
bb = bb / 9;
}
}*/
blur_pic->writeImage();
//free(pix);
}