-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.cpp
200 lines (164 loc) · 4.36 KB
/
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
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
/* File: image.cpp; Mode: C++; Tab-width: 3; Author: Simon Flannery; */
#include "image.h"
#ifdef BITMAP_SUPPORT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
Image::Image(size_t width, size_t height) : w(width), h(height), data(NULL)
{
size_t size = w * h;
data = new vector3f[size];
vector3f white(1.0f, 1.0f, 1.0f);
for (size_t i = 0; i < size; ++i)
{
data[i] = white;
}
}
Image::Image(const Image& i)
{
w = i.w;
h = i.w;
size_t size = w * h;
data = new vector3f[size];
for (size_t j = 0; j < size; ++j)
{
data[j] = i.data[j];
}
}
Image::~Image()
{
delete [] data;
}
void Image::SetPixel(float _x, float _y, const color3f& color)
{
if (_x < 0.0f)
{
_x = 0.0f;
}
else if (_x > 1.0f)
{
_x = 1.0f;
}
if (_y < 0.0f)
{
_y = 0.0f;
}
else if (_y > 1.0f)
{
_y = 1.0f;
}
size_t ix = (size_t) (_x * (float) w);
size_t iy = (size_t) (_y * (float) h);
data[iy * w + ix] = color;
return;
}
void Image::SetPixel(size_t _x, size_t _y, const color3f& color)
{
if (_x >= w)
{
_x = w - 1;
}
if (_y >= h)
{
_y = h - 1;
}
data[_y * w + _x] = color;
return;
}
vector3f Image::GetPixel(size_t _x, size_t _y) const
{
return data[_y * w + _x];
}
void Image::Save(const char* szFileName, Format t) const
{
if (szFileName != NULL)
{
if (t == Format::TGA)
{
SaveTGA(szFileName);
}
else if (t == Format::BMP)
{
SaveBMP(szFileName);
}
}
return;
}
void Image::SaveTGA(const char* szFileName) const
{
FILE* file = fopen(szFileName, "wb");
if (file != NULL)
{
/* Header information. */
for (size_t i = 0; i < 18; ++i)
{
if (i == 2) WriteByte(file, 2);
else if (i == 12) WriteByte(file, static_cast<unsigned char>(w % 256));
else if (i == 13) WriteByte(file, static_cast<unsigned char>(w / 256));
else if (i == 14) WriteByte(file, static_cast<unsigned char>(h % 256));
else if (i == 15) WriteByte(file, static_cast<unsigned char>(h / 256));
else if (i == 16) WriteByte(file, static_cast<unsigned char>(24));
else if (i == 17) WriteByte(file, static_cast<unsigned char>(32));
else WriteByte(file, 0);
}
/* Write the image data. */
for (int j = h - 1; j >= 0; --j)
{
for (int i = 0; i < w; ++i)
{
color3f v = GetPixel(i, j);
WriteByte(file, ClampColorComponent(v[b]));
WriteByte(file, ClampColorComponent(v[g]));
WriteByte(file, ClampColorComponent(v[r]));
}
}
fclose(file);
}
return;
}
void Image::SaveBMP(const char* szFileName) const
{
#ifdef BITMAP_SUPPORT
FILE* file = fopen(szFileName, "wb");
if (file != NULL)
{
BITMAPFILEHEADER bmpHeader = {0}; /* Bitmap file header. */
bmpHeader.bfSize = sizeof(BITMAPFILEHEADER);
bmpHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmpHeader.bfType = BITMAP_ID; /* The magic number. */
size_t pad = (4 - (w * 3) % 4) % 4;
BITMAPINFOHEADER bmpInfomation = {0}; /* Bitmap infomation header. */
bmpInfomation.biSize = sizeof(BITMAPINFOHEADER);
bmpInfomation.biPlanes = 1; /* The only supported value. */
bmpInfomation.biBitCount = 24;
bmpInfomation.biCompression = BI_RGB;
bmpInfomation.biWidth = w;
bmpInfomation.biHeight = h;
bmpInfomation.biSizeImage = ((w * 3) + pad) * h;
bmpInfomation.biXPelsPerMeter = 2834;
bmpInfomation.biYPelsPerMeter = 2834;
fwrite(&bmpHeader, sizeof(BITMAPFILEHEADER), 1, file);
fwrite(&bmpInfomation, sizeof(BITMAPINFOHEADER), 1, file);
/* Write the image data upside-down. */
for (size_t j = 0; j < h; ++j)
{
for (size_t i = 0; i < w; ++i)
{
color3f v = GetPixel(i, j);
WriteByte(file, ClampColorComponent(v[b]));
WriteByte(file, ClampColorComponent(v[g]));
WriteByte(file, ClampColorComponent(v[r]));
}
for (size_t i = 0; i < pad; ++i)
{
WriteByte(file, 0);
}
}
fclose(file);
}
#endif
return;
}