-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathImage.cpp
More file actions
201 lines (174 loc) · 5.82 KB
/
Copy pathImage.cpp
File metadata and controls
201 lines (174 loc) · 5.82 KB
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
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
#include "Image.h"
namespace DDG
{
Image :: Image( int width, int height )
: w( width ), h( height ), pixels( w*h*3 )
{}
float& Image :: operator()( int x, int y )
// accesses pixel (x,y)
{
return pixels[ x + y*w ];
}
const float& Image :: operator()( int x, int y ) const
// accesses pixel (x,y)
{
return pixels[ x + y*w ];
}
float Image :: sample( float x, float y ) const
// samples image at (x,y) using bilinear filtering
{
const Image& I( *this );
float ax = x - floor( x );
float ay = y - floor( y );
float bx = 1. - ax;
float by = 1. - ay;
int x0 = (int) floor( x );
int y0 = (int) floor( y );
int x1 = x0 + 1;
int y1 = y0 + 1;
clamp( x0, y0 );
clamp( x1, y1 );
return by * ( bx * I(x0,y0) + ax * I(x1,y0) ) +
ay * ( bx * I(x0,y1) + ax * I(x1,y1) ) ;
}
int Image :: width( void ) const
// returns image width
{
return w;
}
int Image :: height( void ) const
// returns image height
{
return h;
}
class TGAHeader
// header format for Truevision TGA images
{
public:
char idFieldSize;
char colorMapType;
char dataTypeCode;
short colorMapOrigin;
short colorMapLength;
char colorMapEntrySize;
short xOrigin;
short yOrigin;
short width;
short height;
char bitsPerPixel;
char imageSpecification;
};
void Image :: read( const char* filename )
// loads an image file in Truevision TGA format
// (must be uncompressed RGB image with 24 or 32 bits per pixel)
{
ifstream in( filename, ios_base::binary );
if( !in.is_open() )
{
cerr << "Error: could not open file " << filename << " for input!" << endl;
exit( 1 );
}
// read header
TGAHeader header;
in.read( (char*) &(header.idFieldSize), 1 );
in.read( (char*) &(header.colorMapType), 1 );
in.read( (char*) &(header.dataTypeCode), 1 );
in.read( (char*) &(header.colorMapOrigin), 2 );
in.read( (char*) &(header.colorMapLength), 2 );
in.read( (char*) &(header.colorMapEntrySize), 1 );
in.read( (char*) &(header.xOrigin), 2 );
in.read( (char*) &(header.yOrigin), 2 );
in.read( (char*) &(header.width), 2 );
in.read( (char*) &(header.height), 2 );
in.read( (char*) &(header.bitsPerPixel), 1 );
in.read( (char*) &(header.imageSpecification), 1 );
w = header.width;
h = header.height;
// validate data type
const char uncompressedRGB = 2;
if( header.dataTypeCode != uncompressedRGB ||
( header.bitsPerPixel != 24 &&
header.bitsPerPixel != 32 ))
{
cerr << "Error: input must be uncompressed RGB image with 24 or 32 bits per pixel." << endl;
exit( 1 );
}
// read identification field (unused)
vector<char> idField( header.idFieldSize );
in.read( &idField[0], header.idFieldSize );
// read color map data (unused)
if( header.colorMapType == 1 )
{
int bytesPerEntry = header.colorMapEntrySize / 8;
int colorMapSize = header.colorMapLength * bytesPerEntry;
vector<char> colorMapData( colorMapSize );
in.read( &colorMapData[0], colorMapSize );
}
// read pixel data
int n = w*h*header.bitsPerPixel/8;
vector<unsigned char> pixelData( n );
in.read( (char*) &pixelData[0], n );
// convert pixel data to floating point
pixels.resize( n );
for( int i = 0; i < n; i++ )
{
pixels[i] = (double) pixelData[i] / 255.;
}
}
void Image :: write( const char* filename ) const
// writes an image file in Truevision TGA format
// (uncompressed RGB image with 24 bits per pixel)
{
ofstream out( filename, ios_base::binary );
if( !out.is_open() )
{
cerr << "Error: could not open file " << filename << " for output!" << endl;
exit( 1 );
}
TGAHeader header;
header.idFieldSize = 0;
header.colorMapType = 0;
header.dataTypeCode = 2;
header.colorMapOrigin = 0;
header.colorMapLength = 0;
header.colorMapEntrySize = 0;
header.xOrigin = 0;
header.yOrigin = 0;
header.width = w;
header.height = h;
header.bitsPerPixel = 24;
header.imageSpecification = 0;
// write header
out.write( (char*) &(header.idFieldSize), 1 );
out.write( (char*) &(header.colorMapType), 1 );
out.write( (char*) &(header.dataTypeCode), 1 );
out.write( (char*) &(header.colorMapOrigin), 2 );
out.write( (char*) &(header.colorMapLength), 2 );
out.write( (char*) &(header.colorMapEntrySize), 1 );
out.write( (char*) &(header.xOrigin), 2 );
out.write( (char*) &(header.yOrigin), 2 );
out.write( (char*) &(header.width), 2 );
out.write( (char*) &(header.height), 2 );
out.write( (char*) &(header.bitsPerPixel), 1 );
out.write( (char*) &(header.imageSpecification), 1 );
// convert pixel data from floating point
vector<unsigned char> pixelData( w*h*3 );
for( int i = 0; i < w*h*3; i++ )
{
pixelData[i] = (unsigned char)( pixels[i] * 255. );
}
// write pixel data
out.write( (char*) &pixelData[0], w*h*3 );
}
void Image :: clamp( int& x, int& y ) const
// clamps coordinates to range [0,w-1] x [0,h-1]
{
x = max( 0, min( w-1, x ));
y = max( 0, min( h-1, y ));
}
}