Changeset 2767 for libpipi


Ignore:
Timestamp:
Aug 25, 2008, 9:13:50 PM (12 years ago)
Author:
Sam Hocevar
Message:
  • screen.c: add pipi_render_halftone() to create a 45-degree clustered dot halftoning pattern.
Location:
libpipi/trunk/pipi
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • libpipi/trunk/pipi/codec.c

    r2759 r2767  
    3131    if(!strncmp(name, "random:", 7) ||
    3232       !strncmp(name, "ediff:", 6) ||
     33       !strncmp(name, "halftone:", 6) ||
    3334       !strncmp(name, "bayer:", 6))
    3435        return pipi_load_stock(name);
  • libpipi/trunk/pipi/pipi.h

    r2765 r2767  
    108108extern pipi_image_t *pipi_render_random(int, int);
    109109extern pipi_image_t *pipi_render_bayer(int, int);
     110extern pipi_image_t *pipi_render_halftone(int, int);
    110111
    111112extern pipi_image_t *pipi_rgb(pipi_image_t *, pipi_image_t *, pipi_image_t *);
  • libpipi/trunk/pipi/render/screen.c

    r2765 r2767  
    2323#include <stdlib.h>
    2424#include <string.h>
     25#include <math.h>
    2526
    2627#include "pipi.h"
     
    6566}
    6667
     68typedef struct
     69{
     70    int x, y;
     71    double dist;
     72}
     73dot_t;
     74
     75static int cmpdot(const void *p1, const void *p2)
     76{
     77    return ((dot_t const *)p1)->dist > ((dot_t const *)p2)->dist;
     78}
     79
     80pipi_image_t *pipi_render_halftone(int w, int h)
     81{
     82    pipi_image_t *ret;
     83    pipi_pixels_t *pix;
     84    float *data;
     85    dot_t *circle;
     86    int x, y, n;
     87
     88    if(w <= 0 || h <= 0)
     89        return NULL;
     90
     91    circle = malloc(w * h * sizeof(dot_t));
     92
     93    for(y = 0; y < h; y++)
     94        for(x = 0; x < w; x++)
     95        {
     96            double dy = ((double)y + .07) / h - .5;
     97            double dx = (double)x / w - .5;
     98            /* Using dx²+dy² here creates another interesting halftone. */
     99            double r = - cos(M_PI * (dx - dy)) - cos(M_PI * (dx + dy));
     100            circle[y * w + x].x = x;
     101            circle[y * w + x].y = y;
     102            circle[y * w + x].dist = r;
     103        }
     104    qsort(circle, w * h, sizeof(dot_t), cmpdot);
     105
     106    ret = pipi_new(w * 2, h * 2);
     107    pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
     108    data = (float *)pix->pixels;
     109
     110    for(n = 0; n < w * h; n++)
     111    {
     112        x = circle[n].x;
     113        y = circle[n].y;
     114
     115        data[y * (2 * w) + x] = (float)(2 * n + 1) / (w * h * 4 + 1);
     116        data[(y + h) * (2 * w) + x + w] = (float)(2 * n + 2) / (w * h * 4 + 1);
     117        data[(y + h) * (2 * w) + x] = 1. - (float)(2 * n + 1) / (w * h * 4 + 1);
     118        data[y * (2 * w) + x + w] = 1. - (float)(2 * n + 2) / (w * h * 4 + 1);
     119    }
     120
     121    free(circle);
     122
     123    return ret;
     124}
     125
  • libpipi/trunk/pipi/stock.c

    r2766 r2767  
    4747    }
    4848
     49    /* Generate a clustered dithering pattern. */
     50    if(!strncmp(name, "halftone:", 9))
     51    {
     52        int w, h = 0;
     53
     54        w = atoi(name + 9);
     55        name = strchr(name + 9, 'x');
     56        if(name)
     57            h = atoi(name + 1);
     58        if(!h)
     59            h = w;
     60
     61        return pipi_render_halftone(w, h);
     62    }
     63
    4964    /* Generate an error diffusion matrix. */
    5065    if(!strncmp(name, "ediff:", 6))
Note: See TracChangeset for help on using the changeset viewer.