-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQuadTreeEncoder.cpp
188 lines (159 loc) · 5.16 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
/*
* 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>
using namespace std;
#include "Image.h"
#include "IFSTransform.h"
#include "Encoder.h"
#include "QuadTreeEncoder.h"
extern int verb;
extern bool useYCbCr;
#define BUFFER_SIZE (16)
QuadTreeEncoder::QuadTreeEncoder(int threshold, bool symmetry)
{
this->threshold = threshold;
this->symmetry = symmetry;
}
QuadTreeEncoder::~QuadTreeEncoder()
{
}
Transforms* QuadTreeEncoder::Encode(Image* source)
{
Transforms* transforms = new Transforms;
img.width = source->GetWidth();
img.height = source->GetHeight();
img.channels = source->GetChannels();
transforms->channels = img.channels;
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.
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;
// Go through all the range blocks
for (int y = 0; y < img.height; y += BUFFER_SIZE)
{
for (int x = 0; x < img.width; x += BUFFER_SIZE)
{
findMatchesFor(transforms->ch[channel-1], x, y, BUFFER_SIZE);
printf(".");
}
printf("\n");
}
// 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");
}
return transforms;
}
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)
{
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);
ifs->Execute(img.imagedata2, img.width / 2, buffer, blockSize, true);
// Get average pixel for the downsampled domain block
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);
if (error < bestError)
{
bestError = error;
bestX = x;
bestY = y;
bestSymmetry = symmetryEnum;
bestScale = scale;
bestOffset = offset;
}
delete ifs;
if (!symmetry)
break;
}
}
}
delete []buffer;
buffer = NULL;
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
transforms.push_back(
new IFSTransform(
bestX, bestY,
toX, toY,
blockSize,
bestSymmetry,
bestScale,
bestOffset
)
);
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);
}
}
}