Intial commit
[profile/ivi/w3m.git] / w3mimg / fb / fb_imlib2.c
1 /* $Id: fb_imlib2.c,v 1.10 2004/08/04 17:32:28 ukai Exp $ */
2 /**************************************************************************
3                 fb_imlib2.c 0.3 Copyright (C) 2002, hito
4  **************************************************************************/
5
6 #include <X11/Xlib.h>
7 #include <Imlib2.h>
8 #include "fb.h"
9 #include "fb_img.h"
10
11 static void draw(FB_IMAGE * img, Imlib_Image image);
12 static Imlib_Image resize_image(Imlib_Image image, int width, int height);
13
14 void
15 fb_image_init()
16 {
17     return;
18 }
19
20 int
21 get_image_size(char *filename, int *w, int *h)
22 {
23     Imlib_Image image;
24
25     if (filename == NULL)
26         return 1;
27
28     image = imlib_load_image(filename);
29     if (image == NULL)
30         return 1;
31
32     imlib_context_set_image(image);
33     *w = imlib_image_get_width();
34     *h = imlib_image_get_height();
35     imlib_free_image();
36
37     return 0;
38 }
39
40 FB_IMAGE **
41 fb_image_load(char *filename, int w, int h, int n)
42 {
43     Imlib_Image image;
44     FB_IMAGE **frame;
45
46     if (filename == NULL)
47         return NULL;
48
49     image = imlib_load_image(filename);
50     if (image == NULL)
51         return NULL;
52
53     image = resize_image(image, w, h);
54     if (image == NULL)
55         return NULL;
56
57     imlib_context_set_image(image);
58
59     w = imlib_image_get_width();
60     h = imlib_image_get_height();
61
62     frame = fb_frame_new(w, h, 1);
63
64     if (frame == NULL) {
65         imlib_free_image();
66         return NULL;
67     }
68
69     draw(frame[0], image);
70
71     imlib_free_image();
72
73     return frame;
74 }
75
76 static void
77 draw(FB_IMAGE * img, Imlib_Image image)
78 {
79     int i, j, r, g, b, a = 0, offset;
80     DATA32 *data;
81
82     if (img == NULL)
83         return;
84
85     imlib_context_set_image(image);
86     data = imlib_image_get_data_for_reading_only();
87
88     for (j = 0; j < img->height; j++) {
89         offset = img->width * j;
90         for (i = 0; i < img->width; i++) {
91             a = (data[offset + i] >> 24) & 0x000000ff;
92             r = (data[offset + i] >> 16) & 0x000000ff;
93             g = (data[offset + i] >> 8) & 0x000000ff;
94             b = (data[offset + i]) & 0x000000ff;
95
96             if (a == 0) {
97                 fb_image_pset(img, i, j, bg_r, bg_g, bg_b);
98             }
99             else {
100                 fb_image_pset(img, i, j, r, g, b);
101             }
102         }
103     }
104     return;
105 }
106
107 static Imlib_Image
108 resize_image(Imlib_Image image, int width, int height)
109 {
110     Imlib_Image resized_image;
111     int w, h;
112
113     if (image == NULL)
114         return NULL;
115
116     imlib_context_set_image(image);
117     w = imlib_image_get_width();
118     h = imlib_image_get_height();
119
120     if (width < 1 || height < 1)
121         return image;
122
123     if (w == width && h == height)
124         return image;
125
126     resized_image =
127         imlib_create_cropped_scaled_image(0, 0, w, h, width, height);
128
129     imlib_free_image();
130
131     return resized_image;
132 }