AlbumShaper  1.0a3
Functions
imageTools.cpp File Reference
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <qstring.h>
#include <qimage.h>
#include <qdir.h>
#include <qfile.h>
#include <qcolor.h>
#include <qpoint.h>
#include <qpainter.h>
#include "imageTools.h"
#include "jpeg/jpegTools.h"
#include "jpeg/jpegSize.h"
#include "../../config.h"
Include dependency graph for imageTools.cpp:

Go to the source code of this file.

Functions

bool isJpeg (const char *filename)
 Checks to see if an image is a valid jpg by seeing if the image dimensions can be read.
void calcScaledImageDimensions (int origWidth, int origHeight, int idealWidth, int idealHeight, int &width, int &height)
 Computes scale of image dimensions while respecting aspect ratio, equivalent to a QImage::scaleMin without actually scaling any image.
void constructImages (QString imageName, QImage &slideshowImage, QImage &thumbnailImage)
 Constructs slideshow and thumbnail images for a full sized image.
bool transformImage (QString fileIn, QString fileOut, TRANSFORM_CODE transformation)
 Apply image transformation on image.
bool scaleImage (QString fileIn, QString fileOut, int newWidth, int newHeight)
 Scale image and save copy to disk.
bool scaleImage (QString fileIn, QImage &scaledImage, int targetWidth, int targetHeight)
 Loaded scaled version of image.
bool getImageSize (const char *filename, QSize &size)
 Get image dimensions.
bool getImageSize (const char *filename, int &width, int &height)
 Get image dimensions.
double RGBtoL (QRgb *rgb)
 find luminance of a rgb color triplet
void RGBtoHSV (double r, double g, double b, double *h, double *s, double *v)
 Convert a RGB color triplet to HSV.
void HSVtoRGB (double *r, double *g, double *b, double h, double s, double v)
 Convert a HSV color triplet to RGB.

Function Documentation

void calcScaledImageDimensions ( int  origWidth,
int  origHeight,
int  idealWidth,
int  idealHeight,
int &  width,
int &  height 
)

Computes scale of image dimensions while respecting aspect ratio, equivalent to a QImage::scaleMin without actually scaling any image.

Definition at line 39 of file imageTools.cpp.

Referenced by AlbumStatistics::AlbumStatistics(), constructImages(), SubalbumPreviewWidget::createSubalbumPixmap(), PhotoDescEdit::PhotoDescEdit(), EditingInterface::rotateSelection(), EditingInterface::selectAspectRatio(), Subalbum::setRepresentativeImage(), and Album::setRepresentativeImages().

{
  //if original dimensions are within ideal new size then use
  //original dimensions
  if(origWidth <= idealWidth &&
     origHeight <= idealHeight)
  {
    width = origWidth;
    height = origHeight;
    return;
  }

  //else find dimension which is way over bounds
  float widthRatio = ((float)idealWidth) / ((float)origWidth);
  float heightRatio = ((float)idealHeight) / ((float)origHeight);

  if(widthRatio < heightRatio)
  {
    width = idealWidth;
    height = (int)((((float)idealWidth) / ((float)origWidth)) * ((float)origHeight));
  }
  else
  {
    height = idealHeight;
    width = (int)((((float)idealHeight) / ((float)origHeight)) * ((float)origWidth));
  }
}
void constructImages ( QString  imageName,
QImage &  slideshowImage,
QImage &  thumbnailImage 
)

Constructs slideshow and thumbnail images for a full sized image.

Definition at line 69 of file imageTools.cpp.

References calcScaledImageDimensions(), getImageSize(), scaleImage(), SLIDESHOW_HEIGHT, SLIDESHOW_WIDTH, THUMBNAIL_HEIGHT, and THUMBNAIL_WIDTH.

Referenced by Photo::constructSmallerImages().

