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

Last change on this file since 2605 was 2605, checked in by Sam Hocevar, 12 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
RevLine 
[2244]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
[2246]22#include <stdlib.h>
23#include <string.h>
24
[2260]25#include "pipi.h"
[2244]26#include "pipi_internals.h"
27
[2605]28pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)
[2244]29{
[2605]30    float *srcdata, *dstdata, *aline, *line;
[2244]31    pipi_image_t *dst;
[2605]32    pipi_pixels_t *srcp, *dstp;
[2246]33    int x, y, x0, y0, sw, dw, sh, dh, remy;
[2245]34
[2605]35    srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
36    srcdata = (float *)srcp->pixels;
37
[2244]38    dst = pipi_new(w, h);
[2605]39    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
40    dstdata = (float *)dstp->pixels;
[2244]41
[2605]42    sw = src->w; sh = src->h;
43    dw = dst->w; dh = dst->h;
[2244]44
[2605]45    aline = malloc(3 * dw * sizeof(float));
46    line = malloc(3 * dw * sizeof(float));
[2245]47
[2605]48    memset(line, 0, 3 * dw * sizeof(float));
[2246]49    remy = 0;
50
[2605]51    for(y = 0, y0 = 0; y < dh; y++)
[2244]52    {
[2245]53        int toty = 0, ny;
54
[2605]55        memset(aline, 0, 3 * dw * sizeof(float));
[2245]56
57        while(toty < sh)
[2244]58        {
[2245]59            if(remy == 0)
[2244]60            {
[2605]61                float r = 0, g = 0, b = 0;
[2246]62                int remx = 0;
63
[2605]64                for(x = 0, x0 = 0; x < dw; x++)
[2244]65                {
[2605]66                    float ar = 0, ag = 0, ab = 0;
[2246]67                    int totx = 0, nx;
[2245]68
69                    while(totx < sw)
70                    {
71                        if(remx == 0)
72                        {
[2605]73                            r = srcdata[(y0 * sw + x0) * 4];
74                            g = srcdata[(y0 * sw + x0) * 4 + 1];
75                            b = srcdata[(y0 * sw + x0) * 4 + 2];
[2245]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;
[2244]89                }
90
[2245]91                y0++;
92                remy = dh;
[2244]93            }
94
[2245]95            ny = (toty + remy <= sh) ? remy : sh - toty;
[2605]96            for(x = 0; x < dw; x++)
[2245]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;
[2244]104        }
[2245]105
[2605]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        }
[2244]112    }
113
[2245]114    free(aline);
115    free(line);
116
[2244]117    return dst;
118}
119
Note: See TracBrowser for help on using the repository browser.