這是(相當低效和醜陋)的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;
}
不能確定'GDK-pixbuf',但你可能想嘗試'libcairo ',這通常基於'gdk-pixbuf',並具有所有這些jpeg,bmp,...相關的東西例程。 – 2010-04-30 14:16:13
謝謝,但通過文檔掃描不會顯示_saving_到jpeg&c的任何函數,僅用於從它們導入。 – 2010-04-30 20:02:21