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

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

Move resize.c to an algorithm-specific file so that we can implement
other resizing methods.

  • Property svn:keywords set to Id
File size: 3.8 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 4690 2010-10-16 21:52:38Z 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
41    srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
42    srcdata = (float *)srcp->pixels;
43
44    dst = pipi_new(w, h);
45    dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
46    dstdata = (float *)dstp->pixels;
47
48    sw = src->w; sh = src->h;
49    dw = dst->w; dh = dst->h;
50
51    aline = malloc(4 * dw * sizeof(float));
52    line = malloc(4 * dw * sizeof(float));
53
54    memset(line, 0, 4 * dw * sizeof(float));
55    remy = 0;
56
57    for(y = 0, y0 = 0; y < dh; y++)
58    {
59        int toty = 0, ny;
60
61        memset(aline, 0, 4 * dw * sizeof(float));
62
63        while(toty < sh)
64        {
65            if(remy == 0)
66            {
67                float r = 0, g = 0, b = 0, a = 0;
68                int remx = 0;
69
70                for(x = 0, x0 = 0; x < dw; x++)
71                {
72                    float ar = 0, ag = 0, ab = 0, aa = 0;
73                    int totx = 0, nx;
74
75                    while(totx < sw)
76                    {
77                        if(remx == 0)
78                        {
79                            r = srcdata[(y0 * sw + x0) * 4];
80                            g = srcdata[(y0 * sw + x0) * 4 + 1];
81                            b = srcdata[(y0 * sw + x0) * 4 + 2];
82                            a = srcdata[(y0 * sw + x0) * 4 + 3];
83                            x0++;
84                            remx = dw;
85                        }
86
87                        nx = (totx + remx <= sw) ? remx : sw - totx;
88                        ar += nx * r; ag += nx * g; ab += nx * b; aa += nx * a;
89                        totx += nx;
90                        remx -= nx;
91                    }
92
93                    line[4 * x] = ar;
94                    line[4 * x + 1] = ag;
95                    line[4 * x + 2] = ab;
96                    line[4 * x + 3] = aa;
97                }
98
99                y0++;
100                remy = dh;
101            }
102
103            ny = (toty + remy <= sh) ? remy : sh - toty;
104            for(x = 0; x < dw; x++)
105            {
106                aline[4 * x] += ny * line[4 * x];
107                aline[4 * x + 1] += ny * line[4 * x + 1];
108                aline[4 * x + 2] += ny * line[4 * x + 2];
109                aline[4 * x + 3] += ny * line[4 * x + 3];
110            }
111            toty += ny;
112            remy -= ny;
113        }
114
115        for(x = 0; x < dw; x++)
116        {
117            dstdata[(y * dw + x) * 4] = aline[4 * x] / (sw * sh);
118            dstdata[(y * dw + x) * 4 + 1] = aline[4 * x + 1] / (sw * sh);
119            dstdata[(y * dw + x) * 4 + 2] = aline[4 * x + 2] / (sw * sh);
120            dstdata[(y * dw + x) * 4 + 3] = aline[4 * x + 3] / (sw * sh);
121        }
122    }
123
124    free(aline);
125    free(line);
126
127    return dst;
128}
129
Note: See TracBrowser for help on using the repository browser.