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 * CopyChannelsTrueColor operation class
27 *
28 * Used to perform CopyChannels operation on truecolor images
29 *
30 * @package Internal/Operations
31 */
32 class WideImage_Operation_CopyChannelsTrueColor
33 {
34 /**
35 * Returns an image with only specified channels copied
36 *
37 * @param WideImage_Image $img
38 * @param array $channels
39 * @return WideImage_Image
40 */
41 function execute($img, $channels)
42 {
43 $blank = array('red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 0);
44
45 $width = $img->getWidth();
46 $height = $img->getHeight();
47 $copy = WideImage_TrueColorImage::create($width, $height);
48
49 if (count($channels) > 0)
50 for ($x = 0; $x < $width; $x++)
51 for ($y = 0; $y < $height; $y++)
52 {
53 $RGBA = $img->getRGBAt($x, $y);
54 $newRGBA = $blank;
55 foreach ($channels as $channel)
56 $newRGBA[$channel] = $RGBA[$channel];
57
58 $color = $copy->getExactColorAlpha($newRGBA);
59 if ($color == -1)
60 $color = $copy->allocateColorAlpha($newRGBA);
61
62 $copy->setColorAt($x, $y, $color);
63 }
64
65 return $copy;
66 }
67 }
68