|
2 | 2 | <?php |
3 | 3 |
|
4 | 4 | require __DIR__ . '/../vendor/autoload.php'; |
5 | | -use Jcupitt\Vips; |
6 | | - |
7 | | -# sigmoidal contrast adjustment in php-vips |
8 | 5 |
|
9 | | -# This is a standard contrast adjustment technique: grey values are put through |
10 | | -# an S-shaped curve which boosts the slope in the mid-tones and drops it for |
11 | | -# white and black. |
| 6 | +use Jcupitt\Vips; |
12 | 7 |
|
13 | | -function sigmoid(float $alpha, float $beta, bool $ushort = false): Vips\Image |
| 8 | +/** |
| 9 | + * sigmoidal contrast adjustment in php-vips |
| 10 | + * |
| 11 | + * This is a standard contrast adjustment technique: grey values are put through |
| 12 | + * an S-shaped curve which boosts the slope in the mid-tones and drops it for |
| 13 | + * white and black. |
| 14 | + * |
| 15 | + * @param bool $sharpen If true increase the contrast, if false decrease the contrast. |
| 16 | + * @param float $midpoint Midpoint of the contrast (typically 0.5). |
| 17 | + * @param float $contrast Strength of the contrast (typically 3-20). |
| 18 | + * @param bool $ushort Indicating if we have a 16-bit LUT. |
| 19 | + * |
| 20 | + * @return Vips\Image 16-bit or 8-bit LUT |
| 21 | + */ |
| 22 | +function sigmoid(bool $sharpen, float $midpoint, float $contrast, bool $ushort = false): Vips\Image |
14 | 23 | { |
15 | | - # make a identity LUT, that is, a lut where each pixel has the value of |
16 | | - # its index ... if you map an image through the identity, you get the |
17 | | - # same image back again |
18 | | - # |
19 | | - # LUTs in libvips are just images with either the width or height set |
20 | | - # to 1, and the 'interpretation' tag set to HISTOGRAM |
21 | | - # |
22 | | - # if 'ushort' is TRUE, we make a 16-bit LUT, ie. 0 - 65535 values; |
23 | | - # otherwise it's 8-bit (0 - 255) |
| 24 | + /** |
| 25 | + * Make a identity LUT, that is, a lut where each pixel has the value of |
| 26 | + * its index ... if you map an image through the identity, you get the |
| 27 | + * same image back again. |
| 28 | + * |
| 29 | + * LUTs in libvips are just images with either the width or height set |
| 30 | + * to 1, and the 'interpretation' tag set to HISTOGRAM. |
| 31 | + * |
| 32 | + * If 'ushort' is TRUE, we make a 16-bit LUT, ie. 0 - 65535 values; |
| 33 | + * otherwise it's 8-bit (0 - 255) |
| 34 | + */ |
24 | 35 | $lut = Vips\Image::identity(['ushort' => $ushort]); |
25 | 36 |
|
26 | | - # rescale so each element is in [0, 1] |
27 | | - $max = $lut->max(); |
28 | | - $lut = $lut->divide($max); |
29 | | - |
30 | | - # the sigmoidal equation, see |
31 | | - # |
32 | | - # http://www.imagemagick.org/Usage/color_mods/#sigmoidal |
33 | | - # |
34 | | - # though that's missing a term -- it should be |
35 | | - # |
36 | | - # (1/(1+exp(β*(α-u))) - 1/(1+exp(β*α))) / |
37 | | - # (1/(1+exp(β*(α-1))) - 1/(1+exp(β*α))) |
38 | | - # |
39 | | - # ie. there should be an extra α in the second term |
40 | | - $x = 1.0 / (1.0 + exp($beta * $alpha)); |
41 | | - $y = 1.0 / (1.0 + exp($beta * ($alpha - 1.0))) - $x; |
42 | | - $z = $lut->multiply(-1)->add($alpha)->multiply($beta)->exp()->add(1); |
43 | | - $result = $z->pow(-1)->subtract($x)->divide($y); |
44 | | - |
45 | | - # rescale back to 0 - 255 or 0 - 65535 |
46 | | - $result = $result->multiply($max); |
47 | | - |
48 | | - # and get the format right ... $result will be a float image after all |
49 | | - # that maths, but we want uchar or ushort |
50 | | - $result = $result->cast($ushort ? |
51 | | - Vips\BandFormat::USHORT : Vips\BandFormat::UCHAR); |
52 | | - |
| 37 | + // Rescale so each element is in [0, 1] |
| 38 | + $range = $lut->max(); |
| 39 | + $lut = $lut->divide($range); |
| 40 | + |
| 41 | + /** |
| 42 | + * The sigmoidal equation, see |
| 43 | + * |
| 44 | + * http://www.imagemagick.org/Usage/color_mods/#sigmoidal |
| 45 | + * |
| 46 | + * and |
| 47 | + * |
| 48 | + * http://osdir.com/ml/video.image-magick.devel/2005-04/msg00006.html |
| 49 | + * |
| 50 | + * Though that's missing a term -- it should be |
| 51 | + * |
| 52 | + * (1/(1+exp(β*(α-u))) - 1/(1+exp(β*α))) / |
| 53 | + * (1/(1+exp(β*(α-1))) - 1/(1+exp(β*α))) |
| 54 | + * |
| 55 | + * ie. there should be an extra α in the second term |
| 56 | + */ |
| 57 | + if ($sharpen) { |
| 58 | + $x = $lut->multiply(-1)->add($midpoint)->multiply($contrast)->exp()->add(1)->pow(-1); |
| 59 | + $min = $x->min(); |
| 60 | + $max = $x->max(); |
| 61 | + $result = $x->subtract($min)->divide($max - $min); |
| 62 | + } else { |
| 63 | + $min = 1 / (1 + exp($contrast * $midpoint)); |
| 64 | + $max = 1 / (1 + exp($contrast * ($midpoint - 1))); |
| 65 | + $x = $lut->multiply($max - $min)->add($min); |
| 66 | + $result = $x->multiply(-1)->add(1)->divide($x)->log()->divide($contrast)->multiply(-1)->add($midpoint); |
| 67 | + } |
| 68 | + |
| 69 | + // Rescale back to 0 - 255 or 0 - 65535 |
| 70 | + $result = $result->multiply($range); |
| 71 | + |
| 72 | + /** |
| 73 | + * And get the format right ... $result will be a float image after all |
| 74 | + * that maths, but we want uchar or ushort |
| 75 | + */ |
| 76 | + $result = $result->cast($ushort ? Vips\BandFormat::USHORT : Vips\BandFormat::UCHAR); |
53 | 77 | return $result; |
54 | 78 | } |
55 | 79 |
|
56 | | -# Apply to RGB. This takes no account of image gamma, and applies the |
57 | | -# contrast boost to R, G and B bands, thereby also boosting colourfulness. |
58 | | -function sigRGB(Vips\Image $image, float $alpha, float $beta): Vips\Image |
| 80 | +/** |
| 81 | + * Apply to RGB. This takes no account of image gamma, and applies the |
| 82 | + * contrast boost to R, G and B bands, thereby also boosting colourfulness. |
| 83 | + * |
| 84 | + * @param Vips\Image $image The source image. |
| 85 | + * @param bool $sharpen If true increase the contrast, if false decrease the contrast. |
| 86 | + * @param float $midpoint Midpoint of the contrast (typically 0.5). |
| 87 | + * @param float $contrast Strength of the contrast (typically 3-20). |
| 88 | + * |
| 89 | + * @return Vips\Image The manipulated image. |
| 90 | + */ |
| 91 | +function sigRGB(Vips\Image $image, bool $sharpen, float $midpoint, float $contrast): Vips\Image |
59 | 92 | { |
60 | | - $lut = sigmoid($alpha, $beta, $image->format == Vips\BandFormat::USHORT); |
61 | | - |
| 93 | + $lut = sigmoid($sharpen, $midpoint, $contrast, $image->format === Vips\BandFormat::USHORT); |
62 | 94 | return $image->maplut($lut); |
63 | 95 | } |
64 | 96 |
|
65 | | -# Fancier: apply to L of CIELAB. This will change luminance equally, and will |
66 | | -# not change colourfulness. |
67 | | -function sigLAB(Vips\Image $image, float $alpha, float $beta): Vips\Image |
| 97 | +/** |
| 98 | + * Fancier: apply to L of CIELAB. This will change luminance equally, and will |
| 99 | + * not change colourfulness. |
| 100 | + * |
| 101 | + * @param Vips\Image $image The source image. |
| 102 | + * @param bool $sharpen If true increase the contrast, if false decrease the contrast. |
| 103 | + * @param float $midpoint Midpoint of the contrast (typically 0.5). |
| 104 | + * @param float $contrast Strength of the contrast (typically 3-20). |
| 105 | + * |
| 106 | + * @return Vips\Image The manipulated image. |
| 107 | + */ |
| 108 | +function sigLAB(Vips\Image $image, bool $sharpen, float $midpoint, float $contrast): Vips\Image |
68 | 109 | { |
69 | | - $old_interpretation = $image->interpretation; |
| 110 | + $oldInterpretation = $image->interpretation; |
70 | 111 |
|
71 | | - # labs is CIELAB with colour values expressed as short (signed 16-bit ints) |
72 | | - # L is in 0 - 32767 |
| 112 | + /** |
| 113 | + * Labs is CIELAB with colour values expressed as short (signed 16-bit ints) |
| 114 | + * L is in 0 - 32767 |
| 115 | + */ |
73 | 116 | $image = $image->colourspace(Vips\Interpretation::LABS); |
74 | 117 |
|
75 | | - # make a 16-bit LUT, then shrink by x2 to make it fit the range of L in labs |
76 | | - $lut = sigmoid($alpha, $beta, true); |
| 118 | + // Make a 16-bit LUT, then shrink by x2 to make it fit the range of L in labs |
| 119 | + $lut = sigmoid($sharpen, $midpoint, $contrast, true); |
77 | 120 | $lut = $lut->shrinkh(2)->multiply(0.5); |
78 | 121 | $lut = $lut->cast(Vips\BandFormat::SHORT); |
79 | 122 |
|
80 | | - # get the L band from our labs image, map though the LUT, then reattach the |
81 | | - # ab bands from the labs image |
| 123 | + /** |
| 124 | + * Get the L band from our labs image, map though the LUT, then reattach the |
| 125 | + * ab bands from the labs image |
| 126 | + */ |
82 | 127 | $L = $image->extract_band(0); |
83 | 128 | $AB = $image->extract_band(1, ['n' => 2]); |
84 | 129 | $L = $L->maplut($lut); |
85 | 130 | $image = $L->bandjoin($AB); |
86 | 131 |
|
87 | | - # and back to our original colourspace again (probably rgb) |
88 | | - # |
89 | | - # after the manipulation above, $image will just be tagged as a generic |
90 | | - # multiband image, vips will no longer know that it's a labs, so we need to |
91 | | - # tell colourspace what the source space is |
92 | | - $image = $image->colourspace( |
93 | | - $old_interpretation, |
94 | | - ['source_space' => Vips\Interpretation::LABS] |
95 | | - ); |
96 | | - |
97 | | - return $image; |
| 132 | + /** |
| 133 | + * And back to our original colourspace again (probably rgb) |
| 134 | + * |
| 135 | + * After the manipulation above, $image will just be tagged as a generic |
| 136 | + * multiband image, vips will no longer know that it's a labs, so we need to |
| 137 | + * tell colourspace what the source space is |
| 138 | + */ |
| 139 | + return $image->colourspace($oldInterpretation, [ |
| 140 | + 'source_space' => Vips\Interpretation::LABS |
| 141 | + ]); |
98 | 142 | } |
99 | 143 |
|
100 | 144 | $im = Vips\Image::newFromFile($argv[1], ['access' => Vips\Access::SEQUENTIAL]); |
101 | 145 |
|
102 | | -# $beta == 10 is a large contrast boost, values below about 4 drop the contrast |
103 | | -# |
104 | | -# sigLAB is the fancy one, and is much slower than sigRGB |
105 | | -$im = sigLAB($im, 0.5, 7); |
| 146 | +/** |
| 147 | + * $contrast == 10 is a large contrast boost, values below about 4 drop the contrast |
| 148 | + * |
| 149 | + * sigLAB is the fancy one, and is much slower than sigRGB |
| 150 | + */ |
| 151 | +$im = sigLAB($im, true, 0.5, 7); |
106 | 152 |
|
107 | 153 | $im->writeToFile($argv[2]); |
108 | 154 |
|
|
0 commit comments