Changeset 3398


Ignore:
Timestamp:
02/21/09 15:26:24 (4 years ago)
Author:
sam
Message:

Properly handle alpha components in the resize code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libpipi/trunk/pipi/resize.c

    r3342 r3398  
    2525#include "pipi_internals.h" 
    2626 
     27/* FIXME: the algorithm does not handle alpha components properly. Resulting 
     28 * alpha should be the mean alpha value of the neightbouring pixels, but 
     29 * the colour components should be weighted with the alpha value. */ 
    2730pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h) 
    2831{ 
     
    4245    dw = dst->w; dh = dst->h; 
    4346 
    44     aline = malloc(3 * dw * sizeof(float)); 
    45     line = malloc(3 * dw * sizeof(float)); 
     47    aline = malloc(4 * dw * sizeof(float)); 
     48    line = malloc(4 * dw * sizeof(float)); 
    4649 
    47     memset(line, 0, 3 * dw * sizeof(float)); 
     50    memset(line, 0, 4 * dw * sizeof(float)); 
    4851    remy = 0; 
    4952 
     
    5255        int toty = 0, ny; 
    5356 
    54         memset(aline, 0, 3 * dw * sizeof(float)); 
     57        memset(aline, 0, 4 * dw * sizeof(float)); 
    5558 
    5659        while(toty < sh) 
     
    5861            if(remy == 0) 
    5962            { 
    60                 float r = 0, g = 0, b = 0; 
     63                float r = 0, g = 0, b = 0, a = 0; 
    6164                int remx = 0; 
    6265 
    6366                for(x = 0, x0 = 0; x < dw; x++) 
    6467                { 
    65                     float ar = 0, ag = 0, ab = 0; 
     68                    float ar = 0, ag = 0, ab = 0, aa = 0; 
    6669                    int totx = 0, nx; 
    6770 
     
    7376                            g = srcdata[(y0 * sw + x0) * 4 + 1]; 
    7477                            b = srcdata[(y0 * sw + x0) * 4 + 2]; 
     78                            a = srcdata[(y0 * sw + x0) * 4 + 3]; 
    7579                            x0++; 
    7680                            remx = dw; 
     
    7882 
    7983                        nx = (totx + remx <= sw) ? remx : sw - totx; 
    80                         ar += nx * r; ag += nx * g; ab += nx * b; 
     84                        ar += nx * r; ag += nx * g; ab += nx * b; aa += nx * a; 
    8185                        totx += nx; 
    8286                        remx -= nx; 
    8387                    } 
    8488 
    85                     line[3 * x] = ar; 
    86                     line[3 * x + 1] = ag; 
    87                     line[3 * x + 2] = ab; 
     89                    line[4 * x] = ar; 
     90                    line[4 * x + 1] = ag; 
     91                    line[4 * x + 2] = ab; 
     92                    line[4 * x + 3] = aa; 
    8893                } 
    8994 
     
    95100            for(x = 0; x < dw; x++) 
    96101            { 
    97                 aline[3 * x] += ny * line[3 * x]; 
    98                 aline[3 * x + 1] += ny * line[3 * x + 1]; 
    99                 aline[3 * x + 2] += ny * line[3 * x + 2]; 
     102                aline[4 * x] += ny * line[4 * x]; 
     103                aline[4 * x + 1] += ny * line[4 * x + 1]; 
     104                aline[4 * x + 2] += ny * line[4 * x + 2]; 
     105                aline[4 * x + 3] += ny * line[4 * x + 3]; 
    100106            } 
    101107            toty += ny; 
     
    105111        for(x = 0; x < dw; x++) 
    106112        { 
    107             dstdata[(y * dw + x) * 4] = aline[3 * x] / (sw * sh); 
    108             dstdata[(y * dw + x) * 4 + 1] = aline[3 * x + 1] / (sw * sh); 
    109             dstdata[(y * dw + x) * 4 + 2] = aline[3 * x + 2] / (sw * sh); 
     113            dstdata[(y * dw + x) * 4] = aline[4 * x] / (sw * sh); 
     114            dstdata[(y * dw + x) * 4 + 1] = aline[4 * x + 1] / (sw * sh); 
     115            dstdata[(y * dw + x) * 4 + 2] = aline[4 * x + 2] / (sw * sh); 
     116            dstdata[(y * dw + x) * 4 + 3] = aline[4 * x + 3] / (sw * sh); 
    110117        } 
    111118    } 
Note: See TracChangeset for help on using the changeset viewer.