Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for png itxt chunks #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/file-parsers/png.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class PngFileParser extends FileParserBase {
await this.readSegments(this.metaChunks)
this.findIhdr()
this.parseTextChunks()
await this.parseItxtChunks().catch(this.catchError)
await this.findExif().catch(this.catchError)
await this.findXmp().catch(this.catchError)
await this.findIcc().catch(this.catchError)
Expand Down Expand Up @@ -80,6 +81,65 @@ export class PngFileParser extends FileParserBase {
}
}

async parseItxtChunks() {
let itxtChunks = this.metaChunks.filter(info => info.type === ITXT)
for (let seg of itxtChunks) {
let prefix = seg.chunk.getString(0, PNG_XMP_PREFIX.length)
// exclude xmp data here
if (prefix !== PNG_XMP_PREFIX) {
let currPos = 0
let nextPos

const findEnd = (chunk, start) => {
let i = start
for (; i < chunk.byteLength; i++) {
if (chunk.getUint8(i) === 0) {
break
}
}
return i
}

nextPos = findEnd(seg.chunk, currPos)
const keyword = seg.chunk.getString(currPos, nextPos - currPos)
currPos = nextPos + 1

const compressed = seg.chunk.getUint8(nextPos + 1) === 1
const compressionMethod = seg.chunk.getUint8(nextPos + 2)
currPos = currPos + 2

nextPos = findEnd(seg.chunk, currPos)
const language = seg.chunk.getString(currPos, nextPos - currPos)
currPos = nextPos + 1

nextPos = findEnd(seg.chunk, currPos)
const translatedKeyword = seg.chunk.getString(currPos, nextPos - currPos)
currPos = nextPos + 1

nextPos = findEnd(seg.chunk, currPos)
let text
if (compressed && platform.node) {
let zlib = await zlibPromise
let dataChunk = seg.chunk.getUint8Array(currPos, nextPos - currPos)
dataChunk = zlib.inflateSync(dataChunk)
text = dataChunk.toString()
} else {
text = seg.chunk.getString(currPos, nextPos - currPos)
}
// here nextPos should equal seg.chunk.byteLength

// console.log('compressed', compressed)
// console.log('compressionMethod', compressionMethod)
// console.log('keyword', keyword)
// console.log('language', language)
// console.log('translatedKeyword', translatedKeyword)
// console.log('text', text)

this.injectKeyValToIhdr(keyword, text)
}
}
}

injectKeyValToIhdr(key, val) {
let parser = this.parsers.ihdr
if (parser) parser.raw.set(key, val)
Expand Down
Binary file added test/fixtures/png/iTXt-compressed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/png/iTXt-uncompressed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions test/formats/png.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ describe('PNG File format', () => {
DeviceManufacturer: 'Google',
ProfileConnectionSpace: 'XYZ',
})

testImageFull('png/iTXt-uncompressed.png', {
ImageWidth: 2400,
ImageHeight: 2000,
BitDepth: 8,
// itxt chunk
Comment: 'Created with GIMP',
})

testImageFull('png/iTXt-compressed.png', {
ImageWidth: 10,
ImageHeight: 11,
BitDepth: 8,
// itxt chunks
Title: 'PNG',
Author: 'La plume de ma tante', // uncompressed
Warning: 'Es is verboten, um diese Datei in das GIF-Bildformat\n'
+ 'umzuwandeln. Sie sind gevarnt worden.', // compressed
})
}

})
Expand Down