QGIS API Documentation  2.99.0-Master (7d4f81d)
qgscubicrasterresampler.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscubicrasterresampler.cpp
3  ----------------------------
4  begin : December 2011
5  copyright : (C) 2011 by Marco Hugentobler
6  email : marco at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include <QImage>
20 #include <qmath.h>
21 
23 // red
24  : cRed00( 0.0 ), cRed10( 0.0 ), cRed20( 0.0 ), cRed30( 0.0 ), cRed01( 0.0 ), cRed11( 0.0 ), cRed21( 0.0 ), cRed31( 0.0 )
25  , cRed02( 0.0 ), cRed12( 0.0 ), cRed22( 0.0 ), cRed32( 0.0 ), cRed03( 0.0 ), cRed13( 0.0 ), cRed23( 0.0 ), cRed33( 0.0 )
26  // green
27  , cGreen00( 0.0 ), cGreen10( 0.0 ), cGreen20( 0.0 ), cGreen30( 0.0 ), cGreen01( 0.0 ), cGreen11( 0.0 ), cGreen21( 0.0 ), cGreen31( 0.0 )
28  , cGreen02( 0.0 ), cGreen12( 0.0 ), cGreen22( 0.0 ), cGreen32( 0.0 ), cGreen03( 0.0 ), cGreen13( 0.0 ), cGreen23( 0.0 ), cGreen33( 0.0 )
29  // blue
30  , cBlue00( 0.0 ), cBlue10( 0.0 ), cBlue20( 0.0 ), cBlue30( 0.0 ), cBlue01( 0.0 ), cBlue11( 0.0 ), cBlue21( 0.0 ), cBlue31( 0.0 )
31  , cBlue02( 0.0 ), cBlue12( 0.0 ), cBlue22( 0.0 ), cBlue32( 0.0 ), cBlue03( 0.0 ), cBlue13( 0.0 ), cBlue23( 0.0 ), cBlue33( 0.0 )
32  // alpha
33  , cAlpha00( 0.0 ), cAlpha10( 0.0 ), cAlpha20( 0.0 ), cAlpha30( 0.0 ), cAlpha01( 0.0 ), cAlpha11( 0.0 ), cAlpha21( 0.0 ), cAlpha31( 0.0 )
34  , cAlpha02( 0.0 ), cAlpha12( 0.0 ), cAlpha22( 0.0 ), cAlpha32( 0.0 ), cAlpha03( 0.0 ), cAlpha13( 0.0 ), cAlpha23( 0.0 ), cAlpha33( 0.0 )
35 {
36 }
37 
39 {
40  return new QgsCubicRasterResampler();
41 }
42 
43 void QgsCubicRasterResampler::resample( const QImage &srcImage, QImage &dstImage )
44 {
45  int nCols = srcImage.width();
46  int nRows = srcImage.height();
47 
48  int pos = 0;
49  QRgb px;
50  int *redMatrix = new int[ nCols * nRows ];
51  int *greenMatrix = new int[ nCols * nRows ];
52  int *blueMatrix = new int[ nCols * nRows ];
53  int *alphaMatrix = new int[ nCols * nRows ];
54 
55  for ( int heightIndex = 0; heightIndex < nRows; ++heightIndex )
56  {
57  QRgb *scanLine = ( QRgb * )srcImage.constScanLine( heightIndex );
58  for ( int widthIndex = 0; widthIndex < nCols; ++widthIndex )
59  {
60  px = scanLine[widthIndex];
61  int alpha = qAlpha( px );
62  alphaMatrix[pos] = alpha;
63  redMatrix[pos] = qRed( px );
64  greenMatrix[pos] = qGreen( px );
65  blueMatrix[pos] = qBlue( px );
66 
67  pos++;
68  }
69  }
70 
71  //derivative x
72  double *xDerivativeMatrixRed = new double[ nCols * nRows ];
73  xDerivativeMatrix( nCols, nRows, xDerivativeMatrixRed, redMatrix );
74  double *xDerivativeMatrixGreen = new double[ nCols * nRows ];
75  xDerivativeMatrix( nCols, nRows, xDerivativeMatrixGreen, greenMatrix );
76  double *xDerivativeMatrixBlue = new double[ nCols * nRows ];
77  xDerivativeMatrix( nCols, nRows, xDerivativeMatrixBlue, blueMatrix );
78  double *xDerivativeMatrixAlpha = new double[ nCols * nRows ];
79  xDerivativeMatrix( nCols, nRows, xDerivativeMatrixAlpha, alphaMatrix );
80 
81  //derivative y
82  double *yDerivativeMatrixRed = new double[ nCols * nRows ];
83  yDerivativeMatrix( nCols, nRows, yDerivativeMatrixRed, redMatrix );
84  double *yDerivativeMatrixGreen = new double[ nCols * nRows ];
85  yDerivativeMatrix( nCols, nRows, yDerivativeMatrixGreen, greenMatrix );
86  double *yDerivativeMatrixBlue = new double[ nCols * nRows ];
87  yDerivativeMatrix( nCols, nRows, yDerivativeMatrixBlue, blueMatrix );
88  double *yDerivativeMatrixAlpha = new double[ nCols * nRows ];
89  yDerivativeMatrix( nCols, nRows, yDerivativeMatrixAlpha, alphaMatrix );
90 
91  //compute output
92  double nSrcPerDstX = ( double ) srcImage.width() / ( double ) dstImage.width();
93  double nSrcPerDstY = ( double ) srcImage.height() / ( double ) dstImage.height();
94 
95  double currentSrcRow = nSrcPerDstY / 2.0 - 0.5;
96  double currentSrcCol;
97  int currentSrcColInt;
98  int currentSrcRowInt;
99  int lastSrcColInt = -100;
100  int lastSrcRowInt = -100;
101 
102  //bernstein polynomials
103  double bp0u, bp1u, bp2u, bp3u, bp0v, bp1v, bp2v, bp3v;
104  double u, v;
105 
106  for ( int y = 0; y < dstImage.height(); ++y )
107  {
108  currentSrcRowInt = floor( currentSrcRow );
109  v = currentSrcRow - currentSrcRowInt;
110 
111  currentSrcCol = nSrcPerDstX / 2.0 - 0.5;
112 
113  QRgb *scanLine = ( QRgb * )dstImage.scanLine( y );
114  for ( int x = 0; x < dstImage.width(); ++x )
115  {
116  currentSrcColInt = floor( currentSrcCol );
117  u = currentSrcCol - currentSrcColInt;
118 
119  //handle eight edge-cases
120  if ( ( currentSrcRowInt < 0 || currentSrcRowInt >= ( srcImage.height() - 1 ) || currentSrcColInt < 0 || currentSrcColInt >= ( srcImage.width() - 1 ) ) )
121  {
122  QRgb px1, px2;
123  //pixels at the border of the source image needs to be handled in a special way
124  if ( currentSrcRowInt < 0 && currentSrcColInt < 0 )
125  {
126  scanLine[x] = srcImage.pixel( 0, 0 );
127  }
128  else if ( currentSrcRowInt < 0 && currentSrcColInt >= ( srcImage.width() - 1 ) )
129  {
130  scanLine[x] = srcImage.pixel( srcImage.width() - 1, 0 );
131  }
132  else if ( currentSrcRowInt >= ( srcImage.height() - 1 ) && currentSrcColInt >= ( srcImage.width() - 1 ) )
133  {
134  scanLine[x] = srcImage.pixel( srcImage.width() - 1, srcImage.height() - 1 );
135  }
136  else if ( currentSrcRowInt >= ( srcImage.height() - 1 ) && currentSrcColInt < 0 )
137  {
138  scanLine[x] = srcImage.pixel( 0, srcImage.height() - 1 );
139  }
140  else if ( currentSrcRowInt < 0 )
141  {
142  px1 = srcImage.pixel( currentSrcColInt, 0 );
143  px2 = srcImage.pixel( currentSrcColInt + 1, 0 );
144  scanLine[x] = curveInterpolation( px1, px2, u, xDerivativeMatrixRed[ currentSrcColInt ], xDerivativeMatrixGreen[ currentSrcColInt ],
145  xDerivativeMatrixBlue[ currentSrcColInt ], xDerivativeMatrixAlpha[ currentSrcColInt ], xDerivativeMatrixRed[ currentSrcColInt + 1 ], xDerivativeMatrixGreen[ currentSrcColInt + 1 ],
146  xDerivativeMatrixBlue[ currentSrcColInt + 1 ], xDerivativeMatrixAlpha[ currentSrcColInt + 1 ] );
147  }
148  else if ( currentSrcRowInt >= ( srcImage.height() - 1 ) )
149  {
150  int idx = ( srcImage.height() - 1 ) * srcImage.width() + currentSrcColInt;
151  px1 = srcImage.pixel( currentSrcColInt, srcImage.height() - 1 );
152  px2 = srcImage.pixel( currentSrcColInt + 1, srcImage.height() - 1 );
153  scanLine[x] = curveInterpolation( px1, px2, u, xDerivativeMatrixRed[ idx ], xDerivativeMatrixGreen[ idx ], xDerivativeMatrixBlue[idx],
154  xDerivativeMatrixAlpha[idx], xDerivativeMatrixRed[ idx + 1 ], xDerivativeMatrixGreen[ idx + 1 ], xDerivativeMatrixBlue[idx + 1],
155  xDerivativeMatrixAlpha[idx + 1] );
156  }
157  else if ( currentSrcColInt < 0 )
158  {
159  int idx1 = currentSrcRowInt * srcImage.width();
160  int idx2 = idx1 + srcImage.width();
161  px1 = srcImage.pixel( 0, currentSrcRowInt );
162  px2 = srcImage.pixel( 0, currentSrcRowInt + 1 );
163  scanLine[x] = curveInterpolation( px1, px2, v, yDerivativeMatrixRed[ idx1 ], yDerivativeMatrixGreen[ idx1 ], yDerivativeMatrixBlue[ idx1],
164  yDerivativeMatrixAlpha[ idx1], yDerivativeMatrixRed[ idx2 ], yDerivativeMatrixGreen[ idx2 ], yDerivativeMatrixBlue[ idx2],
165  yDerivativeMatrixAlpha[ idx2] );
166  }
167  else if ( currentSrcColInt >= ( srcImage.width() - 1 ) )
168  {
169  int idx1 = currentSrcRowInt * srcImage.width() + srcImage.width() - 1;
170  int idx2 = idx1 + srcImage.width();
171  px1 = srcImage.pixel( srcImage.width() - 1, currentSrcRowInt );
172  px2 = srcImage.pixel( srcImage.width() - 1, currentSrcRowInt + 1 );
173  scanLine[x] = curveInterpolation( px1, px2, v, yDerivativeMatrixRed[ idx1 ], yDerivativeMatrixGreen[ idx1 ], yDerivativeMatrixBlue[ idx1],
174  yDerivativeMatrixAlpha[ idx1], yDerivativeMatrixRed[ idx2 ], yDerivativeMatrixGreen[ idx2 ], yDerivativeMatrixBlue[ idx2],
175  yDerivativeMatrixAlpha[ idx2] );
176  }
177  currentSrcCol += nSrcPerDstX;
178  continue;
179  }
180 
181  //first update the control points if necessary
182  if ( currentSrcColInt != lastSrcColInt || currentSrcRowInt != lastSrcRowInt )
183  {
184  calculateControlPoints( nCols, nRows, currentSrcRowInt, currentSrcColInt, redMatrix, greenMatrix, blueMatrix, alphaMatrix,
185  xDerivativeMatrixRed, xDerivativeMatrixGreen, xDerivativeMatrixBlue, xDerivativeMatrixAlpha,
186  yDerivativeMatrixRed, yDerivativeMatrixGreen, yDerivativeMatrixBlue, yDerivativeMatrixAlpha );
187  }
188 
189  //bernstein polynomials
190  bp0u = calcBernsteinPolyN3( 0, u );
191  bp1u = calcBernsteinPolyN3( 1, u );
192  bp2u = calcBernsteinPolyN3( 2, u );
193  bp3u = calcBernsteinPolyN3( 3, u );
194  bp0v = calcBernsteinPolyN3( 0, v );
195  bp1v = calcBernsteinPolyN3( 1, v );
196  bp2v = calcBernsteinPolyN3( 2, v );
197  bp3v = calcBernsteinPolyN3( 3, v );
198 
199  //then calculate value based on bernstein form of Bezier patch
200  //todo: move into function
201  int r = bp0u * bp0v * cRed00 +
202  bp1u * bp0v * cRed10 +
203  bp2u * bp0v * cRed20 +
204  bp3u * bp0v * cRed30 +
205  bp0u * bp1v * cRed01 +
206  bp1u * bp1v * cRed11 +
207  bp2u * bp1v * cRed21 +
208  bp3u * bp1v * cRed31 +
209  bp0u * bp2v * cRed02 +
210  bp1u * bp2v * cRed12 +
211  bp2u * bp2v * cRed22 +
212  bp3u * bp2v * cRed32 +
213  bp0u * bp3v * cRed03 +
214  bp1u * bp3v * cRed13 +
215  bp2u * bp3v * cRed23 +
216  bp3u * bp3v * cRed33;
217 
218  int g = bp0u * bp0v * cGreen00 +
219  bp1u * bp0v * cGreen10 +
220  bp2u * bp0v * cGreen20 +
221  bp3u * bp0v * cGreen30 +
222  bp0u * bp1v * cGreen01 +
223  bp1u * bp1v * cGreen11 +
224  bp2u * bp1v * cGreen21 +
225  bp3u * bp1v * cGreen31 +
226  bp0u * bp2v * cGreen02 +
227  bp1u * bp2v * cGreen12 +
228  bp2u * bp2v * cGreen22 +
229  bp3u * bp2v * cGreen32 +
230  bp0u * bp3v * cGreen03 +
231  bp1u * bp3v * cGreen13 +
232  bp2u * bp3v * cGreen23 +
233  bp3u * bp3v * cGreen33;
234 
235  int b = bp0u * bp0v * cBlue00 +
236  bp1u * bp0v * cBlue10 +
237  bp2u * bp0v * cBlue20 +
238  bp3u * bp0v * cBlue30 +
239  bp0u * bp1v * cBlue01 +
240  bp1u * bp1v * cBlue11 +
241  bp2u * bp1v * cBlue21 +
242  bp3u * bp1v * cBlue31 +
243  bp0u * bp2v * cBlue02 +
244  bp1u * bp2v * cBlue12 +
245  bp2u * bp2v * cBlue22 +
246  bp3u * bp2v * cBlue32 +
247  bp0u * bp3v * cBlue03 +
248  bp1u * bp3v * cBlue13 +
249  bp2u * bp3v * cBlue23 +
250  bp3u * bp3v * cBlue33;
251 
252  int a = bp0u * bp0v * cAlpha00 +
253  bp1u * bp0v * cAlpha10 +
254  bp2u * bp0v * cAlpha20 +
255  bp3u * bp0v * cAlpha30 +
256  bp0u * bp1v * cAlpha01 +
257  bp1u * bp1v * cAlpha11 +
258  bp2u * bp1v * cAlpha21 +
259  bp3u * bp1v * cAlpha31 +
260  bp0u * bp2v * cAlpha02 +
261  bp1u * bp2v * cAlpha12 +
262  bp2u * bp2v * cAlpha22 +
263  bp3u * bp2v * cAlpha32 +
264  bp0u * bp3v * cAlpha03 +
265  bp1u * bp3v * cAlpha13 +
266  bp2u * bp3v * cAlpha23 +
267  bp3u * bp3v * cAlpha33;
268 
269  scanLine[x] = createPremultipliedColor( r, g, b, a );
270 
271  lastSrcColInt = currentSrcColInt;
272  currentSrcCol += nSrcPerDstX;
273  }
274  lastSrcRowInt = currentSrcRowInt;
275  currentSrcRow += nSrcPerDstY;
276  }
277 
278 
279  //cleanup memory
280  delete[] redMatrix;
281  delete[] greenMatrix;
282  delete[] blueMatrix;
283  delete[] alphaMatrix;
284  delete[] xDerivativeMatrixRed;
285  delete[] xDerivativeMatrixGreen;
286  delete[] xDerivativeMatrixBlue;
287  delete[] xDerivativeMatrixAlpha;
288  delete[] yDerivativeMatrixRed;
289  delete[] yDerivativeMatrixGreen;
290  delete[] yDerivativeMatrixBlue;
291  delete[] yDerivativeMatrixAlpha;
292 }
293 
294 void QgsCubicRasterResampler::xDerivativeMatrix( int nCols, int nRows, double *matrix, const int *colorMatrix )
295 {
296  double val = 0;
297  int index = 0;
298 
299  for ( int y = 0; y < nRows; ++y )
300  {
301  for ( int x = 0; x < nCols; ++x )
302  {
303  if ( x == 0 )
304  {
305  val = colorMatrix[index + 1] - colorMatrix[index];
306  }
307  else if ( x == ( nCols - 1 ) )
308  {
309  val = colorMatrix[index] - colorMatrix[ index - 1 ];
310  }
311  else
312  {
313  val = ( colorMatrix[index + 1] - colorMatrix[index - 1] ) / 2.0;
314  }
315  matrix[index] = val;
316  ++index;
317  }
318  }
319 }
320 
321 void QgsCubicRasterResampler::yDerivativeMatrix( int nCols, int nRows, double *matrix, const int *colorMatrix )
322 {
323  double val = 0;
324  int index = 0;
325 
326  for ( int y = 0; y < nRows; ++y )
327  {
328  for ( int x = 0; x < nCols; ++x )
329  {
330  if ( y == 0 )
331  {
332  val = colorMatrix[ index + nCols ] - colorMatrix[ index ];
333  }
334  else if ( y == ( nRows - 1 ) )
335  {
336  val = colorMatrix[ index ] - colorMatrix[ index - nCols ];
337  }
338  else
339  {
340  val = ( colorMatrix[ index + nCols ] - colorMatrix[ index - nCols ] ) / 2.0;
341  }
342  matrix[index] = val;
343  ++index;
344  }
345  }
346 }
347 
348 void QgsCubicRasterResampler::calculateControlPoints( int nCols, int nRows, int currentRow, int currentCol, int *redMatrix, int *greenMatrix, int *blueMatrix,
349  int *alphaMatrix, double *xDerivativeMatrixRed, double *xDerivativeMatrixGreen, double *xDerivativeMatrixBlue, double *xDerivativeMatrixAlpha,
350  double *yDerivativeMatrixRed, double *yDerivativeMatrixGreen, double *yDerivativeMatrixBlue, double *yDerivativeMatrixAlpha )
351 {
352  Q_UNUSED( nRows );
353  int idx00 = currentRow * nCols + currentCol;
354  int idx10 = idx00 + 1;
355  int idx01 = idx00 + nCols;
356  int idx11 = idx01 + 1;
357 
358  //corner points
359  cRed00 = redMatrix[idx00];
360  cGreen00 = greenMatrix[idx00];
361  cBlue00 = blueMatrix[idx00];
362  cAlpha00 = alphaMatrix[idx00];
363  cRed30 = redMatrix[idx10];
364  cGreen30 = greenMatrix[idx10];
365  cBlue30 = blueMatrix[idx10];
366  cAlpha30 = alphaMatrix[idx10];
367  cRed03 = redMatrix[idx01];
368  cGreen03 = greenMatrix[idx01];
369  cBlue03 = blueMatrix[idx01];
370  cAlpha03 = alphaMatrix[idx01];
371  cRed33 = redMatrix[idx11];
372  cGreen33 = greenMatrix[idx11];
373  cBlue33 = blueMatrix[idx11];
374  cAlpha33 = alphaMatrix[idx11];
375 
376  //control points near c00
377  cRed10 = cRed00 + 0.333 * xDerivativeMatrixRed[idx00];
378  cGreen10 = cGreen00 + 0.333 * xDerivativeMatrixGreen[idx00];
379  cBlue10 = cBlue00 + 0.333 * xDerivativeMatrixBlue[idx00];
380  cAlpha10 = cAlpha00 + 0.333 * xDerivativeMatrixAlpha[idx00];
381  cRed01 = cRed00 + 0.333 * yDerivativeMatrixRed[idx00];
382  cGreen01 = cGreen00 + 0.333 * yDerivativeMatrixGreen[idx00];
383  cBlue01 = cBlue00 + 0.333 * yDerivativeMatrixBlue[idx00];
384  cAlpha01 = cAlpha00 + 0.333 * yDerivativeMatrixAlpha[idx00];
385  cRed11 = cRed10 + 0.333 * yDerivativeMatrixRed[idx00];
386  cGreen11 = cGreen10 + 0.333 * yDerivativeMatrixGreen[idx00];
387  cBlue11 = cBlue10 + 0.333 * yDerivativeMatrixBlue[idx00];
388  cAlpha11 = cAlpha10 + 0.333 * yDerivativeMatrixAlpha[idx00];
389 
390  //control points near c30
391  cRed20 = cRed30 - 0.333 * xDerivativeMatrixRed[idx10];
392  cGreen20 = cGreen30 - 0.333 * xDerivativeMatrixGreen[idx10];
393  cBlue20 = cBlue30 - 0.333 * xDerivativeMatrixBlue[idx10];
394  cAlpha20 = cAlpha30 - 0.333 * xDerivativeMatrixAlpha[idx10];
395  cRed31 = cRed30 + 0.333 * yDerivativeMatrixRed[idx10];
396  cGreen31 = cGreen30 + 0.333 * yDerivativeMatrixGreen[idx10];
397  cBlue31 = cBlue30 + 0.333 * yDerivativeMatrixBlue[idx10];
398  cAlpha31 = cAlpha30 + 0.333 * yDerivativeMatrixAlpha[idx10];
399  cRed21 = cRed20 + 0.333 * yDerivativeMatrixRed[idx10];
400  cGreen21 = cGreen20 + 0.333 * yDerivativeMatrixGreen[idx10];
401  cBlue21 = cBlue20 + 0.333 * yDerivativeMatrixBlue[idx10];
402  cAlpha21 = cAlpha20 + 0.333 * yDerivativeMatrixAlpha[idx10];
403 
404  //control points near c03
405  cRed13 = cRed03 + 0.333 * xDerivativeMatrixRed[idx01];
406  cGreen13 = cGreen03 + 0.333 * xDerivativeMatrixGreen[idx01];
407  cBlue13 = cBlue03 + 0.333 * xDerivativeMatrixBlue[idx01];
408  cAlpha13 = cAlpha03 + 0.333 * xDerivativeMatrixAlpha[idx01];
409  cRed02 = cRed03 - 0.333 * yDerivativeMatrixRed[idx01];
410  cGreen02 = cGreen03 - 0.333 * yDerivativeMatrixGreen[idx01];
411  cBlue02 = cBlue03 - 0.333 * yDerivativeMatrixBlue[idx01];
412  cAlpha02 = cAlpha03 - 0.333 * yDerivativeMatrixAlpha[idx01];
413  cRed12 = cRed02 + 0.333 * xDerivativeMatrixRed[idx01];
414  cGreen12 = cGreen02 + 0.333 * xDerivativeMatrixGreen[idx01];
415  cBlue12 = cBlue02 + 0.333 * xDerivativeMatrixBlue[idx01];
416  cAlpha12 = cAlpha02 + 0.333 * xDerivativeMatrixAlpha[idx01];
417 
418  //control points near c33
419  cRed23 = cRed33 - 0.333 * xDerivativeMatrixRed[idx11];
420  cGreen23 = cGreen33 - 0.333 * xDerivativeMatrixGreen[idx11];
421  cBlue23 = cBlue33 - 0.333 * xDerivativeMatrixBlue[idx11];
422  cAlpha23 = cAlpha33 - 0.333 * xDerivativeMatrixAlpha[idx11];
423  cRed32 = cRed33 - 0.333 * yDerivativeMatrixRed[idx11];
424  cGreen32 = cGreen33 - 0.333 * yDerivativeMatrixGreen[idx11];
425  cBlue32 = cBlue33 - 0.333 * yDerivativeMatrixBlue[idx11];
426  cAlpha32 = cAlpha33 - 0.333 * yDerivativeMatrixAlpha[idx11];
427  cRed22 = cRed32 - 0.333 * xDerivativeMatrixRed[idx11];
428  cGreen22 = cGreen32 - 0.333 * xDerivativeMatrixGreen[idx11];
429  cBlue22 = cBlue32 - 0.333 * xDerivativeMatrixBlue[idx11];
430  cAlpha22 = cAlpha32 - 0.333 * xDerivativeMatrixAlpha[idx11];
431 }
432 
433 QRgb QgsCubicRasterResampler::curveInterpolation( QRgb pt1, QRgb pt2, double t, double d1red, double d1green, double d1blue, double d1alpha,
434  double d2red, double d2green, double d2blue, double d2alpha )
435 {
436  //control points
437  double p0r = qRed( pt1 );
438  double p1r = p0r + 0.333 * d1red;
439  double p3r = qRed( pt2 );
440  double p2r = p3r - 0.333 * d2red;
441  double p0g = qGreen( pt1 );
442  double p1g = p0g + 0.333 * d1green;
443  double p3g = qGreen( pt2 );
444  double p2g = p3g - 0.333 * d2green;
445  double p0b = qBlue( pt1 );
446  double p1b = p0b + 0.333 * d1blue;
447  double p3b = qBlue( pt2 );
448  double p2b = p3b - 0.333 * d2blue;
449  double p0a = qAlpha( pt1 );
450  double p1a = p0a + 0.333 * d1alpha;
451  double p3a = qAlpha( pt2 );
452  double p2a = p3a - 0.333 * d2alpha;
453 
454  //bernstein polynomials
455  double bp0 = calcBernsteinPolyN3( 0, t );
456  double bp1 = calcBernsteinPolyN3( 1, t );
457  double bp2 = calcBernsteinPolyN3( 2, t );
458  double bp3 = calcBernsteinPolyN3( 3, t );
459 
460  int red = bp0 * p0r + bp1 * p1r + bp2 * p2r + bp3 * p3r;
461  int green = bp0 * p0g + bp1 * p1g + bp2 * p2g + bp3 * p3g;
462  int blue = bp0 * p0b + bp1 * p1b + bp2 * p2b + bp3 * p3b;
463  int alpha = bp0 * p0a + bp1 * p1a + bp2 * p2a + bp3 * p3a;
464 
465  return createPremultipliedColor( red, green, blue, alpha );
466 }
467 
468 double QgsCubicRasterResampler::calcBernsteinPolyN3( int i, double t )
469 {
470  if ( i < 0 )
471  {
472  return 0;
473  }
474 
475  return lowerN3( i ) * qPow( t, i ) * qPow( ( 1 - t ), ( 3 - i ) );
476 }
477 
478 inline int QgsCubicRasterResampler::lowerN3( int i )
479 {
480  switch ( i )
481  {
482  case 0:
483  case 3:
484  return 1;
485  case 1:
486  case 2:
487  return 3;
488  default:
489  return 0;
490  }
491 }
492 
493 QRgb QgsCubicRasterResampler::createPremultipliedColor( const int r, const int g, const int b, const int a )
494 {
495  int maxComponentBounds = qBound( 0, a, 255 );
496  return qRgba( qBound( 0, r, maxComponentBounds ),
497  qBound( 0, g, maxComponentBounds ),
498  qBound( 0, b, maxComponentBounds ),
499  a );
500 }
Cubic Raster Resampler.
QgsCubicRasterResampler * clone() const override
Get a deep copy of this object.
void resample(const QImage &srcImage, QImage &dstImage) override