Browse Source

Move Texture2D::GetImage and TextureCube::GetImage implementations.

Eugene Kozlov 9 years ago
parent
commit
b16b7f6f7a

+ 8 - 32
Source/Urho3D/AngelScript/GraphicsAPI.cpp

@@ -181,42 +181,18 @@ static Viewport* ConstructViewportSceneCameraRect(Scene* scene, Camera* camera,
 
 
 static Image* Texture2DGetImage(Texture2D* tex2d)
 static Image* Texture2DGetImage(Texture2D* tex2d)
 {
 {
-    Image* rawImage = new Image(tex2d->GetContext());
-    const unsigned texSize = tex2d->GetDataSize(tex2d->GetWidth(), tex2d->GetHeight());
-    const unsigned format = tex2d->GetFormat();
-
-    if (format == Graphics::GetRGBAFormat())
-        rawImage->SetSize(tex2d->GetWidth(), tex2d->GetHeight(), 4);
-    else if (format == Graphics::GetRGBFormat())
-        rawImage->SetSize(tex2d->GetWidth(), tex2d->GetHeight(), 3);
-    else
-    {
-        delete rawImage;
-        return 0;
-    }
-
-    tex2d->GetData(0, rawImage->GetData());
-    return rawImage;
+    SharedPtr<Image> sharedImage = tex2d->GetImage();
+    Image* image = sharedImage;
+    sharedImage.Detach();
+    return image;
 }
 }
 
 
 static Image* TextureCubeGetImage(CubeMapFace face, TextureCube* texCube)
 static Image* TextureCubeGetImage(CubeMapFace face, TextureCube* texCube)
 {
 {
-    Image* rawImage = new Image(texCube->GetContext());
-    const unsigned texSize = texCube->GetDataSize(texCube->GetWidth(), texCube->GetHeight());
-    const unsigned format = texCube->GetFormat();
-
-    if (format == Graphics::GetRGBAFormat())
-        rawImage->SetSize(texCube->GetWidth(), texCube->GetHeight(), 4);
-    else if (format == Graphics::GetRGBFormat())
-        rawImage->SetSize(texCube->GetWidth(), texCube->GetHeight(), 3);
-    else
-    {
-        delete rawImage;
-        return 0;
-    }
-
-    texCube->GetData(face, 0, rawImage->GetData());
-    return rawImage;
+    SharedPtr<Image> sharedImage = texCube->GetImage(face);
+    Image* image = sharedImage;
+    sharedImage.Detach();
+    return image;
 }
 }
 
 
 static void ConstructRenderTargetInfo(RenderTargetInfo* ptr)
 static void ConstructRenderTargetInfo(RenderTargetInfo* ptr)

+ 20 - 0
Source/Urho3D/Graphics/Texture2D.cpp

@@ -156,6 +156,26 @@ bool Texture2D::SetSize(int width, int height, unsigned format, TextureUsage usa
     return Create();
     return Create();
 }
 }
 
 
+SharedPtr<Image> Texture2D::GetImage() const
+{
+    if (format_ != Graphics::GetRGBAFormat() && format_ != Graphics::GetRGBFormat())
+    {
+        URHO3D_LOGERROR("Unsupported texture format, can not convert to Image");
+        return SharedPtr<Image>();
+    }
+
+    Image* rawImage = new Image(context_);
+    if (format_ == Graphics::GetRGBAFormat())
+        rawImage->SetSize(width_, height_, 4);
+    else if (format_ == Graphics::GetRGBFormat())
+        rawImage->SetSize(width_, height_, 3);
+    else
+        assert(0);
+
+    GetData(0, rawImage->GetData());
+    return SharedPtr<Image>(rawImage);
+}
+
 void Texture2D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
 void Texture2D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
 {
 {
     if (renderSurface_ && (renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS || renderSurface_->IsUpdateQueued()))
     if (renderSurface_ && (renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS || renderSurface_->IsUpdateQueued()))

+ 2 - 0
Source/Urho3D/Graphics/Texture2D.h

@@ -68,6 +68,8 @@ public:
 
 
     /// Get data from a mip level. The destination buffer must be big enough. Return true if successful.
     /// Get data from a mip level. The destination buffer must be big enough. Return true if successful.
     bool GetData(unsigned level, void* dest) const;
     bool GetData(unsigned level, void* dest) const;
+    /// Get image data from zero mip level. Only RGB and RGBA textures are supported.
+    SharedPtr<Image> GetImage() const;
 
 
     /// Return render surface.
     /// Return render surface.
     RenderSurface* GetRenderSurface() const { return renderSurface_; }
     RenderSurface* GetRenderSurface() const { return renderSurface_; }

+ 20 - 0
Source/Urho3D/Graphics/TextureCube.cpp

@@ -316,6 +316,26 @@ bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage, int mul
     return Create();
     return Create();
 }
 }
 
 
