Browse Source

* src/caca.c src/caca.h:

+ Added caca_get_dithering_name().
  * src/bitmap.c:
    + Created a new dithering method with an 8x8 ordered matrix.
    + Replaced the char list with a string for better readability.
    + Dithering functions now return a value between 0 and 255.
  * examples/demo.c examples/view.c:
    + Adapted to use caca_get_dithering_name().
tags/v0.99.beta14
Sam Hocevar sam 21 years ago
parent
commit
624a13f3ce
5 changed files with 138 additions and 64 deletions
  1. +4
    -6
      examples/demo.c
  2. +5
    -5
      examples/view.c
  3. +97
    -46
      src/bitmap.c
  4. +19
    -3
      src/caca.c
  5. +13
    -4
      src/caca.h

+ 4
- 6
examples/demo.c View File

@@ -100,10 +100,8 @@ int main(int argc, char **argv)
break;
case 'd':
case 'D':
dithering = (dithering + 1) % 3;
caca_set_dithering(dithering == 0 ? CACA_DITHER_NONE :
dithering == 1 ? CACA_DITHER_ORDERED :
CACA_DITHER_RANDOM);
dithering = (dithering + 1) % 4;
caca_set_dithering(dithering);
display_menu();
break;
case 'c':
@@ -203,8 +201,8 @@ static void display_menu(void)
outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
caca_printf(4, 18, "'b': drawing boundaries: %s",
bounds == 0 ? "screen" : "infinite");
caca_printf(4, 19, "'d': %s dithering",
dithering == 0 ? "no" : dithering == 1 ? "ordered" : "random");
caca_printf(4, 19, "'d': dithering (%s)",
caca_get_dithering_name(dithering));

caca_putstr(4, yo - 2, "'q': quit");
}


+ 5
- 5
examples/view.c View File

@@ -37,9 +37,7 @@ char *pixels = NULL;
struct caca_bitmap *bitmap = NULL;
int x, y, w, h;

int dithering = 1;
const enum caca_dithering dithering_list[] =
{ CACA_DITHER_NONE, CACA_DITHER_ORDERED, CACA_DITHER_RANDOM };
int dithering = CACA_DITHERING_ORDERED4;

static void load_image(const char *);

@@ -104,7 +102,7 @@ int main(int argc, char **argv)
break;
case CACA_EVENT_KEY_PRESS | 'd':
case CACA_EVENT_KEY_PRESS | 'D':
dithering = (dithering + 1) % 3;
dithering = (dithering + 1) % 4;
update = 1;
break;
case CACA_EVENT_KEY_PRESS | '+':
@@ -178,7 +176,7 @@ int main(int argc, char **argv)
if(update)
{
caca_clear();
caca_set_dithering(dithering_list[dithering]);
caca_set_dithering(dithering);
caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);

if(!items)
@@ -226,6 +224,8 @@ int main(int argc, char **argv)
caca_putstr(0, 0, "q:Quit +/-/x:Zoom h/j/k/l: Move "
"d:Dithering ?:Help");
caca_printf(3, wh - 1, "cacaview %s", VERSION);
caca_printf(ww / 2 - 5, wh - 1, "(dithering: %s)",
caca_get_dithering_name(dithering));
caca_printf(ww - 14, wh - 1,
"(zoom: %s%i)", zoom > 0 ? "+" : "", zoom);



+ 97
- 46
src/bitmap.c View File

@@ -53,16 +53,20 @@ static void init_no_dither(int);
static int get_no_dither(void);
static void increment_no_dither(void);

static void init_ordered_dither(int);
static int get_ordered_dither(void);
static void increment_ordered_dither(void);
static void init_ordered4_dither(int);
static int get_ordered4_dither(void);
static void increment_ordered4_dither(void);

static void init_ordered8_dither(int);
static int get_ordered8_dither(void);
static void increment_ordered8_dither(void);

static void init_random_dither(int);
static int get_random_dither(void);
static void increment_random_dither(void);

/* Current dithering method */
static enum caca_dithering _caca_dithering = CACA_DITHER_NONE;
static enum caca_dithering _caca_dithering = CACA_DITHERING_NONE;

