aboutsummaryrefslogtreecommitdiffstats
path: root/w3mimg/fb/fb_gdkpixbuf.c
blob: 003096aaac66575f8653c9a5a472fb012b441577 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* $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;
}