{
  //---------------------------------------------------------
  //obtain original image width and height
  int origWidth, origHeight;
  getImageSize( imageName, origWidth, origHeight );
  
  //compute dimensions of unhapped scaled thumbnail and slideshow images
  int thumbWidth, thumbHeight;
  calcScaledImageDimensions( origWidth, origHeight,
                             THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT,
                             thumbWidth, thumbHeight);
  
  int slideWidth, slideHeight;
  calcScaledImageDimensions( origWidth, origHeight,
                             SLIDESHOW_WIDTH, SLIDESHOW_HEIGHT,
                             slideWidth, slideHeight);
  //---------------------------------------------------------
  //create slide show image
  
  //first scale full image to unpadded dimensions
  QImage temp;
  scaleImage( imageName, temp, slideWidth, slideHeight );
  slideWidth = temp.width();
  slideHeight = temp.height();
 
  //create slideshow image and fill with white
  slideshowImage.create( SLIDESHOW_WIDTH, SLIDESHOW_HEIGHT, 32 );
  slideshowImage.fill( Qt::white.rgb() );
  
  //paint unpadded scaled image
  int xDiff = SLIDESHOW_WIDTH - slideWidth;
  int yDiff = SLIDESHOW_HEIGHT - slideHeight;
  bitBlt( &slideshowImage, xDiff/2, yDiff/2,
          &temp, 0, 0, slideWidth, slideHeight );
   
  //---------------------------------------------------------
  //create thumnail image
  scaleImage( imageName, thumbnailImage, thumbWidth, thumbHeight );
  //---------------------------------------------------------
}
bool getImageSize ( const char *  filename,
QSize &  size 
)
bool getImageSize ( const char *  filename,
int &  width,
int &  height 
)

Get image dimensions.

Definition at line 201 of file imageTools.cpp.

References getJPEGSize(), height, and width.

{
  //if file is jpeg use faster method
  QString extension = QFileInfo(filename).extension(false).lower();
  if( extension.compare("jpeg") == 0 ||
      extension.compare("jpg") == 0 )
    return getJPEGSize( QFile::encodeName(filename),
                        width, height );

  //load entire image to qimage object in order to determine size
  QImage image(filename);
  width = image.width();
  height = image.height();
  return true;
}
void HSVtoRGB ( double *  r,
double *  g,
double *  b,
double  h,
double  s,
double  v 
)

Convert a HSV color triplet to RGB.

Definition at line 264 of file imageTools.cpp.

References q.

Referenced by HistogramEditor::adjustImage(), SelectionInterface::constructDisplayImages(), embossEffect(), enhanceImageContrast(), SelectionPlacementInterface::SelectionPlacementInterface(), and sharpenImage().

{
        int i;
        double f, p, q, t;
  
        if( s == 0 ) {
                // achromatic (grey)
                *r = *g = *b = v;
                return;
        }
  
        h /= 60;                        // sector 0 to 5
        i = (int)floor( h );
        f = h - i;                      // factorial part of h
        p = v * ( 1 - s );
        q = v * ( 1 - s * f );
        t = v * ( 1 - s * ( 1 - f ) );
  
        switch( i ) {
                case 0:
                        *r = v;
                        *g = t;
                        *b = p;
                        break;
                case 1:
                        *r = q;
                        *g = v;
                        *b = p;
                        break;
                case 2:
                        *r = p;
                        *g = v;
                        *b = t;
                        break;
                case 3:
                        *r = p;
                        *g = q;
                        *b = v;
                        break;
                case 4:
                        *r = t;
                        *g = p;
                        *b = v;
                        break;
                default:                // case 5:
                        *r = v;
                        *g = p;
                        *b = q;
                        break;
        }  
}
bool isJpeg ( const char *  filename)

Checks to see if an image is a valid jpg by seeing if the image dimensions can be read.

Definition at line 33 of file imageTools.cpp.

References getJPEGSize().

Referenced by Photo::setImage(), and transformImage().

{
  int w,h;
  return getJPEGSize( QFile::encodeName(filename), w, h );
}
void RGBtoHSV ( double  r,
double  g,
double  b,
double *  h,
double *  s,
double *  v 
)

Convert a RGB color triplet to HSV.

Definition at line 231 of file imageTools.cpp.

References b.

Referenced by HistogramEditor::adjustImage(), SelectionInterface::constructDisplayImages(), embossEffect(), enhanceImageContrast(), RGBtoL(), SelectionPlacementInterface::SelectionPlacementInterface(), and sharpenImage().

