forked from kennberg/fractal-compression
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQuadTreeEncoder.cpp
399 lines (332 loc) · 12 KB
/
QuadTreeEncoder.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Fractal Image Compression. Copyright 2004 Alex Kennberg.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <time.h>
#include <cstring>
#include <cstdint>
#include <stdint.h>
#include <omp.h>
#include "counters.h"
using namespace std;
#include "Image.h"
#include "IFSTransform.h"
#include "Encoder.h"
#include "QuadTreeEncoder.h"
#include "count_ops.h"
#define use_openmp true
#define memoize true
extern int verb;
extern bool useYCbCr;
#define BUFFER_SIZE (16)
#if memoize
#define IFS_EXECUTE_NEW
#endif
QuadTreeEncoder::QuadTreeEncoder(int threshold, bool symmetry)
{
this->threshold = threshold;
this->symmetry = symmetry;
}
QuadTreeEncoder::~QuadTreeEncoder()
{
}
Transforms* QuadTreeEncoder::Encode(Image* source)
{
//Initialize a hardware counter
hwCounter_t cl;
cl.init = false;
initTicks(cl);
Transforms* transforms = new Transforms;
img.width = source->GetWidth();
img.height = source->GetHeight();
img.channels = source->GetChannels();
transforms->channels = img.channels;
omp_set_num_threads(N_THREADS);
#ifndef IFS_EXECUTE_NEW
buffers = new PixelValue*[N_THREADS];
for (int i = 0; i < N_THREADS; i++){
buffers[i] = new PixelValue[BUFFER_SIZE * BUFFER_SIZE];
}
#endif
/*
The following code allocates space for the
data from IFS->execute().
*/
#ifdef IFS_EXECUTE_NEW
int dim = (img.width * img.height) / 4;
executePixels[0] = new PixelValue[dim];
executePixels[1] = new PixelValue[dim];
executePixels[2] = new PixelValue[dim];
executePixels[3] = new PixelValue[dim];
averagePixels[0] = new PixelValue[(img.width / 4) * (img.height / 4)];
averagePixels[1] = new PixelValue[(img.width / 8) * (img.height / 8)];
averagePixels[2] = new PixelValue[(img.width / 16) * (img.height / 16)];
averagePixels[3] = new PixelValue[(img.width / 32) * (img.height / 32)];
#endif
for (int channel = 1; channel <= img.channels; channel++)
{
// Load image into a local copy
img.imagedata = new PixelValue[img.width * img.height];
source->GetChannelData(channel, img.imagedata, img.width * img.height);
if (img.width % 32 != 0 || img.height %32 != 0)
{
printf("Error: Image must have dimensions that are multiples of 32.\n");
exit(-1);
}
// Make second channel the downsampled version of the image.
//Get time before
img.imagedata2 = IFSTransform::DownSample(img.imagedata, img.width, 0, 0, img.width / 2);
// When using YCbCr we can reduce the quality of colour, because the eye
// is more sensitive to intensity which is channel 1.
if (channel >= 2 && useYCbCr)
threshold *= 2;
/*
Build up buffers for IFS->execute()
Block-sizes = 2, 4, 8, 16 (max -> BUFFER_SIZE)
*/
#ifdef IFS_EXECUTE_NEW
executeIFS(2);
executeIFS(4);
executeIFS(8);
executeIFS(16);
#endif
// Go through all the range blocks
#if use_openmp
#pragma omp parallel for schedule(dynamic)
#endif
for (int y = 0; y < img.height; y += BUFFER_SIZE)
{
for (int x = 0; x < img.width; x += BUFFER_SIZE)
{
//printf("****Buffer Size: %d\n", BUFFER_SIZE);
findMatchesFor(transforms->ch[channel-1], x, y, BUFFER_SIZE);
printf(".");
}
printf("\n");
}
//elapsed = getTicks(cl) - current_time;
//printf("Number of Cycles required to take findBestMatch: %lu\n", elapsed);
// Bring the threshold back to original.
if (channel >= 2 && useYCbCr)
threshold /= 2;
delete []img.imagedata2;
img.imagedata2 = NULL;
delete []img.imagedata;
img.imagedata = NULL;
printf("\n");
}
#ifdef IFS_EXECUTE_NEW
delete[] executePixels[0];
delete[] executePixels[1];
delete[] executePixels[2];
delete[] executePixels[3];
#endif
return transforms;
}
void QuadTreeEncoder::executeIFS(int blockSize) {
int index = 0;
switch(blockSize){
case 4:
index = 1;
break;
case 8:
index = 2;
break;
case 16:
index = 3;
break;
}
PixelValue *ptr = executePixels[index];
int pixelCount = blockSize * blockSize;
PixelValue *avg = averagePixels[index];
for (int y = 0; y < img.height; y += blockSize * 2) {
for (int x = 0; x < img.width; x += blockSize * 2) {
/* IFS */
IFSTransform::SYM symmetryEnum = (IFSTransform::SYM)symmetry;
IFSTransform *ifs = new IFSTransform(x, y, 0, 0, blockSize, symmetryEnum, 1.0, 0);
*avg = ifs->Execute(img.imagedata2, img.width / 2, ptr, blockSize, true);
/* Shift pointer */
ptr += pixelCount;
avg += 1;
}
}
}
#ifdef IFS_EXECUTE_NEW
// new version of QuadTreeEncoder::findMatchesFor
void QuadTreeEncoder::findMatchesFor(Transform& transforms, int toX, int toY, int blockSize)
{
int bestX = 0;
int bestY = 0;
int bestOffset = 0;
IFSTransform::SYM bestSymmetry = IFSTransform::SYM_NONE;
double bestScale = 0;
double bestError = 1e9;
// Get average pixel for the range block
int rangeAvg = GetAveragePixel(img.imagedata, img.width, toX, toY, blockSize);
int index = 3;
switch(blockSize){
case 2:
index = 0;
break;
case 4:
index = 1;
break;
case 8:
index = 2;
break;
}
PixelValue *buffer = executePixels[index];
int pixelCount = blockSize * blockSize;
PixelValue *avg = averagePixels[index];
// Go through all the downsampled domain blocks
for (int y = 0; y < img.height; y += blockSize * 2)
{
for (int x = 0; x < img.width; x += blockSize * 2)
{
// Get average pixel for the downsampled domain block
int domainAvg = *avg;
// Get scale and offset
double scale = GetScaleFactor(img.imagedata, img.width, toX, toY, domainAvg,
buffer, blockSize, 0, 0, rangeAvg, blockSize);
int offset = (int)(rangeAvg - scale * (double)domainAvg);
// Get error and compare to best error so far
double error = GetError(buffer, blockSize, 0, 0, domainAvg,
img.imagedata, img.width, toX, toY, rangeAvg, blockSize, scale);
if (error < bestError){
bestError = error;
bestX = x;
bestY = y;
bestSymmetry = (IFSTransform::SYM)0;
bestScale = scale;
bestOffset = offset;
}
buffer += pixelCount;
avg += 1;
}
}
if (blockSize > 2 && bestError >= threshold)
{
// Recurse into the four corners of the current block.
blockSize /= 2;
findMatchesFor(transforms, toX, toY, blockSize);
findMatchesFor(transforms, toX + blockSize, toY, blockSize);
findMatchesFor(transforms, toX, toY + blockSize, blockSize);
findMatchesFor(transforms, toX + blockSize, toY + blockSize, blockSize);
}
else
{
// Use this transformation
IFSTransform* new_transform = new IFSTransform(bestX, bestY,
toX, toY,
blockSize,
bestSymmetry,
bestScale,
bestOffset);
#pragma omp critical
{
transforms.push_back(new_transform);
}
}
}
#else
// old version of QuadTreeEncoder::findMatchesFor
void QuadTreeEncoder::findMatchesFor(Transform& transforms, int toX, int toY, int blockSize)
{
int bestX = 0;
int bestY = 0;
int bestOffset = 0;
IFSTransform::SYM bestSymmetry = IFSTransform::SYM_NONE;
double bestScale = 0;
double bestError = 1e9;
//PixelValue* buffer = new PixelValue[blockSize * blockSize];
// Get average pixel for the range block
int rangeAvg = GetAveragePixel(img.imagedata, img.width, toX, toY, blockSize);
// Go through all the downsampled domain blocks
for (int y = 0; y < img.height; y += blockSize * 2)
{
for (int x = 0; x < img.width; x += blockSize * 2)
{
INC_OP(3);
PixelValue* buffer = buffers[omp_get_thread_num()];
for (int symmetry = 0; symmetry < IFSTransform::SYM_MAX; symmetry++)
{
IFSTransform::SYM symmetryEnum = (IFSTransform::SYM)symmetry;
IFSTransform* ifs = new IFSTransform(x, y, 0, 0, blockSize, symmetryEnum, 1.0, 0);
INC_OP(1);
ifs->Execute(img.imagedata2, img.width / 2, buffer, blockSize, true);
int domainAvg = GetAveragePixel(buffer, blockSize, 0, 0, blockSize);
// Get scale and offset
double scale = GetScaleFactor(img.imagedata, img.width, toX, toY, domainAvg,
buffer, blockSize, 0, 0, rangeAvg, blockSize);
int offset = (int)(rangeAvg - scale * (double)domainAvg);
// Get error and compare to best error so far
double error = GetError(buffer, blockSize, 0, 0, domainAvg,
img.imagedata, img.width, toX, toY, rangeAvg, blockSize, scale);
INC_OP(1);
if (error < bestError)
{
bestError = error;
bestX = x;
bestY = y;
bestSymmetry = symmetryEnum;
bestScale = scale;
bestOffset = offset;
}
delete ifs;
if (!symmetry)
break;
}
}
}
if (blockSize > 2 && bestError >= threshold)
{
// Recurse into the four corners of the current block.
blockSize /= 2;
findMatchesFor(transforms, toX, toY, blockSize);
findMatchesFor(transforms, toX + blockSize, toY, blockSize);
findMatchesFor(transforms, toX, toY + blockSize, blockSize);
findMatchesFor(transforms, toX + blockSize, toY + blockSize, blockSize);
}
else
{
// Use this transformation
IFSTransform* new_transform = new IFSTransform(
bestX, bestY,
toX, toY,
blockSize,
bestSymmetry,
bestScale,
bestOffset
);
#pragma omp critical
{
transforms.push_back(new_transform);
}
INC_OP(1);
if (verb >= 1)
{
printf("to=(%d, %d)\n", toX, toY);
printf("from=(%d, %d)\n", bestX, bestY);
printf("best error=%lf\n", bestError);
printf("best symmetry=%d\n", (int)bestSymmetry);
printf("best offset=%d\n", bestOffset);
printf("best scale=%lf\n", bestScale);
}
}
}
#endif //end old version of QuadTreeEncoder::findMatchesFor