magick icon indicating copy to clipboard operation
magick copied to clipboard

Question: Memory layout of Class 'magick-image' <externalptr>

Open ramiromagno opened this issue 1 year ago • 2 comments

Hi Jeroen,

I would like to use {magick} to conveniently import images into R and pass the image data into a function at C level. Is this possible?

My use case is passing image data into the OpenGL function glTexImage2D(..., data) to load a texture into the GPU, where data is the address of the image data. So, is there a safe way of getting the address of a 'magick-image' , and perhaps also access its metadata (such as number of channels, depth, dimensions)?

Dankje!

ramiromagno avatar Sep 01 '22 14:09 ramiromagno

Nevermind, I realised just now that image_data() gives exactly what I wanted.

ramiromagno avatar Sep 01 '22 15:09 ramiromagno

I am getting Professor Frink upside down and a small white rectangle...

# Load image using `{magick}`
img <- image_read("https://jeroen.github.io/images/frink.png")
data <- image_data(img)
img_width <- dim(data)[2]
img_height <- dim(data)[3]

gl_tex_image_2d(GL$TEXTURE_2D, 0L, GL$RGBA, img_width, img_height, 0L, GL$RGBA, GL$UNSIGNED_BYTE, data)
gl_generate_mipmap(GL$TEXTURE_2D)

snap

ramiromagno avatar Sep 01 '22 15:09 ramiromagno