summaryrefslogtreecommitdiff
path: root/src/graphics/TwoPassScale.h
blob: 89bf993d43dc0441e6425a80b94819d2d5eca873 (plain)
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
// Fast and accurate bitmap scaling. Original code by Eran Yariv and Jake Montgomery,
// posted on codeguru.com.

#ifndef _TwoPassScale_h_
#define _TwoPassScale_h_

#include "ContribDefs.h"

#include "../base/Exception.h"

#include <math.h>
#include <algorithm>
#include <cstring>

namespace avg {

typedef struct
{
   int *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
   int 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)

class CDataA_UBYTE
{
public:
  typedef unsigned char PixelClass;
  class _Accumulator {
  public:
      _Accumulator ()
      {
        val = 0;
      };
      void Accumulate (int Weight, PixelClass &value)
      {
        val += (Weight * value);
      };

      void Store (PixelClass* value)
      {
        *value = (unsigned char) ((val + 128)/256);
      };
      int val;
  };
};

class CDataRGB_UBYTE
{
public:
  typedef unsigned char PixelClass[3];
  class _Accumulator {
  public:
      _Accumulator ()
      {
        val [0] = val [1] = val [2] = 0;
      };
      void Accumulate (int Weight, PixelClass &value)
      {
        val [0] += (Weight * value [0]);
        val [1] += (Weight * value [1]);
        val [2] += (Weight * value [2]);
      };

      void Store (PixelClass* value)
      {
        (*value) [0] = (unsigned char) ((val [0] + 128)/256);
        (*value) [1] = (unsigned char) ((val [1] + 128)/256);
        (*value) [2] = (unsigned char) ((val [2] + 128)/256);
      };
      int val [3];
  };
};

class CDataRGBA_UBYTE {
public:
  typedef unsigned char PixelClass[4];
  class _Accumulator {
  public:
      _Accumulator ()
      {
        val [0] = val [1] = val [2] = val [3] = 0;
      };
      void Accumulate (int dWeight, PixelClass &value)
      {
        val [0] += (dWeight * (value [0]));
        val [1] += (dWeight * (value [1]));
        val [2] += (dWeight * (value [2]));
        val [3] += (dWeight * (value [3]));
      };

      void Store (PixelClass* value)
      {
        (*value) [0] = (unsigned char) ((val [0] + 128)/256);
        (*value) [1] = (unsigned char) ((val [1] + 128)/256);
        (*value) [2] = (unsigned char) ((val [2] + 128)/256);
        (*value) [3] = (unsigned char) ((val [3] + 128)/256);
      };
      int val [4];
  };
};

template <class DataClass>
class TwoPassScale
{
public:
    typedef typename DataClass::PixelClass PixelClass;

    TwoPassScale (const ContribDef& contribDef)
        : m_ContribDef (contribDef)
    {};

    virtual ~TwoPassScale() {};

    void Scale(PixelClass * pSrcData, const IntPoint& srcSize, int srcStride, 
            PixelClass *pDstData, const IntPoint& dstSize, int dstStride);

private:
    LineContribType *AllocContributions (unsigned uLineLength,
                                         unsigned uWindowSize);

    void FreeContributions (LineContribType * p);

    LineContribType *CalcContributions (unsigned    uLineSize,
                                        unsigned    uSrcSize);

    void ScaleRow(PixelClass *pSrc, int uSrcWidth, PixelClass *pDest, int uResWidth, 
            LineContribType *pContrib);

    void HorizScale(PixelClass * pSrcData, const IntPoint& srcSize, int srcStride, 
            PixelClass *pDestData, const IntPoint& destSize, int destStride);

    void VertScale(PixelClass *pSrcData, const IntPoint& srcSize, int srcStride,
            PixelClass *pDestData, const IntPoint& destSize, int destStride);

