r85966 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85965‎ | r85966 | r85967 >
Date:18:03, 13 April 2011
Author:btongminh
Status:deferred
Tags:
Comment:
Use the ImageMagick sharpening algorithm to generate a convolution matrix. Made this more configurable by using the array( 'radius' => 0, 'sigma' => 0.8 ) syntax.
Modified paths:
  • /trunk/extensions/VipsScaler/VipsScaler.php (modified) (history)
  • /trunk/extensions/VipsScaler/VipsScaler_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/VipsScaler/VipsScaler_body.php
@@ -97,29 +97,7 @@
9898 }
9999
100100 if ( !empty( $options['sharpen'] ) ) {
101 - # Use a Laplacian-of-Gaussian convolution matrix with sigma=0.4
102 - # See http://homepages.inf.ed.ac.uk/rbf/HIPR2/unsharp.htm for
103 - # an explanation on how to use the Laplacian operator to
104 - # sharpen an image using a convolution matrix
105 - /*
106 - # Integer version of the convolution matrix below. Can be used
107 - # if im_conv instead of im_convf is prefered.
108 - $convolution = array(
109 - # Matrix descriptor
110 - array( 3, 3, 8, 0 ),
111 - # Matrix itself
112 - array( 0, -1, 0 ),
113 - array( -1, 12, -1 ),
114 - array( 0, -1, 0 )
115 - );
116 - */
117 - $options['convolution'] = array(
118 - array( 3, 3, 7.2863, 0 ),
119 - array( -0.1260, -1.1609, -0.1260 ),
120 - array( -1.1609, 12.4340, -1.1609 ),
121 - array( -0.1260, -1.1609, -0.1260 ),
122 - );
123 -
 101+ $options['convolution'] = self::makeSharpenMatrix( $options['sharpen'] );
124102 }
125103
126104 if ( !empty( $options['convolution'] ) ) {
@@ -135,7 +113,46 @@
136114 return $commands;
137115 }
138116
 117+ /**
 118+ * Create a sharpening matrix suitable for im_convf. Uses the ImageMagick
 119+ * sharpening algorithm from SharpenImage() in magick/effect.c
 120+ *
 121+ * @param mixed $params
 122+ * @return array
 123+ */
 124+ public static function makeSharpenMatrix( $params ) {
 125+ $sigma = $params['sigma'];
 126+ $radius = empty( $params['radius'] ) ?
 127+ # After 3 sigma there should be no significant values anymore
 128+ intval( round( $sigma * 3 ) ) : $params['radius'];
 129+
 130+ $norm = 0;
 131+ $conv = array();
 132+
 133+ # Fill the matrix with a negative Gaussian distribution
 134+ $variance = $sigma * $sigma;
 135+ for ( $x = -$radius; $x <= $radius; $x++ ) {
 136+ $row = array();
 137+ for ( $y = -$radius; $y <= $radius; $y++ ) {
 138+ $z = -exp( -( $x*$x + $y*$y ) / ( 2 * $variance ) ) /
 139+ ( 2 * pi() * $variance );
 140+ $row[] = $z;
 141+ $norm += $z;
 142+ }
 143+ $conv[] = $row;
 144+ }
 145+
 146+ # Calculate the scaling parameter to ensure that the mean of the
 147+ # matrix is zero
 148+ $scale = - $conv[$radius][$radius] - $norm;
 149+ # Set the center pixel to obtain a sharpening matrix
 150+ $conv[$radius][$radius] = -$norm * 2;
 151+ # Add the matrix descriptor
 152+ array_unshift( $conv, array( $radius * 2 + 1, $radius * 2 + 1, $scale, 0 ) );
 153+ return $conv;
 154+ }
139155
 156+
140157 /**
141158 * Check the file and params against $wgVipsOptions
142159 *
@@ -319,7 +336,7 @@
320337 # Convert a 2D array into a space/newline separated matrix
321338 $convolutionMatrix = array_pop( $this->args );
322339 $convolutionString = '';
323 - foreach ( $convolutionMatrix as $row ) {
 340+ foreach ( $convolutionMatrix as $i=>$row ) {
324341 $convolutionString .= implode( ' ', $row ) . "\n";
325342 }
326343 # Save the matrix in a tempfile
@@ -327,6 +344,8 @@
328345 file_put_contents( $convolutionFile, $convolutionString );
329346 array_push( $this->args, $convolutionFile );
330347
 348+ wfDebug( __METHOD__ . ": Convolving image [\n" . $convolutionString . "] \n" );
 349+
331350 # Call the parent to actually execute the command
332351 $retval = parent::execute();
333352
Index: trunk/extensions/VipsScaler/VipsScaler.php
@@ -44,7 +44,7 @@
4545 'mimeType' => 'image/jpeg',
4646 'minShrinkFactor' => 1.2,
4747 ),
48 - 'sharpen' => true,
 48+ 'sharpen' => array( 'radius' => 0, 'sigma' => 0.8 ),
4949 ),
5050 # Other jpeg files
5151 array(

Status & tagging log