/[drupal]/contributions/modules/imagecache_actions/imagefilter.inc
ViewVC logotype

Contents of /contributions/modules/imagecache_actions/imagefilter.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Wed May 28 11:45:08 2008 UTC (18 months ago) by dman
Branch: MAIN
CVS Tags: DRUPAL-5--3-1, DRUPAL-5--2-2, DRUPAL-5--2-3, DRUPAL-5--2-1, DRUPAL-6--1-4, DRUPAL-6--1-5, DRUPAL-6--1-6, DRUPAL-6--1-0, DRUPAL-6--1-1, DRUPAL-6--1-2, DRUPAL-5--2-0, DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5--3, DRUPAL-5--2, DRUPAL-6--1
File MIME type: text/x-php
First commit of the imageapi extensions as per:
http://drupal.org/node/184816
1 <?php
2 // $Id: $
3
4 //include this file whenever you have to use imageconvolution…
5 //you can use in your project, but keep the comment below
6 //great for any image manipulation library
7 //Made by Chao Xu(Mgccl) 3/1/07
8 //www.webdevlogs.com
9 //V 1.0
10 if (!function_exists('imagefilter')) {
11 function imagefilter($source, $var, $arg1 = null, $arg2 = null, $arg3 = null) {
12 #define(’IMAGE_FILTER_NEGATE’, 0);
13 #define(’IMAGE_FILTER_GRAYSCALE’, 1);
14 #define(’IMAGE_FILTER_BRIGHTNESS’, 2);
15 #define(’IMAGE_FILTER_CONTRAST’, 3);
16 #define(’IMAGE_FILTER_COLORIZE’, 4);
17 #define(’IMAGE_FILTER_EDGEDETECT’, 5);
18 #define(’IMAGE_FILTER_EMBOSS’, 6);
19 #define(’IMAGE_FILTER_GAUSSIAN_BLUR’, 7);
20 #define(’IMAGE_FILTER_SELECTIVE_BLUR’, 8);
21 #define(’IMAGE_FILTER_MEAN_REMOVAL’, 9);
22 #define(’IMAGE_FILTER_SMOOTH’, 10);
23 $max_y = imagesy($source);
24 $max_x = imagesx($source);
25 switch ($var) {
26 case 0:
27 $y = 0;
28 while ($y<$max_y) {
29 $x = 0;
30 while ($x<$max_x) {
31 $rgb = imagecolorat($source, $x, $y);
32 $r = 255 - (($rgb >> 16) & 0xFF);
33 $g = 255 - (($rgb >> 8) & 0xFF);
34 $b = 255 - ($rgb & 0xFF);
35 $a = $rgb >> 24;
36 $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a);
37 if ($new_pxl == false) {
38 $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a);
39 }
40 imagesetpixel($source, $x, $y, $new_pxl);
41 ++$x;
42 }
43 ++$y;
44 }
45 return true;
46 break;
47 case 1:
48 $y = 0;
49 while ($y<$max_y) {
50 $x = 0;
51 while ($x<$max_x) {
52 $rgb = imagecolorat($source, $x, $y);
53 $a = $rgb >> 24;
54 $r = ((($rgb >> 16) & 0xFF)*0.299)+((($rgb >> 8) & 0xFF)*0.587)+(($rgb & 0xFF)*0.114);
55 $new_pxl = imagecolorallocatealpha($source, $r, $r, $r, $a);
56 if ($new_pxl == false) {
57 $new_pxl = imagecolorclosestalpha($source, $r, $r, $r, $a);
58 }
59 imagesetpixel($source, $x, $y, $new_pxl);
60 ++$x;
61 }
62 ++$y;
63 }
64 return true;
65 break;
66 case 2:
67 $y = 0;
68 while ($y<$max_y) {
69 $x = 0;
70 while ($x<$max_x) {
71 $rgb = imagecolorat($source, $x, $y);
72 $r = (($rgb >> 16) & 0xFF) + $arg1;
73 $g = (($rgb >> 8) & 0xFF) + $arg1;
74 $b = ($rgb & 0xFF) + $arg1;
75 $a = $rgb >> 24;
76 $r = ($r > 255)? 255 : (($r < 0)? 0:$r);
77 $g = ($g > 255)? 255 : (($g < 0)? 0:$g);
78 $b = ($b > 255)? 255 : (($b < 0)? 0:$b);
79 $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a);
80 if ($new_pxl == false) {
81 $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a);
82 }
83 imagesetpixel($source, $x, $y, $new_pxl);
84 ++$x;
85 }
86 ++$y;
87 }
88 return true;
89 break;
90 case 3:
91 $contrast = pow((100-$arg1)/100, 2);
92 $y = 0;
93 while ($y<$max_y) {
94 $x = 0;
95 while ($x<$max_x) {
96 $rgb = imagecolorat($source, $x, $y);
97 $a = $rgb >> 24;
98 $r = (((((($rgb >> 16) & 0xFF)/255)-0.5)*$contrast)+0.5)*255;
99 $g = (((((($rgb >> 8) & 0xFF)/255)-0.5)*$contrast)+0.5)*255;
100 $b = ((((($rgb & 0xFF)/255)-0.5)*$contrast)+0.5)*255;
101 $r = ($r > 255)? 255 : (($r < 0)? 0:$r);
102 $g = ($g > 255)? 255 : (($g < 0)? 0:$g);
103 $b = ($b > 255)? 255 : (($b < 0)? 0:$b);
104 $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a);
105 if ($new_pxl == false) {
106 $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a);
107 }
108 imagesetpixel($source, $x, $y, $new_pxl);
109 ++$x;
110 }
111 ++$y;
112 }
113 return true;
114 break;
115 case 4:
116 $x = 0;
117 while ($x<$max_x) {
118 $y = 0;
119 while ($y<$max_y) {
120 $rgb = imagecolorat($source, $x, $y);
121 $r = (($rgb >> 16) & 0xFF) + $arg1;
122 $g = (($rgb >> 8) & 0xFF) + $arg2;
123 $b = ($rgb & 0xFF) + $arg3;
124 $a = $rgb >> 24;
125 $r = ($r > 255)? 255 : (($r < 0)? 0:$r);
126 $g = ($g > 255)? 255 : (($g < 0)? 0:$g);
127 $b = ($b > 255)? 255 : (($b < 0)? 0:$b);
128 $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a);
129 if ($new_pxl == false) {
130 $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a);
131 }
132 imagesetpixel($source, $x, $y, $new_pxl);
133 ++$y;
134 }
135 ++$x;
136 }
137 return true;
138 break;
139 case 5:
140 return imageconvolution($source, array(array(-1, 0, -1), array(0, 4, 0), array(-1, 0, -1)), 1, 127);
141 break;
142 case 6:
143 return imageconvolution($source, array(array(1.5, 0, 0), array(0, 0, 0), array(0, 0, -1.5)), 1, 127);
144 break;
145 case 7:
146 return imageconvolution($source, array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)), 16, 0);
147 break;
148 case 8:
149 for ($y = 0; $y<$max_y; $y++) {
150 for ($x = 0; $x<$max_x; $x++) {
151 $flt_r_sum = $flt_g_sum = $flt_b_sum = 0;
152 $cpxl = imagecolorat($source, $x, $y);
153 for ($j=0; $j<3; $j++) {
154 for ($i=0; $i<3; $i++) {
155 if (($j == 1) && ($i == 1)) {
156 $flt_r[1][1] = $flt_g[1][1] = $flt_b[1][1] = 0.5;
157 }
158 else {
159 $pxl = imagecolorat($source, $x-(3>>1)+$i, $y-(3>>1)+$j);
160
161 $new_a = $pxl >> 24;
162 //$r = (($pxl >> 16) & 0xFF);
163 //$g = (($pxl >> 8) & 0xFF);
164 //$b = ($pxl & 0xFF);
165 $new_r = abs((($cpxl >> 16) & 0xFF) - (($pxl >> 16) & 0xFF));
166 if ($new_r != 0) {
167 $flt_r[$j][$i] = 1/$new_r;
168 }
169 else {
170 $flt_r[$j][$i] = 1;
171 }
172
173 $new_g = abs((($cpxl >> 8) & 0xFF) - (($pxl >> 8) & 0xFF));
174 if ($new_g != 0) {
175 $flt_g[$j][$i] = 1/$new_g;
176 }
177 else {
178 $flt_g[$j][$i] = 1;
179 }
180
181 $new_b = abs(($cpxl & 0xFF) - ($pxl & 0xFF));
182 if ($new_b != 0) {
183 $flt_b[$j][$i] = 1/$new_b;
184 }
185 else {
186 $flt_b[$j][$i] = 1;
187 }
188 }
189
190 $flt_r_sum += $flt_r[$j][$i];
191 $flt_g_sum += $flt_g[$j][$i];
192 $flt_b_sum += $flt_b[$j][$i];
193 }
194 }
195
196 for ($j=0; $j<3; $j++) {
197 for ($i=0; $i<3; $i++) {
198 if ($flt_r_sum != 0) {
199 $flt_r[$j][$i] /= $flt_r_sum;
200 }
201 if ($flt_g_sum != 0) {
202 $flt_g[$j][$i] /= $flt_g_sum;
203 }
204 if ($flt_b_sum != 0) {
205 $flt_b[$j][$i] /= $flt_b_sum;
206 }
207 }
208 }
209
210 $new_r = $new_g = $new_b = 0;
211
212 for ($j=0; $j<3; $j++) {
213 for ($i=0; $i<3; $i++) {
214 $pxl = imagecolorat($source, $x-(3>>1)+$i, $y-(3>>1)+$j);
215 $new_r += (($pxl >> 16) & 0xFF) * $flt_r[$j][$i];
216 $new_g += (($pxl >> 8) & 0xFF) * $flt_g[$j][$i];
217 $new_b += ($pxl & 0xFF) * $flt_b[$j][$i];
218 }
219 }
220
221 $new_r = ($new_r > 255)? 255 : (($new_r < 0)? 0:$new_r);
222 $new_g = ($new_g > 255)? 255 : (($new_g < 0)? 0:$new_g);
223 $new_b = ($new_b > 255)? 255 : (($new_b < 0)? 0:$new_b);
224 $new_pxl = imagecolorallocatealpha($source, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
225 if ($new_pxl == false) {
226 $new_pxl = imagecolorclosestalpha($source, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
227 }
228 imagesetpixel($source, $x, $y, $new_pxl);
229 }
230 }
231 return true;
232 break;
233 case 9:
234 return imageconvolution($source, array(array(-1, -1, -1), array(-1, 9, -1), array(-1, -1, -1)), 1, 0);
235 break;
236 case 10:
237 return imageconvolution($source, array(array(1, 1, 1), array(1, $arg1, 1), array(1, 1, 1)), $arg1+8, 0);
238 break;
239 }
240 }
241 }

  ViewVC Help
Powered by ViewVC 1.1.2