    const ContribDef& m_ContribDef;
};

template <class DataClass>
LineContribType *
TwoPassScale<DataClass>::AllocContributions (unsigned uLineLength, unsigned uWindowSize)
{
    LineContribType *res = new LineContribType;
        // Init structure header
    res->WindowSize = uWindowSize;
    res->LineLength = uLineLength;
        // Allocate list of contributions
    res->ContribRow = new ContributionType[uLineLength];
    for (unsigned u = 0 ; u < uLineLength ; u++)
    {
        // Allocate contributions for every pixel
        res->ContribRow[u].Weights = new int[uWindowSize];
    }
    return res;
}

template <class DataClass>
void
TwoPassScale<DataClass>::FreeContributions (LineContribType * p)
{
    for (int 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 DataClass>
LineContribType *
TwoPassScale<DataClass>::CalcContributions(unsigned uLineSize, unsigned uSrcSize)
{
    float dScale = float(uLineSize)/uSrcSize;
    float dWidth;
    float dFScale = 1.0;
    float dFilterWidth = m_ContribDef.GetWidth();

    if (dScale < 1.0) {
        // Minification
        dWidth = dFilterWidth / dScale;
        dFScale = dScale;
    } else {
        // Magnification
        dWidth= dFilterWidth;
    }

    // Window size is the number of sampled pixels
    int iWindowSize = 2 * (int)ceil(dWidth) + 1;

    // Allocate a new line contributions strucutre
    LineContribType *res = AllocContributions (uLineSize, iWindowSize);

    for (unsigned u = 0; u < uLineSize; u++) {
        // Scan through line of contributions
        float dCenter = (u+0.5f)/dScale-0.5f;   // Reverse mapping
        // Find the significant edge points that affect the pixel
        int iLeft = std::max (0, (int)floor (dCenter - dWidth));
        int iRight = std::min ((int)ceil (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;
        
        int dTotalWeight = 0;  // Zero sum of weights
        for (int iSrc = iLeft; iSrc <= iRight; iSrc++) {
            // Calculate weights
            int CurWeight = int (dFScale * (m_ContribDef.Filter (dFScale * 
                    (dCenter - (float)iSrc)))*256);
            res->ContribRow[u].Weights[iSrc-iLeft] = CurWeight;
            dTotalWeight += CurWeight;
        }
        AVG_ASSERT(dTotalWeight >= 0);   // An error in the filter function can cause this
        int UsedWeight = 0;
        if (dTotalWeight > 0) {
            // Normalize weight of neighbouring points
            for (int iSrc = iLeft; iSrc < iRight; iSrc++) {
                // Normalize point
                int CurWeight = (res->ContribRow[u].Weights[iSrc-iLeft]*256)/dTotalWeight;
                res->ContribRow[u].Weights[iSrc-iLeft] = CurWeight;
                UsedWeight += CurWeight;
            }
            // The last point gets everything that's left over so the sum is
            // always correct.
            res->ContribRow[u].Weights[iRight-iLeft] = 256 - UsedWeight;
        }
   }
   return res;
}

template <class DataClass>
void
TwoPassScale<DataClass>::ScaleRow(PixelClass *pSrc, int uSrcWidth,
        PixelClass *pDest, int uResWidth, LineContribType *pContrib)
{
    PixelClass * pDestPixel = pDest;
    for (int x = 0; x < uResWidth; x++) {
        typename DataClass::_Accumulator a;
        int iLeft = pContrib->ContribRow[x].Left;    // Retrieve left boundries
        int iRight = pContrib->ContribRow[x].Right;  // Retrieve right boundries
        int * Weights = pContrib->ContribRow[x].Weights;
        for (int i = iLeft; i <= iRight; i++) {
            // Scan between boundries
            // Accumulate weighted effect of each neighboring pixel
            a.Accumulate(Weights[i-iLeft], pSrc[i]);
        }
        a.Store(pDestPixel);
        pDestPixel++;
    }
}

template <class DataClass>
void TwoPassScale<DataClass>::HorizScale(PixelClass * pSrcData, const IntPoint& srcSize, 
        int srcStride, PixelClass *pDestData, const IntPoint& destSize, int destStride)
{
    PixelClass * pSrc = pSrcData;
    PixelClass * pDest = pDestData;
    if (srcSize.x == destSize.x) {
        // No scaling required, just copy
        for (int y = 0; y < destSize.y; y++) {
            memcpy(pDest, pSrc, sizeof (PixelClass) * srcSize.x);
            pSrc = (PixelClass*)((char*)(pSrc)+srcStride);
            pDest = (PixelClass*)((char*)(pDest)+destStride);
        }
    } else {
        LineContribType * pContrib = CalcContributions(destSize.x, srcSize.x);
        for (int y = 0; y < destSize.y; y++) {
            ScaleRow(pSrc, srcSize.x, pDest, destSize.x, pContrib);
            pSrc = (PixelClass*)((char*)(pSrc)+srcStride);
            pDest = (PixelClass*)((char*)(pDest)+destStride);
        }
        FreeContributions(pContrib);  // Free contributions structure
    }
}


template <class DataClass>
void TwoPassScale<DataClass>::VertScale(PixelClass *pSrcData, const IntPoint& srcSize,
        int srcStride, PixelClass *pDestData, const IntPoint& destSize, int destStride)
{
    PixelClass * pSrc = pSrcData;
    PixelClass * pDest = pDestData;
    if (srcSize.y == destSize.y) {
        // No scaling required, just copy
        for (int y = 0; y < destSize.y; y++) {
            memcpy(pDest, pSrc, sizeof (PixelClass) * srcSize.x);
            pSrc = (PixelClass*)((char*)(pSrc)+srcStride);
            pDest = (PixelClass*)((char*)(pDest)+destStride);
        }
    } else {
        LineContribType * pContrib = CalcContributions(destSize.y, srcSize.y);
        for (int y = 0; y < destSize.y; y++) {
            PixelClass * pDestPixel = pDest;
            int * pWeights = pContrib->ContribRow[y].Weights;
            int iLeft = pContrib->ContribRow[y].Left;
            int iRight = pContrib->ContribRow[y].Right;
            PixelClass* pSrcPixelBase = (PixelClass*)((char*)(pSrc)
                    + size_t(iLeft)*srcStride);
            for (int x = 0; x < destSize.x; x++) {
                typename DataClass::_Accumulator a;
                int * pWeight = pWeights;
                PixelClass * pSrcPixel = pSrcPixelBase;
                pSrcPixelBase++;
                for (int i = iLeft; i <= iRight; i++) {
                    // Scan between boundries
                    // Accumulate weighted effect of each neighboring pixel
                    a.Accumulate(*pWeight, *pSrcPixel);
                    pWeight++;
                    pSrcPixel = (PixelClass*)((char*)(pSrcPixel)+srcStride);
                }
                a.Store(pDestPixel);
                pDestPixel++;
            }
            pDest = (PixelClass*)((char*)(pDest)+destStride);
        }
        FreeContributions(pContrib);     // Free contributions structure
    }
}


template <class DataClass>
void TwoPassScale<DataClass>::Scale(PixelClass * pSrcData, const IntPoint& srcSize, 
        int srcStride, PixelClass *pDstData, const IntPoint& dstSize, int dstStride)
{
    // Allocate temp image
    PixelClass *pTempData = new PixelClass[srcSize.y*dstSize.x];
    IntPoint tempSize(dstSize.x, srcSize.y);
    int tempStride = dstSize.x*sizeof(PixelClass);

    // Scale source image horizontally into temporary image
    HorizScale(pSrcData, srcSize, srcStride, 
            pTempData, tempSize, tempStride);
 
    // Scale temporary image vertically into result image
    VertScale (pTempData, tempSize, tempStride, 
            pDstData, dstSize, dstStride);
    delete [] pTempData;
}

}

#endif