Skip to content

Commit 53f9a1a

Browse files
authored
Merge pull request #829 from dragonfly-ai/patch-1
Added missing constructors to ImageData facade.
2 parents e7165a5 + 34d6117 commit 53f9a1a

File tree

7 files changed

+131
-3
lines changed

7 files changed

+131
-3
lines changed

Diff for: api-reports/2_12.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -15838,9 +15838,10 @@ ImageBitmap[JT] def width: Double
1583815838
ImageCapture[JC] def grabFrame(): js.Promise[ImageBitmap]
1583915839
ImageCapture[JC] def takePhoto(): js.Promise[Blob]
1584015840
ImageCapture[JC] val track: MediaStreamTrack
15841-
ImageData[JC] def data: js.typedarray.Uint8ClampedArray
15841+
ImageData[JC] def data: Uint8ClampedArray
1584215842
ImageData[JC] def height: Int
1584315843
ImageData[JC] def width: Int
15844+
ImageDataSettings[JT] var colorSpace: js.UndefOr[PredefinedColorSpace]
1584415845
InputEvent[JC] def bubbles: Boolean
1584515846
InputEvent[JC] def cancelBubble: Boolean
1584615847
InputEvent[JC] def cancelable: Boolean
@@ -17397,6 +17398,9 @@ PositionError[JT] def message: String
1739717398
PositionOptions[JC] var enableHighAccuracy: Boolean
1739817399
PositionOptions[JC] var maximumAge: Int
1739917400
PositionOptions[JC] var timeout: Int
17401+
PredefinedColorSpace[JT]
17402+
PredefinedColorSpace[SO] val `display-p3`: PredefinedColorSpace
17403+
PredefinedColorSpace[SO] val srgb: PredefinedColorSpace
1740017404
PresentationStyle[JT]
1740117405
PresentationStyle[SO] val attachment: PresentationStyle
1740217406
PresentationStyle[SO] val inline: PresentationStyle

