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
RevLine 
[2244]1/*
[2844]2 *  libpipi       Pathetic image processing interface library
[2244]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
[2246]21#include <stdlib.h>
22#include <string.h>
23
[2260]24#include "pipi.h"
[2244]25#include "pipi_internals.h"
26
[3398]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. */
[2605]30pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)
[2244]31{
[2605]32    float *srcdata, *dstdata, *aline, *line;
[2244]33    pipi_image_t *dst;
[2605]34    pipi_pixels_t *srcp, *dstp;
[2246]35    int x, y, x0, y0, sw, dw, sh, dh, remy;
[2245]36
[3342]37    srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
[2605]38    srcdata = (float *)srcp->pixels;
39
[2244]40    dst = pipi_new(w, h);
[3342]41    dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
[2605]42    dstdata = (float *)dstp->pixels;
[2244]43
[2605]44    sw = src->w; sh = src->h;
45    dw = dst->w; dh = dst->h;
[2244]46
[3398]47    aline = malloc(4 * dw * sizeof(float));
48    line = malloc(4 * dw * sizeof(float));
[2245]49
[3398]50    memset(line, 0, 4 * dw * sizeof(float));
[2246]51    remy = 0;
52
[2605]53    for(y = 0, y0 = 0; y < dh; y++)
[2244]54    {
[2245]55        int toty = 0, ny;
56
[3398]57        memset(aline, 0, 4 * dw * sizeof(float));
[2245]58
59        while(toty < sh)
[2244]60        {
[2245]61            if(remy == 0)
[2244]62            {
[3398]63                float r = 0, g = 0, b = 0, a = 0;
[2246]64                int remx = 0;
65
[2605]66                for(x = 0, x0 = 0; x < dw; x++)
[2244]67                {
[3398]68                    float ar = 0, ag = 0, ab = 0, aa = 0;
[2246]69                    int totx = 0, nx;
[2245]70
71                    while(totx < sw)
72                    {
73                        if(remx == 0)
74                        {
[2605]75                            r = srcdata[(y0 * sw + x0) * 4];
76                            g = srcdata[(y0 * sw + x0) * 4 + 1];
77                            b = srcdata[(y0 * sw + x0) * 4 + 2];
[3398]78                            a = srcdata[(y0 * sw + x0) * 4 + 3];
[2245]79                            x0++;
80                            remx = dw;
81                        }
82
83                        nx = (totx + remx <= sw) ? remx : sw - totx;
[3398]84                        ar += nx * r; ag += nx * g; ab += nx * b; aa += nx * a;
[2245]85                        totx += nx;
86                        remx -= nx;
87                    }
88
[3398]89                    line[4 * x] = ar;
90                    line[4 * x + 1] = ag;
91                    line[4 * x + 2] = ab;
92                    line[4 * x + 3] = aa;
[2244]93                }
94
[2245]95                y0++;
96                remy = dh;
[2244]97            }
98
[2245]99            ny = (toty + remy <= sh) ? remy : sh - toty;
[2605]100            for(x = 0; x < dw; x++)
[2245]101            {
[3398]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];
[2245]106            }
107            toty += ny;
108            remy -= ny;
[2244]109        }
[2245]110
[2605]111        for(x = 0; x < dw; x++)
112        {
[3398]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);
[2605]117        }
[2244]118    }
119
[2245]120    free(aline);
121    free(line);
122
[2244]123    return dst;
124}
125
Note: See TracBrowser for help on using the repository browser.