Completed
Push — master ( d072d1...74ad36 )
by Michael
11:11
created

AddNoise::byte()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
	/**
3
	* @author Tomasz Kapusta
4
	* @copyright 2010
5
	
6
	This file is part of WideImage.
7
		
8
		WideImage is free software; you can redistribute it and/or modify
9
	it under the terms of the GNU Lesser General Public License as published by
10
	the Free Software Foundation; either version 2.1 of the License, or
11
	(at your option) any later version.
12
		
13
	WideImage is distributed in the hope that it will be useful,
14
	but WITHOUT ANY WARRANTY; without even the implied warranty of
15
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
	GNU Lesser General Public License for more details.
17
		
18
	You should have received a copy of the GNU Lesser General Public License
19
	along with WideImage; if not, write to the Free Software
20
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
	
22
	* @package Internal/Operations
23
	**/
24
25
namespace WideImage\Operation;
26
27
/**
28
 * Noise filter
29
 * 
30
 * @package Internal/Operations
31
 */
32
class AddNoise
33
{
34
	/**
35
	 * Returns image with noise added
36
	 *
37
	 * @param \WideImage\Image $image
38
	 * @param float $amount
39
	 * @param const $type
40
	 * @param float $threshold
0 ignored issues
show
Bug introduced by
There is no parameter named $threshold. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
41
	 * @return \WideImage\Image
42
	 */
43
	public function execute($image, $amount, $type) {
44
		switch ($type) {
45
			case 'salt&pepper':
46
				$fun = 'saltPepperNoise_fun'; 
47
				break;
48
			case 'color':
49
				$fun = 'colorNoise_fun';
50
				break;	
51
			default	:
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
52
				$fun = 'monoNoise_fun';
53
				break; 			
54
		}
55
		
56
		return static::filter($image->asTrueColor(), $fun, $amount);			
57
	}
58
	
59
	/**
60
	 * Returns image with every pixel changed by specififed function
61
	 *
62
	 * @param \WideImage\Image $image
63
	 * @param str $function
64
	 * @param int $value
65
	 * @return \WideImage\Image
66
	 */
67
	public function filter($image, $function, $value)
68
	{
69
		for ($y = 0; $y < $image->getHeight(); $y++) {
70
	    	for ($x = 0; $x< $image->getWidth(); $x++) {	     
71
				$rgb = imagecolorat($image->getHandle(), $x, $y);				
72
				
73
				$a = ($rgb >> 24) & 0xFF;
74
				$r = ($rgb >> 16) & 0xFF;
75
				$g = ($rgb >> 8) & 0xFF;
76
				$b = $rgb & 0xFF;
77
				
78
				static::$function($r, $g, $b, $value);
79
				
80
				$color = imagecolorallocatealpha($image->getHandle(), $r, $g, $b, $a);				
81
				imagesetpixel($image->getHandle(), $x, $y, $color); 
82
	      	}
83
	    }
84
	    
85
	    return $image;
86
	}
87
	
88
	/**
89
	 * Adds color noise by altering given R,G,B values using specififed amount
90
	 *
91
	 * @param int $r
92
	 * @param int $g
93
	 * @param int $b
94
	 * @param int $value		 
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
95
	 * @return void
96
	 */
97
	public function colorNoise_fun(&$r, &$g, &$b, $amount)
98
	{				
99
		$r = static::byte($r + mt_rand(0, $amount) - ($amount >> 1) );
100
		$g = static::byte($g + mt_rand(0, $amount) - ($amount >> 1) );
101
		$b = static::byte($b + mt_rand(0, $amount) - ($amount >> 1) );
102
	}
103
	
104
	/**
105
	 * Adds mono noise by altering given R,G,B values using specififed amount
106
	 *
107
	 * @param int $r
108
	 * @param int $g
109
	 * @param int $b
110
	 * @param int $value		 
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
111
	 * @return void
112
	 */
113
	public function monoNoise_fun(&$r, &$g, &$b, $amount)
114
	{				
115
		$rand = mt_rand(0, $amount) - ($amount >> 1);
116
		
117
		$r = static::byte($r + $rand);
118
		$g = static::byte($g + $rand);
119
		$b = static::byte($b + $rand);
120
	}
121
	
122
	/**
123
	 * Adds salt&pepper noise by altering given R,G,B values using specififed amount
124
	 *
125
	 * @param int $r
126
	 * @param int $g
127
	 * @param int $b
128
	 * @param int $value		 
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
129
	 * @return void
130
	 */
131
	public function saltPepperNoise_fun(&$r, &$g, &$b, $amount)
132
	{
133
		if (mt_rand(0, 255 - $amount) != 0) {
134
			return;
135
		}
136
		
137
		$rand = mt_rand(0, 1);
138
		
139
		switch ($rand) {
140
			case 0:
141
				$r = $g = $b = 0;
142
				break;
143
			case 1:
144
				$r = $g = $b = 255;
145
				break;
146
		}
147
	}
148
	
149
	/**
150
	 * Returns value within (0,255)
151
	 *
152
	 * @param int $b		 
153
	 * @return int
154
	 */		
155
	public function byte($b)
156
	{
157
		if ($b > 255) {
158
			return 255;
159
		}
160
		
161
		if ($b < 0) {
162
			return 0;
163
		}
164
		
165
		return (int) $b;
166
	}
167
}
168