farver icon indicating copy to clipboard operation
farver copied to clipboard

support raw vector input?

Open mdsumner opened this issue 3 years ago • 2 comments

It seems feasible to support raw vector input, say as a 3 or 4 column matrix for RGB,RGBA. I'm reading raw direct from GDAL imagery, but it seems I need to expand to integer type to then convert to character hex or nativeRaster.

f <- system.file("img", "Rlogo.png", package="png")
info <- vapour::vapour_raster_info(f)
rawv <- lapply(1:4, function(.x) vapour::vapour_read_raster_raw(f, native = TRUE, band = .x))
plot(0:1, 0:1)
rasterImage(structure(matrix(farver::encode_native(do.call(cbind, lapply(rawv, as.integer))), 
                             info$dimXY[2]), 
                      class = "nativeRaster"), 
            0, 0, 1, 1, interpolate = FALSE)

pryr::object_size(rawv)
#> 30,672 B

pryr::object_size(lapply(rawv, as.integer))
#> 121,872 B

Created on 2022-06-30 by the reprex package (v2.0.1)

It would save some memory to avoid expansion to integer, but is there another pathway perhaps? Ultimately it's probably best if the conversion is done all in Cpp in my own read code, and I have UI to provide nativeRaster output or hex output from there - but maybe this is of interest generally.

mdsumner avatar Jun 30 '22 04:06 mdsumner

(I don't know why my alpha isn't working in the example above, but it's unrelated to the request - I'll update once I find what's wrong) 🙏

mdsumner avatar Jun 30 '22 23:06 mdsumner

ah, it's a separate input - apologies, I'lll leave the original example above just please refer to this one:

f <- system.file("img", "Rlogo.png", package="png")
info <- vapour::vapour_raster_info(f)
rawv <- lapply(1:4, function(.x) vapour::vapour_read_raster_raw(f, native = TRUE, band = .x))
plot(0:1, 0:1, type = "n")
alpha <- as.integer(rawv[[4]])
nr_object <- structure(matrix(farver::encode_native(do.call(cbind, lapply(rawv[1:3], as.integer)), alpha = alpha), 
                             info$dimXY[2]), 
                      class = "nativeRaster")
rasterImage(nr_object, 
            0, 0, 1, 1, interpolate = FALSE)

pryr::object_size(rawv)
#> 30,672 B
pryr::object_size(lapply(rawv, as.integer))
#> 121,872 B

Created on 2022-07-01 by the reprex package (v2.0.1)

I guess it's a different reqeuest now too, allow raw input for 'colour' and 'alpha' - which might have UI-implications, I'll have more of a think.

mdsumner avatar Jun 30 '22 23:06 mdsumner