Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Remove padding during scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelerz committed May 23, 2015
1 parent dbd94de commit 334c55e
Showing 1 changed file with 55 additions and 8 deletions.
63 changes: 55 additions & 8 deletions lib/png/utils/scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ scale.scaleUp = function (input, output, spreadFn, isColor, hasAlphaChannel, isI
bytes = [],
bytesNeeded,
method,
increment = 1;
increment = 1,
currentBytes;

if (isIndexedColor) {
bytesNeeded = 1;
Expand Down Expand Up @@ -146,7 +147,8 @@ scale.scaleUp = function (input, output, spreadFn, isColor, hasAlphaChannel, isI
spreadFn(value, bytes);

while (bytes.length >= bytesNeeded) {
this[method](output, bytes.splice(0, bytesNeeded));
currentBytes = bytes.splice(0, bytesNeeded);
this[method](output, currentBytes);
}
}
};
Expand All @@ -161,12 +163,15 @@ scale.scaleUp = function (input, output, spreadFn, isColor, hasAlphaChannel, isI
* @param {boolean} isColor Is data in color?
* @param {boolean} hasAlphaChannel Does data have an alpha-channel?
* @param {boolean} isIndexedColor Is image indexed on a palette?
* @param {number} paddingAt Defines the position of padding within each scanline
*/
scale.scale1to8bit = function (input, output, isColor, hasAlphaChannel, isIndexedColor) {
scale.scale1to8bit = function (input, output, isColor, hasAlphaChannel, isIndexedColor, paddingAt) {

var scaleFactor = isIndexedColor ? 1 : 255; // 255 / 1
var scaleFactor = isIndexedColor ? 1 : 255, // 255 / 1
byteCounter = 0;

this.scaleUp(input, output, function (value, bytes) {

bytes.push(((value >> 7) & 1) * scaleFactor);
bytes.push(((value >> 6) & 1) * scaleFactor);
bytes.push(((value >> 5) & 1) * scaleFactor);
Expand All @@ -175,6 +180,18 @@ scale.scale1to8bit = function (input, output, isColor, hasAlphaChannel, isIndexe
bytes.push(((value >> 2) & 1) * scaleFactor);
bytes.push(((value >> 1) & 1) * scaleFactor);
bytes.push((value & 1) * scaleFactor);

// Make sure that padding is removed
if (paddingAt) {
byteCounter += 8;
if (byteCounter >= paddingAt) {
if (byteCounter > paddingAt) {
bytes.splice(-(byteCounter - paddingAt));
}
byteCounter = 0;
}
}

}, isColor, hasAlphaChannel, isIndexedColor);
};

Expand All @@ -188,16 +205,31 @@ scale.scale1to8bit = function (input, output, isColor, hasAlphaChannel, isIndexe
* @param {boolean} isColor Is data in color?
* @param {boolean} hasAlphaChannel Does data have an alpha-channel?
* @param {boolean} isIndexedColor Is image indexed on a palette?
* @param {number} paddingAt Defines the position of padding within each scanline
*/
scale.scale2to8bit = function (input, output, isColor, hasAlphaChannel, isIndexedColor) {
scale.scale2to8bit = function (input, output, isColor, hasAlphaChannel, isIndexedColor, paddingAt) {

var scaleFactor = isIndexedColor ? 1 : 255 / 3;
var scaleFactor = isIndexedColor ? 1 : 255 / 3,
byteCounter = 0;

this.scaleUp(input, output, function (value, bytes) {

bytes.push(((value >> 6) & 3) * scaleFactor);
bytes.push(((value >> 4) & 3) * scaleFactor);
bytes.push(((value >> 2) & 3) * scaleFactor);
bytes.push((value & 3) * scaleFactor);

// Make sure that padding is removed
if (paddingAt) {
byteCounter += 4;
if (byteCounter >= paddingAt) {
if (byteCounter > paddingAt) {
bytes.splice(-(byteCounter - paddingAt));
}
byteCounter = 0;
}
}

}, isColor, hasAlphaChannel, isIndexedColor);
};

Expand All @@ -211,14 +243,29 @@ scale.scale2to8bit = function (input, output, isColor, hasAlphaChannel, isIndexe
* @param {boolean} isColor Is data in color?
* @param {boolean} hasAlphaChannel Does data have an alpha-channel?
* @param {boolean} isIndexedColor Is image indexed on a palette?
* @param {number} paddingAt Defines the position of padding within each scanline
*/
scale.scale4to8bit = function (input, output, isColor, hasAlphaChannel, isIndexedColor) {
scale.scale4to8bit = function (input, output, isColor, hasAlphaChannel, isIndexedColor, paddingAt) {

var scaleFactor = isIndexedColor ? 1 : 255 / 15;
var scaleFactor = isIndexedColor ? 1 : 255 / 15,
byteCounter = 0;

this.scaleUp(input, output, function (value, bytes) {

bytes.push(((value >> 4) & 15) * scaleFactor);
bytes.push((value & 15) * scaleFactor);

// Make sure that padding is removed
if (paddingAt) {
byteCounter += 2;
if (byteCounter >= paddingAt) {
if (byteCounter > paddingAt) {
bytes.splice(-(byteCounter - paddingAt));
}
byteCounter = 0;
}
}

}, isColor, hasAlphaChannel, isIndexedColor);
};

Expand Down

0 comments on commit 334c55e

Please sign in to comment.