SDL 2.0
SDL_stretch.c File Reference
#include "../SDL_internal.h"
#include "SDL_video.h"
#include "SDL_blit.h"
+ Include dependency graph for SDL_stretch.c:

Go to the source code of this file.

Macros

#define DEFINE_COPY_ROW(name, type)
 

Functions

static void copy_row3 (Uint8 *src, int src_w, Uint8 *dst, int dst_w)
 
int SDL_SoftStretch (SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect)
 Perform a fast, low quality, stretch blit between two surfaces of the same pixel format. More...
 

Macro Definition Documentation

◆ DEFINE_COPY_ROW

#define DEFINE_COPY_ROW (   name,
  type 
)
Value:
static void name(type *src, int src_w, type *dst, int dst_w) \
{ \
int i; \
int pos, inc; \
type pixel = 0; \
\
pos = 0x10000; \
inc = (src_w << 16) / dst_w; \
for ( i=dst_w; i>0; --i ) { \
while ( pos >= 0x10000L ) { \
pixel = *src++; \
pos -= 0x10000L; \
} \
*dst++ = pixel; \
pos += inc; \
} \
}
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
GLenum src
GLenum GLenum dst
GLuint const GLchar * name
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50

Definition at line 151 of file SDL_stretch.c.

Function Documentation

◆ copy_row3()

static void copy_row3 ( Uint8 src,
int  src_w,
Uint8 dst,
int  dst_w 
)
static

Definition at line 177 of file SDL_stretch.c.

178{
179 int i;
180 int pos, inc;
181 Uint8 pixel[3] = { 0, 0, 0 };
182
183 pos = 0x10000;
184 inc = (src_w << 16) / dst_w;
185 for (i = dst_w; i > 0; --i) {
186 while (pos >= 0x10000L) {
187 pixel[0] = *src++;
188 pixel[1] = *src++;
189 pixel[2] = *src++;
190 pos -= 0x10000L;
191 }
192 *dst++ = pixel[0];
193 *dst++ = pixel[1];
194 *dst++ = pixel[2];
195 pos += inc;
196 }
197}
uint8_t Uint8
Definition: SDL_stdinc.h:179

References i.

Referenced by SDL_SoftStretch().

◆ SDL_SoftStretch()

int SDL_SoftStretch ( SDL_Surface src,
const SDL_Rect srcrect,
SDL_Surface dst,
const SDL_Rect dstrect 
)

Perform a fast, low quality, stretch blit between two surfaces of the same pixel format.

Note
This function uses a static buffer, and is not thread-safe.

Definition at line 203 of file SDL_stretch.c.

