Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

sdlstretch.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           SDLStretch.cpp  -  description
00003                              -------------------
00004     begin                : Thu Nov 11 1999
00005     copyright            : (C) 1999 by Alexander Pipelka
00006     email                : pipelka@teleweb.at
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010 
00011     This library is free software; you can redistribute it and/or
00012     modify it under the terms of the GNU Library General Public
00013     License as published by the Free Software Foundation; either
00014     version 2 of the License, or (at your option) any later version.
00015 
00016     This library is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019     Library General Public License for more details.
00020 
00021  ***************************************************************************/
00022 
00023 
00024  
00025 #include "../global.h"
00026 #include <stdio.h>
00027 #include <cstring>
00028 #include <math.h>
00029 #include <stdlib.h>
00030 #include "sdlstretch.h"
00031 
00032 #define sign(x) ((x)>0 ? 1:-1)
00033 
00034 
00035 template<class ST, class DT>
00036 inline void StretchTemplateLUT(int x1, int x2, int y1, int y2, int yr, int yw, ST src_pixels, DT dst_pixels, Uint32* lut)
00037 {
00038         int dx,dy,e,d,dx2;
00039 
00040         register Uint32 p;
00041         
00042         dx=(x2-x1);
00043         dy=(y2-y1);
00044 
00045         dy<<=1;
00046         e=dy-dx;
00047         dx2=dx<<1;
00048 
00049         for(d=0;d<=dx;d++)
00050         {
00051                 p = lut[*src_pixels];
00052                 *dst_pixels++ = p;
00053 
00054                 while(e>=0)
00055                 {
00056                         src_pixels ++;
00057                         e-=dx2;
00058                 }
00059                 
00060                 e+=dy;
00061         }
00062 }
00063 
00064 template<class ST, class DT>
00065 inline void StretchTemplate(int x1, int x2, int y1, int y2, int yr, int yw, ST src_pixels, DT dst_pixels)
00066 {
00067         int dx,dy,e,d,dx2;
00068         
00069         dx=(x2-x1);
00070         dy=(y2-y1);
00071 
00072         dy<<=1;
00073         e=dy-dx;
00074         dx2=dx<<1;
00075 
00076         for(d=0;d<=dx;d++)
00077         {
00078                 *dst_pixels++ = *src_pixels;
00079 
00080                 while(e>=0)
00081                 {
00082                         src_pixels ++;
00083                         e-=dx2;
00084                 }
00085                 
00086                 e+=dy;
00087         }
00088 }
00089 
00090 template<class ST, class DT>
00091 inline void RectStretchTemplate(SDL_Surface* src_surface, ST src, int xs1, int ys1, int xs2, int ys2, SDL_Surface* dst_surface, DT dst, int xd1, int yd1, int xd2, int yd2, Uint32* lutVOI)
00092 {
00093         int dx,dy,e,d,dx2;
00094         int sx,sy;
00095         dx=abs((int)(yd2-yd1));
00096         dy=abs((int)(ys2-ys1));
00097         sx=sign(yd2-yd1);
00098         sy=sign(ys2-ys1);
00099         e=(dy<<1)-dx;
00100         dx2=dx<<1;
00101         dy<<=1;
00102 
00103         Uint16 src_pitch = src_surface->pitch;
00104         Uint16 dst_pitch = dst_surface->pitch;
00105         
00106         int src_bpp = src_surface->format->BytesPerPixel;
00107         int dst_bpp = dst_surface->format->BytesPerPixel;
00108 
00109         register long src_pixels = ((long)src + ys1*src_pitch + xs1 * src_bpp);
00110         register long dst_pixels = ((long)dst + yd1*dst_pitch + xd1 * dst_bpp);
00111         register Uint32* lut = lutVOI;
00112 
00113         if(lut == NULL){                                        // Stretch without lookup table
00114                 for(d=0; (d<=dx) && (yd1<dst_surface->h); d++)
00115                 {
00116                         StretchTemplate(xd1,xd2,xs1,xs2,ys1,yd1, (ST)src_pixels, (DT)dst_pixels);
00117 
00118                         while(e>=0)
00119                         {
00120                                 src_pixels += src_pitch;
00121                                 ys1++;
00122                                 e-=dx2;
00123                         }
00124                         dst_pixels += dst_pitch;
00125                         yd1++;
00126                         e+=dy;
00127                 }
00128         }
00129         else {                                                                  // Stretch with lookup table
00130                 for(d=0; (d<=dx) && (yd1<dst_surface->h); d++)
00131                 {
00132                         StretchTemplateLUT(xd1,xd2,xs1,xs2,ys1,yd1, (ST)src_pixels, (DT)dst_pixels, lut);
00133 
00134                         while(e>=0)
00135                         {
00136                                 src_pixels += src_pitch;
00137                                 ys1++;
00138                                 e-=dx2;
00139                         }
00140                         dst_pixels += dst_pitch;
00141                         yd1++;
00142                         e+=dy;
00143                 }
00144         }
00145 
00146 }
00147 
00148 
00149 void SDL_StretchSurface(SDL_Surface* src_surface, int xs1, int ys1, int xs2, int ys2, SDL_Surface* dst_surface, int xd1, int yd1, int xd2, int yd2, Uint32* lutVOI)
00150 {
00151         int src_bpp = src_surface->format->BytesPerPixel;
00152         int dst_bpp = dst_surface->format->BytesPerPixel;
00153 
00154         if(dst_surface->format->BytesPerPixel == 1){
00155             SDL_SetColors(dst_surface, src_surface->format->palette->colors, 0, 256);
00156         }
00157 
00158         switch(dst_bpp){
00159 
00160                 case 1:
00161                         switch(src_bpp){
00162 
00163                                 case 1:
00164                                         RectStretchTemplate(src_surface, (Uint8*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint8*)dst_surface->pixels, xd1, yd1, xd2, yd2, (Uint32*)NULL);
00165                                         break;
00166 
00167                                 case 2:
00168                                         RectStretchTemplate(src_surface, (Uint16*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint8*)dst_surface->pixels, xd1, yd1, xd2, yd2, lutVOI);
00169                                         break;
00170                         }
00171                         break;
00172 
00173                 case 2:
00174                         switch(src_bpp){
00175 
00176                                 case 2:
00177                                         RectStretchTemplate(src_surface, (Uint16*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint16*)dst_surface->pixels, xd1, yd1, xd2, yd2, (Uint32*)NULL);
00178                                         break;
00179                         }
00180                         break;
00181 
00182                 case 4:
00183                         switch(src_bpp){
00184 
00185                                 case 1:
00186                                         RectStretchTemplate(src_surface, (Uint8*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint32*)dst_surface->pixels, xd1, yd1, xd2, yd2, lutVOI);
00187                                         break;
00188 
00189                                 case 2:
00190                                         RectStretchTemplate(src_surface, (Uint16*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint32*)dst_surface->pixels, xd1, yd1, xd2, yd2, lutVOI);
00191                                         break;
00192 
00193                                 case 4:
00194                                         RectStretchTemplate(src_surface, (Uint32*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint32*)dst_surface->pixels, xd1, yd1, xd2, yd2, (Uint32*)NULL);
00195                                         break;
00196 
00197                         }
00198                         break;
00199 
00200 
00201         }
00202 
00203 }
00204 
00205 
00206 void SDL_StretchSurface(SDL_Surface* src_surface, SDL_Rect* src_rect, SDL_Surface* dst_surface, SDL_Rect* dst_rect, Uint32* voiLUT){
00207         
00208         SDL_StretchSurface(
00209                                 src_surface,
00210                                 src_rect->x,
00211                                 src_rect->y,
00212                                 src_rect->w,
00213                                 src_rect->h,
00214                                 dst_surface,
00215                                 dst_rect->x,
00216                                 dst_rect->y,
00217                                 dst_rect->w,
00218                                 dst_rect->h,    
00219                                 voiLUT);
00220 }
00221 

Generated on Tue Jun 24 01:27:51 2008 for Advanced Strategic Command by  doxygen 1.4.2