Some class-methods in Cairo.ImageSurface module are missing
After see the cairo-image-surface module, I note that there are no way to load a Cairo.ImageSurface
from a data cairo-image-surface-create-for-data, like this example from PyCairo where this functions exist as a class method:
stride = cairo.ImageSurface.format_stride_for_width (format, width)
surface = cairo.ImageSurface.create_for_data (data, format, width, height, stride)
I think the same of createFromPNG
that was used in the gjs module can be implemented to this functionality and resolve the missing support.
The context where I found this inconvenience was when trying to load an ARGB
data from an ARRAY of BYTE
as is how the icons are in the StatusNotifierItem specification. Also how the GDKPixbuf have not support of ARGB data and transform ARGB to RGBA have a big cost directly in gjs:
// This function convert a ARGB pixel format to a RGBA pixel format.
_changeARGBToRGBA: function(bytes, width, height) {
let temp;
for (let x = 4*(width*height-1); x>=0; x-=4) {
temp = bytes[x+3];
bytes[x+3] = bytes[x+0];
bytes[x+0] = bytes[x+1];
bytes[x+1] = bytes[x+2];
bytes[x+2] = temp;
}
return bytes;
},
bytes = this._changeARGBToRGBA(bytes, width, height);
let pixbuf = GdkPixbuf.Pixbuf.new_from_bytes (bytes,
GdkPixbuf.Colorspace.RGB, //Cogl.PixelFormat.ARGB_8888,
true, //has_alpha
8, //bits_per_sample
width,
height,
rowstride);
I then try to do something different to get the gicon that i want, but i discovered that also this is not yet supported:
const Cairo = imports.cairo;
const Gdk = imports.gi.Gdk;
const GdkPixbuf = imports.gi.GdkPixbuf;
function get_gicon_for_data(bytes, width, height) {
let format = Cairo.Format.ARGB32;
let stride = Cairo.ImageSurface.format_stride_for_width(format, width); //unsupported
let surface = Cairo.ImageSurface.create_for_data(bytes, format, width, height, stride); //unsupported
return Gdk.pixbuf_get_from_surface(surface, 0, 0, width, height);
}