Browse Source

Properly handle alpha components in the resize code.

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@3398 92316355-f0b4-4df1-b90c-862c8a59935f
master
sam 17 years ago
parent
commit
1b42147e18
1 changed files with 23 additions and 16 deletions
  1. +23
    -16
      pipi/resize.c

+ 23
- 16
pipi/resize.c View File

@@ -24,6 +24,9 @@
#include "pipi.h" #include "pipi.h"
#include "pipi_internals.h" #include "pipi_internals.h"


/* FIXME: the algorithm does not handle alpha components properly. Resulting
* alpha should be the mean alpha value of the neightbouring pixels, but
* the colour components should be weighted with the alpha value. */
pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h) pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)
{ {
float *srcdata, *dstdata, *aline, *line; float *srcdata, *dstdata, *aline, *line;
@@ -41,28 +44,28 @@ pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)
sw = src->w; sh = src->h; sw = src->w; sh = src->h;
dw = dst->w; dh = dst->h; dw = dst->w; dh = dst->h;


aline = malloc(3 * dw * sizeof(float));
line = malloc(3 * dw * sizeof(float));
aline = malloc(4 * dw * sizeof(float));
line = malloc(4 * dw * sizeof(float));


memset(line, 0, 3 * dw * sizeof(float));
memset(line, 0, 4 * dw * sizeof(float));
remy = 0; remy = 0;


for(y = 0, y0 = 0; y < dh; y++) for(y = 0, y0 = 0; y < dh; y++)
{ {
int toty = 0, ny; int toty = 0, ny;


memset(aline, 0, 3 * dw * sizeof(float));
memset(aline, 0, 4 * dw * sizeof(float));


while(toty < sh) while(toty < sh)
{ {
if(remy == 0) if(remy == 0)
{ {
float r = 0, g = 0, b = 0;
float r = 0, g = 0, b = 0, a = 0;
int remx = 0; int remx = 0;


for(x = 0, x0 = 0; x < dw; x++) for(x = 0, x0 = 0; x < dw; x++)
{ {
float ar = 0, ag = 0, ab = 0;
float ar = 0, ag = 0, ab = 0, aa = 0;
int totx = 0, nx; int totx = 0, nx;


while(totx < sw) while(totx < sw)
@@ -72,19 +75,21 @@ pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)
r = srcdata[(y0 * sw + x0) * 4]; r = srcdata[(y0 * sw + x0) * 4];
g = srcdata[(y0 * sw + x0) * 4 + 1]; g = srcdata[(y0 * sw + x0) * 4 + 1];
b = srcdata[(y0 * sw + x0) * 4 + 2]; b = srcdata[(y0 * sw + x0) * 4 + 2];
a = srcdata[(y0 * sw + x0) * 4 + 3];
x0++; x0++;
remx = dw; remx = dw;
} }


nx = (totx + remx <= sw) ? remx : sw - totx; nx = (totx + remx <= sw) ? remx : sw - totx;
ar += nx * r; ag += nx * g; ab += nx * b;
ar += nx * r; ag += nx * g; ab += nx * b; aa += nx * a;
totx += nx; totx += nx;
remx -= nx; remx -= nx;
} }


line[3 * x] = ar;
line[3 * x + 1] = ag;
line[3 * x + 2] = ab;
line[4 * x] = ar;
line[4 * x + 1] = ag;
line[4 * x + 2] = ab;
line[4 * x + 3] = aa;
} }


y0++; y0++;
@@ -94,9 +99,10 @@ pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)
ny = (toty + remy <= sh) ? remy : sh - toty; ny = (toty + remy <= sh) ? remy : sh - toty;
for(x = 0; x < dw; x++) for(x = 0; x < dw; 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];
aline[4 * x] += ny * line[4 * x];
aline[4 * x + 1] += ny * line[4 * x + 1];
aline[4 * x + 2] += ny * line[4 * x + 2];
aline[4 * x + 3] += ny * line[4 * x + 3];
} }
toty += ny; toty += ny;
remy -= ny; remy -= ny;
@@ -104,9 +110,10 @@ pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)


for(x = 0; x < dw; x++) for(x = 0; x < dw; x++)
{ {
dstdata[(y * dw + x) * 4] = aline[3 * x] / (sw * sh);
dstdata[(y * dw + x) * 4 + 1] = aline[3 * x + 1] / (sw * sh);
dstdata[(y * dw + x) * 4 + 2] = aline[3 * x + 2] / (sw * sh);
dstdata[(y * dw + x) * 4] = aline[4 * x] / (sw * sh);
dstdata[(y * dw + x) * 4 + 1] = aline[4 * x + 1] / (sw * sh);
dstdata[(y * dw + x) * 4 + 2] = aline[4 * x + 2] / (sw * sh);
dstdata[(y * dw + x) * 4 + 3] = aline[4 * x + 3] / (sw * sh);
} }
} }




Loading…
Cancel
Save