{
        double min, max, delta;
  
        min = QMIN(QMIN( r, g), b );
        max = QMAX(QMAX( r, g), b );
        *v = max;                               // v
  
        delta = max - min;
  
        if( max != 0 )
                *s = delta / max;               // s
        else {
                // r = g = b = 0                // s = 0, v is undefined
                *s = 0;
                *h = -1;
                return;
        }
  
        if( r == max )
                *h = ( g - b ) / delta;         // between yellow & magenta
        else if( g == max )
                *h = 2 + ( b - r ) / delta;     // between cyan & yellow
        else
                *h = 4 + ( r - g ) / delta;     // between magenta & cyan
  
        *h *= 60;                               // degrees
        if( *h < 0 )
                *h += 360;
  
}
double RGBtoL ( QRgb *  rgb)

find luminance of a rgb color triplet

Definition at line 217 of file imageTools.cpp.

References b, and RGBtoHSV().

Referenced by enhanceImageContrast().

{
  double r = ((double)qRed(*rgb)   )/255.0;
  double g = ((double)qGreen(*rgb) )/255.0;
  double b = ((double)qBlue(*rgb)  )/255.0;

  double h,s,v;
  RGBtoHSV(r,g,b,&h,&s,&v);
  return 255.0*v;
}
bool scaleImage ( QString  fileIn,
QString  fileOut,
int  newWidth,
int  newHeight 
)
bool scaleImage ( QString  fileIn,
QImage &  scaledImage,
int  targetWidth,
int  targetHeight 
)

Loaded scaled version of image.

Definition at line 171 of file imageTools.cpp.

References scaleJPEG().

{
  //if file is jpeg use faster method
  QString extension = QFileInfo(fileIn).extension(false).lower();
  if( extension.compare("jpeg") == 0 ||
      extension.compare("jpg") == 0 )
    return scaleJPEG( QFile::encodeName(fileIn), scaledImage, targetWidth, targetHeight );
  
  //use slow smooth-scale method for scaling image.
  //clamp scaling to <= 2x
  QImage orig(fileIn);  
  if(QMIN( ((float)targetWidth)/orig.width(), ((float)targetHeight)/orig.height() ) > 2)    
  {
    targetWidth = 2*orig.width();
    targetHeight = 2*orig.height();
  }
  
  scaledImage = orig.smoothScale( targetWidth, targetHeight, QImage::ScaleMin );
  return true;
}
bool transformImage ( QString  fileIn,
QString  fileOut,
TRANSFORM_CODE  transformation 
)

Apply image transformation on image.

Definition at line 112 of file imageTools.cpp.

References FLIP_H, isJpeg(), ROTATE_270, ROTATE_90, and transformJPEG().

Referenced by Photo::applyTransformation(), and EditingInterface::rotateFlip().

{
  //if file is jpeg use faster method
  if( isJpeg(fileIn) )
    return transformJPEG( fileIn, fileOut, transformation );
  
  //load image
  QImage origImage(fileIn);
  QImage transformedImage;
  
  //transform image
  if(transformation == ROTATE_90)
  {
    if(!transformedImage.create( origImage.height(), origImage.width(), origImage.depth() ) )
      return false;
    
    int x,y;
    for(x=0; x < origImage.height(); x++)
    {
      for(y=0; y < origImage.width(); y++)
        transformedImage.setPixel(origImage.height() - 1 - x, y, origImage.pixel(y, x) );
    }
  }
  else if(transformation == ROTATE_270)
  {
    if(!transformedImage.create( origImage.height(), origImage.width(), origImage.depth() ) )
      return false;
    
    int x,y;
    for(x=0; x < origImage.height(); x++)
    {
      for(y=0; y < origImage.width(); y++)
          transformedImage.setPixel(x, origImage.width() - 1 - y, origImage.pixel(y, x) );
    }
  }
  else if(transformation == FLIP_H)
  {  transformedImage = origImage.mirror(false,true); }
  else
  {  transformedImage = origImage.mirror(true,false); }
  
  //save out transformed image  
  transformedImage.save( fileOut, "JPEG", 95 );
  return true;  
}