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 /**
26 * Noise filter
27 *
28 * @package Internal/Operations
29 */
30 class WideImage_Operation_AddNoise {
31 /**
32 * Returns image with noise added
33 *
34 * @param WideImage_Image $image
35 * @param float $amount
36 * @param const $type
37 * @param float $threshold
38 * @return WideImage_Image
39 */
40
41 function execute($image, $amount, $type) {
42
43 switch ($type)
44 {
45 case 'salt&pepper' : $fun = 'saltPepperNoise_fun';
46 break;
47 case 'color' : $fun = 'colorNoise_fun';
48 break;
49 default : $fun = 'monoNoise_fun';
50 break;
51 }
52
53 return self::filter($image->asTrueColor(), $fun, $amount);
54 }
55
56 /**
57 * Returns image with every pixel changed by specififed function
58 *
59 * @param WideImage_Image $image
60 * @param str $function
61 * @param int $value
62 * @return WideImage_Image
63 */
64 function filter($image, $function, $value)
65 {
66 for ($y = 0; $y < $image->getHeight(); $y++)
67 {
68 for ($x = 0; $x< $image->getWidth(); $x++)
69 {
70 $rgb = imagecolorat($image->getHandle(), $x, $y);
71
72 $a = ($rgb >> 24) & 0xFF;
73 $r = ($rgb >> 16) & 0xFF;
74 $g = ($rgb >> 8) & 0xFF;
75 $b = $rgb & 0xFF;
76
77
78 self::$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 return $image;
85 }
86 /**
87 * Adds color noise by altering given R,G,B values using specififed amount
88 *
89 * @param int $r
90 * @param int $g
91 * @param int $b
92 * @param int $value
93 * @return void
94 */
95 function colorNoise_fun(&$r, &$g, &$b, $amount)
96 {
97 $r = self::byte($r + mt_rand(0, $amount) - ($amount >> 1) );
98 $g = self::byte($g + mt_rand(0, $amount) - ($amount >> 1) );
99 $b = self::byte($b + mt_rand(0, $amount) - ($amount >> 1) );
100 }
101 /**
102 * Adds mono noise by altering given R,G,B values using specififed amount
103 *
104 * @param int $r
105 * @param int $g
106 * @param int $b
107 * @param int $value
108 * @return void
109 */
110 function monoNoise_fun(&$r, &$g, &$b, $amount)
111 {
112 $rand = mt_rand(0, $amount) - ($amount >> 1);
113
114 $r = self::byte($r + $rand);
115 $g = self::byte($g + $rand);
116 $b = self::byte($b + $rand);
117 }
118 /**
119 * Adds salt&pepper noise by altering given R,G,B values using specififed amount
120 *
121 * @param int $r
122 * @param int $g
123 * @param int $b
124 * @param int $value
125 * @return void
126 */
127 function saltPepperNoise_fun(&$r, &$g, &$b, $amount)
128 {
129 if (mt_rand(0, 255 - $amount) != 0) return;
130
131 $rand = mt_rand(0, 1);
132 switch ($rand)
133 {
134 case 0 : $r = $g = $b = 0;
135 break;
136 case 1 : $r = $g = $b = 255;
137 break;
138 }
139 }
140 /**
141 * Returns value within (0,255)
142 *
143 * @param int $b
144 * @return int
145 */
146 function byte($b)
147 {
148 if ($b > 255) return 255;
149 if ($b < 0) return 0;
150 return (int) $b;
151 }
152
153 }
154