Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marat-Tanalin committed Mar 1, 2020
0 parents commit e321f3a
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Marat Tanalin (http://tanalin.com )

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Calculates integer ratios for pixel-perfect image upscaling with optional aspect-ratio correction.

See the project [webpage](http://tanalin.com/en/projects/integer-scaling/) for details.
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "marat-tanalin/integer-scaling",
"version" : "1.2.1",
"type": "library",
"description": "Calculates integer ratios for pixel-perfect image upscaling with optional aspect-ratio correction.",
"homepage": "http://tanalin.com/en/projects/integer-scaling/",
"license": "MIT",
"authors": [
{
"name": "Marat Tanalin",
"homepage": "http://tanalin.com"
}
],
"keywords": [
"integer-scaling",
"pixel-perfect-scaling",
"image-scaling",
"algorithms",
"graphics"
],
"require": {
"php": "^7.2"
},
"autoload": {
"psr-4": {
"MaratTanalin\\": "src/"
}
}
}
133 changes: 133 additions & 0 deletions src/IntegerScaling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

/*! Marat Tanalin | http://tanalin.com */

namespace MaratTanalin;

require_once 'IntegerScaling/Ratios.php';

class IntegerScaling
{
/**
* Calculates an integer scaling ratio common for X/Y axes (square pixels).
*
* @param int $areaWidth
* @param int $areaHeight
* @param int $imageWidth
* @param int $imageHeight
* @return int;
*/
public static function calculateRatio(int $areaWidth, int $areaHeight, int $imageWidth, int $imageHeight) {
if ($areaHeight * $imageWidth < $areaWidth * $imageHeight) {
$areaSize = $areaHeight;
$imageSize = $imageHeight;
}
else {
$areaSize = $areaWidth;
$imageSize = $imageWidth;
}

$ratio = floor($areaSize / $imageSize);

if ($ratio < 1) {
$ratio = 1;
}

return $ratio;
}

/**
* Calculates integer scaling ratios potentially different for X/Y axes
* as a result of aspect-ratio correction (rectangular pixels).
*
* @param int $areaWidth
* @param int $areaHeight
* @param int $imageWidth
* @param int $imageHeight
* @param float $aspectX
* @param float $aspectY
* @return array(string => int)
*/
public static function calculateRatios(int $areaWidth, int $areaHeight, int $imageWidth, int $imageHeight, float $aspectX = 0.0, float $aspectY = 0.0) {
if ($imageWidth * $aspectY === $imageHeight * $aspectX) {
$ratio = self::calculateRatio($areaWidth, $areaHeight, $imageWidth, $imageHeight);

return new IntegerScaling\Ratios($ratio);
}

$maxRatioX = floor($areaWidth / $imageWidth);
$maxRatioY = floor($areaHeight / $imageHeight);
$maxWidth = $imageWidth * $maxRatioX;
$maxHeight = $imageHeight * $maxRatioY;
$maxWidthAspectY = $maxWidth * $aspectY;
$maxHeightAspectX = $maxHeight * $aspectX;

if ($maxWidthAspectY === $maxHeightAspectX) {
$ratioX = $maxRatioX;
$ratioY = $maxRatioY;
}
else {
$maxAspectLessThanTarget = $maxWidthAspectY < $maxHeightAspectX;

if ($maxAspectLessThanTarget) {
$ratioA = $maxRatioX;
$maxSizeA = $maxWidth;
$imageSizeB = $imageHeight;
$aspectA = $aspectX;
$aspectB = $aspectY;
}
else {
$ratioA = $maxRatioY;
$maxSizeA = $maxHeight;
$imageSizeB = $imageWidth;
$aspectA = $aspectY;
$aspectB = $aspectX;
}

$ratioBFract = $maxSizeA * $aspectB / $aspectA / $imageSizeB;
$ratioBFloor = floor($ratioBFract);
$ratioBCeil = ceil($ratioBFract);
$parFloor = $ratioBFloor / $ratioA;
$parCeil = $ratioBCeil / $ratioA;

if ($maxAspectLessThanTarget) {
$parFloor = 1 / $parFloor;
$parCeil = 1 / $parCeil;
}

$commonFactor = $imageWidth * $aspectY / $aspectX / $imageHeight;
$errorFloor = abs(1 - $commonFactor * $parFloor);
$errorCeil = abs(1 - $commonFactor * $parCeil);

if (abs($errorFloor - $errorCeil) < .001) {
$ratioB = abs($ratioA - $ratioBFloor) < abs($ratioA - $ratioBCeil)
? $ratioBFloor
: $ratioBCeil;
}
else {
$ratioB = $errorFloor < $errorCeil
? $ratioBFloor
: $ratioBCeil;
}

if ($maxAspectLessThanTarget) {
$ratioX = $ratioA;
$ratioY = $ratioB;
}
else {
$ratioX = $ratioB;
$ratioY = $ratioA;
}
}

if ($ratioX < 1) {
$ratioX = 1;
}

if ($ratioY < 1) {
$ratioY = 1;
}

return new IntegerScaling\Ratios($ratioX, $ratioY);
}
}
15 changes: 15 additions & 0 deletions src/IntegerScaling/Ratios.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/*! Marat Tanalin | http://tanalin.com */

namespace MaratTanalin\IntegerScaling;

class Ratios
{
public $x, $y;

public function __construct(int $x, int $y = 0) {
$this->x = $x;
$this->y = 0 === $y ? $x : $y;
}
}

0 comments on commit e321f3a

Please sign in to comment.