1 | /* |
---|
2 | * libcaca ASCII-Art library |
---|
3 | * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * This library is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the GNU Lesser General Public |
---|
8 | * License as published by the Free Software Foundation; either |
---|
9 | * version 2 of the License, or (at your option) any later version. |
---|
10 | * |
---|
11 | * This library is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | * Lesser General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU Lesser General Public |
---|
17 | * License along with this library; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
19 | * 02111-1307 USA |
---|
20 | */ |
---|
21 | |
---|
22 | /** \file blit.c |
---|
23 | * \version \$Id: blit.c 205 2003-11-22 12:45:25Z sam $ |
---|
24 | * \author Sam Hocevar <sam@zoy.org> |
---|
25 | * \brief Bitmap functions |
---|
26 | * |
---|
27 | * This file contains bitmap blitting functions. |
---|
28 | */ |
---|
29 | |
---|
30 | #include "config.h" |
---|
31 | |
---|
32 | #ifdef HAVE_INTTYPES_H |
---|
33 | # include <inttypes.h> |
---|
34 | #else |
---|
35 | typedef unsigned char uint8_t; |
---|
36 | #endif |
---|
37 | |
---|
38 | #include <stdlib.h> |
---|
39 | |
---|
40 | #include "caca.h" |
---|
41 | #include "caca_internals.h" |
---|
42 | |
---|
43 | /* Dithering methods */ |
---|
44 | static void init_no_dither(int); |
---|
45 | static int get_no_dither(void); |
---|
46 | static void increment_no_dither(void); |
---|
47 | |
---|
48 | static void init_ordered_dither(int); |
---|
49 | static int get_ordered_dither(void); |
---|
50 | static void increment_ordered_dither(void); |
---|
51 | |
---|
52 | static void init_random_dither(int); |
---|
53 | static int get_random_dither(void); |
---|
54 | static void increment_random_dither(void); |
---|
55 | |
---|
56 | /* Current dithering method */ |
---|
57 | static enum caca_dithering _caca_dithering = CACA_DITHER_NONE; |
---|
58 | |
---|
59 | static void (*_init_dither) (int) = init_no_dither; |
---|
60 | static int (*_get_dither) (void) = get_no_dither; |
---|
61 | static void (*_increment_dither) (void) = increment_no_dither; |
---|
62 | |
---|
63 | void caca_set_dithering(enum caca_dithering dither) |
---|
64 | { |
---|
65 | switch(dither) |
---|
66 | { |
---|
67 | case CACA_DITHER_NONE: |
---|
68 | _init_dither = init_no_dither; |
---|
69 | _get_dither = get_no_dither; |
---|
70 | _increment_dither = increment_no_dither; |
---|
71 | break; |
---|
72 | |
---|
73 | case CACA_DITHER_ORDERED: |
---|
74 | _init_dither = init_ordered_dither; |
---|
75 | _get_dither = get_ordered_dither; |
---|
76 | _increment_dither = increment_ordered_dither; |
---|
77 | break; |
---|
78 | |
---|
79 | case CACA_DITHER_RANDOM: |
---|
80 | _init_dither = init_random_dither; |
---|
81 | _get_dither = get_random_dither; |
---|
82 | _increment_dither = increment_random_dither; |
---|
83 | break; |
---|
84 | |
---|
85 | default: |
---|
86 | return; |
---|
87 | } |
---|
88 | |
---|
89 | _caca_dithering = dither; |
---|
90 | } |
---|
91 | |
---|
92 | struct caca_bitmap |
---|
93 | { |
---|
94 | int bpp; |
---|
95 | int w, h, pitch; |
---|
96 | int rmask, gmask, bmask; |
---|
97 | }; |
---|
98 | |
---|
99 | struct caca_bitmap *caca_create_bitmap(int bpp, int w, int h, int pitch, |
---|
100 | int rmask, int gmask, int bmask) |
---|
101 | { |
---|
102 | struct caca_bitmap *bitmap; |
---|
103 | |
---|
104 | /* Currently only this format is supported. Will improve later. */ |
---|
105 | if(!w || !h || !pitch || bpp != 32 || |
---|
106 | rmask != 0x00ff0000 || gmask != 0x0000ff00 || bmask != 0x000000ff) |
---|
107 | return NULL; |
---|
108 | |
---|
109 | bitmap = malloc(sizeof(struct caca_bitmap)); |
---|
110 | if(!bitmap) |
---|
111 | return NULL; |
---|
112 | |
---|
113 | bitmap->bpp = bpp; |
---|
114 | |
---|
115 | bitmap->w = w; |
---|
116 | bitmap->h = h; |
---|
117 | bitmap->pitch = pitch; |
---|
118 | |
---|
119 | bitmap->rmask = rmask; |
---|
120 | bitmap->gmask = gmask; |
---|
121 | bitmap->bmask = bmask; |
---|
122 | |
---|
123 | return bitmap; |
---|
124 | } |
---|
125 | |
---|
126 | void caca_free_bitmap(struct caca_bitmap *bitmap) |
---|
127 | { |
---|
128 | if(!bitmap) |
---|
129 | return; |
---|
130 | |
---|
131 | free(bitmap); |
---|
132 | } |
---|
133 | |
---|
134 | void caca_draw_bitmap(int x1, int y1, int x2, int y2, |
---|
135 | struct caca_bitmap *bitmap, char *pixels) |
---|
136 | { |
---|
137 | /* FIXME: this code is shite! */ |
---|
138 | static int white_colors[] = {CACA_COLOR_DARKGRAY, CACA_COLOR_LIGHTGRAY, CACA_COLOR_WHITE}; |
---|
139 | static int light_colors[] = {CACA_COLOR_LIGHTMAGENTA, CACA_COLOR_LIGHTRED, CACA_COLOR_YELLOW, CACA_COLOR_LIGHTGREEN, CACA_COLOR_LIGHTCYAN, CACA_COLOR_LIGHTBLUE, CACA_COLOR_LIGHTMAGENTA}; |
---|
140 | static int dark_colors[] = {CACA_COLOR_MAGENTA, CACA_COLOR_RED, CACA_COLOR_BROWN, CACA_COLOR_GREEN, CACA_COLOR_CYAN, CACA_COLOR_BLUE, CACA_COLOR_MAGENTA}; |
---|
141 | static char foo[] = { ' ', '.', ':', ';', '=', '%', '$', 'W', '#', '8', '@' }; |
---|
142 | int x, y, w, h, pitch; |
---|
143 | |
---|
144 | if(!bitmap || !pixels) |
---|
145 | return; |
---|
146 | |
---|
147 | w = bitmap->w; |
---|
148 | h = bitmap->h; |
---|
149 | pitch = bitmap->pitch; |
---|
150 | |
---|
151 | if(x1 > x2) |
---|
152 | { |
---|
153 | int tmp = x2; x2 = x1; x1 = tmp; |
---|
154 | } |
---|
155 | |
---|
156 | if(y1 > y2) |
---|
157 | { |
---|
158 | int tmp = y2; y2 = y1; y1 = tmp; |
---|
159 | } |
---|
160 | |
---|
161 | for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)caca_get_height(); y++) |
---|
162 | { |
---|
163 | /* Initialize dither tables for the current line */ |
---|
164 | _init_dither(y); |
---|
165 | |
---|
166 | /* Dither the current line */ |
---|
167 | for(x = x1 > 0 ? x1 : 0; x <= x2 && x <= (int)caca_get_width(); x++) |
---|
168 | { |
---|
169 | int fromx = w * (x - x1) / (x2 - x1 + 1); |
---|
170 | int fromy = h * (y - y1) / (y2 - y1 + 1); |
---|
171 | /* FIXME: bwahaaa, we don't even respect masks */ |
---|
172 | int b = ((unsigned char *)pixels)[4 * fromx + pitch * fromy]; |
---|
173 | int g = ((unsigned char *)pixels)[4 * fromx + 1 + pitch * fromy]; |
---|
174 | int r = ((unsigned char *)pixels)[4 * fromx + 2 + pitch * fromy]; |
---|
175 | int hue, sat, val; |
---|
176 | |
---|
177 | int min = r, max = r, delta; |
---|
178 | if(min > g) min = g; if(max < g) max = g; |
---|
179 | if(min > b) min = b; if(max < b) max = b; |
---|
180 | |
---|
181 | delta = max - min; |
---|
182 | val = max; /* 0 - 255 */ |
---|
183 | sat = max ? 256 * delta / max : 0; /* 0 - 255 */ |
---|
184 | |
---|
185 | if(sat > (_get_dither() + 24) * 4) |
---|
186 | { |
---|
187 | /* XXX: Values should be between 1 and 6, but since we |
---|
188 | * are dithering, there may be overflows, hence our bigger |
---|
189 | * *_colors[] tables. */ |
---|
190 | if( r == max ) |
---|
191 | hue = 256 + 256 * (g - b) / delta; |
---|
192 | else if( g == max ) |
---|
193 | hue = 768 + 256 * (b - r) / delta; |
---|
194 | else |
---|
195 | hue = 1280 + 256 * (r - g) / delta; |
---|
196 | |
---|
197 | hue = (hue + 128 + 16 * _get_dither()) / 256; |
---|
198 | |
---|
199 | if(val > (_get_dither() + 40) * 4) |
---|
200 | caca_set_color(light_colors[hue]); |
---|
201 | else |
---|
202 | caca_set_color(dark_colors[hue]); |
---|
203 | } |
---|
204 | else |
---|
205 | { |
---|
206 | caca_set_color(white_colors[max * 3 / 256]); |
---|
207 | } |
---|
208 | |
---|
209 | caca_putchar(x, y, foo[(r + g + b + 2 * _get_dither()) / 3 / 25]); |
---|
210 | |
---|
211 | _increment_dither(); |
---|
212 | } |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | /* |
---|
217 | * XXX: The following functions are local. |
---|
218 | */ |
---|
219 | |
---|
220 | /* |
---|
221 | * No dithering |
---|
222 | */ |
---|
223 | static void init_no_dither(int line) |
---|
224 | { |
---|
225 | ; |
---|
226 | } |
---|
227 | |
---|
228 | static int get_no_dither(void) |
---|
229 | { |
---|
230 | return 0; |
---|
231 | } |
---|
232 | |
---|
233 | static void increment_no_dither(void) |
---|
234 | { |
---|
235 | return; |
---|
236 | } |
---|
237 | |
---|
238 | /* |
---|
239 | * Ordered dithering |
---|
240 | */ |
---|
241 | static int dither4x4[] = {-8, 0, -6, 2, |
---|
242 | 4, -4, 6, -2, |
---|
243 | -5, 3, -7, 1, |
---|
244 | 7, -1, 5, -3}; |
---|
245 | static int *dither_table; |
---|
246 | static int dither_index; |
---|
247 | |
---|
248 | static void init_ordered_dither(int line) |
---|
249 | { |
---|
250 | dither_table = dither4x4 + (line % 4) * 4; |
---|
251 | dither_index = 0; |
---|
252 | } |
---|
253 | |
---|
254 | static int get_ordered_dither(void) |
---|
255 | { |
---|
256 | return dither_table[dither_index]; |
---|
257 | } |
---|
258 | |
---|
259 | static void increment_ordered_dither(void) |
---|
260 | { |
---|
261 | dither_index = (dither_index + 1) % 4; |
---|
262 | } |
---|
263 | |
---|
264 | /* |
---|
265 | * Random dithering |
---|
266 | */ |
---|
267 | static void init_random_dither(int line) |
---|
268 | { |
---|
269 | ; |
---|
270 | } |
---|
271 | |
---|
272 | static int get_random_dither(void) |
---|
273 | { |
---|
274 | return caca_rand(-8, 7); |
---|
275 | } |
---|
276 | |
---|
277 | static void increment_random_dither(void) |
---|
278 | { |
---|
279 | return; |
---|
280 | } |
---|
281 | |
---|