1: <?php
2: /**
3: ##DOC-SIGNATURE##
4:
5: This file is part of WideImage.
6:
7: WideImage is free software; you can redistribute it and/or modify
8: it under the terms of the GNU Lesser General Public License as published by
9: the Free Software Foundation; either version 2.1 of the License, or
10: (at your option) any later version.
11:
12: WideImage is distributed in the hope that it will be useful,
13: but WITHOUT ANY WARRANTY; without even the implied warranty of
14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: GNU Lesser General Public License for more details.
16:
17: You should have received a copy of the GNU Lesser General Public License
18: along with WideImage; if not, write to the Free Software
19: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20:
21: * @package Internal/Operations
22: **/
23:
24: namespace WideImage\Operation;
25:
26: use WideImage\TrueColorImage;
27:
28: /**
29: * CopyChannelsTrueColor operation class
30: *
31: * Used to perform CopyChannels operation on truecolor images
32: *
33: * @package Internal/Operations
34: */
35: class CopyChannelsTrueColor {
36: /**
37: * Returns an image with only specified channels copied
38: *
39: * @param \WideImage\Image $img
40: * @param array $channels
41: * @return \WideImage\Image
42: */
43: public function execute($img, $channels)
44: {
45: $blank = array('red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 0);
46:
47: $width = $img->getWidth();
48: $height = $img->getHeight();
49: $copy = TrueColorImage::create($width, $height);
50:
51: if (count($channels) > 0) {
52: for ($x = 0; $x < $width; $x++) {
53: for ($y = 0; $y < $height; $y++) {
54: $RGBA = $img->getRGBAt($x, $y);
55: $newRGBA = $blank;
56:
57: foreach ($channels as $channel) {
58: $newRGBA[$channel] = $RGBA[$channel];
59: }
60:
61: $color = $copy->getExactColorAlpha($newRGBA);
62:
63: if ($color == -1) {
64: $color = $copy->allocateColorAlpha($newRGBA);
65: }
66:
67: $copy->setColorAt($x, $y, $color);
68: }
69: }
70: }
71:
72: return $copy;
73: }
74: }
75: