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

Last change on this file since 2709 was 2605, checked in by Sam Hocevar, 15 years ago
  • Big API reorganisation. Now libpipi can transparently convert between colour spaces for a given image. For instance, if pipi_gaussian_blur is applied to a 32-bpp image, it is automatically converted to gamma-corrected 32-bit floats beforehands, then converted back to normal.
  • TODO: clipping, regions of interest, more formats, getpixel macros...
File size: 3.2 KB
Line 
1/*
2 *  libpipi       Proper image processing implementation 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#include "common.h"
21
22#include <stdlib.h>
23#include <string.h>
24
25#include "pipi.h"
26#include "pipi_internals.h"
27
28pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)
29{
30    float *srcdata, *dstdata, *aline, *line;
31    pipi_image_t *dst;
32    pipi_pixels_t *srcp, *dstp;
33    int x, y, x0, y0, sw, dw, sh, dh, remy;
34
35    srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
36    srcdata = (float *)srcp->pixels;
37
38    dst = pipi_new(w, h);
39    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
40    dstdata = (float *)dstp->pixels;
41
42    sw = src->w; sh = src->h;
43    dw = dst->w; dh = dst->h;
44
45    aline = malloc(3 * dw * sizeof(float));
46    line = malloc(3 * dw * sizeof(float));
47
48    memset(line, 0, 3 * dw * sizeof(float));
49    remy = 0;
50
51    for(y = 0, y0 = 0; y < dh; y++)
52    {
53        int toty = 0, ny;
54
55        memset(aline, 0, 3 * dw * sizeof(float));
56
57        while(toty < sh)
58        {
59            if(remy == 0)
60            {
61                float r = 0, g = 0, b = 0;
62                int remx = 0;
63
64                for(x = 0, x0 = 0; x < dw; x++)
65                {
66                    float ar = 0, ag = 0, ab = 0;
67                    int totx = 0, nx;
68
69                    while(totx < sw)
70                    {
71                        if(remx == 0)
72                        {
73                            r = srcdata[(y0 * sw + x0) * 4];
74                            g = srcdata[(y0 * sw + x0) * 4 + 1];
75                            b = srcdata[(y0 * sw + x0) * 4 + 2];
76                            x0++;
77                            remx = dw;
78                        }
79
80                        nx = (totx + remx <= sw) ? remx : sw - totx;
81                        ar += nx * r; ag += nx * g; ab += nx * b;
82                        totx += nx;
83                        remx -= nx;
84                    }
85
86                    line[3 * x] = ar;
87                    line[3 * x + 1] = ag;
88                    line[3 * x + 2] = ab;
89                }
90
91                y0++;
92                remy = dh;
93            }
94
95            ny = (toty + remy <= sh) ? remy : sh - toty;
96            for(x = 0; x < dw; x++)
97            {
98                aline[3 * x] += ny * line[3 * x];
99                aline[3 * x + 1] += ny * line[3 * x + 1];
100                aline[3 * x + 2] += ny * line[3 * x + 2];
101            }
102            toty += ny;
103            remy -= ny;
104        }
105
106        for(x = 0; x < dw; x++)
107        {
108            dstdata[(y * dw + x) * 4] = aline[3 * x] / (sw * sh);
109            dstdata[(y * dw + x) * 4 + 1] = aline[3 * x + 1] / (sw * sh);
110            dstdata[(y * dw + x) * 4 + 2] = aline[3 * x + 2] / (sw * sh);
111        }
112    }
113
114    free(aline);
115    free(line);
116
117    return dst;
118}
119
Note: See TracBrowser for help on using the repository browser.