Ignore:
Timestamp:
08/28/08 19:19:30 (5 years ago)
Author:
sam
Message:
  • Add pipi_order, to transform any image into an ordered dithering matrix.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libpipi/trunk/pipi/dither/ordered.c

    r2696 r2790  
    1919#include "config.h" 
    2020#include "common.h" 
     21 
     22#include <stdlib.h> 
    2123 
    2224#include "pipi.h" 
     
    5759} 
    5860 
     61typedef struct 
     62{ 
     63    int x, y; 
     64    double val; 
     65} 
     66dot_t; 
     67 
     68static int cmpdot(const void *p1, const void *p2) 
     69{ 
     70    return ((dot_t const *)p1)->val > ((dot_t const *)p2)->val; 
     71} 
     72 
     73pipi_image_t *pipi_order(pipi_image_t *src) 
     74{ 
     75    double epsilon; 
     76    pipi_image_t *dst; 
     77    pipi_pixels_t *dstp, *srcp; 
     78    float *dstdata, *srcdata; 
     79    dot_t *circle; 
     80    int x, y, w, h, n; 
     81 
     82    w = src->w; 
     83    h = src->h; 
     84    epsilon = 1. / (w * h + 1); 
     85 
     86    srcp = pipi_getpixels(src, PIPI_PIXELS_Y_F); 
     87    srcdata = (float *)srcp->pixels; 
     88 
     89    dst = pipi_new(w, h); 
     90    dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F); 
     91    dstdata = (float *)dstp->pixels; 
     92 
     93    circle = malloc(w * h * sizeof(dot_t)); 
     94 
     95    for(y = 0; y < h; y++) 
     96        for(x = 0; x < w; x++) 
     97        { 
     98            circle[y * w + x].x = x; 
     99            circle[y * w + x].y = y; 
     100            circle[y * w + x].val = srcdata[y * w + x]; 
     101        } 
     102    qsort(circle, w * h, sizeof(dot_t), cmpdot); 
     103 
     104    for(n = 0; n < w * h; n++) 
     105    { 
     106        x = circle[n].x; 
     107        y = circle[n].y; 
     108        dstdata[y * w + x] = (float)(n + 1) * epsilon; 
     109    } 
     110 
     111    free(circle); 
     112 
     113    return dst; 
     114} 
     115 
Note: See TracChangeset for help on using the changeset viewer.