sdl_zoom_template.h 6.9 KB
Newer Older
S
Stefano Stabellini 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * SDL_zoom_template - surface scaling
 * 
 * Copyright (c) 2009 Citrix Systems, Inc.
 *
 * Derived from: SDL_rotozoom,  LGPL (c) A. Schiffler from the SDL_gfx library.
 * Modifications by Stefano Stabellini.
 *
 * This work is licensed under the terms of the GNU GPL version 2.
 * See the COPYING file in the top-level directory.
 *
 */

#if BPP == 16
#define SDL_TYPE Uint16
#elif BPP == 32
#define SDL_TYPE Uint32
#else
#error unsupport depth
#endif

/*  
 *  Simple helper functions to make the code looks nicer
 *
 *  Assume spf = source SDL_PixelFormat
 *         dpf = dest SDL_PixelFormat
 *
 */
#define getRed(color)   (((color) & spf->Rmask) >> spf->Rshift)
#define getGreen(color) (((color) & spf->Gmask) >> spf->Gshift)
#define getBlue(color)  (((color) & spf->Bmask) >> spf->Bshift)
#define getAlpha(color) (((color) & spf->Amask) >> spf->Ashift)

#define setRed(r, pcolor) do { \
    *pcolor = ((*pcolor) & (~(dpf->Rmask))) + \
              (((r) & (dpf->Rmask >> dpf->Rshift)) << dpf->Rshift); \
37
} while (0)
S
Stefano Stabellini 已提交
38 39 40 41

#define setGreen(g, pcolor) do { \
    *pcolor = ((*pcolor) & (~(dpf->Gmask))) + \
              (((g) & (dpf->Gmask >> dpf->Gshift)) << dpf->Gshift); \
42
} while (0)
S
Stefano Stabellini 已提交
43 44 45 46

#define setBlue(b, pcolor) do { \
    *pcolor = ((*pcolor) & (~(dpf->Bmask))) + \
              (((b) & (dpf->Bmask >> dpf->Bshift)) << dpf->Bshift); \
47
} while (0)
S
Stefano Stabellini 已提交
48 49 50 51

#define setAlpha(a, pcolor) do { \
    *pcolor = ((*pcolor) & (~(dpf->Amask))) + \
              (((a) & (dpf->Amask >> dpf->Ashift)) << dpf->Ashift); \
52
} while (0)
S
Stefano Stabellini 已提交
53

