Index: trunk/extensions/VipsScaler/VipsScaler_body.php |
— | — | @@ -97,29 +97,7 @@ |
98 | 98 | } |
99 | 99 | |
100 | 100 | 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'] ); |
124 | 102 | } |
125 | 103 | |
126 | 104 | if ( !empty( $options['convolution'] ) ) { |
— | — | @@ -135,7 +113,46 @@ |
136 | 114 | return $commands; |
137 | 115 | } |
138 | 116 | |
| 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 | + } |
139 | 155 | |
| 156 | + |
140 | 157 | /** |
141 | 158 | * Check the file and params against $wgVipsOptions |
142 | 159 | * |
— | — | @@ -319,7 +336,7 @@ |
320 | 337 | # Convert a 2D array into a space/newline separated matrix |
321 | 338 | $convolutionMatrix = array_pop( $this->args ); |
322 | 339 | $convolutionString = ''; |
323 | | - foreach ( $convolutionMatrix as $row ) { |
| 340 | + foreach ( $convolutionMatrix as $i=>$row ) { |
324 | 341 | $convolutionString .= implode( ' ', $row ) . "\n"; |
325 | 342 | } |
326 | 343 | # Save the matrix in a tempfile |
— | — | @@ -327,6 +344,8 @@ |
328 | 345 | file_put_contents( $convolutionFile, $convolutionString ); |
329 | 346 | array_push( $this->args, $convolutionFile ); |
330 | 347 | |
| 348 | + wfDebug( __METHOD__ . ": Convolving image [\n" . $convolutionString . "] \n" ); |
| 349 | + |
331 | 350 | # Call the parent to actually execute the command |
332 | 351 | $retval = parent::execute(); |
333 | 352 | |
Index: trunk/extensions/VipsScaler/VipsScaler.php |
— | — | @@ -44,7 +44,7 @@ |
45 | 45 | 'mimeType' => 'image/jpeg', |
46 | 46 | 'minShrinkFactor' => 1.2, |
47 | 47 | ), |
48 | | - 'sharpen' => true, |
| 48 | + 'sharpen' => array( 'radius' => 0, 'sigma' => 0.8 ), |
49 | 49 | ), |
50 | 50 | # Other jpeg files |
51 | 51 | array( |