feat(struct): add Uint8ClampedArray type

This commit is contained in:
Simon Chan 2021-01-06 18:45:57 +08:00
parent c37e3dd953
commit b601f7a7f2
4 changed files with 21 additions and 6 deletions

View file

@ -2,7 +2,9 @@ import { Struct, StructValueType } from "@yume-chan/struct";
import { Adb } from '../adb'; import { Adb } from '../adb';
import { AdbBufferedStream } from '../stream'; import { AdbBufferedStream } from '../stream';
const Version = new Struct({ littleEndian: true }).uint32('version'); const Version =
new Struct({ littleEndian: true })
.uint32('version');
/* /*
* ADB uses 8 int32 fields to describe bit depths * ADB uses 8 int32 fields to describe bit depths
@ -31,7 +33,7 @@ export const AdbFrameBufferV1 =
.uint32('green_length') .uint32('green_length')
.uint32('alpha_offset') .uint32('alpha_offset')
.uint32('alpha_length') .uint32('alpha_length')
.arrayBuffer('data', { lengthField: 'size' }); .uint8ClampedArray('data', { lengthField: 'size' });
export type AdbFrameBufferV1 = StructValueType<typeof AdbFrameBufferV1>; export type AdbFrameBufferV1 = StructValueType<typeof AdbFrameBufferV1>;
@ -50,7 +52,7 @@ export const AdbFrameBufferV2 =
.uint32('green_length') .uint32('green_length')
.uint32('alpha_offset') .uint32('alpha_offset')
.uint32('alpha_length') .uint32('alpha_length')
.arrayBuffer('data', { lengthField: 'size' }); .uint8ClampedArray('data', { lengthField: 'size' });
export type AdbFrameBufferV2 = StructValueType<typeof AdbFrameBufferV2>; export type AdbFrameBufferV2 = StructValueType<typeof AdbFrameBufferV2>;

View file

@ -43,7 +43,7 @@ export const FrameBuffer = withDisplayName('FrameBuffer')(({
canvas.height = height; canvas.height = height;
const context = canvas.getContext("2d")!; const context = canvas.getContext("2d")!;
const image = new ImageData(new Uint8ClampedArray(framebuffer.data!), width, height); const image = new ImageData(framebuffer.data, width, height);
context.putImageData(image, 0, 0); context.putImageData(image, 0, 0);
setHasImage(true); setHasImage(true);
} catch (e) { } catch (e) {

View file

@ -1,6 +1,6 @@
{ {
"name": "@yume-chan/struct", "name": "@yume-chan/struct",
"version": "0.0.1", "version": "0.0.2",
"description": "C-style structure serializer and deserializer.", "description": "C-style structure serializer and deserializer.",
"keywords": [ "keywords": [
"structure", "structure",

View file

@ -1,5 +1,5 @@
import { createRuntimeObject, FieldDefinition, FieldRuntimeValue, getRuntimeValue, setRuntimeValue, StructDefaultOptions, StructDeserializationContext, StructOptions, StructSerializationContext } from './basic'; import { createRuntimeObject, FieldDefinition, FieldRuntimeValue, getRuntimeValue, setRuntimeValue, StructDefaultOptions, StructDeserializationContext, StructOptions, StructSerializationContext } from './basic';
import { ArrayBufferFieldType, ArrayBufferLikeFieldType, Evaluate, FixedLengthArrayBufferLikeFieldDefinition, FixedLengthArrayBufferLikeFieldOptions, Identity, KeysOfType, NumberFieldDefinition, NumberFieldType, Overwrite, StringFieldType, VariableLengthArrayBufferLikeFieldDefinition, VariableLengthArrayBufferLikeFieldOptions } from './types'; import { ArrayBufferFieldType, ArrayBufferLikeFieldType, Evaluate, FixedLengthArrayBufferLikeFieldDefinition, FixedLengthArrayBufferLikeFieldOptions, Identity, KeysOfType, NumberFieldDefinition, NumberFieldType, Overwrite, StringFieldType, Uint8ClampedArrayFieldType, VariableLengthArrayBufferLikeFieldDefinition, VariableLengthArrayBufferLikeFieldOptions } from './types';
/** /**
* Extract the value type of the specified `Struct` * Extract the value type of the specified `Struct`
@ -354,6 +354,19 @@ export default class Struct<
return this.arrayBufferLike(name, ArrayBufferFieldType.instance, options); return this.arrayBufferLike(name, ArrayBufferFieldType.instance, options);
}; };
public uint8ClampedArray: ArrayBufferTypeFieldDefinitionCreator<
TValue,
TInit,
TExtra,
TPostDeserialized,
Uint8ClampedArrayFieldType
> = (
name: PropertyKey,
options: any
): any => {
return this.arrayBufferLike(name, Uint8ClampedArrayFieldType.instance, options);
};
public string: ArrayBufferTypeFieldDefinitionCreator< public string: ArrayBufferTypeFieldDefinitionCreator<
TValue, TValue,
TInit, TInit,