crop.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003-2005 by Will Stokes
00003 //==============================================
00004 //  This program is free software; you can redistribute it
00005 //  and/or modify it under the terms of the GNU General
00006 //  Public License as published by the Free Software
00007 //  Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //==============================================
00010 
00011 //Systemwide includes
00012 #include <qimage.h>
00013 #include <qstring.h>
00014 
00015 //Projectwide includes
00016 #include "crop.h"
00017 
00018 //----------------------------------------------
00019 // Inputs:
00020 // -------
00021 // QString filename   - location of original image on disk
00022 // QPoint topLeft     - top left of selected region
00023 // QPoint bottomRight - bottom right of selected region
00024 //
00025 // Outputs:
00026 // --------
00027 // QImage* returned - constructed image
00028 //
00029 // Description:
00030 // ------------
00031 // This method constructs a cropped form of the original image
00032 // within the selected region by simply copying over pixel values.
00033 //----------------------------------------------
00034 
00035 //==============================================
00036 QImage* cropImage( QString filename, QPoint topLeft, QPoint bottomRight )
00037 {
00038   //load original image
00039   QImage origImage( filename );
00040   
00041   //convert to 32-bit depth if necessary
00042   if( origImage.depth() < 32 ) { origImage = origImage.convertDepth( 32, Qt::AutoColor ); }
00043   
00044   //construct cropped image
00045   QImage* croppedImage = new QImage(bottomRight.x() - topLeft.x() + 1, 
00046                                     bottomRight.y() - topLeft.y() + 1,
00047                                     origImage.depth());  
00048   
00049   //iterate over each selected scanline 
00050   int xOrig, yOrig;
00051   int xCropped, yCropped;
00052   uchar *origScanLine, *croppedScanLine;
00053   
00054   for( yOrig=topLeft.y(),yCropped=0;    yOrig<=bottomRight.y();     yOrig++, yCropped++)
00055   {   
00056     //iterate over each selected pixel in scanline
00057     origScanLine    = origImage.scanLine(yOrig);
00058     croppedScanLine = croppedImage->scanLine(yCropped);
00059     
00060     for( xOrig=topLeft.x(),xCropped=0; xOrig<=bottomRight.x(); xOrig++,xCropped++)
00061     {
00062       //copy pixel color from original image to cropped image
00063       *((QRgb*)croppedScanLine+xCropped) = *((QRgb*)origScanLine+xOrig);      
00064     }
00065   }
00066   
00067   //return pointer to cropped image
00068   return croppedImage;  
00069 }
00070 //==============================================

Generated on Wed Jan 24 05:38:05 2007 for AlbumShaper by  doxygen 1.5.1