jp2-to-image
Version:
A library for converting JPEG 2000 (JP2) images to another format
105 lines • 4.16 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.JPEG2000NodeConverter = void 0;
const child_process_1 = require("child_process");
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
class JPEG2000NodeConverter {
constructor() {
const platform = process.platform;
let binFileName = 'convert'; // base path
if (platform === 'win32')
binFileName = 'convert-win.exe';
else if (platform === 'darwin')
binFileName = 'convert-mac';
else if (platform === 'linux')
binFileName = 'convert-linux';
else
throw new Error(`Unsupported platform: ${platform}`);
this.binPath = path.resolve(__dirname, 'bin', binFileName);
if (platform !== 'win32') {
fs.ensureFileSync(this.binPath);
fs.chmodSync(this.binPath, 0o755);
}
}
/**
* Handles the conversion process
* @param inputBuffer The input image buffer
* @param format Target format
* @returns Converted image as a buffer
*/
async convertImage(inputBuffer, format = 'png') {
try {
const convertedBuffer = await this._executeJar(inputBuffer, format);
return convertedBuffer;
}
catch (error) {
console.error(`Error converting to ${format}:`, error);
throw error;
}
}
/**
* Executes the conversion binary, sending the image via stdin and receiving the result via stdout.
*
* @param inputBuffer The input image as a Buffer
* @param format The desired output format (e.g., "jpg", "png", "jp2")
* @returns The converted image as a Buffer
*/
_executeJar(inputBuffer, format) {
return new Promise((resolve, reject) => {
const process = (0, child_process_1.spawn)(this.binPath, [format]);
const outputBuffer = [];
let errorOutput = '';
// Capture stdout as Buffer
process.stdout.on('data', data => outputBuffer.push(data));
// Capture stderr
process.stderr.on('data', data => (errorOutput += data.toString()));
process.on('close', code => {
if (code !== 0) {
reject(new Error(`Process failed with code ${code}: ${errorOutput}`));
}
else {
resolve(Buffer.concat(outputBuffer)); // Combine all received chunks
}
});
// Write image buffer to process and close stdin
process.stdin.write(inputBuffer);
process.stdin.end();
});
}
}
exports.JPEG2000NodeConverter = JPEG2000NodeConverter;
//# sourceMappingURL=JPEG2000NodeConverter.js.map