source: libpipi/trunk/pipi/resample/bresenham.c @ 4691

Last change on this file since 4691 was 4691, checked in by Sam Hocevar, 9 years ago

Avoid floating-point divisions in the Bresenham resize.

  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface library
3 *  Copyright (c) 2004-2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: bresenham.c 4691 2010-10-16 21:52:45Z sam $
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * bresenham.c: Bresenham image resizing functions
17 */
18
19#include "config.h"
20
21#include <stdlib.h>
22#include <string.h>
23
24#include "pipi.h"
25#include "pipi_internals.h"
26
27/* This is Bresenham resizing. I rediscovered it independently but it was
28 * actually first described in 1995 by Tim Kientzle in "Scaling Bitmaps
29 * with Bresenham". */
30
31/* FIXME: the algorithm does not handle alpha components properly. Resulting
32 * alpha should be the mean alpha value of the neightbouring pixels, but
33 * the colour components should be weighted with the alpha value. */
34pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)
35{
36    float *srcdata, *dstdata, *aline, *line;
37    pipi_image_t *dst;
38    pipi_pixels_t *srcp, *dstp;
39    int x, y, x0, y0, sw, dw, sh, dh, remy;
40    float invswsh;
41
42    srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
43    srcdata = (float *)srcp->pixels;
44
45    dst = pipi_new(w, h);
46    dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
47    dstdata = (float *)dstp->pixels;
48
49    sw = src->w; sh = src->h;
50    dw = dst->w; dh = dst->h;
51    invswsh = 1.0f / (sw * sh);
52
53    aline = malloc(4 * dw * sizeof(float));
54    line = malloc(4 * dw * sizeof(float));
55
56    memset(line, 0, 4 * dw * sizeof(float));
57    remy = 0;
58
59    for(y = 0, y0 = 0; y < dh; y++)
60    {
61        int toty = 0, ny;
62
63        memset(aline, 0, 4 * dw * sizeof(float));
64
65        while(toty < sh)
66        {
67            if(remy == 0)
68            {
69                float r = 0, g = 0, b = 0, a = 0;
70                int remx = 0;
71
72                for(x = 0, x0 = 0; x < dw; x++)
73                {
74                    float ar = 0, ag = 0, ab = 0, aa = 0;
75                    int totx = 0, nx;
76
77                    while(totx < sw)
78                    {
79                        if(remx == 0)
80                        {
81                            r = srcdata[(y0 * sw + x0) * 4];
82                            g = srcdata[(y0 * sw + x0) * 4 + 1];
83                            b = srcdata[(y0 * sw + x0) * 4 + 2];
84                            a = srcdata[(y0 * sw + x0) * 4 + 3];
85                            x0++;
86                            remx = dw;
87                        }
88
89                        nx = (totx + remx <= sw) ? remx : sw - totx;
90                        ar += nx * r; ag += nx * g; ab += nx * b; aa += nx * a;
91                        totx += nx;
92                        remx -= nx;
93                    }
94
95                    line[4 * x] = ar;
96                    line[4 * x + 1] = ag;
97                    line[4 * x + 2] = ab;
98                    line[4 * x + 3] = aa;
99                }
100
101                y0++;
102                remy = dh;
103            }
104
105            ny = (toty + remy <= sh) ? remy : sh - toty;
106            for(x = 0; x < dw; x++)
107            {
108                aline[4 * x] += ny * line[4 * x];
109                aline[4 * x + 1] += ny * line[4 * x + 1];
110                aline[4 * x + 2] += ny * line[4 * x + 2];
111                aline[4 * x + 3] += ny * line[4 * x + 3];
112            }
113            toty += ny;
114            remy -= ny;
115        }
116
117        for(x = 0; x < dw; x++)
118        {
119            dstdata[(y * dw + x) * 4] = aline[4 * x] * invswsh;
120            dstdata[(y * dw + x) * 4 + 1] = aline[4 * x + 1] * invswsh;
121            dstdata[(y * dw + x) * 4 + 2] = aline[4 * x + 2] * invswsh;
122            dstdata[(y * dw + x) * 4 + 3] = aline[4 * x + 3] * invswsh;
123        }
124    }
125
126    free(aline);
127    free(line);
128
129    return dst;
130}
131
Note: See TracBrowser for help on using the repository browser.