Advanced Strategic Command
sdlstretch.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  SDLStretch.cpp - description
3  -------------------
4  begin : Thu Nov 11 1999
5  copyright : (C) 1999 by Alexander Pipelka
6  email : pipelka@teleweb.at
7  ***************************************************************************/
8 
9 /***************************************************************************
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Library General Public
13  License as published by the Free Software Foundation; either
14  version 2 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Library General Public License for more details.
20 
21  ***************************************************************************/
22 
23 
24 
25 #include "../global.h"
26 #include <stdio.h>
27 #include <cstring>
28 #include <math.h>
29 #include <stdlib.h>
30 #include "sdlstretch.h"
31 
32 #define sign(x) ((x)>0 ? 1:-1)
33 
34 
35 template<class ST, class DT>
36 inline void StretchTemplateLUT(int x1, int x2, int y1, int y2, int yr, int yw, ST src_pixels, DT dst_pixels, Uint32* lut)
37 {
38  int dx,dy,e,d,dx2;
39 
40  register Uint32 p;
41 
42  dx=(x2-x1);
43  dy=(y2-y1);
44 
45  dy<<=1;
46  e=dy-dx;
47  dx2=dx<<1;
48 
49  for(d=0;d<=dx;d++)
50  {
51  p = lut[*src_pixels];
52  *dst_pixels++ = p;
53 
54  while(e>=0)
55  {
56  src_pixels ++;
57  e-=dx2;
58  }
59 
60  e+=dy;
61  }
62 }
63 
64 template<class ST, class DT>
65 inline void StretchTemplate(int x1, int x2, int y1, int y2, int yr, int yw, ST src_pixels, DT dst_pixels)
66 {
67  int dx,dy,e,d,dx2;
68 
69  dx=(x2-x1);
70  dy=(y2-y1);
71 
72  dy<<=1;
73  e=dy-dx;
74  dx2=dx<<1;
75 
76  for(d=0;d<=dx;d++)
77  {
78  *dst_pixels++ = *src_pixels;
79 
80  while(e>=0)
81  {
82  src_pixels ++;
83  e-=dx2;
84  }
85 
86  e+=dy;
87  }
88 }
89 
90 template<class ST, class DT>
91 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)
92 {
93  int dx,dy,e,d,dx2;
94  dx=abs((int)(yd2-yd1));
95  dy=abs((int)(ys2-ys1));
96  e=(dy<<1)-dx;
97  dx2=dx<<1;
98  dy<<=1;
99 
100  Uint16 src_pitch = src_surface->pitch;
101  Uint16 dst_pitch = dst_surface->pitch;
102 
103  int src_bpp = src_surface->format->BytesPerPixel;
104  int dst_bpp = dst_surface->format->BytesPerPixel;
105 
106  register long src_pixels = ((long)src + ys1*src_pitch + xs1 * src_bpp);
107  register long dst_pixels = ((long)dst + yd1*dst_pitch + xd1 * dst_bpp);
108  register Uint32* lut = lutVOI;
109 
110  if(lut == NULL){ // Stretch without lookup table
111  for(d=0; (d<=dx) && (yd1<dst_surface->h); d++)
112  {
113  StretchTemplate(xd1,xd2,xs1,xs2,ys1,yd1, (ST)src_pixels, (DT)dst_pixels);
114 
115  while(e>=0)
116  {
117  src_pixels += src_pitch;
118  ys1++;
119  e-=dx2;
120  }
121  dst_pixels += dst_pitch;
122  yd1++;
123  e+=dy;
124  }
125  }
126  else { // Stretch with lookup table
127  for(d=0; (d<=dx) && (yd1<dst_surface->h); d++)
128  {
129  StretchTemplateLUT(xd1,xd2,xs1,xs2,ys1,yd1, (ST)src_pixels, (DT)dst_pixels, lut);
130 
131  while(e>=0)
132  {
133  src_pixels += src_pitch;
134  ys1++;
135  e-=dx2;
136  }
137  dst_pixels += dst_pitch;
138  yd1++;
139  e+=dy;
140  }
141  }
142 
143 }
144 
145 
146 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)
147 {
148  int src_bpp = src_surface->format->BytesPerPixel;
149  int dst_bpp = dst_surface->format->BytesPerPixel;
150 
151  if(dst_surface->format->BytesPerPixel == 1){
152  SDL_SetColors(dst_surface, src_surface->format->palette->colors, 0, 256);
153  }
154 
155  switch(dst_bpp){
156 
157  case 1:
158  switch(src_bpp){
159 
160  case 1:
161  RectStretchTemplate(src_surface, (Uint8*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint8*)dst_surface->pixels, xd1, yd1, xd2, yd2, (Uint32*)NULL);
162  break;
163 
164  case 2:
165  RectStretchTemplate(src_surface, (Uint16*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint8*)dst_surface->pixels, xd1, yd1, xd2, yd2, lutVOI);
166  break;
167  }
168  break;
169 
170  case 2:
171  switch(src_bpp){
172 
173  case 2:
174  RectStretchTemplate(src_surface, (Uint16*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint16*)dst_surface->pixels, xd1, yd1, xd2, yd2, (Uint32*)NULL);
175  break;
176  }
177  break;
178 
179  case 4:
180  switch(src_bpp){
181 
182  case 1:
183  RectStretchTemplate(src_surface, (Uint8*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint32*)dst_surface->pixels, xd1, yd1, xd2, yd2, lutVOI);
184  break;
185 
186  case 2:
187  RectStretchTemplate(src_surface, (Uint16*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint32*)dst_surface->pixels, xd1, yd1, xd2, yd2, lutVOI);
188  break;
189 
190  case 4:
191  RectStretchTemplate(src_surface, (Uint32*)src_surface->pixels, xs1, ys1, xs2, ys2, dst_surface, (Uint32*)dst_surface->pixels, xd1, yd1, xd2, yd2, (Uint32*)NULL);
192  break;
193 
194  }
195  break;
196 
197 
198  }
199 
200 }
201 
202 
203 void SDL_StretchSurface(SDL_Surface* src_surface, SDL_Rect* src_rect, SDL_Surface* dst_surface, SDL_Rect* dst_rect, Uint32* voiLUT){
204 
206  src_surface,
207  src_rect->x,
208  src_rect->y,
209  src_rect->w,
210  src_rect->h,
211  dst_surface,
212  dst_rect->x,
213  dst_rect->y,
214  dst_rect->w,
215  dst_rect->h,
216  voiLUT);
217 }
218 
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)
Definition: sdlstretch.cpp:146
void StretchTemplate(int x1, int x2, int y1, int y2, int yr, int yw, ST src_pixels, DT dst_pixels)
Definition: sdlstretch.cpp:65
void StretchTemplateLUT(int x1, int x2, int y1, int y2, int yr, int yw, ST src_pixels, DT dst_pixels, Uint32 *lut)
Definition: sdlstretch.cpp:36
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)
Definition: sdlstretch.cpp:91