-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
While the algorithm to getNextCode was rewritten inspired by a java code (and looks much better and faster) the main difference with the previous implementation is this return 257 in case of OutOfBounds. https://github.com/sugark/Tiffus/blob/master/src/org/eclipse/swt/internal/image/TIFFLZWDecoder.java#L238-L241 This change will prevent issues with probably corrupted files. Some other implementations also allow to check for this: https://gitlab.com/libtiff/libtiff/blob/master/libtiff/tif_lzw.c#L170-185
- Loading branch information
Showing
9 changed files
with
136 additions
and
36 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
import { decode } from '..'; | ||
|
||
// We can decompress images using 'convert' from imagemagick | ||
// convert image-lzw.tif -define colorspace:auto-grayscale=false -type truecolor image.tif | ||
|
||
describe('decode lzw', () => { | ||
it('image', () => { | ||
const lzwBuffer = readFileSync(join(__dirname, '../../img/image-lzw.tif')); | ||
const imageLzw = decode(lzwBuffer); | ||
const buffer = readFileSync(join(__dirname, '../../img/image.tif')); | ||
const image = decode(buffer); | ||
expect( | ||
dataEqual(imageLzw[0].data as Uint8Array, image[0].data as Uint8Array), | ||
).toBe(true); | ||
}); | ||
it('color8', () => { | ||
const lzwBuffer = readFileSync(join(__dirname, '../../img/color8-lzw.tif')); | ||
const imageLzw = decode(lzwBuffer); | ||
const buffer = readFileSync(join(__dirname, '../../img/color8.tif')); | ||
const image = decode(buffer); | ||
expect( | ||
dataEqual(imageLzw[0].data as Uint8Array, image[0].data as Uint8Array), | ||
).toBe(true); | ||
}); | ||
}); | ||
|
||
function dataEqual(data1: Uint8Array, data2: Uint8Array): boolean { | ||
if (data1.length !== data2.length) return false; | ||
for (let i = 0; i < data1.length; i++) { | ||
if (data1[i] !== data2[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
import { decompressLzw } from '../lzw'; | ||
|
||
describe('lzw', () => { | ||
it('1', () => { | ||
const buffer = readFileSync(join(__dirname, 'data/1.strip')); | ||
const result = decompressLzw( | ||
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength), | ||
); | ||
expect(result.byteLength).toBe(7770); | ||
expect( | ||
new Uint8Array( | ||
result.buffer, | ||
result.byteOffset, | ||
result.byteLength, | ||
).reduce((sum, current) => (sum += current), 0), | ||
).toBe(675); | ||
}); | ||
it('173', () => { | ||
const buffer = readFileSync(join(__dirname, 'data/173.strip')); | ||
const result = decompressLzw( | ||
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength), | ||
); | ||
expect(result.byteLength).toBe(7770); | ||
expect( | ||
new Uint8Array( | ||
result.buffer, | ||
result.byteOffset, | ||
result.byteLength, | ||
).reduce((sum, current) => (sum += current), 0), | ||
).toBe(38307); | ||
}); | ||
it('174', () => { | ||
const buffer = readFileSync(join(__dirname, 'data/174.strip')); | ||
const result = decompressLzw( | ||
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength), | ||
); | ||
expect(result.byteLength).toBe(7770); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters