Passed
Push — master ( b5dddf...91d417 )
by Richard
09:12
created

Unsharp::execute()   C

Complexity

Conditions 16
Paths 160

Size

Total Lines 94
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 94
rs 5.0666
c 0
b 0
f 0
cc 16
nc 160
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
	/**
3
##DOC-SIGNATURE##
4
5
    This file is part of WideImage.
6
		
7
    WideImage is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU Lesser General Public License as published by
9
    the Free Software Foundation; either version 2.1 of the License, or
10
    (at your option) any later version.
11
		
12
    WideImage is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU Lesser General Public License for more details.
16
		
17
    You should have received a copy of the GNU Lesser General Public License
18
    along with WideImage; if not, write to the Free Software
19
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21
    * @package Internal/Operations
22
  **/
23
24
namespace WideImage\Operation;
25
26
/**
27
 * Unsharp filter
28
 * 
29
 * This filter was taken from http://vikjavev.no/computing/ump.php,
30
 * the original author Torstein Hønsi. Adapted to fit better within
31
 * the Wideimage package.
32
 * 
33
 * @package Internal/Operations
34
 */
35
class Unsharp
36
{
37
	/**
38
	 * Returns sharpened image
39
	 *
40
	 * @param \WideImage\Image $image
41
	 * @param float $amount
42
	 * @param int $radius
43
	 * @param float $threshold
44
	 * @return \WideImage\Image
45
	 */
46
	public function execute($image, $amount, $radius, $threshold)
47
	{
48
		// Attempt to calibrate the parameters to Photoshop:
49
		if ($amount > 500) {
50
			$amount = 500;
51
		}
52
		
53
		$amount = $amount * 0.016;
54
		
55
		if ($radius > 50) {
56
			$radius = 50;
57
		}
58
		
59
		$radius = $radius * 2;
60
		
61
		if ($threshold > 255) {
62
			$threshold = 255;
63
		}
64
		
65
		$radius = abs(round($radius)); // Only integers make sense.
66
		
67
		if ($radius == 0) {
68
			return $image;
69
		}
70
		
71
		// Gaussian blur matrix
72
		$matrix = array(
73
			array(1, 2, 1),
74
			array(2, 4, 2),
75
			array(1, 2, 1)
76
		);
77
		
78
		$blurred = $image->applyConvolution($matrix, 16, 0);
79
		
80
		if ($threshold > 0) {
81
			// Calculate the difference between the blurred pixels and the original
82
			// and set the pixels
83
			for ($x = 0; $x < $image->getWidth(); $x++) {
84
				for ($y = 0; $y < $image->getHeight(); $y++) {
85
					$rgbOrig = $image->getRGBAt($x, $y);
86
					$rOrig   = $rgbOrig["red"];
87
					$gOrig   = $rgbOrig["green"];
88
					$bOrig   = $rgbOrig["blue"];
89
					 
90
					$rgbBlur = $blurred->getRGBAt($x, $y);
91
					$rBlur   = $rgbBlur["red"];
92
					$gBlur   = $rgbBlur["green"];
93
					$bBlur   = $rgbBlur["blue"];
94
					
95
					// When the masked pixels differ less from the original
96
					// than the threshold specifies, they are set to their original value.
97
					$rNew = (abs($rOrig - $rBlur) >= $threshold)  
98
						? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig))  
99
						: $rOrig;
100
					$gNew = (abs($gOrig - $gBlur) >= $threshold)  
101
						? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig))  
102
						: $gOrig;
103
					$bNew = (abs($bOrig - $bBlur) >= $threshold)  
104
						? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig))  
105
						: $bOrig;
106
					$rgbNew = array("red" => $rNew, "green" => $gNew, "blue" => $bNew, "alpha" => 0);
107
					
108
					if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {
109
						$image->setRGBAt($x, $y, $rgbNew);
110
					}
111
				}
112
			}
113
		} else {
114
			$w = $image->getWidth();
115
			$h = $image->getHeight();
116
			
117
			for ($x = 0; $x < $w; $x++) {
118
				for ($y = 0; $y < $h; $y++) {
119
					$rgbOrig = $image->getRGBAt($x, $y);
120
					$rOrig   = $rgbOrig["red"];
121
					$gOrig   = $rgbOrig["green"];
122
					$bOrig   = $rgbOrig["blue"];
123
					
124
					$rgbBlur = $blurred->getRGBAt($x, $y);
125
					$rBlur   = $rgbBlur["red"];
126
					$gBlur   = $rgbBlur["green"];
127
					$bBlur   = $rgbBlur["blue"];
128
					
129
					$rNew   = static::bit(($amount * ($rOrig - $rBlur)) + $rOrig);
130
					$gNew   = static::bit(($amount * ($gOrig - $gBlur)) + $gOrig);
131
					$bNew   = static::bit(($amount * ($bOrig - $bBlur)) + $bOrig);
132
					$rgbNew = array("red" => $rNew, "green" => $gNew, "blue" => $bNew, "alpha" => 0);
133
					
134
					$image->setRGBAt($x, $y, $rgbNew);
135
				}
136
			}
137
		}
138
		
139
		return $image;
140
	}
141
	
142
	protected static function bit($val)
143
	{
144
		if ($val > 255) {
145
			return 255;
146
		}
147
		
148
		if ($val < 0) {
149
			return 0;
150
		}
151
		
152
		return $val;
153
	}
154
}
155