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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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
|
/* -*- C++ -*- */
/*
GAV - Gpl Arcade Volleyball
Copyright (C) 2002
GAV team (http://sourceforge.net/projects/gav/)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* These functions are derived from SDL_buffer.
*/
#include <stdlib.h>
#include "ResizeSurface.h"
typedef struct tColorRGBA {
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 a;
} tColorRGBA;
/*
* 32bit Zoomer with optional anti-aliasing by bilinear interpolation.
* Zoomes 32bit RGBA/ABGR 'src' surface to 'dst' surface.
* Forked from SDL_rotozoom.c, LGPL (c) A. Schiffler
*/
static int zoomSurfaceRGBA (SDL_Surface * src, SDL_Surface * dst)
{
int x, y, //current dst->x and dst->y
xscale, yscale, //src/dst proportions * 65536
*sx_offset, *sy_offset, //row/columnt increment tables
*csx_offset, *csy_offset, //pointers to current row/columnt increment element
csx, csy,
sgap, dgap;
tColorRGBA *sp, *csp, *dp;
// Variables setup
xscale = (int) (65536.0 * (double) src->w / (double) dst->w);
yscale = (int) (65536.0 * (double) src->h / (double) dst->h);
// Allocate memory for row increments
sx_offset = (int*)malloc((dst->w + 1) * sizeof(Uint32));
sy_offset = (int*)malloc((dst->h + 1) * sizeof(Uint32));
if (sx_offset == NULL || sy_offset == NULL) {
free(sx_offset);
free(sy_offset);
return -1;
}
// Precalculate row increments
csx = 0;
csx_offset = sx_offset;
for (x = 0; x <= dst->w; x++) {
*csx_offset = csx;
csx_offset++;
csx &= 0xffff;
csx += xscale;
}
csy = 0;
csy_offset = sy_offset;
for (y = 0; y <= dst->h; y++) {
*csy_offset = csy;
csy_offset++;
csy &= 0xffff;
csy += yscale;
}
// Pointer setup
sp = csp = (tColorRGBA *) src->pixels;
dp = (tColorRGBA *) dst->pixels;
sgap = src->pitch - src->w * 4;
dgap = dst->pitch - dst->w * 4;
// Switch between interpolating and non-interpolating code
// Non-Interpolating Zoom
csy_offset = sy_offset;
for (y = 0; y < dst->h; y++) {
sp = csp;
csx_offset = sx_offset;
for (x = 0; x < dst->w; x++) {
// Draw
*dp = *sp;
// Advance source pointers
csx_offset++;
sp += (*csx_offset >> 16);
// Advance destination pointer
dp++;
}
// Advance source pointer
csy_offset++;
csp = (tColorRGBA *) ((Uint8 *) csp + (*csy_offset >> 16) * src->pitch);
// Advance destination pointers
dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
}
// Remove temp arrays
free(sx_offset);
free(sy_offset);
return 0;
}
SDL_Surface * resizeSurface (SDL_Surface * buf, int width, int height) {
SDL_PixelFormat * format = buf->format;
int dst_w, dst_h;
Uint32 Rmask,Gmask,Bmask,Amask;
SDL_Surface * dest;
SDL_Surface * tmp;
bool toFree = false;
if (format->BitsPerPixel == 32) {
Rmask = format->Rmask;
Gmask = format->Gmask;
Bmask = format->Bmask;
Amask = format->Amask;
} else {
Rmask = 0x000000ff;
Gmask = 0x0000ff00;
Bmask = 0x00ff0000;
Amask = 0xff000000;
}
if (format->BitsPerPixel != 32 ||
format->Rmask != Rmask ||
format->Gmask != Gmask ||
format->Bmask != Bmask) {
// New source surface is 32bit with defined RGBA ordering.
// Note that Amask has been ignored in test
tmp = SDL_CreateRGBSurface (SDL_SWSURFACE, buf->w, buf->h, 32,
Rmask, Gmask, Bmask, Amask);
SDL_BlitSurface (buf, NULL, tmp, NULL);
toFree = true;
} else
tmp = buf;
dst_w = width;
dst_h = height;
// Alloc space to completely contain the zoomed surface
// Target surface is 32bit with source RGBA/ABGR ordering
// (note that buf->zoom is already NULL)
dest = SDL_CreateRGBSurface (SDL_SWSURFACE, width, height, 32,
Rmask, Gmask, Bmask, Amask);
SDL_LockSurface(tmp);
SDL_LockSurface(dest);
zoomSurfaceRGBA(tmp, dest);
// Turn on source-alpha support
SDL_SetAlpha(dest, SDL_SRCALPHA, 255);
SDL_UnlockSurface(tmp);
SDL_UnlockSurface(dest);
if (toFree) SDL_FreeSurface(tmp);
// Return destination surface
return dest;
}
|