Skip to content

Commit becb91d

Browse files
refactor(pixel-io-tiff): internal update __readEntry()
- extract __readValues() to dedupe logic
1 parent 42a0a79 commit becb91d

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

packages/pixel-io-tiff/src/read.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type NumericArray,
55
type UIntArray,
66
} from "@thi.ng/api/typedarray";
7-
import { DATAVIEW as DV } from "@thi.ng/binary/endianess";
7+
import { DATAVIEW as DV, type IDataView } from "@thi.ng/binary/endianess";
88
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
99
import { unsupportedFeature as unsupported } from "@thi.ng/errors/unsupported";
1010
import { ARGB8888 } from "@thi.ng/pixel/format/argb8888";
@@ -231,24 +231,17 @@ const __readEntry = (
231231
case 3:
232232
case 8: {
233233
const offset = num < 3 ? addr + 8 : DV.getU32(buf, addr + 8, isLE);
234-
const val: number[] = [];
235234
const read = type === 3 ? DV.getU16 : DV.getI16;
236-
for (let i = 0; i < num; i++)
237-
val.push(read(buf, offset + i * 2, isLE));
238-
return val;
235+
return __readValues(read, num, 2, buf, offset, isLE);
239236
}
240237
// u32 (4) / i32 (9) / f32 (11)
241238
case 4:
242239
case 9:
243240
case 11: {
244241
const offset = num < 2 ? addr + 8 : DV.getU32(buf, addr + 8, isLE);
245-
const val: number[] = [];
246242
const read =
247243
type === 4 ? DV.getU32 : type === 9 ? DV.getI32 : DV.getF32;
248-
num *= 4;
249-
for (let i = 0; i < num; i += 4)
250-
val.push(read(buf, offset + i, isLE));
251-
return val;
244+
return __readValues(read, num, 4, buf, offset, isLE);
252245
}
253246
// rational
254247
case 5:
@@ -268,17 +261,39 @@ const __readEntry = (
268261
// f64
269262
case 12: {
270263
const offset = DV.getU32(buf, addr + 8, isLE);
271-
const val: number[] = [];
272-
num *= 8;
273-
for (let i = 0; i < num; i += 8)
274-
val.push(DV.getF64(buf, offset + i, isLE));
275-
return val;
264+
return __readValues(DV.getF64, num, 8, buf, offset, isLE);
276265
}
277266
default:
278267
console.log("ignoring IFD entry w/ type:", type);
279268
}
280269
};
281270

271+
/**
272+
* Reads array of values for a single IFD entry.
273+
*
274+
* @param read
275+
* @param num
276+
* @param size
277+
* @param buf
278+
* @param offset
279+
* @param isLE
280+
*
281+
* @internal
282+
*/
283+
const __readValues = (
284+
read: IDataView["getU32"],
285+
num: number,
286+
size: number,
287+
buf: Uint8Array,
288+
offset: number,
289+
isLE: boolean
290+
) => {
291+
num *= size;
292+
const value: number[] = [];
293+
for (let i = 0; i < num; i += size) value.push(read(buf, offset + i, isLE));
294+
return value;
295+
};
296+
282297
/**
283298
* Reads all image tiles into a single u8/u16 pixel buffer.
284299
*
@@ -455,6 +470,9 @@ const __readDeflate = async (buf: Uint8Array<ArrayBuffer>, bpp: number) => {
455470
/**
456471
* Applies predictor=2 differencing to decode given image chunk.
457472
*
473+
* Reference:
474+
* - TIFF Rev.6 Section 14: Differencing Predictor
475+
*
458476
* @internal
459477
*/
460478
const __horizontalDifferencing = (

0 commit comments

Comments
 (0)