summaryrefslogtreecommitdiff
path: root/ext/canvas/01_image.js
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2024-02-07 01:11:15 +0100
committerGitHub <noreply@github.com>2024-02-07 01:11:15 +0100
commit1007358768584856ac63dffaa82fdb00bf726bd9 (patch)
tree57ee78c3ea4dad03f12982483ded3efc5d8061af /ext/canvas/01_image.js
parent89ae2d88b8fe3e5cff0bd6023c05643fc4297686 (diff)
refactor: move ImageData to web extension (#22295)
Signed-off-by: Leo Kettmeir <crowlkats@toaxl.com>
Diffstat (limited to 'ext/canvas/01_image.js')
-rw-r--r--ext/canvas/01_image.js214
1 files changed, 7 insertions, 207 deletions
diff --git a/ext/canvas/01_image.js b/ext/canvas/01_image.js
index 3018e9e2a..844650dcb 100644
--- a/ext/canvas/01_image.js
+++ b/ext/canvas/01_image.js
@@ -13,30 +13,18 @@ const {
SymbolFor,
TypeError,
TypedArrayPrototypeGetBuffer,
- TypedArrayPrototypeGetLength,
- TypedArrayPrototypeGetSymbolToStringTag,
Uint8Array,
- Uint8ClampedArray,
MathCeil,
PromiseResolve,
PromiseReject,
RangeError,
} = primordials;
-
-webidl.converters["PredefinedColorSpace"] = webidl.createEnumConverter(
- "PredefinedColorSpace",
- [
- "srgb",
- "display-p3",
- ],
-);
-
-webidl.converters["ImageDataSettings"] = webidl.createDictionaryConverter(
- "ImageDataSettings",
- [
- { key: "colorSpace", converter: webidl.converters["PredefinedColorSpace"] },
- ],
-);
+import {
+ _data,
+ _height,
+ _width,
+ ImageDataPrototype,
+} from "ext:deno_web/16_image_data.js";
webidl.converters["ImageOrientation"] = webidl.createEnumConverter(
"ImageOrientation",
@@ -115,194 +103,6 @@ webidl.converters["ImageBitmapOptions"] = webidl.createDictionaryConverter(
],
);
-const _data = Symbol("[[data]]");
-const _width = Symbol("[[width]]");
-const _height = Symbol("[[height]]");
-class ImageData {
- /** @type {number} */
- [_width];
- /** @type {height} */
- [_height];
- /** @type {Uint8Array} */
- [_data];
- /** @type {'srgb' | 'display-p3'} */
- #colorSpace;
-
- constructor(arg0, arg1, arg2 = undefined, arg3 = undefined) {
- webidl.requiredArguments(
- arguments.length,
- 2,
- 'Failed to construct "ImageData"',
- );
- this[webidl.brand] = webidl.brand;
-
- let sourceWidth;
- let sourceHeight;
- let data;
- let settings;
- const prefix = "Failed to construct 'ImageData'";
-
- // Overload: new ImageData(data, sw [, sh [, settings ] ])
- if (
- arguments.length > 3 ||
- TypedArrayPrototypeGetSymbolToStringTag(arg0) === "Uint8ClampedArray"
- ) {
- data = webidl.converters.Uint8ClampedArray(arg0, prefix, "Argument 1");
- sourceWidth = webidl.converters["unsigned long"](
- arg1,
- prefix,
- "Argument 2",
- );
- const dataLength = TypedArrayPrototypeGetLength(data);
-
- if (webidl.type(arg2) !== "Undefined") {
- sourceHeight = webidl.converters["unsigned long"](
- arg2,
- prefix,
- "Argument 3",
- );
- }
-
- settings = webidl.converters["ImageDataSettings"](
- arg3,
- prefix,
- "Argument 4",
- );
-
- if (dataLength === 0) {
- throw new DOMException(
- "Failed to construct 'ImageData': The input data has zero elements.",
- "InvalidStateError",
- );
- }
-
- if (dataLength % 4 !== 0) {
- throw new DOMException(
- "Failed to construct 'ImageData': The input data length is not a multiple of 4.",
- "InvalidStateError",
- );
- }
-
- if (sourceWidth < 1) {
- throw new DOMException(
- "Failed to construct 'ImageData': The source width is zero or not a number.",
- "IndexSizeError",
- );
- }
-
- if (webidl.type(sourceHeight) !== "Undefined" && sourceHeight < 1) {
- throw new DOMException(
- "Failed to construct 'ImageData': The source height is zero or not a number.",
- "IndexSizeError",
- );
- }
-
- if (dataLength / 4 % sourceWidth !== 0) {
- throw new DOMException(
- "Failed to construct 'ImageData': The input data length is not a multiple of (4 * width).",
- "IndexSizeError",
- );
- }
-
- if (
- webidl.type(sourceHeight) !== "Undefined" &&
- (sourceWidth * sourceHeight * 4 !== dataLength)
- ) {
- throw new DOMException(
- "Failed to construct 'ImageData': The input data length is not equal to (4 * width * height).",
- "IndexSizeError",
- );
- }
-
- if (webidl.type(sourceHeight) === "Undefined") {
- this[_height] = dataLength / 4 / sourceWidth;
- } else {
- this[_height] = sourceHeight;
- }
-
- this.#colorSpace = settings.colorSpace ?? "srgb";
- this[_width] = sourceWidth;
- this[_data] = data;
- return;
- }
-
- // Overload: new ImageData(sw, sh [, settings])
- sourceWidth = webidl.converters["unsigned long"](
- arg0,
- prefix,
- "Argument 1",
- );
- sourceHeight = webidl.converters["unsigned long"](
- arg1,
- prefix,
- "Argument 2",
- );
-
- settings = webidl.converters["ImageDataSettings"](
- arg2,
- prefix,
- "Argument 3",
- );
-
- if (sourceWidth < 1) {
- throw new DOMException(
- "Failed to construct 'ImageData': The source width is zero or not a number.",
- "IndexSizeError",
- );
- }
-
- if (sourceHeight < 1) {
- throw new DOMException(
- "Failed to construct 'ImageData': The source height is zero or not a number.",
- "IndexSizeError",
- );
- }
-
- this.#colorSpace = settings.colorSpace ?? "srgb";
- this[_width] = sourceWidth;
- this[_height] = sourceHeight;
- this[_data] = new Uint8ClampedArray(sourceWidth * sourceHeight * 4);
- }
-
- get width() {
- webidl.assertBranded(this, ImageDataPrototype);
- return this[_width];
- }
-
- get height() {
- webidl.assertBranded(this, ImageDataPrototype);
- return this[_height];
- }
-
- get data() {
- webidl.assertBranded(this, ImageDataPrototype);
- return this[_data];
- }
-
- get colorSpace() {
- webidl.assertBranded(this, ImageDataPrototype);
- return this.#colorSpace;
- }
-
- [SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
- return inspect(
- createFilteredInspectProxy({
- object: this,
- evaluate: ObjectPrototypeIsPrototypeOf(ImageDataPrototype, this),
- keys: [
- "data",
- "width",
- "height",
- "colorSpace",
- ],
- }),
- inspectOptions,
- );
- }
-}
-
-const ImageDataPrototype = ImageData.prototype;
-
const _bitmapData = Symbol("[[bitmapData]]");
const _detached = Symbol("[[detached]]");
class ImageBitmap {
@@ -549,4 +349,4 @@ function getBitmapData(imageBitmap) {
internals.getBitmapData = getBitmapData;
-export { _bitmapData, _detached, createImageBitmap, ImageBitmap, ImageData };
+export { _bitmapData, _detached, createImageBitmap, ImageBitmap };