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 187 2003-11-16 11:26:54Z sam $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or modify |
---|
9 | * it under the terms of the GNU General Public License as published by |
---|
10 | * the Free Software Foundation; either version 2 of the License, or |
---|
11 | * (at your option) any later version. |
---|
12 | * |
---|
13 | * This program 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 |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "config.h" |
---|
24 | |
---|
25 | #ifdef HAVE_INTTYPES_H |
---|
26 | # include <inttypes.h> |
---|
27 | #else |
---|
28 | typedef unsigned char uint8_t; |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <stdlib.h> |
---|
32 | |
---|
33 | #include "caca.h" |
---|
34 | #include "caca_internals.h" |
---|
35 | |
---|
36 | #include <stdio.h> |
---|
37 | void caca_blit(int x1, int y1, int x2, int y2, void *pixels, int w, int h) |
---|
38 | { |
---|
39 | char foo[] = { ' ', '.', ':', ';', '=', '$', '%', '@', '#', '8', 'W' }; |
---|
40 | int x, y, pitch; |
---|
41 | |
---|
42 | if(x1 > x2) |
---|
43 | { |
---|
44 | int tmp = x2; x2 = x1; x1 = tmp; |
---|
45 | } |
---|
46 | |
---|
47 | if(y1 > y2) |
---|
48 | { |
---|
49 | int tmp = y2; y2 = y1; y1 = tmp; |
---|
50 | } |
---|
51 | |
---|
52 | pitch = (3 * w + 3) / 4 * 4; |
---|
53 | |
---|
54 | for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)caca_get_height(); y++) |
---|
55 | for(x = x1 > 0 ? x1 : 0; x <= x2 && x <= (int)caca_get_width(); x++) |
---|
56 | { |
---|
57 | int fromx = w * (x - x1) / (x2 - x1 + 1); |
---|
58 | int fromy = h * (y - y1) / (y2 - y1 + 1); |
---|
59 | int r = ((unsigned char *)pixels)[3 * fromx + pitch * fromy]; |
---|
60 | int g = ((unsigned char *)pixels)[3 * fromx + 1 + pitch * fromy]; |
---|
61 | int b = ((unsigned char *)pixels)[3 * fromx + 2 + pitch * fromy]; |
---|
62 | |
---|
63 | if(r == g && g == b) |
---|
64 | { |
---|
65 | caca_set_color(EE_LIGHTGRAY); |
---|
66 | } |
---|
67 | else |
---|
68 | { |
---|
69 | static int foo_colors[6] = {EE_LIGHTRED, EE_YELLOW, EE_LIGHTGREEN, EE_LIGHTCYAN, EE_LIGHTBLUE, EE_LIGHTMAGENTA}; |
---|
70 | float min = r, max = r, delta, hue, sat; |
---|
71 | if(min > g) min = g; if(max < g) max = g; |
---|
72 | if(min > b) min = b; if(max < b) max = b; |
---|
73 | |
---|
74 | delta = max - min; |
---|
75 | |
---|
76 | sat = max / delta; |
---|
77 | |
---|
78 | if(delta > 20) |
---|
79 | { |
---|
80 | if( r == max ) |
---|
81 | hue = (g - b) / delta; // between yellow & magenta |
---|
82 | else if( g == max ) |
---|
83 | hue = 2 + (b - r) / delta; // between cyan & yellow |
---|
84 | else |
---|
85 | hue = 4 + (r - g) / delta; // between magenta & cyan |
---|
86 | |
---|
87 | hue *= 60; // degrees |
---|
88 | if( hue < 0 ) |
---|
89 | hue += 360; |
---|
90 | |
---|
91 | caca_set_color(foo_colors[(int)(hue + 30) / 60]); |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | caca_set_color(EE_LIGHTGRAY); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | caca_putchar(x, y, foo[(r + g + b) / 3 / 25]); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|