File indexing completed on 2024-04-14 03:48:49

0001 #include "BilinearInterpolation.h"
0002 
0003 #include "ReadOnlyMapImage.h"
0004 
0005 #include <cmath>
0006 
0007 BilinearInterpolation::BilinearInterpolation( ReadOnlyMapImage * const mapImage )
0008     : InterpolationMethod( mapImage )
0009 {
0010 }
0011 
0012 QRgb BilinearInterpolation::interpolate( double const x, double const y )
0013 {
0014     int const x1 = x;
0015     int const x2 = x1 + 1;
0016     int const y1 = y;
0017     int const y2 = y1 + 1;
0018 
0019     QRgb const lowerLeftPixel = m_mapImage->pixel( x1, y1 );
0020     QRgb const lowerRightPixel = m_mapImage->pixel( x2, y1 );
0021     QRgb const upperLeftPixel = m_mapImage->pixel( x1, y2 );
0022     QRgb const upperRightPixel = m_mapImage->pixel( x2, y2 );
0023 
0024     // interpolate horizontically
0025     //
0026     // x2 - x    x2 - x
0027     // ------- = ------ = x1 + 1 - x = 1 - fractionX
0028     // x2 - x1      1
0029     //
0030     // x - x1    x - x1
0031     // ------- = ------ = fractionX
0032     // x2 - x1     1
0033 
0034     double const fractionX = x - x1;
0035     double const lowerMidRed   = ( 1.0 - fractionX ) * qRed( lowerLeftPixel )   + fractionX * qRed( lowerRightPixel );
0036     double const lowerMidGreen = ( 1.0 - fractionX ) * qGreen( lowerLeftPixel ) + fractionX * qGreen( lowerRightPixel );
0037     double const lowerMidBlue  = ( 1.0 - fractionX ) * qBlue( lowerLeftPixel )  + fractionX * qBlue( lowerRightPixel );
0038     double const lowerMidAlpha = ( 1.0 - fractionX ) * qAlpha( lowerLeftPixel ) + fractionX * qAlpha( lowerRightPixel );
0039 
0040     double const upperMidRed   = ( 1.0 - fractionX ) * qRed( upperLeftPixel )   + fractionX * qRed( upperRightPixel );
0041     double const upperMidGreen = ( 1.0 - fractionX ) * qGreen( upperLeftPixel ) + fractionX * qGreen( upperRightPixel );
0042     double const upperMidBlue  = ( 1.0 - fractionX ) * qBlue( upperLeftPixel )  + fractionX * qBlue( upperRightPixel );
0043     double const upperMidAlpha = ( 1.0 - fractionX ) * qAlpha( upperLeftPixel ) + fractionX * qAlpha( upperRightPixel );
0044 
0045     // interpolate vertically
0046     //
0047     // y2 - y    y2 - y
0048     // ------- = ------ = y1 + 1 - y = 1 - fractionY
0049     // y2 - y1      1
0050     //
0051     // y - y1    y - y1
0052     // ------- = ------ = fractionY
0053     // y2 - y1     1
0054 
0055     double const fractionY = y - y1;
0056     double const red   = ( 1.0 - fractionY ) * lowerMidRed   + fractionY * upperMidRed;
0057     double const green = ( 1.0 - fractionY ) * lowerMidGreen + fractionY * upperMidGreen;
0058     double const blue  = ( 1.0 - fractionY ) * lowerMidBlue  + fractionY * upperMidBlue;
0059     double const alpha = ( 1.0 - fractionY ) * lowerMidAlpha + fractionY * upperMidAlpha;
0060 
0061     return qRgba( round( red ), round( green ), round( blue ), round( alpha ));
0062 }