Skip to content

Commit 08076f0

Browse files
Add ImageData Float16Array support
Add the ImageDataPixelFormat enum type, which can take on values of "rgba-unorm8" (the current default of 8-bit unsigned normalized RGBA) and "rgba-float16" (the new option for 16-bit floating point RGBA). Add ImageDataPixelFormat to the ImageDataSettings dictionary used by ImageData constructors, to allow creation of ImageData objects that are backed by Float16Array. Add ImageDataPixelFormat as an attribute to ImageData. Add a new ImageDataArray type which is the union of Uint8ClampedArray and Float16Array. Change the ImageData data attribute to be this union type instead of Uint8ClampedArray. Fix the Canvas Pixel ArrayBuffer section to correctly describe the pixel layout of an ImageBuffer (there was a bug in the spec specifying that an ImageData's pixel data starts at the beginning of its data attributes's backing ArrayBuffer, when what was intended was for it to start at the beginning of its data attribute). Fixes #10856
1 parent 85effea commit 08076f0

File tree

1 file changed

+101
-35
lines changed

1 file changed

+101
-35
lines changed

source

+101-35
Original file line numberDiff line numberDiff line change
@@ -2907,6 +2907,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
29072907
<li><dfn data-x="idl-DOMString" data-x-href="https://webidl.spec.whatwg.org/#idl-DOMString"><code>DOMString</code></dfn></li>
29082908
<li><dfn data-x="idl-double" data-x-href="https://webidl.spec.whatwg.org/#idl-double"><code>double</code></dfn></li>
29092909
<li><dfn data-x="IDL enumeration" data-x-href="https://webidl.spec.whatwg.org/#idl-enums">enumeration</dfn></li>
2910+
<li><dfn data-x="idl-Float16Array" data-x-href="https://webidl.spec.whatwg.org/#idl-Float16Array"><code>Float16Array</code></dfn></li>
29102911
<li><dfn data-x="idl-Function" data-x-href="https://webidl.spec.whatwg.org/#common-Function"><code>Function</code></dfn></li>
29112912
<li><dfn data-x="idl-long" data-x-href="https://webidl.spec.whatwg.org/#idl-long"><code>long</code></dfn></li>
29122913
<li><dfn data-x="idl-object" data-x-href="https://webidl.spec.whatwg.org/#idl-object"><code>object</code></dfn></li>
@@ -66035,19 +66036,25 @@ interface <dfn interface>TextMetrics</dfn> {
6603566036
readonly attribute double <span data-x="dom-textmetrics-ideographicBaseline">ideographicBaseline</span>;
6603666037
};
6603766038

66039+
typedef (<span data-x="idl-Uint8ClampedArray">Uint8ClampedArray</span> or <span data-x="idl-Float16Array">Float16Array</span>) <dfn typedef>ImageDataArray</dfn>;
66040+
66041+
enum <dfn enum>ImageDataPixelFormat</dfn> { "<span data-x="dom-ImageDataPixelFormat-rgba-unorm8">rgba-unorm8</span>", "<span data-x="dom-ImageDataPixelFormat-rgba-float16">rgba-float16</span>" };
66042+
6603866043
dictionary <dfn dictionary>ImageDataSettings</dfn> {
6603966044
<span>PredefinedColorSpace</span> <span data-x="dom-PredefinedColorSpace-srgb">colorSpace</span>;
66045+
<span>ImageDataPixelFormat</span> <span data-x="dom-ImageDataSettings-pixelFormat">pixelFormat</span> = "<span data-x="dom-ImageDataPixelFormat-rgba-unorm8">rgba-unorm8</span>";
6604066046
};
6604166047

