http://homepage3.nifty.com/slokar/fb/w3mfb.patch.gz * w3mimg/fb/readme.txt: update * w3mimg/fb/fb.c: update * w3mimg/fb/fb.h: update * w3mimg/fb/fb_gdkpixbuf.c: update * w3mimg/fb/fb_img.c: update * w3mimg/fb/fb_img.h: update * w3mimg/fb/fb_imlib2.c: update * w3mimg/fb/fb_w3mimg.c: update * w3mimg/fb/fb_gdkpixbuf.h: deleted * w3mimg/fb/fb_imlib2.h: deleted * w3mimg/w3mimg.h (w3mimg_op): add get_image_size() * w3mimg/x11/x11_w3mimg.c: update * w3mimgdisplay.c (main): use get_image_size() * w3mimgsize.c (main): use get_image_size() From: Hiroyuki Ito <hito@crl.go.jp>
124 lines
2.6 KiB
C
124 lines
2.6 KiB
C
/* $Id: fb_gdkpixbuf.c,v 1.6 2002/07/22 16:17:32 ukai Exp $ */
|
|
/**************************************************************************
|
|
fb_gdkpixbuf.c 0.3 Copyright (C) 2002, hito
|
|
**************************************************************************/
|
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
|
#include "fb.h"
|
|
#include "fb_img.h"
|
|
|
|
static void draw(FB_IMAGE * img, GdkPixbuf * pixbuf);
|
|
static GdkPixbuf *resize_image(GdkPixbuf * pixbuf, int width, int height);
|
|
|
|
int
|
|
get_image_size(char *filename, int *w, int *h)
|
|
{
|
|
GdkPixbuf *pixbuf;
|
|
|
|
if (filename == NULL)
|
|
return 1;
|
|
|
|
pixbuf = gdk_pixbuf_new_from_file(filename);
|
|
if (pixbuf == NULL)
|
|
return 1;
|
|
|
|
*w = gdk_pixbuf_get_width(pixbuf);
|
|
*h = gdk_pixbuf_get_height(pixbuf);
|
|
|
|
gdk_pixbuf_finalize(pixbuf);
|
|
return 0;
|
|
}
|
|
|
|
FB_IMAGE *
|
|
fb_image_load(char *filename, int w, int h)
|
|
{
|
|
GdkPixbuf *pixbuf;
|
|
FB_IMAGE *img;
|
|
|
|
if (filename == NULL)
|
|
return NULL;
|
|
|
|
pixbuf = gdk_pixbuf_new_from_file(filename);
|
|
if (pixbuf == NULL)
|
|
return NULL;
|
|
|
|
pixbuf = resize_image(pixbuf, w, h);
|
|
if (pixbuf == NULL)
|
|
return NULL;
|
|
|
|
w = gdk_pixbuf_get_width(pixbuf);
|
|
h = gdk_pixbuf_get_height(pixbuf);
|
|
|
|
img = fb_image_new(w, h);
|
|
|
|
if (img == NULL) {
|
|
gdk_pixbuf_finalize(pixbuf);
|
|
return NULL;
|
|
}
|
|
|
|
draw(img, pixbuf);
|
|
|
|
gdk_pixbuf_finalize(pixbuf);
|
|
|
|
return img;
|
|
}
|
|
|
|
void
|
|
draw(FB_IMAGE * img, GdkPixbuf * pixbuf)
|
|
{
|
|
int i, j, r, g, b, offset, bpp, rowstride;
|
|
guchar *pixels;
|
|
gboolean alpha;
|
|
|
|
if (img == NULL || pixbuf == NULL)
|
|
return;
|
|
|
|
rowstride = gdk_pixbuf_get_rowstride(pixbuf);
|
|
pixels = gdk_pixbuf_get_pixels(pixbuf);
|
|
alpha = gdk_pixbuf_get_has_alpha(pixbuf);
|
|
|
|
bpp = rowstride / img->width;
|
|
for (j = 0; j < img->height; j++) {
|
|
offset = j * rowstride;
|
|
for (i = 0; i < img->width; i++, offset += bpp) {
|
|
r = pixels[offset];
|
|
g = pixels[offset + 1];
|
|
b = pixels[offset + 2];
|
|
if (alpha && pixels[offset + 3] == 0)
|
|
fb_image_pset(img, i, j, bg_r, bg_g, bg_b);
|
|
else
|
|
fb_image_pset(img, i, j, r, g, b);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
static GdkPixbuf *
|
|
resize_image(GdkPixbuf * pixbuf, int width, int height)
|
|
{
|
|
GdkPixbuf * resized_pixbuf;
|
|
int w, h;
|
|
|
|
if (pixbuf == NULL)
|
|
return NULL;
|
|
|
|
w = gdk_pixbuf_get_width(pixbuf);
|
|
h = gdk_pixbuf_get_height(pixbuf);
|
|
|
|
if (width < 1 || height < 1)
|
|
return pixbuf;
|
|
|
|
if (w == width && h == height)
|
|
return pixbuf;
|
|
|
|
resized_pixbuf =
|
|
gdk_pixbuf_scale_simple(pixbuf, width, height, GDK_INTERP_HYPER);
|
|
|
|
gdk_pixbuf_finalize(pixbuf);
|
|
|
|
if (resized_pixbuf == NULL)
|
|
return NULL;
|
|
|
|
return resized_pixbuf;
|
|
}
|