Skip to content

Commit

Permalink
feat: Added the ability to iterate over ifd entries
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor-pelykh committed Mar 12, 2023
1 parent 341d99b commit d3d213c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
29 changes: 11 additions & 18 deletions src/exif/exif-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ export class ExifData extends IfdContainer {
let offset = dataOffset;
const stripOffsetTag = ExifTagNameToID.get('StripOffsets');
out.writeUint16(ifd.size);
for (const tag of ifd.keys) {
const value = ifd.getValue(tag)!;

for (const [tag, value] of ifd.entries) {
// Special-case StripOffsets, used by TIFF, that if it points to
// Undefined value type, then its storing the image data and should
// be translated to the StripOffsets long type.
Expand Down Expand Up @@ -257,11 +255,10 @@ export class ExifData extends IfdContainer {
}

// storage for sub-ifd blocks
for (const subName of ifd.sub.keys) {
const subIfd = ifd.sub.get(subName);
for (const [subName, subDir] of ifd.sub.entries) {
offsets.set(subName, dataOffset);
let subSize = 2 + 12 * subIfd.size;
for (const value of subIfd.values) {
let subSize = 2 + 12 * subDir.size;
for (const value of subDir.values) {
const dataSize = value.dataSize;
if (dataSize > 4) {
subSize += dataSize;
Expand Down Expand Up @@ -301,12 +298,11 @@ export class ExifData extends IfdContainer {

this.writeDirectoryLargeValues(out, ifd);

for (const subName of ifd.sub.keys) {
const subIfd = ifd.sub.get(subName);
for (const [subName, subDir] of ifd.sub.entries) {
const subOffset = offsets.get(subName)!;
const dataOffset = subOffset + 2 + 12 * subIfd.size;
this.writeDirectory(out, subIfd, dataOffset);
this.writeDirectoryLargeValues(out, subIfd);
const dataOffset = subOffset + 2 + 12 * subDir.size;
this.writeDirectory(out, subDir, dataOffset);
this.writeDirectoryLargeValues(out, subDir);
}
}

Expand Down Expand Up @@ -409,19 +405,16 @@ export class ExifData extends IfdContainer {
let s = '';
for (const [name, directory] of this.directories) {
s += `${name}\n`;
for (const tag of directory.keys) {
const value = directory.getValue(tag);
for (const [tag, value] of directory.entries) {
if (value === undefined) {
s += `\t${this.getTagName(tag)}\n`;
} else {
s += `\t${this.getTagName(tag)}: ${value.toString()}\n`;
}
}
for (const subName of directory.sub.keys) {
for (const [subName, subDir] of directory.sub.entries) {
s += `${subName}\n`;
const subDirectory = directory.sub.get(subName);
for (const tag of subDirectory.keys) {
const value = subDirectory.getValue(tag);
for (const [tag, value] of subDir.entries) {
if (value === undefined) {
s += `\t${this.getTagName(tag)}\n`;
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/exif/ifd-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export class IfdContainer {
return this.directories.values();
}

public get entries(): IterableIterator<[string, IfdDirectory]> {
return this.directories.entries();
}

public get size(): number {
return this.directories.size;
}
Expand Down
11 changes: 7 additions & 4 deletions src/exif/ifd-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export class IfdDirectory {
return this._data.values();
}

public get entries(): IterableIterator<[number, IfdValue]> {
return this._data.entries();
}

public get size(): number {
return this._data.size;
}
Expand Down Expand Up @@ -251,10 +255,9 @@ export class IfdDirectory {
}
}
// storage for sub-ifd blocks
for (const subName of this.sub.keys) {
const subIfd = this.sub.get(subName);
let subSize = 2 + 12 * subIfd.size;
for (const value of subIfd.values) {
for (const [_subName, subDir] of this.sub.entries) {
let subSize = 2 + 12 * subDir.size;
for (const value of subDir.values) {
const dataSize = value.dataSize;
if (dataSize > 4) {
subSize += dataSize;
Expand Down

0 comments on commit d3d213c

Please sign in to comment.