Skip to content

Commit

Permalink
ADDED a simple downscaling function for surfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
ksterker committed Mar 20, 2013
1 parent 4881c8a commit 33ea682
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/gfx/sdl/screen_sdl.cc
Expand Up @@ -82,7 +82,7 @@ void gfx_screen_update()
{
if (shadow_surface)
{
shadow_surface->scale (display, base::Scale);
shadow_surface->scale_up (display, base::Scale);
}

SDL_Flip (display->get_vis());
Expand Down
28 changes: 27 additions & 1 deletion src/gfx/sdl/surface_sdl.cc
Expand Up @@ -219,7 +219,7 @@ namespace gfx
return col;
}

void surface_sdl::scale(surface *target, const u_int32 & factor) const
void surface_sdl::scale_up(surface *target, const u_int32 & factor) const
{
if (length() * factor > target->length() ||
height() * factor > target->height())
Expand Down Expand Up @@ -262,6 +262,32 @@ namespace gfx
unlock();
}

void surface_sdl::scale_down(surface *target, const u_int32 & factor) const
{
if (length() * factor > target->length() ||
height() * factor > target->height())
return;

lock();
target->lock();

s_int32 target_y = 0;
for (s_int32 src_y = factor/2; src_y < height(); src_y += factor)
{
s_int32 target_x = 0;
for (s_int32 src_x = factor/2; src_x < length(); src_x += factor)
{
u_int32 px = get_pix (src_x, src_y);
target->put_pix (target_x, target_y, px);
target_x++;
}
target_y++;
}

target->unlock();
unlock();
}

