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
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 :
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
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
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
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: