AlbumShaper  1.0a3
Classes | Functions
tilt_internal.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  DPoint

Functions

QRgb interpolatedPixelValue (double xp, double yp, QImage *image)
QRgb blendColors (QRgb color1, QRgb color2, double alpha)
DPoint findTwoLineIntersection (DPoint p1, DPoint p2, DPoint p3, DPoint p4)

Function Documentation

QRgb blendColors ( QRgb  color1,
QRgb  color2,
double  alpha 
)

Definition at line 359 of file tilt.cpp.

Referenced by interpolatedPixelValue().

{
  double alpha2 = 1.0-alpha;
  return qRgb( (int) QMAX( QMIN( 255, alpha2*qRed  (color1) + alpha*qRed(color2)   ), 0 ),
               (int) QMAX( QMIN( 255, alpha2*qGreen(color1) + alpha*qGreen(color2) ), 0 ),
               (int) QMAX( QMIN( 255, alpha2*qBlue (color1) + alpha*qBlue(color2)  ), 0 ) );
}
DPoint findTwoLineIntersection ( DPoint  p1,
DPoint  p2,
DPoint  p3,
DPoint  p4 
)

Definition at line 367 of file tilt.cpp.

References DPoint::x(), and DPoint::y().

Referenced by correctImageTilt().

{
  //----------------------------------------------
  //=== Case 1: neither line has a change in X ===
  //----------------------------------------------
  //If there is no change in x for both lines, 
  //either lines will NEVER or ALWAYS intersect.
  if(p1.x() == p2.x() &&
     p4.x() == p3.x())
  {
    //Ok, if their x values are equal, return 
    //intersection point as line A's point A.
    //Yes, this is a little arbitratry. But 
    //theoreticaly this section of code will almost
    //never be executed.
    if( p1.x() == p3.x() )
    { return DPoint( p1.x(), p1.y() ); }
    //Else lines will never intersect,
    //return pair (-32000,-32000)
    else
    { return DPoint( -32000, -32000 ); }
  } 
  //----------------------------------------------
  //Else, we know at least one of the lines 
  //does NOT have a slope of infinity!!!
  //----------------------------------------------
  
  //----------------------------------------------
  //=== Case 2: line A has no change in X      ===
  //----------------------------------------------
  //If line A has an infinite slope (no change in x)
  //we know line B does not have an infinite slope...
  else if( p1.x() == p2.x() )
  {
    double slopeB = ((double) (p4.y() - p3.y()) ) / (p4.x() - p3.x());
    
    double yInterceptB = p3.y() - slopeB*p3.x();
    
    //y = mx+b
    return DPoint( p2.x(), slopeB*p2.x() + yInterceptB );
  }
  //----------------------------------------------
  //=== Case 3: line B has no change in X      ===
  //----------------------------------------------
  //If line B has an infinite slope (no change in x)
  //we know line A does not have an infinite slope...
  else if( p4.x() == p3.x() )
  {
    double slopeA = ((double) (p2.y() - p1.y()) ) / (p2.x() - p1.x());
    
    double yInterceptA = p1.y() - slopeA*p1.x();
    
    //y = mx+b
    return DPoint( p4.x(), slopeA*p4.x() + yInterceptA );
  }
  //----------------------------------------------
  //=== Case 4: both lines have non infinite slopes ===
  //----------------------------------------------
  else
  {
    double slopeA = ((double) (p2.y() - p1.y()) ) / (p2.x() - p1.x());
    double slopeB = ((double) (p4.y() - p3.y()) ) / (p4.x() - p3.x());
    double yInterceptA = p1.y() - slopeA*p1.x();
    double yInterceptB = p3.y() - slopeB*p3.x();
    
    //y1 = mx1+b
    //y2 = nx2+c
    //at intersection y1=y2 and x1 = x2 so...
    //mx +b = nx + c
    //x(m-n) = c-b
    //x = (c-b)/(m-n)
    //where m and n are slope and
    //b and c are y-intercepts.
    //x = (c-b)/(m-n)
    double x = (yInterceptB - yInterceptA) / (slopeA - slopeB);
    return DPoint( x, (slopeA * x) + yInterceptA );
  }
}
QRgb interpolatedPixelValue ( double  xp,
double  yp,
QImage *  image 
)

Definition at line 312 of file tilt.cpp.

References blendColors(), height, and width.

Referenced by correctImageTilt().

{
  //do boundary checking to 
  //ensure we don't read beyond image boundaries
  if(xp < 0 || xp >= image->width() ||
     yp < 0 || yp >= image->height() )
    return qRgb( 0, 0, 0 );

  //get four pixel colors, 
  int x = (int)xp;
  int y = (int)yp;
  
  uchar* scanLine1 = image->scanLine( y );

  uchar* scanLine2;
  if( y < image->height() - 1 )
    scanLine2 = image->scanLine( y+1 );
  else
    scanLine2 = scanLine1;
  
  QRgb p1,p2,p3,p4;
  
  p1 = *((QRgb*)scanLine1+x);
  p3 = *((QRgb*)scanLine2+x);
        
  if( x < image->width() - 1)
  {
    p2 = *((QRgb*)scanLine1+x+1);
    p4 = *((QRgb*)scanLine2+x+1);     
  }
  else
  {
    p2 = p1;
    p4 = p3;
  }
  
  //blend four colors
  double alphaY = yp - y;
  double alphaX = xp - x;
  
  p1 = blendColors( p1, p2, alphaX );
  p3 = blendColors( p3, p4, alphaX );
  p1 = blendColors( p1, p3, alphaY );
  return p1;
}