ropensci / magick

Magic, madness, heaven, sin
https://docs.ropensci.org/magick
Other
462 stars 65 forks source link

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

Open ramiromagno opened 2 years ago

ramiromagno commented 2 years ago

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 commented 2 years ago

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

ramiromagno commented 2 years ago

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