source: libpipi/trunk/pipi/resize.c @ 3398

Last change on this file since 3398 was 3398, checked in by Sam Hocevar, 12 years ago

Properly handle alpha components in the resize code.

File size: 3.6 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
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 * resize.c: 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/* FIXME: the algorithm does not handle alpha components properly. Resulting
28 * alpha should be the mean alpha value of the neightbouring pixels, but
29 * the colour components should be weighted with the alpha value. */
30pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)
31{
32    float *srcdata, *dstdata, *aline, *line;
33    pipi_image_t *dst;
34    pipi_pixels_t *srcp, *dstp;
35    int x, y, x0, y0, sw, dw, sh, dh, remy;
36
37    srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
38    srcdata = (float *)srcp->pixels;
39
40    dst = pipi_new(w, h);
41    dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
42    dstdata = (float *)dstp->pixels;
43
44    sw = src->w; sh = src->h;
45    dw = dst->w; dh = dst->h;
46
47    aline = malloc(4 * dw * sizeof(float));
48    line = malloc(4 * dw * sizeof(float));
49
50    memset(line, 0, 4 * dw * sizeof(float));
51    remy = 0;
52
53    for(y = 0, y0 = 0; y < dh; y++)
54    {
55        int toty = 0, ny;
56
57        memset(aline, 0, 4 * dw * sizeof(float));
58
59        while(toty < sh)
60        {
61            if(remy == 0)
62            {
63                float r = 0, g = 0, b = 0, a = 0;
64                int remx = 0;
65
66                for(x = 0, x0 = 0; x < dw; x++)
67                {
68                    float ar = 0, ag = 0, ab = 0, aa = 0;
69                    int totx = 0, nx;
70
71                    while(totx < sw)
72                    {
73                        if(remx == 0)
74                        {
75                            r = srcdata[(y0 * sw + x0) * 4];
76                            g = srcdata[(y0 * sw + x0) * 4 + 1];
77                            b = srcdata[(y0 * sw + x0) * 4 + 2];
78                            a = srcdata[(y0 * sw + x0) * 4 + 3];
79                            x0++;
80                            remx = dw;
81                        }
82
83                        nx = (totx + remx <= sw) ? remx : sw - totx;
84                        ar += nx * r; ag += nx * g; ab += nx * b; aa += nx * a;
85                        totx += nx;
86                        remx -= nx;
87                    }
88
89                    line[4 * x] = ar;
90                    line[4 * x + 1] = ag;
91                    line[4 * x + 2] = ab;
92                    line[4 * x + 3] = aa;
93                }
94
95                y0++;
96                remy = dh;
97            }
98
99            ny = (toty + remy <= sh) ? remy : sh - toty;
100            for(x = 0; x < dw; x++)
101            {
102                aline[4 * x] += ny * line[4 * x];
103                aline[4 * x + 1] += ny * line[4 * x + 1];
104                aline[4 * x + 2] += ny * line[4 * x + 2];
105                aline[4 * x + 3] += ny * line[4 * x + 3];
106            }
107            toty += ny;
108            remy -= ny;
109        }
110
111        for(x = 0; x < dw; x++)
112        {
113            dstdata[(y * dw + x) * 4] = aline[4 * x] / (sw * sh);
114            dstdata[(y * dw + x) * 4 + 1] = aline[4 * x + 1] / (sw * sh);
115            dstdata[(y * dw + x) * 4 + 2] = aline[4 * x + 2] / (sw * sh);
116            dstdata[(y * dw + x) * 4 + 3] = aline[4 * x + 3] / (sw * sh);
117        }
118    }
119
120    free(aline);
121    free(line);
122
123    return dst;
124}
125
Note: See TracBrowser for help on using the repository browser.