From d5c9290cd3c6dfb97b106dc46ab2ec7f93b348e2 Mon Sep 17 00:00:00 2001 From: stpohle Date: Thu, 24 Jul 2003 12:41:27 +0000 Subject: [PATCH] scaling routine speeded up a little bit by using less switch case and using inline --- src/gfx.h | 12 +++- src/gfxpixelimage.c | 170 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 157 insertions(+), 25 deletions(-) diff --git a/src/gfx.h b/src/gfx.h index 0a85281..19c602e 100644 --- a/src/gfx.h +++ b/src/gfx.h @@ -1,4 +1,4 @@ -/* $Id: gfx.h,v 1.15 2003/07/22 23:15:31 stpohle Exp $ */ +/* $Id: gfx.h,v 1.16 2003/07/24 12:41:27 stpohle Exp $ */ #ifndef _GFX_H_ #define _GFX_H_ @@ -82,8 +82,14 @@ extern void gfx_free_players (); // gfxpixelimage.c extern void getRGBpixel (SDL_Surface *surface, int x, int y, int *R, int *G, int *B); -extern Uint32 getpixel(SDL_Surface *surface, int x, int y); -extern void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel); +inline Uint32 getpixel(SDL_Surface *surface, int x, int y); +inline void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel); +inline Uint32 getpixel16(SDL_Surface *surface, int x, int y); +inline void putpixel16(SDL_Surface *surface, int x, int y, Uint32 pixel); +inline Uint32 getpixel24(SDL_Surface *surface, int x, int y); +inline void putpixel24(SDL_Surface *surface, int x, int y, Uint32 pixel); +inline Uint32 getpixel32(SDL_Surface *surface, int x, int y); +inline void putpixel32(SDL_Surface *surface, int x, int y, Uint32 pixel); extern void scale (short int *dpattern, short int x, short int y); extern SDL_Surface *scale_image (SDL_Surface * orginal, int newx, int newy); extern void shade_pixel(SDL_Surface *s, int x, int y, int c); diff --git a/src/gfxpixelimage.c b/src/gfxpixelimage.c index 77a3271..9e909b2 100644 --- a/src/gfxpixelimage.c +++ b/src/gfxpixelimage.c @@ -1,4 +1,4 @@ -/* $Id: gfxpixelimage.c,v 1.1 2003/07/22 18:29:08 stpohle Exp $ */ +/* $Id: gfxpixelimage.c,v 1.2 2003/07/24 12:41:27 stpohle Exp $ */ /* gfx pixel manipulation and image manipulation */ #include "bomberclone.h" @@ -27,7 +27,7 @@ getRGBpixel (SDL_Surface * surface, int x, int y, int *R, int *G, int *B) *B = b; }; - +/* getpixel for every BPP version */ Uint32 getpixel (SDL_Surface * surface, int x, int y) { @@ -54,6 +54,38 @@ getpixel (SDL_Surface * surface, int x, int y) }; +inline Uint32 +getpixel32 (SDL_Surface * surface, int x, int y) +{ + /* Here p is the address to the pixel we want to retrieve */ + Uint8 *p = (Uint8 *) surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel; + return *(Uint32 *) p; +}; + + +inline Uint32 +getpixel24 (SDL_Surface * surface, int x, int y) +{ + /* Here p is the address to the pixel we want to retrieve */ + Uint8 *p = (Uint8 *) surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel;; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + return p[0] << 16 | p[1] << 8 | p[2]; +#else + return p[0] | p[1] << 8 | p[2] << 16; +#endif +}; + + +inline Uint32 +getpixel16 (SDL_Surface * surface, int x, int y) +{ + /* Here p is the address to the pixel we want to retrieve */ + Uint8 *p = (Uint8 *) surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel; + return *(Uint16 *) p; +}; + + +/* putpixel seperated for every BPP version */ void putpixel (SDL_Surface * surface, int x, int y, Uint32 pixel) { @@ -87,6 +119,41 @@ putpixel (SDL_Surface * surface, int x, int y, Uint32 pixel) }; +inline void +putpixel32 (SDL_Surface * surface, int x, int y, Uint32 pixel) +{ + /* Here p is the address to the pixel we want to set */ + Uint8 *p = (Uint8 *) surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel; + *(Uint32 *) p = pixel; +}; + + +inline void +putpixel24 (SDL_Surface * surface, int x, int y, Uint32 pixel) +{ + /* Here p is the address to the pixel we want to set */ + Uint8 *p = (Uint8 *) surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + p[0] = (pixel >> 16) & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = pixel & 0xff; +#else + p[0] = pixel & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = (pixel >> 16) & 0xff; +#endif +}; + + +inline void +putpixel16 (SDL_Surface * surface, int x, int y, Uint32 pixel) +{ + /* Here p is the address to the pixel we want to set */ + Uint8 *p = (Uint8 *) surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel; + *(Uint16 *) p = pixel; +}; + + void scale (short int *dpattern, short int x, short int y) { @@ -132,7 +199,7 @@ scale (short int *dpattern, short int x, short int y) SDL_Surface * -scale_image (SDL_Surface * orginal, int newx, int newy) +scale_image (SDL_Surface *orginal, int newx, int newy) { Uint32 rmask, gmask, @@ -141,6 +208,7 @@ scale_image (SDL_Surface * orginal, int newx, int newy) SDL_Surface *surface; int y, x; + int bpp = orginal->format->BytesPerPixel; short int xpattern[SCALE_MAXRES]; short int ypattern[SCALE_MAXRES]; @@ -155,7 +223,6 @@ scale_image (SDL_Surface * orginal, int newx, int newy) gmask = 0x0000ff00; bmask = 0x000000ff; amask = 0xff000000; - #endif /* */ surface = SDL_CreateRGBSurface (SDL_SWSURFACE, newx, newy, 32, rmask, gmask, bmask, amask); @@ -180,11 +247,31 @@ scale_image (SDL_Surface * orginal, int newx, int newy) } /* do the scaling work */ - scale (xpattern, orginal->w - 1, newx); - scale (ypattern, orginal->h - 1, newy); - for (x = newx - 1; x >= 0; x--) - for (y = newy - 1; y >= 0; y--) - putpixel (surface, x, y, getpixel (orginal, xpattern[x], ypattern[y])); + scale (xpattern, orginal->w - 1, newx); + scale (ypattern, orginal->h - 1, newy); + + switch (bpp) { + case (2): + for (x = newx - 1; x >= 0; x--) + for (y = newy - 1; y >= 0; y--) + putpixel16 (surface, x, y, getpixel16 (orginal, xpattern[x], ypattern[y])); + break; + case (3): + for (x = newx - 1; x >= 0; x--) + for (y = newy - 1; y >= 0; y--) + putpixel24 (surface, x, y, getpixel24 (orginal, xpattern[x], ypattern[y])); + break; + case (4): + for (x = newx - 1; x >= 0; x--) + for (y = newy - 1; y >= 0; y--) + putpixel32 (surface, x, y, getpixel32 (orginal, xpattern[x], ypattern[y])); + break; + default: + printf ("scale_image, wrong bpp value (%d).\n", bpp); + exit (1); + break; + } + if (SDL_MUSTLOCK (orginal)) { SDL_UnlockSurface (orginal); } @@ -204,7 +291,8 @@ makegray_image (SDL_Surface * org) Uint32 pixel, transpixel = 0; SDL_Surface *dest; - int y, + int bpp = org->format->BytesPerPixel, + y, x; Uint8 r, g, @@ -242,18 +330,56 @@ makegray_image (SDL_Surface * org) } return NULL; } - for (x = 0; x < org->w; x++) - for (y = 0; y < org->h; y++) { - pixel = getpixel (org, x, y); - if (x == 0 && y == 0) - transpixel = pixel; - if (pixel != transpixel) { - SDL_GetRGB (pixel, org->format, &r, &g, &b); - gray = (r / 3 + g / 3 + b / 3); - pixel = SDL_MapRGB (dest->format, gray, gray, gray); - } - putpixel (dest, x, y, pixel); - } + + switch (bpp) { + case (2): + for (x = 0; x < org->w; x++) + for (y = 0; y < org->h; y++) { + pixel = getpixel16 (org, x, y); + if (x == 0 && y == 0) + transpixel = pixel; + if (pixel != transpixel) { + SDL_GetRGB (pixel, org->format, &r, &g, &b); + gray = (r / 3 + g / 3 + b / 3); + pixel = SDL_MapRGB (dest->format, gray, gray, gray); + } + putpixel16 (dest, x, y, pixel); + } + break; + case (3): + for (x = 0; x < org->w; x++) + for (y = 0; y < org->h; y++) { + pixel = getpixel24 (org, x, y); + if (x == 0 && y == 0) + transpixel = pixel; + if (pixel != transpixel) { + SDL_GetRGB (pixel, org->format, &r, &g, &b); + gray = (r / 3 + g / 3 + b / 3); + pixel = SDL_MapRGB (dest->format, gray, gray, gray); + } + putpixel24 (dest, x, y, pixel); + } + break; + case (4): + for (x = 0; x < org->w; x++) + for (y = 0; y < org->h; y++) { + pixel = getpixel32 (org, x, y); + if (x == 0 && y == 0) + transpixel = pixel; + if (pixel != transpixel) { + SDL_GetRGB (pixel, org->format, &r, &g, &b); + gray = (r / 3 + g / 3 + b / 3); + pixel = SDL_MapRGB (dest->format, gray, gray, gray); + } + putpixel32 (dest, x, y, pixel); + } + break; + default: + printf ("gray_image, wrong bpp value (%d).\n", bpp); + exit (1); + break; + } + if (SDL_MUSTLOCK (org)) { SDL_UnlockSurface (org); }