Skip to content

Commit 7f38227

Browse files
committed
Fix PSR-2 errors from phpcs + more
- Update phpunit. - Update sigmoidal contrast example. - Fix regression from libvips#49. - Rename the test files and apply the correct namespace.
1 parent 9fb93b1 commit 7f38227

13 files changed

+255
-212
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"psr/log": "^1.0.1"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "^6.0",
25+
"phpunit/phpunit": "^6.3",
2626
"phpdocumentor/phpdocumentor" : "^2.9",
2727
"jakub-onderka/php-parallel-lint": "^0.9.2",
2828
"squizlabs/php_codesniffer": "3.*"

examples/class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
$image = Vips\Image::newFromFile($argv[1]);
1111

12-
echo "width = " . $image->width . "\n";
12+
echo 'width = ' . $image->width . "\n";
1313

1414
$image = $image->invert();
1515

examples/sig.php

+119-73
Original file line numberDiff line numberDiff line change
@@ -2,107 +2,153 @@
22
<?php
33

44
require __DIR__ . '/../vendor/autoload.php';
5-
use Jcupitt\Vips;
6-
7-
# sigmoidal contrast adjustment in php-vips
85

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;
127

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
1423
{
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+
*/
2435
$lut = Vips\Image::identity(['ushort' => $ushort]);
2536

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);
5377
return $result;
5478
}
5579

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
5992
{
60-
$lut = sigmoid($alpha, $beta, $image->format == Vips\BandFormat::USHORT);
61-
93+
$lut = sigmoid($sharpen, $midpoint, $contrast, $image->format === Vips\BandFormat::USHORT);
6294
return $image->maplut($lut);
6395
}
6496

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
68109
{
69-
$old_interpretation = $image->interpretation;
110+
$oldInterpretation = $image->interpretation;
70111

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+
*/
73116
$image = $image->colourspace(Vips\Interpretation::LABS);
74117

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);
77120
$lut = $lut->shrinkh(2)->multiply(0.5);
78121
$lut = $lut->cast(Vips\BandFormat::SHORT);
79122

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+
*/
82127
$L = $image->extract_band(0);
83128
$AB = $image->extract_band(1, ['n' => 2]);
84129
$L = $L->maplut($lut);
85130
$image = $L->bandjoin($AB);
86131

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+
]);
98142
}
99143

100144
$im = Vips\Image::newFromFile($argv[1], ['access' => Vips\Access::SEQUENTIAL]);
101145

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);
106152

107153
$im->writeToFile($argv[2]);
108154

src/Image.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,11 @@ public function bandjoin($other, array $options = []): Image
16631663
{
16641664
/* Allow a single unarrayed value as well.
16651665
*/
1666-
$other = (array) $other;
1666+
if ($other instanceof Image) {
1667+
$other = [$other];
1668+
} else {
1669+
$other = (array) $other;
1670+
}
16671671

16681672
/* If $other is all numbers, we can use self::bandjoin_const().
16691673
*/
@@ -1727,7 +1731,11 @@ public function bandrank($other, array $options = []): Image
17271731

17281732
/* Allow a single unarrayed value as well.
17291733
*/
1730-
$other = (array) $other;
1734+
if ($other instanceof Image) {
1735+
$other = [$other];
1736+
} else {
1737+
$other = (array) $other;
1738+
}
17311739

17321740
return self::call('bandrank', $this, $other, $options);
17331741
}

tests/call.php renamed to tests/CallTest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
namespace Jcupitt\Vips\Test;
4+
35
use Jcupitt\Vips;
6+
use PHPUnit\Framework\TestCase;
47

5-
class VipsCallTest extends PHPUnit\Framework\TestCase
8+
class CallTest extends TestCase
69
{
710
public function testVipsCall()
811
{
@@ -16,7 +19,7 @@ public function testVipsCall()
1619

1720
public function testVipsCallStatic()
1821
{
19-
$image = Vips\Image::black(1, 4, ["bands" => 3]);
22+
$image = Vips\Image::black(1, 4, ['bands' => 3]);
2023

2124
$this->assertEquals($image->width, 1);
2225
$this->assertEquals($image->height, 4);
@@ -45,7 +48,6 @@ public function testVipsDraw()
4548
$this->assertEquals($image->getpoint(0, 0), [0]);
4649
$this->assertEquals($image->getpoint(50, 50), [255]);
4750
}
48-
4951
}
5052

5153
/*

tests/Main.php renamed to tests/ConfigTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
namespace Jcupitt\Vips\Test;
4+
35
use Jcupitt\Vips;
6+
use PHPUnit\Framework\TestCase;
47

5-
class VipsConfigTest extends PHPUnit\Framework\TestCase
8+
class ConfigTest extends TestCase
69
{
710
public function testVipsCacheSetMax()
811
{
@@ -25,7 +28,6 @@ public function testVipsConcurrencySet()
2528
{
2629
Vips\Config::concurrencySet(12);
2730
}
28-
2931
}
3032

3133
/*

tests/convenience.php renamed to tests/ConvenienceTest.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
namespace Jcupitt\Vips\Test;
4+
35
use Jcupitt\Vips;
6+
use PHPUnit\Framework\TestCase;
47

5-
class VipsConvenienceTest extends PHPUnit\Framework\TestCase
8+
class ConvenienceTest extends TestCase
69
{
710
/**
811
* @var Vips\Image
@@ -16,7 +19,7 @@ class VipsConvenienceTest extends PHPUnit\Framework\TestCase
1619

1720
protected function setUp()
1821
{
19-
$filename = dirname(__FILE__) . '/images/img_0076.jpg';
22+
$filename = __DIR__ . '/images/img_0076.jpg';
2023
$this->image = Vips\Image::newFromFile($filename);
2124
$this->pixel = $this->image->getpoint(0, 0);
2225
}
@@ -35,7 +38,7 @@ public function testVipsBandsplit()
3538
{
3639
$arr = $this->image->bandsplit();
3740

38-
$this->assertEquals(count($arr), 3);
41+
$this->assertCount(3, $arr);
3942
$this->assertEquals($arr[0]->bands, 1);
4043
}
4144

@@ -54,7 +57,7 @@ public function testVipsSubtractConst()
5457
$image = $image->subtract(1);
5558
$pixel = $image->getpoint(1, 1);
5659

57-
$this->assertEquals(count($pixel), 1);
60+
$this->assertCount(1, $pixel);
5861
$this->assertEquals($pixel[0], 4);
5962
}
6063

@@ -64,7 +67,7 @@ public function testVipsMultiplyConst()
6467
$image = $image->multiply(2);
6568
$pixel = $image->getpoint(1, 1);
6669

67-
$this->assertEquals(count($pixel), 1);
70+
$this->assertCount(1, $pixel);
6871
$this->assertEquals($pixel[0], 10);
6972
}
7073

@@ -74,7 +77,7 @@ public function testVipsDivideConst()
7477
$image = $image->divide(2);
7578
$pixel = $image->getpoint(0, 1);
7679

77-
$this->assertEquals(count($pixel), 1);
80+
$this->assertCount(1, $pixel);
7881
$this->assertEquals($pixel[0], 2);
7982
}
8083

@@ -144,7 +147,6 @@ public function testVipsMedian()
144147

145148
$this->assertEquals($result, [37, 38, 33]);
146149
}
147-
148150
}
149151

150152
/*

0 commit comments

Comments
 (0)