image-in-browser
Version:
Package for encoding / decoding images, transforming images, applying filters, drawing primitives on images on the client side (no need for server Node.js)
49 lines • 1.77 kB
JavaScript
import { OutputBuffer } from '../common/output-buffer.js';
export class TgaEncoder {
constructor() {
this._supportsAnimation = false;
}
get supportsAnimation() {
return this._supportsAnimation;
}
encode(opt) {
var _a, _b;
const image = opt.image;
const out = new OutputBuffer({
bigEndian: true,
});
const header = new Uint8Array(18);
header.fill(0);
header[2] = 2;
header[12] = image.width & 0xff;
header[13] = (image.width >>> 8) & 0xff;
header[14] = image.height & 0xff;
header[15] = (image.height >>> 8) & 0xff;
const nc = (_b = (_a = image.palette) === null || _a === void 0 ? void 0 : _a.numChannels) !== null && _b !== void 0 ? _b : image.numChannels;
header[16] = nc === 3 ? 24 : 32;
out.writeBytes(header);
if (nc === 4) {
for (let y = image.height - 1; y >= 0; --y) {
for (let x = 0; x < image.width; ++x) {
const c = image.getPixel(x, y);
out.writeByte(Math.trunc(c.b));
out.writeByte(Math.trunc(c.g));
out.writeByte(Math.trunc(c.r));
out.writeByte(Math.trunc(c.a));
}
}
}
else {
for (let y = image.height - 1; y >= 0; --y) {
for (let x = 0; x < image.width; ++x) {
const c = image.getPixel(x, y);
out.writeByte(Math.trunc(c.b));
out.writeByte(Math.trunc(c.g));
out.writeByte(Math.trunc(c.r));
}
}
}
return out.getBytes();
}
}
//# sourceMappingURL=tga-encoder.js.map