@flighthq/image 0.2.1-next.841.373388f → 0.3.0-next.1032.e2b99fc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ export * from './imageResource';
2
+ export * from './imageResourceFrom';
3
+ //# sourceMappingURL=contract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../src/contract.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './imageResource';
2
+ export * from './imageResourceFrom';
3
+ //# sourceMappingURL=contract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract.js","sourceRoot":"","sources":["../src/contract.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}
@@ -1,13 +1,7 @@
1
- import type { ImageResource, ImageResourceCompressed } from '@flighthq/types';
1
+ import type { CompressedImage, CompressedImageData, ImageResource } from '@flighthq/types/contract';
2
2
  export declare function cloneImageResource(resource: Readonly<ImageResource>): ImageResource;
3
- export declare function createCompressedImageResource(compressed: Readonly<ImageResourceCompressed>): ImageResource;
4
- export declare function createImageResource(image?: CanvasImageSource): ImageResource;
5
- export declare function disposeImageResource(resource: ImageResource): void;
6
- export declare function getImageResourceByteSize(resource: Readonly<ImageResource>): number;
7
- export declare function hasImageResourceData(resource: Readonly<ImageResource>): boolean;
8
- export declare function hasImageResourcePixels(resource: Readonly<ImageResource>): boolean;
9
- export declare function hasImageResourceSource(resource: Readonly<ImageResource>): boolean;
3
+ export declare function createCompressedImage(compressed: Readonly<CompressedImageData>): CompressedImage;
4
+ export declare function createImageResource(image: CanvasImageSource): ImageResource;
10
5
  export declare function invalidateImageResource(resource: ImageResource): void;
11
6
  export declare function isImageResourceEmpty(resource: Readonly<ImageResource>): boolean;
12
- export declare function setImageResourceSource(resource: ImageResource, element: CanvasImageSource | null): void;
13
7
  //# sourceMappingURL=imageResource.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"imageResource.d.ts","sourceRoot":"","sources":["../src/imageResource.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAM9E,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,CAAC,aAAa,CAAC,GAAG,aAAa,CAWnF;AAMD,wBAAgB,6BAA6B,CAAC,UAAU,EAAE,QAAQ,CAAC,uBAAuB,CAAC,GAAG,aAAa,CAW1G;AAED,wBAAgB,mBAAmB,CAAC,KAAK,CAAC,EAAE,iBAAiB,GAAG,aAAa,CAa5E;AAMD,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,aAAa,GAAG,IAAI,CAKlE;AAMD,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,QAAQ,CAAC,aAAa,CAAC,GAAG,MAAM,CAElF;AAED,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,CAAC,aAAa,CAAC,GAAG,OAAO,CAE/E;AAQD,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,QAAQ,CAAC,aAAa,CAAC,GAAG,OAAO,CAEjF;AAED,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,QAAQ,CAAC,aAAa,CAAC,GAAG,OAAO,CAEjF;AAKD,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,aAAa,GAAG,IAAI,CAErE;AAED,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,CAAC,aAAa,CAAC,GAAG,OAAO,CAE/E;AAID,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAE,iBAAiB,GAAG,IAAI,GAAG,IAAI,CAIvG"}
