AlbumShaper  1.0a3
crop.cpp
Go to the documentation of this file.
1 //==============================================
2 // copyright : (C) 2003-2005 by Will Stokes
3 //==============================================
4 // This program is free software; you can redistribute it
5 // and/or modify it under the terms of the GNU General
6 // Public License as published by the Free Software
7 // Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //==============================================
10 
11 //Systemwide includes
12 #include <qimage.h>
13 #include <qstring.h>
14 
15 //Projectwide includes
16 #include "crop.h"
17 
18 //----------------------------------------------
19 // Inputs:
20 // -------
21 // QString filename - location of original image on disk
22 // QPoint topLeft - top left of selected region
23 // QPoint bottomRight - bottom right of selected region
24 //
25 // Outputs:
26 // --------
27 // QImage* returned - constructed image
28 //
29 // Description:
30 // ------------
31 // This method constructs a cropped form of the original image
32 // within the selected region by simply copying over pixel values.
33 //----------------------------------------------
34 
35 //==============================================
36 QImage* cropImage( QString filename, QPoint topLeft, QPoint bottomRight )
37 {
38  //load original image
39  QImage origImage( filename );
40 
41  //convert to 32-bit depth if necessary
42  if( origImage.depth() < 32 ) { origImage = origImage.convertDepth( 32, Qt::AutoColor ); }
43 
44  //construct cropped image
45  QImage* croppedImage = new QImage(bottomRight.x() - topLeft.x() + 1,
46  bottomRight.y() - topLeft.y() + 1,
47  origImage.depth());
48 
49  //iterate over each selected scanline
50  int xOrig, yOrig;
51  int xCropped, yCropped;
52  uchar *origScanLine, *croppedScanLine;
53 
54  for( yOrig=topLeft.y(),yCropped=0; yOrig<=bottomRight.y(); yOrig++, yCropped++)
55  {
56  //iterate over each selected pixel in scanline
57  origScanLine = origImage.scanLine(yOrig);
58  croppedScanLine = croppedImage->scanLine(yCropped);
59 
60  for( xOrig=topLeft.x(),xCropped=0; xOrig<=bottomRight.x(); xOrig++,xCropped++)
61  {
62  //copy pixel color from original image to cropped image
63  *((QRgb*)croppedScanLine+xCropped) = *((QRgb*)origScanLine+xOrig);
64  }
65  }
66 
67  //return pointer to cropped image
68  return croppedImage;
69 }
70 //==============================================
QPoint bottomRight
QPoint topLeft
QImage * cropImage(QString filename, QPoint topLeft, QPoint bottomRight)
Definition: crop.cpp:36