+SharedPtr<Image> TextureCube::GetImage(CubeMapFace face) const
+{
+    if (format_ != Graphics::GetRGBAFormat() && format_ != Graphics::GetRGBFormat())
+    {
+        URHO3D_LOGERROR("Unsupported texture format, can not convert to Image");
+        return SharedPtr<Image>();
+    }
+
+    Image* rawImage = new Image(context_);
+    if (format_ == Graphics::GetRGBAFormat())
+        rawImage->SetSize(width_, height_, 4);
+    else if (format_ == Graphics::GetRGBFormat())
+        rawImage->SetSize(width_, height_, 3);
+    else
+        assert(0);
+
+    GetData(face, 0, rawImage->GetData());
+    return SharedPtr<Image>(rawImage);
+}
+
 void TextureCube::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
 void TextureCube::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
 {
 {
     Renderer* renderer = GetSubsystem<Renderer>();
     Renderer* renderer = GetSubsystem<Renderer>();

+ 2 - 0
Source/Urho3D/Graphics/TextureCube.h

@@ -67,6 +67,8 @@ public:
 
 
     /// Get data from a face's mip level. The destination buffer must be big enough. Return true if successful.
     /// Get data from a face's mip level. The destination buffer must be big enough. Return true if successful.
     bool GetData(CubeMapFace face, unsigned level, void* dest) const;
     bool GetData(CubeMapFace face, unsigned level, void* dest) const;
+    /// Get image data from a face's zero mip level. Only RGB and RGBA textures are supported.
+    SharedPtr<Image> GetImage(CubeMapFace face) const;
 
 
     /// Return render surface for one face.
     /// Return render surface for one face.
     RenderSurface* GetRenderSurface(CubeMapFace face) const { return renderSurfaces_[face]; }
     RenderSurface* GetRenderSurface(CubeMapFace face) const { return renderSurfaces_[face]; }

+ 2 - 0
Source/Urho3D/LuaScript/pkgs/Graphics/Texture2D.pkg

@@ -10,6 +10,8 @@ class Texture2D : public Texture
     bool SetSize(int width, int height, unsigned format, TextureUsage usage = TEXTURE_STATIC, int multiSample = 1, bool autoResolve = true);
     bool SetSize(int width, int height, unsigned format, TextureUsage usage = TEXTURE_STATIC, int multiSample = 1, bool autoResolve = true);
     bool SetData(Image* image, bool useAlpha = false);
     bool SetData(Image* image, bool useAlpha = false);
 
 
+    Image* GetImage() const;
+
     RenderSurface* GetRenderSurface() const;
     RenderSurface* GetRenderSurface() const;
     
     
     tolua_readonly tolua_property__get_set RenderSurface* renderSurface;
     tolua_readonly tolua_property__get_set RenderSurface* renderSurface;

+ 2 - 0
Source/Urho3D/LuaScript/pkgs/Graphics/TextureCube.pkg

@@ -8,6 +8,8 @@ class TextureCube : public Texture
     bool SetSize(int size, unsigned format, TextureUsage usage = TEXTURE_STATIC, int multiSample = 1);
     bool SetSize(int size, unsigned format, TextureUsage usage = TEXTURE_STATIC, int multiSample = 1);
     bool SetData(CubeMapFace face, Image* image, bool useAlpha = false);
     bool SetData(CubeMapFace face, Image* image, bool useAlpha = false);
 
 
+    Image* GetImage(CubeMapFace face) const;
+
     RenderSurface* GetRenderSurface(CubeMapFace face) const;
     RenderSurface* GetRenderSurface(CubeMapFace face) const;
 };
 };