From 1b2bf5eab12be2bc84b7213e9f6a542cce0a125d Mon Sep 17 00:00:00 2001 From: "giwoong.kim" Date: Sun, 18 Mar 2012 02:50:11 +0900 Subject: [PATCH] [Title] added scaling and rotation feature [Type] [Module] Emulator [Priority] [Jira#] [Redmine#] [Problem] [Cause] [Solution] [TestCase] --- tizen/src/Makefile.tizen | 7 +- tizen/src/emul_state.c | 36 +- tizen/src/emul_state.h | 22 +- tizen/src/emulator.c | 10 +- tizen/src/emulator.h | 1 + tizen/src/maru_sdl.c | 92 +- tizen/src/maru_sdl.h | 1 + tizen/src/sdl_rotate.c | 1610 +++++++++++++++++++++++++++++++++++ tizen/src/sdl_rotate.h | 99 +++ tizen/src/sdl_zoom.c | 96 +++ tizen/src/sdl_zoom.h | 25 + tizen/src/sdl_zoom_template.h | 225 +++++ tizen/src/skin/maruskin_client.c | 24 - tizen/src/skin/maruskin_operation.c | 68 +- tizen/src/skin/maruskin_operation.h | 6 +- tizen/src/skin/maruskin_server.c | 34 +- 16 files changed, 2259 insertions(+), 97 deletions(-) mode change 100644 => 100755 tizen/src/Makefile.tizen create mode 100644 tizen/src/sdl_rotate.c create mode 100644 tizen/src/sdl_rotate.h create mode 100644 tizen/src/sdl_zoom.c create mode 100644 tizen/src/sdl_zoom.h create mode 100644 tizen/src/sdl_zoom_template.h mode change 100755 => 100644 tizen/src/skin/maruskin_operation.h mode change 100755 => 100644 tizen/src/skin/maruskin_server.c diff --git a/tizen/src/Makefile.tizen b/tizen/src/Makefile.tizen old mode 100644 new mode 100755 index c1ff963..46767a4 --- a/tizen/src/Makefile.tizen +++ b/tizen/src/Makefile.tizen @@ -51,10 +51,13 @@ opengl_exec.o : opengl_exec.c server_stub.c opengl_func.h gl_beginend.h opengl_p endif #($(TARGET_ARCH), i386) ########################################################### -endif #CONFIG_WIN32 +endif #CONFIG_WIN32 # maru loader -obj-y += emulator.o emul_state.o process.o option.o maru_signal.o maru_sdl.o +obj-y += emulator.o emul_state.o process.o option.o maru_signal.o + +# display +obj-y += maru_sdl.o sdl_rotate.o sdl_zoom.o # sdb obj-y += sdb.o diff --git a/tizen/src/emul_state.c b/tizen/src/emul_state.c index c6a6062..e2c722e 100644 --- a/tizen/src/emul_state.c +++ b/tizen/src/emul_state.c @@ -37,21 +37,47 @@ MULTI_DEBUG_CHANNEL(qemu, emul_state); static emulator_config_info _emul_info; +static emulator_config_state _emul_state; +/* lcd screen size */ void set_emul_lcd_size(int width, int height) { - _emul_info.resolution_w = width; - _emul_info.resolution_h = height; + _emul_info.lcd_size_w = width; + _emul_info.lcd_size_h = height; - INFO("emulator graphic resolution %dx%d\n", _emul_info.resolution_w, _emul_info.resolution_h); + INFO("emulator graphic resolution = %dx%d\n", _emul_info.lcd_size_w, _emul_info.lcd_size_h); } int get_emul_lcd_width(void) { - return _emul_info.resolution_w; + return _emul_info.lcd_size_w; } int get_emul_lcd_height(void) { - return _emul_info.resolution_h; + return _emul_info.lcd_size_h; +} + +/* emulator window scale */ +void set_emul_win_scale(double scale_factor) +{ + _emul_state.scale_factor = scale_factor; + INFO("emulator window scale_factor = %lf\n", _emul_state.scale_factor); +} + +double get_emul_win_scale(void) +{ + return _emul_state.scale_factor; +} + +/* emulator rotation */ +void set_emul_rotation(short rotation_type) +{ + _emul_state.rotation_type = rotation_type; + INFO("emulator rotation type = %d\n", _emul_state.rotation_type); +} + +short get_emul_rotation(void) +{ + return _emul_state.rotation_type; } diff --git a/tizen/src/emul_state.h b/tizen/src/emul_state.h index 6db3e07..5d5e5b9 100644 --- a/tizen/src/emul_state.h +++ b/tizen/src/emul_state.h @@ -33,23 +33,37 @@ #ifndef __EMUL_STATE_H__ #define __EMUL_STATE_H__ +enum { + ROTATION_PORTRAIT = 0, + ROTATION_LANDSCAPE = 1, + ROTATION_REVERSE_PORTRAIT = 2, + ROTATION_REVERSE_LANDSCAPE = 3, +}; + typedef struct emulator_config_info { - int resolution_w; - int resolution_h; + int lcd_size_w; + int lcd_size_h; + //TODO: } emulator_config_info; -struct emulator_config_state { +typedef struct emulator_config_state { + double scale_factor; + short rotation_type; //TODO: -}; +} emulator_config_state; /* setter */ void set_emul_lcd_size(int width, int height); +void set_emul_win_scale(double scale); +void set_emul_rotation(short rotation_type); /* getter */ int get_emul_lcd_width(void); int get_emul_lcd_height(void); +double get_emul_win_scale(void); +short get_emul_rotation(void); #endif /* __EMUL_STATE_H__ */ diff --git a/tizen/src/emulator.c b/tizen/src/emulator.c index 01f2aa8..5517390 100644 --- a/tizen/src/emulator.c +++ b/tizen/src/emulator.c @@ -42,6 +42,8 @@ #include "debug_ch.h" #include "process.h" #include "option.h" +#include "emul_state.h" + #ifdef _WIN32 #include #endif @@ -85,6 +87,12 @@ void exit_emulator(void) static void construct_main_window(int skin_argc, char* skin_argv[]) { INFO("construct main window\n"); + + //TODO: init + set_emul_win_scale(0.5); + set_emul_rotation(0); + + start_skin_server(0, 0); #if 1 if ( 0 > start_skin_client(skin_argc, skin_argv) ) { @@ -217,7 +225,7 @@ void extract_info(int qemu_argc, char** qemu_argv) static int skin_argc = 0; static char** skin_argv = NULL; -void prepare_maru(void) +void prepare_maru(void) { INFO("Prepare maru specified feature\n"); diff --git a/tizen/src/emulator.h b/tizen/src/emulator.h index 3347ac5..e883443 100644 --- a/tizen/src/emulator.h +++ b/tizen/src/emulator.h @@ -37,6 +37,7 @@ #ifndef __EMULATOR_H__ #define __EMULATOR_H__ + int get_emulator_condition(void); void set_emulator_condition(int state); void exit_emulator(void); diff --git a/tizen/src/maru_sdl.c b/tizen/src/maru_sdl.c index b396eaf..55cb58c 100644 --- a/tizen/src/maru_sdl.c +++ b/tizen/src/maru_sdl.c @@ -31,6 +31,7 @@ #include #include "maru_sdl.h" #include "emul_state.h" +#include "sdl_rotate.h" #include "debug_ch.h" MULTI_DEBUG_CHANNEL(tizen, maru_sdl); @@ -40,26 +41,34 @@ MULTI_DEBUG_CHANNEL(tizen, maru_sdl); SDL_Surface *surface_screen; SDL_Surface *surface_qemu; +static double scale_factor = 1.0; +static double screen_degree = 0.0; + #define SDL_THREAD -#ifdef SDL_THREAD static pthread_mutex_t sdl_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifdef SDL_THREAD static pthread_cond_t sdl_cond = PTHREAD_COND_INITIALIZER; static int sdl_thread_initialized = 0; #endif +#define SDL_FLAGS (SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_NOFRAME) +#define SDL_BPP 32 + static void qemu_update(void) { -#ifndef SDL_THREAD - pthread_mutex_lock(&sdl_mutex); -#endif - - SDL_BlitSurface(surface_qemu, NULL, surface_screen, NULL); + SDL_Surface *processing_screen = NULL; + + if (scale_factor != 1.0 || screen_degree != 0.0) { + //image processing + processing_screen = rotozoomSurface(surface_qemu, screen_degree, scale_factor, 1); + SDL_BlitSurface(processing_screen, NULL, surface_screen, NULL); + } else { + SDL_BlitSurface(surface_qemu, NULL, surface_screen, NULL); + } SDL_UpdateRect(surface_screen, 0, 0, 0, 0); -#ifndef SDL_THREAD - pthread_mutex_unlock(&sdl_mutex); -#endif + SDL_FreeSurface(processing_screen); } @@ -67,13 +76,13 @@ static void qemu_update(void) static void* run_qemu_update(void* arg) { while(1) { - pthread_mutex_lock(&sdl_mutex); + pthread_mutex_lock(&sdl_mutex); pthread_cond_wait(&sdl_cond, &sdl_mutex); qemu_update(); - pthread_mutex_unlock(&sdl_mutex); + pthread_mutex_unlock(&sdl_mutex); } return NULL; @@ -117,7 +126,7 @@ static void qemu_ds_resize(DisplayState *ds) pthread_mutex_unlock(&sdl_mutex); #endif - if (!surface_qemu) { + if (surface_qemu == NULL) { ERR( "Unable to set the RGBSurface: %s", SDL_GetError() ); return; } @@ -132,9 +141,45 @@ static void qemu_ds_refresh(DisplayState *ds) while (SDL_PollEvent(ev)) { switch (ev->type) { - case SDL_KEYDOWN: - case SDL_KEYUP: + case SDL_VIDEORESIZE: + { + int w, h, temp; + + //get current setting information and calculate screen size + scale_factor = get_emul_win_scale(); + w = get_emul_lcd_width() * scale_factor; + h = get_emul_lcd_height() * scale_factor; + + short rotaton_type = get_emul_rotation(); + if (rotaton_type == ROTATION_PORTRAIT) { + screen_degree = 0.0; + } else if (rotaton_type == ROTATION_LANDSCAPE) { + screen_degree = 90.0; + temp = w; + w = h; + h = temp; + } else if (rotaton_type == ROTATION_REVERSE_PORTRAIT) { + screen_degree = 180.0; + } else if (rotaton_type == ROTATION_REVERSE_LANDSCAPE) { + screen_degree = 270.0; + temp = w; + w = h; + h = temp; + } + + pthread_mutex_lock(&sdl_mutex); + + SDL_Quit(); //The returned surface is freed by SDL_Quit and must not be freed by the caller + surface_screen = SDL_SetVideoMode(w, h, SDL_BPP, SDL_FLAGS); + if (surface_screen == NULL) { + ERR("Could not open SDL display (%dx%dx%d): %s\n", w, h, SDL_BPP, SDL_GetError()); + } + + pthread_mutex_unlock(&sdl_mutex); + break; + } + default: break; } @@ -156,7 +201,7 @@ void maruskin_display_init(DisplayState *ds) register_displaychangelistener(ds, dcl); #ifdef SDL_THREAD - if (sdl_thread_initialized == 0 ) { + if (sdl_thread_initialized == 0) { sdl_thread_initialized = 1; pthread_t thread_id; INFO( "sdl update thread create\n"); @@ -184,8 +229,10 @@ void maruskin_sdl_init(int swt_handle, int lcd_size_width, int lcd_size_height) } INFO( "qemu_sdl_initialize\n"); - surface_screen = SDL_SetVideoMode(lcd_size_width, lcd_size_height, - 0, SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_NOFRAME); + surface_screen = SDL_SetVideoMode(lcd_size_width, lcd_size_height, SDL_BPP, SDL_FLAGS); + if (surface_screen == NULL) { + ERR("Could not open SDL display (%dx%dx%d): %s\n", lcd_size_width, lcd_size_height, SDL_BPP, SDL_GetError()); + } set_emul_lcd_size(lcd_size_width, lcd_size_height); #ifndef _WIN32 @@ -193,3 +240,14 @@ void maruskin_sdl_init(int swt_handle, int lcd_size_width, int lcd_size_height) SDL_GetWMInfo(&info); #endif } + +void maruskin_sdl_resize() +{ + SDL_Event ev; + + /* this fails if SDL is not initialized */ + memset(&ev, 0, sizeof(ev)); + ev.resize.type = SDL_VIDEORESIZE; + + SDL_PushEvent(&ev); +} diff --git a/tizen/src/maru_sdl.h b/tizen/src/maru_sdl.h index 3ea67cd..b0bbaae 100644 --- a/tizen/src/maru_sdl.h +++ b/tizen/src/maru_sdl.h @@ -45,5 +45,6 @@ void maruskin_display_init(DisplayState *ds); void maruskin_sdl_init(int swt_handle, int lcd_size_width, int lcd_size_height); +void maruskin_sdl_resize(void); #endif /* MARU_SDL_H_ */ diff --git a/tizen/src/sdl_rotate.c b/tizen/src/sdl_rotate.c new file mode 100644 index 0000000..e268c97 --- /dev/null +++ b/tizen/src/sdl_rotate.c @@ -0,0 +1,1610 @@ +/* + * Derived from: SDL_rotozoom, LGPL (c) A. Schiffler from the SDL_gfx library. + * Modifications by Hyunjun Son(hj79.son@samsung.com) + * + * This work is licensed under the terms of the GNU GPL version 2. + * See the COPYING file in the top-level directory. + * + */ + +#ifdef WIN32 +#include +#endif + +#include +#include + +#include "sdl_rotate.h" + +#if 1 + +/* ---- Internally used structures */ + +/*! +\brief A 32 bit RGBA pixel. +*/ +typedef struct tColorRGBA { + Uint8 r; + Uint8 g; + Uint8 b; + Uint8 a; +} tColorRGBA; + +/*! +\brief A 8bit Y/palette pixel. +*/ +typedef struct tColorY { + Uint8 y; +} tColorY; + +/*! +\brief Returns maximum of two numbers a and b. +*/ +#define MAX(a,b) (((a) > (b)) ? (a) : (b)) + +/*! +\brief Number of guard rows added to destination surfaces. + +This is a simple but effective workaround for observed issues. +These rows allocate extra memory and are then hidden from the surface. +Rows are added to the end of destination surfaces when they are allocated. +This catches any potential overflows which seem to happen with +just the right src image dimensions and scale/rotation and can lead +to a situation where the program can segfault. +*/ +#define GUARD_ROWS (0) + +/*! +\brief Lower limit of absolute zoom factor or rotation degrees. +*/ +#define VALUE_LIMIT 0.001 + +Uint32 _colorkey(SDL_Surface *src); +int _shrinkSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int factorx, int factory); +int _shrinkSurfaceY(SDL_Surface * src, SDL_Surface * dst, int factorx, int factory); +int _zoomSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int flipx, int flipy, int smooth); +int _zoomSurfaceY(SDL_Surface * src, SDL_Surface * dst, int flipx, int flipy); +void _transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth); +void transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy); +void _rotozoomSurfaceSizeTrig(int width, int height, double angle, double zoomx, double zoomy, + int *dstwidth, int *dstheight, + double *canglezoom, double *sanglezoom); + +/*! +\brief Returns colorkey info for a surface +*/ +Uint32 _colorkey(SDL_Surface *src) +{ + Uint32 key = 0; +#if (SDL_MINOR_VERSION == 3) + SDL_GetColorKey(src, &key); +#else + if (src) + { + key = src->format->colorkey; + } +#endif + return key; +} + + +/*! +\brief Internal 32 bit integer-factor averaging Shrinker. + +Shrinks 32 bit RGBA/ABGR 'src' surface to 'dst' surface. +Averages color and alpha values values of src pixels to calculate dst pixels. +Assumes src and dst surfaces are of 32 bit depth. +Assumes dst surface was allocated with the correct dimensions. + +\param src The surface to shrink (input). +\param dst The shrunken surface (output). +\param factorx The horizontal shrinking ratio. +\param factory The vertical shrinking ratio. + +\return 0 for success or -1 for error. +*/ +int _shrinkSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int factorx, int factory) +{ + int x, y, dx, dy, sgap, dgap, ra, ga, ba, aa; + int n_average; + tColorRGBA *sp, *osp, *oosp; + tColorRGBA *dp; + + /* + * Averaging integer shrink + */ + + /* Precalculate division factor */ + n_average = factorx*factory; + + /* + * Scan destination + */ + sp = (tColorRGBA *) src->pixels; + sgap = src->pitch - src->w * 4; + + dp = (tColorRGBA *) dst->pixels; + dgap = dst->pitch - dst->w * 4; + + for (y = 0; y < dst->h; y++) { + + osp=sp; + for (x = 0; x < dst->w; x++) { + + /* Trace out source box and accumulate */ + oosp=sp; + ra=ga=ba=aa=0; + for (dy=0; dy < factory; dy++) { + for (dx=0; dx < factorx; dx++) { + ra += sp->r; + ga += sp->g; + ba += sp->b; + aa += sp->a; + + sp++; + } + /* src dx loop */ + sp = (tColorRGBA *)((Uint8*)sp + (src->pitch - 4*factorx)); // next y + } + /* src dy loop */ + + /* next box-x */ + sp = (tColorRGBA *)((Uint8*)oosp + 4*factorx); + + /* Store result in destination */ + dp->r = ra/n_average; + dp->g = ga/n_average; + dp->b = ba/n_average; + dp->a = aa/n_average; + + /* + * Advance destination pointer + */ + dp++; + } + /* dst x loop */ + + /* next box-y */ + sp = (tColorRGBA *)((Uint8*)osp + src->pitch*factory); + + /* + * Advance destination pointers + */ + dp = (tColorRGBA *) ((Uint8 *) dp + dgap); + } + /* dst y loop */ + + return (0); +} + +/*! +\brief Internal 8 bit integer-factor averaging shrinker. + +Shrinks 8bit Y 'src' surface to 'dst' surface. +Averages color (brightness) values values of src pixels to calculate dst pixels. +Assumes src and dst surfaces are of 8 bit depth. +Assumes dst surface was allocated with the correct dimensions. + +\param src The surface to shrink (input). +\param dst The shrunken surface (output). +\param factorx The horizontal shrinking ratio. +\param factory The vertical shrinking ratio. + +\return 0 for success or -1 for error. +*/ +int _shrinkSurfaceY(SDL_Surface * src, SDL_Surface * dst, int factorx, int factory) +{ + int x, y, dx, dy, sgap, dgap, a; + int n_average; + Uint8 *sp, *osp, *oosp; + Uint8 *dp; + + /* + * Averaging integer shrink + */ + + /* Precalculate division factor */ + n_average = factorx*factory; + + /* + * Scan destination + */ + sp = (Uint8 *) src->pixels; + sgap = src->pitch - src->w; + + dp = (Uint8 *) dst->pixels; + dgap = dst->pitch - dst->w; + + for (y = 0; y < dst->h; y++) { + + osp=sp; + for (x = 0; x < dst->w; x++) { + + /* Trace out source box and accumulate */ + oosp=sp; + a=0; + for (dy=0; dy < factory; dy++) { + for (dx=0; dx < factorx; dx++) { + a += (*sp); + /* next x */ + sp++; + } + /* end src dx loop */ + /* next y */ + sp = (Uint8 *)((Uint8*)sp + (src->pitch - factorx)); + } + /* end src dy loop */ + + /* next box-x */ + sp = (Uint8 *)((Uint8*)oosp + factorx); + + /* Store result in destination */ + *dp = a/n_average; + + /* + * Advance destination pointer + */ + dp++; + } + /* end dst x loop */ + + /* next box-y */ + sp = (Uint8 *)((Uint8*)osp + src->pitch*factory); + + /* + * Advance destination pointers + */ + dp = (Uint8 *)((Uint8 *)dp + dgap); + } + /* end dst y loop */ + + return (0); +} + +/*! +\brief Internal 32 bit Zoomer with optional anti-aliasing by bilinear interpolation. + +Zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface. +Assumes src and dst surfaces are of 32 bit depth. +Assumes dst surface was allocated with the correct dimensions. + +\param src The surface to zoom (input). +\param dst The zoomed surface (output). +\param flipx Flag indicating if the image should be horizontally flipped. +\param flipy Flag indicating if the image should be vertically flipped. +\param smooth Antialiasing flag; set to SMOOTHING_ON to enable. + +\return 0 for success or -1 for error. +*/ +int _zoomSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int flipx, int flipy, int smooth) +{ + int x, y, sx, sy, *sax, *say, *csax, *csay, csx, csy, ex, ey, t1, t2, sstep, lx, ly; + tColorRGBA *c00, *c01, *c10, *c11, *cswap; + tColorRGBA *sp, *csp, *dp; + int dgap; + + /* + * Variable setup + */ + if (smooth) { + /* + * For interpolation: assume source dimension is one pixel + */ + /* + * smaller to avoid overflow on right and bottom edge. + */ + sx = (int) (65536.0 * (float) (src->w - 1) / (float) dst->w); + sy = (int) (65536.0 * (float) (src->h - 1) / (float) dst->h); + } else { + sx = (int) (65536.0 * (float) src->w / (float) dst->w); + sy = (int) (65536.0 * (float) src->h / (float) dst->h); + } + + /* + * Allocate memory for row increments + */ + if ((sax = (int *) malloc((dst->w + 1) * sizeof(Uint32))) == NULL) { + return (-1); + } + if ((say = (int *) malloc((dst->h + 1) * sizeof(Uint32))) == NULL) { + free(sax); + return (-1); + } + + /* + * Precalculate row increments + */ + sp = csp = (tColorRGBA *) src->pixels; + dp = (tColorRGBA *) dst->pixels; + + if (flipx) csp += (src->w-1); + if (flipy) csp += (src->pitch*(src->h-1)); + + csx = 0; + csax = sax; + for (x = 0; x <= dst->w; x++) { + *csax = csx; + csax++; + csx &= 0xffff; + csx += sx; + } + csy = 0; + csay = say; + for (y = 0; y <= dst->h; y++) { + *csay = csy; + csay++; + csy &= 0xffff; + csy += sy; + } + + dgap = dst->pitch - dst->w * 4; + + /* + * Switch between interpolating and non-interpolating code + */ + if (smooth) { + + /* + * Interpolating Zoom + */ + + /* + * Scan destination + */ + ly = 0; + csay = say; + for (y = 0; y < dst->h; y++) { + /* + * Setup color source pointers + */ + c00 = csp; + c01 = csp; + c01++; + c10 = (tColorRGBA *) ((Uint8 *) csp + src->pitch); + c11 = c10; + c11++; + csax = sax; + if (flipx) { + cswap = c00; c00=c01; c01=cswap; + cswap = c10; c10=c11; c11=cswap; + } + if (flipy) { + cswap = c00; c00=c10; c10=cswap; + cswap = c01; c01=c11; c11=cswap; + } + lx = 0; + for (x = 0; x < dst->w; x++) { + /* + * Interpolate colors + */ + ex = (*csax & 0xffff); + ey = (*csay & 0xffff); + t1 = ((((c01->r - c00->r) * ex) >> 16) + c00->r) & 0xff; + t2 = ((((c11->r - c10->r) * ex) >> 16) + c10->r) & 0xff; + dp->r = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01->g - c00->g) * ex) >> 16) + c00->g) & 0xff; + t2 = ((((c11->g - c10->g) * ex) >> 16) + c10->g) & 0xff; + dp->g = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01->b - c00->b) * ex) >> 16) + c00->b) & 0xff; + t2 = ((((c11->b - c10->b) * ex) >> 16) + c10->b) & 0xff; + dp->b = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01->a - c00->a) * ex) >> 16) + c00->a) & 0xff; + t2 = ((((c11->a - c10->a) * ex) >> 16) + c10->a) & 0xff; + dp->a = (((t2 - t1) * ey) >> 16) + t1; + + /* + * Advance source pointers + */ + csax++; + sstep = (*csax >> 16); + lx += sstep; + if (lx >= src->w) sstep = 0; + if (flipx) sstep = -sstep; + c00 += sstep; + c01 += sstep; + c10 += sstep; + c11 += sstep; + /* + * Advance destination pointer + */ + dp++; + } + /* + * Advance source pointer + */ + csay++; + sstep = (*csay >> 16); + ly += sstep; + if (ly >= src->h) sstep = 0; + sstep *= src->pitch; + if (flipy) sstep = -sstep; + csp = (tColorRGBA *) ((Uint8 *) csp + sstep); + + /* + * Advance destination pointers + */ + dp = (tColorRGBA *) ((Uint8 *) dp + dgap); + } + } else { + + /* + * Non-Interpolating Zoom + */ + + csay = say; + for (y = 0; y < dst->h; y++) { + sp = csp; + csax = sax; + for (x = 0; x < dst->w; x++) { + /* + * Draw + */ + *dp = *sp; + /* + * Advance source pointers + */ + csax++; + sstep = (*csax >> 16); + if (flipx) sstep = -sstep; + sp += sstep; + /* + * Advance destination pointer + */ + dp++; + } + /* + * Advance source pointer + */ + csay++; + sstep = (*csay >> 16) * src->pitch; + if (flipy) sstep = -sstep; + csp = (tColorRGBA *) ((Uint8 *) csp + sstep); + + /* + * Advance destination pointers + */ + dp = (tColorRGBA *) ((Uint8 *) dp + dgap); + } + } + + /* + * Remove temp arrays + */ + free(sax); + free(say); + + return (0); +} + +/*! + +\brief Internal 8 bit Zoomer without smoothing. + +Zooms 8bit palette/Y 'src' surface to 'dst' surface. +Assumes src and dst surfaces are of 8 bit depth. +Assumes dst surface was allocated with the correct dimensions. + +\param src The surface to zoom (input). +\param dst The zoomed surface (output). +\param flipx Flag indicating if the image should be horizontally flipped. +\param flipy Flag indicating if the image should be vertically flipped. + +\return 0 for success or -1 for error. +*/ +int _zoomSurfaceY(SDL_Surface * src, SDL_Surface * dst, int flipx, int flipy) +{ + int x, y; + Uint32 *sax, *say, *csax, *csay; + int csx, csy; + Uint8 *sp, *dp, *csp; + int dgap; + + /* + * Allocate memory for row increments + */ + if ((sax = (Uint32 *) malloc((dst->w + 1) * sizeof(Uint32))) == NULL) { + return (-1); + } + if ((say = (Uint32 *) malloc((dst->h + 1) * sizeof(Uint32))) == NULL) { + free(sax); + return (-1); + } + + /* + * Pointer setup + */ + sp = csp = (Uint8 *) src->pixels; + dp = (Uint8 *) dst->pixels; + dgap = dst->pitch - dst->w; + + if (flipx) csp += (src->w-1); + if (flipy) csp = ( (Uint8*)csp + src->pitch*(src->h-1) ); + + /* + * Precalculate row increments + */ + csx = 0; + csax = sax; + for (x = 0; x < dst->w; x++) { + csx += src->w; + *csax = 0; + while (csx >= dst->w) { + csx -= dst->w; + (*csax)++; + } + csax++; + } + csy = 0; + csay = say; + for (y = 0; y < dst->h; y++) { + csy += src->h; + *csay = 0; + while (csy >= dst->h) { + csy -= dst->h; + (*csay)++; + } + csay++; + } + + /* + * Draw + */ + csay = say; + for (y = 0; y < dst->h; y++) { + csax = sax; + sp = csp; + for (x = 0; x < dst->w; x++) { + /* + * Draw + */ + *dp = *sp; + /* + * Advance source pointers + */ + sp += (*csax) * (flipx ? -1 : 1); + csax++; + /* + * Advance destination pointer + */ + dp++; + } + /* + * Advance source pointer (for row) + */ + csp += ((*csay) * src->pitch) * (flipy ? -1 : 1); + csay++; + + /* + * Advance destination pointers + */ + dp += dgap; + } + + /* + * Remove temp arrays + */ + free(sax); + free(say); + + return (0); +} + +/*! +\brief Internal 32 bit rotozoomer with optional anti-aliasing. + +Rotates and zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control +parameters by scanning the destination surface and applying optionally anti-aliasing +by bilinear interpolation. +Assumes src and dst surfaces are of 32 bit depth. +Assumes dst surface was allocated with the correct dimensions. + +\param src Source surface. +\param dst Destination surface. +\param cx Horizontal center coordinate. +\param cy Vertical center coordinate. +\param isin Integer version of sine of angle. +\param icos Integer version of cosine of angle. +\param flipx Flag indicating horizontal mirroring should be applied. +\param flipy Flag indicating vertical mirroring should be applied. +\param smooth Flag indicating anti-aliasing should be used. +*/ +void _transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth) +{ + int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh; + tColorRGBA c00, c01, c10, c11, cswap; + tColorRGBA *pc, *sp; + int gap; + + /* + * Variable setup + */ + xd = ((src->w - dst->w) << 15); + yd = ((src->h - dst->h) << 15); + ax = (cx << 16) - (icos * cx); + ay = (cy << 16) - (isin * cx); + sw = src->w - 1; + sh = src->h - 1; + pc = (tColorRGBA*) dst->pixels; + gap = dst->pitch - dst->w * 4; + + /* + * Switch between interpolating and non-interpolating code + */ + if (smooth) { + for (y = 0; y < dst->h; y++) { + dy = cy - y; + sdx = (ax + (isin * dy)) + xd; + sdy = (ay - (icos * dy)) + yd; + for (x = 0; x < dst->w; x++) { + dx = (sdx >> 16); + dy = (sdy >> 16); + if ((dx > -1) && (dy > -1) && (dx < src->w) && (dy < src->h)) { + if (flipx) dx = sw - dx; + if (flipy) dy = sh - dy; + sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy); + sp += dx; + c00 = *sp; + sp += 1; + c01 = *sp; + sp = (tColorRGBA *) ((Uint8 *) sp + src->pitch); + c11 = *sp; + sp -= 1; + c10 = *sp; + if (flipx) { + cswap = c00; c00=c01; c01=cswap; + cswap = c10; c10=c11; c11=cswap; + } + if (flipy) { + cswap = c00; c00=c10; c10=cswap; + cswap = c01; c01=c11; c11=cswap; + } + /* + * Interpolate colors + */ + ex = (sdx & 0xffff); + ey = (sdy & 0xffff); + t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff; + t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff; + pc->r = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff; + t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff; + pc->g = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff; + t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff; + pc->b = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff; + t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff; + pc->a = (((t2 - t1) * ey) >> 16) + t1; + } + sdx += icos; + sdy += isin; + pc++; + } + pc = (tColorRGBA *) ((Uint8 *) pc + gap); + } + } else { + for (y = 0; y < dst->h; y++) { + dy = cy - y; + sdx = (ax + (isin * dy)) + xd; + sdy = (ay - (icos * dy)) + yd; + for (x = 0; x < dst->w; x++) { + dx = (short) (sdx >> 16); + dy = (short) (sdy >> 16); + if (flipx) dx = (src->w-1)-dx; + if (flipy) dy = (src->h-1)-dy; + if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) { + sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy); + sp += dx; + *pc = *sp; + } + sdx += icos; + sdy += isin; + pc++; + } + pc = (tColorRGBA *) ((Uint8 *) pc + gap); + } + } +} + +/*! + +\brief Rotates and zooms 8 bit palette/Y 'src' surface to 'dst' surface without smoothing. + +Rotates and zooms 8 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control +parameters by scanning the destination surface. +Assumes src and dst surfaces are of 8 bit depth. +Assumes dst surface was allocated with the correct dimensions. + +\param src Source surface. +\param dst Destination surface. +\param cx Horizontal center coordinate. +\param cy Vertical center coordinate. +\param isin Integer version of sine of angle. +\param icos Integer version of cosine of angle. +\param flipx Flag indicating horizontal mirroring should be applied. +\param flipy Flag indicating vertical mirroring should be applied. +*/ +void transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy) +{ + int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay, sw, sh; + tColorY *pc, *sp; + int gap; + + /* + * Variable setup + */ + xd = ((src->w - dst->w) << 15); + yd = ((src->h - dst->h) << 15); + ax = (cx << 16) - (icos * cx); + ay = (cy << 16) - (isin * cx); + sw = src->w - 1; + sh = src->h - 1; + pc = (tColorY*) dst->pixels; + gap = dst->pitch - dst->w; + /* + * Clear surface to colorkey + */ + memset(pc, (unsigned char) (_colorkey(src) & 0xff), dst->pitch * dst->h); + /* + * Iterate through destination surface + */ + for (y = 0; y < dst->h; y++) { + dy = cy - y; + sdx = (ax + (isin * dy)) + xd; + sdy = (ay - (icos * dy)) + yd; + for (x = 0; x < dst->w; x++) { + dx = (short) (sdx >> 16); + dy = (short) (sdy >> 16); + if (flipx) dx = (src->w-1)-dx; + if (flipy) dy = (src->h-1)-dy; + if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) { + sp = (tColorY *) (src->pixels); + sp += (src->pitch * dy + dx); + *pc = *sp; + } + sdx += icos; + sdy += isin; + pc++; + } + pc += gap; + } +} +#endif + +/*! +\brief Rotates a 32 bit surface in increments of 90 degrees. + +Specialized 90 degree rotator which rotates a 'src' surface in 90 degree +increments clockwise returning a new surface. Faster than rotozoomer since +not scanning or interpolation takes place. Input surface must be 32 bit. +(code contributed by J. Schiller, improved by C. Allport and A. Schiffler) + +\param src Source surface to rotate. +\param numClockwiseTurns Number of clockwise 90 degree turns to apply to the source. + +\returns The new, rotated surface; or NULL for surfaces with incorrect input format. +*/ +SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns) +{ + int row, col, newWidth, newHeight; + int bpp, src_ipr, dst_ipr; + SDL_Surface* dst; + Uint32* srcBuf; + Uint32* dstBuf; + + /* Has to be a valid surface pointer and only 32-bit surfaces (for now) */ + if (!src || src->format->BitsPerPixel != 32) { return NULL; } + + /* normalize numClockwiseTurns */ + while(numClockwiseTurns < 0) { numClockwiseTurns += 4; } + numClockwiseTurns = (numClockwiseTurns % 4); + + /* if it's even, our new width will be the same as the source surface */ + newWidth = (numClockwiseTurns % 2) ? (src->h) : (src->w); + newHeight = (numClockwiseTurns % 2) ? (src->w) : (src->h); + dst = SDL_CreateRGBSurface( src->flags, newWidth, newHeight, src->format->BitsPerPixel, + src->format->Rmask, + src->format->Gmask, + src->format->Bmask, + src->format->Amask); + if(!dst) { + return NULL; + } + + if (SDL_MUSTLOCK(dst)) { + SDL_LockSurface(dst); + } + if (SDL_MUSTLOCK(dst)) { + SDL_LockSurface(dst); + } + + /* Calculate int-per-row */ + bpp = src->format->BitsPerPixel / 8; + src_ipr = src->pitch / bpp; + dst_ipr = dst->pitch / bpp; + + switch(numClockwiseTurns) { + case 0: /* Make a copy of the surface */ + { + /* Unfortunately SDL_BlitSurface cannot be used to make a copy of the surface + since it does not preserve alpha. */ + + if (src->pitch == dst->pitch) { + /* If the pitch is the same for both surfaces, the memory can be copied all at once. */ + memcpy(dst->pixels, src->pixels, (src->h * src->pitch)); + } + else + { + /* If the pitch differs, copy each row separately */ + srcBuf = (Uint32*)(src->pixels); + dstBuf = (Uint32*)(dst->pixels); + for (row = 0; row < src->h; row++) { + memcpy(dstBuf, srcBuf, dst->w * bpp); + srcBuf += src_ipr; + dstBuf += dst_ipr; + } /* end for(col) */ + } /* end for(row) */ + } + break; + + /* rotate clockwise */ + case 1: /* rotated 90 degrees clockwise */ + { + for (row = 0; row < src->h; ++row) { + srcBuf = (Uint32*)(src->pixels) + (row * src_ipr); + dstBuf = (Uint32*)(dst->pixels) + (dst->w - row - 1); + for (col = 0; col < src->w; ++col) { + *dstBuf = *srcBuf; + ++srcBuf; + dstBuf += dst_ipr; + } + /* end for(col) */ + } + /* end for(row) */ + } + break; + + case 2: /* rotated 180 degrees clockwise */ + { + for (row = 0; row < src->h; ++row) { + srcBuf = (Uint32*)(src->pixels) + (row * src_ipr); + dstBuf = (Uint32*)(dst->pixels) + ((dst->h - row - 1) * dst_ipr) + (dst->w - 1); + for (col = 0; col < src->w; ++col) { + *dstBuf = *srcBuf; + ++srcBuf; + --dstBuf; + } + } + } + break; + + case 3: + { + for (row = 0; row < src->h; ++row) { + srcBuf = (Uint32*)(src->pixels) + (row * src_ipr); + dstBuf = (Uint32*)(dst->pixels) + row + ((dst->h - 1) * dst_ipr); + for (col = 0; col < src->w; ++col) { + *dstBuf = *srcBuf; + ++srcBuf; + dstBuf -= dst_ipr; + } + } + } + break; + } + /* end switch */ + + if (SDL_MUSTLOCK(src)) { + SDL_UnlockSurface(src); + } + if (SDL_MUSTLOCK(dst)) { + SDL_UnlockSurface(dst); + } + + return dst; +} + + +#if 1 +/*! +\brief Internal target surface sizing function for rotozooms with trig result return. + +\param width The source surface width. +\param height The source surface height. +\param angle The angle to rotate in degrees. +\param zoomx The horizontal scaling factor. +\param zoomy The vertical scaling factor. +\param dstwidth The calculated width of the destination surface. +\param dstheight The calculated height of the destination surface. +\param canglezoom The sine of the angle adjusted by the zoom factor. +\param sanglezoom The cosine of the angle adjusted by the zoom factor. + +*/ +void _rotozoomSurfaceSizeTrig(int width, int height, double angle, double zoomx, double zoomy, + int *dstwidth, int *dstheight, + double *canglezoom, double *sanglezoom) +{ + double x, y, cx, cy, sx, sy; + double radangle; + int dstwidthhalf, dstheighthalf; + + /* + * Determine destination width and height by rotating a centered source box + */ + radangle = angle * (M_PI / 180.0); + *sanglezoom = sin(radangle); + *canglezoom = cos(radangle); + *sanglezoom *= zoomx; + *canglezoom *= zoomx; + x = width / 2; + y = height / 2; + cx = *canglezoom * x; + cy = *canglezoom * y; + sx = *sanglezoom * x; + sy = *sanglezoom * y; + + dstwidthhalf = MAX((int) + ceil(MAX(MAX(MAX(fabs(cx + sy), fabs(cx - sy)), fabs(-cx + sy)), fabs(-cx - sy))), 1); + dstheighthalf = MAX((int) + ceil(MAX(MAX(MAX(fabs(sx + cy), fabs(sx - cy)), fabs(-sx + cy)), fabs(-sx - cy))), 1); + *dstwidth = 2 * dstwidthhalf; + *dstheight = 2 * dstheighthalf; +} + +/*! +\brief Returns the size of the resulting target surface for a rotozoomSurfaceXY() call. + +\param width The source surface width. +\param height The source surface height. +\param angle The angle to rotate in degrees. +\param zoomx The horizontal scaling factor. +\param zoomy The vertical scaling factor. +\param dstwidth The calculated width of the rotozoomed destination surface. +\param dstheight The calculated height of the rotozoomed destination surface. +*/ +void rotozoomSurfaceSizeXY(int width, int height, double angle, double zoomx, double zoomy, int *dstwidth, int *dstheight) +{ + double dummy_sanglezoom, dummy_canglezoom; + + _rotozoomSurfaceSizeTrig(width, height, angle, zoomx, zoomy, dstwidth, dstheight, &dummy_sanglezoom, &dummy_canglezoom); +} + +/*! +\brief Returns the size of the resulting target surface for a rotozoomSurface() call. + +\param width The source surface width. +\param height The source surface height. +\param angle The angle to rotate in degrees. +\param zoom The scaling factor. +\param dstwidth The calculated width of the rotozoomed destination surface. +\param dstheight The calculated height of the rotozoomed destination surface. +*/ +void rotozoomSurfaceSize(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight) +{ + double dummy_sanglezoom, dummy_canglezoom; + + _rotozoomSurfaceSizeTrig(width, height, angle, zoom, zoom, dstwidth, dstheight, &dummy_sanglezoom, &dummy_canglezoom); +} + +/*! +\brief Rotates and zooms a surface and optional anti-aliasing. + +Rotates and zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface. +'angle' is the rotation in degrees and 'zoom' a scaling factor. If 'smooth' is set +then the destination 32bit surface is anti-aliased. If the surface is not 8bit +or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. + +\param src The surface to rotozoom. +\param angle The angle to rotate in degrees. +\param zoom The scaling factor. +\param smooth Antialiasing flag; set to SMOOTHING_ON to enable. + +\return The new rotozoomed surface. +*/ +SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int smooth) +{ + return rotozoomSurfaceXY(src, angle, zoom, zoom, smooth); +} + +/*! +\brief Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing. + +Rotates and zooms a 32bit or 8bit 'src' surface to newly created 'dst' surface. +'angle' is the rotation in degrees, 'zoomx and 'zoomy' scaling factors. If 'smooth' is set +then the destination 32bit surface is anti-aliased. If the surface is not 8bit +or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. + +\param src The surface to rotozoom. +\param angle The angle to rotate in degrees. +\param zoomx The horizontal scaling factor. +\param zoomy The vertical scaling factor. +\param smooth Antialiasing flag; set to SMOOTHING_ON to enable. + +\return The new rotozoomed surface. +*/ +SDL_Surface *rotozoomSurfaceXY(SDL_Surface * src, double angle, double zoomx, double zoomy, int smooth) +{ + SDL_Surface *rz_src; + SDL_Surface *rz_dst; + double zoominv; + double sanglezoom, canglezoom, sanglezoominv, canglezoominv; + int dstwidthhalf, dstwidth, dstheighthalf, dstheight; + int is32bit; + int i, src_converted; + int flipx,flipy; + Uint8 r,g,b; + Uint32 colorkey = 0; + int colorKeyAvailable = 0; + + /* + * Sanity check + */ + if (src == NULL) + return (NULL); + + if (src->flags & SDL_SRCCOLORKEY) + { + colorkey = _colorkey(src); + SDL_GetRGB(colorkey, src->format, &r, &g, &b); + colorKeyAvailable = 1; + } + /* + * Determine if source surface is 32bit or 8bit + */ + is32bit = (src->format->BitsPerPixel == 32); + if ((is32bit) || (src->format->BitsPerPixel == 8)) { + /* + * Use source surface 'as is' + */ + rz_src = src; + src_converted = 0; + } else { + /* + * New source surface is 32bit with a defined RGBA ordering + */ + rz_src = + SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, +#if SDL_BYTEORDER == SDL_LIL_ENDIAN + 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 +#else + 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff +#endif + ); + if(colorKeyAvailable) + SDL_SetColorKey(src, 0, 0); + + SDL_BlitSurface(src, NULL, rz_src, NULL); + + if(colorKeyAvailable) + SDL_SetColorKey(src, SDL_SRCCOLORKEY, colorkey); + src_converted = 1; + is32bit = 1; + } + + /* + * Sanity check zoom factor + */ + flipx = (zoomx<0.0); + if (flipx) zoomx=-zoomx; + flipy = (zoomy<0.0); + if (flipy) zoomy=-zoomy; + if (zoomx < VALUE_LIMIT) zoomx = VALUE_LIMIT; + if (zoomy < VALUE_LIMIT) zoomy = VALUE_LIMIT; + zoominv = 65536.0 / (zoomx * zoomx); + + /* + * Check if we have a rotozoom or just a zoom + */ + if (fabs(angle) > VALUE_LIMIT) { + + /* + * Angle!=0: full rotozoom + */ + /* + * ----------------------- + */ + + /* Determine target size */ + _rotozoomSurfaceSizeTrig(rz_src->w, rz_src->h, angle, zoomx, zoomy, &dstwidth, &dstheight, &canglezoom, &sanglezoom); + + /* + * Calculate target factors from sin/cos and zoom + */ + sanglezoominv = sanglezoom; + canglezoominv = canglezoom; + sanglezoominv *= zoominv; + canglezoominv *= zoominv; + + /* Calculate half size */ + dstwidthhalf = dstwidth / 2; + dstheighthalf = dstheight / 2; + + /* + * Alloc space to completely contain the rotated surface + */ + rz_dst = NULL; + if (is32bit) { + /* + * Target surface is 32bit with source RGBA/ABGR ordering + */ + rz_dst = + SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32, + rz_src->format->Rmask, rz_src->format->Gmask, + rz_src->format->Bmask, rz_src->format->Amask); + } else { + /* + * Target surface is 8bit + */ + rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0); + } + + if (colorKeyAvailable == 1){ + colorkey = SDL_MapRGB(rz_dst->format, r, g, b); + + SDL_FillRect(rz_dst, NULL, colorkey ); + } + + /* + * Lock source surface + */ + if (SDL_MUSTLOCK(rz_src)) { + SDL_LockSurface(rz_src); + } + + /* + * Check which kind of surface we have + */ + if (is32bit) { + /* + * Call the 32bit transformation routine to do the rotation (using alpha) + */ + _transformSurfaceRGBA(rz_src, rz_dst, dstwidthhalf, dstheighthalf, + (int) (sanglezoominv), (int) (canglezoominv), + flipx, flipy, + smooth); + /* + * Turn on source-alpha support + */ + SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255); + SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src)); + } else { + /* + * Copy palette and colorkey info + */ + for (i = 0; i < rz_src->format->palette->ncolors; i++) { + rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i]; + } + rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors; + /* + * Call the 8bit transformation routine to do the rotation + */ + transformSurfaceY(rz_src, rz_dst, dstwidthhalf, dstheighthalf, + (int) (sanglezoominv), (int) (canglezoominv), + flipx, flipy); + SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src)); + } + /* + * Unlock source surface + */ + if (SDL_MUSTLOCK(rz_src)) { + SDL_UnlockSurface(rz_src); + } + + } else { + + /* + * Angle=0: Just a zoom + */ + /* + * -------------------- + */ + + /* + * Calculate target size + */ + zoomSurfaceSize(rz_src->w, rz_src->h, zoomx, zoomy, &dstwidth, &dstheight); + + /* + * Alloc space to completely contain the zoomed surface + */ + rz_dst = NULL; + if (is32bit) { + /* + * Target surface is 32bit with source RGBA/ABGR ordering + */ + rz_dst = + SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32, + rz_src->format->Rmask, rz_src->format->Gmask, + rz_src->format->Bmask, rz_src->format->Amask); + } else { + /* + * Target surface is 8bit + */ + rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0); + } + + if (colorKeyAvailable == 1){ + colorkey = SDL_MapRGB(rz_dst->format, r, g, b); + + SDL_FillRect(rz_dst, NULL, colorkey ); + } + + /* + * Lock source surface + */ + if (SDL_MUSTLOCK(rz_src)) { + SDL_LockSurface(rz_src); + } + + /* + * Check which kind of surface we have + */ + if (is32bit) { + /* + * Call the 32bit transformation routine to do the zooming (using alpha) + */ + _zoomSurfaceRGBA(rz_src, rz_dst, flipx, flipy, smooth); + /* + * Turn on source-alpha support + */ + SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255); + SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src)); + } else { + /* + * Copy palette and colorkey info + */ + for (i = 0; i < rz_src->format->palette->ncolors; i++) { + rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i]; + } + rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors; + /* + * Call the 8bit transformation routine to do the zooming + */ + _zoomSurfaceY(rz_src, rz_dst, flipx, flipy); + SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src)); + } + /* + * Unlock source surface + */ + if (SDL_MUSTLOCK(rz_src)) { + SDL_UnlockSurface(rz_src); + } + } + + /* + * Cleanup temp surface + */ + if (src_converted) { + SDL_FreeSurface(rz_src); + } + + /* + * Return destination surface + */ + rz_dst->h -= GUARD_ROWS; + return (rz_dst); +} + +/*! +\brief Calculates the size of the target surface for a zoomSurface() call. + +The minimum size of the target surface is 1. The input factors can be positive or negative. + +\param width The width of the source surface to zoom. +\param height The height of the source surface to zoom. +\param zoomx The horizontal zoom factor. +\param zoomy The vertical zoom factor. +\param dstwidth Pointer to an integer to store the calculated width of the zoomed target surface. +\param dstheight Pointer to an integer to store the calculated height of the zoomed target surface. +*/ +void zoomSurfaceSize(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight) +{ + /* + * Make zoom factors positive + */ + int flipx, flipy; + flipx = (zoomx<0.0); + if (flipx) zoomx = -zoomx; + flipy = (zoomy<0.0); + if (flipy) zoomy = -zoomy; + + /* + * Sanity check zoom factors + */ + if (zoomx < VALUE_LIMIT) { + zoomx = VALUE_LIMIT; + } + if (zoomy < VALUE_LIMIT) { + zoomy = VALUE_LIMIT; + } + + /* + * Calculate target size + */ + *dstwidth = (int) ((double) width * zoomx); + *dstheight = (int) ((double) height * zoomy); + if (*dstwidth < 1) { + *dstwidth = 1; + } + if (*dstheight < 1) { + *dstheight = 1; + } +} + +/*! +\brief Zoom a surface by independent horizontal and vertical factors with optional smoothing. + +Zooms a 32bit or 8bit 'src' surface to newly created 'dst' surface. +'zoomx' and 'zoomy' are scaling factors for width and height. If 'smooth' is on +then the destination 32bit surface is anti-aliased. If the surface is not 8bit +or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. +If zoom factors are negative, the image is flipped on the axes. + +\param src The surface to zoom. +\param zoomx The horizontal zoom factor. +\param zoomy The vertical zoom factor. +\param smooth Antialiasing flag; set to SMOOTHING_ON to enable. + +\return The new, zoomed surface. +*/ +SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smooth) +{ + SDL_Surface *rz_src; + SDL_Surface *rz_dst; + int dstwidth, dstheight; + int is32bit; + int i, src_converted; + int flipx, flipy; + + /* + * Sanity check + */ + if (src == NULL) + return (NULL); + + /* + * Determine if source surface is 32bit or 8bit + */ + is32bit = (src->format->BitsPerPixel == 32); + if ((is32bit) || (src->format->BitsPerPixel == 8)) { + /* + * Use source surface 'as is' + */ + rz_src = src; + src_converted = 0; + } else { + /* + * New source surface is 32bit with a defined RGBA ordering + */ + rz_src = + SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, +#if SDL_BYTEORDER == SDL_LIL_ENDIAN + 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 +#else + 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff +#endif + ); + SDL_BlitSurface(src, NULL, rz_src, NULL); + src_converted = 1; + is32bit = 1; + } + + flipx = (zoomx<0.0); + if (flipx) zoomx = -zoomx; + flipy = (zoomy<0.0); + if (flipy) zoomy = -zoomy; + + /* Get size if target */ + zoomSurfaceSize(rz_src->w, rz_src->h, zoomx, zoomy, &dstwidth, &dstheight); + + /* + * Alloc space to completely contain the zoomed surface + */ + rz_dst = NULL; + if (is32bit) { + /* + * Target surface is 32bit with source RGBA/ABGR ordering + */ + rz_dst = + SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32, + rz_src->format->Rmask, rz_src->format->Gmask, + rz_src->format->Bmask, rz_src->format->Amask); + } else { + /* + * Target surface is 8bit + */ + rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0); + } + + /* + * Lock source surface + */ + if (SDL_MUSTLOCK(rz_src)) { + SDL_LockSurface(rz_src); + } + + /* + * Check which kind of surface we have + */ + if (is32bit) { + /* + * Call the 32bit transformation routine to do the zooming (using alpha) + */ + _zoomSurfaceRGBA(rz_src, rz_dst, flipx, flipy, smooth); + /* + * Turn on source-alpha support + */ + SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255); + } else { + /* + * Copy palette and colorkey info + */ + for (i = 0; i < rz_src->format->palette->ncolors; i++) { + rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i]; + } + rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors; + /* + * Call the 8bit transformation routine to do the zooming + */ + _zoomSurfaceY(rz_src, rz_dst, flipx, flipy); + SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src)); + } + /* + * Unlock source surface + */ + if (SDL_MUSTLOCK(rz_src)) { + SDL_UnlockSurface(rz_src); + } + + /* + * Cleanup temp surface + */ + if (src_converted) { + SDL_FreeSurface(rz_src); + } + + /* + * Return destination surface + */ + rz_dst->h -= GUARD_ROWS; + return (rz_dst); +} + +/*! +\brief Shrink a surface by an integer ratio using averaging. + +Shrinks a 32bit or 8bit 'src' surface to a newly created 'dst' surface. +'factorx' and 'factory' are the shrinking ratios (i.e. 2=1/2 the size, +3=1/3 the size, etc.) The destination surface is antialiased by averaging +the source box RGBA or Y information. If the surface is not 8bit +or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. +The input surface is not modified. The output surface is newly allocated. + +\param src The surface to shrink. +\param factorx The horizontal shrinking ratio. +\param factory The vertical shrinking ratio. + +\return The new, shrunken surface. +*/ +SDL_Surface *shrinkSurface(SDL_Surface *src, int factorx, int factory) +{ + SDL_Surface *rz_src; + SDL_Surface *rz_dst; + int dstwidth, dstheight; + int is32bit; + int i, src_converted; + + /* + * Sanity check + */ + if (src == NULL) + return (NULL); + + /* + * Determine if source surface is 32bit or 8bit + */ + is32bit = (src->format->BitsPerPixel == 32); + if ((is32bit) || (src->format->BitsPerPixel == 8)) { + /* + * Use source surface 'as is' + */ + rz_src = src; + src_converted = 0; + } else { + /* + * New source surface is 32bit with a defined RGBA ordering + */ + rz_src = + SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, +#if SDL_BYTEORDER == SDL_LIL_ENDIAN + 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 +#else + 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff +#endif + ); + SDL_BlitSurface(src, NULL, rz_src, NULL); + src_converted = 1; + is32bit = 1; + } + + /* Get size for target */ + dstwidth=rz_src->w/factorx; + while (dstwidth*factorx>rz_src->w) { dstwidth--; } + dstheight=rz_src->h/factory; + while (dstheight*factory>rz_src->h) { dstheight--; } + + /* + * Alloc space to completely contain the shrunken surface + * (with added guard rows) + */ + rz_dst = NULL; + if (is32bit) { + /* + * Target surface is 32bit with source RGBA/ABGR ordering + */ + rz_dst = + SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32, + rz_src->format->Rmask, rz_src->format->Gmask, + rz_src->format->Bmask, rz_src->format->Amask); + } else { + /* + * Target surface is 8bit + */ + rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0); + } + + /* + * Lock source surface + */ + if (SDL_MUSTLOCK(rz_src)) { + SDL_LockSurface(rz_src); + } + + /* + * Check which kind of surface we have + */ + if (is32bit) { + /* + * Call the 32bit transformation routine to do the shrinking (using alpha) + */ + _shrinkSurfaceRGBA(rz_src, rz_dst, factorx, factory); + /* + * Turn on source-alpha support + */ + SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255); + } else { + /* + * Copy palette and colorkey info + */ + for (i = 0; i < rz_src->format->palette->ncolors; i++) { + rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i]; + } + rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors; + /* + * Call the 8bit transformation routine to do the shrinking + */ + _shrinkSurfaceY(rz_src, rz_dst, factorx, factory); + SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src)); + } + + /* + * Unlock source surface + */ + if (SDL_MUSTLOCK(rz_src)) { + SDL_UnlockSurface(rz_src); + } + + /* + * Cleanup temp surface + */ + if (src_converted) { + SDL_FreeSurface(rz_src); + } + + /* + * Return destination surface + */ + rz_dst->h -= GUARD_ROWS; + return (rz_dst); +} +#endif diff --git a/tizen/src/sdl_rotate.h b/tizen/src/sdl_rotate.h new file mode 100644 index 0000000..4611690 --- /dev/null +++ b/tizen/src/sdl_rotate.h @@ -0,0 +1,99 @@ +/* + * Derived from: SDL_rotozoom, LGPL (c) A. Schiffler from the SDL_gfx library. + * Modifications by Hyunjun Son(hj79.son@samsung.com) + * + * This work is licensed under the terms of the GNU GPL version 2. + * See the COPYING file in the top-level directory. + * + */ + +#ifndef _SDL_rotate_h +#define _SDL_rotate_h + +#include +#include + +/* ---- Prototypes */ + +#ifdef WIN32 +# ifdef DLL_EXPORT +# define SDL_ROTOZOOM_SCOPE __declspec(dllexport) +# else +# ifdef LIBSDL_GFX_DLL_IMPORT +# define SDL_ROTOZOOM_SCOPE __declspec(dllimport) +# endif +# endif +#endif +#ifndef SDL_ROTOZOOM_SCOPE +# define SDL_ROTOZOOM_SCOPE extern +#endif + +/* + + rotozoomSurface() + + Rotates and zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface. + 'angle' is the rotation in degrees. 'zoom' a scaling factor. If 'smooth' is 1 + then the destination 32bit surface is anti-aliased. If the surface is not 8bit + or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. + + */ + +SDL_ROTOZOOM_SCOPE SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int smooth); + +SDL_ROTOZOOM_SCOPE SDL_Surface *rotozoomSurfaceXY +(SDL_Surface * src, double angle, double zoomx, double zoomy, int smooth); + +/* Returns the size of the target surface for a rotozoomSurface() call */ + +SDL_ROTOZOOM_SCOPE void rotozoomSurfaceSize(int width, int height, double angle, double zoom, int *dstwidth, + int *dstheight); + +SDL_ROTOZOOM_SCOPE void rotozoomSurfaceSizeXY +(int width, int height, double angle, double zoomx, double zoomy, + int *dstwidth, int *dstheight); + +/* + + zoomSurface() + + Zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface. + 'zoomx' and 'zoomy' are scaling factors for width and height. If 'smooth' is 1 + then the destination 32bit surface is anti-aliased. If the surface is not 8bit + or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. + + */ + +SDL_ROTOZOOM_SCOPE SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smooth); + +/* Returns the size of the target surface for a zoomSurface() call */ + +SDL_ROTOZOOM_SCOPE void zoomSurfaceSize(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight); + + +/* + shrinkSurface() + + Shrinks a 32bit or 8bit 'src' surface ti a newly created 'dst' surface. + 'factorx' and 'factory' are the shrinking ratios (i.e. 2=1/2 the size, + 3=1/3 the size, etc.) The destination surface is antialiased by averaging + the source box RGBA or Y information. If the surface is not 8bit + or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. + */ + +SDL_ROTOZOOM_SCOPE SDL_Surface *shrinkSurface(SDL_Surface * src, int factorx, int factory); + +/* + + Other functions + + */ + +SDL_ROTOZOOM_SCOPE SDL_Surface* rotateSurface90Degrees(SDL_Surface* pSurf, int numClockwiseTurns); + +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +} +#endif + +#endif /* _SDL_rotate_h */ diff --git a/tizen/src/sdl_zoom.c b/tizen/src/sdl_zoom.c new file mode 100644 index 0000000..287f9e8 --- /dev/null +++ b/tizen/src/sdl_zoom.c @@ -0,0 +1,96 @@ +/* + * SDL_zoom - surface scaling + * + * Copyright (c) 2009 Citrix Systems, Inc. + * + * Derived from: SDL_rotozoom, LGPL (c) A. Schiffler from the SDL_gfx library. + * Modifications by Stefano Stabellini. + * + * This work is licensed under the terms of the GNU GPL version 2. + * See the COPYING file in the top-level directory. + * + */ + +#include "sdl_zoom.h" +#include "osdep.h" +#include +#include + +static int sdl_zoom_rgb16(SDL_Surface *src, SDL_Surface *dst, int smooth, + SDL_Rect *dst_rect); +static int sdl_zoom_rgb32(SDL_Surface *src, SDL_Surface *dst, int smooth, + SDL_Rect *dst_rect); + +#define BPP 32 +#include "sdl_zoom_template.h" +#undef BPP +#define BPP 16 +#include "sdl_zoom_template.h" +#undef BPP + +#if 0 +int sdl_zoom_blit(SDL_Surface *src_sfc, SDL_Surface *dst_sfc, int smooth, + SDL_Rect *in_rect) +{ + SDL_Rect zoom, src_rect; + int extra; + + /* Grow the size of the modified rectangle to avoid edge artefacts */ + src_rect.x = (in_rect->x > 0) ? (in_rect->x - 1) : 0; + src_rect.y = (in_rect->y > 0) ? (in_rect->y - 1) : 0; + + src_rect.w = in_rect->w + 1; + if (src_rect.x + src_rect.w > src_sfc->w) + src_rect.w = src_sfc->w - src_rect.x; + + src_rect.h = in_rect->h + 1; + if (src_rect.y + src_rect.h > src_sfc->h) + src_rect.h = src_sfc->h - src_rect.y; + + /* (x,y) : round down */ + zoom.x = (int)(((float)(src_rect.x * dst_sfc->w)) / (float)(src_sfc->w)); + zoom.y = (int)(((float)(src_rect.y * dst_sfc->h)) / (float)(src_sfc->h)); + + /* (w,h) : round up */ + zoom.w = (int)( ((double)((src_rect.w * dst_sfc->w) + (src_sfc->w - 1))) / + (double)(src_sfc->w)); + + zoom.h = (int)( ((double)((src_rect.h * dst_sfc->h) + (src_sfc->h - 1))) / + (double)(src_sfc->h)); + + /* Account for any (x,y) rounding by adding one-source-pixel's worth + * of destination pixels and then edge checking. + */ + + extra = ((dst_sfc->w-1) / src_sfc->w) + 1; + + if ((zoom.x + zoom.w) < (dst_sfc->w - extra)) + zoom.w += extra; + else + zoom.w = dst_sfc->w - zoom.x; + + extra = ((dst_sfc->h-1) / src_sfc->h) + 1; + + if ((zoom.y + zoom.h) < (dst_sfc->h - extra)) + zoom.h += extra; + else + zoom.h = dst_sfc->h - zoom.y; + + /* The rectangle (zoom.x, zoom.y, zoom.w, zoom.h) is the area on the + * destination surface that needs to be updated. + */ + if (src_sfc->format->BitsPerPixel == 32) + sdl_zoom_rgb32(src_sfc, dst_sfc, smooth, &zoom); + else if (src_sfc->format->BitsPerPixel == 16) + sdl_zoom_rgb16(src_sfc, dst_sfc, smooth, &zoom); + else { + fprintf(stderr, "pixel format not supported\n"); + return -1; + } + + /* Return the rectangle of the update to the caller */ + *in_rect = zoom; + + return 0; +} +#endif diff --git a/tizen/src/sdl_zoom.h b/tizen/src/sdl_zoom.h new file mode 100644 index 0000000..1303352 --- /dev/null +++ b/tizen/src/sdl_zoom.h @@ -0,0 +1,25 @@ +/* + * SDL_zoom - surface scaling + * + * Copyright (c) 2009 Citrix Systems, Inc. + * + * Derived from: SDL_rotozoom, LGPL (c) A. Schiffler from the SDL_gfx library. + * Modifications by Stefano Stabellini. + * + * This work is licensed under the terms of the GNU GPL version 2. + * See the COPYING file in the top-level directory. + * + */ + +#ifndef SDL_zoom_h +#define SDL_zoom_h + +#include + +#define SMOOTHING_OFF 0 +#define SMOOTHING_ON 1 + +//int sdl_zoom_blit(SDL_Surface *src_sfc, SDL_Surface *dst_sfc, + // int smooth, SDL_Rect *src_rect); + +#endif /* SDL_zoom_h */ diff --git a/tizen/src/sdl_zoom_template.h b/tizen/src/sdl_zoom_template.h new file mode 100644 index 0000000..64bbca8 --- /dev/null +++ b/tizen/src/sdl_zoom_template.h @@ -0,0 +1,225 @@ +/* + * SDL_zoom_template - surface scaling + * + * Copyright (c) 2009 Citrix Systems, Inc. + * + * Derived from: SDL_rotozoom, LGPL (c) A. Schiffler from the SDL_gfx library. + * Modifications by Stefano Stabellini. + * + * This work is licensed under the terms of the GNU GPL version 2. + * See the COPYING file in the top-level directory. + * + */ + +#if BPP == 16 +#define SDL_TYPE Uint16 +#elif BPP == 32 +#define SDL_TYPE Uint32 +#else +#error unsupport depth +#endif + +/* + * Simple helper functions to make the code looks nicer + * + * Assume spf = source SDL_PixelFormat + * dpf = dest SDL_PixelFormat + * + */ +#define getRed(color) (((color) & spf->Rmask) >> spf->Rshift) +#define getGreen(color) (((color) & spf->Gmask) >> spf->Gshift) +#define getBlue(color) (((color) & spf->Bmask) >> spf->Bshift) +#define getAlpha(color) (((color) & spf->Amask) >> spf->Ashift) + +#define setRed(r, pcolor) do { \ + *pcolor = ((*pcolor) & (~(dpf->Rmask))) + \ + (((r) & (dpf->Rmask >> dpf->Rshift)) << dpf->Rshift); \ +} while (0); + +#define setGreen(g, pcolor) do { \ + *pcolor = ((*pcolor) & (~(dpf->Gmask))) + \ + (((g) & (dpf->Gmask >> dpf->Gshift)) << dpf->Gshift); \ +} while (0); + +#define setBlue(b, pcolor) do { \ + *pcolor = ((*pcolor) & (~(dpf->Bmask))) + \ + (((b) & (dpf->Bmask >> dpf->Bshift)) << dpf->Bshift); \ +} while (0); + +#define setAlpha(a, pcolor) do { \ + *pcolor = ((*pcolor) & (~(dpf->Amask))) + \ + (((a) & (dpf->Amask >> dpf->Ashift)) << dpf->Ashift); \ +} while (0); + +static int glue(sdl_zoom_rgb, BPP)(SDL_Surface *src, SDL_Surface *dst, int smooth, + SDL_Rect *dst_rect) +{ + int x, y, sx, sy, *sax, *say, *csax, *csay, csx, csy, ex, ey, t1, t2, sstep, sstep_jump; + SDL_TYPE *c00, *c01, *c10, *c11, *sp, *csp, *dp; + int d_gap; + SDL_PixelFormat *spf = src->format; + SDL_PixelFormat *dpf = dst->format; + + if (smooth) { + /* For interpolation: assume source dimension is one pixel. + * Smaller here to avoid overflow on right and bottom edge. + */ + sx = (int) (65536.0 * (float) (src->w - 1) / (float) dst->w); + sy = (int) (65536.0 * (float) (src->h - 1) / (float) dst->h); + } else { + sx = (int) (65536.0 * (float) src->w / (float) dst->w); + sy = (int) (65536.0 * (float) src->h / (float) dst->h); + } + + if ((sax = (int *) malloc((dst->w + 1) * sizeof(Uint32))) == NULL) { + return (-1); + } + if ((say = (int *) malloc((dst->h + 1) * sizeof(Uint32))) == NULL) { + free(sax); + return (-1); + } + + sp = csp = (SDL_TYPE *) src->pixels; + dp = (SDL_TYPE *) (dst->pixels + dst_rect->y * dst->pitch + + dst_rect->x * dst->format->BytesPerPixel); + + csx = 0; + csax = sax; + for (x = 0; x <= dst->w; x++) { + *csax = csx; + csax++; + csx &= 0xffff; + csx += sx; + } + csy = 0; + csay = say; + for (y = 0; y <= dst->h; y++) { + *csay = csy; + csay++; + csy &= 0xffff; + csy += sy; + } + + d_gap = dst->pitch - dst_rect->w * dst->format->BytesPerPixel; + + if (smooth) { + csay = say; + for (y = 0; y < dst_rect->y; y++) { + csay++; + sstep = (*csay >> 16) * src->pitch; + csp = (SDL_TYPE *) ((Uint8 *) csp + sstep); + } + + /* Calculate sstep_jump */ + csax = sax; + sstep_jump = 0; + for (x = 0; x < dst_rect->x; x++) { + csax++; + sstep = (*csax >> 16); + sstep_jump += sstep; + } + + for (y = 0; y < dst_rect->h ; y++) { + /* Setup colour source pointers */ + c00 = csp + sstep_jump; + c01 = c00 + 1; + c10 = (SDL_TYPE *) ((Uint8 *) csp + src->pitch) + sstep_jump; + c11 = c10 + 1; + csax = sax + dst_rect->x; + + for (x = 0; x < dst_rect->w; x++) { + + /* Interpolate colours */ + ex = (*csax & 0xffff); + ey = (*csay & 0xffff); + t1 = ((((getRed(*c01) - getRed(*c00)) * ex) >> 16) + + getRed(*c00)) & (dpf->Rmask >> dpf->Rshift); + t2 = ((((getRed(*c11) - getRed(*c10)) * ex) >> 16) + + getRed(*c10)) & (dpf->Rmask >> dpf->Rshift); + setRed((((t2 - t1) * ey) >> 16) + t1, dp); + t1 = ((((getGreen(*c01) - getGreen(*c00)) * ex) >> 16) + + getGreen(*c00)) & (dpf->Gmask >> dpf->Gshift); + t2 = ((((getGreen(*c11) - getGreen(*c10)) * ex) >> 16) + + getGreen(*c10)) & (dpf->Gmask >> dpf->Gshift); + setGreen((((t2 - t1) * ey) >> 16) + t1, dp); + t1 = ((((getBlue(*c01) - getBlue(*c00)) * ex) >> 16) + + getBlue(*c00)) & (dpf->Bmask >> dpf->Bshift); + t2 = ((((getBlue(*c11) - getBlue(*c10)) * ex) >> 16) + + getBlue(*c10)) & (dpf->Bmask >> dpf->Bshift); + setBlue((((t2 - t1) * ey) >> 16) + t1, dp); + t1 = ((((getAlpha(*c01) - getAlpha(*c00)) * ex) >> 16) + + getAlpha(*c00)) & (dpf->Amask >> dpf->Ashift); + t2 = ((((getAlpha(*c11) - getAlpha(*c10)) * ex) >> 16) + + getAlpha(*c10)) & (dpf->Amask >> dpf->Ashift); + setAlpha((((t2 - t1) * ey) >> 16) + t1, dp); + + /* Advance source pointers */ + csax++; + sstep = (*csax >> 16); + c00 += sstep; + c01 += sstep; + c10 += sstep; + c11 += sstep; + /* Advance destination pointer */ + dp++; + } + /* Advance source pointer */ + csay++; + csp = (SDL_TYPE *) ((Uint8 *) csp + (*csay >> 16) * src->pitch); + /* Advance destination pointers */ + dp = (SDL_TYPE *) ((Uint8 *) dp + d_gap); + } + + + } else { + csay = say; + + for (y = 0; y < dst_rect->y; y++) { + csay++; + sstep = (*csay >> 16) * src->pitch; + csp = (SDL_TYPE *) ((Uint8 *) csp + sstep); + } + + /* Calculate sstep_jump */ + csax = sax; + sstep_jump = 0; + for (x = 0; x < dst_rect->x; x++) { + csax++; + sstep = (*csax >> 16); + sstep_jump += sstep; + } + + for (y = 0 ; y < dst_rect->h ; y++) { + sp = csp + sstep_jump; + csax = sax + dst_rect->x; + + for (x = 0; x < dst_rect->w; x++) { + + /* Draw */ + *dp = *sp; + + /* Advance source pointers */ + csax++; + sstep = (*csax >> 16); + sp += sstep; + + /* Advance destination pointer */ + dp++; + } + /* Advance source pointers */ + csay++; + sstep = (*csay >> 16) * src->pitch; + csp = (SDL_TYPE *) ((Uint8 *) csp + sstep); + + /* Advance destination pointer */ + dp = (SDL_TYPE *) ((Uint8 *) dp + d_gap); + } + } + + free(sax); + free(say); + return (0); +} + +#undef SDL_TYPE + diff --git a/tizen/src/skin/maruskin_client.c b/tizen/src/skin/maruskin_client.c index e9cc4af..2d7345b 100644 --- a/tizen/src/skin/maruskin_client.c +++ b/tizen/src/skin/maruskin_client.c @@ -86,32 +86,8 @@ static void* run_skin_client(void* arg) argv ); #ifdef _WIN32 -#if 0 WinExec( cmd, SW_SHOW ); #else - { - STARTUPINFO sti = { 0 }; - PROCESS_INFORMATION pi = { 0 }; - if(!CreateProcess(NULL, - cmd, - NULL, - NULL, - FALSE, - NORMAL_PRIORITY_CLASS, - NULL, - NULL, - &sti, - &pi)) - { - printf("Unable to generate process \n"); - exit(1); - } - DWORD rc = WaitForSingleObject( - pi.hProcess, // process handle - INFINITE); - } -#endif -#else if( system(cmd) ) { }else { diff --git a/tizen/src/skin/maruskin_operation.c b/tizen/src/skin/maruskin_operation.c index 22fa48b..d758e86 100644 --- a/tizen/src/skin/maruskin_operation.c +++ b/tizen/src/skin/maruskin_operation.c @@ -38,6 +38,7 @@ #include "sdb.h" #include "nbd.h" #include "../mloop_event.h" +#include "emul_state.h" #ifndef _WIN32 #include "maruskin_keymap.h" @@ -51,20 +52,6 @@ enum { }; enum { - ROTATION_PORTRAIT = 0, - ROTATION_LANDSCAPE = 1, - ROTATION_REVERSE_PORTRAIT = 2, - ROTATION_REVERSE_LANDSCAPE = 3, -}; - -enum { - SCALE_ONE = 1, - SCALE_THREE_QUARTERS = 2, - SCALE_HALF = 3, - SCALE_ONE_QUARTER = 4, -}; - -enum { MOUSE_DOWN = 1, MOUSE_UP = 2, MOUSE_DRAG = 3, @@ -75,9 +62,9 @@ enum { KEY_RELEASED = 2, }; -void start_display( int handle_id, int lcd_size_width, int lcd_size_height, short scale, short rotation ) { - INFO( "start_display handle_id:%d, lcd size:%dx%d, scale:%d, rotation:%d\n", - handle_id, lcd_size_width, lcd_size_height, scale, rotation ); +void start_display( int handle_id, int lcd_size_width, int lcd_size_height, double scale_factor, short rotation ) { + INFO( "start_display handle_id:%d, lcd size:%dx%d, scale_factor:%lf, rotation:%d\n", + handle_id, lcd_size_width, lcd_size_height, scale_factor, rotation ); maruskin_sdl_init(handle_id, lcd_size_width, lcd_size_height); } @@ -139,26 +126,40 @@ void do_hardkey_event( int event_type, int keycode ) { } -void do_rotation_event( int event_type) { +void do_scale_event( double scale_factor ) { + INFO( "do_scale_event scale_factor:%lf", scale_factor); - INFO( "do_rotation_event event_type:%d", event_type); + set_emul_win_scale(scale_factor); + + //TODO: thread safe + //qemu refresh + //vga_hw_invalidate(); + //vga_hw_update(); +} + +void do_rotation_event( int rotation_type) { + + INFO( "do_rotation_event rotation_type:%d", rotation_type); int buf_size = 32; char send_buf[32] = { 0 }; - switch ( event_type ) { - case ROTATION_PORTRAIT: - sprintf( send_buf, "1\n3\n0\n-9.80665\n0\n" ); - break; - case ROTATION_LANDSCAPE: - sprintf( send_buf, "1\n3\n0\n9.80665\n0\n" ); - break; - case ROTATION_REVERSE_PORTRAIT: - sprintf( send_buf, "1\n3\n-9.80665\n0\n0\n" ); - break; - case ROTATION_REVERSE_LANDSCAPE: - sprintf(send_buf, "1\n3\n9.80665\n0\n0\n"); - break; + switch ( rotation_type ) { + case ROTATION_PORTRAIT: + sprintf( send_buf, "1\n3\n0\n-9.80665\n0\n" ); + break; + case ROTATION_LANDSCAPE: + sprintf( send_buf, "1\n3\n0\n9.80665\n0\n" ); + break; + case ROTATION_REVERSE_PORTRAIT: + sprintf( send_buf, "1\n3\n-9.80665\n0\n0\n" ); + break; + case ROTATION_REVERSE_LANDSCAPE: + sprintf(send_buf, "1\n3\n9.80665\n0\n0\n"); + break; + + default: + break; } // send_to_sensor_daemon @@ -176,6 +177,9 @@ void do_rotation_event( int event_type) { socket_send( s, send_buf, buf_size ); INFO( "send to sendord(size: %d) 127.0.0.1:%d/tcp \n", buf_size, tizen_base_port + SDB_TCP_EMULD_INDEX); + + set_emul_rotation(rotation_type); + #ifdef _WIN32 closesocket( s ); #else diff --git a/tizen/src/skin/maruskin_operation.h b/tizen/src/skin/maruskin_operation.h old mode 100755 new mode 100644 index 5b6ffbe..e04386d --- a/tizen/src/skin/maruskin_operation.h +++ b/tizen/src/skin/maruskin_operation.h @@ -30,7 +30,7 @@ #ifndef MARUSKIN_OPERATION_H_ #define MARUSKIN_OPERATION_H_ -void start_display( int handle_id, int lcd_size_width, int lcd_size_height, short scale, short direction ); +void start_display( int handle_id, int lcd_size_width, int lcd_size_height, double scale_factor, short direction ); void do_mouse_event( int event_type, int x, int y, int z ); @@ -38,7 +38,9 @@ void do_key_event( int event_type, int keycode ); void do_hardkey_event( int event_type, int keycode ); -void do_rotation_event( int event_type ); +void do_scale_event( double scale_factor); + +void do_rotation_event( int rotation_type ); void open_shell(void); diff --git a/tizen/src/skin/maruskin_server.c b/tizen/src/skin/maruskin_server.c old mode 100755 new mode 100644 index a8a7154..7ea719f --- a/tizen/src/skin/maruskin_server.c +++ b/tizen/src/skin/maruskin_server.c @@ -47,6 +47,7 @@ #include "maruskin_operation.h" #include "debug_ch.h" #include "qemu-thread.h" +#include "emul_state.h" MULTI_DEBUG_CHANNEL( qemu, maruskin_server ); @@ -329,7 +330,8 @@ static void* run_skin_server( void* args ) { int handle_id = 0; int lcd_size_width = 0; int lcd_size_height = 0; - short scale = 0; + int scale = 0; + double scale_ratio = 0.0; short rotation = 0; char* p = readbuf; @@ -346,11 +348,14 @@ static void* run_skin_server( void* args ) { handle_id = ntohl( handle_id ); lcd_size_width = ntohl( lcd_size_width ); lcd_size_height = ntohl( lcd_size_height ); - scale = ntohs( scale ); + scale = ntohl( scale ); + scale_ratio = ((double)scale) / 100; rotation = ntohs( rotation ); + set_emul_win_scale(scale_ratio); + if ( start_heart_beat( client_sock ) ) { - start_display( handle_id, lcd_size_width, lcd_size_height, scale, rotation ); + start_display( handle_id, lcd_size_width, lcd_size_height, scale_ratio, rotation ); } else { stop_server = 1; } @@ -434,20 +439,29 @@ static void* run_skin_server( void* args ) { continue; } - short scale = 0; - short rotation = 0; + int scale = 0; + double scale_ratio = 0.0; + short rotation_type = 0; char* p = readbuf; memcpy( &scale, p, sizeof( scale ) ); p += sizeof( scale ); - memcpy( &rotation, p, sizeof( rotation ) ); + memcpy( &rotation_type, p, sizeof( rotation_type ) ); - scale = ntohs( scale ); - rotation = ntohs( rotation ); + scale = ntohl( scale ); + scale_ratio = ((double)scale) / 100; + rotation_type = ntohs( rotation_type ); - if ( is_sensord_initialized ) { - do_rotation_event( rotation ); + if ( get_emul_win_scale() != scale_ratio ) { + do_scale_event( scale_ratio ); } + + if ( is_sensord_initialized == 1 && get_emul_rotation() != rotation_type ) { + do_rotation_event( rotation_type ); + } + + maruskin_sdl_resize(); //send sdl event + break; } case RECV_RESPONSE_HEART_BEAT: { -- 2.7.4