Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function parse(path) {
const data = bmp.decode(jetpack.read(path, 'buffer'));
const wrapX = true;
const wrapY = false;
const indexMap = {};
const pixels = [];
const colorNrs = [];
let colorNr = 0;
for (let i = 0; i < data.width * data.height; i += 1) {
// Todo: color order can be RGB or BGR - need to fix so it always is RGB
const colorName = data.data[i * 4] + ',' + data.data[(i * 4) + 1] + ',' + data.data[(i * 4) + 2];
if (!indexMap[colorName]) {
indexMap[colorName] = {
nr: colorNr,
color_name: colorName,
that.bitmap = {
data: new Buffer(data.data),
width: data.width,
height: data.height
};
return cb.call(that, null, that);
});
break;
case Jimp.MIME_JPEG:
this.bitmap = JPEG.decode(data);
exifRotate(this, data); // EXIF data
return cb.call(this, null, this);
case Jimp.MIME_BMP:
this.bitmap = BMP.decode(data);
return cb.call(this, null, this);
default:
return throwError.call(this, "Unsupported MIME type: " + mime, cb);
}
}
module.exports = (TessModule, api, image) => {
const buf = Buffer.from(Array.from({ ...image, length: Object.keys(image).length }));
const type = fileType(buf);
let bytesPerPixel = 0;
let data = null;
let pix = null;
let w = 0;
let h = 0;
/*
* Although leptonica should support reading bmp, there is a bug of "compressed BMP files".
* As there is no solution, we need to use bmp-js for now.
* @see https://groups.google.com/forum/#!topic/tesseract-ocr/4mPD9zTxdxE
*/
if (type && type.mime === 'image/bmp') {
const bmpBuf = bmp.decode(buf);
data = TessModule._malloc(bmpBuf.data.length * Uint8Array.BYTES_PER_ELEMENT);
TessModule.HEAPU8.set(bmpBuf.data, data);
w = bmpBuf.width;
h = bmpBuf.height;
bytesPerPixel = 4;
} else {
const ptr = TessModule._malloc(buf.length * Uint8Array.BYTES_PER_ELEMENT);
TessModule.HEAPU8.set(buf, ptr);
pix = TessModule._pixReadMem(ptr, buf.length);
if (TessModule.getValue(pix + (7 * 4), 'i32') === 0) {
/*
* Set a yres default value to prevent warning from tesseract
* See kMinCredibleResolution in tesseract/src/ccstruct/publictypes.h
*/
TessModule.setValue(pix + (7 * 4), 300, 'i32');
}
const decode = data => fromAGBR(BMP.decode(data));
const encode = image => BMP.encode(toAGBR(image)).data;