|
| 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 | + |
| 18 | +#include "qgscubicrasterresampler.h" |
| 19 | +#include <QImage> |
| 20 | + |
| 21 | +QgsCubicRasterResampler::QgsCubicRasterResampler() |
| 22 | +{ |
| 23 | +} |
| 24 | + |
| 25 | +QgsCubicRasterResampler::~QgsCubicRasterResampler() |
| 26 | +{ |
| 27 | +} |
| 28 | + |
| 29 | +void QgsCubicRasterResampler::resample( const QImage& srcImage, QImage& dstImage ) const |
| 30 | +{ |
| 31 | + int nCols = srcImage.width(); |
| 32 | + int nRows = srcImage.height(); |
| 33 | + |
| 34 | + int pos = 0; |
| 35 | + QRgb px; |
| 36 | + int* redMatrix = new int[ nCols * nRows ]; |
| 37 | + int* greenMatrix = new int[ nCols * nRows ]; |
| 38 | + int* blueMatrix = new int[ nCols * nRows ]; |
| 39 | + |
| 40 | + for( int i = 0; i < nRows; ++i ) |
| 41 | + { |
| 42 | + for( int j = 0; j < nCols; ++j ) |
| 43 | + { |
| 44 | + px = srcImage.pixel( j, i ); |
| 45 | + redMatrix[pos] = qRed( px ); |
| 46 | + greenMatrix[pos] = qGreen( px ); |
| 47 | + blueMatrix[pos] = qBlue( px ); |
| 48 | + ++pos; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + //derivative x |
| 53 | + double* xDerivativeMatrixRed = new double[ nCols * nRows ]; |
| 54 | + xDerivativeMatrix( nCols, nRows, xDerivativeMatrixRed, redMatrix ); |
| 55 | + double* xDerivativeMatrixGreen = new double[ nCols * nRows ]; |
| 56 | + xDerivativeMatrix( nCols, nRows, xDerivativeMatrixGreen, greenMatrix ); |
| 57 | + double* xDerivativeMatrixBlue = new double[ nCols * nRows ]; |
| 58 | + xDerivativeMatrix( nCols, nRows, xDerivativeMatrixBlue, blueMatrix ); |
| 59 | + |
| 60 | + //derivative y |
| 61 | + double* yDerivativeMatrixRed = new double[ nCols * nRows ]; |
| 62 | + yDerivativeMatrix<int>( nCols, nRows, yDerivativeMatrixRed, redMatrix ); |
| 63 | + double* yDerivativeMatrixGreen = new double[ nCols * nRows ]; |
| 64 | + yDerivativeMatrix<int>( nCols, nRows, yDerivativeMatrixGreen, greenMatrix ); |
| 65 | + double* yDerivativeMatrixBlue = new double[ nCols * nRows ]; |
| 66 | + yDerivativeMatrix<int>( nCols, nRows, yDerivativeMatrixBlue, blueMatrix ); |
| 67 | + |
| 68 | + //derivative xy |
| 69 | + double* xyDerivativeMatrixRed = new double[ nCols * nRows ]; |
| 70 | + yDerivativeMatrix<double>( nCols, nRows, xyDerivativeMatrixRed, xDerivativeMatrixRed ); |
| 71 | + double* xyDerivativeMatrixGreen = new double[ nCols * nRows ]; |
| 72 | + yDerivativeMatrix<double>( nCols, nRows, xyDerivativeMatrixGreen, xDerivativeMatrixGreen ); |
| 73 | + double* xyDerivativeMatrixBlue = new double[ nCols * nRows ]; |
| 74 | + yDerivativeMatrix<double>( nCols, nRows, xyDerivativeMatrixBlue, xDerivativeMatrixBlue ); |
| 75 | + |
| 76 | + //compute output |
| 77 | + double nSrcPerDstX = ( double ) srcImage.width() / ( double ) dstImage.width(); |
| 78 | + double nSrcPerDstY = ( double ) srcImage.height() / ( double ) dstImage.height(); |
| 79 | + |
| 80 | + double currentSrcRow = nSrcPerDstX / 2.0 - 0.5; |
| 81 | + double currentSrcCol; |
| 82 | + int currentSrcColInt; |
| 83 | + int currentSrcRowInt; |
| 84 | + |
| 85 | + int r, g, b; |
| 86 | + int index; |
| 87 | + |
| 88 | + for ( int i = 0; i < dstImage.height(); ++i ) |
| 89 | + { |
| 90 | + currentSrcRowInt = (int) currentSrcRow; |
| 91 | + |
| 92 | + currentSrcCol = nSrcPerDstY / 2.0 - 0.5; |
| 93 | + for( int j = 0; j < dstImage.width(); ++j ) |
| 94 | + { |
| 95 | + //out of bounds check |
| 96 | + currentSrcColInt = (int)currentSrcCol; |
| 97 | + if( currentSrcColInt < 0 || currentSrcColInt > srcImage.width() - 2 |
| 98 | + || currentSrcRowInt < 0 || currentSrcRowInt > srcImage.height() - 2 ) |
| 99 | + { |
| 100 | + currentSrcCol += nSrcPerDstX; |
| 101 | + ++index; |
| 102 | + dstImage.setPixel( j, i, qRgb( 0, 0, 255 ) ); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + //interpolation |
| 108 | + index = currentSrcRowInt * nCols + currentSrcColInt; |
| 109 | + r = cubicInterpolation( redMatrix[index], redMatrix[index + 1], redMatrix[index + nCols + 1 ], redMatrix[ index + nCols ], |
| 110 | + xDerivativeMatrixRed[index], xDerivativeMatrixRed[index + 1], xDerivativeMatrixRed[index + nCols + 1 ], xDerivativeMatrixRed[ index + nCols ], |
| 111 | + yDerivativeMatrixRed[index], yDerivativeMatrixRed[index + 1], yDerivativeMatrixRed[index + nCols + 1 ], yDerivativeMatrixRed[ index + nCols ], |
| 112 | + xyDerivativeMatrixRed[index], xyDerivativeMatrixRed[index + 1], xyDerivativeMatrixRed[index + nCols + 1 ], xyDerivativeMatrixRed[ index + nCols ] ); |
| 113 | + g = cubicInterpolation( greenMatrix[index], greenMatrix[index + 1], greenMatrix[index + nCols + 1 ], greenMatrix[ index + nCols ], |
| 114 | + xDerivativeMatrixGreen[index], xDerivativeMatrixGreen[index + 1], xDerivativeMatrixGreen[index + nCols + 1 ], xDerivativeMatrixGreen[ index + nCols ], |
| 115 | + yDerivativeMatrixGreen[index], yDerivativeMatrixGreen[index + 1], yDerivativeMatrixGreen[index + nCols + 1 ], yDerivativeMatrixGreen[ index + nCols ], |
| 116 | + xyDerivativeMatrixGreen[index], xyDerivativeMatrixGreen[index + 1], xyDerivativeMatrixGreen[index + nCols + 1 ], xyDerivativeMatrixGreen[ index + nCols ] ); |
| 117 | + b = cubicInterpolation( blueMatrix[index], blueMatrix[index + 1], blueMatrix[index + nCols + 1 ], blueMatrix[ index + nCols ], |
| 118 | + xDerivativeMatrixBlue[index], xDerivativeMatrixBlue[index + 1], xDerivativeMatrixBlue[index + nCols + 1 ], xDerivativeMatrixBlue[ index + nCols ], |
| 119 | + yDerivativeMatrixBlue[index], yDerivativeMatrixBlue[index + 1], yDerivativeMatrixBlue[index + nCols + 1 ], yDerivativeMatrixBlue[ index + nCols ], |
| 120 | + xyDerivativeMatrixBlue[index], xyDerivativeMatrixBlue[index + 1], xyDerivativeMatrixBlue[index + nCols + 1 ], xyDerivativeMatrixBlue[ index + nCols ] ); |
| 121 | + |
| 122 | + dstImage.setPixel( j, i, qRgb( 255, 0, 0 ) ); |
| 123 | + currentSrcCol += nSrcPerDstX; |
| 124 | + } |
| 125 | + currentSrcRow += nSrcPerDstY; |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + //cleanup memory |
| 130 | + delete[] redMatrix; |
| 131 | + delete[] greenMatrix; |
| 132 | + delete[] blueMatrix; |
| 133 | + delete[] xDerivativeMatrixRed; |
| 134 | + delete[] xDerivativeMatrixGreen; |
| 135 | + delete[] xDerivativeMatrixBlue; |
| 136 | + delete[] yDerivativeMatrixRed; |
| 137 | + delete[] yDerivativeMatrixGreen; |
| 138 | + delete[] yDerivativeMatrixBlue; |
| 139 | + delete[] xyDerivativeMatrixRed; |
| 140 | + delete[] xyDerivativeMatrixGreen; |
| 141 | + delete[] xyDerivativeMatrixBlue; |
| 142 | +} |
| 143 | + |
| 144 | +void QgsCubicRasterResampler::xDerivativeMatrix( int nCols, int nRows, double* matrix, const int* colorMatrix ) |
| 145 | +{ |
| 146 | + double val; |
| 147 | + int index = 0; |
| 148 | + |
| 149 | + for( int i = 0; i < nRows; ++i ) |
| 150 | + { |
| 151 | + for( int j = 0; j < nCols; ++j ) |
| 152 | + { |
| 153 | + if( j < 1 ) |
| 154 | + { |
| 155 | + val = colorMatrix[index + 1] - colorMatrix[index]; |
| 156 | + } |
| 157 | + else if( j == ( nCols - 1 ) ) |
| 158 | + { |
| 159 | + val = colorMatrix[index] - colorMatrix[ index - 1 ]; |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + val = ( colorMatrix[index + 1] - colorMatrix[index - 1] ) / 2.0; |
| 164 | + } |
| 165 | + matrix[index] = val; |
| 166 | + ++index; |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +int QgsCubicRasterResampler::cubicInterpolation( double p1, double p2, double p3, double p4, double p1x, double p2x, double p3x, double p4x, |
| 172 | + double p1y, double p2y, double p3y, double p4y, double p1xy, double p2xy, double p3xy, double p4xy ) |
| 173 | +{ |
| 174 | +#if 0 |
| 175 | + //calculate 16 coefficients |
| 176 | + double a00 = p1 - 3 * p4 + 2* p3 - 3 * p1y + 9 * p4y - 6 * p3y + 2 * p1xy - 6 * p4xy+ 4 * p3xy; |
| 177 | + double a10 = 3 * p4 - 2 * p3 - 9* p4y + 6 * p3y + 6 * p4xy - 4 * p3xy; |
| 178 | + double a20 = 3 * p1y - 9 * p4y + 6 * p3y - 2 * p1xy + 6 * p4xy - 4 * p3xy; |
| 179 | + double a30 = 9 * p4y - 6 * p3y - 6 * p4xy + 4 * p3xy; |
| 180 | + double a01 = p2 - 2 * p4 + p3 - 3 * p2y + 6 * p4y -3 * p3y + 2 * p2xy - 4 * p4xy + 2 * p3xy; |
| 181 | + double a11 = - p4 + p3 + 3 * p4y + -3 * p3y - 2 * p4xy; |
| 182 | + double a21 = 3 * p2y - 6 * p4y + 3 * p3y -2 * p2xy + 4 * p4xy - 2 * p3xy; |
| 183 | + double a31 = - 3 * p4y + 3 * p3y + 2 * p4xy -2 * p3xy; |
| 184 | + double a02 = p1x - 3 * p4x + 2 * p3x - 2 * p1y + 6 * p4y - 4 * p3y + p1xy - 3 * p4xy + 2 * p3xy; |
| 185 | + double a12 = 3 * p4x - 2 * p3x - 6 * p4y + 4 * p3y + 3 * p4xy - 2 * p3xy; |
| 186 | + double a22 = - p1y + 3 * p4y - 2 * p3y + p1xy - 3 * p4xy + 2 * p3xy; |
| 187 | + double a32 = -3 * p4y + 2 * p3y + 3 * p4xy - 2 * p3xy; |
| 188 | + double a03 = p2x - 2 * p4x + p3x - 2 * p4y + 4 * p4y - 2 * p3y + p2xy - 2 * p4xy + p3xy; |
| 189 | + double a13 = - p4x + p3x + 2 * p4y - 2 * p3y - p4xy + p3xy; |
| 190 | +#endif //0 |
| 191 | + return 0; |
| 192 | +} |
0 commit comments