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 //construct cropped image 00042 QImage* croppedImage = new QImage(bottomRight.x() - topLeft.x() + 1, 00043 bottomRight.y() - topLeft.y() + 1, 00044 origImage.depth()); 00045 00046 //iterate over each selected scanline 00047 int xOrig, yOrig; 00048 int xCropped, yCropped; 00049 uchar *origScanLine, *croppedScanLine; 00050 00051 for( yOrig=topLeft.y(),yCropped=0; yOrig<=bottomRight.y(); yOrig++, yCropped++) 00052 { 00053 //iterate over each selected pixel in scanline 00054 origScanLine = origImage.scanLine(yOrig); 00055 croppedScanLine = croppedImage->scanLine(yCropped); 00056 00057 for( xOrig=topLeft.x(),xCropped=0; xOrig<=bottomRight.x(); xOrig++,xCropped++) 00058 { 00059 //copy pixel color from original image to cropped image 00060 *((QRgb*)croppedScanLine+xCropped) = *((QRgb*)origScanLine+xOrig); 00061 } 00062 } 00063 00064 //return pointer to cropped image 00065 return croppedImage; 00066 } 00067 //==============================================