1 | /* |
---|
2 | * libcaca ASCII-Art library |
---|
3 | * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: blit.c 193 2003-11-16 15:20:17Z sam $ |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU Lesser General Public |
---|
10 | * License as published by the Free Software Foundation; either |
---|
11 | * version 2 of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This library is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | * Lesser General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU Lesser General Public |
---|
19 | * License along with this library; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
21 | * 02111-1307 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include "config.h" |
---|
25 | |
---|
26 | #ifdef HAVE_INTTYPES_H |
---|
27 | # include <inttypes.h> |
---|
28 | #else |
---|
29 | typedef unsigned char uint8_t; |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <stdlib.h> |
---|
33 | |
---|
34 | #include "caca.h" |
---|
35 | #include "caca_internals.h" |
---|
36 | |
---|
37 | static enum caca_dithering _caca_dithering = CACA_DITHER_NONE; |
---|
38 | |
---|
39 | void caca_set_dithering(enum caca_dithering dither) |
---|
40 | { |
---|
41 | if(dither < 0 || dither > 1) |
---|
42 | return; |
---|
43 | |
---|
44 | _caca_dithering = dither; |
---|
45 | } |
---|
46 | |
---|
47 | void caca_blit(int x1, int y1, int x2, int y2, void *pixels, int w, int h) |
---|
48 | { |
---|
49 | char foo[] = { ' ', '.', ':', ';', '=', '%', '$', 'W', '#', '8', '@' }; |
---|
50 | int x, y, pitch; |
---|
51 | |
---|
52 | if(x1 > x2) |
---|
53 | { |
---|
54 | int tmp = x2; x2 = x1; x1 = tmp; |
---|
55 | } |
---|
56 | |
---|
57 | if(y1 > y2) |
---|
58 | { |
---|
59 | int tmp = y2; y2 = y1; y1 = tmp; |
---|
60 | } |
---|
61 | |
---|
62 | pitch = (3 * w + 3) / 4 * 4; |
---|
63 | |
---|
64 | for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)caca_get_height(); y++) |
---|
65 | for(x = x1 > 0 ? x1 : 0; x <= x2 && x <= (int)caca_get_width(); x++) |
---|
66 | { |
---|
67 | 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}; |
---|
68 | 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}; |
---|
69 | int fromx = w * (x - x1) / (x2 - x1 + 1); |
---|
70 | int fromy = h * (y - y1) / (y2 - y1 + 1); |
---|
71 | int r = ((unsigned char *)pixels)[3 * fromx + pitch * fromy]; |
---|
72 | int g = ((unsigned char *)pixels)[3 * fromx + 1 + pitch * fromy]; |
---|
73 | int b = ((unsigned char *)pixels)[3 * fromx + 2 + pitch * fromy]; |
---|
74 | int hue, sat, val; |
---|
75 | |
---|
76 | int min = r, max = r, delta; |
---|
77 | if(min > g) min = g; if(max < g) max = g; |
---|
78 | if(min > b) min = b; if(max < b) max = b; |
---|
79 | |
---|
80 | delta = max - min; |
---|
81 | val = max; /* 0 - 255 */ |
---|
82 | sat = max ? 256 * delta / max : 0; /* 0 - 255 */ |
---|
83 | |
---|
84 | if(sat > caca_rand(64, 128)) |
---|
85 | { |
---|
86 | /* XXX: Values are automatically clipped between 0 and 6 |
---|
87 | * because of delta/2 */ |
---|
88 | if( r == max ) |
---|
89 | hue = 1 + (float)(g - b + delta / 2 + caca_rand(-40, 40)) / delta; |
---|
90 | else if( g == max ) |
---|
91 | hue = 3 + (float)(b - r + delta / 2 + caca_rand(-40, 40)) / delta; |
---|
92 | else |
---|
93 | hue = 5 + (float)(r - g + delta / 2 + caca_rand(-40, 40)) / delta; |
---|
94 | |
---|
95 | if(val > caca_rand(128, 192)) |
---|
96 | caca_set_color(light_colors[hue]); |
---|
97 | else |
---|
98 | caca_set_color(dark_colors[hue]); |
---|
99 | } |
---|
100 | else |
---|
101 | { |
---|
102 | caca_set_color(CACA_COLOR_LIGHTGRAY); |
---|
103 | } |
---|
104 | |
---|
105 | caca_putchar(x, y, foo[(r + g + b + caca_rand(-10, 10)) / 3 / 25]); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|