File indexing completed on 2024-04-14 03:47:47

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2013 Adrian Draghici <draghici.adrian.b@gmail.com>
0004 
0005 #include "ImageF.h"
0006 
0007 namespace Marble {
0008 
0009 ImageF::ImageF()
0010 {
0011 }
0012 
0013 uint ImageF::pixelF( const QImage& image, qreal x, qreal y )
0014 {
0015 
0016     const QRgb& topLeftPixel = image.pixel( (int) x, (int) y );
0017 
0018     // Bilinear interpolation to determine the color of a subpixel
0019 
0020     int iX = int( x );
0021     int iY = int( y );
0022 
0023     qreal fY = y - iY;
0024 
0025     // Interpolation in y-direction
0026     if ( ( iY + 1 ) < image.height() ) {
0027 
0028         QRgb bottomLeftPixel  =  image.pixel( iX, iY + 1 );
0029 
0030         // Blending the color values of the top left and bottom left point
0031         qreal ml_red   = ( 1.0 - fY ) * qRed  ( topLeftPixel  ) + fY * qRed  ( bottomLeftPixel  );
0032         qreal ml_green = ( 1.0 - fY ) * qGreen( topLeftPixel  ) + fY * qGreen( bottomLeftPixel  );
0033         qreal ml_blue  = ( 1.0 - fY ) * qBlue ( topLeftPixel  ) + fY * qBlue ( bottomLeftPixel  );
0034 
0035         // Interpolation in x-direction
0036         if ( iX + 1 < image.width() ) {
0037 
0038             qreal fX = x - iX;
0039 
0040             QRgb topRightPixel    =  image.pixel( iX + 1, iY );
0041             QRgb bottomRightPixel =  image.pixel( iX + 1, iY + 1 );
0042 
0043             // Blending the color values of the top right and bottom right point
0044             qreal mr_red   = ( 1.0 - fY ) * qRed  ( topRightPixel ) + fY * qRed  ( bottomRightPixel );
0045             qreal mr_green = ( 1.0 - fY ) * qGreen( topRightPixel ) + fY * qGreen( bottomRightPixel );
0046             qreal mr_blue  = ( 1.0 - fY ) * qBlue ( topRightPixel ) + fY * qBlue ( bottomRightPixel );
0047 
0048             // Blending the color values of the resulting middle left and middle right points
0049             int mm_red   = int( ( ( 1.0 - fX ) * ml_red   + fX * mr_red   ) );
0050             int mm_green = int( ( ( 1.0 - fX ) * ml_green + fX * mr_green ) );
0051             int mm_blue  = int( ( ( 1.0 - fX ) * ml_blue  + fX * mr_blue  ) );
0052 
0053             return qRgb( mm_red, mm_green, mm_blue );
0054         }
0055         else {
0056             return qRgb( ml_red, ml_green, ml_blue );
0057         }
0058     }
0059     else {
0060         // Interpolation in x-direction
0061         if ( iX + 1 < image.width() ) {
0062 
0063             qreal fX = x - iX;
0064 
0065             if ( fX == 0.0 )
0066                 return topLeftPixel;
0067 
0068             QRgb topRightPixel    =  image.pixel( iX + 1, iY );
0069 
0070             // Blending the color values of the top left and top right point
0071             int tm_red   = int( ( ( 1.0 - fX ) * qRed  ( topLeftPixel ) + fX * qRed  ( topRightPixel ) ) );
0072             int tm_green = int( ( ( 1.0 - fX ) * qGreen( topLeftPixel ) + fX * qGreen( topRightPixel ) ) );
0073             int tm_blue  = int( ( ( 1.0 - fX ) * qBlue ( topLeftPixel ) + fX * qBlue ( topRightPixel ) ) );
0074 
0075             return qRgb( tm_red, tm_green, tm_blue );
0076         }
0077     }
0078 
0079     return topLeftPixel;
0080 }
0081 
0082 }