-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTImageScaler.cpp
More file actions
292 lines (262 loc) · 9.33 KB
/
Copy pathTImageScaler.cpp
File metadata and controls
292 lines (262 loc) · 9.33 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <cstdlib>
#include <cstdio>
#include "TImageScaler.h"
//---------------------------------------------------------------------
/*!
* \brief Clear all containers and reset members.
*/
void TImageScaler::Clean()
{
Width = 0;
Height = 0;
NumComp = 0;
if(Data != NULL) delete [] Data;
Data = NULL;
}
/*!
* \brief Get a pixel value from the image.
* \param Y Position in height.
* \param X Position in width.
* \param Component Component (channel) to be used.
* \return The color in float value of the position/channel, or 0 if not found.
*/
float TImageScaler::GetPixel(uint Y, uint X, uint Component)
{
if(X >= Width || Y >= Height) return 0;
unsigned char c = Data[(Y * NumComp * Width) + (X * NumComp) + Component];
return (float)c;
}
/*!
* \brief Convert to unsigned char a float value of a pixel, making sure it's inside 0f to 255f.
* \param Pixel Value to be converted.
* \return An unsigned char from 0f to 255f, being the value casted.
*/
inline unsigned char Saturate(float Pixel)
{
if(Pixel > 255.0f) return 255;
if(Pixel < 0.0f) return 0;
return (unsigned char)Pixel;
}
//---------------------------------------------------------------------
TImageScaler::TImageScaler()
{
Width = 0;
Height = 0;
NumComp = 0;
Data = NULL;
}
TImageScaler::~TImageScaler()
{
Clean();
}
unsigned int TImageScaler::GetWidth() { return Width; }
unsigned int TImageScaler::GetHeight() { return Height; }
unsigned int TImageScaler::GetComponents() { return NumComp; }
void TImageScaler::SetWidth(unsigned int NewWidth) { this->Width = NewWidth; }
void TImageScaler::SetHeight(unsigned int NewHeight) { this->Height = NewHeight; }
void TImageScaler::Reallocate()
{
if(Data != NULL) delete [] Data;
Data = (unsigned char *)std::malloc(Width * Height * NumComp);
}
void TImageScaler::CopyToPixel(uint X, uint Y, uint Component, unsigned char Pixel)
{
if(X >= Width || Y >= Height) return;
Data[(Y * NumComp * Width) + (X * NumComp) + Component] = Pixel;
}
unsigned char TImageScaler::GetFromPixel(uint X, uint Y, uint Component)
{
if(X >= Width || Y >= Height) return 0;
unsigned char c = Data[(Y * NumComp * Width) + (X * NumComp) + Component];
return c;
}
//---------------------------------------------------------------------
/*!
* \brief Decompress and load image in memory.
* \param FileName Path to file to be loaded.
*/
void TImageScaler::LoadHeader(const char* FileName)
{
// try to open the file
FILE* infile = fopen(FileName, "rb");
if (!infile) return;
// structs to read the jpeg
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
// let's set default stuff, create decompress, etc
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
// store headers value
Width = cinfo.image_width;
Height = cinfo.image_height;
NumComp = cinfo.num_components;
fclose(infile);
}
/*!
* \brief Decompress and load image in memory.
* \param FileName Path to file to be loaded.
*/
void TImageScaler::LoadData(const char* FileName,uint Width0, uint Width1, uint Height0, uint Height1)
{
if(Data != NULL) delete Data;
// try to open the file
FILE* infile = fopen(FileName, "rb");
if (!infile) return;
// structs to read the jpeg
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
// let's set default stuff, create decompress, etc
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
// store headers value
Width = cinfo.image_width;
Height = cinfo.image_height;
NumComp = cinfo.num_components;
// start it, allocate memory
jpeg_start_decompress(&cinfo);
// get bitmap
if(Width0 == 0 && Width1 == 0)
{
Width0 = 1;
Width1 = Width;
}
if(Height0 == 0 && Height1 == 0)
{
Height0 = 1;
Height1 = Height;
}
Width = Width1 - Width0 + 1;
Height = Height1 - Height0 + 1;
Data = (unsigned char *)std::malloc(Width * Height * NumComp);
JSAMPROW row_pointer[1]; // pointer of line read by libjpeg
row_pointer[0] = (unsigned char *)malloc(cinfo.output_width * cinfo.num_components); // allocate memory for one line
uint k = 0;
for(uint j = 0; j < cinfo.image_height; j++) // read all lines
{
jpeg_read_scanlines(&cinfo,row_pointer,1); // scan the line, toss it in the vector
if((j+1) < Height0 || (j+1) > Height1) continue;
for(uint i = 0; i < cinfo.image_width; i++)
{
if((i+1) < Width0 || (i+1) > Width1) continue;
for(int c = 0; c < cinfo.num_components; c++) // read all points in line, toss in the bitmap vector
{
uint k1 = i * cinfo.num_components + c;
Data[k] = row_pointer[0][k1];
k++;
}
}
}
// close and destroy
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
free(row_pointer[0]);
fclose(infile);
}
/*!
* \brief Compress image and save to JPEG file.
* \param FileName Path to file to be saved.
* \param Quality Quality of the image to be saved (from 0 to 100).
*/
void TImageScaler::Save(const char* FileName, int Quality)
{
// try to open the file
FILE* outfile = fopen(FileName, "wb");
if (!outfile) return;
// struct to compress image to jpeg
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
// initializate, create compress and set error messages
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
// set image parameters
cinfo.image_width = Width;
cinfo.image_height = Height;
cinfo.input_components = NumComp;
cinfo.in_color_space = JCS_RGB; // just in case ;-)
jpeg_set_defaults(&cinfo);
// set quality and start compress
jpeg_set_quality (&cinfo, Quality, true);
jpeg_start_compress(&cinfo, true);
// pointer to a row
JSAMPROW row_pointer; // pointer to a row
while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer = (JSAMPROW)&Data[cinfo.next_scanline * cinfo.image_width * cinfo.num_components]; // just drop the line of bitmap directly, so I only need a pointer to the line
jpeg_write_scanlines(&cinfo, &row_pointer, 1);
}
// close and destroy, we are finished
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
fclose(outfile);
}
/*!
* \brief Scale image to the desired side, using a bicubic method.
* \param Size Desired new size of the greater side of the image in px.
*/
void TImageScaler::Scale(float RateWidth, float RateHeight)
{
// sanity check
if(Data == NULL) return;
// determine width and height (I won't deal here with float and integer division and approximation, since the error for rounding is irrelevant
int newWidth = RateWidth * Width;
int newHeight = RateHeight * Height;
// printf("%d x %d => %d x %d \n",Width, Height,newWidth,newHeight);
// create new container with the new width x height
unsigned int newSize = newWidth * newHeight * NumComp;
unsigned char *newData = (unsigned char *)std::malloc(newSize);
// calcuate the tax rates for x and y
float tx = (float)Width/newWidth;
float ty = (float)Height/newHeight;
// bicubic matrix
float C[5] = {0}; // I hate this dennotation...
// help with the raw vector
const unsigned int rowStride = newWidth * NumComp;
// let's go for it, pay attention in width and height (I have mistaked it
for(int i = 0; i < newHeight; i++)
{
for(int j = 0; j < newWidth; j++)
{
//printf("%d %d \r",i,j);
// we will use float to avoid rouding and conversion problems
float x = float(tx * j);
float y = float(ty * i);
float dx = tx * j - x;
float dy = ty * i - y;
// don't touch the channels
for(unsigned k = 0; k < NumComp; k++)
{
//printf("%d %d %d\n",i,j,k);
for(int jj = 0; jj <= 3; jj++)
{
float d0, d2, d3, a0, a1, a2, a3;
a0 = GetPixel(y-1+jj,x,k);
d0 = GetPixel(y-1+jj,x-1,k) - a0;
d2 = GetPixel(y-1+jj,x+1,k) - a0;
d3 = GetPixel(y-1+jj,x+2,k) - a0;
a1 = -1.0f / 3.0f * d0 + d2 - 1.0f / 6.0f * d3;
a2 = 1.0f / 2.0f * d0 + 1.0f / 2.0f * d2;
a3 = -1.0f / 6.0f * d0 - 1.0f / 2.0f * d2 + 1.0f / 6.0f * d3;
C[jj] = a0 + a1 * dx + a2 * dx * dx + a3 * dx * dx * dx;
d0 = C[0] - C[1];
d2 = C[2] - C[1];
d3 = C[3] - C[1];
a0 = C[1];
a1 = -1.0f / 3.0f * d0 + d2 -1.0f / 6.0f * d3;
a2 = 1.0f / 2.0f * d0 + 1.0f / 2.0f * d2;
a3 = -1.0f / 6.0f * d0 - 1.0f / 2.0f * d2 + 1.0f / 6.0f * d3;
newData[i * rowStride + j * NumComp + k] = Saturate(a0 + a1 * dy + a2 * dy * dy + a3 * dy * dy * dy); // saturate or fail!
}
}
}
}
// ok, now delete the old, assign new
Width = newWidth;
Height = newHeight;
delete [] Data;
Data = newData;
}