1
+ {"version":3,"file":"imageResource.d.ts","sourceRoot":"","sources":["../src/imageResource.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAKpG,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,CAAC,aAAa,CAAC,GAAG,aAAa,CAQnF;AAID,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,QAAQ,CAAC,mBAAmB,CAAC,GAAG,eAAe,CAQhG;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,iBAAiB,GAAG,aAAa,CAU3E;AAGD,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,aAAa,GAAG,IAAI,CAGrE;AAED,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,CAAC,aAAa,CAAC,GAAG,OAAO,CAE/E"}
@@ -1,104 +1,52 @@
1
- import { createEntity } from '@flighthq/entity';
2
- // Allocates a new resource identity over the SAME underlying pixels. The element, the `data` array, and
3
- // the `compressed` payload are shared by reference, not duplicated clone gives you an independent
4
- // version counter and entity identity over the same pixels, e.g. to upload one image into two render
5
- // states with separate invalidation. Use a Surface copy when you need the pixels themselves duplicated.
1
+ import { createEntity } from '@flighthq/entity/contract';
2
+ import { CompressedImageTextureBackingKind, ImageTextureBackingKind } from '@flighthq/types/contract';
3
+ // Allocates a new resource identity over the same borrowed host image. The host handle is shared by
4
+ // reference; the clone owns an independent version counter for renderer cache invalidation.
6
5
  export function cloneImageResource(resource) {
7
6
  return createEntity({
8
- alphaType: resource.alphaType,
9
- compressed: resource.compressed,
10
- data: resource.data,
11
- format: resource.format,
12
7
  height: resource.height,
8
+ kind: resource.kind,
13
9
  source: resource.source,
14
10
  version: resource.version,
15
11
  width: resource.width,
16
12
  });
17
13
  }
18
- // Wraps a parsed block-compressed (KTX2/DDS/Basis) payload as an ImageResource. `width`/`height` mirror
19
- // the container's base-mip dimensions so the same 2D draw path sizes the quad; `source`/`data` stay null
20
- // (a GPU backend uploads the `compressed` payload instead, and a Canvas/DOM backend ignores it). The
21
- // caller owns the payload bytes the container's level ranges index into.
22
- export function createCompressedImageResource(compressed) {
14
+ // Wraps a parsed block-compressed payload as its own GPU-only backing. The caller owns the payload
15
+ // bytes indexed by the container's level ranges.
16
+ export function createCompressedImage(compressed) {
23
17
  return createEntity({
24
- alphaType: 'straight',
25
18
  compressed,
26
- data: null,
27
- format: 'rgba8unorm',
28
19
  height: compressed.container.height,
29
- source: null,
20
+ kind: CompressedImageTextureBackingKind,
30
21
  version: 0,
31
22
  width: compressed.container.width,
32
23
  });
33
24
  }
34
25
  export function createImageResource(image) {
35
26
  const resource = createEntity({
36
- alphaType: 'straight',
37
- compressed: null,
38
- data: null,
39
- format: 'rgba8unorm',
40
27
  height: 0,
41
- source: image ?? null,
28
+ kind: ImageTextureBackingKind,
29
+ source: image,
42
30
  version: 0,
43
31
  width: 0,
44
32
  });
45
- if (resource.source !== null)
46
- updateImageResourceSize(resource);
33
+ updateImageResourceSize(resource);
47
34
  return resource;
48
35
  }
49
- // Releases the element and raw-data references so they become eligible for GC, and marks the resource
50
- // changed. This does NOT free the backend GPU texture (that is owned per render state — call the
51
- // backend's destroy*Texture) and does NOT close an owned ImageBitmap (ownership is ambiguous; close it
52
- // explicitly if you own it). Dimensions are left intact; setImageResourceSource repopulates the resource.
53
- export function disposeImageResource(resource) {
54
- resource.compressed = null;
55
- resource.data = null;
56
- resource.source = null;
57
- invalidateImageResource(resource);
58
- }
59
- // Returns the byte footprint of the CPU-side pixel data (`data` array). Returns 0 when `data` is
60
- // null (element-only resource — the GPU texture footprint is tracked by the render state, not here).
61
- // For `rgba8unorm` (the default) the result equals width × height × 4; other formats are not yet
62
- // exercised but the formula still applies (byteLength of the actual buffer is authoritative).
63
- export function getImageResourceByteSize(resource) {
64
- return resource.data !== null ? resource.data.byteLength : 0;
65
- }
66
- export function hasImageResourceData(resource) {
67
- return resource.data !== null;
68
- }
69
- // True when the resource carries pixels in any representation — a decoded `source` element, raw CPU
70
- // `data`, or a block-`compressed` payload. The upload-readiness predicate for backends that can consume
71
- // any of them: an element-only load, a data-only generated Surface, a compressed-only parsed container,
72
- // or a resource holding several are all uploadable. Prefer this over hasImageResourceSource at any gate
73
- // that feeds a source-or-data uploader (bindGlTexture, bindWgpuTexture), so a memory-generated Surface
74
- // or a compressed container is not rejected for lacking an element.
75
- export function hasImageResourcePixels(resource) {
76
- return resource.source !== null || resource.data !== null || resource.compressed !== null;
77
- }
78
- export function hasImageResourceSource(resource) {
79
- return resource.source !== null;
80
- }
81
- // Bumps the resource content revision so consumers (renderer texture caches) know the pixels behind
82
- // this image changed even though the object identity is the same. Call after mutating the backing
83
- // pixels in place; the Surface API calls it for you. Resource-tier analog of invalidateNodeLocalContent.
36
+ // Marks changed pixels behind the same borrowed host handle. The handle itself remains immutable.
84
37
  export function invalidateImageResource(resource) {
38
+ updateImageResourceSize(resource);
85
39
  resource.version = (resource.version + 1) >>> 0;
86
40
  }
87
41
  export function isImageResourceEmpty(resource) {
88
42
  return resource.width <= 0 || resource.height <= 0;
89
43
  }
90
- // Swaps the element representation, re-reads its dimensions, and marks the resource changed. Pass
91
- // null to clear the element (dimensions are left intact; use disposeImageResource to release for GC).
92
- export function setImageResourceSource(resource, element) {
93
- resource.source = element;
94
- if (element !== null)
95
- updateImageResourceSize(resource);
96
- invalidateImageResource(resource);
97
- }
98
- // Reads pixel dimensions from the current (non-null) element. Video sources carry their size on
44
+ // Reads pixel dimensions from the current host element. Video sources carry their size on
99
45
  // videoWidth/videoHeight; every other CanvasImageSource exposes width/height directly.
100
46
  function updateImageResourceSize(resource) {
101
47
  const element = resource.source;
48
+ if (element === null)
49
+ return;
102
50
  if (element instanceof HTMLVideoElement) {
103
51
  resource.width = element.videoWidth;
104
52
  resource.height = element.videoHeight;
@@ -1 +1 @@
1
- {"version":3,"file":"imageResource.js","sourceRoot":"","sources":["../src/imageResource.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAGhD,wGAAwG;AACxG,oGAAoG;AACpG,qGAAqG;AACrG,wGAAwG;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M,CAAC;QACN,MAAM,KAAK,GAAG,OAA6D,CAAC;QAC5E,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;QAC7B,QAAQ,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;IACjC,CAAC;AACH,CAAC"}
@@ -1,5 +1,5 @@
1
- import type { ImageResource } from '@flighthq/types';
2
- export declare function createCanvasFromImageResource(image: Readonly<ImageResource>): HTMLCanvasElement | null;
1
+ import type { Bitmap, ImageResource } from '@flighthq/types/contract';
2
+ export declare function createImageResourceFromBitmap(bitmap: Readonly<Bitmap>): ImageResource;
3
3
  export declare function createImageResourceFromCanvas(canvas: HTMLCanvasElement): ImageResource;
4
4
  export declare function createImageResourceFromImageBitmap(bitmap: ImageBitmap): ImageResource;
5
5
  export declare function createImageResourceFromImageElement(img: HTMLImageElement): ImageResource;
@@ -1 +1 @@
1
- {"version":3,"file":"imageResourceFrom.d.ts","sourceRoot":"","sources":["../src/imageResourceFrom.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAQrD,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,QAAQ,CAAC,aAAa,CAAC,GAAG,iBAAiB,GAAG,IAAI,CAStG;AAED,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,iBAAiB,GAAG,aAAa,CAWtF;AAED,wBAAgB,kCAAkC,CAAC,MAAM,EAAE,WAAW,GAAG,aAAa,CAWrF;AAED,wBAAgB,mCAAmC,CAAC,GAAG,EAAE,gBAAgB,GAAG,aAAa,CAWxF;AAED,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAO9D;AAED,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,aAAa,CAAC,CAExB;AAED,wBAAsB,yBAAyB,CAAC,IAAI,EAAE,IAAI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AAC,aAAa,CAAC,CAOxB;AAED,wBAAsB,wBAAwB,CAC5C,GAAG,EAAE,MAAM,EACX,WAAW,CAAC,EAAE,MAAM,EACpB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,aAAa,CAAC,CAexB"}
@@ -1,29 +1,23 @@
1
- import { createEntity } from '@flighthq/entity';
2
- import { detectImageMimeType } from '@flighthq/image-codec';
3
- // Materializes a data-backed ImageResource's raw pixels into a detached, drawable HTMLCanvasElement
4
- // via putImageData. The inverse of createImageResourceFromCanvas: it turns the portable `data`
5
- // representation of a generated Surface into the host element that Canvas/DOM `drawImage` needs.
6
- // Returns null for an element-only resource (no `data` to transcode). Straight-alpha in, straight-alpha
7
- // out the drawn canvas matches the source's `alphaType`. Callers that draw data-only resources cache
8
- // the result keyed on `version`; this primitive allocates a fresh canvas on every call.
9
- export function createCanvasFromImageResource(image) {
10
- if (image.data === null)
11
- return null;
1
+ import { createEntity } from '@flighthq/entity/contract';
2
+ import { detectImageMimeType } from '@flighthq/image-codec/contract';
3
+ import { ImageTextureBackingKind } from '@flighthq/types/contract';
4
+ // Transcodes a Bitmap's raw pixels into an element-backed ImageResource, via a detached canvas.
5
+ // The inverse of captureBitmapFromImageResource. Lives here rather than in @flighthq/bitmap because a
6
+ // conversion belongs with the type it PRODUCES: you look for it under what you want to end up with.
7
+ // Allocates a fresh canvas on every call; callers that draw repeatedly should hold the result.
8
+ export function createImageResourceFromBitmap(bitmap) {
12
9
  const canvas = document.createElement('canvas');
13
- canvas.width = image.width;
14
- canvas.height = image.height;
15
- const imageData = new globalThis.ImageData(image.width, image.height);
16
- imageData.data.set(image.data);
17
- canvas.getContext('2d').putImageData(imageData, 0, 0);
18
- return canvas;
10
+ canvas.width = bitmap.width;
11
+ canvas.height = bitmap.height;
12
+ const domImageData = new globalThis.ImageData(bitmap.width, bitmap.height);
13
+ domImageData.data.set(bitmap.data);
14
+ canvas.getContext('2d').putImageData(domImageData, 0, 0);
15
+ return createImageResourceFromCanvas(canvas);
19
16
  }
20
17
  export function createImageResourceFromCanvas(canvas) {
21
18
  return createEntity({
22
- alphaType: 'straight',
23
- compressed: null,
24
- data: null,
25
- format: 'rgba8unorm',
26
19
  height: canvas.height,
20
+ kind: ImageTextureBackingKind,
27
21
  source: canvas,
28
22
  version: 0,
29
23
  width: canvas.width,
@@ -31,11 +25,8 @@ export function createImageResourceFromCanvas(canvas) {
31
25
  }
32
26
  export function createImageResourceFromImageBitmap(bitmap) {
33
27
  return createEntity({
34
- alphaType: 'straight',
35
- compressed: null,
36
- data: null,
37
- format: 'rgba8unorm',
38
28
  height: bitmap.height,
29
+ kind: ImageTextureBackingKind,
39
30
  source: bitmap,
40
31
  version: 0,
41
32
  width: bitmap.width,
@@ -43,11 +34,8 @@ export function createImageResourceFromImageBitmap(bitmap) {
43
34
  }
44
35
  export function createImageResourceFromImageElement(img) {
45
36
  return createEntity({
46
- alphaType: 'straight',
47
- compressed: null,
48
- data: null,
49
- format: 'rgba8unorm',
50
37
  height: img.height,
38
+ kind: ImageTextureBackingKind,
51
39
  source: img,
52
40
  version: 0,
53
41
  width: img.width,
@@ -1 +1 @@
1
- {"version":3,"file":"imageResourceFrom.js","sourceRoot":"","sources":["../src/imageResourceFrom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAG5D,oGAAoG;AACpG,+FAA+F;AAC/F,iGAAiG;AACjG,wGAAwG;AACxG,uGAAuG;AACvG,wFAAwF;AACxF,MAAM,UAAU,6BAA6B,CAAC,KAA8B;IAC1E,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IACrC,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAChD,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;IAC3B,MAAM,CAAC,MAAM,GA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KAAK,CAAC,UAAU,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;IACvG,OAAO,yBAAyB,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC;AACtE,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC5C,GAAW,EACX,WAAoB,EACpB,MAAoB;IAEpB,MAAM,EAAE,cAAc,EAAE,CAAC;IACzB,MAAM,GAAG,GAAG,IAAI,KAAK,EAAE,CAAC;IACxB,IAAI,WAAW,KAAK,SAAS;QAAE,GAAG,CAAC,WAAW,GAAG,WAAW,CAAC;IAC7D,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC;IACd,oEAAoE;IACpE,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,MAAM,OAAO,CAAC,IAAI,CAAC;YACjB,GAAG,CAAC,MAAM,EAAE;YACZ,IAAI,OAAO,CAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;SACjH,CAAC,CAAC;IACL,CAAC;SAAM,CAAC;QACN,MAAM,GAAG,CAAC,MAAM,EAAE,CAAC;IACrB,CAAC;IACD,OAAO,mCAAmC,CAAC,GAAG,CAAC,CAAC;AAClD,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
- export * from './imageResource';
2
- export * from './imageResourceFrom';
1
+ export { cloneImageResource, createCompressedImage, createImageResource, createImageResourceFromBitmap, createImageResourceFromCanvas, createImageResourceFromImageBitmap, createImageResourceFromImageElement, isImageResourceEmpty, isImageResourceSameOrigin, loadImageResourceFromBase64, loadImageResourceFromBlob, loadImageResourceFromBytes, loadImageResourceFromUrl, } from './contract';
3
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,qBAAqB,EACrB,mBAAmB,EACnB,6BAA6B,EAC7B,6BAA6B,EAC7B,kCAAkC,EAClC,mCAAmC,EACnC,oBAAoB,EACpB,yBAAyB,EACzB,2BAA2B,EAC3B,yBAAyB,EACzB,0BAA0B,EAC1B,wBAAwB,GACzB,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,3 +1,2 @@
1
- export * from './imageResource';
2
- export * from './imageResourceFrom';
1
+ export { cloneImageResource, createCompressedImage, createImageResource, createImageResourceFromBitmap, createImageResourceFromCanvas, createImageResourceFromImageBitmap, createImageResourceFromImageElement, isImageResourceEmpty, isImageResourceSameOrigin, loadImageResourceFromBase64, loadImageResourceFromBlob, loadImageResourceFromBytes, loadImageResourceFromUrl, } from './contract';
3
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,qBAAqB,EACrB,mBAAmB,EACnB,6BAA6B,EAC7B,6BAA6B,EAC7B,kCAAkC,EAClC,mCAAmC,EACnC,oBAAoB,EACpB,yBAAyB,EACzB,2BAA2B,EAC3B,yBAAyB,EACzB,0BAA0B,EAC1B,wBAAwB,GACzB,MAAM,YAAY,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flighthq/image",
3
- "version": "0.2.1-next.841.373388f",
3
+ "version": "0.3.0-next.1032.e2b99fc",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/flighthq/flight.git",
@@ -13,6 +13,10 @@
13
13
  ".": {
14
14
  "types": "./dist/index.d.ts",
15
15
  "default": "./dist/index.js"
16
+ },
17
+ "./contract": {
18
+ "types": "./dist/contract.d.ts",
19
+ "default": "./dist/contract.js"
16
20
  }
17
21
  },
18
22
  "files": [
@@ -32,9 +36,9 @@
32
36
  "clean:dist": "tsx ../../scripts/clean-package-dist.ts"
33
37
  },
34
38
  "dependencies": {
35
- "@flighthq/entity": "0.2.1-next.841.373388f",
36
- "@flighthq/image-codec": "0.2.1-next.841.373388f",
37
- "@flighthq/types": "0.2.1-next.841.373388f"
39
+ "@flighthq/entity": "0.3.0-next.1032.e2b99fc",
40
+ "@flighthq/image-codec": "0.3.0-next.1032.e2b99fc",
41
+ "@flighthq/types": "0.3.0-next.1032.e2b99fc"
38
42
  },
39
43
  "devDependencies": {
40
44
  "typescript": "^5.3.0"
@@ -1,234 +1,86 @@
1
- import type { ImageResourceCompressed } from '@flighthq/types';
1
+ import type { CompressedImageData } from '@flighthq/types/contract';
2
+ import { CompressedImageTextureBackingKind, ImageTextureBackingKind } from '@flighthq/types/contract';
2
3
 
3
4
  import {
4
5
  cloneImageResource,
5
- createCompressedImageResource,
6
+ createCompressedImage,
6
7
  createImageResource,
7
- disposeImageResource,
8
- getImageResourceByteSize,
9
- hasImageResourceData,
10
- hasImageResourcePixels,
11
- hasImageResourceSource,
12
8
  invalidateImageResource,
13
9
  isImageResourceEmpty,
14
- setImageResourceSource,
15
10
  } from './imageResource';
16
11
 
17
- // A minimal single-mip bc3 4x4 compressed payload stand-in for the resource wrapper tests.
18
- function makeCompressed(): ImageResourceCompressed {
12
+ function makeCompressed(): CompressedImageData {
19
13
  return {
20
14
  container: {
15
+ depth: 1,
16
+ faces: 1,
21
17
  format: 'bc3',
22
- width: 4,
23
18
  height: 4,
24
- depth: 1,
25
- mipLevels: 1,
26
19
  layers: 1,
27
- faces: 1,
20
+ levels: [{ byteLength: 16, byteOffset: 0, height: 4, width: 4 }],
21
+ mipLevels: 1,
28
22
  supercompression: 'None',
29
- levels: [{ byteOffset: 0, byteLength: 16, width: 4, height: 4 }],
23
+ width: 4,
30
24
  },
31
25
  payload: new Uint8Array(16),
32
26
  };
33
27
  }
34
28
 
35
29
  describe('cloneImageResource', () => {
36
- it('copies fields, shares the element, and gives an independent identity', () => {
37
- const element = { width: 4, height: 5 } as HTMLImageElement;
38
- const resource = createImageResource(element);
30
+ it('shares the host source under an independent identity and version', () => {
31
+ const source = document.createElement('canvas');
32
+ source.width = 4;
33
+ source.height = 5;
34
+ const resource = createImageResource(source);
39
35
  resource.version = 3;
40
-
41
36
  const copy = cloneImageResource(resource);
42
-
43
37
  expect(copy).not.toBe(resource);
44
- expect(copy.source).toBe(element);
45
- expect(copy.width).toStrictEqual(4);
46
- expect(copy.height).toStrictEqual(5);
47
- expect(copy.version).toStrictEqual(3);
48
-
38
+ expect(copy.source).toBe(source);
39
+ expect(copy.version).toBe(3);
49
40
  copy.version++;
50
- expect(resource.version).toStrictEqual(3);
51
- });
52
-
53
- it('shares the data buffer by reference and copies the format and alphaType', () => {
54
- const resource = createImageResource();
55
- resource.data = new Uint8ClampedArray([1, 2, 3, 4]);
56
- resource.format = 'bgra8unorm';
57
- resource.alphaType = 'premultiplied';
58
-
59
- const copy = cloneImageResource(resource);
60
-
61
- expect(copy.data).toBe(resource.data);
62
- expect(copy.format).toStrictEqual('bgra8unorm');
63
- expect(copy.alphaType).toStrictEqual('premultiplied');
64
- });
65
-
66
- it('shares the compressed payload by reference', () => {
67
- const resource = createCompressedImageResource(makeCompressed());
68
- const copy = cloneImageResource(resource);
69
- expect(copy.compressed).toBe(resource.compressed);
41
+ expect(resource.version).toBe(3);
70
42
  });
71
43
  });
72
44
 
73
- describe('createCompressedImageResource', () => {
74
- it('wraps the container, mirrors its base dimensions, and leaves source/data null', () => {
45
+ describe('createCompressedImage', () => {
46
+ it('wraps the compressed payload as a distinct backing', () => {
75
47
  const compressed = makeCompressed();
76
- const resource = createCompressedImageResource(compressed);
77
- expect(resource.compressed).toBe(compressed);
78
- expect(resource.width).toStrictEqual(4);
79
- expect(resource.height).toStrictEqual(4);
80
- expect(resource.source).toBeNull();
81
- expect(resource.data).toBeNull();
82
- expect(resource.version).toStrictEqual(0);
48
+ const image = createCompressedImage(compressed);
49
+ expect(image.kind).toBe(CompressedImageTextureBackingKind);
50
+ expect(image.compressed).toBe(compressed);
51
+ expect(image.width).toBe(4);
52
+ expect(image.height).toBe(4);
83
53
  });
84
54
  });
85
55
 
86
56
  describe('createImageResource', () => {
87
- it('returns an object', () => {
88
- const resource = createImageResource();
89
- expect(resource).not.toBeNull();
90
- });
91
-
92
- it('sets source, width and height if you pass in a source element', () => {
93
- const element = { width: 100, height: 100 } as HTMLImageElement;
94
- const resource = createImageResource(element);
95
- expect(resource.source).toStrictEqual(element);
96
- expect(resource.width).toStrictEqual(element.width);
97
- expect(resource.height).toStrictEqual(element.height);
98
- });
99
-
100
- it('starts at version 0', () => {
101
- expect(createImageResource().version).toBe(0);
102
- });
103
-
104
- it('defaults to null data, rgba8unorm format, and straight alphaType', () => {
105
- const resource = createImageResource();
106
- expect(resource.data).toBeNull();
107
- expect(resource.format).toStrictEqual('rgba8unorm');
108
- expect(resource.alphaType).toStrictEqual('straight');
109
- });
110
- });
111
-
112
- describe('disposeImageResource', () => {
113
- it('releases the element, data, and compressed payload and marks the resource changed', () => {
114
- const resource = createImageResource({ width: 4, height: 5 } as HTMLImageElement);
115
- resource.data = new Uint8ClampedArray(4 * 5 * 4);
116
- resource.compressed = makeCompressed();
117
- const before = resource.version;
118
-
119
- disposeImageResource(resource);
120
-
121
- expect(resource.source).toBeNull();
122
- expect(resource.data).toBeNull();
123
- expect(resource.compressed).toBeNull();
124
- expect(resource.version).toStrictEqual(before + 1);
125
- });
126
- });
127
-
128
- describe('getImageResourceByteSize', () => {
129
- it('returns 0 when data is null (element-only resource)', () => {
130
- const resource = createImageResource();
131
- expect(getImageResourceByteSize(resource)).toBe(0);
132
- });
133
-
134
- it('returns the byteLength of the data array when present', () => {
135
- const resource = createImageResource();
136
- resource.data = new Uint8ClampedArray(100);
137
- expect(getImageResourceByteSize(resource)).toBe(100);
138
- });
139
-
140
- it('reflects width × height × 4 for a typical rgba8unorm resource', () => {
141
- const resource = createImageResource();
142
- resource.width = 4;
143
- resource.height = 4;
144
- resource.data = new Uint8ClampedArray(4 * 4 * 4);
145
- expect(getImageResourceByteSize(resource)).toBe(64);
146
- });
147
- });
148
-
149
- describe('hasImageResourceData', () => {
150
- it('is false without data and true with it', () => {
151
- const resource = createImageResource();
152
- expect(hasImageResourceData(resource)).toStrictEqual(false);
153
- resource.data = new Uint8ClampedArray(4);
154
- expect(hasImageResourceData(resource)).toStrictEqual(true);
155
- });
156
- });
157
-
158
- describe('hasImageResourcePixels', () => {
159
- it('is false when neither representation is present', () => {
160
- expect(hasImageResourcePixels(createImageResource())).toStrictEqual(false);
161
- });
162
-
163
- it('is true for a data-only resource (a generated Surface)', () => {
164
- const resource = createImageResource();
165
- resource.data = new Uint8ClampedArray(4);
166
- expect(hasImageResourcePixels(resource)).toStrictEqual(true);
167
- });
168
-
169
- it('is true for an element-only resource', () => {
170
- expect(hasImageResourcePixels(createImageResource({ width: 1, height: 1 } as HTMLImageElement))).toStrictEqual(
171
- true,
172
- );
173
- });
174
-
175
- it('is true for a compressed-only resource (a parsed container)', () => {
176
- expect(hasImageResourcePixels(createCompressedImageResource(makeCompressed()))).toStrictEqual(true);
177
- });
178
- });
179
-
180
- describe('hasImageResourceSource', () => {
181
- it('is false without an element and true with one', () => {
182
- expect(hasImageResourceSource(createImageResource())).toStrictEqual(false);
183
- expect(hasImageResourceSource(createImageResource({ width: 1, height: 1 } as HTMLImageElement))).toStrictEqual(
184
- true,
185
- );
57
+ it('wraps one host source with its dimensions and declared kind', () => {
58
+ const source = document.createElement('canvas');
59
+ source.width = 8;
60
+ source.height = 6;
61
+ const resource = createImageResource(source);
62
+ expect(resource.kind).toBe(ImageTextureBackingKind);
63
+ expect(resource.source).toBe(source);
64
+ expect(resource.width).toBe(8);
65
+ expect(resource.height).toBe(6);
186
66
  });
187
67
  });
188
68
 
189
69
  describe('invalidateImageResource', () => {
190
- it('increments the version', () => {
191
- const resource = createImageResource();
70
+ it('refreshes host dimensions and advances the version', () => {
71
+ const source = document.createElement('canvas');
72
+ const resource = createImageResource(source);
73
+ source.width = 12;
74
+ source.height = 9;
192
75
  invalidateImageResource(resource);
76
+ expect(resource.width).toBe(12);
77
+ expect(resource.height).toBe(9);
193
78
  expect(resource.version).toBe(1);
194
- invalidateImageResource(resource);
195
- expect(resource.version).toBe(2);
196
- });
197
-
198
- it('wraps around with >>> 0', () => {
199
- const resource = createImageResource();
200
- resource.version = 0xffffffff;
201
- invalidateImageResource(resource);
202
- expect(resource.version).toBe(0);
203
79
  });
204
80
  });
205
81
 
206
82
  describe('isImageResourceEmpty', () => {
207
- it('is true with no dimensions and false once sized', () => {
208
- expect(isImageResourceEmpty(createImageResource())).toStrictEqual(true);
209
- expect(isImageResourceEmpty(createImageResource({ width: 2, height: 2 } as HTMLImageElement))).toStrictEqual(false);
210
- });
211
- });
212
-
213
- describe('setImageResourceSource', () => {
214
- it('swaps the element, re-reads dimensions, and marks the resource changed', () => {
215
- const resource = createImageResource();
216
- const before = resource.version;
217
- const element = { width: 8, height: 6 } as HTMLImageElement;
218
-
219
- setImageResourceSource(resource, element);
220
-
221
- expect(resource.source).toBe(element);
222
- expect(resource.width).toStrictEqual(8);
223
- expect(resource.height).toStrictEqual(6);
224
- expect(resource.version).toStrictEqual(before + 1);
225
- });
226
-
227
- it('clears the element when passed null', () => {
228
- const resource = createImageResource({ width: 8, height: 6 } as HTMLImageElement);
229
-
230
- setImageResourceSource(resource, null);
231
-
232
- expect(resource.source).toBeNull();
83
+ it('reports a zero-sized host image', () => {
84
+ expect(isImageResourceEmpty(createImageResource(globalThis.document.createElement('img')))).toBe(true);
233
85
  });
234
86
  });
@@ -1,6 +1,10 @@
1
+ import { createEntity } from '@flighthq/entity/contract';
2
+ import type { Bitmap } from '@flighthq/types/contract';
3
+ import { BitmapTextureBackingKind } from '@flighthq/types/contract';
4
+
1
5
  import { createImageResource } from './imageResource';
2
6
  import {
3
- createCanvasFromImageResource,
7
+ createImageResourceFromBitmap,
4
8
  createImageResourceFromCanvas,
5
9
  createImageResourceFromImageBitmap,
6
10
  createImageResourceFromImageElement,
@@ -21,29 +25,24 @@ afterEach(() => {
21
25
  delete (HTMLImageElement.prototype as Partial<HTMLImageElement>).decode;
22
26
  });
23
27
 
24
- describe('createCanvasFromImageResource', () => {
25
- function makeDataResource(width: number, height: number) {
26
- const resource = createImageResource();
27
- resource.width = width;
28
- resource.height = height;
29
- resource.data = new Uint8ClampedArray(width * height * 4).fill(255);
30
- return resource;
31
- }
32
-
33
- it('materializes a canvas sized to the resource', () => {
34
- const canvas = createCanvasFromImageResource(makeDataResource(6, 5));
35
- expect(canvas).toBeInstanceOf(HTMLCanvasElement);
36
- expect(canvas!.width).toBe(6);
37
- expect(canvas!.height).toBe(5);
38
- });
39
-
40
- it('returns a new canvas each call', () => {
41
- const resource = makeDataResource(4, 4);
42
- expect(createCanvasFromImageResource(resource)).not.toBe(createCanvasFromImageResource(resource));
43
- });
44
-
45
- it('returns null for an element-only resource with no data', () => {
46
- expect(createCanvasFromImageResource(createImageResource(document.createElement('img')))).toBeNull();
28
+ describe('createImageResourceFromBitmap', () => {
29
+ it('returns an ImageResource with matching dimensions', () => {
30
+ // Built with createEntity rather than @flighthq/bitmap's createBitmap: bitmap depends on image,
31
+ // so importing it here would force a circular tsconfig project reference.
32
+ const bitmap: Bitmap = createEntity({
33
+ alphaType: 'straight',
34
+ colorSpace: 'srgb',
35
+ data: new Uint8ClampedArray(4 * 4 * 4),
36
+ format: 'rgba8unorm',
37
+ height: 4,
38
+ kind: BitmapTextureBackingKind,
39
+ version: 0,
40
+ width: 4,
41
+ });
42
+ const resource = createImageResourceFromBitmap(bitmap);
43
+ expect(resource.width).toBe(4);
44
+ expect(resource.height).toBe(4);
45
+ expect(resource.source).not.toBeNull();
47
46
  });
48
47
  });
49
48