Skip to content
Browse files

Method to crop in a subrect of a surface

  • Loading branch information...
1 parent ec91b18 commit e648696386988067e477b7bd21862e9a6e225050 @rsaarelm committed Apr 22, 2012
Showing with 15 additions and 10 deletions.
  1. +13 −10 src/util/surface.cpp
  2. +2 −0 src/util/surface.hpp
View
23 src/util/surface.cpp
@@ -94,16 +94,19 @@ void Surface::init_image(int width, int height) {
}
Recti Surface::crop_rect() const {
- int x0 = width_, y0 = height_, x1 = 0, y1 = 0;
- for (int i = 0; i < width_ * height_; i++) {
- if ((*this)[i].a) {
- int x = i % width_, y = i / width_;
- if (x < x0) x0 = x;
- if (y < y0) y0 = y;
- if (x > x1) x1 = x;
- if (y > y1) y1 = y;
- }
- }
+ return crop_rect(Recti(Vec2i(width_, height_)));
+}
+
+Recti Surface::crop_rect(const Recti& sub_rect) const {
+ int x0 = sub_rect.max()[0], y0 = sub_rect.max()[1], x1 = sub_rect.min()[0], y1 = sub_rect.min()[1];
+ for (int y = sub_rect.min()[1]; y < sub_rect.max()[1]; y++)
+ for (int x = sub_rect.min()[0]; x < sub_rect.max()[0]; x++)
+ if ((*this)[Vec2i(x, y)].a) {
+ if (x < x0) x0 = x;
+ if (y < y0) y0 = y;
+ if (x > x1) x1 = x;
+ if (y > y1) y1 = y;
+ }
if (x0 < x1 && y0 < y1)
return Recti(Vec2i(x0, y0), Vec2i(x1 - x0 + 1, y1 - y0 + 1));
View
2 src/util/surface.hpp
@@ -68,6 +68,8 @@ class Surface {
/// non-transparent pixels.
Recti crop_rect() const;
+ Recti crop_rect(const Recti& sub_rect) const;
+
void blit(const Recti& src_rect, Surface& dest, const Vec2i& dest_pos);
bool contains(const Vec2i& pos) {

0 comments on commit e648696

Please sign in to comment.
Something went wrong with that request. Please try again.