diff --git a/gfx/thebes/gfxImageSurface.cpp b/gfx/thebes/gfxImageSurface.cpp index 258f1a8b8322..eec00cd47294 100644 --- a/gfx/thebes/gfxImageSurface.cpp +++ b/gfx/thebes/gfxImageSurface.cpp @@ -192,3 +192,27 @@ gfxImageSurface::CopyFrom(gfxImageSurface *other) return PR_TRUE; } + +already_AddRefed +gfxImageSurface::GetSubimage(const gfxRect& aRect) +{ + gfxRect r(aRect); + r.Round(); + unsigned char* subData = Data() + + (Stride() * (int)r.Y()) + + (int)r.X() * gfxASurface::BytePerPixelFromFormat(Format()); + + nsRefPtr image = + new gfxSubimageSurface(this, subData, + gfxIntSize((int)r.Width(), (int)r.Height())); + + return image.forget().get(); +} + +gfxSubimageSurface::gfxSubimageSurface(gfxImageSurface* aParent, + unsigned char* aData, + const gfxIntSize& aSize) + : gfxImageSurface(aData, aSize, aParent->Stride(), aParent->Format()) + , mParent(aParent) +{ +} diff --git a/gfx/thebes/gfxImageSurface.h b/gfx/thebes/gfxImageSurface.h index 51e1c3eaf15e..63176500266e 100644 --- a/gfx/thebes/gfxImageSurface.h +++ b/gfx/thebes/gfxImageSurface.h @@ -43,6 +43,8 @@ // ARGB -- raw buffer.. wont be changed.. good for storing data. +class gfxSubimageSurface; + /** * A raw image buffer. The format can be set in the constructor. Its main * purpose is for storing read-only images and using it as a source surface, @@ -99,6 +101,11 @@ public: /* Fast copy from another image surface; returns TRUE if successful, FALSE otherwise */ PRBool CopyFrom (gfxImageSurface *other); + /* return new Subimage with pointing to original image starting from aRect.pos + * and size of aRect.size. New subimage keeping current image reference + */ + already_AddRefed GetSubimage(const gfxRect& aRect); + protected: gfxImageSurface(); void InitFromSurface(cairo_surface_t *csurf); @@ -111,4 +118,14 @@ protected: long mStride; }; +class THEBES_API gfxSubimageSurface : public gfxImageSurface { +protected: + friend class gfxImageSurface; + gfxSubimageSurface(gfxImageSurface* aParent, + unsigned char* aData, + const gfxIntSize& aSize); +private: + nsRefPtr mParent; +}; + #endif /* GFX_IMAGESURFACE_H */