RescaleOpFilter::loop()   C
last analyzed

Complexity

Conditions 8
Paths 10

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 44
ccs 28
cts 28
cp 1
rs 5.3846
cc 8
eloc 29
nc 10
nop 1
crap 8
1
<?php
2
3
/*
4
 * This file is part of the kaloa/image package.
5
 *
6
 * For full copyright and license information, please view the LICENSE file
7
 * that was distributed with this source code.
8
 */
9
10
namespace Kaloa\Image\Filter;
11
12
use Kaloa\Image\Filter\AbstractFilter;
13
use Kaloa\Image\Image;
14
15
/**
16
 *
17
 *
18
 * The class is modeled after java.awt.image.RescaleOp
19
 * <http://docs.oracle.com/javase/7/docs/api/java/awt/image/RescaleOp.html>
20
 */
21
final class RescaleOpFilter extends AbstractFilter
22
{
23
    /**
24
     *
25
     * @var array (float[3])
26
     */
27
    private $scaleFactors;
28
29
    /**
30
     *
31
     * @var array (float[3])
32
     */
33
    private $offsets;
34
35
    /**
36
     *
37
     * @param array $scaleFactors Scaling factors for (r, g, b) components
38
     * @param array $offsets      Offsets for (r, g, b) components
39
     */
40 1
    public function __construct(array $scaleFactors, array $offsets)
41
    {
42 1
        $this->scaleFactors = $scaleFactors;
43 1
        $this->offsets = $offsets;
44 1
    }
45
46
    /**
47
     * Apply scaling factors and offsets to each pixel
48
     *
49
     * The algorithm uses micro-optimization techniques. As it is written in
50
     * PHP, nevertheless expect runtimes in the range of a second per 200,000
51
     * pixels or so. That is really slow compared to non-PHP implementations.
52
     *
53
     * @param  resource $img GD image resource
54
     * @return resource GD image resource
55
     */
56 1
    private function loop($img)
57
    {
58
        // Cache already computed pixels
59 1
        $cache = array();
60
61
        // Compute all 256 rescaled values for each color dimension (R, G, B)
62 1
        for ($dimension = 0; $dimension <= 2; ++$dimension) {
63 1
            $s = $this->scaleFactors[$dimension];
64 1
            $o = $this->offsets[$dimension];
65
66 1
            $table[$dimension] = array_fill(0, 256, 0);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$table was never initialized. Although not strictly required by PHP, it is generally a good practice to add $table = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
67 1
            for ($i = 0; $i <= 255; $i++) {
68 1
                $tmp = (int) round($i * $s + $o);
69 1
                if ($tmp > 255) $tmp = 255; else if ($tmp < 0) $tmp = 0;
70 1
                $table[$dimension][$i] = $tmp;
0 ignored issues
show
Bug introduced by
The variable $table does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
71 1
            }
72 1
        }
73
74
        // Dereferencing ($table[0]) would be slower
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75 1
        list($table0, $table1, $table2) = $table;
76
77 1
        $width = imagesx($img);
78 1
        $y = imagesy($img);
79
80
        // Loops through the image row by row from bottom right to top left. The
81
        // idea here is to reduce the number of opcodes in the inner loop
82
        do {
83 1
            --$y;
84 1
            $x = $width;
85
            do
86 1
                if (isset($cache[$rgb = imagecolorat($img, --$x, $y)]))
87 1
                    imagesetpixel($img, $x, $y, $cache[$rgb]);
88
                else
89 1
                    imagesetpixel($img, $x, $y,
90 1
                            $cache[$rgb] = imagecolorallocate(
91 1
                                    $img,
92 1
                                    $table0[$rgb >> 16 & 0xFF],
93 1
                                    $table1[$rgb >>  8 & 0xFF],
94 1
                                    $table2[$rgb       & 0xFF]));
95 1
            while ($x);
96 1
        } while ($y);
97
98 1
        return $img;
99
    }
100
101
    /**
102
     *
103
     *
104
     * For each pixel do: pixelVector = pixelVector * scaleFactors + offsets
105
     *
106
     * @param  resource $img
0 ignored issues
show
Bug introduced by
There is no parameter named $img. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
107
     * @return resource
108
     */
109 1
    public function render(Image $srcImg)
110
    {
111 1
        $img = $srcImg->getResource();
112
113 1
        $img = $this->loop($img);
114
115 1
        return $img;
116
    }
117
}
118