| 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\PaletteImage; |
| 27: | |
| 28: | /** |
| 29: | * CopyChannelsPalette operation class |
| 30: | * |
| 31: | * This operation is intended to be used on palette images |
| 32: | * |
| 33: | * @package Internal/Operations |
| 34: | */ |
| 35: | class CopyChannelsPalette |
| 36: | { |
| 37: | /** |
| 38: | * Returns an image with only specified channels copied |
| 39: | * |
| 40: | * @param \WideImage\PaletteImage $img |
| 41: | * @param array $channels |
| 42: | * @return \WideImage\PaletteImage |
| 43: | */ |
| 44: | public function execute($img, $channels) |
| 45: | { |
| 46: | $blank = array('red' => 0, 'green' => 0, 'blue' => 0); |
| 47: | |
| 48: | if (isset($channels['alpha'])) { |
| 49: | unset($channels['alpha']); |
| 50: | } |
| 51: | |
| 52: | $width = $img->getWidth(); |
| 53: | $height = $img->getHeight(); |
| 54: | $copy = PaletteImage::create($width, $height); |
| 55: | |
| 56: | if ($img->isTransparent()) { |
| 57: | $otci = $img->getTransparentColor(); |
| 58: | $TRGB = $img->getColorRGB($otci); |
| 59: | $tci = $copy->allocateColor($TRGB); |
| 60: | } else { |
| 61: | $otci = null; |
| 62: | $tci = null; |
| 63: | } |
| 64: | |
| 65: | for ($x = 0; $x < $width; $x++) { |
| 66: | for ($y = 0; $y < $height; $y++) { |
| 67: | $ci = $img->getColorAt($x, $y); |
| 68: | |
| 69: | if ($ci === $otci) { |
| 70: | $copy->setColorAt($x, $y, $tci); |
| 71: | continue; |
| 72: | } |
| 73: | |
| 74: | $RGB = $img->getColorRGB($ci); |
| 75: | |
| 76: | $newRGB = $blank; |
| 77: | |
| 78: | foreach ($channels as $channel) { |
| 79: | $newRGB[$channel] = $RGB[$channel]; |
| 80: | } |
| 81: | |
| 82: | $color = $copy->getExactColor($newRGB); |
| 83: | |
| 84: | if ($color == -1) { |
| 85: | $color = $copy->allocateColor($newRGB); |
| 86: | } |
| 87: | |
| 88: | $copy->setColorAt($x, $y, $color); |
| 89: | } |
| 90: | } |
| 91: | |
| 92: | if ($img->isTransparent()) { |
| 93: | $copy->setTransparentColor($tci); |
| 94: | } |
| 95: | |
| 96: | return $copy; |
| 97: | } |
| 98: | } |
| 99: |