54
static void glue(sdl_zoom_rgb, BPP)(SDL_Surface *src, SDL_Surface *dst, int smooth,
S
Stefano Stabellini 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
                                   SDL_Rect *dst_rect)
{
    int x, y, sx, sy, *sax, *say, *csax, *csay, csx, csy, ex, ey, t1, t2, sstep, sstep_jump;
    SDL_TYPE *c00, *c01, *c10, *c11, *sp, *csp, *dp;
    int d_gap;
    SDL_PixelFormat *spf = src->format;
    SDL_PixelFormat *dpf = dst->format;

    if (smooth) { 
        /* For interpolation: assume source dimension is one pixel.
         * Smaller here to avoid overflow on right and bottom edge.
         */
        sx = (int) (65536.0 * (float) (src->w - 1) / (float) dst->w);
        sy = (int) (65536.0 * (float) (src->h - 1) / (float) dst->h);
    } else {
        sx = (int) (65536.0 * (float) src->w / (float) dst->w);
        sy = (int) (65536.0 * (float) src->h / (float) dst->h);
    }

74 75
    sax = g_new(int, dst->w + 1);
    say = g_new(int, dst->h + 1);
S
Stefano Stabellini 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

    sp = csp = (SDL_TYPE *) src->pixels;
    dp = (SDL_TYPE *) (dst->pixels + dst_rect->y * dst->pitch +
                       dst_rect->x * dst->format->BytesPerPixel);

    csx = 0;
    csax = sax;
    for (x = 0; x <= dst->w; x++) {
        *csax = csx;
        csax++;
        csx &= 0xffff;
        csx += sx;
    }
    csy = 0;
    csay = say;
    for (y = 0; y <= dst->h; y++) {
        *csay = csy;
        csay++;
        csy &= 0xffff;
        csy += sy;
    }

    d_gap = dst->pitch - dst_rect->w * dst->format->BytesPerPixel;

    if (smooth) {
        csay = say;
        for (y = 0; y < dst_rect->y; y++) {
            csay++;
            sstep = (*csay >> 16) * src->pitch;
            csp = (SDL_TYPE *) ((Uint8 *) csp + sstep);
        }

        /* Calculate sstep_jump */
        csax = sax; 
        sstep_jump = 0;
        for (x = 0; x < dst_rect->x; x++) {
            csax++; 
            sstep = (*csax >> 16);
            sstep_jump += sstep;
        }

        for (y = 0; y < dst_rect->h ; y++) {
            /* Setup colour source pointers */
            c00 = csp + sstep_jump;
            c01 = c00 + 1;
            c10 = (SDL_TYPE *) ((Uint8 *) csp + src->pitch) + sstep_jump;
            c11 = c10 + 1;
            csax = sax + dst_rect->x; 

            for (x = 0; x < dst_rect->w; x++) {

                /* Interpolate colours */
                ex = (*csax & 0xffff);
                ey = (*csay & 0xffff);
                t1 = ((((getRed(*c01) - getRed(*c00)) * ex) >> 16) +
                     getRed(*c00)) & (dpf->Rmask >> dpf->Rshift);
                t2 = ((((getRed(*c11) - getRed(*c10)) * ex) >> 16) +
                     getRed(*c10)) & (dpf->Rmask >> dpf->Rshift);
                setRed((((t2 - t1) * ey) >> 16) + t1, dp);
                t1 = ((((getGreen(*c01) - getGreen(*c00)) * ex) >> 16) +
                     getGreen(*c00)) & (dpf->Gmask >> dpf->Gshift);
                t2 = ((((getGreen(*c11) - getGreen(*c10)) * ex) >> 16) +
                     getGreen(*c10)) & (dpf->Gmask >> dpf->Gshift);
                setGreen((((t2 - t1) * ey) >> 16) + t1, dp);
                t1 = ((((getBlue(*c01) - getBlue(*c00)) * ex) >> 16) +
                     getBlue(*c00)) & (dpf->Bmask >> dpf->Bshift);
                t2 = ((((getBlue(*c11) - getBlue(*c10)) * ex) >> 16) +
                     getBlue(*c10)) & (dpf->Bmask >> dpf->Bshift);
                setBlue((((t2 - t1) * ey) >> 16) + t1, dp);
                t1 = ((((getAlpha(*c01) - getAlpha(*c00)) * ex) >> 16) +
                     getAlpha(*c00)) & (dpf->Amask >> dpf->Ashift);
                t2 = ((((getAlpha(*c11) - getAlpha(*c10)) * ex) >> 16) +
                     getAlpha(*c10)) & (dpf->Amask >> dpf->Ashift);
                setAlpha((((t2 - t1) * ey) >> 16) + t1, dp); 

                /* Advance source pointers */
                csax++; 
                sstep = (*csax >> 16);
                c00 += sstep;
                c01 += sstep;
                c10 += sstep;
                c11 += sstep;
                /* Advance destination pointer */
                dp++;
            }
            /* Advance source pointer */
            csay++;
            csp = (SDL_TYPE *) ((Uint8 *) csp + (*csay >> 16) * src->pitch);
            /* Advance destination pointers */
            dp = (SDL_TYPE *) ((Uint8 *) dp + d_gap);
        }


    } else {
        csay = say;

        for (y = 0; y < dst_rect->y; y++) {
            csay++;
            sstep = (*csay >> 16) * src->pitch;
            csp = (SDL_TYPE *) ((Uint8 *) csp + sstep);
        }

        /* Calculate sstep_jump */
        csax = sax; 
        sstep_jump = 0;
        for (x = 0; x < dst_rect->x; x++) {
            csax++; 
            sstep = (*csax >> 16);
            sstep_jump += sstep;
        }

        for (y = 0 ; y < dst_rect->h ; y++) {
            sp = csp + sstep_jump;
            csax = sax + dst_rect->x;

            for (x = 0; x < dst_rect->w; x++) {

                /* Draw */
                *dp = *sp;

                /* Advance source pointers */
                csax++;
                sstep = (*csax >> 16);
                sp += sstep;

                /* Advance destination pointer */
                dp++;
            }
            /* Advance source pointers */
            csay++;
            sstep = (*csay >> 16) * src->pitch;
            csp = (SDL_TYPE *) ((Uint8 *) csp + sstep);

            /* Advance destination pointer */
            dp = (SDL_TYPE *) ((Uint8 *) dp + d_gap);
        }
    }

214 215
    g_free(sax);
    g_free(say);
S
Stefano Stabellini 已提交
216 217 218 219
}

#undef SDL_TYPE