这是(相当低效和丑陋的)Vala 应用程序,它从图像中删除透明度并将其保存为指定的格式。注意:在 gdk_pixbuf_new_from_data() 的 vala 绑定中有一个小错误,会导致生成的图像损坏。我很快就会解决这个问题,但这只是为了现在的演示目的(除了问题是关于 C++ 的):
public static int main (string[] args) {
if (args.length < 4) {
print ("Usage: %s SOURCE DESTINATION FORMAT\n", args[0]);
return -1;
}
var src_path = args[1];
var destination_path = args[2];
var dest_type = args[3];
var pixbuf = new Gdk.Pixbuf.from_file_at_scale (src_path, 48, 48, false);
// Remove alpha channel
if (pixbuf.get_has_alpha () && pixbuf.get_n_channels () == 4 && pixbuf.get_bits_per_sample () == 8) {
var width = pixbuf.get_width ();
var height = pixbuf.get_height ();
var rowstride = pixbuf.get_rowstride ();
unowned uint8[] orig_pixels = pixbuf.get_pixels ();
var pixels = new uint8[rowstride * height];
for (var i = 0; i < height; i++) {
for (var j = 0, k = 0; j < width * 4; j += 4, k += 3) {
var orig_index = rowstride * i + j;
var index = rowstride * i + k;
if (orig_pixels[orig_index] == 0 &&
orig_pixels[orig_index + 1] == 0 &&
orig_pixels[orig_index + 2] == 0 &&
orig_pixels[orig_index + 3] == 0) {
pixels[index] = 0xFF;
pixels[index + 1] = 0xFF;
pixels[index + 2] = 0xFF;
} else {
pixels[index] = orig_pixels[orig_index];
pixels[index + 1] = orig_pixels[orig_index + 1];
pixels[index + 2] = orig_pixels[orig_index + 2];
}
}
}
pixbuf = new Gdk.Pixbuf.from_data (pixels,
pixbuf.get_colorspace (),
false,
8,
width,
height,
rowstride,
null);
}
pixbuf.save (destination_path, dest_type);
return 0;
}