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

Go to the source code of this file.

Functions

QImage * correctImageTilt (QString filename, QPoint p1, QPoint p2, StatusWidget *status)

Function Documentation

QImage* correctImageTilt ( QString  filename,
QPoint  p1,
QPoint  p2,
StatusWidget status 
)

Definition at line 100 of file tilt.cpp.

References bottomRight, editedImage, findTwoLineIntersection(), StatusWidget::incrementProgress(), interpolatedPixelValue(), newProgress, StatusWidget::setStatus(), StatusWidget::showProgressBar(), topLeft, updateIncrement, DPoint::x(), and DPoint::y().

Referenced by EditingInterface::finishCorrectTilt().

{
  //first compute distance between two points or "radius"
  int dx = p2.x() - p1.x();
  int dy = p2.y() - p1.y();
  
  //determine tilt angle
  int delta = 0;
  
  //compute recirpocal of distance between points
  double recip_r = 1.0 / sqrt( (double) (dx*dx + dy*dy) );
  
  //compute angle with horizontal axis
  if( QABS(dx) > QABS(dy) )
  {
    delta = dy;
    if(dx > 0) delta = -delta;
  }
  //compute angle with vertical axis
  else
  {
    delta = dx;
    if(dy < 0) delta = -delta;
  }
  
  double sinTheta = (delta * recip_r);
  double theta = asin( sinTheta );
  double cosTheta = cos( theta );
  
  //if angle is 0 (improbable but possible) then quit now
  if( theta == 0 )
    return NULL;
  
  //load original and edited images
  QImage originalImage( filename );
  
  //convert to 32-bit depth if necessary
  if( originalImage.depth() < 32 ) { originalImage = originalImage.convertDepth( 32, Qt::AutoColor ); }
  
  QImage rotatedImage( originalImage.width(), originalImage.height(), originalImage.depth() );
  
  //setup progress bar
  QString statusMessage = qApp->translate( "correctImageTilt", "Correcting Tilt:" );
  status->showProgressBar( statusMessage, 200 );
  qApp->processEvents();  
  
  //during the first phase update the status bar for every 1% of image pixels that are processed
  int updateIncrement = (int) ( 0.01 * originalImage.width() * originalImage.height() );
  int newProgress = 0;
  
  //set each pixel to the rotated value
  double xp, yp;
  
  double w2 = 0.5 * rotatedImage.width();
  double h2 = 0.5 * rotatedImage.height();
  
  int x,y;
  uchar* scanLine;
  QRgb* rgb;
  for( y=0; y<rotatedImage.height(); y++)
  {   
    //iterate over each selected pixel in scanline
    scanLine = rotatedImage.scanLine(y);
    for( x=0; x<rotatedImage.width(); x++)
    {
      //compute unrotated coordinates
      xp = cosTheta*(x-w2) + sinTheta*(y-h2) + w2;
      yp = -sinTheta*(x-w2) + cosTheta*(y-h2) + h2;

      //set unrotated value
      rgb = ((QRgb*)scanLine+x);
      *rgb = interpolatedPixelValue( xp, yp, &originalImage);

      //update status bar if significant progress has been made since last update
      newProgress++;
      if(newProgress >= updateIncrement)
      {
        newProgress = 0;
        status->incrementProgress();
        qApp->processEvents();  
      }
      
    }
  }
  
  //find rotated corners
  double nTheta = -theta;
  double sinNTheta = sin( nTheta );
  double cosNTheta = cos( nTheta );
  
  DPoint topLeft = DPoint( cosNTheta*(-w2) + sinNTheta*(-h2) + w2,
                           -sinNTheta*(-w2) + cosNTheta*(-h2) + h2 );

  DPoint topRight = DPoint( cosNTheta*(w2) + sinNTheta*(-h2) + w2,                           
                            -sinNTheta*(w2) + cosNTheta*(-h2) + h2 );
  
  DPoint bottomLeft = DPoint( cosNTheta*(-w2) + sinNTheta*(h2) + w2,                           
                              -sinNTheta*(-w2) + cosNTheta*(h2) + h2 );
  
  DPoint bottomRight = DPoint( cosNTheta*(w2) + sinNTheta*(h2) + w2,                           
                               -sinNTheta*(w2) + cosNTheta*(h2) + h2 );
  
  //determine which of these points are which in their rotated form
  DPoint top, bottom, left, right;
  if( theta < 0 )
  {
    top = topRight;
    bottom = bottomLeft;
    left = topLeft;
    right = bottomRight;
  }
  else
  {
    top = topLeft;
    bottom = bottomRight;    
    left = bottomLeft;
    right = topRight;
  }
  
  //construct true corners
  DPoint trueTopLeft    ( 0, 0 );
  DPoint trueTopRight   ( rotatedImage.width()-1, 0 );
  DPoint trueBottomLeft ( 0, rotatedImage.height()-1 );
  DPoint trueBottomRight( rotatedImage.width()-1, rotatedImage.height()-1 );
  
  //find intersections with image boundary
  DPoint topEdgeL = findTwoLineIntersection( left, top, trueTopLeft, trueTopRight );
  DPoint topEdgeR = findTwoLineIntersection( top, right, trueTopLeft, trueTopRight );
                                            
  DPoint bottomEdgeL = findTwoLineIntersection( left, bottom, trueBottomLeft, trueBottomRight );
  DPoint bottomEdgeR = findTwoLineIntersection( bottom, right, trueBottomLeft, trueBottomRight );

  DPoint leftEdgeT = findTwoLineIntersection( left, top, trueTopLeft, trueBottomLeft );
  DPoint leftEdgeB = findTwoLineIntersection( left, bottom, trueTopLeft, trueBottomLeft );
    
  DPoint rightEdgeT = findTwoLineIntersection( right, top, trueTopRight, trueBottomRight );
  DPoint rightEdgeB = findTwoLineIntersection( right, bottom, trueTopRight, trueBottomRight );
  
  //shot rays out from image center to each true corner and find intersections with clipped corners
  DPoint center( (int)w2, (int)h2 );
  DPoint safeTopLeft     = findTwoLineIntersection( center, trueTopLeft, leftEdgeT, topEdgeL );
  DPoint safeTopRight    = findTwoLineIntersection( center, trueTopRight, rightEdgeT, topEdgeR );
  DPoint safeBottomLeft  = findTwoLineIntersection( center, trueBottomLeft, leftEdgeB, bottomEdgeL );
  DPoint safeBottomRight = findTwoLineIntersection( center, trueBottomRight, rightEdgeB, bottomEdgeR );
  
  //find constrained area
  double minY = QMAX( safeTopLeft.y(), safeTopRight.y() );
  double maxY = QMIN( safeBottomLeft.y(), safeBottomRight.y() );
  
  double minX = QMAX( safeTopLeft.x(), safeBottomLeft.x() );
  double maxX = QMIN( safeTopRight.x(), safeBottomRight.x() );

  //find contrained area in integer coordinates. this is semi-tricky.
  //if the minimum values decimal porition is nonzero then increment by one 
  // (eg 5.37 -> 6)
  int xMin = (int) minX;
  int xMax = (int) maxX;
  
  int yMin = (int) minY;
  int yMax = (int) maxY;
  
  if( xMin < minX ) xMin++;
  if( yMin < minY ) yMin++;
  
  //construct cropped rotated image
  QImage* editedImage = new QImage( xMax - xMin + 1,
                                    yMax - yMin + 1,
                                    rotatedImage.depth() );    

  //during the second phase update the status bar for every 1% of cropped pixels that are procesed
  updateIncrement = (int) ( 0.01 * editedImage->width() * editedImage->height() );
  newProgress = 0;
  
  int x2,y2;
  uchar* scanLine2;
  QRgb* rgb2;

  y2 = 0;
  for( y=yMin; y<=yMax; y++, y2++)
  {   
    //iterate over each selected pixel in scanline
    scanLine = rotatedImage.scanLine(y);
    scanLine2 = editedImage->scanLine(y2);

    x2 = 0;
    for( x=xMin; x<=xMax; x++, x2++)
    {
      rgb  = ((QRgb*)scanLine +x );
      rgb2 = ((QRgb*)scanLine2+x2);
      *rgb2 = *rgb;      

      //update status bar if significant progress has been made since last update
      newProgress++;
      if(newProgress >= updateIncrement)
      {
        newProgress = 0;
        status->incrementProgress();
        qApp->processEvents();  
      }
    
    }
  }
  
  //remove status bar
  status->setStatus( "" );
  qApp->processEvents();  
  
  //return pointer to edited image
  return editedImage;
}