|
@@ -50,40 +50,40 @@ void argb2rgba32(unsigned char *image, unsigned int w, unsigned int h) {
|
50
|
50
|
}
|
51
|
51
|
|
52
|
52
|
#define NEXT_POWER(x) do { \
|
53
|
|
- int i; \
|
|
53
|
+ unsigned int i; \
|
54
|
54
|
for (i = 1; i < (x); i *= 2); \
|
55
|
|
- x = i; \
|
|
55
|
+ (x) = i; \
|
56
|
56
|
} while (false);
|
57
|
57
|
|
58
|
58
|
// This code based off on gluScaleImage()
|
59
|
|
-unsigned char *scaleBuffer(unsigned char *image, int width, int height, int components) {
|
|
59
|
+unsigned char *scaleBuffer(unsigned char *image, unsigned int *w, unsigned int *h, unsigned int bpp) {
|
|
60
|
+ unsigned int width = *w;
|
|
61
|
+ unsigned int height = *h;
|
60
|
62
|
assert(image != NULL);
|
61
|
63
|
assert(width > 0);
|
62
|
64
|
assert(height > 0);
|
63
|
|
- assert((components == 3) || (components == 4));
|
|
65
|
+ assert((bpp % 8) == 0);
|
64
|
66
|
|
65
|
|
- int original_height = height;
|
66
|
|
- int original_width = width;
|
|
67
|
+ unsigned int components = bpp / 8;
|
|
68
|
+ unsigned int original_height = height;
|
|
69
|
+ unsigned int original_width = width;
|
67
|
70
|
|
68
|
71
|
NEXT_POWER(height);
|
69
|
72
|
NEXT_POWER(width);
|
70
|
73
|
|
71
|
|
- if (height > 256)
|
72
|
|
- height = 256;
|
73
|
|
-
|
74
|
|
- if (width > 256)
|
75
|
|
- width = 256;
|
76
|
|
-
|
77
|
74
|
// Check to see if scaling is needed
|
78
|
75
|
if (height == original_height && width == original_width)
|
79
|
76
|
return NULL;
|
80
|
77
|
|
|
78
|
+ *w = width;
|
|
79
|
+ *h = height;
|
|
80
|
+
|
81
|
81
|
unsigned char *timage = new unsigned char[height * width * components];
|
82
|
82
|
float *tempin = new float[original_width * original_height * components];
|
83
|
83
|
float *tempout = new float[width * height * components];
|
84
|
84
|
|
85
|
85
|
// Copy user data to float format.
|
86
|
|
- for (int i = 0; i < original_height * original_width * components; ++i) {
|
|
86
|
+ for (unsigned int i = 0; i < original_height * original_width * components; ++i) {
|
87
|
87
|
tempin[i] = (float)image[i];
|
88
|
88
|
}
|
89
|
89
|
|
|
@@ -103,9 +103,9 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
|
103
|
103
|
}
|
104
|
104
|
|
105
|
105
|
if (sx < 1.0 && sy < 1.0) { // Magnify both width and height: use weighted sample of 4 pixels
|
106
|
|
- for (int i = 0; i < height; ++i) {
|
107
|
|
- int i0 = (int)(i * sy);
|
108
|
|
- int i1 = i0 + 1;
|
|
106
|
+ for (unsigned int i = 0; i < height; ++i) {
|
|
107
|
+ unsigned int i0 = (unsigned int)(i * sy);
|
|
108
|
+ unsigned int i1 = i0 + 1;
|
109
|
109
|
|
110
|
110
|
if (i1 >= original_height) {
|
111
|
111
|
i1 = original_height - 1;
|
|
@@ -113,9 +113,9 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
|
113
|
113
|
|
114
|
114
|
float alpha = i * sy - i0;
|
115
|
115
|
|
116
|
|
- for (int j = 0; j < width; ++j) {
|
117
|
|
- int j0 = (int) (j * sx);
|
118
|
|
- int j1 = j0 + 1;
|
|
116
|
+ for (unsigned int j = 0; j < width; ++j) {
|
|
117
|
+ unsigned int j0 = (unsigned int) (j * sx);
|
|
118
|
+ unsigned int j1 = j0 + 1;
|
119
|
119
|
|
120
|
120
|
if (j1 >= original_width) {
|
121
|
121
|
j1 = original_width - 1;
|
|
@@ -131,7 +131,7 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
|
131
|
131
|
|
132
|
132
|
float *dst = tempout + (i * width + j) * components;
|
133
|
133
|
|
134
|
|
- for (int k = 0; k < components; ++k) {
|
|
134
|
+ for (unsigned int k = 0; k < components; ++k) {
|
135
|
135
|
float s1 = *src00++ * (1.0f - beta) + *src01++ * beta;
|
136
|
136
|
float s2 = *src10++ * (1.0f - beta) + *src11++ * beta;
|
137
|
137
|
*dst++ = s1 * (1.0f - alpha) + s2 * alpha;
|
|
@@ -139,17 +139,17 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
|
139
|
139
|
}
|
140
|
140
|
}
|
141
|
141
|
} else { // Shrink width and/or height: use an unweighted box filter
|
142
|
|
- for (int i = 0; i < height; ++i) {
|
143
|
|
- int i0 = (int) (i * sy);
|
144
|
|
- int i1 = i0 + 1;
|
|
142
|
+ for (unsigned int i = 0; i < height; ++i) {
|
|
143
|
+ unsigned int i0 = (unsigned int) (i * sy);
|
|
144
|
+ unsigned int i1 = i0 + 1;
|
145
|
145
|
|
146
|
146
|
if (i1 >= original_height) {
|
147
|
147
|
i1 = original_height - 1;
|
148
|
148
|
}
|
149
|
149
|
|
150
|
|
- for (int j = 0; j < width; ++j) {
|
151
|
|
- int j0 = (int) (j * sx);
|
152
|
|
- int j1 = j0 + 1;
|
|
150
|
+ for (unsigned int j = 0; j < width; ++j) {
|
|
151
|
+ unsigned int j0 = (unsigned int) (j * sx);
|
|
152
|
+ unsigned int j1 = j0 + 1;
|
153
|
153
|
|
154
|
154
|
if (j1 >= original_width) {
|
155
|
155
|
j1 = original_width - 1;
|
|
@@ -158,11 +158,11 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
|
158
|
158
|
float *dst = tempout + (i * width + j) * components;
|
159
|
159
|
|
160
|
160
|
// Compute average of pixels in the rectangle (i0,j0)-(i1,j1)
|
161
|
|
- for (int k = 0; k < components; ++k) {
|
|
161
|
+ for (unsigned int k = 0; k < components; ++k) {
|
162
|
162
|
float sum = 0.0;
|
163
|
163
|
|
164
|
|
- for (int ii = i0; ii <= i1; ++ii) {
|
165
|
|
- for (int jj = j0; jj <= j1; ++jj) {
|
|
164
|
+ for (unsigned int ii = i0; ii <= i1; ++ii) {
|
|
165
|
+ for (unsigned int jj = j0; jj <= j1; ++jj) {
|
166
|
166
|
sum += *(tempin + (ii * original_width + jj)
|
167
|
167
|
* components + k);
|
168
|
168
|
}
|
|
@@ -176,7 +176,7 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
|
176
|
176
|
}
|
177
|
177
|
|
178
|
178
|
// Copy to our results.
|
179
|
|
- for (int i = 0; i < height * width * components; ++i) {
|
|
179
|
+ for (unsigned int i = 0; i < height * width * components; ++i) {
|
180
|
180
|
timage[i] = (unsigned char)tempout[i];
|
181
|
181
|
}
|
182
|
182
|
|