3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <libx/base.h>
20 #include <libx/image.h>
25 xbitmap
*ximage_open (const char *filename
)
27 int fd
= open (filename
, O_RDONLY
);
32 unsigned char *header
= (unsigned char *) malloc (sizeof (unsigned char) * 1080);
37 int len
= read (fd
, header
, 1078);
42 if (header
[0] != 'B' && header
[1] != 'M')
45 /* read in the width and height of the image, and the
46 number of colors used; ignore the rest */
47 xbitmap
*bitmap
= (xbitmap
*) malloc (sizeof (xbitmap
));
51 bitmap
->width
= header
[18] | (header
[19] << 8);
52 bitmap
->height
= header
[22] | (header
[23] << 8);
53 unsigned short num_colors
= header
[46] | (header
[47] << 8);
55 /* assume we are working with an 8-bit file */
59 bitmap
->data
= (unsigned char *) malloc (sizeof (unsigned char) * (bitmap
->width
* bitmap
->height
));
64 len
= read (fd
, bitmap
->data
, (bitmap
->width
* bitmap
->height
));
69 void ximage_draw (xbitmap
*bitmap
, unsigned x
, unsigned y
, int transparency
)
75 if (transparency
== -1) {
76 for(a
= 0; a
< (unsigned) bitmap
->height
* (unsigned) bitmap
->width
; a
++) {
77 if (j
>= (unsigned) bitmap
->width
) {
81 xpixel (j
+x
, b
+y
, bitmap
->data
[a
]);
85 for(a
= 0; a
< (unsigned) bitmap
->height
* (unsigned) bitmap
->width
; a
++) {
86 if (j
>= bitmap
->width
) {
91 if (bitmap
->data
[a
] != (unsigned char) transparency
)
92 xpixel (j
+x
, b
+y
, bitmap
->data
[a
]);
99 void ximage_draw_spec (xbitmap
*bitmap
, unsigned x
, unsigned y
, unsigned size_x
, unsigned size_y
, unsigned offset_x
, unsigned offset_y
, int transparency
)
104 unsigned k
= ((offset_y
*2) * size_x
+ offset_x
);
106 if (transparency
== -1) {
107 for(a
= 0; a
< size_x
* size_y
; a
++) {
111 a
+= bitmap
->width
- size_x
;
113 xpixel (j
+x
, b
+y
, bitmap
->data
[a
+k
]);
117 for(a
= 0; a
< (unsigned) bitmap
->height
* (unsigned) bitmap
->width
; a
++) {
125 a
+= bitmap
->width
- size_x
;
128 if (bitmap
->data
[a
+k
] != (unsigned char) transparency
)
129 xpixel (j
+x
, b
+y
, bitmap
->data
[a
+k
]);