Skip to content

Commit d230208

Browse files
author
Ruslan Yarullin
committed
some optimizations
1 parent f00f53f commit d230208

File tree

8 files changed

+26
-29
lines changed

8 files changed

+26
-29
lines changed

lib/IMagickLuminanceSource.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class IMagickLuminanceSource extends LuminanceSource
1616
private $image;
1717

1818
public function __construct(
19-
$image,
19+
\Imagick $image,
2020
$dataWidth,
2121
$dataHeight,
2222
$left = null,
@@ -57,7 +57,7 @@ public function _IMagickLuminanceSource(\Imagick $image, $width, $height)
5757

5858
$image->setImageColorspace(\Imagick::COLORSPACE_GRAY);
5959
// $image->newPseudoImage(0, 0, "magick:rose");
60-
$pixels = $image->exportImagePixels(1, 1, $width, $height, "RGB", \Imagick::COLORSPACE_RGB);
60+
$pixels = $image->exportImagePixels(1, 1, $width, $height, "RGB", \Imagick::PIXEL_CHAR);
6161

6262
$array = [];
6363
$rgb = [];
@@ -137,6 +137,8 @@ public function isCropSupported()
137137
//@Override
138138
public function crop($left, $top, $width, $height)
139139
{
140+
return $this->luminances->cropImage($width, $height, $left, $top);
141+
140142
return new GDLuminanceSource($this->luminances,
141143
$this->dataWidth,
142144
$this->dataHeight,

lib/PlanarYUVLuminanceSource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ public function renderThumbnail()
142142
for ($y = 0; $y < $height; $y++) {
143143
$outputOffset = $y * $width;
144144
for ($x = 0; $x < $width; $x++) {
145-
$grey = intval32bits($yuv[$inputOffset + $x * self::$THUMBNAIL_SCALE_FACTOR] & 0xff);
146-
$pixels[$outputOffset + $x] = intval32bits(0xFF000000 | ($grey * 0x00010101));
145+
$grey = ($yuv[$inputOffset + $x * self::$THUMBNAIL_SCALE_FACTOR] & 0xff);
146+
$pixels[$outputOffset + $x] = (0xFF000000 | ($grey * 0x00010101));
147147
}
148148
$inputOffset += $this->dataWidth * self::$THUMBNAIL_SCALE_FACTOR;
149149
}

lib/QrReader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,9 @@ public function text()
9797

9898
return $this->result;
9999
}
100+
101+
public function getResult()
102+
{
103+
return $this->result;
104+
}
100105
}

lib/common/BitArray.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public function get($i)
333333
{
334334
$key = (int)($i / 32);
335335

336-
return intval32bits($this->bits[$key] & (1 << ($i & 0x1F))) != 0;
336+
return ($this->bits[$key] & (1 << ($i & 0x1F))) != 0;
337337
}
338338

339339
/**

lib/common/GlobalHistogramBinarizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private static function estimateBlackPoint($buckets)
158158
}
159159
}
160160

161-
return intval32bits($bestValley << self::$LUMINANCE_SHIFT);
161+
return ($bestValley << self::$LUMINANCE_SHIFT);
162162
}
163163

164164
public function getBlackMatrix()
@@ -177,8 +177,8 @@ public function getBlackMatrix()
177177
$localLuminances = $source->getRow($row, $this->luminances);
178178
$right = (int)(($width * 4) / 5);
179179
for ($x = (int)($width / 5); $x < $right; $x++) {
180-
$pixel = intval32bits($localLuminances[(int)($x)] & 0xff);
181-
$localBuckets[intval32bits($pixel >> self::$LUMINANCE_SHIFT)]++;
180+
$pixel = ($localLuminances[(int)($x)] & 0xff);
181+
$localBuckets[($pixel >> self::$LUMINANCE_SHIFT)]++;
182182
}
183183
}
184184
$blackPoint = self::estimateBlackPoint($localBuckets);

lib/common/HybridBinarizer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ private static function calculateBlackPoints(
114114
$blackPoints[$key] = fill_array(0, $subWidth, 0);
115115
}
116116
for ($y = 0; $y < $subHeight; $y++) {
117-
$yoffset = intval32bits($y << self::$BLOCK_SIZE_POWER);
117+
$yoffset = ($y << self::$BLOCK_SIZE_POWER);
118118
$maxYOffset = $height - self::$BLOCK_SIZE;
119119
if ($yoffset > $maxYOffset) {
120120
$yoffset = $maxYOffset;
121121
}
122122
for ($x = 0; $x < $subWidth; $x++) {
123-
$xoffset = intval32bits($x << self::$BLOCK_SIZE_POWER);
123+
$xoffset = ($x << self::$BLOCK_SIZE_POWER);
124124
$maxXOffset = $width - self::$BLOCK_SIZE;
125125
if ($xoffset > $maxXOffset) {
126126
$xoffset = $maxXOffset;
@@ -130,7 +130,7 @@ private static function calculateBlackPoints(
130130
$max = 0;
131131
for ($yy = 0, $offset = $yoffset * $width + $xoffset; $yy < self::$BLOCK_SIZE; $yy++, $offset += $width) {
132132
for ($xx = 0; $xx < self::$BLOCK_SIZE; $xx++) {
133-
$pixel = intval32bits((int)($luminances[(int)($offset + $xx)]) & 0xFF);
133+
$pixel = ((int)($luminances[(int)($offset + $xx)]) & 0xFF);
134134
$sum += $pixel;
135135
// still looking for good contrast
136136
if ($pixel < $min) {
@@ -145,14 +145,14 @@ private static function calculateBlackPoints(
145145
// finish the rest of the rows quickly
146146
for ($yy++, $offset += $width; $yy < self::$BLOCK_SIZE; $yy++, $offset += $width) {
147147
for ($xx = 0; $xx < self::$BLOCK_SIZE; $xx++) {
148-
$sum += intval32bits($luminances[$offset + $xx] & 0xFF);
148+
$sum += ($luminances[$offset + $xx] & 0xFF);
149149
}
150150
}
151151
}
152152
}
153153

154154
// The default estimate is the average of the values in the block.
155-
$average = intval32bits($sum >> (self::$BLOCK_SIZE_POWER * 2));
155+
$average = ($sum >> (self::$BLOCK_SIZE_POWER * 2));
156156
if ($max - $min <= self::$MIN_DYNAMIC_RANGE) {
157157
// If variation within the block is low, assume this is a block with only light or only
158158
// dark pixels. In that case we do not want to use the average, as it would divide this
@@ -199,13 +199,13 @@ private static function calculateThresholdForBlock(
199199
$matrix
200200
) {
201201
for ($y = 0; $y < $subHeight; $y++) {
202-
$yoffset = intval32bits($y << self::$BLOCK_SIZE_POWER);
202+
$yoffset = ($y << self::$BLOCK_SIZE_POWER);
203203
$maxYOffset = $height - self::$BLOCK_SIZE;
204204
if ($yoffset > $maxYOffset) {
205205
$yoffset = $maxYOffset;
206206
}
207207
for ($x = 0; $x < $subWidth; $x++) {
208-
$xoffset = intval32bits($x << self::$BLOCK_SIZE_POWER);
208+
$xoffset = ($x << self::$BLOCK_SIZE_POWER);
209209
$maxXOffset = $width - self::$BLOCK_SIZE;
210210
if ($xoffset > $maxXOffset) {
211211
$xoffset = $maxXOffset;

lib/common/customFunctions.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,14 @@ function numberOfTrailingZeros($i)
3131
return $num;
3232
}
3333

34-
function intval32bits($value)
35-
{
36-
$value = ($value & 0xFFFFFFFF);
37-
38-
if ($value & 0x80000000) {
39-
$value = -((~$value & 0xFFFFFFFF) + 1);
40-
}
41-
42-
return $value;
43-
}
44-
4534
function uRShift($a, $b)
4635
{
47-
if ($b == 0) {
36+
static $mask = (8 * PHP_INT_SIZE - 1);
37+
if ($b === 0) {
4838
return $a;
4939
}
5040

51-
return ($a >> $b) & ~(1 << (8 * PHP_INT_SIZE - 1) >> ($b - 1));
41+
return ($a >> $b) & ~(1 << $mask >> ($b - 1));
5242
}
5343

5444
/*

lib/qrcode/detector/Detector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected final function processFinderPatternInfo($info)
8080
if ($moduleSize < 1.0) {
8181
throw NotFoundException::getNotFoundInstance();
8282
}
83-
$dimension = (int)$this->computeDimension($topLeft, $topRight, $bottomLeft, $moduleSize);
83+
$dimension = (int)self::computeDimension($topLeft, $topRight, $bottomLeft, $moduleSize);
8484
$provisionalVersion = Version::getProvisionalVersionForDimension($dimension);
8585
$modulesBetweenFPCenters = $provisionalVersion->getDimensionForVersion() - 7;
8686

0 commit comments

Comments
 (0)