6604266048
[Exposed=(Window,Worker),
6604366049
<span>Serializable</span>]
6604466050
interface <dfn interface>ImageData</dfn> {
6604566051
<span data-x="dom-imagedata">constructor</span>(unsigned long sw, unsigned long sh, optional <span>ImageDataSettings</span> settings = {});
66046-
<span data-x="dom-imagedata-with-data">constructor</span>(<span data-x="idl-Uint8ClampedArray">Uint8ClampedArray</span> data, unsigned long sw, optional unsigned long sh, optional <span>ImageDataSettings</span> settings = {});
66052+
<span data-x="dom-imagedata-with-data">constructor</span>(<span>ImageDataArray</span> data, unsigned long sw, optional unsigned long sh, optional <span>ImageDataSettings</span> settings = {});
6604766053

6604866054
readonly attribute unsigned long <span data-x="dom-imagedata-width">width</span>;
6604966055
readonly attribute unsigned long <span data-x="dom-imagedata-height">height</span>;
66050-
readonly attribute <span data-x="idl-Uint8ClampedArray">Uint8ClampedArray</span> <span data-x="dom-imagedata-data">data</span>;
66056+
readonly attribute <span>ImageDataArray</span> <span data-x="dom-imagedata-data">data</span>;
66057+
readonly attribute <span>ImageDataPixelFormat</span> <span data-x="dom-imagedata-pixelFormat">pixelFormat</span>;
6605166058
readonly attribute <span>PredefinedColorSpace</span> <span data-x="dom-imagedata-colorSpace">colorSpace</span>;
6605266059
};
6605366060

