Changeset 657


Ignore:
Timestamp:
Mar 22, 2006, 2:12:54 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • Added optional support for CP437 gray blocks in the bitmap renderer.
  • Use a custom pow() function for gamma computation.
Location:
libcaca/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/configure.ac

    r652 r657  
    6060  AC_DEFINE(HAVE_SLEEP, 1, [Define to 1 if you have the `Sleep' function.])],
    6161 [AC_MSG_RESULT(no)])
     62
    6263AC_MSG_CHECKING(for fsin/fcos)
    6364AC_TRY_COMPILE([],[asm("fsin\n\tfcos");],
     
    6566  AC_DEFINE(HAVE_FSIN_FCOS, 1, [Define to 1 if you have the `fsin' and `fcos' operands.])],
    6667 [AC_MSG_RESULT(no)])
     68
     69AC_MSG_CHECKING(for fldln2/fxch/fyl2x)
     70AC_TRY_COMPILE([],[asm("fldln2; fldln2; fxch; fyl2x");],
     71 [AC_MSG_RESULT(yes)
     72  AC_DEFINE(HAVE_FLDLN2, 1, [Define to 1 if you have the `fldln2' and other floating points operands.])],
     73 [AC_MSG_RESULT(no)])
     74
    6775AC_CHECK_LIB(m, sin, MATH_LIBS="${MATH_LIBS} -lm")
    6876
  • libcaca/trunk/cucul/bitmap.c

    r653 r657  
    3232#include "cucul.h"
    3333#include "cucul_internals.h"
     34
     35#define CP437 0
    3436
    3537/*
     
    5860
    5961/* RGB palette for the new colour picker */
    60 static int rgb_palette[] =
     62static int const rgb_palette[] =
    6163{
    6264    0x0,   0x0,   0x0,
     
    7880};
    7981
    80 static int rgb_weight[] =
     82static int const rgb_weight[] =
    8183{
    8284    //2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2
     
    107109 */
    108110static void mask2shift(unsigned int, int *, int *);
     111static float gammapow(float x, float y);
    109112
    110113static void get_rgba_default(struct cucul_bitmap const *, uint8_t *, int, int,
     
    175178}
    176179
     180static float gammapow(float x, float y)
     181{
     182#ifdef HAVE_FLDLN2
     183    register double logx;
     184    register long double v, e;
     185#else
     186    register float tmp, t, r;
     187    int i;
     188#endif
     189
     190    if(x == 0.0)
     191        return y == 0.0 ? 1.0 : 0.0;
     192
     193#ifdef HAVE_FLDLN2
     194    /* FIXME: this can be optimised by directly calling fyl2x for x and y */
     195    asm volatile("fldln2; fxch; fyl2x"
     196                 : "=t" (logx) : "0" (x) : "st(1)");
     197
     198    asm volatile("fldl2e\n\t"
     199                 "fmul %%st(1)\n\t"
     200                 "fst %%st(1)\n\t"
     201                 "frndint\n\t"
     202                 "fxch\n\t"
     203                 "fsub %%st(1)\n\t"
     204                 "f2xm1\n\t"
     205                 : "=t" (v), "=u" (e) : "0" (y * logx));
     206    v += 1.0;
     207    asm volatile("fscale"
     208                 : "=t" (v) : "0" (v), "u" (e));
     209    return v;
     210#else
     211    /* Compute ln(x) as ln(1+t) where t = x - 1, x ∈ ]0,1[
     212     *   ln(1+t) = t - t^2/2 + t^3/3 - t^4/4 + t^5/5 ...
     213     * The convergence is quite slow, especially when x is near 0. */
     214    tmp = t = r = x - 1.0;
     215    for(i = 2; i < 30; i++)
     216    {
     217        r *= -t;
     218        tmp += r / i;
     219    }
     220
     221    /* Compute x^-y as e^t where t = y*ln(x):
     222     *   e^t = 1 + t/1! + t^2/2! + t^3/3! + t^4/4! + t^5/5! ...
     223     * The convergence is a lot faster here, thanks to the factorial. */
     224    r = t = - y * tmp;
     225    tmp = 1.0 + t;
     226    for(i = 2; i < 16; i++)
     227    {
     228        r = r * t / i;
     229        tmp += r;
     230    }
     231
     232    /* Return x^y as 1/(x^-y) */
     233    return 1.0 / tmp;
     234#endif
     235}
     236
    177237/**
    178238 * \brief Create an internal bitmap object.
     
    313373
    314374    for(i = 0; i < 4096; i++)
    315         bitmap->gammatab[i] = 4096.0 * cucul_powf((float)i / 4096.0, 1.0 / gamma);
     375        bitmap->gammatab[i] = 4096.0 * gammapow((float)i / 4096.0, 1.0 / gamma);
    316376}
    317377
     
    449509    /* FIXME: choose better characters! */
    450510#if !defined(_DOXYGEN_SKIP_ME)
    451 #   define DCHMAX ((sizeof(density_chars)/sizeof(char const)/4)-1)
     511#   define DCHMAX ((sizeof(density_chars)/sizeof(*density_chars)))
    452512#endif
    453     static char const density_chars[] =
    454         "    "
    455         "...."
    456         "::::"
    457         ";=;="
    458         "tftf"
    459         "%$%$"
    460         "SK&Z"
    461         "XWGM"
    462         "@@@@"
    463         "8888"
    464         "####"
    465         "????";
     513    static char const * density_chars[] =
     514    {
     515#if CP437
     516        " ", ":", "░", "▒", "?"
     517        /* "0", "1", "2", "3", "?" */
     518#else
     519        " ", ".", ":", ";", "t", "%", "S", "X", "@", "8", "?"
     520#endif
     521    };
    466522
    467523    int x, y, w, h, pitch, deltax, deltay;
     
    553609
    554610        enum cucul_color outfg = 0, outbg = 0;
    555         char outch;
     611        char const *outch;
    556612
    557613        r = g = b = a = 0;
     
    676732                }
    677733            }
    678             outch = density_chars[4 * ch];
     734            outch = density_chars[ch];
    679735
    680736            if(qq->dithering == CUCUL_DITHERING_FSTEIN)
     
    696752            else if(ch > (int)(DCHMAX - 1))
    697753                ch = DCHMAX - 1;
    698             outch = density_chars[4 * ch];
     754            outch = density_chars[ch];
    699755
    700756            if(qq->dithering == CUCUL_DITHERING_FSTEIN)
     
    724780        /* Now output the character */
    725781        cucul_set_color(qq, outfg, outbg);
    726         cucul_putchar(qq, x, y, outch);
     782        cucul_putstr(qq, x, y, outch);
    727783
    728784        _increment_dither();
Note: See TracChangeset for help on using the changeset viewer.