Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

sharpen.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 "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   //construct blur copy
00026   QImage blurredImage = image.copy();
00027   blurImage( blurredImage, sigma );
00028   
00029   //iterate over each pixel and adjust luminance value
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       //get rgb triplets
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       //sharpen the entire thing!
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         //blur regions, not edges
00071         if(!blurEdges)
00072           alpha = 1.0f - alpha;
00073       }
00074       
00075       //convert to hsv
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       //reset v
00083       v1  = (alpha * QMIN( QMAX(2*v1 - v2, 0), 1.0 )) + (1-alpha)*v1;
00084       
00085       //convert adjusted color back to rgb colorspace and clamp
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       //set adjusted color value
00092       *origRgb = qRgb(rp,gp,bp);
00093     } //x
00094   } //y
00095 
00096 }
00097 //==============================================

Generated on Sat Apr 2 05:44:04 2005 for AlbumShaper by  doxygen 1.3.9.1