1 <?php
2 /**
3 * @author Gasper Kozak
4 * @copyright 2007-2011
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 * ApplyMask operation class
27 *
28 * @package Internal/Operations
29 */
30 class WideImage_Operation_ApplyMask
31 {
32 /**
33 * Applies a mask on the copy of source image
34 *
35 * @param WideImage_Image $image
36 * @param WideImage_Image $mask
37 * @param smart_coordinate $left
38 * @param smart_coordinate $top
39 * @return WideImage_Image
40 */
41 function execute($image, $mask, $left = 0, $top = 0)
42 {
43 $left = WideImage_Coordinate::fix($left, $image->getWidth(), $mask->getWidth());
44 $top = WideImage_Coordinate::fix($top, $image->getHeight(), $mask->getHeight());
45
46 $width = $image->getWidth();
47 $mask_width = $mask->getWidth();
48
49 $height = $image->getHeight();
50 $mask_height = $mask->getHeight();
51
52 $result = $image->asTrueColor();
53
54 $result->alphaBlending(false);
55 $result->saveAlpha(true);
56
57 $srcTransparentColor = $result->getTransparentColor();
58 if ($srcTransparentColor >= 0)
59 {
60 # this was here. works without.
61 #$trgb = $image->getColorRGB($srcTransparentColor);
62 #$trgb['alpha'] = 127;
63 #$destTransparentColor = $result->allocateColorAlpha($trgb);
64 #$result->setTransparentColor($destTransparentColor);
65 $destTransparentColor = $srcTransparentColor;
66 }
67 else
68 {
69 $destTransparentColor = $result->allocateColorAlpha(255, 255, 255, 127);
70 }
71
72 for ($x = 0; $x < $width; $x++)
73 for ($y = 0; $y < $height; $y++)
74 {
75 $mx = $x - $left;
76 $my = $y - $top;
77 if ($mx >= 0 && $mx < $mask_width && $my >= 0 && $my < $mask_height)
78 {
79 $srcColor = $image->getColorAt($x, $y);
80 if ($srcColor == $srcTransparentColor)
81 $destColor = $destTransparentColor;
82 else
83 {
84 $maskRGB = $mask->getRGBAt($mx, $my);
85 if ($maskRGB['red'] == 0)
86 $destColor = $destTransparentColor;
87 elseif ($srcColor >= 0)
88 {
89 $imageRGB = $image->getRGBAt($x, $y);
90 $level = ($maskRGB['red'] / 255) * (1 - $imageRGB['alpha'] / 127);
91 $imageRGB['alpha'] = 127 - round($level * 127);
92 if ($imageRGB['alpha'] == 127)
93 $destColor = $destTransparentColor;
94 else
95 $destColor = $result->allocateColorAlpha($imageRGB);
96 }
97 else
98 $destColor = $destTransparentColor;
99 }
100 $result->setColorAt($x, $y, $destColor);
101 }
102 }
103 return $result;
104 }
105 }
106