Skip to content

Commit f13a796

Browse files
authored
Merge pull request #94 from ssssota/instantiate-struct-definition
refactor: instantiate struct definition first
2 parents 12494fc + fff0fa2 commit f13a796

File tree

1 file changed

+75
-70
lines changed

1 file changed

+75
-70
lines changed

packages/libraw.wasm/src/index.ts

Lines changed: 75 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,56 @@ import type {
2525
type PromiseOr<T> = T | Promise<T>;
2626
type WasmInput = PromiseOr<Response | ArrayBuffer>;
2727

28+
const imgother_t = libraw_imgother_t()
29+
.override("desc", typ.sizedCharArrayAsString(512))
30+
.override("artist", typ.sizedCharArrayAsString(64));
31+
const iparams_t = libraw_iparams_t()
32+
.override("make", typ.sizedCharArrayAsString(64))
33+
.override("model", typ.sizedCharArrayAsString(64))
34+
.override("software", typ.sizedCharArrayAsString(64))
35+
.override("normalized_make", typ.sizedCharArrayAsString(64))
36+
.override("normalized_model", typ.sizedCharArrayAsString(64))
37+
.override("cdesc", typ.sizedCharArrayAsString(5))
38+
.override("xmpdata", typ.charPointerAsString());
39+
const makernotes_lens_t = libraw_makernotes_lens_t()
40+
.override("Lens", typ.sizedCharArrayAsString(128))
41+
.override("body", typ.sizedCharArrayAsString(64))
42+
.override(
43+
"FocalType",
44+
typ.enumLike(typ.i16, {
45+
unknown: 0,
46+
"fixed focal": 1,
47+
zoom: 2,
48+
} as const),
49+
)
50+
.override("LensFeatures_pre", typ.sizedCharArrayAsString(16))
51+
.override("LensFeatures_suf", typ.sizedCharArrayAsString(16))
52+
.override("Teleconverter", typ.sizedCharArrayAsString(128))
53+
.override("Adapter", typ.sizedCharArrayAsString(128))
54+
.override("Attachment", typ.sizedCharArrayAsString(128));
55+
const lensinfo_t = libraw_lensinfo_t()
56+
.override("LensMake", typ.sizedCharArrayAsString(128))
57+
.override("Lens", typ.sizedCharArrayAsString(128))
58+
.override("LensSerial", typ.sizedCharArrayAsString(128))
59+
.override("InternalLensSerial", typ.sizedCharArrayAsString(128))
60+
.override("makernotes", makernotes_lens_t);
61+
const processed_image_t = libraw_processed_image_t().override("data", {
62+
size: 1,
63+
read(opts, ctx) {
64+
const offset = opts.offset ?? 0;
65+
const size = ctx.data_size;
66+
return opts.buf.slice(offset, offset + size);
67+
},
68+
});
69+
const shootinginfo_t = libraw_shootinginfo_t()
70+
.override("BodySerial", typ.sizedCharArrayAsString(64))
71+
.override("InternalBodySerial", typ.sizedCharArrayAsString(64));
72+
const makernotes_t = libraw_makernotes_t();
73+
const thumbnail_t = libraw_thumbnail_t().override(
74+
"thumb",
75+
typ.pointerArrayFromLengthField(typ.u8, "tlength"),
76+
);
77+
2878
export class LibRaw implements Disposable {
2979
private static modulePromise: Promise<LibRawWasmModule> | undefined;
3080
private static module: LibRawWasmModule;
@@ -123,16 +173,10 @@ export class LibRaw implements Disposable {
123173
return ret;
124174
}
125175
private readProcessedImage(processed: LibRawProcessedImageT) {
126-
return libraw_processed_image_t()
127-
.override("data", {
128-
size: 1,
129-
read(opts, ctx) {
130-
const offset = opts.offset ?? 0;
131-
const size = ctx.data_size;
132-
return opts.buf.slice(offset, offset + size);
133-
},
134-
})
135-
.read({ buf: LibRaw.module.HEAPU8, offset: processed });
176+
return processed_image_t.read({
177+
buf: LibRaw.module.HEAPU8,
178+
offset: processed,
179+
});
136180
}
137181
getRawHeight() {
138182
return LibRaw.module._libraw_get_raw_height(this.lr);
@@ -225,56 +269,22 @@ export class LibRaw implements Disposable {
225269
return new Uint16Array(LibRaw.module.HEAPU8.buffer, ptr);
226270
}
227271
getIParams() {
228-
return libraw_iparams_t()
229-
.override("make", typ.sizedCharArrayAsString(64))
230-
.override("model", typ.sizedCharArrayAsString(64))
231-
.override("software", typ.sizedCharArrayAsString(64))
232-
.override("normalized_make", typ.sizedCharArrayAsString(64))
233-
.override("normalized_model", typ.sizedCharArrayAsString(64))
234-
.override("cdesc", typ.sizedCharArrayAsString(5))
235-
.override("xmpdata", typ.charPointerAsString())
236-
.read({
237-
buf: LibRaw.module.HEAPU8,
238-
offset: LibRaw.module._libraw_get_iparams(this.lr),
239-
});
272+
return iparams_t.read({
273+
buf: LibRaw.module.HEAPU8,
274+
offset: LibRaw.module._libraw_get_iparams(this.lr),
275+
});
240276
}
241277
getLensInfo() {
242-
const makernotes = libraw_makernotes_lens_t()
243-
.override("Lens", typ.sizedCharArrayAsString(128))
244-
.override("body", typ.sizedCharArrayAsString(64))
245-
.override(
246-
"FocalType",
247-
typ.enumLike(typ.i16, {
248-
unknown: 0,
249-
"fixed focal": 1,
250-
zoom: 2,
251-
} as const),
252-
)
253-
.override("LensFeatures_pre", typ.sizedCharArrayAsString(16))
254-
.override("LensFeatures_suf", typ.sizedCharArrayAsString(16))
255-
.override("Teleconverter", typ.sizedCharArrayAsString(128))
256-
.override("Adapter", typ.sizedCharArrayAsString(128))
257-
.override("Attachment", typ.sizedCharArrayAsString(128));
258-
259-
return libraw_lensinfo_t()
260-
.override("LensMake", typ.sizedCharArrayAsString(128))
261-
.override("Lens", typ.sizedCharArrayAsString(128))
262-
.override("LensSerial", typ.sizedCharArrayAsString(128))
263-
.override("InternalLensSerial", typ.sizedCharArrayAsString(128))
264-
.override("makernotes", makernotes)
265-
.read({
266-
buf: LibRaw.module.HEAPU8,
267-
offset: LibRaw.module._libraw_get_lensinfo(this.lr),
268-
});
278+
return lensinfo_t.read({
279+
buf: LibRaw.module.HEAPU8,
280+
offset: LibRaw.module._libraw_get_lensinfo(this.lr),
281+
});
269282
}
270283
getImgOther() {
271-
return libraw_imgother_t()
272-
.override("desc", typ.sizedCharArrayAsString(512))
273-
.override("artist", typ.sizedCharArrayAsString(64))
274-
.read({
275-
buf: LibRaw.module.HEAPU8,
276-
offset: LibRaw.module._libraw_get_imgother(this.lr),
277-
});
284+
return imgother_t.read({
285+
buf: LibRaw.module.HEAPU8,
286+
offset: LibRaw.module._libraw_get_imgother(this.lr),
287+
});
278288
}
279289
// getDecoderInfo() {
280290
// /**
@@ -297,24 +307,19 @@ export class LibRaw implements Disposable {
297307
// };
298308
// }
299309
getThumbnail() {
300-
return libraw_thumbnail_t()
301-
.override("thumb", typ.pointerArrayFromLengthField(typ.u8, "tlength"))
302-
.read({
303-
buf: LibRaw.module.HEAPU8,
304-
offset: LibRaw.module._libraw_get_thumbnail(this.lr),
305-
});
310+
return thumbnail_t.read({
311+
buf: LibRaw.module.HEAPU8,
312+
offset: LibRaw.module._libraw_get_thumbnail(this.lr),
313+
});
306314
}
307315
getShootingInfo() {
308-
return libraw_shootinginfo_t()
309-
.override("BodySerial", typ.sizedCharArrayAsString(64))
310-
.override("InternalBodySerial", typ.sizedCharArrayAsString(64))
311-
.read({
312-
buf: LibRaw.module.HEAPU8,
313-
offset: LibRaw.module._libraw_get_shootinginfo(this.lr),
314-
});
316+
return shootinginfo_t.read({
317+
buf: LibRaw.module.HEAPU8,
318+
offset: LibRaw.module._libraw_get_shootinginfo(this.lr),
319+
});
315320
}
316321
getMakernotes() {
317-
return libraw_makernotes_t().read({
322+
return makernotes_t.read({
318323
buf: LibRaw.module.HEAPU8,
319324
offset: LibRaw.module._libraw_get_makernotes(this.lr),
320325
});

0 commit comments

Comments
 (0)