Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaked jpeg-js library encoder to allow for RGB image data (rather than... #2

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 17 additions & 17 deletions lib/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ function JPEGEncoder(quality) {
}
}

this.encode = function(image,quality) // image data object
this.encode = function(image,quality,channels) // image data object
{
var time_start = new Date().getTime();

Expand Down Expand Up @@ -620,36 +620,35 @@ function JPEGEncoder(quality) {
var width = image.width;
var height = image.height;

var quadWidth = width*4;
var tripleWidth = width*3;
var channelWidth = width*channels;

var x, y = 0;
var r, g, b;
var start,p, col,row,pos;
while(y < height){
x = 0;
while(x < quadWidth){
start = quadWidth * y + x;
while(x < channelWidth){
start = channelWidth * y + x;
p = start;
col = -1;
row = 0;

for(pos=0; pos < 64; pos++){
row = pos >> 3;// /8
col = ( pos & 7 ) * 4; // %8
p = start + ( row * quadWidth ) + col;
col = ( pos & 7 ) * channels; // %8
p = start + ( row * channelWidth ) + col;

if(y+row >= height){ // padding bottom
p-= (quadWidth*(y+1+row-height));
p-= (channelWidth*(y+1+row-height));
}

if(x+col >= quadWidth){ // padding right
p-= ((x+col) - quadWidth +4)
if(x+col >= channelWidth){ // padding right
p-= ((x+col) - channelWidth + channels)
}

r = imageData[ p++ ];
g = imageData[ p++ ];
b = imageData[ p++ ];
r = imageData[ p ];
g = imageData[ p+1 ];
b = imageData[ p+2 ];


/* // calculate YUV values dynamically
Expand All @@ -668,9 +667,9 @@ function JPEGEncoder(quality) {
DCY = processDU(YDU, fdtbl_Y, DCY, YDC_HT, YAC_HT);
DCU = processDU(UDU, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);
DCV = processDU(VDU, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);
x+=32;
x += 8 * channels;
}
y+=8;
y += 8;
}


Expand Down Expand Up @@ -742,10 +741,11 @@ function JPEGEncoder(quality) {
};
module.exports = encode;

function encode(imgData, qu) {
function encode(imgData, qu, channels) {
if (typeof qu === 'undefined') qu = 50;
if (typeof channels === 'undefined') channels = 3;
var encoder = new JPEGEncoder(qu);
var data = encoder.encode(imgData, qu);
var data = encoder.encode(imgData, qu, channels);
return {
data: data,
width: imgData.width,
Expand Down