Re-enabled stubs for PNG analyser and ICO maker, Implemented Data utils, file utils and PNG parser, Still testing
Update png-analyser.yml, png-chunk-analyser.yml, and 17 more files...
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
|
||||
.ico-maker-advanced {
|
||||
display: none;
|
||||
}
|
||||
|
||||
input[type="checkbox"]#ico-maker-enable-expert-mode:checked ~ .ico-maker-advanced {
|
||||
display: block;
|
||||
}
|
||||
|
@@ -0,0 +1,27 @@
|
||||
|
||||
import {initCore} from "../../js/nibblepoker-core.mjs"
|
||||
import {parsePngFile} from "../../libs/png-utils.mjs";
|
||||
|
||||
{
|
||||
initCore();
|
||||
|
||||
const toolId = "png-analyser";
|
||||
|
||||
const eFileInput = document.getElementById(`${toolId}-test-input`);
|
||||
|
||||
window.onload = function () {
|
||||
|
||||
eFileInput.addEventListener('change', function(e) {
|
||||
let files = e.target.files;
|
||||
|
||||
console.log(files);
|
||||
|
||||
parsePngFile(files[0]).then(pngFile => {
|
||||
console.log(pngFile);
|
||||
|
||||
console.log(pngFile.getImageHeaderChunk().getWidth());
|
||||
console.log(pngFile.getImageHeaderChunk().getHeight());
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
52
static/resources/NibblePoker/libs/data-utils.mjs
Normal file
52
static/resources/NibblePoker/libs/data-utils.mjs
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
/**
|
||||
* Compares two UintXArray and checks if their content is the same.
|
||||
* @param array1 {Uint8Array|Uint16Array|Uint32Array}
|
||||
* @param array2 {Uint8Array|Uint16Array|Uint32Array}
|
||||
* @return {boolean}
|
||||
*/
|
||||
export function areUintArraysEqual(array1, array2) {
|
||||
if ((typeof array1) !== (typeof array2)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (array1.length !== array2.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 0; i < array1.length; i++) {
|
||||
if (array1[i] !== array2[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Peeks a UInt32 from the given data at the given offset in Big-Endian.
|
||||
* @param data {Uint8Array} - Data to read from.
|
||||
* @param offset {number} - Offset to read from in the given `data`.
|
||||
* @return {number} The peeked number.
|
||||
* @throws RangeError If the given offset is too close or over the end of the data.
|
||||
*/
|
||||
export function peekUInt32BE(data, offset = 0) {
|
||||
if(offset + 4 > data.length) {
|
||||
throw new RangeError(`Offset is too far into the given data ! (${offset} & ${data.length})`);
|
||||
}
|
||||
return new DataView(data.buffer, offset, 4).getUint32(0, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Peeks a UInt32 from the given data at the given offset in Little-Endian.
|
||||
* @param data {Uint8Array} - Data to read from.
|
||||
* @param offset {number} - Offset to read from in the given `data`.
|
||||
* @return {number} The peeked number.
|
||||
* @throws RangeError If the given offset is too close or over the end of the data.
|
||||
*/
|
||||
export function peekUInt32LE(data, offset = 0) {
|
||||
if(offset + 4 > data.length) {
|
||||
throw new RangeError(`Offset is too far into the given data ! (${offset} & ${data.length})`);
|
||||
}
|
||||
return new DataView(data.buffer, offset, 4).getUint32(0, true);
|
||||
}
|
28
static/resources/NibblePoker/libs/file-utils.mjs
Normal file
28
static/resources/NibblePoker/libs/file-utils.mjs
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
/**
|
||||
* Reads a file and returns its content as a string.
|
||||
* @param {File} file - The file to read.
|
||||
* @returns {Promise<string>} A promise that resolves with the file content as a string.
|
||||
*/
|
||||
export function loadFileAsText(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(reader.result);
|
||||
reader.onerror = reject;
|
||||
reader.readAsText(file);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a file and returns its content as a `Uint8Array`.
|
||||
* @param {File} file - The file to read.
|
||||
* @returns {Promise<Uint8Array>} A promise that resolves with the file content as a byte buffer.
|
||||
*/
|
||||
export function loadFileAsUint8Array(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(new Uint8Array(reader.result));
|
||||
reader.onerror = reject;
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
}
|
233
static/resources/NibblePoker/libs/png-utils.mjs
Normal file
233
static/resources/NibblePoker/libs/png-utils.mjs
Normal file
@@ -0,0 +1,233 @@
|
||||
|
||||
//import {__crc32, crc32b, _crc32b, decimalToHexString} from "./crc32.mjs";
|
||||
import {areUintArraysEqual, peekUInt32BE, peekUInt32LE} from "./data-utils.mjs"
|
||||
import {loadFileAsUint8Array} from "./file-utils.mjs";
|
||||
|
||||
/**
|
||||
* Parent class extended by all PNG-related errors.
|
||||
*/
|
||||
export class PngError extends Error {}
|
||||
|
||||
export class PngInvalidFileHeaderError extends PngError {}
|
||||
|
||||
export class PngInvalidStructureError extends PngError {}
|
||||
|
||||
export class PngInvalidChunkNameError extends PngError {}
|
||||
|
||||
export class PngInvalidImageHeaderError extends PngError {}
|
||||
|
||||
export const PngFileHeader = new Uint8Array([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
|
||||
|
||||
export class PngChunk {
|
||||
/** @type {string} */
|
||||
type;
|
||||
|
||||
/** @type {Uint8Array} */
|
||||
data;
|
||||
|
||||
/**
|
||||
* @param type {string}
|
||||
* @param data {Uint8Array}
|
||||
* @param expectedChecksum {Uint8Array|null}
|
||||
* @throws PngInvalidChunkNameError If the given chunk name isn't a valid chunk name.
|
||||
*/
|
||||
constructor(type, data, expectedChecksum) {
|
||||
this.type = type;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
getChecksum() {
|
||||
throw new Error("This function isn't implemented yet !");
|
||||
}
|
||||
}
|
||||
|
||||
class PngImageHeaderChunk extends PngChunk {
|
||||
/**
|
||||
* @param type {string}
|
||||
* @param data {Uint8Array}
|
||||
* @param expectedChecksum {Uint8Array|null}
|
||||
* @throws PngInvalidChunkNameError If the given chunk name isn't a valid chunk name.
|
||||
* @throws PngInvalidImageHeaderError If the given chunk's size isn't exactly 13 bytes.
|
||||
*/
|
||||
constructor(type, data, expectedChecksum) {
|
||||
super(type, data, expectedChecksum);
|
||||
|
||||
if(this.data.length !== 13) {
|
||||
throw new PngInvalidImageHeaderError(`Invalid IHDR chunk size, got ${this.data.length} instead of 13 !`);
|
||||
}
|
||||
}
|
||||
|
||||
getWidth() {
|
||||
return peekUInt32BE(this.data, 0);
|
||||
}
|
||||
|
||||
getHeight() {
|
||||
return peekUInt32BE(this.data, 4);
|
||||
}
|
||||
|
||||
getBitDepth() {
|
||||
return this.data[8];
|
||||
}
|
||||
|
||||
getColorType() {
|
||||
return this.data[9];
|
||||
}
|
||||
|
||||
getCompressionMethod() {
|
||||
return this.data[10];
|
||||
}
|
||||
|
||||
getFilterMethod() {
|
||||
return this.data[11];
|
||||
}
|
||||
|
||||
getInterlaceMethod() {
|
||||
return this.data[12];
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a `PngChunk` to a `PngImageHeaderChunk`.
|
||||
* @param pngChunk {PngChunk}
|
||||
* @return {PngImageHeaderChunk}
|
||||
*/
|
||||
static fromPngChunk(pngChunk) {
|
||||
return new PngImageHeaderChunk(pngChunk.type, pngChunk.data, null);
|
||||
}
|
||||
}
|
||||
|
||||
export class PngFile {
|
||||
/** @type {File|null} */
|
||||
originalFile;
|
||||
|
||||
/**
|
||||
* Optional trailing data located after the 'IEND' chunk.
|
||||
* @type {Uint8Array|null}
|
||||
*/
|
||||
trailingData;
|
||||
|
||||
/** @type {PngChunk[]} */
|
||||
chunks;
|
||||
|
||||
/**
|
||||
* @param file {File|null}
|
||||
* @param fileData {Uint8Array|null}
|
||||
* @throws PngInvalidFileHeaderError If the `fileData` is provided and doesn't contain a valid PNG file header.
|
||||
*/
|
||||
constructor(file = null, fileData = null) {
|
||||
this.originalFile = file;
|
||||
|
||||
this.chunks = [];
|
||||
|
||||
// Parsing the data
|
||||
if(fileData !== null) {
|
||||
this.#validateFileHeader(fileData);
|
||||
this.#parseChunks(fileData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param originalFileData {Uint8Array}
|
||||
*/
|
||||
#validateFileHeader = (originalFileData) => {
|
||||
if(originalFileData.length < 8) {
|
||||
throw new PngInvalidFileHeaderError(
|
||||
`The file header's length is smaller than the required 8 ! (Got: ${originalFileData.length})`);
|
||||
}
|
||||
|
||||
if(!areUintArraysEqual(originalFileData.slice(0, 8), PngFileHeader)) {
|
||||
throw new PngInvalidFileHeaderError(
|
||||
"The file header didn't have the expected data !\n" +
|
||||
`Expected: ${PngFileHeader}\\n` +
|
||||
`Got: ${originalFileData.slice(0, 8)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param originalFileData {Uint8Array}
|
||||
* @throws TypeError If a chunk's type couldn't be parsed.
|
||||
*/
|
||||
#parseChunks = (originalFileData) => {
|
||||
let currentOffset = PngFileHeader.length;
|
||||
|
||||
while(currentOffset < originalFileData.length) {
|
||||
// Checking if we haven't encountered an IEND, and we encountered a truncated file or trash data.
|
||||
if(currentOffset + 12 > originalFileData.length) {
|
||||
throw new PngInvalidStructureError("Unable to parse more chunks and no 'IEND' was encountered !");
|
||||
}
|
||||
|
||||
const chunkLength = peekUInt32BE(originalFileData.slice(currentOffset, currentOffset + 4));
|
||||
const chunkType = new TextDecoder().decode(originalFileData.slice(currentOffset + 4, currentOffset + 8));
|
||||
|
||||
// Checking if we have enough data left to read for the chunk's data.
|
||||
if(currentOffset + 12 + chunkLength > originalFileData.length) {
|
||||
throw new PngInvalidStructureError("Not enough data left to read the chunk !")
|
||||
}
|
||||
|
||||
let chunkChecksum = originalFileData.slice(
|
||||
currentOffset + 8 + chunkLength, currentOffset + 8 + chunkLength + 4);
|
||||
|
||||
this.chunks.push(
|
||||
new PngChunk(
|
||||
chunkType,
|
||||
(chunkLength === 0) ?
|
||||
new Uint8Array(0) :
|
||||
originalFileData.slice(currentOffset + 8, currentOffset + 8 + chunkLength),
|
||||
chunkChecksum,
|
||||
//originalFileData.slice(currentOffset + 4, currentOffset + 8)
|
||||
)
|
||||
);
|
||||
|
||||
if(chunkType === "IEND") {
|
||||
break;
|
||||
}
|
||||
|
||||
currentOffset += 12 + chunkLength;
|
||||
}
|
||||
|
||||
// Handling trailing data
|
||||
if(currentOffset !== originalFileData.length) {
|
||||
this.trailingData = originalFileData.slice(currentOffset, originalFileData.length);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to retrieve a chunk via its type.
|
||||
* @param chunkName {string} The desired chunk's type.
|
||||
* @return {PngChunk|null}
|
||||
*/
|
||||
getChunkByType(chunkName) {
|
||||
for(let iChunk in this.chunks) {
|
||||
if(this.chunks[iChunk].type === chunkName) {
|
||||
return this.chunks[iChunk];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
hasEndChunk() {
|
||||
return this.getChunkByType("IEND") !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {PngImageHeaderChunk|null}
|
||||
*/
|
||||
getImageHeaderChunk() {
|
||||
let desiredChunk = this.getChunkByType("IHDR");
|
||||
if(desiredChunk !== null) {
|
||||
desiredChunk = PngImageHeaderChunk.fromPngChunk(desiredChunk);
|
||||
}
|
||||
return desiredChunk;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads and parses a given PNG file.
|
||||
* @param {File} file - The PNG file to process.
|
||||
* @returns {Promise<PngFile>} A promise that resolves with parsed PNG file.
|
||||
*/
|
||||
export function parsePngFile(file) {
|
||||
return loadFileAsUint8Array(file).then(byteBuffer => {
|
||||
return new PngFile(file, byteBuffer);
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user