|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * MIT License |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Kevin Masseix |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in all |
| 16 | + * copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +namespace MKCG\Image\QOI\Driver; |
| 28 | + |
| 29 | +use MKCG\Image\QOI\ImageDescriptor; |
| 30 | +use MKCG\Image\QOI\Colorspace; |
| 31 | +use MKCG\Image\QOI\Context; |
| 32 | +use MKCG\Image\QOI\Format; |
| 33 | + |
| 34 | +class Vips |
| 35 | +{ |
| 36 | + public static function loadFromFile(string $filepath): mixed |
| 37 | + { |
| 38 | + return match(($result = vips_image_new_from_file($filepath, []))) { |
| 39 | + -1 => throw new \Exception(), |
| 40 | + default => $result['out'] |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + public static function createImageDescriptor($image, string $filepath): ImageDescriptor |
| 45 | + { |
| 46 | + $width = static::get($image, 'width'); |
| 47 | + $height = static::get($image, 'height'); |
| 48 | + $bands = static::get($image, 'bands'); |
| 49 | + $interpretation = static::get($image, 'interpretation'); |
| 50 | + $channels = 3; |
| 51 | + |
| 52 | + if ($bands === 2 || $bands > 4 || ($bands === 4 && $interpretation !== "cmyk")) { |
| 53 | + $channels = 4; |
| 54 | + } |
| 55 | + |
| 56 | + return new ImageDescriptor($width, $height, $channels, Colorspace::SRGB); |
| 57 | + } |
| 58 | + |
| 59 | + public static function createIterator($image, ImageDescriptor $descriptor): iterable |
| 60 | + { |
| 61 | + $bin = vips_image_write_to_memory($image) or throw new \Exception(); |
| 62 | + $length = strlen($bin); |
| 63 | + |
| 64 | + $bands = static::get($image, 'bands'); |
| 65 | + |
| 66 | + if ($bands === 1) { |
| 67 | + for ($i = 0; $i < $length; $i++) { |
| 68 | + $byte = ord($bin[$i]); |
| 69 | + $alpha = $descriptor->channels === 4 ? $byte : 255; |
| 70 | + yield [ $byte, $byte, $byte, $alpha ]; |
| 71 | + } |
| 72 | + } else { |
| 73 | + if ($length !== $descriptor->countBytes()) { |
| 74 | + throw new \Exception(); |
| 75 | + } |
| 76 | + |
| 77 | + for ($i = 0; $i < $length; $i += $descriptor->channels) { |
| 78 | + $pixel = [ |
| 79 | + ord($bin[$i]), |
| 80 | + ord($bin[$i + 1]), |
| 81 | + ord($bin[$i + 2]), |
| 82 | + $descriptor->channels == 4 ? ord($bin[$i + 3]) : 255, |
| 83 | + ]; |
| 84 | + |
| 85 | + yield $pixel; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static function get($image, $name) |
| 91 | + { |
| 92 | + return match(($result = vips_image_get($image, $name))) { |
| 93 | + -1 => throw new \Exception(), |
| 94 | + default => $result['out'], |
| 95 | + }; |
| 96 | + } |
| 97 | +} |
0 commit comments