/* * libpipi Proper image processing implementation library * Copyright (c) 2004-2008 Sam Hocevar * All Rights Reserved * * $Id$ * * This library is free software. It comes without any warranty, to * the extent permitted by applicable law. You can redistribute it * and/or modify it under the terms of the Do What The Fuck You Want * To Public License, Version 2, as published by Sam Hocevar. See * http://sam.zoy.org/wtfpl/COPYING for more details. */ /* * resize.c: image resizing functions */ #include "config.h" #include "common.h" #include #include #include "pipi.h" #include "pipi_internals.h" pipi_image_t *pipi_resize(pipi_image_t const *src, int w, int h) { double *aline, *line; pipi_image_t *dst; int x, y, x0, y0, sw, dw, sh, dh, remy; dst = pipi_new(w, h); sw = src->width; sh = src->height; dw = dst->width; dh = dst->height; aline = malloc(3 * dw * sizeof(double)); line = malloc(3 * dw * sizeof(double)); memset(line, 0, 3 * dw * sizeof(double)); remy = 0; for(y = 0, y0 = 0; y < dst->height; y++) { int toty = 0, ny; memset(aline, 0, 3 * dw * sizeof(double)); while(toty < sh) { if(remy == 0) { double r = 0, g = 0, b = 0; int remx = 0; for(x = 0, x0 = 0; x < dst->width; x++) { double ar = 0, ag = 0, ab = 0; int totx = 0, nx; while(totx < sw) { if(remx == 0) { pipi_getpixel(src, x0, y0, &r, &g, &b); x0++; remx = dw; } nx = (totx + remx <= sw) ? remx : sw - totx; ar += nx * r; ag += nx * g; ab += nx * b; totx += nx; remx -= nx; } line[3 * x] = ar; line[3 * x + 1] = ag; line[3 * x + 2] = ab; } y0++; remy = dh; } ny = (toty + remy <= sh) ? remy : sh - toty; for(x = 0; x < dst->width; x++) { aline[3 * x] += ny * line[3 * x]; aline[3 * x + 1] += ny * line[3 * x + 1]; aline[3 * x + 2] += ny * line[3 * x + 2]; } toty += ny; remy -= ny; } for(x = 0; x < dst->width; x++) pipi_setpixel(dst, x, y, aline[3 * x] / (sw * sh), aline[3 * x + 1] / (sw * sh), aline[3 * x + 2] / (sw * sh)); } free(aline); free(line); return dst; }