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

mobile out of memory optimisation for decoding large images #480

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/imageLoader/decodeJPEGBaseline8BitColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,27 @@ function decodeJPEGBaseline8BitColor(imageFrame, pixelData, canvas) {
fileReader.onload = function() {
const img = new Image();

img.onload = function() {
img.onload = function () {

const maxSize = Math.max(
img.height,
img.width
)
const maxSizeThresh = 1000

if (maxSize > maxSizeThresh) {
const ratio = maxSizeThresh / maxSize
img.width *= ratio
img.height *= ratio
}

canvas.height = img.height;
canvas.width = img.width;
imageFrame.rows = img.height;
imageFrame.columns = img.width;
const context = canvas.getContext('2d');

context.drawImage(this, 0, 0);
context.drawImage(this, 0, 0,img.width,img.height);
const imageData = context.getImageData(0, 0, img.width, img.height);
const end = new Date().getTime();

Expand Down
34 changes: 34 additions & 0 deletions src/webWorker/decodeTask/decodeTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,40 @@ function handler(data, doneCallback) {
);
}

// snippet for resizing the image pixel data for Mobile
if (imageFrame.samplesPerPixel === 1) {
const maxSize = Math.max(imageFrame.columns, imageFrame.rows);
const maxSizeThresh = 1000;
if (maxSize > maxSizeThresh) {
const factor = maxSize / maxSizeThresh;
const width = imageFrame.columns; // width is columns
const height = imageFrame.rows;
const newWidth = Math.floor(width / factor);
const newHeight = Math.floor(height / factor);

// create new array same type as original
const resizedPixelData = new imageFrame.pixelData.constructor(
newWidth * newHeight
);
// resize using nearest neighbour interpolation
for (let i = 0; i < resizedPixelData.length; i++) {
const x = i % newWidth;
const y = Math.floor(i / newWidth);

const projX = Math.floor(x * factor);
const projY = Math.floor(y * factor);
const projI = projX + projY * width;

resizedPixelData[i] = imageFrame.pixelData[projI];
}

imageFrame.columns = newWidth;
imageFrame.rows = newHeight;
imageFrame.pixelData = resizedPixelData;
imageFrame.pixelDataLength = resizedPixelData.length;
}
}

calculateMinMax(imageFrame, strict);

// convert from TypedArray to ArrayBuffer since web workers support passing ArrayBuffers but not
Expand Down