@@ -70400,9 +70407,8 @@ try {
7040070407

7040170408
<dt><code data-x=""><var>imageData</var> = new <span subdfn data-x="dom-imagedata-with-data">ImageData</span>(<var>data</var>, <var>sw</var> [, <var>sh</var> [, <var>settings</var> ] ])</code></dt>
7040270409
<dd>
70403-
<p>Returns an <code>ImageData</code> object using the data provided in the <code
70404-
data-x="idl-Uint8ClampedArray">Uint8ClampedArray</code> argument, interpreted using the given
70405-
dimensions and the color space indicated by <var>settings</var>.</p>
70410+
<p>Returns an <code>ImageData</code> object using the data provided in the <code>ImageDataArray</code>
70411+
argument, interpreted using the given dimensions and the color space indicated by <var>settings</var>.</p>
7040670412

7040770413
<p>As each pixel in the data is represented by four numbers, the length of the data needs to be
7040870414
a multiple of four times the given width. If the height is provided as well, then the length
@@ -70491,7 +70497,7 @@ try {
7049170497
constructor steps are:</p>
7049270498

7049370499
<ol>
70494-
<li><p>Let <var>length</var> be the number of bytes in <var>data</var>.</p></li>
70500+
<li><p>Let <var>length</var> be the length of <var>data</var>.</p></li>
7049570501

7049670502
<li><p>If <var>length</var> is not a nonzero integral multiple of four, then throw an
7049770503
<span>"<code>InvalidStateError</code>"</span> <code>DOMException</code>.</p></li>
@@ -70519,8 +70525,7 @@ try {
7051970525
<i><span data-x="initialize-imagedata-source">source</span></i> set to <var>data</var>.</p>
7052070526

7052170527
<p class="note">This step does not set <span>this</span>'s data to a copy of <var>data</var>.
70522-
It sets it to the actual <code data-x="idl-Uint8ClampedArray">Uint8ClampedArray</code> object
70523-
passed as <var>data</var>.</p>
70528+
It sets it to the actual <code>ImageData</code> object passed as <var>data</var>.</p>
7052470529
</li>
7052570530
</ol>
7052670531

@@ -70609,30 +70614,61 @@ try {
7060970614
<p>To <dfn>initialize an <code>ImageData</code> object</dfn> <var>imageData</var>, given a
7061070615
positive integer number of rows <var>rows</var>, a positive integer number of pixels per row
7061170616
<var>pixelsPerRow</var>, an optional <code>ImageDataSettings</code> <dfn
70612-
data-x="initialize-imagedata-settings"><var>settings</var></dfn>, an optional <code
70613-
data-x="idl-Uint8ClampedArray">Uint8ClampedArray</code> <dfn
70614-
data-x="initialize-imagedata-source"><var>source</var></dfn>, and an optional
70615-
<code>PredefinedColorSpace</code> <dfn
70617+
data-x="initialize-imagedata-settings"><var>settings</var></dfn>, an optional
70618+
<code>ImageDataArray</code> <dfn data-x="initialize-imagedata-source"><var>source</var></dfn>,
70619+
and an optional <code>PredefinedColorSpace</code> <dfn
7061670620
data-x="initialize-imagedata-defaultcolorspace"><var>defaultColorSpace</var></dfn>:</p>
7061770621

7061870622
<ol>
70619-
<li><p>If <var>source</var> was given, then initialize the <dfn attribute for="ImageData"><code
70620-
data-x="dom-imagedata-data">data</code></dfn> attribute of <var>imageData</var> to
70621-
<var>source</var>.</p></li>
70623+
<li><p>If <var>source</var> was given then:</p>
70624+
<ol>
70625+
<li><p>If <var>settings</var> was given and
70626+
<var>settings</var>["<code data-x="dom-ImageDataSettings-pixelFormat">pixelFormat</code>"]
70627+
equals "<span data-x="dom-ImageDataPixelFormat-rgba-unorm8">rgba-unorm8</span>" and
70628+
<var>source</var> is not of type <code data-x="idl-Uint8ClampedArray">Uint8ClampedArray</code>
70629+
then throw an <span>"<code>InvalidStateError</code>"</span> <code>DOMException</code>.</p></li>
7062270630

70623-
<li>
70624-
<p>Otherwise (<var>source</var> was not given), initialize the <code
70625-
data-x="dom-imagedata-data">data</code> attribute of <var>imageData</var> to a new <code
70626-
data-x="idl-Uint8ClampedArray">Uint8ClampedArray</code> object. The <code
70627-
data-x="idl-Uint8ClampedArray">Uint8ClampedArray</code> object must use a new <span>Canvas
70628-
Pixel <code data-x="idl-ArrayBuffer">ArrayBuffer</code></span> for its storage, and must have a
70629-
zero start offset and a length equal to the length of its storage, in bytes. The <span>Canvas
70630-
Pixel <code data-x="idl-ArrayBuffer">ArrayBuffer</code></span> must have the correct size to
70631-
store <var>rows</var> × <var>pixelsPerRow</var> pixels.</p>
70631+
<li><p>If <var>settings</var> was given and
70632+
<var>settings</var>["<code data-x="dom-ImageDataSettings-pixelFormat">pixelFormat</code>"]
70633+
equals "<span data-x="dom-ImageDataPixelFormat-rgba-float16">rgba-float16</span>" and
70634+
<var>source</var> is not of type <code data-x="idl-Float16Array">Float16Array</code>
70635+
then throw an <span>"<code>InvalidStateError</code>"</span> <code>DOMException</code>.</p></li>
7063270636

70633-
<p>If the <span>Canvas Pixel <code data-x="idl-ArrayBuffer">ArrayBuffer</code></span> cannot be
70634-
allocated, then rethrow the <code data-x="js-RangeError">RangeError</code> thrown by JavaScript,
70635-
and return.</p>
70637+
<li><p>Initialize the <dfn attribute for="ImageData"><code
70638+
data-x="dom-imagedata-data">data</code></dfn> attribute of <var>imageData</var> to
70639+
<var>source</var>.</p></li>
70640+
</ol>
70641+
</li>
70642+
70643+
<li><p>Otherwise (<var>source</var> was not given):</p>
70644+
<ol>
70645+
<li><p>If <var>settings</var> was given and
70646+
<var>settings</var>["<code data-x="dom-ImageDataSettings-pixelFormat">pixelFormat</code>"]
70647+
equals "<span data-x="dom-ImageDataPixelFormat-rgba-unorm8">rgba-unorm8</span>", or if
70648+
<var>settings</var> was not given, then
70649+
initialize the <code data-x="dom-imagedata-data">data</code> attribute of <var>imageData</var> to a new
70650+
<code data-x="idl-Uint8ClampedArray">Uint8ClampedArray</code> object.
70651+
The <code data-x="idl-Uint8ClampedArray">Uint8ClampedArray</code> object
70652+
must have a length of 4 × <var>rows</var> × <var>pixelsPerRow</var> elements,
70653+
must use a new <code data-x="idl-ArrayBuffer">ArrayBuffer</code> for its storage,
70654+
must have a byte offset of zero bytes,
70655+
and must have a byte length of 4 × <var>rows</var> × <var>pixelsPerRow</var> bytes.
70656+
If the <code data-x="idl-ArrayBuffer">ArrayBuffer</code> could not be allocated, then rethrow the
70657+
<code data-x="js-RangeError">RangeError</code> thrown by JavaScript, and return.</p></li>
70658+
70659+
<li><p>If <var>settings</var> was given and
70660+
<var>settings</var>["<code data-x="dom-ImageDataSettings-pixelFormat">pixelFormat</code>"]
70661+
equals "<span data-x="dom-ImageDataPixelFormat-rgba-float16">rgba-float16</span>", then
70662+
initialize the <code data-x="dom-imagedata-data">data</code> attribute of <var>imageData</var> to a new
70663+
<code data-x="idl-Float16Array">Float16Array</code> object.
70664+
The <code data-x="idl-Float16Array">Float16Array</code> object
70665+
must have a length of 4 × <var>rows</var> × <var>pixelsPerRow</var> elements,
70666+
must use a new <code data-x="idl-ArrayBuffer">ArrayBuffer</code> for its storage,
70667+
must have a byte offset of zero bytes,
70668+
and must have a byte length of 8 × <var>rows</var> × <var>pixelsPerRow</var> bytes.
70669+
If the <code data-x="idl-ArrayBuffer">ArrayBuffer</code> could not be allocated, then rethrow the
70670+
<code data-x="js-RangeError">RangeError</code> thrown by JavaScript, and return.</p></li>
70671+
</ol>
7063670672
</li>
7063770673

7063870674
<li><p>Initialize the <dfn attribute for="ImageData"><code
@@ -70643,6 +70679,16 @@ try {
7064370679
data-x="dom-imagedata-height">height</code></dfn> attribute of <var>imageData</var> to
7064470680
<var>rows</var>.</p></li>
7064570681

70682+
<li><p>If <var>settings</var> was given, then initialize the
70683+
<dfn attribute for="ImageData"><code data-x="dom-imagedata-pixelFormat">pixelFormat</code></dfn>
70684+
attribute of <var>imageData</var> to
70685+
<var>settings</var>["<dfn dict-member for="ImageDataSettings"><code
70686+
data-x="dom-ImageDataSettings-pixelFormat">pixelFormat</code></dfn>"].</p></li>
70687+
70688+
<li><p>Otherwise, initialize the <code data-x="dom-imagedata-pixelFormat">pixelFormat</code>
70689+
attribute of <var>imageData</var> to
70690+
"<span data-x="dom-ImageDataPixelFormat-rgba-unorm8">rgba-unorm8</span>".</p></li>
70691+
7064670692
<li><p>If <var>settings</var> was given and <var>settings</var>["<code
7064770693
data-x="dom-ImageDataSettings-colorSpace">colorSpace</code>"] <span data-x="map
7064870694
exists">exists</span>, then initialize the <dfn attribute for="ImageData"><code
@@ -70693,13 +70739,33 @@ try {
7069370739
attribute to <var>serialized</var>.[[ColorSpace]].</p></li>
7069470740
</ol>
7069570741

70696-
<p>A <dfn>Canvas Pixel <code data-x="idl-ArrayBuffer">ArrayBuffer</code></dfn> is an <code
70697-
data-x="idl-ArrayBuffer">ArrayBuffer</code> whose data is represented in left-to-right order, row
70698-
by row top to bottom, starting with the top left, with each pixel's red, green, blue, and alpha
70699-
components being given in that order for each pixel. Each component of each pixel represented in
70700-
this array must be in the range 0..255, representing the 8 bit value for that component. The
70701-
components must be assigned consecutive indices starting with 0 for the top left pixel's red
70702-
component.</p>
70742+
<p>The <code>ImageDataPixelFormat</code> enumeration is used to specify type of the
70743+
<code data-x="dom-imagedata-data">data</code> attribute of an <code>ImageData</code>
70744+
and the arrangement and numerical representation of the color channel values for each pixel.</p>
70745+
70746+
<p>The "<dfn enum-value for="ImageDataPixelFormat"><code
70747+
data-x="dom-ImageDataPixelFormat-rgba-unorm8">rgba-unorm8</code></dfn>" value indicates that the
70748+
<code data-x="dom-imagedata-data">data</code> attribute of an <code>ImageData</code> must be of type
70749+
<span data-x="idl-Uint8ClampedArray">Uint8ClampedArray</span>.
70750+
The color components of each pixel must be stored in four sequential elements in the order of red, green, blue, and then alpha.
70751+
Each element represents the 8-bit unsigned normalized value for that component.</p>
70752+
70753+
<p>The "<dfn enum-value for="ImageDataPixelFormat"><code
70754+
data-x="dom-ImageDataPixelFormat-rgba-float16">rgba-float16</code></dfn>" value indicates that the
70755+
<code data-x="dom-imagedata-data">data</code> attribute of an <code>ImageData</code> must be of type
70756+
<span data-x="idl-Float16Array">Float16Array</span>.
70757+
The color components of each pixel must be stored in four sequential elements in the order of red, green, blue, and then alpha.
70758+
Each element represents the value for that component.</p>
70759+
70760+
<p>An <code>ImageData</code> object <dfn data-x="concept-imagedata-bitmap-representation">represents a rectanglar bitmap</dfn>
70761+
with width equal to the <code data-x="dom-imagedata-width">width</code> attribute
70762+
and height equal to the <code data-x="dom-imagedata-height">height</code> attribute.
70763+
The pixel values of this bitmap are stored in the <code data-x="dom-imagedata-data">data</code> attribute
70764+
in left-to-right order, row by row from top to bottom, starting with 0 for the top left pixel,
70765+
with the order and numerical representation of the color components of each
70766+
pixel determined by the <code data-x="dom-imagedata-pixelFormat">pixelFormat</code> attribute.
70767+
The color space of the pixel values of the bitmap is determined by the
70768+
<code data-x="dom-imagedata-colorSpace">colorSpace</code> attribute.</p>
7070370769

7070470770
<p>The <dfn method for="CanvasImageData"><code
7070570771
data-x="dom-context-2d-putImageData-short">putImageData(<var>imageData</var>,
@@ -70777,7 +70843,7 @@ try {
7077770843
set the pixel with coordinate (<span data-x=""><var>dx</var>+<var>x</var></span>,
7077870844
<var>dy</var>+<var>y</var>) in <var>bitmap</var> to the color of the pixel at coordinate
7077970845
(<var>x</var>, <var>y</var>) in the <var>imageData</var> data structure's
70780-
<span>Canvas Pixel <code data-x="idl-ArrayBuffer">ArrayBuffer</code></span>,
70846+
<span data-x="concept-imagedata-bitmap-representation">bitmap</span>,
7078170847
converted from <var>imageData</var>'s <code data-x="dom-imagedata-colorSpace">colorSpace</code>
7078270848
to the <span data-x="concept-canvas-color-space">color space</span> of <var>bitmap</var>
7078370849
using <span>'relative-colorimetric'</span> rendering intent.</p></li>

0 commit comments

Comments
 (0)