Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for external images using ImageData #291

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ features = [
"HtmlImageElement",
"HtmlVideoElement",
"ImageBitmap",
"ImageData",
"VideoFrame",
"WebGlActiveInfo",
"WebGlBuffer",
Expand Down
187 changes: 186 additions & 1 deletion src/web_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use js_sys::{self, Array};
use slotmap::{new_key_type, SlotMap};
use std::cell::RefCell;
use web_sys::{
self, HtmlCanvasElement, HtmlImageElement, HtmlVideoElement, ImageBitmap,
self, HtmlCanvasElement, HtmlImageElement, HtmlVideoElement, ImageBitmap, ImageData,
WebGl2RenderingContext, WebGlBuffer, WebGlFramebuffer, WebGlProgram, WebGlQuery,
WebGlRenderbuffer, WebGlRenderingContext, WebGlSampler, WebGlShader, WebGlSync, WebGlTexture,
WebGlTransformFeedback, WebGlUniformLocation, WebGlVertexArrayObject,
Expand Down Expand Up @@ -308,6 +308,7 @@ impl Context {
// - html_image
// - html_video
// - video_frame
// - image_data

pub unsafe fn tex_image_2d_with_image_bitmap(
&self,
Expand Down Expand Up @@ -666,6 +667,77 @@ impl Context {
}
}

pub unsafe fn tex_image_2d_with_image_data(
&self,
target: u32,
level: i32,
internal_format: i32,
format: u32,
ty: u32,
pixels: &ImageData,
) {
match self.raw {
RawRenderingContext::WebGl1(ref gl) => {
// TODO: Handle return value?
gl.tex_image_2d_with_u32_and_u32_and_image_data(
target,
level,
internal_format,
format,
ty,
pixels,
)
.unwrap();
}
RawRenderingContext::WebGl2(ref gl) => {
// TODO: Handle return value?
gl.tex_image_2d_with_u32_and_u32_and_image_data(
target,
level,
internal_format,
format,
ty,
pixels,
)
.unwrap();
}
}
}

/// WebGL2 Only
pub unsafe fn tex_image_2d_with_image_data_and_width_and_height(
&self,
target: u32,
level: i32,
internal_format: i32,
width: i32,
height: i32,
format: u32,
ty: u32,
pixels: &ImageData,
) {
match self.raw {
RawRenderingContext::WebGl1(_) => {
panic!("Width and height not supported")
}
RawRenderingContext::WebGl2(ref gl) => {
// TODO: Handle return value?
gl.tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_image_data(
target,
level,
internal_format,
width,
height,
0, // required to be zero
format,
ty,
pixels,
)
.unwrap();
}
}
}

pub unsafe fn tex_sub_image_2d_with_image_bitmap(
&self,
target: u32,
Expand Down Expand Up @@ -950,6 +1022,58 @@ impl Context {
}
}

pub unsafe fn tex_sub_image_2d_with_image_data(
&self,
target: u32,
level: i32,
x_offset: i32,
y_offset: i32,
format: u32,
ty: u32,
image: &ImageData,
) {
match self.raw {
RawRenderingContext::WebGl1(ref gl) => {
gl.tex_sub_image_2d_with_u32_and_u32_and_image_data(
target, level, x_offset, y_offset, format, ty, image,
)
.unwrap(); // TODO: Handle return value?
}
RawRenderingContext::WebGl2(ref gl) => {
gl.tex_sub_image_2d_with_u32_and_u32_and_image_data(
target, level, x_offset, y_offset, format, ty, image,
)
.unwrap(); // TODO: Handle return value?
}
}
}

/// WebGL2 Only
pub unsafe fn tex_sub_image_2d_with_image_data_and_width_and_height(
&self,
target: u32,
level: i32,
x_offset: i32,
y_offset: i32,
width: i32,
height: i32,
format: u32,
ty: u32,
image: &ImageData,
) {
match self.raw {
RawRenderingContext::WebGl1(_) => {
panic!("Width and height not supported")
}
RawRenderingContext::WebGl2(ref gl) => {
gl.tex_sub_image_2d_with_i32_and_i32_and_u32_and_type_and_image_data(
target, level, x_offset, y_offset, width, height, format, ty, image,
)
.unwrap(); // TODO: Handle return value?
}
}
}

/// WebGL2 Only
pub unsafe fn tex_image_3d_with_image_bitmap(
&self,
Expand Down Expand Up @@ -1121,6 +1245,40 @@ impl Context {
}
}

/// WebGL2 Only
pub unsafe fn tex_image_3d_with_image_data(
&self,
target: u32,
level: i32,
internal_format: i32,
width: i32,
height: i32,
depth: i32,
border: i32,
format: u32,
ty: u32,
image: &ImageData,
) {
match self.raw {
RawRenderingContext::WebGl1(_) => panic!("3D images not supported"),
RawRenderingContext::WebGl2(ref gl) => {
gl.tex_image_3d_with_image_data(
target,
level,
internal_format,
width,
height,
depth,
border,
format,
ty,
image,
)
.unwrap(); // TODO: Handle return value?
}
}
}

/// WebGL2 Only
pub unsafe fn tex_sub_image_3d_with_image_bitmap(
&self,
Expand Down Expand Up @@ -1266,6 +1424,33 @@ impl Context {
}
}

/// WebGL2 Only
pub unsafe fn tex_sub_image_3d_with_image_data(
&self,
target: u32,
level: i32,
x_offset: i32,
y_offset: i32,
z_offset: i32,
width: i32,
height: i32,
depth: i32,
format: u32,
ty: u32,
image: &ImageData,
) {
match self.raw {
RawRenderingContext::WebGl1(_) => panic!("3D images not supported"),
RawRenderingContext::WebGl2(ref gl) => {
gl.tex_sub_image_3d_with_image_data(
target, level, x_offset, y_offset, z_offset, width, height, depth, format, ty,
image,
)
.unwrap(); // TODO: Handle return value?
}
}
}

pub unsafe fn framebuffer_texture_multiview_ovr(
&self,
target: u32,
Expand Down
Loading