static void (*_init_dither) (int) = init_no_dither;
static int (*_get_dither) (void) = get_no_dither;
@@ -72,19 +76,25 @@ void caca_set_dithering(enum caca_dithering dither)
{
switch(dither)
{
case CACA_DITHER_NONE:
case CACA_DITHERING_NONE:
_init_dither = init_no_dither;
_get_dither = get_no_dither;
_increment_dither = increment_no_dither;
break;

case CACA_DITHER_ORDERED:
_init_dither = init_ordered_dither;
_get_dither = get_ordered_dither;
_increment_dither = increment_ordered_dither;
case CACA_DITHERING_ORDERED4:
_init_dither = init_ordered4_dither;
_get_dither = get_ordered4_dither;
_increment_dither = increment_ordered4_dither;
break;

case CACA_DITHERING_ORDERED8:
_init_dither = init_ordered8_dither;
_get_dither = get_ordered8_dither;
_increment_dither = increment_ordered8_dither;
break;

case CACA_DITHER_RANDOM:
case CACA_DITHERING_RANDOM:
_init_dither = init_random_dither;
_get_dither = get_random_dither;
_increment_dither = increment_random_dither;
@@ -310,19 +320,18 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2,
};

static char foo[] =
{
' ', ' ', ' ', ' ',
',', '`', '.', '\'',
'i', '-', ':', '^',
'|', '/', ';', '\\',
'=', '+', 'o', 'x',
'<', 'x', '%', '>',
'&', 'z', '$', 'w',
'W', 'X', 'K', 'M',
'#', '8', '#', '#',
'8', '@', '8', '#',
'@', '8', '@', '8',
};
" "
" ,' "
",`.'"
"i-:^"
"|/;\\"
"=+ox"
"<x%>"
"&z$w"
"WXKM"
"#8##"
"8@8#"
"@8@8";

int x, y, w, h, pitch;

@@ -377,16 +386,16 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2,
/* Now get HSV from RGB */
rgb2hsv_default(R, G, B, &hue, &sat, &val);

if(sat < 0x6000 + _get_dither() * 0x800)
if(sat < 0x2000 + _get_dither() * 0x80)
caca_set_color(white_colors[val * 3 / 0x10000], CACA_COLOR_BLACK);
else if(val > (_get_dither() + 40) * 0x400)
caca_set_color(light_colors[(hue + 0x8000 + _get_dither() * 0x1000) / 0x10000], CACA_COLOR_BLACK);
else if(val > 0x8000 + _get_dither() * 0x40)
caca_set_color(light_colors[(hue + _get_dither() * 0x100) / 0x10000], CACA_COLOR_BLACK);
else
caca_set_color(dark_colors[(hue + 0x8000 + _get_dither() * 0x1000) / 0x10000], CACA_COLOR_BLACK);
caca_set_color(dark_colors[(hue + _get_dither() * 0x100) / 0x10000], CACA_COLOR_BLACK);

/* FIXME: choose better characters! */
ch = (val + 0x200 * _get_dither()) * 10 / 0x10000;
ch = 4 * ch + (_get_dither() + 8) / 4;
ch = (val + 0x20 * _get_dither() - 0x1000 /*???*/) * 10 / 0x10000;
ch = 4 * ch + (_get_dither() + 8) / 0x40;
caca_putchar(x, y, foo[ch]);

_increment_dither();
@@ -408,7 +417,7 @@ static void init_no_dither(int line)

static int get_no_dither(void)
{
return 0;
return 0x80;
}

static void increment_no_dither(void)
@@ -417,29 +426,71 @@ static void increment_no_dither(void)
}

/*
* Ordered dithering
* Ordered 4 dithering
*/
static int dither4x4[] = {-8, 0, -6, 2,
4, -4, 6, -2,
-5, 3, -7, 1,
7, -1, 5, -3};
static int *dither_table;
static int dither_index;

static void init_ordered_dither(int line)
/*static int dither4x4[] = { 5, 0, 1, 6,
-1, -6, -5, 2,
-2, -7, -8, 3,
4, -3, -4, -7};*/
static int *ordered4_table;
static int ordered4_index;

static void init_ordered4_dither(int line)
{
static int dither4x4[] =
{
0x00, 0x80, 0x20, 0xa0,
0xc0, 0x40, 0xe0, 0x60,
0x30, 0xb0, 0x10, 0x90,
0xf0, 0x70, 0xd0, 0x50
};

ordered4_table = dither4x4 + (line % 4) * 4;
ordered4_index = 0;
}

