source: libcaca/trunk/src/blit.c @ 192

Last change on this file since 192 was 192, checked in by Sam Hocevar, 18 years ago
  • Changed copyleft to LGPL.
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
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 192 2003-11-16 12:28:29Z 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
29typedef unsigned char uint8_t;
30#endif
31
32#include <stdlib.h>
33
34#include "caca.h"
35#include "caca_internals.h"
36
37#include <stdio.h>
38void caca_blit(int x1, int y1, int x2, int y2, void *pixels, int w, int h)
39{
40    char foo[] = { ' ', '.', ':', ';', '=', '$', '%', '@', '#', '8', 'W' };
41    int x, y, pitch;
42
43    if(x1 > x2)
44    {
45        int tmp = x2; x2 = x1; x1 = tmp;
46    }
47
48    if(y1 > y2)
49    {
50        int tmp = y2; y2 = y1; y1 = tmp;
51    }
52
53    pitch = (3 * w + 3) / 4 * 4;
54
55    for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)caca_get_height(); y++)
56        for(x = x1 > 0 ? x1 : 0; x <= x2 && x <= (int)caca_get_width(); x++)
57        {
58            int fromx = w * (x - x1) / (x2 - x1 + 1);
59            int fromy = h * (y - y1) / (y2 - y1 + 1);
60            int r = ((unsigned char *)pixels)[3 * fromx + pitch * fromy];
61            int g = ((unsigned char *)pixels)[3 * fromx + 1 + pitch * fromy];
62            int b = ((unsigned char *)pixels)[3 * fromx + 2 + pitch * fromy];
63
64            if(r == g && g == b)
65            {
66                caca_set_color(EE_LIGHTGRAY);
67            }
68            else
69            {
70                static int foo_colors[6] = {EE_LIGHTRED, EE_YELLOW, EE_LIGHTGREEN, EE_LIGHTCYAN, EE_LIGHTBLUE, EE_LIGHTMAGENTA};
71                float min = r, max = r, delta, hue, sat;
72                if(min > g) min = g; if(max < g) max = g;
73                if(min > b) min = b; if(max < b) max = b;
74
75                delta = max - min;
76
77                sat = max / delta;
78
79                if(delta > 20)
80                {
81                    if( r == max )
82                        hue = (g - b) / delta; // between yellow & magenta
83                    else if( g == max )
84                        hue = 2 + (b - r) / delta; // between cyan & yellow
85                    else
86                        hue = 4 + (r - g) / delta; // between magenta & cyan
87
88                    hue *= 60; // degrees
89                    if( hue < 0 )
90                        hue += 360;
91
92                    caca_set_color(foo_colors[(int)(hue + 30) / 60]);
93                }
94                else
95                {
96                    caca_set_color(EE_LIGHTGRAY);
97                }
98            }
99
100            caca_putchar(x, y, foo[(r + g + b) / 3 / 25]);
101        }
102}
103
Note: See TracBrowser for help on using the repository browser.