Diff for: api-reports/2_13.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -15838,9 +15838,10 @@ ImageBitmap[JT] def width: Double
1583815838
ImageCapture[JC] def grabFrame(): js.Promise[ImageBitmap]
1583915839
ImageCapture[JC] def takePhoto(): js.Promise[Blob]
1584015840
ImageCapture[JC] val track: MediaStreamTrack
15841-
ImageData[JC] def data: js.typedarray.Uint8ClampedArray
15841+
ImageData[JC] def data: Uint8ClampedArray
1584215842
ImageData[JC] def height: Int
1584315843
ImageData[JC] def width: Int
15844+
ImageDataSettings[JT] var colorSpace: js.UndefOr[PredefinedColorSpace]
1584415845
InputEvent[JC] def bubbles: Boolean
1584515846
InputEvent[JC] def cancelBubble: Boolean
1584615847
InputEvent[JC] def cancelable: Boolean
@@ -17397,6 +17398,9 @@ PositionError[JT] def message: String
1739717398
PositionOptions[JC] var enableHighAccuracy: Boolean
1739817399
PositionOptions[JC] var maximumAge: Int
1739917400
PositionOptions[JC] var timeout: Int
17401+
PredefinedColorSpace[JT]
17402+
PredefinedColorSpace[SO] val `display-p3`: PredefinedColorSpace
17403+
PredefinedColorSpace[SO] val srgb: PredefinedColorSpace
1740017404
PresentationStyle[JT]
1740117405
PresentationStyle[SO] val attachment: PresentationStyle
1740217406
PresentationStyle[SO] val inline: PresentationStyle
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.scalajs.dom
2+
3+
import scala.scalajs.js
4+
5+
@js.native
6+
sealed trait PredefinedColorSpace extends js.Any
7+
8+
object PredefinedColorSpace {
9+
val srgb: PredefinedColorSpace = "srgb".asInstanceOf[PredefinedColorSpace]
10+
11+
val `display-p3`: PredefinedColorSpace = "display-p3".asInstanceOf[PredefinedColorSpace]
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.scalajs.dom
2+
3+
import scala.scalajs.js
4+
5+
opaque type PredefinedColorSpace <: String = String
6+
7+
object PredefinedColorSpace {
8+
val srgb: PredefinedColorSpace = "srgb"
9+
10+
val `display-p3`: PredefinedColorSpace = "display-p3"
11+
}

Diff for: dom/src/main/scala/org/scalajs/dom/ImageData.scala

+51-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package org.scalajs.dom
88

99
import scala.scalajs.js
1010
import scala.scalajs.js.annotation._
11+
import scala.scalajs.js.typedarray.Uint8ClampedArray
1112

1213
/** The ImageData interface represents the underlying pixel data of an area of a &lt;canvas&gt; element. It is created
1314
* using creators on the CanvasRenderingContext2D object associated with the canvas createImageData() and
@@ -17,14 +18,63 @@ import scala.scalajs.js.annotation._
1718
@JSGlobal
1819
class ImageData extends js.Object {
1920

21+
/** Create an ImageData instance from an array of pixel data and a width.
22+
* @param data
23+
* pixel data
24+
* @param width
25+
* width in pixels
26+
*/
27+
def this(data: Uint8ClampedArray, width: Int) = this()
28+
29+
/** Create an ImageData instance from an array of pixel data, width, and height.
30+
* @param data
31+
* pixel data
32+
* @param width
33+
* width in pixels
34+
* @param height
35+
* height in pixels
36+
*/
37+
def this(data: Uint8ClampedArray, width: Int, height: Int) = this()
38+
39+
/** Create a blank ImageData instance from specified width and height.
40+
* @param width
41+
* width in pixels
42+
* @param height
43+
* height in pixels
44+
*/
45+
def this(width: Int, height: Int) = this()
46+
47+
/** Create a blank ImageData instance from specified width, height, and settings object.
48+
* @param width
49+
* width in pixels
50+
* @param height
51+
* height in pixels
52+
* @param settings
53+
* image settings
54+
*/
55+
def this(width: Int, height: Int, settings: ImageDataSettings) = this()
56+
57+
/** Create a blank ImageData instance from specified pixel data, width, height, and settings object.
58+
* @param data
59+
* pixel data
60+
* @param width
61+
* width in pixels
62+
* @param height
63+
* height in pixels
64+
* @param settings
65+
* image settings
66+
*/
67+
def this(data: Uint8ClampedArray, width: Int, height: Int, settings: ImageDataSettings) = this()
68+
2069
/** Is an unsigned long representing the actual width, in pixels, of the ImageData. */
2170
def width: Int = js.native
2271

2372
/** Is a Uint8ClampedArray representing a one-dimensional array containing the data in the RGBA order, with integer
2473
* values between 0 and 255 (included).
2574
*/
26-
def data: js.typedarray.Uint8ClampedArray = js.native
75+
def data: Uint8ClampedArray = js.native
2776

2877
/** Is an unsigned long representing the actual height, in pixels, of the ImageData. */
2978
def height: Int = js.native
79+
3080
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.scalajs.dom
2+
3+
import scala.scalajs.js
4+
5+
trait ImageDataSettings extends js.Object {
6+
var colorSpace: js.UndefOr[PredefinedColorSpace] = js.undefined
7+
}

Diff for: tests-shared/src/main/scala/org/scalajs/dom/tests/shared/BrowserTests.scala

+40
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,44 @@ trait BrowserTests extends WebCryptoApiTests {
128128
_ <- reasonPromise.future.map(assertEquals(expectedReason, _))
129129
} yield ()
130130
}
131+
132+
@Test
133+
final def ImageDataTest(): Unit = {
134+
// Test ImageDataConstructors
135+
// from https://developer.mozilla.org/en-US/docs/Web/API/ImageData/ImageData
136+
137+
import org.scalajs.dom.{ImageData, ImageDataSettings, PredefinedColorSpace}
138+
import PredefinedColorSpace._
139+
140+
val width:Int = 200
141+
val height:Int = 100
142+
143+
// new ImageData(width, height)
144+
val imgDat1: ImageData = new ImageData(width, height)
145+
assertEquals(imgDat1.width, width)
146+
assertEquals(imgDat1.height, height)
147+
assertEquals(imgDat1.data.length, width * height * 4)
148+
149+
// new ImageData(width, height, settings) // Firefox doesn't support this constructor.
150+
// val defaultImageData: ImageData = new ImageData(width, height, new ImageDataSettings { colorSpace = `display-p3` })
151+
// assertEquals(defaultImageData.width, width)
152+
// assertEquals(defaultImageData.height, height)
153+
154+
// new ImageData(dataArray, width)
155+
val imgDat2: ImageData = new ImageData(imgDat1.data, width)
156+
assertEquals(imgDat2.width, width)
157+
assertEquals(imgDat2.height, height)
158+
assertEquals(imgDat2.data.length, width * height * 4)
159+
160+
// new ImageData(dataArray, width, height)
161+
val imgDat3: ImageData = new ImageData(imgDat2.data, width, height)
162+
assertEquals(imgDat3.width, width)
163+
assertEquals(imgDat3.height, height)
164+
assertEquals(imgDat3.data.length, width * height * 4)
165+
166+
// new ImageData(dataArray, width, height, settings)
167+
val defaultImageData: ImageData = new ImageData(imgDat3.data, width, height, new ImageDataSettings { colorSpace = `display-p3` })
168+
assertEquals(defaultImageData.width, width)
169+
assertEquals(defaultImageData.height, height)
170+
}
131171
}

0 commit comments

Comments
 (0)