205{
206 int src_locked;
207 int dst_locked;
208 int pos, inc;
209 int dst_maxrow;
210 int src_row, dst_row;
211 Uint8 *srcp = NULL;
212 Uint8 *dstp;
213 SDL_Rect full_src;
214 SDL_Rect full_dst;
215#ifdef USE_ASM_STRETCH
216 SDL_bool use_asm = SDL_TRUE;
217#ifdef __GNUC__
218 int u1, u2;
219#endif
220#endif /* USE_ASM_STRETCH */
221 const int bpp = dst->format->BytesPerPixel;
222
223 if (src->format->format != dst->format->format) {
224 return SDL_SetError("Only works with same format surfaces");
225 }
226
227 /* Verify the blit rectangles */
228 if (srcrect) {
229 if ((srcrect->x < 0) || (srcrect->y < 0) ||
230 ((srcrect->x + srcrect->w) > src->w) ||
231 ((srcrect->y + srcrect->h) > src->h)) {
232 return SDL_SetError("Invalid source blit rectangle");
233 }
234 } else {
235 full_src.x = 0;
236 full_src.y = 0;
237 full_src.w = src->w;
238 full_src.h = src->h;
239 srcrect = &full_src;
240 }
241 if (dstrect) {
242 if ((dstrect->x < 0) || (dstrect->y < 0) ||
243 ((dstrect->x + dstrect->w) > dst->w) ||
244 ((dstrect->y + dstrect->h) > dst->h)) {
245 return SDL_SetError("Invalid destination blit rectangle");
246 }
247 } else {
248 full_dst.x = 0;
249 full_dst.y = 0;
250 full_dst.w = dst->w;
251 full_dst.h = dst->h;
252 dstrect = &full_dst;
253 }
254
255 /* Lock the destination if it's in hardware */
256 dst_locked = 0;
257 if (SDL_MUSTLOCK(dst)) {
258 if (SDL_LockSurface(dst) < 0) {
259 return SDL_SetError("Unable to lock destination surface");
260 }
261 dst_locked = 1;
262 }
263 /* Lock the source if it's in hardware */
264 src_locked = 0;
265 if (SDL_MUSTLOCK(src)) {
266 if (SDL_LockSurface(src) < 0) {
267 if (dst_locked) {
269 }
270 return SDL_SetError("Unable to lock source surface");
271 }
272 src_locked = 1;
273 }
274
275 /* Set up the data... */
276 pos = 0x10000;
277 inc = (srcrect->h << 16) / dstrect->h;
278 src_row = srcrect->y;
279 dst_row = dstrect->y;
280
281#ifdef USE_ASM_STRETCH
282 /* Write the opcodes for this stretch */
283 if ((bpp == 3) || (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0)) {
284 use_asm = SDL_FALSE;
285 }
286#endif
287
288 /* Perform the stretch blit */
289 for (dst_maxrow = dst_row + dstrect->h; dst_row < dst_maxrow; ++dst_row) {
290 dstp = (Uint8 *) dst->pixels + (dst_row * dst->pitch)
291 + (dstrect->x * bpp);
292 while (pos >= 0x10000L) {
293 srcp = (Uint8 *) src->pixels + (src_row * src->pitch)
294 + (srcrect->x * bpp);
295 ++src_row;
296 pos -= 0x10000L;
297 }
298#ifdef USE_ASM_STRETCH
299 if (use_asm) {
300#ifdef __GNUC__
301 __asm__ __volatile__("call *%4":"=&D"(u1), "=&S"(u2)
302 :"0"(dstp), "1"(srcp), "r"(copy_row)
303 :"memory");
304#elif defined(_MSC_VER) || defined(__WATCOMC__)
305 /* *INDENT-OFF* */
306 {
307 void *code = copy_row;
308 __asm {
309 push edi
310 push esi
311 mov edi, dstp
312 mov esi, srcp
313 call dword ptr code
314 pop esi
315 pop edi
316 }
317 }
318 /* *INDENT-ON* */
319#else
320#error Need inline assembly for this compiler
321#endif
322 } else
323#endif
324 switch (bpp) {
325 case 1:
326 copy_row1(srcp, srcrect->w, dstp, dstrect->w);
327 break;
328 case 2:
329 copy_row2((Uint16 *) srcp, srcrect->w,
330 (Uint16 *) dstp, dstrect->w);
331 break;
332 case 3:
333 copy_row3(srcp, srcrect->w, dstp, dstrect->w);
334 break;
335 case 4:
336 copy_row4((Uint32 *) srcp, srcrect->w,
337 (Uint32 *) dstp, dstrect->w);
338 break;
339 }
340 pos += inc;
341 }
342
343 /* We need to unlock the surfaces if they're locked */
344 if (dst_locked) {
346 }
347 if (src_locked) {
349 }
350 return (0);
351}
#define SDL_SetError
#define SDL_UnlockSurface
#define SDL_LockSurface
GLfixed u1
GLfixed GLfixed u2
#define pop
Definition: SDL_qsort.c:192
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
uint32_t Uint32
Definition: SDL_stdinc.h:203
uint16_t Uint16
Definition: SDL_stdinc.h:191
static void copy_row3(Uint8 *src, int src_w, Uint8 *dst, int dst_w)
Definition: SDL_stretch.c:177
#define SDL_MUSTLOCK(S)
Definition: SDL_surface.h:62
#define NULL
Definition: begin_code.h:167
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
int y
Definition: SDL_rect.h:79
int x
Definition: SDL_rect.h:79

References copy_row3(), SDL_Rect::h, NULL, pop, SDL_FALSE, SDL_LockSurface, SDL_MUSTLOCK, SDL_SetError, SDL_TRUE, SDL_UnlockSurface, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.