Repository navigation
Expand file tree
/
Copy path2passscale.h
More file actions
439 lines (397 loc) · 13.8 KB
/
Copy path2passscale.h
File metadata and controls
439 lines (397 loc) · 13.8 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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#ifndef _2_PASS_SCALE_H_
#define _2_PASS_SCALE_H_
// Modified by M.Brent for 3-byte RGB values instead of 4-byte COLORREFs
#include <math.h>
#include "Filters.h"
typedef struct
{
double *Weights; // Normalized weights of neighboring pixels
int Left,Right; // Bounds of source pixels window
} ContributionType; // Contirbution information for a single pixel
typedef struct
{
ContributionType *ContribRow; // Row (or column) of contribution weights
UINT WindowSize, // Filter window size (of affecting source pixels)
LineLength; // Length of line (no. or rows / cols)
} LineContribType; // Contribution information for an entire line (row or column)
typedef BOOL (*ProgressAnbAbortCallBack)(BYTE bPercentComplete);
template <class FilterClass>
class C2PassScale
{
public:
C2PassScale (ProgressAnbAbortCallBack callback = NULL) :
m_Callback (callback) {}
virtual ~C2PassScale() {}
unsigned char *AllocAndScale (
unsigned char *pOrigImage,
UINT uOrigWidth,
UINT uOrigHeight,
UINT uNewWidth,
UINT uNewHeight);
unsigned char *Scale (
unsigned char *pOrigImage,
UINT uOrigWidth,
UINT uOrigHeight,
unsigned char *pDstImage,
UINT uNewWidth,
UINT uNewHeight);
private:
ProgressAnbAbortCallBack m_Callback;
BOOL m_bCanceled;
LineContribType *AllocContributions ( UINT uLineLength,
UINT uWindowSize);
void FreeContributions (LineContribType * p);
LineContribType *CalcContributions ( UINT uLineSize,
UINT uSrcSize,
double dScale);
void ScaleRow ( unsigned char *pSrc,
UINT uSrcWidth,
unsigned char *pRes,
UINT uResWidth,
UINT uRow,
LineContribType *Contrib);
void HorizScale ( unsigned char *pSrc,
UINT uSrcWidth,
UINT uSrcHeight,
unsigned char *pDst,
UINT uResWidth,
UINT uResHeight);
void ScaleCol ( unsigned char *pSrc,
UINT uSrcWidth,
unsigned char *pRes,
UINT uResWidth,
UINT uResHeight,
UINT uCol,
LineContribType *Contrib);
void VertScale ( unsigned char *pSrc,
UINT uSrcWidth,
UINT uSrcHeight,
unsigned char *pDst,
UINT uResWidth,
UINT uResHeight);
};
template <class FilterClass>
LineContribType *
C2PassScale<FilterClass>::
AllocContributions (UINT uLineLength, UINT uWindowSize)
{
LineContribType *res = new LineContribType;
// Init structure header
res->WindowSize = uWindowSize;
res->LineLength = uLineLength;
// Allocate list of contributions
res->ContribRow = new ContributionType[uLineLength];
for (UINT u = 0 ; u < uLineLength ; u++)
{
// Allocate contributions for every pixel
res->ContribRow[u].Weights = new double[uWindowSize];
}
return res;
}
template <class FilterClass>
void
C2PassScale<FilterClass>::
FreeContributions (LineContribType * p)
{
for (UINT u = 0; u < p->LineLength; u++)
{
// Free contribs for every pixel
delete [] p->ContribRow[u].Weights;
}
delete [] p->ContribRow; // Free list of pixels contribs
delete p; // Free contribs header
}
template <class FilterClass>
LineContribType *
C2PassScale<FilterClass>::
CalcContributions (UINT uLineSize, UINT uSrcSize, double dScale)
{
FilterClass CurFilter;
double dWidth;
double dFScale = 1.0;
double dFilterWidth = CurFilter.GetWidth();
if (dScale < 1.0)
{ // Minification
if (dScale == 0) dScale = 1;
dWidth = dFilterWidth / dScale;
dFScale = dScale;
}
else
{ // Magnification
dWidth= dFilterWidth;
}
// Window size is the number of sampled pixels
int iWindowSize = 2 * (int)ceil((double)dWidth) + 1;
// Allocate a new line contributions strucutre
LineContribType *res = AllocContributions (uLineSize, iWindowSize);
for (UINT u = 0; u < uLineSize; u++)
{ // Scan through line of contributions
double dCenter = (double)u / dScale; // Reverse mapping
// Find the significant edge points that affect the pixel
int iLeft = max (0, (int)floor ((double)dCenter - dWidth));
int iRight = min ((int)ceil ((double)dCenter + dWidth), int(uSrcSize) - 1);
// Cut edge points to fit in filter window in case of spill-off
if (iRight - iLeft + 1 > iWindowSize)
{
if (iLeft < (int(uSrcSize) - 1 / 2))
{
iLeft++;
}
else
{
iRight--;
}
}
res->ContribRow[u].Left = iLeft;
res->ContribRow[u].Right = iRight;
double dTotalWeight = 0.0; // Zero sum of weights
int iSrc;
for (iSrc = iLeft; iSrc <= iRight; iSrc++)
{ // Calculate weights
dTotalWeight += (res->ContribRow[u].Weights[iSrc-iLeft] =
dFScale * CurFilter.Filter (dFScale * (dCenter - (double)iSrc)));
}
ASSERT (dTotalWeight >= 0.0); // An error in the filter function can cause this
if (dTotalWeight > 0.0)
{ // Normalize weight of neighbouring points
for (iSrc = iLeft; iSrc <= iRight; iSrc++)
{ // Normalize point
res->ContribRow[u].Weights[iSrc-iLeft] /= dTotalWeight;
}
}
}
return res;
}
template <class FilterClass>
void
C2PassScale<FilterClass>::
ScaleRow ( unsigned char *pSrc,
UINT uSrcWidth,
unsigned char *pRes,
UINT uResWidth,
UINT uRow,
LineContribType *Contrib)
{
unsigned char *pSrcRow = &(pSrc[uRow * uSrcWidth * 3]);
unsigned char *pDstRow = &(pRes[uRow * uResWidth * 3]);
for (UINT x = 0; x < uResWidth; x++)
{ // Loop through row
float r = 0;
float g = 0;
float b = 0;
int iLeft = Contrib->ContribRow[x].Left; // Retrieve left boundries
int iRight = Contrib->ContribRow[x].Right; // Retrieve right boundries
for (int i = iLeft; i <= iRight; i++)
{ // Scan between boundries
// Accumulate weighted effect of each neighboring pixel
r += (float)(Contrib->ContribRow[x].Weights[i-iLeft] * (double)(pSrcRow[i*3]));
g += (float)(Contrib->ContribRow[x].Weights[i-iLeft] * (double)(pSrcRow[i*3+1]));
b += (float)(Contrib->ContribRow[x].Weights[i-iLeft] * (double)(pSrcRow[i*3+2]));
}
pDstRow[x*3] = (BYTE)(r+0.5)&0xff; // Place result in destination pixel
pDstRow[x*3+1]=(BYTE)(g+0.5)&0xff;
pDstRow[x*3+2]=(BYTE)(b+0.5)&0xff;
}
}
template <class FilterClass>
void
C2PassScale<FilterClass>::
HorizScale ( unsigned char *pSrc,
UINT uSrcWidth,
UINT uSrcHeight,
unsigned char *pDst,
UINT uResWidth,
UINT uResHeight)
{
TRACE ("Performing horizontal scaling...\n");
if (uResWidth == uSrcWidth)
{ // No scaling required, just copy
memcpy (pDst, pSrc, 3 * uSrcHeight * uSrcWidth);
return;
}
// Allocate and calculate the contributions
LineContribType * Contrib = CalcContributions (uResWidth, uSrcWidth, double(uResWidth) / double(uSrcWidth));
for (UINT u = 0; u < uResHeight; u++)
{ // Step through rows
if (NULL != m_Callback)
{
//
// Progress and report callback supplied
//
if (!m_Callback (BYTE(double(u) / double (uResHeight) * 50.0)))
{
//
// User wished to abort now
//
m_bCanceled = TRUE;
FreeContributions (Contrib); // Free contributions structure
return;
}
}
ScaleRow ( pSrc,
uSrcWidth,
pDst,
uResWidth,
u,
Contrib); // Scale each row
}
FreeContributions (Contrib); // Free contributions structure
}
template <class FilterClass>
void
C2PassScale<FilterClass>::
ScaleCol ( unsigned char *pSrc,
UINT uSrcWidth,
unsigned char *pRes,
UINT uResWidth,
UINT uResHeight,
UINT uCol,
LineContribType *Contrib)
{
for (UINT y = 0; y < uResHeight; y++)
{ // Loop through column
float r = 0;
float g = 0;
float b = 0;
int iLeft = Contrib->ContribRow[y].Left; // Retrieve left boundries
int iRight = Contrib->ContribRow[y].Right; // Retrieve right boundries
for (int i = iLeft; i <= iRight; i++)
{ // Scan between boundries
// Accumulate weighted effect of each neighboring pixel
unsigned char *pCurSrc = &pSrc[(i * uSrcWidth + uCol)*3];
r += float(Contrib->ContribRow[y].Weights[i-iLeft] * (double)(*pCurSrc));
g += float(Contrib->ContribRow[y].Weights[i-iLeft] * (double)(*(pCurSrc+1)));
b += float(Contrib->ContribRow[y].Weights[i-iLeft] * (double)(*(pCurSrc+2)));
}
int off=(y*uResWidth+uCol)*3;
pRes[off] = (BYTE)(r+0.5)&0xff; // Place result in destination pixel
pRes[off+1]=(BYTE)(g+0.5)&0xff;
pRes[off+2]=(BYTE)(b+0.5)&0xff;
}
}
template <class FilterClass>
void
C2PassScale<FilterClass>::
VertScale ( unsigned char *pSrc,
UINT uSrcWidth,
UINT uSrcHeight,
unsigned char *pDst,
UINT uResWidth,
UINT uResHeight)
{
TRACE ("Performing vertical scaling...\n");
if (uSrcHeight == uResHeight)
{ // No scaling required, just copy
memcpy (pDst, pSrc, 3 * uSrcHeight * uSrcWidth);
return;
}
// Allocate and calculate the contributions
LineContribType * Contrib = CalcContributions (uResHeight, uSrcHeight, double(uResHeight) / double(uSrcHeight));
for (UINT u = 0; u < uResWidth; u++)
{ // Step through columns
if (NULL != m_Callback)
{
//
// Progress and report callback supplied
//
if (!m_Callback (BYTE(double(u) / double (uResWidth) * 50.0) + 50))
{
//
// User wished to abort now
//
m_bCanceled = TRUE;
FreeContributions (Contrib); // Free contributions structure
return;
}
}
ScaleCol ( pSrc,
uSrcWidth,
pDst,
uResWidth,
uResHeight,
u,
Contrib); // Scale each column
}
FreeContributions (Contrib); // Free contributions structure
}
template <class FilterClass>
unsigned char *
C2PassScale<FilterClass>::
AllocAndScale (
unsigned char *pOrigImage,
UINT uOrigWidth,
UINT uOrigHeight,
UINT uNewWidth,
UINT uNewHeight)
{
// Scale source image horizontally into temporary image
m_bCanceled = FALSE;
unsigned char *pTemp = (unsigned char*)malloc(uNewWidth * uOrigHeight * 3);
HorizScale (pOrigImage,
uOrigWidth,
uOrigHeight,
pTemp,
uNewWidth,
uOrigHeight);
if (m_bCanceled)
{
delete [] pTemp;
return NULL;
}
// Scale temporary image vertically into result image
unsigned char *pRes = (unsigned char*)malloc(uNewWidth * uNewHeight * 3);
VertScale ( pTemp,
uNewWidth,
uOrigHeight,
pRes,
uNewWidth,
uNewHeight);
if (m_bCanceled)
{
free(pTemp);
free(pRes);
return NULL;
}
free(pTemp);
return pRes;
}
template <class FilterClass>
unsigned char *
C2PassScale<FilterClass>::
Scale (
unsigned char *pOrigImage,
UINT uOrigWidth,
UINT uOrigHeight,
unsigned char *pDstImage,
UINT uNewWidth,
UINT uNewHeight)
{
// Scale source image horizontally into temporary image
m_bCanceled = FALSE;
unsigned char *pTemp = (unsigned char*)malloc(uNewWidth * uOrigHeight * 3);
HorizScale (pOrigImage,
uOrigWidth,
uOrigHeight,
pTemp,
uNewWidth,
uOrigHeight);
if (m_bCanceled)
{
free(pTemp);
return NULL;
}
// Scale temporary image vertically into result image
VertScale ( pTemp,
uNewWidth,
uOrigHeight,
pDstImage,
uNewWidth,
uNewHeight);
free(pTemp);
if (m_bCanceled)
{
return NULL;
}
return pDstImage;
}
#endif // _2_PASS_SCALE_H_