surface & surface_sdl::operator = (const surface& src)
{
const surface_sdl & src_sdl = (const surface_sdl &) src;
Expand Down
35 changes: 15 additions & 20 deletions src/gfx/sdl/surface_sdl.h
Expand Up @@ -35,31 +35,32 @@ namespace gfx

virtual ~surface_sdl ();

virtual void set_mask (bool m);
void set_mask (bool m);

virtual void set_alpha (const u_int8 & surface_alpha, const bool & alpha_channel = false);
void set_alpha (const u_int8 & surface_alpha, const bool & alpha_channel = false);

virtual void draw (s_int16 x, s_int16 y, s_int16 sx, s_int16 sy, u_int16 sl,
void draw (s_int16 x, s_int16 y, s_int16 sx, s_int16 sy, u_int16 sl,
u_int16 sh, const drawing_area * da_opt = NULL,
surface * target = NULL) const;

virtual void fillrect (s_int16 x, s_int16 y, u_int16 l, u_int16 h,
void fillrect (s_int16 x, s_int16 y, u_int16 l, u_int16 h,
u_int32 col, drawing_area * da_opt = NULL);

virtual void scale(surface *target, const u_int32 & factor) const;
void scale_up(surface *target, const u_int32 & factor) const;
void scale_down(surface *target, const u_int32 & factor) const;

virtual u_int32 map_color(const u_int8 & r, const u_int8 & g, const u_int8 & b, const u_int8 & a = 255) const;
virtual void unmap_color(u_int32 col, u_int8 & r, u_int8 & g, u_int8 & b, u_int8 & a) const;
virtual void lock () const;
virtual void unlock () const;
virtual void put_pix (u_int16 x, u_int16 y, u_int32 col);
virtual u_int32 get_pix (u_int16 x, u_int16 y) const;
u_int32 map_color(const u_int8 & r, const u_int8 & g, const u_int8 & b, const u_int8 & a = 255) const;
void unmap_color(u_int32 col, u_int8 & r, u_int8 & g, u_int8 & b, u_int8 & a) const;
void lock () const;
void unlock () const;
void put_pix (u_int16 x, u_int16 y, u_int32 col);
u_int32 get_pix (u_int16 x, u_int16 y) const;

virtual surface& operator = (const surface& src);
surface& operator = (const surface& src);

virtual void resize (u_int16 l, u_int16 h);
void resize (u_int16 l, u_int16 h);

virtual void clear ();
void clear ();

protected:
void set_data (void * data, u_int16 l, u_int16 h,
Expand All @@ -84,12 +85,6 @@ namespace gfx
/// SDL_Rects used in every blitting function.
static SDL_Rect srcrect, dstrect;

/// Used internally for blitting operations with drawing_areas.
void setup_rects (u_int16 x, u_int16 y, const drawing_area * draw_to) const
{
setup_rects (x, y, 0, 0, length (), height (), draw_to);
}

/// Used internally for blitting operations with drawing_areas.
void setup_rects (s_int16 x, s_int16 y, s_int16 sx, s_int16 sy,
u_int16 sl, u_int16 sh, const drawing_area * draw_to) const;
Expand Down
31 changes: 30 additions & 1 deletion src/gfx/sdl2/surface_sdl.cc
Expand Up @@ -427,7 +427,7 @@ namespace gfx
#endif
}

void surface_sdl::scale(surface *target, const u_int32 & factor) const
void surface_sdl::scale_up(surface *target, const u_int32 & factor) const
{
// scaling of the final result is handled in surface_sdl::draw
if (!target || target == display) return;
Expand Down Expand Up @@ -473,6 +473,35 @@ namespace gfx
SDL_FreeSurface(target_surf);
}

void surface_sdl::scale_down(surface *target, const u_int32 & factor) const
{
// downscaling directly to screen is not supported
if (!target || target == display) return;

if (length() / factor > target->length() ||
height() / factor > target->height())
return;

lock(NULL);
SDL_Surface *target_surf = ((surface_sdl*) target)->to_sw_surface ();

s_int32 target_y = 0;
for (s_int32 src_y = factor/2; src_y < height(); src_y += factor)
{
s_int32 target_x = 0;
for (s_int32 src_x = factor/2; src_x < length(); src_x += factor)
{
u_int32 px = get_pix (src_x, src_y);
target->put_pix (target_x, target_y, px);
target_x++;
}
target_y++;
}

target->unlock();
SDL_FreeSurface(target_surf);
}

surface & surface_sdl::operator = (const surface& src)
{
const surface_sdl & src_sdl = (const surface_sdl &) src;
Expand Down
35 changes: 15 additions & 20 deletions src/gfx/sdl2/surface_sdl.h
Expand Up @@ -51,31 +51,32 @@ namespace gfx

virtual ~surface_sdl ();

virtual void set_mask (bool m);
void set_mask (bool m);

virtual void set_alpha (const u_int8 & surface_alpha, const bool & alpha_channel = false);
void set_alpha (const u_int8 & surface_alpha, const bool & alpha_channel = false);

virtual void draw (s_int16 x, s_int16 y, s_int16 sx, s_int16 sy, u_int16 sl,
void draw (s_int16 x, s_int16 y, s_int16 sx, s_int16 sy, u_int16 sl,
u_int16 sh, const drawing_area * da_opt = NULL,
surface * target = NULL) const;

virtual void fillrect (s_int16 x, s_int16 y, u_int16 l, u_int16 h,
void fillrect (s_int16 x, s_int16 y, u_int16 l, u_int16 h,
u_int32 col, drawing_area * da_opt = NULL);

void scale(surface *target, const u_int32 & factor) const;
void scale_up(surface *target, const u_int32 & factor) const;
void scale_down(surface *target, const u_int32 & factor) const;

virtual u_int32 map_color(const u_int8 & r, const u_int8 & g, const u_int8 & b, const u_int8 & a = 255) const;
virtual void unmap_color(u_int32 col, u_int8 & r, u_int8 & g, u_int8 & b, u_int8 & a) const;
virtual void lock () const { lock(NULL); };
virtual void unlock () const;
virtual void put_pix (u_int16 x, u_int16 y, u_int32 col);
virtual u_int32 get_pix (u_int16 x, u_int16 y) const;
u_int32 map_color(const u_int8 & r, const u_int8 & g, const u_int8 & b, const u_int8 & a = 255) const;
void unmap_color(u_int32 col, u_int8 & r, u_int8 & g, u_int8 & b, u_int8 & a) const;
void lock () const { lock(NULL); };
void unlock () const;
void put_pix (u_int16 x, u_int16 y, u_int32 col);
u_int32 get_pix (u_int16 x, u_int16 y) const;

virtual surface& operator = (const surface& src);
surface& operator = (const surface& src);

virtual void resize (u_int16 l, u_int16 h);
void resize (u_int16 l, u_int16 h);

virtual void clear ();
void clear ();

protected:
void set_data (void * data, u_int16 l, u_int16 h,
Expand Down Expand Up @@ -107,12 +108,6 @@ namespace gfx
/// SDL_Rects used in every blitting function.
static SDL_Rect srcrect, dstrect;

/// Used internally for blitting operations with drawing_areas.
void setup_rects (u_int16 x, u_int16 y, const drawing_area * draw_to) const
{
setup_rects (x, y, 0, 0, length (), height (), draw_to);
}

/// Used internally for blitting operations with drawing_areas.
void setup_rects (s_int16 x, s_int16 y, s_int16 sx, s_int16 sy,
u_int16 sl, u_int16 sh, const drawing_area * draw_to) const;
Expand Down
18 changes: 14 additions & 4 deletions src/gfx/surface.h
Expand Up @@ -271,14 +271,24 @@ namespace gfx
void tile (const drawing_area *da_opt = NULL, surface *target = NULL) const;

/**
* Scale this image onto the target image with a given factor.
* The target image must be large enough to hold the scaled source
* image.
* Enlarge this image onto the target surface with a given factor.
* The target surface must be large enough to hold the scaled up
* source image.
*
* @param target the target image.
* @param factor the scaling factor.
*/
virtual void scale (surface *target, const u_int32 & factor) const = 0;
virtual void scale_up (surface *target, const u_int32 & factor) const = 0;

/**
* Shrink this image onto the target surface with a given factor.
* The target surface must be large enough to hold the scaled down
* source image.
*
* @param target the target image.
* @param factor the scaling factor.
*/
virtual void scale_down(surface *target, const u_int32 & factor) const = 0;

/**
* Adjust image brightness. Values < 127 will darken
Expand Down

0 comments on commit 33ea682

Please sign in to comment.