var IMAGE_HEAD_SIGS = {
GIF: [0x47, 0x49, 0x46],
PNG: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a],
JPG: [0xff, 0xd8, 0xff],
BMP: [0x42, 0x4d],
};
function readUint32BE(bytes, start) {
var uarr = new Uint32Array(1);
uarr[0] = (bytes[start + 0] & 0xff) << 24;
uarr[0] = uarr[0] | ((bytes[start + 1] & 0xff) << 16);
uarr[0] = uarr[0] | ((bytes[start + 2] & 0xff) << 8);
uarr[0] = uarr[0] | (bytes[start + 3] & 0xff);
return uarr[0];
}
function readUint16BE(bytes, start) {
var uarr = new Uint32Array(1);
uarr[0] = (bytes[start + 0] & 0xff) << 8;
uarr[0] = uarr[0] | (bytes[start + 1] & 0xff);
return uarr[0];
}
function readUint32LE(bytes, start) {
var uarr = new Uint32Array(1);
uarr[0] = (bytes[start + 3] & 0xff) << 24;
uarr[0] = uarr[0] | ((bytes[start + 2] & 0xff) << 16);
uarr[0] = uarr[0] | ((bytes[start + 1] & 0xff) << 8);
uarr[0] = uarr[0] | (bytes[start + 0] & 0xff);
return uarr[0];
}
function readUint16LE(bytes, start) {
var uarr = new Uint32Array(1);
uarr[0] = (bytes[start + 1] & 0xff) << 8;
uarr[0] = uarr[0] | (bytes[start + 0] & 0xff);
return uarr[0];
}
function ReadPNG(bytes) {
let h = new Uint8Array(bytes.slice(0, 8));
if (h.toString() === IMAGE_HEAD_SIGS.PNG.toString()) {
console.log("ReadPNG2");
let width = readUint32BE(bytes, 16);
let height = readUint32BE(bytes, 20);
return {
width,
height,
};
}
}
function ReadJPG(bytes) {
let h = new Uint8Array(bytes.slice(0, 3));
console.log("ReadPNG3", h);
console.log("ReadPNG3", h.toString());
console.log("ReadPNG3", IMAGE_HEAD_SIGS.JPG.toString());
if (h.toString() === IMAGE_HEAD_SIGS.JPG.toString()) {
const M_SOF0 = 0xc0;
const M_SOF1 = 0xc1;
const M_SOF2 = 0xc2;
const M_SOF3 = 0xc3;
const M_SOF5 = 0xc5;
const M_SOF6 = 0xc6;
const M_SOF7 = 0xc7;
const M_SOF9 = 0xc9;
const M_SOF10 = 0xca;
const M_SOF11 = 0xcb;
const M_SOF13 = 0xcd;
const M_SOF14 = 0xce;
const M_SOF15 = 0xcf;
for (let i = 0; i < bytes.length; i++) {
if (bytes[i] === 0xff) {
switch (bytes[i + 1]) {
case M_SOF0:
case M_SOF1:
case M_SOF2:
case M_SOF3:
case M_SOF5:
case M_SOF6:
case M_SOF7:
case M_SOF9:
case M_SOF10:
case M_SOF11:
case M_SOF13:
case M_SOF14:
case M_SOF15: {
let width = readUint16BE(bytes, i + 7);
let height = readUint16BE(bytes, i + 5);
return {
width,
height,
};
}
default:
break;
}
}
}
}
}
function ReadGIF(bytes) {
if (bytes.slice(0, 3).toString() === IMAGE_HEAD_SIGS.GIF.toString()) {
let width = readUint16LE(bytes, 6);
let height = readUint16LE(bytes, 8);
return {
width,
height,
};
}
}
function ReadBMP(bytes) {
let h = new Uint8Array(bytes.slice(0, 2));
if (h.toString() === IMAGE_HEAD_SIGS.BMP.toString()) {
let height = readUint16LE(bytes, 22);
let width = readUint16LE(bytes, 18);
return {
width,
height,
};
}
}
function base64ToBytes(base64) {
let baseSplit = base64.split(",");
let strArray = window.atob(baseSplit[1]);
let buffer = new ArrayBuffer(strArray.length);
let bytes = new Uint8Array(buffer);
for (let i = 0; i < strArray.length; i++) {
bytes[i] = strArray.charCodeAt(i);
}
return bytes;
}
function ReadPNGBase64(base64) {
return ReadPNG(base64ToBytes(base64));
}
function ReadJPGBase64(base64) {
return ReadJPG(base64ToBytes(base64));
}
function ReadGIFBase64(base64) {
return ReadGIF(base64ToBytes(base64));
}
function ReadBMPBase64(base64) {
return ReadBMP(base64ToBytes(base64));
}
function ReadBase64Dimension(base64, extType) {
switch (extType) {
case "png":
return ReadPNGBase64(base64);
case "jpg":
case "jpeg":
return ReadJPGBase64(base64);
case "gif":
return ReadGIFBase64(base64);
case "bmp":
return ReadBMPBase64(base64);
default:
return undefined;
}
}
class cpsImg {
constructor() {
this.data = {
png: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a].toString(),
jpg: [0xff, 0xd8, 0xff, 0xe0].toString(),
git: [0x47, 0x49, 0x46].toString(),
bmp: [0x42, 0x4d].toString(),
};
}
getExt(filename, callback) {
fs.readFile(filename, (err, bytes) => {
if (err) {
console.log(err);
} else {
let h = new Uint8Array(bytes.slice(0, 8));
for (let each of this.data) {
if (each.head == h.slice(0, each.byte)) {
console.log("当前的格式识别为:" + each.ext);
}
}
}
});
}
pngSize(bytes) {
let h = new Uint8Array(bytes.slice(0, 8));
console.log(h);
console.log(h.toString());
if (h.toString() === this.data.png) {
let width = readUint32BE(bytes, 16);
let height = readUint32BE(bytes, 20);
return {
type: "png",
width,
height,
};
}
}
}
if (require.main === module) {
const fs = require("fs");
let tar = fs.readFileSync("D:/User/桌面/ccvbb.png");
res = ReadPNG(tar);
console.log("res: ", res);
}
module.exports = {
ReadPNG,
ReadJPG,
ReadGIF,
ReadBMP,
ReadPNGBase64,
ReadJPGBase64,
ReadGIFBase64,
ReadBMPBase64,
ReadBase64Dimension,
};