1--TEST--
2ImagickKernel::fromMatrix test
3--SKIPIF--
4<?php
5
6$imageMagickRequiredVersion = 0x680;
7require_once(dirname(__FILE__) . '/skipif.inc');
8
9?>
10--FILE--
11<?php
12
13
14$kernel = array(
15	array(1, 0, -1),
16	array(1, 0, -1),
17	array(1, 0, -1),
18);
19
20$kernel = ImagickKernel::fromMatrix($kernel);
21$kernel->scale(1, \Imagick::NORMALIZE_KERNEL_VALUE);
22$kernel->addUnityKernel(0.50);
23$imagick = new \Imagick();
24$imagick->newPseudoImage(640, 480, "magick:logo");
25$imagick->morphology(
26	\Imagick::MORPHOLOGY_CONVOLVE,
27	1,
28	$kernel
29);
30
31
32$tests = array();
33
34$tests[] = array(
35	"Cannot create kernel, matrix is empty.",
36	array()
37);
38
39$tests[] = array(
40	"Values must be matrix, with the same number of columns in each row.",
41	array(0, 1, 1)
42);
43
44//Should fail, matrix needs to have same number columns in each row
45$tests[] = array(
46	"Values must be matrix, with the same number of columns in each row.",
47	array(
48		array(1, 0, 1),
49		array(1, 0, 1),
50		array(1, 0),
51	)
52);
53
54//Should fail, value instead of second row
55$tests[] = array(
56	"Values must be matrix, with the same number of columns in each row.",
57	array(
58		array(0, 1),
59		1
60	)
61);
62
63//Should fail, value instead of second row
64$tests[] = array(
65	"Only numbers or false are valid values in a kernel matrix.",
66	array(
67		array(0, 1),
68		array(0, new StdClass),
69	)
70);
71
72$tests[] = array(
73	"For kernels with even numbered rows or columns, the origin position must be specified.",
74	array(
75		array(1, 0),
76	),
77);
78
79
80foreach ($tests as $test) {
81
82	list($expectedMessage, $testValue) = $test;
83
84	try {
85		$kernel = ImagickKernel::fromMatrix($testValue);
86		echo "Failed to throw exception".PHP_EOL;
87	}
88	catch(ImagickKernelException $e) {
89		if ($e->getMessage() != $expectedMessage) {
90			echo "Unexpected message ".$e->getMessage()." for test:".PHP_EOL;
91			var_dump($test);
92		}
93	}
94}
95
96
97try {
98	$kernel = ImagickKernel::fromBuiltin(\Imagick::KERNEL_DIAMOND, "CestNestPasUneKernel");
99	//echo "builtIn OK".PHP_EOL;
100}
101catch(Exception $e) {
102	echo "Unexpected exception: ".$e->getMessage().PHP_EOL;
103}
104
105
106//Test adding unity kernel works.
107$matrix1 = array(
108	array(1, 1, 1),
109	array(0, 0, 0),
110	array(-1, -1, -1),
111);
112$kernel = ImagickKernel::fromMatrix($matrix1);
113$kernel->addUnityKernel(0.5);
114$matrix = $kernel->getMatrix();
115
116if ($matrix[1][1] != 0.5) {
117	echo "center point should be 0.5 but is actually ".$matrix[1][1].PHP_EOL;
118	var_dump($matrix);
119}
120//echo "Adding unity kernel ok".PHP_EOL;
121
122// Test adding kernel works and you can get the values back
123$matrix1 = array(
124	array(1, 1),
125	array(0, 0),
126);
127$matrix2 = array(
128	array(0, 0),
129	array(1, 1),
130);
131$kernel1 = ImagickKernel::fromMatrix($matrix1, array(0, 0));
132$kernel2 = ImagickKernel::fromMatrix($matrix2, array(0, 0));
133$kernel1->addKernel($kernel2);
134
135$kernelList = $kernel1->separate();
136if (count($kernelList) != 2) {
137	echo "Incorrect number of matrixes returned.";
138}
139else {
140	if ($kernelList[0]->getMatrix() != $matrix1) {
141		echo "Matrix 1 does not match".PHP_EOL;
142		var_dump($kernelList[0]);
143	}
144	if ($kernelList[1]->getMatrix() != $matrix2) {
145		echo "Matrix 2 does not match".PHP_EOL;
146		var_dump($kernelList[1]);
147	}
148}
149
150//Test Scaling
151$matrixIn = array(
152	array(-1, 0, -1),
153	array( 0, 8,  0),
154	array(-1, 0, -1),
155);
156$kernel = ImagickKernel::fromMatrix($matrixIn);
157$kernel->scale(1, \Imagick::NORMALIZE_KERNEL_VALUE);
158$matrixOut = $kernel->getMatrix();
159
160if ($matrixOut[1][1] != 2) {
161	echo "Matrix was not normalised correctly.";
162	var_dump($matrixOut);
163}
164
165
166
167//Test single line kernel works
168$matrixIn = array(
169	array(1, 0),
170);
171$kernel = ImagickKernel::fromMatrix($matrixIn, array(1, 0));
172if ($kernel->getMatrix() != $matrixIn) {
173	echo "Values do not match for 'Test single line kernel works'".PHP_EOL;
174}
175
176//Test even sized kernel works
177$matrixIn = array(
178	array(-1, 0),
179	array( 0, 1)
180);
181$kernel = ImagickKernel::fromMatrix($matrixIn, array(0, 1));
182if ($kernel->getMatrix() != $matrixIn) {
183	echo "Values do not match for 'Test even sized kernel works'".PHP_EOL;
184}
185
186//Test 'wrong' order matrix is converted correctly.
187$matrix = array(
188	array(0.0, 1.0),
189	array(0.5, false)
190);
191
192$outOfOrderMatrix = array();
193$outOfOrderMatrix[1][1] = $matrix[1][1];
194$outOfOrderMatrix[1][0] = $matrix[1][0];
195$outOfOrderMatrix[0][1] = $matrix[0][1];
196$outOfOrderMatrix[0][0] = $matrix[0][0];
197
198$kernel = ImagickKernel::fromMatrix($outOfOrderMatrix, array(0, 0));
199$kernelMatrix = $kernel->getMatrix();
200if ($kernelMatrix !== $matrix) {
201	echo "Kernel generated from 'out of order' matrix is incorrect.".PHP_EOL;
202	var_dump($matrix);
203	echo "vs".PHP_EOL;
204	var_dump($kernelMatrix);
205}
206
207echo "Complete".PHP_EOL;
208?>
209--EXPECTF--
210Complete
211