Skip to content

Commit

Permalink
test: Tests for new approach of managing EXIF data
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor-pelykh committed Dec 13, 2022
1 parent c0cee43 commit 78d1131
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
76 changes: 76 additions & 0 deletions test/exif.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/** @format */

import {
ExifData,
ExifLongValue,
ExifRationalValue,
ExifShortValue,
ExifSLongValue,
ExifSRationalValue,
ExifSShortValue,
InputBuffer,
OutputBuffer,
Rational,
} from '../src';

describe('Exif', () => {
test('write/read', () => {
const exif = new ExifData();
exif.imageIfd.setValue(0, new ExifShortValue(124));
exif.imageIfd.setValue(1, new ExifLongValue(52141));
exif.imageIfd.setValue(2, new ExifSShortValue(-42));
exif.imageIfd.setValue(3, new ExifSLongValue(-42141));
exif.imageIfd.setValue(4, new ExifRationalValue(new Rational(72, 1)));
exif.imageIfd.setValue(5, new ExifSRationalValue(new Rational(-50, 5)));
const exifSubDir = exif.imageIfd.sub.get('exif');
exifSubDir.setValue(0, new ExifShortValue(124));
exifSubDir.setValue(1, new ExifLongValue(52141));
exifSubDir.setValue(2, new ExifSShortValue(-42));
exifSubDir.setValue(3, new ExifSLongValue(-42141));
exifSubDir.setValue(4, new ExifRationalValue(new Rational(72, 1)));
exifSubDir.setValue(5, new ExifSRationalValue(new Rational(-50, 5)));
exif.thumbnailIfd.setValue(0, new ExifShortValue(124));
exif.thumbnailIfd.setValue(1, new ExifLongValue(52141));
exif.thumbnailIfd.setValue(2, new ExifSShortValue(-42));
exif.thumbnailIfd.setValue(3, new ExifSLongValue(-42141));
exif.thumbnailIfd.setValue(4, new ExifRationalValue(new Rational(72, 1)));
exif.thumbnailIfd.setValue(5, new ExifSRationalValue(new Rational(-50, 5)));
const out = new OutputBuffer();
exif.write(out);
const exif2 = new ExifData();
const input = new InputBuffer({
buffer: out.getBytes(),
});
exif2.read(input);

expect(exif2.imageIfd.size).toBe(exif.imageIfd.size);
const exifValues = Array.from(exif.imageIfd.values);
const exif2Values = Array.from(exif2.imageIfd.values);
for (let i = 0; i < exifValues.length; i++) {
const isEqual = exifValues[i].equalsTo(exif2Values[i]);
expect(isEqual).toBe(true);
}

expect(exif2.imageIfd.sub.size).toBe(1);

const exif2FirstKey = exif2.imageIfd.sub.keys.next().value;
expect(exif2FirstKey).toBe('exif');

const exif2SubDir = exif2.imageIfd.sub.get('exif');

const exifSubValues = Array.from(exifSubDir.values);
const exif2SubValues = Array.from(exif2SubDir.values);
for (let i = 0; i < exifSubValues.length; ++i) {
const isEqual = exifSubValues[i].equalsTo(exif2SubValues[i]);
expect(isEqual).toBe(true);
}

expect(exif2.thumbnailIfd.size).toBe(exif.thumbnailIfd.size);
const exifThumbValues = Array.from(exif.thumbnailIfd.values);
const exif2ThumbValues = Array.from(exif2.thumbnailIfd.values);
for (let i = 0; i < exifThumbValues.length; ++i) {
const isEqual = exifThumbValues[i].equalsTo(exif2ThumbValues[i]);
expect(isEqual).toBe(true);
}
});
});
27 changes: 27 additions & 0 deletions test/jpeg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ describe('JPEG', () => {
}
});

test('exif', () => {
const input = TestHelpers.readFromFile(
TestFolder.res,
TestSection.jpeg,
'big_buck_bunny.jpg'
);
const image = decodeJpg(input)!;
image.exifData.imageIfd.setValue('XResolution', [300, 1]);
image.exifData.imageIfd.setValue('YResolution', [300, 1]);
const jpg = encodeJpg(image);
const image2 = decodeJpg(jpg)!;

const imageResolutionX = image.exifData.imageIfd.getValue('XResolution');
expect(imageResolutionX).toBeDefined();
const image2ResolutionX = image2.exifData.imageIfd.getValue('XResolution');
expect(image2ResolutionX).toBeDefined();
const isResXEquals = imageResolutionX!.equalsTo(image2ResolutionX!);
expect(isResXEquals).toBe(true);

const imageResolutionY = image.exifData.imageIfd.getValue('YResolution');
expect(imageResolutionY).toBeDefined();
const image2ResolutionY = image2.exifData.imageIfd.getValue('YResolution');
expect(image2ResolutionY).toBeDefined();
const isResYEquals = imageResolutionY!.equalsTo(image2ResolutionY!);
expect(isResYEquals).toBe(true);
});

const resFiles = TestHelpers.listFiles(
TestFolder.res,
TestSection.jpeg,
Expand Down

0 comments on commit 78d1131

Please sign in to comment.