r98491 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98490‎ | r98491 | r98492 >
Date:23:42, 29 September 2011
Author:reedy
Status:deferred (Comments)
Tags:
Comment:
Refactor out duplicate code
Modified paths:
  • /trunk/extensions/LilyPond/LilyPond.class.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LilyPond/LilyPond.class.php
@@ -226,60 +226,57 @@
227227 $width = imagesx( $srcImage );
228228 $height = imagesy( $srcImage );
229229
230 - $xmin = 0;
231 - $found = false;
232 - for ( $x = 0; $x < $width && !$found; $x++ ) {
233 - for ( $y = 0; $y < $height && !$found; $y++ ) {
234 - $rgb = imagecolorat( $srcImage, $x, $y );
235 - if ( $rgb != $bgColour ) {
236 - $xmin = $x;
237 - $found = true;
238 - }
239 - }
240 - }
 230+ $xmin = self::findMin( $width, $height, $srcImage, $bgColour );
 231+ $xmax = self::findMax( $width, $height, $xmin, $srcImage, $bgColour );
241232
242 - $xmax = $xmin;
243 - $found = false;
244 - for ( $x = $width -1; $x > $xmin && !$found; $x-- ) {
245 - for ( $y = 0; $y < $height && !$found; $y++ ) {
246 - $rgb = imagecolorat( $srcImage, $x, $y );
247 - if ( $rgb != $bgColour ) {
248 - $xmax = $x;
249 - $found = true;
250 - }
251 - }
252 - }
 233+ $ymin = self::findMin( $height, $width, $srcImage, $bgColour );
 234+ $ymax = self::findMax( $height, $width, $ymin, $srcImage, $bgColour );
253235
254 - $ymin = 0;
255 - $found = false;
256 - for ( $y = 0; $y < $height && !$found; $y++ ) {
257 - for ( $x = 0; $x < $width && !$found; $x++ ) {
 236+ $newWidth = $xmax - $xmin + 1;
 237+ $newHeight = $ymax - $ymin + 1;
 238+
 239+ $dstImage = imagecreatetruecolor( $newWidth, $newHeight );
 240+ imagecopy( $dstImage, $srcImage, 0, 0, $xmin, $ymin, $newWidth, $newHeight );
 241+ imagepng( $dstImage, $dest );
 242+ }
 243+
 244+ /**
 245+ * @param $outer int
 246+ * @param $inner int
 247+ * @param $srcImage
 248+ * @param $bgColour
 249+ * @return int
 250+ */
 251+ static function findMin( $outer, $inner, $srcImage, $bgColour ) {
 252+ for ( $x = 0; $x < $outer; $x++ ) {
 253+ for ( $y = 0; $y < $inner; $y++ ) {
258254 $rgb = imagecolorat( $srcImage, $x, $y );
259255 if ( $rgb != $bgColour ) {
260 - $ymin = $y;
261 - $found = true;
 256+ return $x;
262257 }
263258 }
264259 }
 260+ return 0;
 261+ }
265262
266 - $ymax = $ymin;
267 - $found = false;
268 - for ( $y = $height -1; $y > $ymin && !$found; $y-- ) {
269 - for ( $x = 0; $x < $width && !$found; $x++ ) {
 263+ /**
 264+ * @param $outer int
 265+ * @param $inner int
 266+ * @param $min int
 267+ * @param $srcImage
 268+ * @param $bgColour
 269+ * @return int
 270+ */
 271+ static function findMax( $outer, $inner, $min, $srcImage, $bgColour ) {
 272+ for ( $x = $outer - 1; $x > $min; $x-- ) {
 273+ for ( $y = 0; $y < $inner; $y++ ) {
270274 $rgb = imagecolorat( $srcImage, $x, $y );
271275 if ( $rgb != $bgColour ) {
272 - $ymax = $y;
273 - $found = true;
 276+ return $x;
274277 }
275278 }
276279 }
277 -
278 - $newWidth = $xmax - $xmin + 1;
279 - $newHeight = $ymax - $ymin + 1;
280 -
281 - $dstImage = imagecreatetruecolor( $newWidth, $newHeight );
282 - imagecopy( $dstImage, $srcImage, 0, 0, $xmin, $ymin, $newWidth, $newHeight );
283 - imagepng( $dstImage, $dest );
 280+ return 0;
284281 }
285282
286283 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r100248Followup r98491...reedy18:11, 19 October 2011

Comments

#Comment by He7d3r (talk | contribs)   01:18, 19 October 2011

It seems to me that

return 0;

on function findMax should have been

return $min;

Could you check that?

#Comment by Reedy (talk | contribs)   01:37, 19 October 2011

I think you're right

#Comment by Tim Starling (talk | contribs)   23:28, 11 December 2011

Fixme per note on CR r98414: these functions don't find bounds in the Y direction.

Status & tagging log