減色プログラム
Revision | 67e551fdb121c01fa39e8f7572d283f740f49a5a (tree) |
---|---|
Zeit | 2011-05-17 01:57:56 |
Autor | berupon <berupon@gmai...> |
Commiter | berupon |
型をdoubleからfloatに変更できるところを変更。
@@ -23,15 +23,15 @@ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
23 | 23 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
24 | 24 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
25 | 25 | */ |
26 | - | |
27 | - | |
28 | -#include "stdafx.h" | |
29 | - | |
30 | -#include "Color4f.h" | |
31 | -#include "Array.h" | |
32 | - | |
33 | -typedef Array2D<Color4f> Image4f; | |
34 | - | |
26 | + | |
27 | + | |
28 | +#include "stdafx.h" | |
29 | + | |
30 | +#include "Color4f.h" | |
31 | +#include "Array.h" | |
32 | + | |
33 | +typedef Array2D<Color4f> Image4f; | |
34 | + | |
35 | 35 | #include <algorithm> |
36 | 36 | #include <math.h> |
37 | 37 | #include <stdio.h> |
@@ -40,91 +40,91 @@ typedef Array2D<Color4f> Image4f; | ||
40 | 40 | #include <deque> |
41 | 41 | #include <limits> |
42 | 42 | #include <iostream> |
43 | - | |
44 | -#include "ReadImage/ReadImage.h" | |
45 | -#include "ReadImage/File.h" | |
46 | - | |
47 | -#include "quantize.h" | |
48 | - | |
49 | -// http://proglab.aki.gs/mediaproc/colors.html | |
50 | - | |
51 | -int _tmain(int argc, _TCHAR* argv[]) | |
52 | -{ | |
53 | - if (argc < 1+3 || argc > 1+5) { | |
54 | - puts("Usage: color_quantizer <source image.bmp> <output image.raw> <desired palette size> [dithering level] [filter size (1/3/5)]\n"); | |
55 | - return -1; | |
56 | - } | |
57 | - | |
58 | - int num_colors = _ttoi(argv[3]); | |
43 | + | |
44 | +#include "ReadImage/ReadImage.h" | |
45 | +#include "ReadImage/File.h" | |
46 | + | |
47 | +#include "quantize.h" | |
48 | + | |
49 | +// http://proglab.aki.gs/mediaproc/colors.html | |
50 | + | |
51 | +int _tmain(int argc, _TCHAR* argv[]) | |
52 | +{ | |
53 | + if (argc < 1+3 || argc > 1+5) { | |
54 | + puts("Usage: color_quantizer <source image.bmp> <output image.raw> <desired palette size> [dithering level] [filter size (1/3/5)]\n"); | |
55 | + return -1; | |
56 | + } | |
57 | + | |
58 | + int num_colors = _ttoi(argv[3]); | |
59 | 59 | if (num_colors <= 1 || num_colors > 256) { |
60 | 60 | printf("Number of colors must be at least 2 and no more than 256.\n"); |
61 | 61 | return -1; |
62 | 62 | } |
63 | - | |
64 | - srand(time(NULL)); | |
65 | - | |
66 | - ImageInfo imageInfo; | |
67 | - FILE* f = _tfopen(argv[1], _T("rb")); | |
68 | - File file(f); | |
69 | - if (!ReadImageInfo(file, imageInfo)) { | |
70 | - puts("failed to open image\n"); | |
71 | - return 0; | |
72 | - } | |
73 | - if ( | |
74 | - imageInfo.bitsPerSample != 8 | |
75 | - || (imageInfo.samplesPerPixel != 3 && imageInfo.samplesPerPixel != 4) | |
76 | - ) { | |
77 | - puts("unsupported image file format.\n"); | |
78 | - return 0; | |
79 | - } | |
80 | - size_t lineBytes = imageInfo.samplesPerPixel * imageInfo.width; | |
81 | - if (lineBytes % 4) { | |
82 | - lineBytes += 4 - (lineBytes % 4); | |
83 | - } | |
84 | - std::vector<uint8_t> buff(lineBytes * imageInfo.height); | |
85 | - ReadImageData(file, &buff[0], lineBytes, 0); | |
86 | - fclose(f); | |
87 | - uint8_t* p = &buff[0]; | |
88 | - | |
89 | - size_t width = imageInfo.width; | |
90 | - size_t height = imageInfo.height; | |
91 | - Image4f image(width, height); | |
92 | - Color4f* pLine = image.pBuff_; | |
93 | - uint8_t* pSrcLine = p; | |
94 | - for (size_t y=0; y<height; ++y) { | |
95 | - for (size_t x=0; x<width; ++x) { | |
96 | - float r = pSrcLine[x*3+0] / 255.0f; | |
97 | - float g = pSrcLine[x*3+1] / 255.0f; | |
98 | - float b = pSrcLine[x*3+2] / 255.0f; | |
99 | - pLine[x] = Color4f(r,g,b,0.0f); | |
100 | - } | |
101 | - pLine += width; | |
102 | - pSrcLine += lineBytes; | |
103 | - } | |
104 | - Color4f buff_filter1_weights[1*1]; | |
105 | - Color4f buff_filter3_weights[3*3]; | |
106 | - Color4f buff_filter5_weights[5*5]; | |
107 | - | |
108 | - Image4f filter1_weights(1, 1, buff_filter1_weights); | |
109 | - Image4f filter3_weights(3, 3, buff_filter3_weights); | |
110 | - Image4f filter5_weights(5, 5, buff_filter5_weights); | |
111 | - std::vector<uint8_t> buff_quantized(width * height); | |
112 | - Array2D<uint8_t> quantized_image(width, height, &buff_quantized[0]); | |
113 | - Color4f palette[256]; | |
114 | - | |
115 | - filter1_weights[0][0] = Color4f(1.0f, 1.0f, 1.0f, 0.0f); | |
116 | - | |
117 | - for (size_t i=0; i<num_colors; ++i) { | |
118 | - palette[i] = Color4f( | |
119 | - ((float)rand())/RAND_MAX, | |
120 | - ((float)rand())/RAND_MAX, | |
121 | - ((float)rand())/RAND_MAX, | |
122 | - 0.0f | |
123 | - ); | |
124 | - } | |
125 | - | |
126 | - Array3D<double>* coarse_variables; | |
127 | - double dithering_level = 0.09*log((double)image.width_*image.height_) - 0.04*log((double)num_colors) + 0.001; | |
63 | + | |
64 | + srand(time(NULL)); | |
65 | + | |
66 | + ImageInfo imageInfo; | |
67 | + FILE* f = _tfopen(argv[1], _T("rb")); | |
68 | + File file(f); | |
69 | + if (!ReadImageInfo(file, imageInfo)) { | |
70 | + puts("failed to open image\n"); | |
71 | + return 0; | |
72 | + } | |
73 | + if ( | |
74 | + imageInfo.bitsPerSample != 8 | |
75 | + || (imageInfo.samplesPerPixel != 3 && imageInfo.samplesPerPixel != 4) | |
76 | + ) { | |
77 | + puts("unsupported image file format.\n"); | |
78 | + return 0; | |
79 | + } | |
80 | + size_t lineBytes = imageInfo.samplesPerPixel * imageInfo.width; | |
81 | + if (lineBytes % 4) { | |
82 | + lineBytes += 4 - (lineBytes % 4); | |
83 | + } | |
84 | + std::vector<uint8_t> buff(lineBytes * imageInfo.height); | |
85 | + ReadImageData(file, &buff[0], lineBytes, 0); | |
86 | + fclose(f); | |
87 | + uint8_t* p = &buff[0]; | |
88 | + | |
89 | + size_t width = imageInfo.width; | |
90 | + size_t height = imageInfo.height; | |
91 | + Image4f image(width, height); | |
92 | + Color4f* pLine = image.pBuff_; | |
93 | + uint8_t* pSrcLine = p; | |
94 | + for (size_t y=0; y<height; ++y) { | |
95 | + for (size_t x=0; x<width; ++x) { | |
96 | + float r = pSrcLine[x*3+0] / 255.0f; | |
97 | + float g = pSrcLine[x*3+1] / 255.0f; | |
98 | + float b = pSrcLine[x*3+2] / 255.0f; | |
99 | + pLine[x] = Color4f(r,g,b,0.0f); | |
100 | + } | |
101 | + pLine += width; | |
102 | + pSrcLine += lineBytes; | |
103 | + } | |
104 | + Color4f buff_filter1_weights[1*1]; | |
105 | + Color4f buff_filter3_weights[3*3]; | |
106 | + Color4f buff_filter5_weights[5*5]; | |
107 | + | |
108 | + Image4f filter1_weights(1, 1, buff_filter1_weights); | |
109 | + Image4f filter3_weights(3, 3, buff_filter3_weights); | |
110 | + Image4f filter5_weights(5, 5, buff_filter5_weights); | |
111 | + std::vector<uint8_t> buff_quantized(width * height); | |
112 | + Array2D<uint8_t> quantized_image(width, height, &buff_quantized[0]); | |
113 | + Color4f palette[256]; | |
114 | + | |
115 | + filter1_weights[0][0] = Color4f(1.0f, 1.0f, 1.0f, 0.0f); | |
116 | + | |
117 | + for (size_t i=0; i<num_colors; ++i) { | |
118 | + palette[i] = Color4f( | |
119 | + ((float)rand())/RAND_MAX, | |
120 | + ((float)rand())/RAND_MAX, | |
121 | + ((float)rand())/RAND_MAX, | |
122 | + 0.0f | |
123 | + ); | |
124 | + } | |
125 | + | |
126 | + Array3D<float>* coarse_variables; | |
127 | + float dithering_level = 0.09*log((float)image.width_*image.height_) - 0.04*log((float)num_colors) + 0.001; | |
128 | 128 | if (argc > 1+3) { |
129 | 129 | dithering_level = _ttof(argv[4]); |
130 | 130 | if (dithering_level <= 0.0) { |
@@ -141,11 +141,11 @@ int _tmain(int argc, _TCHAR* argv[]) | ||
141 | 141 | } |
142 | 142 | } |
143 | 143 | |
144 | - double stddev = dithering_level; | |
145 | - double sum = 0.0; | |
144 | + float stddev = dithering_level; | |
145 | + float sum = 0.0; | |
146 | 146 | for (int i=0; i<3; i++) { |
147 | 147 | for (int j=0; j<3; j++) { |
148 | - double w = exp(-sqrt((double)((i-1)*(i-1) + (j-1)*(j-1)))/(stddev*stddev)); | |
148 | + float w = exp(-sqrt((float)((i-1)*(i-1) + (j-1)*(j-1)))/(stddev*stddev)); | |
149 | 149 | filter3_weights[i][j] = Color4f(w,w,w,0); |
150 | 150 | sum += 3 * w; |
151 | 151 | } |
@@ -161,7 +161,7 @@ int _tmain(int argc, _TCHAR* argv[]) | ||
161 | 161 | sum = 0.0; |
162 | 162 | for (int i=0; i<5; i++) { |
163 | 163 | for (int j=0; j<5; j++) { |
164 | - double w = exp(-sqrt((double)((i-2)*(i-2) + (j-2)*(j-2)))/(stddev*stddev)); | |
164 | + float w = exp(-sqrt((float)((i-2)*(i-2) + (j-2)*(j-2)))/(stddev*stddev)); | |
165 | 165 | filter5_weights[i][j] = Color4f(w,w,w,0); |
166 | 166 | sum += 3 * w; |
167 | 167 | } |
@@ -183,8 +183,8 @@ int _tmain(int argc, _TCHAR* argv[]) | ||
183 | 183 | NULL, |
184 | 184 | &filter5_weights |
185 | 185 | }; |
186 | - spatial_color_quant(image, *filters[filter_size], quantized_image, palette, num_colors, coarse_variables, 1.0, 0.001, 3, 1); | |
187 | - | |
186 | + spatial_color_quant(image, *filters[filter_size], quantized_image, palette, num_colors, coarse_variables, 1.0, 0.001, 3, 1); | |
187 | + | |
188 | 188 | { |
189 | 189 | FILE* out = _tfopen(argv[2], _T("wb")); |
190 | 190 | if (out == NULL) { |
@@ -194,15 +194,17 @@ int _tmain(int argc, _TCHAR* argv[]) | ||
194 | 194 | unsigned char c[3] = {0,0,0}; |
195 | 195 | for (int y=0; y<height; y++) { |
196 | 196 | for (int x=0; x<width; x++) { |
197 | - c[2] = (unsigned char)(255*palette[quantized_image[y][x]][0]); | |
198 | - c[1] = (unsigned char)(255*palette[quantized_image[y][x]][1]); | |
199 | - c[0] = (unsigned char)(255*palette[quantized_image[y][x]][2]); | |
197 | + const uint8_t idx = quantized_image[y][x]; | |
198 | + Color4f col = palette[idx]; | |
199 | + c[2] = (unsigned char)(255*col[0]); | |
200 | + c[1] = (unsigned char)(255*col[1]); | |
201 | + c[0] = (unsigned char)(255*col[2]); | |
200 | 202 | fwrite(c, 3, 1, out); |
201 | 203 | } |
202 | 204 | } |
203 | 205 | fclose(out); |
204 | 206 | } |
205 | - | |
206 | - return 0; | |
207 | -} | |
208 | - | |
207 | + | |
208 | + return 0; | |
209 | +} | |
210 | + |
@@ -1,5 +1,5 @@ | ||
1 | -#include "stdafx.h" | |
2 | - | |
1 | +#include "stdafx.h" | |
2 | + | |
3 | 3 | #include <algorithm> |
4 | 4 | #include <math.h> |
5 | 5 | #include <stdio.h> |
@@ -8,14 +8,14 @@ | ||
8 | 8 | #include <deque> |
9 | 9 | #include <limits> |
10 | 10 | #include <iostream> |
11 | - | |
12 | -using namespace std; | |
13 | - | |
14 | -#include "Array.h" | |
15 | -#include "Color4f.h" | |
16 | - | |
17 | -typedef Array2D<Color4f> Image4f; | |
18 | - | |
11 | + | |
12 | +using namespace std; | |
13 | + | |
14 | +#include "Array.h" | |
15 | +#include "Color4f.h" | |
16 | + | |
17 | +typedef Array2D<Color4f> Image4f; | |
18 | + | |
19 | 19 | int compute_max_coarse_level(int width, int height) |
20 | 20 | { |
21 | 21 | // We want the coarsest layer to have at most MAX_PIXELS pixels |
@@ -29,23 +29,23 @@ int compute_max_coarse_level(int width, int height) | ||
29 | 29 | return result; |
30 | 30 | } |
31 | 31 | |
32 | -void fill_random(Array3D<double>& a) | |
32 | +void fill_random(Array3D<float>& a) | |
33 | 33 | { |
34 | 34 | for (int i=0; i<a.width_; i++) { |
35 | 35 | for (int j=0; j<a.height_; j++) { |
36 | 36 | for (int k=0; k<a.depth_; k++) { |
37 | - a[k][j][i] = ((double)rand())/RAND_MAX; | |
37 | + a[k][j][i] = ((float)rand())/RAND_MAX; | |
38 | 38 | } |
39 | 39 | } |
40 | 40 | } |
41 | -} | |
42 | - | |
43 | -double get_initial_temperature() | |
41 | +} | |
42 | + | |
43 | +float get_initial_temperature() | |
44 | 44 | { |
45 | 45 | return 2.0; // TODO: Figure out what to make this |
46 | 46 | } |
47 | 47 | |
48 | -double get_final_temperature() | |
48 | +float get_final_temperature() | |
49 | 49 | { |
50 | 50 | return 0.02; // TODO: Figure out what to make this |
51 | 51 | } |
@@ -81,7 +81,7 @@ void init_image(Image4f& image) | ||
81 | 81 | { |
82 | 82 | std::fill(image.pBuff_, image.pBuff_+image.width_*image.height_, Color4f(0.0f,0.0f,0.0f,0.0f)); |
83 | 83 | } |
84 | - | |
84 | + | |
85 | 85 | void compute_b_array( |
86 | 86 | const Image4f& filter_weights, |
87 | 87 | Image4f& b |
@@ -157,7 +157,7 @@ void sum_coarsen( | ||
157 | 157 | { |
158 | 158 | for (int y=0; y<coarse.height_; y++) { |
159 | 159 | for (int x=0; x<coarse.width_; x++) { |
160 | - double divisor = 1.0; | |
160 | + float divisor = 1.0; | |
161 | 161 | Color4f val = fine[y*2][x*2]; |
162 | 162 | if (x*2 + 1 < fine.width_) { |
163 | 163 | divisor += 1; val += fine[y*2][x*2 + 1]; |
@@ -195,7 +195,7 @@ vector<float> extract_vector_layer_1d(const Color4f* s, size_t sz, int k) | ||
195 | 195 | } |
196 | 196 | |
197 | 197 | int best_match_color( |
198 | - const Array3D<double>& vars, | |
198 | + const Array3D<float>& vars, | |
199 | 199 | int i_x, |
200 | 200 | int i_y, |
201 | 201 | const Color4f* palette, |
@@ -203,7 +203,7 @@ int best_match_color( | ||
203 | 203 | ) |
204 | 204 | { |
205 | 205 | int max_v = 0; |
206 | - double max_weight = vars[0][i_y][i_x]; | |
206 | + float max_weight = vars[0][i_y][i_x]; | |
207 | 207 | for (size_t v=1; v<num_colors; ++v) { |
208 | 208 | if (vars[v][i_y][i_x] > max_weight) { |
209 | 209 | max_v = v; |
@@ -213,7 +213,7 @@ int best_match_color( | ||
213 | 213 | return max_v; |
214 | 214 | } |
215 | 215 | |
216 | -void zoom_double(const Array3D<double>& small, Array3D<double>& big) | |
216 | +void zoom(const Array3D<float>& small, Array3D<float>& big) | |
217 | 217 | { |
218 | 218 | // Simple scaling of the weights array based on mixing the four |
219 | 219 | // pixels falling under each fine pixel, weighted by area. |
@@ -221,19 +221,19 @@ void zoom_double(const Array3D<double>& small, Array3D<double>& big) | ||
221 | 221 | // is 1.2 fine pixels wide and high. |
222 | 222 | for (int y=0; y<big.height_/2*2; y++) { |
223 | 223 | for (int x=0; x<big.width_/2*2; x++) { |
224 | - double left = max(0.0, (x-0.1)/2.0), right = min(small.width_-0.001, (x+1.1)/2.0); | |
225 | - double top = max(0.0, (y-0.1)/2.0), bottom = min(small.height_-0.001, (y+1.1)/2.0); | |
224 | + float left = max(0.0, (x-0.1)/2.0), right = min(small.width_-0.001, (x+1.1)/2.0); | |
225 | + float top = max(0.0, (y-0.1)/2.0), bottom = min(small.height_-0.001, (y+1.1)/2.0); | |
226 | 226 | int x_left = (int)floor(left), x_right = (int)floor(right); |
227 | 227 | int y_top = (int)floor(top), y_bottom = (int)floor(bottom); |
228 | - double area = (right-left)*(bottom-top); | |
229 | - double top_left_weight = (ceil(left) - left)*(ceil(top) - top)/area; | |
230 | - double top_right_weight = (right - floor(right))*(ceil(top) - top)/area; | |
231 | - double bottom_left_weight = (ceil(left) - left)*(bottom - floor(bottom))/area; | |
232 | - double bottom_right_weight = (right - floor(right))*(bottom - floor(bottom))/area; | |
233 | - double top_weight = (right-left)*(ceil(top) - top)/area; | |
234 | - double bottom_weight = (right-left)*(bottom - floor(bottom))/area; | |
235 | - double left_weight = (bottom-top)*(ceil(left) - left)/area; | |
236 | - double right_weight = (bottom-top)*(right - floor(right))/area; | |
228 | + float area = (right-left)*(bottom-top); | |
229 | + float top_left_weight = (ceil(left) - left)*(ceil(top) - top)/area; | |
230 | + float top_right_weight = (right - floor(right))*(ceil(top) - top)/area; | |
231 | + float bottom_left_weight = (ceil(left) - left)*(bottom - floor(bottom))/area; | |
232 | + float bottom_right_weight = (right - floor(right))*(bottom - floor(bottom))/area; | |
233 | + float top_weight = (right-left)*(ceil(top) - top)/area; | |
234 | + float bottom_weight = (right-left)*(bottom - floor(bottom))/area; | |
235 | + float left_weight = (bottom-top)*(ceil(left) - left)/area; | |
236 | + float right_weight = (bottom-top)*(right - floor(right))/area; | |
237 | 237 | for (int z=0; z<big.depth_; z++) { |
238 | 238 | if (x_left == x_right && y_top == y_bottom) { |
239 | 239 | big[z][y][x] = small[z][y_top][x_left]; |
@@ -256,7 +256,7 @@ void zoom_double(const Array3D<double>& small, Array3D<double>& big) | ||
256 | 256 | |
257 | 257 | void compute_initial_s( |
258 | 258 | Image4f& s, |
259 | - const Array3D<double>& coarse_variables, | |
259 | + const Array3D<float>& coarse_variables, | |
260 | 260 | const Image4f& b |
261 | 261 | ) |
262 | 262 | { |
@@ -282,7 +282,7 @@ void compute_initial_s( | ||
282 | 282 | Color4f b_ij = b_value(b,i_x,i_y,j_x,j_y); |
283 | 283 | for (int v=0; v<palette_size; v++) { |
284 | 284 | for (int alpha=v; alpha<palette_size; alpha++) { |
285 | - double mult = coarse_variables[v][i_y][i_x] * coarse_variables[alpha][j_y][j_x]; | |
285 | + float mult = coarse_variables[v][i_y][i_x] * coarse_variables[alpha][j_y][j_x]; | |
286 | 286 | s[alpha][v][0] += mult * b_ij[0]; |
287 | 287 | s[alpha][v][1] += mult * b_ij[1]; |
288 | 288 | s[alpha][v][2] += mult * b_ij[2]; |
@@ -299,12 +299,12 @@ void compute_initial_s( | ||
299 | 299 | |
300 | 300 | void update_s( |
301 | 301 | Image4f& s, |
302 | - const Array3D<double>& coarse_variables, | |
302 | + const Array3D<float>& coarse_variables, | |
303 | 303 | const Image4f& b, |
304 | 304 | int j_x, |
305 | 305 | int j_y, |
306 | 306 | int alpha, |
307 | - double delta | |
307 | + float delta | |
308 | 308 | ) |
309 | 309 | { |
310 | 310 | int palette_size = s.width_; |
@@ -318,13 +318,13 @@ void update_s( | ||
318 | 318 | Color4f delta_b_ij = delta*b_value(b,i_x,i_y,j_x,j_y); |
319 | 319 | if (i_x == j_x && i_y == j_y) continue; |
320 | 320 | for (int v=0; v <= alpha; v++) { |
321 | - double mult = coarse_variables[v][i_y][i_x]; | |
321 | + float mult = coarse_variables[v][i_y][i_x]; | |
322 | 322 | s[alpha][v][0] += mult * delta_b_ij[0]; |
323 | 323 | s[alpha][v][1] += mult * delta_b_ij[1]; |
324 | 324 | s[alpha][v][2] += mult * delta_b_ij[2]; |
325 | 325 | } |
326 | 326 | for (int v=alpha; v<palette_size; v++) { |
327 | - double mult = coarse_variables[v][i_y][i_x]; | |
327 | + float mult = coarse_variables[v][i_y][i_x]; | |
328 | 328 | s[v][alpha][0] += mult * delta_b_ij[0]; |
329 | 329 | s[v][alpha][1] += mult * delta_b_ij[1]; |
330 | 330 | s[v][alpha][2] += mult * delta_b_ij[2]; |
@@ -336,7 +336,7 @@ void update_s( | ||
336 | 336 | |
337 | 337 | void refine_palette( |
338 | 338 | Image4f& s, |
339 | - const Array3D<double>& coarse_variables, | |
339 | + const Array3D<float>& coarse_variables, | |
340 | 340 | const Image4f& a, |
341 | 341 | Color4f* palette, |
342 | 342 | size_t num_colors |
@@ -354,7 +354,7 @@ void refine_palette( | ||
354 | 354 | Color4f sum(0,0,0,0); |
355 | 355 | for (int i_y=0; i_y<coarse_variables.height_; i_y++) { |
356 | 356 | for (int i_x=0; i_x<coarse_variables.width_; i_x++) { |
357 | - double cv = coarse_variables[v][i_y][i_x]; | |
357 | + float cv = coarse_variables[v][i_y][i_x]; | |
358 | 358 | Color4f av = a[i_y][i_x]; |
359 | 359 | Color4f result = cv * av; |
360 | 360 | sum += result; |
@@ -368,7 +368,7 @@ void refine_palette( | ||
368 | 368 | vector<float> R_k = extract_vector_layer_1d(&r[0], num_colors, k); |
369 | 369 | vector<float> palette_channel = -1.0f*((2.0f*S_k).matrix_inverse())*R_k; |
370 | 370 | for (unsigned int v=0; v<num_colors; v++) { |
371 | - double val = palette_channel[v]; | |
371 | + float val = palette_channel[v]; | |
372 | 372 | if (val < 0) val = 0; |
373 | 373 | if (val > 1) val = 1; |
374 | 374 | palette[v][k] = val; |
@@ -384,7 +384,7 @@ void refine_palette( | ||
384 | 384 | |
385 | 385 | void compute_initial_j_palette_sum( |
386 | 386 | Image4f& j_palette_sum, |
387 | - const Array3D<double>& coarse_variables, | |
387 | + const Array3D<float>& coarse_variables, | |
388 | 388 | const Color4f* palette, |
389 | 389 | size_t num_colors |
390 | 390 | ) |
@@ -405,23 +405,23 @@ void spatial_color_quant( | ||
405 | 405 | Image4f& filter_weights, |
406 | 406 | Array2D<uint8_t>& quantized_image, |
407 | 407 | Color4f* palette, size_t num_colors, |
408 | - Array3D<double>*& p_coarse_variables, | |
409 | - double initial_temperature, | |
410 | - double final_temperature, | |
408 | + Array3D<float>*& p_coarse_variables, | |
409 | + float initial_temperature, | |
410 | + float final_temperature, | |
411 | 411 | int temps_per_level, |
412 | 412 | int repeats_per_temp |
413 | 413 | ) |
414 | -{ | |
414 | +{ | |
415 | 415 | int max_coarse_level = //1; |
416 | 416 | compute_max_coarse_level(image.width_, image.height_); |
417 | 417 | size_t width2 = image.width_ >> max_coarse_level; |
418 | 418 | size_t height2 = image.height_ >> max_coarse_level; |
419 | - p_coarse_variables = new Array3D<double>(width2, height2, num_colors); | |
419 | + p_coarse_variables = new Array3D<float>(width2, height2, num_colors); | |
420 | 420 | // For syntactic convenience |
421 | - Array3D<double>& coarse_variables = *p_coarse_variables; | |
421 | + Array3D<float>& coarse_variables = *p_coarse_variables; | |
422 | 422 | fill_random(coarse_variables); |
423 | 423 | |
424 | - double temperature = initial_temperature; | |
424 | + float temperature = initial_temperature; | |
425 | 425 | |
426 | 426 | // Compute a_i, b_{ij} according to (11) |
427 | 427 | int extended_neighborhood_width = filter_weights.width_*2 - 1; |
@@ -463,12 +463,12 @@ void spatial_color_quant( | ||
463 | 463 | Image4f& ai = *p_ai; |
464 | 464 | sum_coarsen(*a_vec.back(), ai); |
465 | 465 | a_vec.push_back(p_ai); |
466 | - } | |
467 | - | |
466 | + } | |
467 | + | |
468 | 468 | // Multiscale annealing |
469 | 469 | coarse_level = max_coarse_level; |
470 | 470 | const int iters_per_level = temps_per_level; |
471 | - double temperature_multiplier = pow(final_temperature/initial_temperature, 1.0/(max(3, max_coarse_level*iters_per_level))); | |
471 | + float temperature_multiplier = pow(final_temperature/initial_temperature, 1.0f/(max(3, max_coarse_level*iters_per_level))); | |
472 | 472 | #if TRACE |
473 | 473 | cout << "Temperature multiplier: " << temperature_multiplier << endl; |
474 | 474 | #endif |
@@ -481,7 +481,7 @@ void spatial_color_quant( | ||
481 | 481 | compute_initial_j_palette_sum(*j_palette_sum, coarse_variables, palette, num_colors); |
482 | 482 | while (coarse_level >= 0 || temperature > final_temperature) { |
483 | 483 | // Need to reseat this reference in case we changed p_coarse_variables |
484 | - Array3D<double>& coarse_variables = *p_coarse_variables; | |
484 | + Array3D<float>& coarse_variables = *p_coarse_variables; | |
485 | 485 | Image4f& a = *a_vec[coarse_level]; |
486 | 486 | Image4f& b = *b_vec[coarse_level]; |
487 | 487 | Color4f middle_b = b_value(b,0,0,0,0); |
@@ -553,7 +553,7 @@ void spatial_color_quant( | ||
553 | 553 | // Prevent the matrix S from becoming singular |
554 | 554 | if (new_val <= 0) new_val = 1e-10; |
555 | 555 | if (new_val >= 1) new_val = 1 - 1e-10; |
556 | - double delta_m_iv = new_val - coarse_variables[v][i_y][i_x]; | |
556 | + float delta_m_iv = new_val - coarse_variables[v][i_y][i_x]; | |
557 | 557 | coarse_variables[v][i_y][i_x] = new_val; |
558 | 558 | j_pal[0] += delta_m_iv*palette[v][0]; |
559 | 559 | j_pal[1] += delta_m_iv*palette[v][1]; |
@@ -612,11 +612,11 @@ void spatial_color_quant( | ||
612 | 612 | { |
613 | 613 | coarse_level--; |
614 | 614 | if (coarse_level < 0) break; |
615 | - Array3D<double>* p_new_coarse_variables = new Array3D<double>( | |
615 | + Array3D<float>* p_new_coarse_variables = new Array3D<float>( | |
616 | 616 | image.width_ >> coarse_level, |
617 | 617 | image.height_ >> coarse_level, |
618 | 618 | num_colors); |
619 | - zoom_double(coarse_variables, *p_new_coarse_variables); | |
619 | + zoom(coarse_variables, *p_new_coarse_variables); | |
620 | 620 | delete p_coarse_variables; |
621 | 621 | p_coarse_variables = p_new_coarse_variables; |
622 | 622 | iters_at_current_level = 0; |
@@ -636,19 +636,19 @@ void spatial_color_quant( | ||
636 | 636 | // This is normally not used, but is handy sometimes for debugging |
637 | 637 | while (coarse_level > 0) { |
638 | 638 | coarse_level--; |
639 | - Array3D<double>* p_new_coarse_variables = new Array3D<double>( | |
639 | + Array3D<float>* p_new_coarse_variables = new Array3D<float>( | |
640 | 640 | image.width_ >> coarse_level, |
641 | 641 | image.height_ >> coarse_level, |
642 | 642 | num_colors |
643 | 643 | ); |
644 | - zoom_double(*p_coarse_variables, *p_new_coarse_variables); | |
644 | + zoom(*p_coarse_variables, *p_new_coarse_variables); | |
645 | 645 | delete p_coarse_variables; |
646 | 646 | p_coarse_variables = p_new_coarse_variables; |
647 | 647 | } |
648 | 648 | |
649 | 649 | { |
650 | 650 | // Need to reseat this reference in case we changed p_coarse_variables |
651 | - Array3D<double>& coarse_variables = *p_coarse_variables; | |
651 | + Array3D<float>& coarse_variables = *p_coarse_variables; | |
652 | 652 | |
653 | 653 | for (int i_x = 0; i_x < image.width_; i_x++) { |
654 | 654 | for (int i_y = 0; i_y < image.height_; i_y++) { |
@@ -666,6 +666,6 @@ void spatial_color_quant( | ||
666 | 666 | #endif |
667 | 667 | } |
668 | 668 | } |
669 | - | |
670 | -} | |
671 | - | |
669 | + | |
670 | +} | |
671 | + |
@@ -5,9 +5,9 @@ void spatial_color_quant( | ||
5 | 5 | Image4f& filter_weights, |
6 | 6 | Array2D<uint8_t>& quantized_image, |
7 | 7 | Color4f* palette, size_t num_colors, |
8 | - Array3D<double>*& p_coarse_variables, | |
9 | - double initial_temperature, | |
10 | - double final_temperature, | |
8 | + Array3D<float>*& p_coarse_variables, | |
9 | + float initial_temperature, | |
10 | + float final_temperature, | |
11 | 11 | int temps_per_level, |
12 | 12 | int repeats_per_temp |
13 | 13 | ); |