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

Cleanup: extract image post processing #126

Merged
6 commits merged into from
Dec 12, 2018
Merged
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
80 changes: 80 additions & 0 deletions src/image_processor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { HDRImage } from '../libs/hdrpng.js';

class gltfImageProcessor
{
processImages(gltf)
{
for (const gltfImage of gltf.images)
{
const image = gltfImage.image;
if (image instanceof HDRImage)
{
continue;
}

let newDimensions = undefined;

if (image.width === image.height)
{
newDimensions = this.processSquareImage(image);
}
else
{
newDimensions = this.processNonSquareImage(image);
}

if (newDimensions.width === image.width && newDimensions.height === image.height)
{
continue;
}

this.resizeImage(image, newDimensions);
}
}

processSquareImage(image)
{
const power = this.nearestPowerOf2(image.height);
return { width: power, height: power };
}

processNonSquareImage(image)
{
return { width: this.makeEven(image.width), height: this.makeEven(image.height) };
}

resizeImage(image, newDimensions)
{
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = newDimensions.width;
canvas.height = newDimensions.height;
context.drawImage(image, 0, 0, canvas.width, canvas.height);
image.src = canvas.toDataURL("image/png");
}

nearestPowerOf2(n)
{
if (this.isPowerOf2(n))
{
return n;
}
return Math.pow(2.0, Math.round(Math.log(n) / Math.log(2.0)));
}

isPowerOf2(n)
{
return n && (n & (n - 1)) === 0;
}

makeEven(n)
{
if (n % 2 === 1)
{
return n + 1;
}
return n;
}
}

export { gltfImageProcessor };
96 changes: 7 additions & 89 deletions src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { gltfTexture } from './texture.js';
import { gltfUserInterface } from './user_interface.js';
import { UserCamera } from './user_camera.js';
import { jsToGl, getIsGlb, getIsGltf, getFileNameWithoutExtension, Timer } from './utils.js';
import { GlbParser } from './glbParser';
import { GlbParser } from './glbParser.js';
import { gltfImageProcessor } from './image_processor.js';

class gltfViewer
{
Expand Down Expand Up @@ -174,97 +175,14 @@ class gltfViewer

let assetPromises = gltfLoader.load(gltf, buffers);

let self = this;
const self = this;
const imageProcessor = new gltfImageProcessor();
Promise.all(assetPromises)
.then(() => self.onResize(gltf))
.then(() => self.onGltfLoaded(gltf));
}

isPowerOf2(n)
{
return n && (n & (n - 1)) === 0;
}

onResize(gltf)
{
const imagePromises = [];
if (gltf.images !== undefined)
{
let i;
for (i = 0; i < gltf.images.length; i++)
{
if (gltf.images[i].image.dataRGBE !== undefined ||
this.isPowerOf2(gltf.images[i].image.width) && (gltf.images[i].image.width === gltf.images[i].image.height))
{
// Square image and power of two, so no resize needed.
continue;
}

let doPower = false;

if (gltf.images[i].image.width == gltf.images[i].image.height)
{
// Square image but not power of two. Resize it to power of two.
doPower = true;
}
else
{
// Rectangle image, so not mip-mapped and ...

if ((gltf.images[i].image.width % 2 == 0) && (gltf.images[i].image.height % 2 == 0))
{
// ... with even size, so no resize needed.
continue;
}

// ... with odd size, so resize needed to make even size.
}

const currentImagePromise = new Promise(function(resolve)
{
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');

function nearestPowerOf2(n)
{
return Math.pow(2.0, Math.round(Math.log(n) / Math.log(2.0)));
}

function makeEven(n)
{
if (n % 2 === 1)
{
return n + 1;
}
return n;
}

if (doPower)
{
canvas.width = nearestPowerOf2(gltf.images[i].image.width);
canvas.height = nearestPowerOf2(gltf.images[i].image.height);
}
else
{
canvas.width = makeEven(gltf.images[i].image.width);
canvas.height = makeEven(gltf.images[i].image.height);
}

context.drawImage(gltf.images[i].image, 0, 0, canvas.width, canvas.height);

gltf.images[i].image.src = canvas.toDataURL("image/png");

resolve();
});

imagePromises.push(currentImagePromise);
}
}

return Promise.all(imagePromises);
.then(() => imageProcessor.processImages(gltf))
.then(() => self.startRendering(gltf));
}

onGltfLoaded(gltf)
startRendering(gltf)
{
this.notifyLoadingEnded(gltf.path);

Expand Down