00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <qimage.h>
00013 #include <qstring.h>
00014
00015
00016 #include "sharpen.h"
00017 #include "blur.h"
00018 #include "../tools/imageTools.h"
00019
00020
00021 void sharpenImage( QImage &image, float sigma,
00022 QPoint offset, QSize fullImageRes,
00023 QImage* edgeImage, bool blurEdges)
00024 {
00025
00026 QImage blurredImage = image.copy();
00027 blurImage( blurredImage, sigma );
00028
00029
00030 int x, y;
00031 QRgb *origRgb, *blurredRgb, *edgeRgb;
00032 uchar *origScanline;
00033 uchar *blurredScanline;
00034 uchar *edgesScanline = NULL;
00035
00036 for(y=0; y<image.height(); y++)
00037 {
00038 origScanline = image.scanLine(y);
00039 blurredScanline = blurredImage.scanLine(y);
00040 if( edgeImage != NULL )
00041 {
00042 int edgeY = ((edgeImage->height()-1) * (y+offset.y())) / (fullImageRes.height()-1);
00043 edgesScanline = edgeImage->scanLine(edgeY);
00044 }
00045
00046 for(x=0; x<image.width(); x++)
00047 {
00048
00049 origRgb = ((QRgb*)origScanline+x);
00050 double r1 = ((double)qRed(*origRgb) )/255.0;
00051 double g1 = ((double)qGreen(*origRgb) )/255.0;
00052 double b1 = ((double)qBlue(*origRgb) )/255.0;
00053
00054 blurredRgb = ((QRgb*)blurredScanline+x);
00055 double r2 = ((double)qRed(*blurredRgb) )/255.0;
00056 double g2 = ((double)qGreen(*blurredRgb) )/255.0;
00057 double b2 = ((double)qBlue(*blurredRgb) )/255.0;
00058
00059
00060 float alpha;
00061 if( edgeImage == NULL)
00062 alpha = 1.0f;
00063 else
00064 {
00065 int edgeX = ((edgeImage->width()-1) * (x+offset.x())) / (fullImageRes.width()-1);
00066 edgeRgb = ((QRgb*)edgesScanline+edgeX);
00067
00068 alpha = ((float) qRed( *edgeRgb )) / 255.0f;
00069
00070
00071 if(!blurEdges)
00072 alpha = 1.0f - alpha;
00073 }
00074
00075
00076 double h1,s1,v1;
00077 RGBtoHSV(r1,g1,b1,&h1,&s1,&v1);
00078
00079 double h2,s2,v2;
00080 RGBtoHSV(r2,g2,b2,&h2,&s2,&v2);
00081
00082
00083 v1 = (alpha * QMIN( QMAX(2*v1 - v2, 0), 1.0 )) + (1-alpha)*v1;
00084
00085
00086 HSVtoRGB( &r1,&g1,&b1, h1,s1,v1);
00087 int rp = (int) QMIN( QMAX((r1*255), 0), 255 );
00088 int gp = (int) QMIN( QMAX((g1*255), 0), 255 );
00089 int bp = (int) QMIN( QMAX((b1*255), 0), 255 );
00090
00091
00092 *origRgb = qRgb(rp,gp,bp);
00093 }
00094 }
00095
00096 }
00097