Ignore:
Timestamp:
Nov 16, 2003, 4:20:17 PM (19 years ago)
Author:
Sam Hocevar
Message:
  • src/blit.c: + Added caca_set_dithering() (unused yet). + Added random dithering.
  • src/caca.h: + Renamed legacy EE_* enums to CACA_*.
  • examples/demo.c examples/caca.txt: + Added a default sprite for libcaca.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/src/blit.c

    r192 r193  
    3535#include "caca_internals.h"
    3636
    37 #include <stdio.h>
     37static enum caca_dithering _caca_dithering = CACA_DITHER_NONE;
     38
     39void caca_set_dithering(enum caca_dithering dither)
     40{
     41    if(dither < 0 || dither > 1)
     42        return;
     43
     44    _caca_dithering = dither;
     45}
     46
    3847void caca_blit(int x1, int y1, int x2, int y2, void *pixels, int w, int h)
    3948{
    40     char foo[] = { ' ', '.', ':', ';', '=', '$', '%', '@', '#', '8', 'W' };
     49    char foo[] = { ' ', '.', ':', ';', '=', '%', '$', 'W', '#', '8', '@' };
    4150    int x, y, pitch;
    4251
     
    5665        for(x = x1 > 0 ? x1 : 0; x <= x2 && x <= (int)caca_get_width(); x++)
    5766        {
     67static int light_colors[] = {CACA_COLOR_LIGHTMAGENTA, CACA_COLOR_LIGHTRED, CACA_COLOR_YELLOW, CACA_COLOR_LIGHTGREEN, CACA_COLOR_LIGHTCYAN, CACA_COLOR_LIGHTBLUE, CACA_COLOR_LIGHTMAGENTA};
     68static int dark_colors[] = {CACA_COLOR_MAGENTA, CACA_COLOR_RED, CACA_COLOR_BROWN, CACA_COLOR_GREEN, CACA_COLOR_CYAN, CACA_COLOR_BLUE, CACA_COLOR_MAGENTA};
    5869            int fromx = w * (x - x1) / (x2 - x1 + 1);
    5970            int fromy = h * (y - y1) / (y2 - y1 + 1);
     
    6172            int g = ((unsigned char *)pixels)[3 * fromx + 1 + pitch * fromy];
    6273            int b = ((unsigned char *)pixels)[3 * fromx + 2 + pitch * fromy];
     74            int hue, sat, val;
    6375
    64             if(r == g && g == b)
     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))
    6585            {
    66                 caca_set_color(EE_LIGHTGRAY);
     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]);
    6799            }
    68100            else
    69101            {
    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                 }
     102                caca_set_color(CACA_COLOR_LIGHTGRAY);
    98103            }
    99104
    100             caca_putchar(x, y, foo[(r + g + b) / 3 / 25]);
     105            caca_putchar(x, y, foo[(r + g + b + caca_rand(-10, 10)) / 3 / 25]);
    101106        }
    102107}
Note: See TracChangeset for help on using the changeset viewer.