Changeset 2777 for libpipi


Ignore:
Timestamp:
Aug 27, 2008, 1:17:26 PM (12 years ago)
Author:
Jean-Yves Lamoureux
Message:
  • Added Wu-Xaolin antialiased lines (Still lacks RGBA32 transparency code, with a fallback to float for now)
Location:
libpipi/trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • libpipi/trunk/examples/line.c

    r2776 r2777  
    3333    pipi_free(img);
    3434
     35    int w = pipi_get_image_width(newimg);
     36    int h = pipi_get_image_height(newimg);
     37
    3538    while(count--) {
    3639        pipi_draw_line(newimg,
    37                        rand() % 500, rand() % 500,
    38                        rand() % 500, rand() % 500,
    39                        rand());
     40                       rand() % w, rand() % h,
     41                       rand() % w, rand() % h,
     42                       rand(),
     43                       1);
    4044    }
     45
    4146    pipi_save(newimg, dstname);
    4247
  • libpipi/trunk/pipi/paint/line.c

    r2776 r2777  
    4949static void draw_aliased_line_gray(pipi_image_t *img, struct line* s);
    5050static void draw_aliased_line_float(pipi_image_t *img, struct line* s);
    51 
    52 
    53 
    54 int pipi_draw_line(pipi_image_t *img , int x1, int y1, int x2, int y2, uint32_t c)
     51static void draw_antialiased_line_float(pipi_image_t *img, struct line* s);
     52static void draw_antialiased_line_gray(pipi_image_t *img, struct line* s);
     53
     54
     55
     56int pipi_draw_line(pipi_image_t *img , int x1, int y1, int x2, int y2, uint32_t c, int aa)
    5557{
    5658    struct line s;
     
    6062    s.y2 = y2;
    6163
    62     if(img->last_modified == PIPI_PIXELS_RGBA_C) {
     64    /* No Transparency routine for u32 yet, fallback to float version */
     65    if(img->last_modified == PIPI_PIXELS_RGBA_C)
     66    {
     67        if(!aa)
     68        {
     69            uint32_t  *dstdata;
     70            dstdata = (uint32_t *)pipi_getpixels(img, PIPI_PIXELS_RGBA_C)->pixels;
     71            s.color32 = c;
     72            s.buf_u32 = dstdata;
     73            s.draw = draw_aliased_line_u32;
     74        }
     75        else
     76        {
     77            float  *dstdata;
     78            dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_RGBA_F)->pixels;
     79            s.colorf[2] = ((c&0x00FF0000)>>16)/255.0f; /* XXX FIXME */
     80            s.colorf[1] = ((c&0x0000FF00)>>8)/255.0f;  /* XXX FIXME */
     81            s.colorf[0] = (c&0x000000FF)/255.0f;       /* XXX FIXME */
     82            s.buf_f = dstdata;
     83            s.draw = draw_antialiased_line_float;
     84        }
     85    }
     86    else if(img->last_modified == PIPI_PIXELS_Y_F)
     87    {
     88        float  *dstdata;
     89        dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_Y_F)->pixels;
     90        s.colorf[0] = c/255.0f; /* XXX FIXME */
     91        s.buf_f = dstdata;
     92        s.draw = aa==0?draw_aliased_line_gray:draw_antialiased_line_gray;
     93    }
     94    else
     95    {
     96        float  *dstdata;
     97        dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_RGBA_F)->pixels;
     98        s.colorf[2] = ((c&0x00FF0000)>>16)/255.0f; /* XXX FIXME */
     99        s.colorf[1] = ((c&0x0000FF00)>>8)/255.0f;  /* XXX FIXME */
     100        s.colorf[0] = (c&0x000000FF)/255.0f;       /* XXX FIXME */
     101        s.buf_f = dstdata;
     102        s.draw = aa==0?draw_aliased_line_float:draw_antialiased_line_float;
     103    }
     104
     105    clip_line(img, &s);
     106    return 0;
     107}
     108
     109
     110int pipi_draw_polyline(pipi_image_t *img, int const x[], int const y[],
     111                       int n, uint32_t c)
     112{
     113    int i;
     114    struct line s;
     115
     116    if(img->last_modified == PIPI_PIXELS_RGBA_C)
     117    {
    63118        uint32_t  *dstdata;
    64119        dstdata = (uint32_t *)pipi_getpixels(img, PIPI_PIXELS_RGBA_C)->pixels;
     
    66121        s.buf_u32 = dstdata;
    67122        s.draw = draw_aliased_line_u32;
    68     } else if(img->last_modified == PIPI_PIXELS_Y_F) {
    69         float  *dstdata;
    70         dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_Y_F)->pixels;
    71         s.colorf[0] = c/255.0f; /* XXX FIXME */
    72         s.buf_f = dstdata;
    73         s.draw = draw_aliased_line_gray;
    74 
    75 
    76     } else {
    77         float  *dstdata;
    78         dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_RGBA_F)->pixels;
    79         s.colorf[0] = (c&0x00FF0000)/255.0f; /* XXX FIXME */
    80         s.colorf[1] = (c&0x0000FF00)/255.0f; /* XXX FIXME */
    81         s.colorf[2] = (c&0x000000FF)/255.0f; /* XXX FIXME */
    82         s.buf_f = dstdata;
    83         s.draw = draw_aliased_line_float;
    84     }
    85 
    86     clip_line(img, &s);
    87     return 0;
    88 }
    89 
    90 
    91 int pipi_draw_polyline(pipi_image_t *img, int const x[], int const y[],
    92                        int n, uint32_t c)
    93 {
    94     int i;
    95     struct line s;
    96 
    97     if(img->last_modified == PIPI_PIXELS_RGBA_C) {
    98         uint32_t  *dstdata;
    99         dstdata = (uint32_t *)pipi_getpixels(img, PIPI_PIXELS_RGBA_C)->pixels;
    100         s.color32 = c;
    101         s.buf_u32 = dstdata;
    102         s.draw = draw_aliased_line_u32;
    103     } else {
     123    }
     124    else
     125    {
    104126        float  *dstdata;
    105127        dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_RGBA_F)->pixels;
     
    205227static void draw_aliased_line_u32(pipi_image_t *img, struct line* s)
    206228{
    207  #undef  ASSIGN
    208  #define ASSIGN(x, y, w) s->buf_u32[x+y*w] = s->color32;
    209  #include "line_template.c"
     229#undef  ASSIGN
     230#define ASSIGN(x, y, w) s->buf_u32[x+y*w] = s->color32;
     231#include "line_template.c"
    210232}
    211233static void draw_aliased_line_float(pipi_image_t *img, struct line* s)
    212234{
    213  #undef  ASSIGN
    214  #define ASSIGN(x, y, w) s->buf_f[(x*4)+y*(w*4)]     = s->colorf[0]; \
     235#undef  ASSIGN
     236#define ASSIGN(x, y, w) s->buf_f[(x*4)+y*(w*4)]     = s->colorf[0]; \
    215237                         s->buf_f[1 + (x*4)+y*(w*4)] = s->colorf[1]; \
    216238                         s->buf_f[2 + (x*4)+y*(w*4)] = s->colorf[2];
    217  #include "line_template.c"
     239#include "line_template.c"
    218240}
    219241static void draw_aliased_line_gray(pipi_image_t *img, struct line* s)
    220242{
    221  #undef  ASSIGN
    222  #define ASSIGN(x, y, w) s->buf_f[x+y*w]     = s->colorf[0];
    223  #include "line_template.c"
    224 }
     243#undef  ASSIGN
     244#define ASSIGN(x, y, w) s->buf_f[x+y*w]     = s->colorf[0];
     245#include "line_template.c"
     246}
     247
     248/* Xiaolin Wu's line algorithm, as seen at http://portal.acm.org/citation.cfm?id=122734 */
     249
     250/* math.h doesn't like y0 (sucker) */
     251float floorf(float x);
     252float truncf(float x);
     253float fabsf(float x);
     254static float fractf(float d) { return (d - floorf(d)); }
     255static float fractinvf(float d) { return (1 - (d - floorf(d))); }
     256
     257static void draw_antialiased_line_float(pipi_image_t *img, struct line* s)
     258{
     259/* Is that an horrible mess ? Yes, it is. */
     260#undef  PLOT
     261#define PLOT(x, y, c)  \
     262    s->buf_f[(((int)(x)*4))+((int)(y))*(img->w*4)] = \
     263        (c*s->colorf[0]) + (1-c) * s->buf_f[(((int)(x)*4))+((int)(y))*(img->w*4)]; \
     264    s->buf_f[(1+((int)(x)*4))+((int)(y))*(img->w*4)] = \
     265        (c*s->colorf[1]) + (1-c) * s->buf_f[(1+((int)(x)*4))+((int)(y))*(img->w*4)]; \
     266    s->buf_f[(2+((int)(x)*4))+((int)(y))*(img->w*4)] = \
     267        (c*s->colorf[2]) + (1-c) * s->buf_f[(2+((int)(x)*4))+((int)(y))*(img->w*4)];
     268#include "aline_template.c"
     269}
     270
     271
     272static void draw_antialiased_line_gray(pipi_image_t *img, struct line* s)
     273{
     274#undef  PLOT
     275#define PLOT(x, y, c) s->buf_f[((int)(x))+((int)(y))*img->w] =  \
     276    (c*s->colorf[0]) + (1-c) * s->buf_f[((int)(x))+((int)(y))*img->w];
     277#include "aline_template.c"
     278}
  • libpipi/trunk/pipi/pipi.h

    r2776 r2777  
    102102
    103103extern pipi_pixels_t *pipi_getpixels(pipi_image_t *, pipi_format_t);
     104extern int pipi_get_image_width(pipi_image_t *img);
     105extern int pipi_get_image_height(pipi_image_t *img);
     106extern int pipi_get_image_pitch(pipi_image_t *img);
    104107
    105108extern double pipi_measure_msd(pipi_image_t *, pipi_image_t *);
     
    151154extern int pipi_flood_fill(pipi_image_t *,
    152155                           int, int, float, float, float, float);
    153 extern int pipi_draw_line(pipi_image_t *, int, int, int, int, uint32_t);
     156extern int pipi_draw_line(pipi_image_t *, int, int, int, int, uint32_t, int);
    154157extern int pipi_draw_polyline(pipi_image_t *, int const[], int const[],
    155158                              int , uint32_t);
  • libpipi/trunk/pipi/pixels.c

    r2775 r2777  
    267267}
    268268
     269/* Accessors */
     270int pipi_get_image_width(pipi_image_t *img)
     271{
     272    return img->w;
     273}
     274int pipi_get_image_height(pipi_image_t *img)
     275{
     276    return img->h;
     277}
     278int pipi_get_image_pitch(pipi_image_t *img)
     279{
     280    return img->pitch;
     281}
Note: See TracChangeset for help on using the changeset viewer.