Skip to content

Commit

Permalink
[HTML5] Make Image.at work on images created from files after calling…
Browse files Browse the repository at this point in the history
… lock/unlock
  • Loading branch information
RobDangerous committed Oct 26, 2022
1 parent 55fbc76 commit 5c27cd7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
12 changes: 6 additions & 6 deletions Backends/HTML5/kha/Image.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Image implements Canvas implements Resource {
if (SystemImpl.gl == null)
return new CanvasImage(width, height, format, false);
else
return new WebGLImage(width, height, format, false, DepthStencilFormat.NoDepthAndStencil, 1);
return new WebGLImage(width, height, format, false, DepthStencilFormat.NoDepthAndStencil, 1, false);
}

public static function create3D(width: Int, height: Int, depth: Int, format: TextureFormat = null, usage: Usage = null): Image {
Expand All @@ -31,7 +31,7 @@ class Image implements Canvas implements Resource {
if (SystemImpl.gl == null)
return new CanvasImage(width, height, format, true);
else
return new WebGLImage(width, height, format, true, depthStencil, antiAliasingSamples);
return new WebGLImage(width, height, format, true, depthStencil, antiAliasingSamples, false);
}

public static function fromCanvas(canvas: CanvasElement): Image {
Expand All @@ -42,7 +42,7 @@ class Image implements Canvas implements Resource {
return img;
}
else {
var img = new WebGLImage(canvas.width, canvas.height, TextureFormat.RGBA32, false, DepthStencilFormat.NoDepthAndStencil, 1);
var img = new WebGLImage(canvas.width, canvas.height, TextureFormat.RGBA32, false, DepthStencilFormat.NoDepthAndStencil, 1, false);
img.image = canvas;
img.createTexture();
return img;
Expand All @@ -57,7 +57,7 @@ class Image implements Canvas implements Resource {
return img;
}
else {
var img = new WebGLImage(image.width, image.height, TextureFormat.RGBA32, false, DepthStencilFormat.NoDepthAndStencil, 1);
var img = new WebGLImage(image.width, image.height, TextureFormat.RGBA32, false, DepthStencilFormat.NoDepthAndStencil, 1, readable);
img.image = image;
img.createTexture();
return img;
Expand All @@ -70,7 +70,7 @@ class Image implements Canvas implements Resource {
if (usage == null)
usage = Usage.StaticUsage;
if (SystemImpl.gl != null) {
var img = new WebGLImage(width, height, format, false, DepthStencilFormat.NoDepthAndStencil, 1);
var img = new WebGLImage(width, height, format, false, DepthStencilFormat.NoDepthAndStencil, 1, false);
img.image = img.bytesToArray(bytes);
img.createTexture();
return img;
Expand Down Expand Up @@ -107,7 +107,7 @@ class Image implements Canvas implements Resource {
}
else {
var img = new WebGLImage(jsvideo.element.videoWidth, jsvideo.element.videoHeight, TextureFormat.RGBA32, false, DepthStencilFormat.NoDepthAndStencil,
1);
1, false);
img.video = jsvideo.element;
img.createTexture();
return img;
Expand Down
41 changes: 29 additions & 12 deletions Backends/HTML5/kha/WebGLImage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class WebGLImage extends Image {

var depthStencilFormat: DepthStencilFormat;

var readable: Bool;

// WebGL2 constants
static inline var GL_RGBA16F = 0x881A;
static inline var GL_RGBA32F = 0x8814;
Expand All @@ -68,12 +70,13 @@ class WebGLImage extends Image {
}
}

public function new(width: Int, height: Int, format: TextureFormat, renderTarget: Bool, depthStencilFormat: DepthStencilFormat, samples: Int) {
public function new(width: Int, height: Int, format: TextureFormat, renderTarget: Bool, depthStencilFormat: DepthStencilFormat, samples: Int, readable: Bool) {
myWidth = width;
myHeight = height;
myFormat = format;
this.renderTarget = renderTarget;
this.samples = samples;
this.readable = readable;
image = null;
video = null;
this.depthStencilFormat = depthStencilFormat;
Expand Down Expand Up @@ -138,19 +141,29 @@ class WebGLImage extends Image {
}

override public function at(x: Int, y: Int): Color {
if (data == null) {
if (context == null)
return Color.Black;
else
createImageData();
if (bytes != null) {
var r = bytes.get(y * width * 4 + x * 4);
var g = bytes.get(y * width * 4 + x * 4 + 1);
var b = bytes.get(y * width * 4 + x * 4 + 2);
var a = bytes.get(y * width * 4 + x * 4 + 3);

return Color.fromValue((a << 24) | (r << 16) | (g << 8) | b);
}
else {
if (data == null) {
if (context == null)
return Color.Black;
else
createImageData();
}

var r = data.data[y * width * 4 + x * 4];
var g = data.data[y * width * 4 + x * 4 + 1];
var b = data.data[y * width * 4 + x * 4 + 2];
var a = data.data[y * width * 4 + x * 4 + 3];
var r = data.data[y * width * 4 + x * 4];
var g = data.data[y * width * 4 + x * 4 + 1];
var b = data.data[y * width * 4 + x * 4 + 2];
var a = data.data[y * width * 4 + x * 4 + 3];

return Color.fromValue((a << 24) | (r << 16) | (g << 8) | b);
return Color.fromValue((a << 24) | (r << 16) | (g << 8) | b);
}
}

function createImageData() {
Expand Down Expand Up @@ -431,6 +444,7 @@ class WebGLImage extends Image {

override public function unlock(): Void {
data = null;
image = null;

if (SystemImpl.gl != null) {
texture = SystemImpl.gl.createTexture();
Expand Down Expand Up @@ -478,7 +492,10 @@ class WebGLImage extends Image {
}

SystemImpl.gl.bindTexture(GL.TEXTURE_2D, null);
bytes = null;

if (!readable) {
bytes = null;
}
}
}

Expand Down

0 comments on commit 5c27cd7

Please sign in to comment.