<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>AlbumShaper: sharpen.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.5.9 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <h1>sharpen.cpp</h1><a href="sharpen_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">//==============================================</span> <a name="l00002"></a>00002 <span class="comment">// copyright : (C) 2003-2005 by Will Stokes</span> <a name="l00003"></a>00003 <span class="comment">//==============================================</span> <a name="l00004"></a>00004 <span class="comment">// This program is free software; you can redistribute it</span> <a name="l00005"></a>00005 <span class="comment">// and/or modify it under the terms of the GNU General</span> <a name="l00006"></a>00006 <span class="comment">// Public License as published by the Free Software</span> <a name="l00007"></a>00007 <span class="comment">// Foundation; either version 2 of the License, or</span> <a name="l00008"></a>00008 <span class="comment">// (at your option) any later version.</span> <a name="l00009"></a>00009 <span class="comment">//==============================================</span> <a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="comment">//Systemwide includes</span> <a name="l00012"></a>00012 <span class="preprocessor">#include <qimage.h></span> <a name="l00013"></a>00013 <span class="preprocessor">#include <qstring.h></span> <a name="l00014"></a>00014 <a name="l00015"></a>00015 <span class="comment">//Projectwide includes</span> <a name="l00016"></a>00016 <span class="preprocessor">#include "<a class="code" href="sharpen_8h.html">sharpen.h</a>"</span> <a name="l00017"></a>00017 <span class="preprocessor">#include "<a class="code" href="blur_8h.html">blur.h</a>"</span> <a name="l00018"></a>00018 <span class="preprocessor">#include "../tools/imageTools.h"</span> <a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="comment">//----------------------------------------------</span> <a name="l00021"></a>00021 <span class="comment">// Inputs:</span> <a name="l00022"></a>00022 <span class="comment">// -------</span> <a name="l00023"></a>00023 <span class="comment">// QImage& image - image to blur</span> <a name="l00024"></a>00024 <span class="comment">// float sigma - how much to blur it</span> <a name="l00025"></a>00025 <span class="comment">// QPoint offset - offset within edge image we're working on</span> <a name="l00026"></a>00026 <span class="comment">// QSize fullImageRes - resolution of the full size image</span> <a name="l00027"></a>00027 <span class="comment">// QImage* edgeImage - an edge image constructing using the full size image</span> <a name="l00028"></a>00028 <span class="comment">// bool blurEdges - are we sharpening edges or regions</span> <a name="l00029"></a>00029 <span class="comment">//</span> <a name="l00030"></a>00030 <span class="comment">// Outputs:</span> <a name="l00031"></a>00031 <span class="comment">// --------</span> <a name="l00032"></a>00032 <span class="comment">// Nothing returned, we'll modify the image passed by refference in place</span> <a name="l00033"></a>00033 <span class="comment">//</span> <a name="l00034"></a>00034 <span class="comment">// Description:</span> <a name="l00035"></a>00035 <span class="comment">// ------------</span> <a name="l00036"></a>00036 <span class="comment">// The common approach to sharpening images is subtract a </span> <a name="l00037"></a>00037 <span class="comment">// blurred version of an image using the following equation:</span> <a name="l00038"></a>00038 <span class="comment">//</span> <a name="l00039"></a>00039 <span class="comment">// v' = 2*v - vBlur</span> <a name="l00040"></a>00040 <span class="comment">//</span> <a name="l00041"></a>00041 <span class="comment">// ...where v is the original value (luminance) for a given pixel, </span> <a name="l00042"></a>00042 <span class="comment">// vBlur is the blurred value, and v' is the end result.</span> <a name="l00043"></a>00043 <span class="comment">//</span> <a name="l00044"></a>00044 <span class="comment">// While one could apply this blur-subtraction in the individual color channels</span> <a name="l00045"></a>00045 <span class="comment">// you will likely encounter strange artifacts at color channel boundaries where new</span> <a name="l00046"></a>00046 <span class="comment">// colors are introducted. Sharpening in the value/luminance domain helps bright out</span> <a name="l00047"></a>00047 <span class="comment">// image contrast without introducing color artifacts.</span> <a name="l00048"></a>00048 <span class="comment">//</span> <a name="l00049"></a>00049 <span class="comment">// Unfortunately, sharpening using this approach will magnify all image contrast, both</span> <a name="l00050"></a>00050 <span class="comment">// somewhat strong edges and low level noise. We'd like to be able to aggressively sharpen</span> <a name="l00051"></a>00051 <span class="comment">// images without magnifying CCD/film grain noise, but how?</span> <a name="l00052"></a>00052 <span class="comment">//</span> <a name="l00053"></a>00053 <span class="comment">// A somewhat popular solution to this problem is to use an edge image. Constructing edge images</span> <a name="l00054"></a>00054 <span class="comment">// can be difficult, but when provided such information can tell us when to to sharpen and when not to, or</span> <a name="l00055"></a>00055 <span class="comment">// used more wisely, how to blend the sharpend data with the unsharpened original image data seemlessly.</span> <a name="l00056"></a>00056 <span class="comment">// Grayscale edge images can be used in this way by first blurring slightly, then dividing the </span> <a name="l00057"></a>00057 <span class="comment">// value component of a pixel by 255 to get an alpha value. Near edges the value will be closer to </span> <a name="l00058"></a>00058 <span class="comment">// 255 and the resulting alpha will be closer to 1. In between regions where we don't want to </span> <a name="l00059"></a>00059 <span class="comment">// enhance noise by sharpening alpha values will be close to 0, preventing aggressively </span> <a name="l00060"></a>00060 <span class="comment">// sharpened vaues from being used.</span> <a name="l00061"></a>00061 <span class="comment">//</span> <a name="l00062"></a>00062 <span class="comment">// Algorithm:</span> <a name="l00063"></a>00063 <span class="comment">// ----------</span> <a name="l00064"></a>00064 <span class="comment">// The algorithm works as follows:</span> <a name="l00065"></a>00065 <span class="comment">// 1.) The input image is blurred using the sigma value. The large the sigma value </span> <a name="l00066"></a>00066 <span class="comment">// the more the input image is blurred and the more pronounced edges will become.</span> <a name="l00067"></a>00067 <span class="comment">// 2.) We iterate over each image pixel, fetching the color values of the original and blurred forms</span> <a name="l00068"></a>00068 <span class="comment">// of the image, as well as the color of the give pixel within the edge image.</span> <a name="l00069"></a>00069 <span class="comment">// </span> <a name="l00070"></a>00070 <span class="comment">// An alpha value is computed using the edge image pixel color, which in turn is used</span> <a name="l00071"></a>00071 <span class="comment">// to compute the blended pixel value after sharpening:</span> <a name="l00072"></a>00072 <span class="comment">//</span> <a name="l00073"></a>00073 <span class="comment">// alpha = edgeColor / 255</span> <a name="l00074"></a>00074 <span class="comment">// v' = alpha* min( max( 2*v - vBlur, 0 ), 1) + (1-alpha)*v;</span> <a name="l00075"></a>00075 <span class="comment">//</span> <a name="l00076"></a>00076 <span class="comment">// Finally, we convert the pixel color back to RGB space and write back</span> <a name="l00077"></a>00077 <span class="comment">// to the resulting sharpened image.</span> <a name="l00078"></a>00078 <span class="comment">//</span> <a name="l00079"></a>00079 <span class="comment">// This algorithm works and was initially tested without the use of an edge image. When</span> <a name="l00080"></a>00080 <span class="comment">// no edge image is provided alpha is simply set to 1 and full sharpening is applied</span> <a name="l00081"></a>00081 <span class="comment">// to every image pixel.</span> <a name="l00082"></a>00082 <span class="comment">//</span> <a name="l00083"></a>00083 <span class="comment">// Future work:</span> <a name="l00084"></a>00084 <span class="comment">// ------------</span> <a name="l00085"></a>00085 <span class="comment">// Further work needs to be done regarding bluring/sharpening edges and region independently.</span> <a name="l00086"></a>00086 <span class="comment">// The "blurEdges" param allows the algorithm to concentrate sharpening on regions instead of</span> <a name="l00087"></a>00087 <span class="comment">// object boundaries when an edge image is provided; however, such usage is not well understood</span> <a name="l00088"></a>00088 <span class="comment">// or used at this time.</span> <a name="l00089"></a>00089 <span class="comment">//----------------------------------------------</span> <a name="l00090"></a>00090 <a name="l00091"></a>00091 <span class="comment">//==============================================</span> <a name="l00092"></a><a class="code" href="sharpen_8h.html#92c3a61ec589ffe5d0aa1811add7a22e">00092</a> <span class="keywordtype">void</span> <a class="code" href="sharpen_8cpp.html#92c3a61ec589ffe5d0aa1811add7a22e">sharpenImage</a>( QImage &image, <span class="keywordtype">float</span> sigma, <a name="l00093"></a>00093 QPoint offset, QSize fullImageRes, <a name="l00094"></a>00094 QImage* <a class="code" href="blur_8cpp.html#8bb23dbf5a4b8bb7e3e90e0567712bdd">edgeImage</a>, <span class="keywordtype">bool</span> blurEdges) <a name="l00095"></a>00095 { <a name="l00096"></a>00096 <span class="comment">//construct blur copy</span> <a name="l00097"></a>00097 QImage blurredImage = image.copy(); <a name="l00098"></a>00098 <a class="code" href="blur_8cpp.html#e7be57816a66918a26e567655f4b8e1c">blurImage</a>( blurredImage, sigma ); <a name="l00099"></a>00099 <a name="l00100"></a>00100 <span class="comment">//iterate over each pixel and adjust luminance value</span> <a name="l00101"></a>00101 <span class="keywordtype">int</span> x, y; <a name="l00102"></a>00102 QRgb *origRgb, *blurredRgb, *edgeRgb; <a name="l00103"></a>00103 uchar *origScanline; <a name="l00104"></a>00104 uchar *blurredScanline; <a name="l00105"></a>00105 uchar *edgesScanline = NULL; <a name="l00106"></a>00106 <a name="l00107"></a>00107 <span class="keywordflow">for</span>(y=0; y<image.height(); y++) <a name="l00108"></a>00108 { <a name="l00109"></a>00109 origScanline = image.scanLine(y); <a name="l00110"></a>00110 blurredScanline = blurredImage.scanLine(y); <a name="l00111"></a>00111 <span class="keywordflow">if</span>( edgeImage != NULL ) <a name="l00112"></a>00112 { <a name="l00113"></a>00113 <span class="keywordtype">int</span> edgeY = ((edgeImage->height()-1) * (y+offset.y())) / (fullImageRes.height()-1); <a name="l00114"></a>00114 edgesScanline = edgeImage->scanLine(edgeY); <a name="l00115"></a>00115 } <a name="l00116"></a>00116 <a name="l00117"></a>00117 <span class="keywordflow">for</span>(x=0; x<image.width(); x++) <a name="l00118"></a>00118 { <a name="l00119"></a>00119 <span class="comment">//get rgb triplets</span> <a name="l00120"></a>00120 origRgb = ((QRgb*)origScanline+x); <a name="l00121"></a>00121 <span class="keywordtype">double</span> r1 = ((double)qRed(*origRgb) )/255.0; <a name="l00122"></a>00122 <span class="keywordtype">double</span> g1 = ((double)qGreen(*origRgb) )/255.0; <a name="l00123"></a>00123 <span class="keywordtype">double</span> <a class="code" href="blur_8cpp.html#0a909289ec9fbfaa70c0e112ec9e3b15">b1</a> = ((double)qBlue(*origRgb) )/255.0; <a name="l00124"></a>00124 <a name="l00125"></a>00125 blurredRgb = ((QRgb*)blurredScanline+x); <a name="l00126"></a>00126 <span class="keywordtype">double</span> r2 = ((double)qRed(*blurredRgb) )/255.0; <a name="l00127"></a>00127 <span class="keywordtype">double</span> g2 = ((double)qGreen(*blurredRgb) )/255.0; <a name="l00128"></a>00128 <span class="keywordtype">double</span> <a class="code" href="blur_8cpp.html#746bcb222044619e5a9b335d242a2a92">b2</a> = ((double)qBlue(*blurredRgb) )/255.0; <a name="l00129"></a>00129 <a name="l00130"></a>00130 <span class="comment">//sharpen the entire thing!</span> <a name="l00131"></a>00131 <span class="keywordtype">float</span> alpha; <a name="l00132"></a>00132 <span class="keywordflow">if</span>( edgeImage == NULL) <a name="l00133"></a>00133 alpha = 1.0f; <a name="l00134"></a>00134 <span class="keywordflow">else</span> <a name="l00135"></a>00135 { <a name="l00136"></a>00136 <span class="keywordtype">int</span> edgeX = ((edgeImage->width()-1) * (x+offset.x())) / (fullImageRes.width()-1); <a name="l00137"></a>00137 edgeRgb = ((QRgb*)edgesScanline+edgeX); <a name="l00138"></a>00138 <a name="l00139"></a>00139 alpha = ((float) qRed( *edgeRgb )) / 255.0f; <a name="l00140"></a>00140 <a name="l00141"></a>00141 <span class="comment">//blur regions, not edges</span> <a name="l00142"></a>00142 <span class="keywordflow">if</span>(!blurEdges) <a name="l00143"></a>00143 alpha = 1.0f - alpha; <a name="l00144"></a>00144 } <a name="l00145"></a>00145 <a name="l00146"></a>00146 <span class="comment">//convert to hsv</span> <a name="l00147"></a>00147 <span class="keywordtype">double</span> h1,s1,v1; <a name="l00148"></a>00148 <a class="code" href="imageTools_8cpp.html#7769c25727d6caedf20321d238b3828d" title="Convert a RGB color triplet to HSV.">RGBtoHSV</a>(r1,g1,b1,&h1,&s1,&v1); <a name="l00149"></a>00149 <a name="l00150"></a>00150 <span class="keywordtype">double</span> h2,s2,v2; <a name="l00151"></a>00151 <a class="code" href="imageTools_8cpp.html#7769c25727d6caedf20321d238b3828d" title="Convert a RGB color triplet to HSV.">RGBtoHSV</a>(r2,g2,b2,&h2,&s2,&v2); <a name="l00152"></a>00152 <a name="l00153"></a>00153 <span class="comment">//reset v</span> <a name="l00154"></a>00154 v1 = (alpha * QMIN( QMAX(2*v1 - v2, 0), 1.0 )) + (1-alpha)*v1; <a name="l00155"></a>00155 <a name="l00156"></a>00156 <span class="comment">//convert adjusted color back to rgb colorspace and clamp</span> <a name="l00157"></a>00157 <a class="code" href="imageTools_8cpp.html#a4ea508b635b1a7648bb5834132d4681" title="Convert a HSV color triplet to RGB.">HSVtoRGB</a>( &r1,&g1,&b1, h1,s1,v1); <a name="l00158"></a>00158 <span class="keywordtype">int</span> rp = (int) QMIN( QMAX((r1*255), 0), 255 ); <a name="l00159"></a>00159 <span class="keywordtype">int</span> gp = (int) QMIN( QMAX((g1*255), 0), 255 ); <a name="l00160"></a>00160 <span class="keywordtype">int</span> bp = (int) QMIN( QMAX((b1*255), 0), 255 ); <a name="l00161"></a>00161 <a name="l00162"></a>00162 <span class="comment">//set adjusted color value</span> <a name="l00163"></a>00163 *origRgb = qRgb(rp,gp,bp); <a name="l00164"></a>00164 } <span class="comment">//x</span> <a name="l00165"></a>00165 } <span class="comment">//y</span> <a name="l00166"></a>00166 <a name="l00167"></a>00167 } <a name="l00168"></a>00168 <span class="comment">//==============================================</span> </pre></div></div> <hr size="1"><address style="text-align: right;"><small>Generated on Sun Aug 23 02:34:27 2009 for AlbumShaper by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address> </body> </html>