[gimp-normalmap] / trunk / scale.c Repository:
ViewVC logotype

View of /trunk/scale.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (download) (as text) (annotate)
Fri Apr 11 20:16:52 2008 UTC (19 months, 1 week ago) by cocidius
File size: 3081 byte(s)
* Update INSTALL, README
* Update contact information if file headers
    1 /*
    2    normalmap GIMP plugin
    3 
    4    Copyright (C) 2002-2008 Shawn Kirst <skirst@insightbb.com>
    5 
    6    This program is free software; you can redistribute it and/or
    7    modify it under the terms of the GNU General Public
    8    License as published by the Free Software Foundation; either
    9    version 2 of the License, or (at your option) any later version.
   10 
   11    This program is distributed in the hope that it will be useful,
   12    but WITHOUT ANY WARRANTY; without even the implied warranty of
   13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14    General Public License for more details.
   15 
   16    You should have received a copy of the GNU General Public License
   17    along with this program; see the file COPYING.  If not, write to
   18    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   19    Boston, MA 02111-1307, USA.
   20 */
   21 
   22 #define LERP(a,b,c) ((a) + ((b) - (a)) * (c))
   23 
   24 float cubic_interpolate(float a, float b, float c, float d, float x)
   25 {
   26    float v0, v1, v2, v3, x2;
   27 
   28    x2 = x * x;
   29    v0 = d - c - a + b;
   30    v1 = a - b - v0;
   31    v2 = c - a;
   32    v3 = b;
   33 
   34    return(v0 * x * x2 + v1 * x2 + v2 * x + v3);
   35 }
   36 
   37 void scale_pixels(unsigned char *dst, int dw, int dh,
   38                   unsigned char *src, int sw, int sh,
   39                   int bpp)
   40 {
   41    int n, x, y;
   42    int ix, iy;
   43    float fx, fy;
   44    float dx, dy, val;
   45    float r0, r1, r2, r3;
   46    int srowbytes = sw * bpp;
   47    int drowbytes = dw * bpp;
   48 
   49 #define VAL(x, y, c) \
   50    (float)src[((y) < 0 ? 0 : (y) >= sh ? sh - 1 : (y)) * srowbytes + \
   51               (((x) < 0 ? 0 : (x) >= sw ? sw - 1 : (x)) * bpp) + c]
   52 
   53    for(y = 0; y < dh; ++y)
   54    {
   55       fy = ((float)y / (float)dh) * (float)sh;
   56       iy = (int)fy;
   57       dy = fy - (float)iy;
   58       for(x = 0; x < dw; ++x)
   59       {
   60          fx = ((float)x / (float)dw) * (float)sw;
   61          ix = (int)fx;
   62          dx = fx - (float)ix;
   63 
   64          for(n = 0; n < bpp; ++n)
   65          {
   66             r0 = cubic_interpolate(VAL(ix - 1, iy - 1, n),
   67                                    VAL(ix,     iy - 1, n),
   68                                    VAL(ix + 1, iy - 1, n),
   69                                    VAL(ix + 2, iy - 1, n), dx);
   70             r1 = cubic_interpolate(VAL(ix - 1, iy,     n),
   71                                    VAL(ix,     iy,     n),
   72                                    VAL(ix + 1, iy,     n),
   73                                    VAL(ix + 2, iy,     n), dx);
   74             r2 = cubic_interpolate(VAL(ix - 1, iy + 1, n),
   75                                    VAL(ix,     iy + 1, n),
   76                                    VAL(ix + 1, iy + 1, n),
   77                                    VAL(ix + 2, iy + 1, n), dx);
   78             r3 = cubic_interpolate(VAL(ix - 1, iy + 2, n),
   79                                    VAL(ix,     iy + 2, n),
   80                                    VAL(ix + 1, iy + 2, n),
   81                                    VAL(ix + 2, iy + 2, n), dx);
   82             val = cubic_interpolate(r0, r1, r2, r3, dy);
   83             if(val <   0) val = 0;
   84             if(val > 255) val = 255;
   85             dst[y * drowbytes + (x * bpp) + n] = (unsigned char)val;
   86          }
   87       }
   88    }
   89 #undef VAL
   90 }

ViewVC Help
Powered by ViewVC 1.0.4