Changeset 239


Ignore:
Timestamp:
Nov 30, 2003, 4:56:02 PM (17 years ago)
Author:
Sam Hocevar
Message:
  • src/bitmap.c: + Fine-tuned the new renderer. + Fixed warnings. + Changed hardcoded values to macros.
File:
1 edited

Legend:

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

    r238 r239  
    4343#include "caca_internals.h"
    4444
     45#define NEW_RENDERER 1
     46
    4547static void mask2shift(unsigned int, int *, int *);
    4648
     
    309311                      struct caca_bitmap *bitmap, char *pixels)
    310312{
    311     /* FIXME: this code is shite! */
    312     static int white_colors[] =
     313#if !NEW_RENDERER
     314    static const int white_colors[] =
    313315    {
    314316        CACA_COLOR_BLACK,
     
    317319        CACA_COLOR_WHITE
    318320    };
    319 
    320     static int light_colors[] =
     321#endif
     322
     323    static const int light_colors[] =
    321324    {
    322325        CACA_COLOR_LIGHTMAGENTA,
     
    329332    };
    330333
    331     static int dark_colors[] =
     334    static const int dark_colors[] =
    332335    {
    333336        CACA_COLOR_MAGENTA,
     
    342345    /* FIXME: choose better characters! */
    343346#   define DENSITY_CHARS 13
    344     static char density_chars[] =
     347    static const char density_chars[] =
    345348        "    "
    346349        "    "
     
    389392        enum caca_color outfg, outbg;
    390393        char outch;
    391 #define NEW_DITHERER 1
    392 #ifdef NEW_DITHERER
    393         int distbg, distfg, dist;
     394#if NEW_RENDERER
     395        int distbg, distfg, dist, hue1, hue2;
    394396#endif
    395397
     
    401403        R = 0, G = 0, B = 0;
    402404        get_rgb_default(bitmap, pixels, fromx, fromy, &r, &g, &b);
    403 #if 0
     405#if 1
     406        R += r; G += g; B += b;
     407#else
    404408        R += r; G += g; B += b;
    405409        get_rgb_default(bitmap, pixels, fromx - 1, fromy, &r, &g, &b);
     
    412416        R += r; G += g; B += b;
    413417        R /= 5; G /= 5; B /= 5;
    414 #else
    415         R += r; G += g; B += b;
    416418#endif
    417419
     
    421423        /* The hard work: calculate foreground and background colours,
    422424         * as well as the most appropriate character to output. */
    423 #if NEW_DITHERER
    424 #define XRATIO 5*5
    425 #define YRATIO 3*3
     425#if NEW_RENDERER
     426#       define XRATIO 5*5
     427#       define YRATIO 3*3
     428#       define HRATIO 2*2
     429#       define FUZZINESS XRATIO * 0x800
     430
    426431        /* distance to black */
    427432        distbg = XRATIO * val * val;
    428         distbg += XRATIO * 0x1000 * _get_dither() - XRATIO * 0x8000;
     433        distbg += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
    429434        outbg = CACA_COLOR_BLACK;
    430435
     
    432437        dist = XRATIO * (val - 0x600) * (val - 0x600)
    433438             + YRATIO * sat * sat;
    434         dist += XRATIO * 0x1000 * _get_dither() - XRATIO * 0x8000;
     439        dist += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
     440        dist = dist * 3 / 2;
    435441        if(dist <= distbg)
    436442        {
     
    449455        dist = XRATIO * (val - 0xa00) * (val - 0xa00)
    450456             + YRATIO * sat * sat;
    451         dist += XRATIO * 0x1000 * _get_dither() - XRATIO * 0x8000;
     457        dist += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
     458        dist = dist * 3 / 2;
    452459        if(dist <= distbg)
    453460        {
     
    466473        dist = XRATIO * (val - 0x1000) * (val - 0x1000)
    467474             + YRATIO * sat * sat;
    468         dist += XRATIO * 0x1000 * _get_dither() - XRATIO * 0x8000;
     475        dist += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
    469476        if(dist <= distbg)
    470477        {
     
    480487        }
    481488
    482         /* check dist to dark */
     489        hue1 = (hue + 0x800) & ~0xfff;
     490        if(hue > hue1)
     491            hue2 = (hue + 0x1800) & ~0xfff;
     492        else
     493            hue2 = (hue - 0x800) & ~0xfff;
     494
     495        /* check dist to 2nd closest dark color */
    483496        dist = XRATIO * (val - 0x600) * (val - 0x600)
    484              + YRATIO * (sat - 0x1000) * (sat - 0x1000);
    485         dist += XRATIO * 0x1000 * _get_dither() - XRATIO * 0x8000;
     497             + YRATIO * (sat - 0x1000) * (sat - 0x1000)
     498             + HRATIO * (hue - hue2) * (hue - hue2);
     499        dist += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
    486500        dist = dist * 3 / 4;
    487501        if(dist <= distbg)
     
    489503            outfg = outbg;
    490504            distfg = distbg;
    491             outbg = dark_colors[(hue + _get_dither() * 0x10) / 0x1000];
     505            outbg = dark_colors[hue2 / 0x1000];
    492506            distbg = dist;
    493507        }
    494508        else if(dist <= distfg)
    495509        {
    496             outfg = dark_colors[(hue + _get_dither() * 0x10) / 0x1000];
     510            outfg = dark_colors[hue2 / 0x1000];
    497511            distfg = dist;
    498512        }
    499513
    500         /* check dist to light */
     514        /* check dist to 2nd closest light color */
    501515        dist = XRATIO * (val - 0x1000) * (val - 0x1000)
    502              + YRATIO * (sat - 0x1000) * (sat - 0x1000);
    503         dist += XRATIO * 0x1000 * _get_dither() - XRATIO * 0x8000;
     516             + YRATIO * (sat - 0x1000) * (sat - 0x1000)
     517             + HRATIO * (hue - hue2) * (hue - hue2);
     518        dist += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
    504519        dist = dist / 2;
    505520        if(dist <= distbg)
     
    507522            outfg = outbg;
    508523            distfg = distbg;
    509             outbg = light_colors[(hue + _get_dither() * 0x10) / 0x1000];
     524            outbg = light_colors[hue2 / 0x1000];
    510525            distbg = dist;
    511526        }
    512527        else if(dist <= distfg)
    513528        {
    514             outfg = light_colors[(hue + _get_dither() * 0x10) / 0x1000];
     529            outfg = light_colors[hue2 / 0x1000];
     530            distfg = dist;
     531        }
     532
     533        /* check dist to closest dark color */
     534        dist = XRATIO * (val - 0x600) * (val - 0x600)
     535             + YRATIO * (sat - 0x1000) * (sat - 0x1000)
     536             + HRATIO * (hue - hue1) * (hue - hue1);
     537        dist += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
     538        dist = dist * 3 / 4;
     539        if(dist <= distbg)
     540        {
     541            outfg = outbg;
     542            distfg = distbg;
     543            outbg = dark_colors[hue1 / 0x1000];
     544            distbg = dist;
     545        }
     546        else if(dist <= distfg)
     547        {
     548            outfg = dark_colors[hue1 / 0x1000];
     549            distfg = dist;
     550        }
     551
     552        /* check dist to closest light color */
     553        dist = XRATIO * (val - 0x1000) * (val - 0x1000)
     554             + YRATIO * (sat - 0x1000) * (sat - 0x1000)
     555             + HRATIO * (hue - hue1) * (hue - hue1);
     556        dist += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
     557        dist = dist / 2;
     558        if(dist <= distbg)
     559        {
     560            outfg = outbg;
     561            distfg = distbg;
     562            outbg = light_colors[hue1 / 0x1000];
     563            distbg = dist;
     564        }
     565        else if(dist <= distfg)
     566        {
     567            outfg = light_colors[hue1 / 0x1000];
    515568            distfg = dist;
    516569        }
     
    521574        /* distbg can be > distfg because of dithering fuzziness */
    522575        ch = distbg * 2 * (DENSITY_CHARS - 1) / (distbg + distfg);
    523         ch = 4 * ch /*+ _get_dither() / 0x40*/;
     576        ch = 4 * ch + _get_dither() / 0x40;
    524577        outch = density_chars[ch];
    525578
    526579#else
    527580        outbg = CACA_COLOR_BLACK;
    528         if(sat < 0x200 + _get_dither() * 0x8)
     581        if((unsigned int)sat < 0x200 + _get_dither() * 0x8)
    529582            outfg = white_colors[1 + (val * 2 + _get_dither() * 0x10) / 0x1000];
    530         else if(val > 0x800 + _get_dither() * 0x4)
     583        else if((unsigned int)val > 0x800 + _get_dither() * 0x4)
    531584            outfg = light_colors[(hue + _get_dither() * 0x10) / 0x1000];
    532585        else
Note: See TracChangeset for help on using the changeset viewer.