File: JpegPixelFormatTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (114 lines) | stat: -rw-r--r-- 2,423 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

use MediaWiki\MainConfigNames;
use MediaWiki\Shell\Shell;

/**
 * Tests related to JPEG chroma subsampling via $wgJpegPixelFormat setting.
 *
 * @group Media
 * @group medium
 */
class JpegPixelFormatTest extends MediaWikiMediaTestCase {

	/**
	 * Mark this test as creating thumbnail files.
	 * @inheritDoc
	 */
	protected function createsThumbnails() {
		return true;
	}

	/**
	 * @dataProvider providePixelFormats
	 * @covers \BitmapHandler::imageMagickSubsampling
	 */
	public function testPixelFormatRendering( $sourceFile, $pixelFormat, $samplingFactor ) {
		global $wgUseImageMagick, $wgUseImageResize;
		if ( !$wgUseImageMagick ) {
			$this->markTestSkipped( "This test is only applicable when using ImageMagick thumbnailing" );
		}
		if ( !$wgUseImageResize ) {
			$this->markTestSkipped( "This test is only applicable when using thumbnailing" );
		}

		$fmtStr = var_export( $pixelFormat, true );
		$this->overrideConfigValue( MainConfigNames::JpegPixelFormat, $pixelFormat );

		$file = $this->dataFile( $sourceFile, 'image/jpeg' );

		$params = [
			'width' => 320,
		];
		$thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
		$this->assertTrue( !$thumb->isError(), "created JPEG thumbnail for pixel format $fmtStr" );

		$path = $thumb->getLocalCopyPath();
		$this->assertIsString( $path, "path returned for JPEG thumbnail for $fmtStr" );

		$result = Shell::command( 'identify',
			'-format',
			'%[jpeg:sampling-factor]',
			$path
		)->execute();
		$this->assertSame( 0,
			$result->getExitCode(),
			"ImageMagick's identify command should return success"
		);

		$expected = $samplingFactor;
		$actual = trim( $result->getStdout() );
		$this->assertEquals(
			$expected,
			$actual,
			"IM identify expects JPEG chroma subsampling \"$expected\" for $fmtStr"
		);
	}

	public static function providePixelFormats() {
		return [
			// From 4:4:4 source file
			[
				'yuv444.jpg',
				false,
				'1x1,1x1,1x1'
			],
			[
				'yuv444.jpg',
				'yuv444',
				'1x1,1x1,1x1'
			],
			[
				'yuv444.jpg',
				'yuv422',
				'2x1,1x1,1x1'
			],
			[
				'yuv444.jpg',
				'yuv420',
				'2x2,1x1,1x1'
			],
			// From 4:2:0 source file
			[
				'yuv420.jpg',
				false,
				'2x2,1x1,1x1'
			],
			[
				'yuv420.jpg',
				'yuv444',
				'1x1,1x1,1x1'
			],
			[
				'yuv420.jpg',
				'yuv422',
				'2x1,1x1,1x1'
			],
			[
				'yuv420.jpg',
				'yuv420',
				'2x2,1x1,1x1'
			]
		];
	}
}