obj2gltf/lib/loadImage.js

108 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-03-13 15:28:51 -04:00
'use strict';
var Cesium = require('cesium');
2017-03-21 14:37:52 -04:00
var fsExtra = require('fs-extra');
2015-10-16 17:32:23 -04:00
var path = require('path');
var PNG = require('pngjs').PNG;
2017-03-13 15:28:51 -04:00
var Promise = require('bluebird');
2015-10-16 17:32:23 -04:00
2017-04-10 17:57:56 -04:00
var defined = Cesium.defined;
2017-03-13 15:28:51 -04:00
var WebGLConstants = Cesium.WebGLConstants;
2016-06-09 13:33:08 -04:00
module.exports = loadImage;
2015-10-16 17:32:23 -04:00
2017-03-13 15:28:51 -04:00
/**
* Load an image file and get information about it.
*
* @param {String} imagePath Path to the image file.
2017-04-10 17:57:56 -04:00
* @param {Object} options An object with the following properties:
* @param {Boolean} options.checkTransparency Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel.
2017-03-13 15:28:51 -04:00
* @returns {Promise} A promise resolving to the image information, or undefined if the file doesn't exist.
*
* @private
*/
2017-03-17 16:05:51 -04:00
function loadImage(imagePath, options) {
return fsExtra.readFile(imagePath)
2017-03-13 15:28:51 -04:00
.then(function(data) {
2017-04-10 17:57:56 -04:00
var extension = path.extname(imagePath).toLowerCase();
2017-03-13 15:28:51 -04:00
var info = {
transparent : false,
format : getFormat(3),
source : data,
extension : extension
2017-03-13 15:28:51 -04:00
};
if (extension === '.png') {
2017-04-10 17:57:56 -04:00
return getPngInfo(data, info, options);
2017-03-13 15:28:51 -04:00
}
return info;
});
}
2017-04-10 17:57:56 -04:00
function getPngInfo(data, info, options) {
// Color type is encoded in the 25th bit of the png
var colorType = data[25];
var channels = getChannels(colorType);
info.format = getFormat(channels);
if (channels === 4) {
if (options.checkTransparency) {
return isTransparent(data)
.then(function(transparent) {
info.transparent = transparent;
return info;
});
}
}
return info;
}
function isTransparent(data) {
2017-03-21 14:37:52 -04:00
return new Promise(function(resolve, reject) {
new PNG().parse(data, function(error, data) {
2017-04-10 17:57:56 -04:00
if (defined(error)) {
2017-03-21 14:37:52 -04:00
reject(error);
2017-03-21 14:51:48 -04:00
return;
2017-03-21 14:37:52 -04:00
}
var pixels = data.data;
var pixelsLength = data.width * data.height;
for (var i = 0; i < pixelsLength; ++i) {
if (pixels[i * 4 + 3] < 255) {
resolve(true);
return;
}
}
resolve(false);
});
});
}
2015-10-16 17:32:23 -04:00
function getChannels(colorType) {
switch (colorType) {
case 0: // greyscale
return 1;
case 2: // RGB
return 3;
case 4: // greyscale + alpha
return 2;
case 6: // RGB + alpha
return 4;
default:
return 3;
}
}
2017-03-13 15:28:51 -04:00
function getFormat(channels) {
switch (channels) {
case 1:
return WebGLConstants.LUMINANCE;
2017-03-13 15:28:51 -04:00
case 2:
return WebGLConstants.LUMINANCE_ALPHA;
case 3:
return WebGLConstants.RGB;
case 4:
return WebGLConstants.RGBA;
}
2015-10-16 17:32:23 -04:00
}