Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / libx / image / ximage.c
blobd599501a8954ad91c4ae02b6445353d2e3281c7e
1 /*
2 * ZeX/OS
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>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
25 xbitmap *ximage_open (const char *filename)
27 int fd = open (filename, O_RDONLY);
29 if (!fd)
30 return 0;
32 unsigned char *header = (unsigned char *) malloc (sizeof (unsigned char) * 1080);
34 if (!header)
35 return 0;
37 int len = read (fd, header, 1078);
39 if (!len)
40 return 0;
42 if (header[0] != 'B' && header[1] != 'M')
43 return 0;
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));
48 if (!bitmap)
49 return 0;
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 */
56 if (num_colors == 0)
57 num_colors = 256;
59 bitmap->data = (unsigned char *) malloc (sizeof (unsigned char) * (bitmap->width * bitmap->height));
61 if (!bitmap->data)
62 return 0;
64 len = read (fd, bitmap->data, (bitmap->width * bitmap->height));
66 return bitmap;
69 void ximage_draw (xbitmap *bitmap, unsigned x, unsigned y, int transparency)
71 unsigned a = 0;
72 unsigned b = 0;
73 unsigned j = 0;
75 if (transparency == -1) {
76 for(a = 0; a < (unsigned) bitmap->height * (unsigned) bitmap->width; a ++) {
77 if (j >= (unsigned) bitmap->width) {
78 j = 0;
79 b ++;
81 xpixel (j+x, b+y, bitmap->data[a]);
82 j ++;
84 } else {
85 for(a = 0; a < (unsigned) bitmap->height * (unsigned) bitmap->width; a ++) {
86 if (j >= bitmap->width) {
87 j = 0;
88 b ++;
91 if (bitmap->data[a] != (unsigned char) transparency)
92 xpixel (j+x, b+y, bitmap->data[a]);
94 j ++;
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)
101 unsigned a = 0;
102 unsigned b = 0;
103 unsigned j = 0;
104 unsigned k = ((offset_y*2) * size_x + offset_x);
106 if (transparency == -1) {
107 for(a = 0; a < size_x * size_y; a ++) {
108 if (j >= size_x) {
109 j = 0;
110 b ++;
111 a += bitmap->width - size_x;
113 xpixel (j+x, b+y, bitmap->data[a+k]);
114 j ++;
116 } else {
117 for(a = 0; a < (unsigned) bitmap->height * (unsigned) bitmap->width; a ++) {
118 if (j >= size_x) {
119 j = 0;
120 b ++;
122 if (b >= size_y)
123 return;
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]);
131 j ++;