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

Last change on this file since 2246 was 2246, checked in by Sam Hocevar, 15 years ago
  • Fixed a bug in the naive resizing code.
File size: 2.8 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_internals.h"
26#include "pipi.h"
27
28pipi_image_t *pipi_resize(pipi_image_t const *src, int w, int h)
29{
30    int *aline, *line;
31    pipi_image_t *dst;
32    int x, y, x0, y0, sw, dw, sh, dh, remy;
33
34    dst = pipi_new(w, h);
35
36    sw = src->width; sh = src->height;
37    dw = dst->width; dh = dst->height;
38
39    aline = malloc(3 * dw * sizeof(int));
40    line = malloc(3 * dw * sizeof(int));
41
42    memset(line, 0, 3 * dw * sizeof(int));
43    remy = 0;
44
45    for(y = 0, y0 = 0; y < dst->height; y++)
46    {
47        int toty = 0, ny;
48
49        memset(aline, 0, 3 * dw * sizeof(int));
50
51        while(toty < sh)
52        {
53            if(remy == 0)
54            {
55                int r = 0, g = 0, b = 0;
56                int remx = 0;
57
58                for(x = 0, x0 = 0; x < dst->width; x++)
59                {
60                    int ar = 0, ag = 0, ab = 0;
61                    int totx = 0, nx;
62
63                    while(totx < sw)
64                    {
65                        if(remx == 0)
66                        {
67                            pipi_getpixel(src, x0, y0, &r, &g, &b);
68                            x0++;
69                            remx = dw;
70                        }
71
72                        nx = (totx + remx <= sw) ? remx : sw - totx;
73                        ar += nx * r; ag += nx * g; ab += nx * b;
74                        totx += nx;
75                        remx -= nx;
76                    }
77
78                    line[3 * x] = ar;
79                    line[3 * x + 1] = ag;
80                    line[3 * x + 2] = ab;
81                }
82
83                y0++;
84                remy = dh;
85            }
86
87            ny = (toty + remy <= sh) ? remy : sh - toty;
88            for(x = 0; x < dst->width; x++)
89            {
90                aline[3 * x] += ny * line[3 * x];
91                aline[3 * x + 1] += ny * line[3 * x + 1];
92                aline[3 * x + 2] += ny * line[3 * x + 2];
93            }
94            toty += ny;
95            remy -= ny;
96        }
97
98        for(x = 0; x < dst->width; x++)
99            pipi_setpixel(dst, x, y,
100                          aline[3 * x] / (sw * sh),
101                          aline[3 * x + 1] / (sw * sh),
102                          aline[3 * x + 2] / (sw * sh));
103    }
104
105    free(aline);
106    free(line);
107
108    return dst;
109}
110
Note: See TracBrowser for help on using the repository browser.