static int get_ordered4_dither(void)
{
dither_table = dither4x4 + (line % 4) * 4;
dither_index = 0;
return ordered4_table[ordered4_index];
}

static void increment_ordered4_dither(void)
{
ordered4_index = (ordered4_index + 1) % 4;
}

/*
* Ordered 8 dithering
*/
static int *ordered8_table;
static int ordered8_index;

static void init_ordered8_dither(int line)
{
static int dither8x8[] =
{
0x00, 0x80, 0x20, 0xa0, 0x08, 0x88, 0x28, 0xa8,
0xc0, 0x40, 0xe0, 0x60, 0xc8, 0x48, 0xe8, 0x68,
0x30, 0xb0, 0x10, 0x90, 0x38, 0xb8, 0x18, 0x98,
0xf0, 0x70, 0xd0, 0x50, 0xf8, 0x78, 0xd8, 0x58,
0x0c, 0x8c, 0x2c, 0xac, 0x04, 0x84, 0x24, 0xa4,
0xcc, 0x4c, 0xec, 0x6c, 0xc4, 0x44, 0xe4, 0x64,
0x3c, 0xbc, 0x1c, 0x9c, 0x34, 0xb4, 0x14, 0x94,
0xfc, 0x7c, 0xdc, 0x5c, 0xf4, 0x74, 0xd4, 0x54,
};

ordered8_table = dither8x8 + (line % 8) * 8;
ordered8_index = 0;
}

static int get_ordered_dither(void)
static int get_ordered8_dither(void)
{
return dither_table[dither_index];
return ordered8_table[ordered8_index];
}

static void increment_ordered_dither(void)
static void increment_ordered8_dither(void)
{
dither_index = (dither_index + 1) % 4;
ordered8_index = (ordered8_index + 1) % 8;
}

/*
@@ -452,7 +503,7 @@ static void init_random_dither(int line)

static int get_random_dither(void)
{
return caca_rand(-8, 7);
return caca_rand(0x00, 0xff);
}

static void increment_random_dither(void)


+ 19
- 3
src/caca.c View File

@@ -152,9 +152,9 @@ unsigned int caca_get_height(void)
#endif
}

const char *caca_get_color_name(unsigned int color)
const char *caca_get_color_name(enum caca_color color)
{
static const char *color_names[16] =
static const char *color_names[] =
{
"black",
"blue",
@@ -175,11 +175,27 @@ const char *caca_get_color_name(unsigned int color)
};

if(color < 0 || color > 15)
return "unknown color";
return "unknown";

return color_names[color];
}

const char *caca_get_dithering_name(enum caca_dithering dithering)
{
static const char *dithering_names[] =
{
"none",
"ordered 4x4",
"ordered 8x8",
"random"
};

if(dithering < 0 || dithering > 3)
return "unknown";

return dithering_names[dithering];
}

void caca_end(void)
{
#if defined(USE_SLANG)


+ 13
- 4
src/caca.h View File

@@ -81,16 +81,26 @@ enum caca_color
CACA_COLOR_WHITE = 15
};

const char *caca_get_color_name(enum caca_color);

/**
* The dithering modes to be used with caca_set_dithering().
*/
enum caca_dithering
{
CACA_DITHER_NONE,
CACA_DITHER_ORDERED,
CACA_DITHER_RANDOM
CACA_DITHERING_NONE = 0,
CACA_DITHERING_ORDERED4 = 1,
CACA_DITHERING_ORDERED8 = 2,
CACA_DITHERING_RANDOM = 3
};

const char *caca_get_dithering_name(enum caca_dithering);

/* Backwards compatibility */
#define CACA_DITHER_NONE CACA_DITHERING_NONE
#define CACA_DITHER_ORDERED CACA_DITHERING_ORDERED8
#define CACA_DITHER_RANDOM CACA_DITHERING_RANDOM

/**
* The event types returned by caca_get_event().
*/
@@ -138,7 +148,6 @@ void caca_set_dithering(enum caca_dithering);
unsigned int caca_get_rendertime(void);
unsigned int caca_get_width(void);
unsigned int caca_get_height(void);
const char *caca_get_color_name(unsigned int);
void caca_refresh(void);
void caca_end(void);



Loading…
Cancel
Save