Added class AbstractImage, inherited by Image and Texture

Former-commit-id: 16e5db4937a145ac244d6c70e4c74662622f86d9
This commit is contained in:
Lynix 2015-01-05 15:08:29 +01:00
parent 304b4d13cc
commit e3f7644831
7 changed files with 178 additions and 82 deletions

View File

@ -12,9 +12,9 @@
#include <Nazara/Core/Resource.hpp> #include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceRef.hpp> #include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Renderer/Enums.hpp> #include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Utility/AbstractImage.hpp>
#include <Nazara/Utility/CubemapParams.hpp> #include <Nazara/Utility/CubemapParams.hpp>
#include <Nazara/Utility/Image.hpp> #include <Nazara/Utility/Image.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
class NzTexture; class NzTexture;
@ -23,7 +23,7 @@ using NzTextureRef = NzResourceRef<NzTexture>;
struct NzTextureImpl; struct NzTextureImpl;
class NAZARA_API NzTexture : public NzResource, NzNonCopyable class NAZARA_API NzTexture : public NzAbstractImage, public NzResource, NzNonCopyable
{ {
friend class NzRenderer; friend class NzRenderer;
friend class NzRenderTexture; friend class NzRenderTexture;
@ -42,18 +42,19 @@ class NAZARA_API NzTexture : public NzResource, NzNonCopyable
void EnsureMipmapsUpdate() const; void EnsureMipmapsUpdate() const;
nzUInt8 GetBytesPerPixel() const;
unsigned int GetDepth(nzUInt8 level = 0) const; unsigned int GetDepth(nzUInt8 level = 0) const;
nzPixelFormat GetFormat() const; nzPixelFormat GetFormat() const;
unsigned int GetHeight(nzUInt8 level = 0) const; unsigned int GetHeight(nzUInt8 level = 0) const;
nzUInt8 GetLevelCount() const;
nzUInt8 GetMaxLevel() const;
unsigned int GetMemoryUsage() const;
unsigned int GetMemoryUsage(nzUInt8 level) const;
NzVector3ui GetSize(nzUInt8 level = 0) const; NzVector3ui GetSize(nzUInt8 level = 0) const;
nzImageType GetType() const; nzImageType GetType() const;
unsigned int GetWidth(nzUInt8 level = 0) const; unsigned int GetWidth(nzUInt8 level = 0) const;
bool HasMipmaps() const; bool HasMipmaps() const;
bool IsCompressed() const;
bool IsCubemap() const;
bool IsValid() const; bool IsValid() const;
// Load // Load

View File

@ -0,0 +1,42 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_ABSTRACTIMAGE_HPP
#define NAZARA_ABSTRACTIMAGE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Math/Box.hpp>
#include <Nazara/Math/Rect.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Utility/Enums.hpp>
class NAZARA_API NzAbstractImage
{
public:
NzAbstractImage() = default;
~NzAbstractImage();
nzUInt8 GetBytesPerPixel() const;
virtual unsigned int GetDepth(nzUInt8 level = 0) const = 0;
virtual nzPixelFormat GetFormat() const = 0;
virtual unsigned int GetHeight(nzUInt8 level = 0) const = 0;
virtual nzUInt8 GetLevelCount() const = 0;
virtual nzUInt8 GetMaxLevel() const = 0;
virtual unsigned int GetMemoryUsage() const = 0;
virtual unsigned int GetMemoryUsage(nzUInt8 level) const = 0;
virtual NzVector3ui GetSize(nzUInt8 level = 0) const = 0;
virtual nzImageType GetType() const = 0;
virtual unsigned int GetWidth(nzUInt8 level = 0) const = 0;
bool IsCompressed() const;
bool IsCubemap() const;
virtual bool Update(const nzUInt8* pixels, unsigned int srcWidth = 0, unsigned int srcHeight = 0, nzUInt8 level = 0) = 0;
virtual bool Update(const nzUInt8* pixels, const NzBoxui& box, unsigned int srcWidth = 0, unsigned int srcHeight = 0, nzUInt8 level = 0) = 0;
virtual bool Update(const nzUInt8* pixels, const NzRectui& rect, unsigned int z = 0, unsigned int srcWidth = 0, unsigned int srcHeight = 0, nzUInt8 level = 0) = 0;
};
#endif // NAZARA_IMAGE_HPP

View File

@ -13,12 +13,8 @@
#include <Nazara/Core/Resource.hpp> #include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp> #include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceRef.hpp> #include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Math/Box.hpp> #include <Nazara/Utility/AbstractImage.hpp>
#include <Nazara/Math/Rect.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Utility/CubemapParams.hpp> #include <Nazara/Utility/CubemapParams.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <atomic> #include <atomic>
///TODO: Filtres ///TODO: Filtres
@ -40,7 +36,7 @@ using NzImageConstRef = NzResourceRef<const NzImage>;
using NzImageLoader = NzResourceLoader<NzImage, NzImageParams>; using NzImageLoader = NzResourceLoader<NzImage, NzImageParams>;
using NzImageRef = NzResourceRef<NzImage>; using NzImageRef = NzResourceRef<NzImage>;
class NAZARA_API NzImage : public NzResource class NAZARA_API NzImage : public NzAbstractImage, public NzResource
{ {
friend NzImageLoader; friend NzImageLoader;
@ -68,7 +64,6 @@ class NAZARA_API NzImage : public NzResource
bool FlipHorizontally(); bool FlipHorizontally();
bool FlipVertically(); bool FlipVertically();
nzUInt8 GetBytesPerPixel() const;
const nzUInt8* GetConstPixels(unsigned int x = 0, unsigned int y = 0, unsigned int z = 0, nzUInt8 level = 0) const; const nzUInt8* GetConstPixels(unsigned int x = 0, unsigned int y = 0, unsigned int z = 0, nzUInt8 level = 0) const;
unsigned int GetDepth(nzUInt8 level = 0) const; unsigned int GetDepth(nzUInt8 level = 0) const;
nzPixelFormat GetFormat() const; nzPixelFormat GetFormat() const;
@ -83,8 +78,6 @@ class NAZARA_API NzImage : public NzResource
nzImageType GetType() const; nzImageType GetType() const;
unsigned int GetWidth(nzUInt8 level = 0) const; unsigned int GetWidth(nzUInt8 level = 0) const;
bool IsCompressed() const;
bool IsCubemap() const;
bool IsValid() const; bool IsValid() const;
// Load // Load
@ -107,9 +100,9 @@ class NAZARA_API NzImage : public NzResource
void SetLevelCount(nzUInt8 levelCount); void SetLevelCount(nzUInt8 levelCount);
bool SetPixelColor(const NzColor& color, unsigned int x, unsigned int y = 0, unsigned int z = 0); bool SetPixelColor(const NzColor& color, unsigned int x, unsigned int y = 0, unsigned int z = 0);
void Update(const nzUInt8* pixels, unsigned int srcWidth = 0, unsigned int srcHeight = 0, nzUInt8 level = 0); bool Update(const nzUInt8* pixels, unsigned int srcWidth = 0, unsigned int srcHeight = 0, nzUInt8 level = 0);
void Update(const nzUInt8* pixels, const NzBoxui& box, unsigned int srcWidth = 0, unsigned int srcHeight = 0, nzUInt8 level = 0); bool Update(const nzUInt8* pixels, const NzBoxui& box, unsigned int srcWidth = 0, unsigned int srcHeight = 0, nzUInt8 level = 0);
void Update(const nzUInt8* pixels, const NzRectui& rect, unsigned int z = 0, unsigned int srcWidth = 0, unsigned int srcHeight = 0, nzUInt8 level = 0); bool Update(const nzUInt8* pixels, const NzRectui& rect, unsigned int z = 0, unsigned int srcWidth = 0, unsigned int srcHeight = 0, nzUInt8 level = 0);
NzImage& operator=(const NzImage& image); NzImage& operator=(const NzImage& image);
NzImage& operator=(NzImage&& image) noexcept; NzImage& operator=(NzImage&& image) noexcept;

View File

@ -9,6 +9,7 @@
#include <Nazara/Renderer/OpenGL.hpp> #include <Nazara/Renderer/OpenGL.hpp>
#include <Nazara/Renderer/Renderer.hpp> #include <Nazara/Renderer/Renderer.hpp>
#include <Nazara/Renderer/RenderBuffer.hpp> #include <Nazara/Renderer/RenderBuffer.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <limits> #include <limits>
#include <memory> #include <memory>
#include <vector> #include <vector>

View File

@ -8,6 +8,7 @@
#include <Nazara/Renderer/Context.hpp> #include <Nazara/Renderer/Context.hpp>
#include <Nazara/Renderer/Renderer.hpp> #include <Nazara/Renderer/Renderer.hpp>
#include <Nazara/Renderer/OpenGL.hpp> #include <Nazara/Renderer/OpenGL.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include <Nazara/Renderer/Debug.hpp> #include <Nazara/Renderer/Debug.hpp>
@ -311,19 +312,6 @@ void NzTexture::EnsureMipmapsUpdate() const
} }
} }
nzUInt8 NzTexture::GetBytesPerPixel() const
{
#if NAZARA_RENDERER_SAFE
if (!m_impl)
{
NazaraError("Texture must be valid");
return 0;
}
#endif
return NzPixelFormat::GetBytesPerPixel(m_impl->format);
}
unsigned int NzTexture::GetDepth(nzUInt8 level) const unsigned int NzTexture::GetDepth(nzUInt8 level) const
{ {
#if NAZARA_RENDERER_SAFE #if NAZARA_RENDERER_SAFE
@ -363,6 +351,89 @@ unsigned int NzTexture::GetHeight(nzUInt8 level) const
return GetLevelSize(m_impl->height, level); return GetLevelSize(m_impl->height, level);
} }
nzUInt8 NzTexture::GetLevelCount() const
{
#if NAZARA_RENDERER_SAFE
if (!m_impl)
{
NazaraError("Texture must be valid");
return 0;
}
#endif
return m_impl->levelCount;
}
nzUInt8 NzTexture::GetMaxLevel() const
{
#if NAZARA_RENDERER_SAFE
if (!m_impl)
{
NazaraError("Texture must be valid");
return 0;
}
#endif
return NzImage::GetMaxLevel(m_impl->type, m_impl->width, m_impl->height, m_impl->depth);
}
unsigned int NzTexture::GetMemoryUsage() const
{
#if NAZARA_RENDERER_SAFE
if (!m_impl)
{
NazaraError("Texture must be valid");
return 0;
}
#endif
unsigned int width = m_impl->width;
unsigned int height = m_impl->height;
unsigned int depth = m_impl->depth;
unsigned int size = 0;
for (unsigned int i = 0; i < m_impl->levelCount; ++i)
{
size += width * height * depth;
if (width > 1)
width >>= 1;
if (height > 1)
height >>= 1;
if (depth > 1)
depth >>= 1;
}
if (m_impl->type == nzImageType_Cubemap)
size *= 6;
return size * NzPixelFormat::GetBytesPerPixel(m_impl->format);
}
unsigned int NzTexture::GetMemoryUsage(nzUInt8 level) const
{
#if NAZARA_UTILITY_SAFE
if (!m_impl)
{
NazaraError("Texture must be valid");
return 0;
}
if (level >= m_impl->levelCount)
{
NazaraError("Level out of bounds (" + NzString::Number(level) + " >= " + NzString::Number(m_impl->levelCount) + ')');
return 0;
}
#endif
return (GetLevelSize(m_impl->width, level)) *
(GetLevelSize(m_impl->height, level)) *
((m_impl->type == nzImageType_Cubemap) ? 6 : GetLevelSize(m_impl->depth, level)) *
NzPixelFormat::GetBytesPerPixel(m_impl->format);
}
NzVector3ui NzTexture::GetSize(nzUInt8 level) const NzVector3ui NzTexture::GetSize(nzUInt8 level) const
{ {
#if NAZARA_RENDERER_SAFE #if NAZARA_RENDERER_SAFE
@ -415,32 +486,6 @@ bool NzTexture::HasMipmaps() const
return m_impl->levelCount > 1; return m_impl->levelCount > 1;
} }
bool NzTexture::IsCompressed() const
{
#if NAZARA_RENDERER_SAFE
if (!m_impl)
{
NazaraError("Texture must be valid");
return false;
}
#endif
return NzPixelFormat::IsCompressed(m_impl->format);
}
bool NzTexture::IsCubemap() const
{
#if NAZARA_RENDERER_SAFE
if (!m_impl)
{
NazaraError("Texture must be valid");
return false;
}
#endif
return m_impl->type == nzImageType_Cubemap;
}
bool NzTexture::IsValid() const bool NzTexture::IsValid() const
{ {
return m_impl != nullptr; return m_impl != nullptr;

View File

@ -0,0 +1,24 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/AbstractImage.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <Nazara/Utility/Debug.hpp>
NzAbstractImage::~NzAbstractImage() = default;
nzUInt8 NzAbstractImage::GetBytesPerPixel() const
{
return NzPixelFormat::GetBytesPerPixel(GetFormat());
}
bool NzAbstractImage::IsCompressed() const
{
return NzPixelFormat::IsCompressed(GetFormat());
}
bool NzAbstractImage::IsCubemap() const
{
return GetType() == nzImageType_Cubemap;
}

View File

@ -5,6 +5,7 @@
#include <Nazara/Utility/Image.hpp> #include <Nazara/Utility/Image.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Config.hpp> #include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <cmath> #include <cmath>
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
@ -590,11 +591,6 @@ bool NzImage::FlipVertically()
return true; return true;
} }
nzUInt8 NzImage::GetBytesPerPixel() const
{
return NzPixelFormat::GetBytesPerPixel(m_sharedImage->format);
}
const nzUInt8* NzImage::GetConstPixels(unsigned int x, unsigned int y, unsigned int z, nzUInt8 level) const const nzUInt8* NzImage::GetConstPixels(unsigned int x, unsigned int y, unsigned int z, nzUInt8 level) const
{ {
#if NAZARA_UTILITY_SAFE #if NAZARA_UTILITY_SAFE
@ -850,16 +846,6 @@ unsigned int NzImage::GetWidth(nzUInt8 level) const
return GetLevelSize(m_sharedImage->width, level); return GetLevelSize(m_sharedImage->width, level);
} }
bool NzImage::IsCompressed() const
{
return NzPixelFormat::IsCompressed(m_sharedImage->format);
}
bool NzImage::IsCubemap() const
{
return m_sharedImage->type == nzImageType_Cubemap;
}
bool NzImage::IsValid() const bool NzImage::IsValid() const
{ {
return m_sharedImage != &emptyImage; return m_sharedImage != &emptyImage;
@ -1199,25 +1185,25 @@ bool NzImage::SetPixelColor(const NzColor& color, unsigned int x, unsigned int y
return true; return true;
} }
void NzImage::Update(const nzUInt8* pixels, unsigned int srcWidth, unsigned int srcHeight, nzUInt8 level) bool NzImage::Update(const nzUInt8* pixels, unsigned int srcWidth, unsigned int srcHeight, nzUInt8 level)
{ {
#if NAZARA_UTILITY_SAFE #if NAZARA_UTILITY_SAFE
if (m_sharedImage == &emptyImage) if (m_sharedImage == &emptyImage)
{ {
NazaraError("Image must be valid"); NazaraError("Image must be valid");
return; return false;
} }
if (!pixels) if (!pixels)
{ {
NazaraError("Invalid pixel source"); NazaraError("Invalid pixel source");
return; return false;
} }
if (level >= m_sharedImage->levelCount) if (level >= m_sharedImage->levelCount)
{ {
NazaraError("Level out of bounds (" + NzString::Number(level) + " >= " + NzString::Number(m_sharedImage->levelCount) + ')'); NazaraError("Level out of bounds (" + NzString::Number(level) + " >= " + NzString::Number(m_sharedImage->levelCount) + ')');
return; return false;
} }
#endif #endif
@ -1229,27 +1215,29 @@ void NzImage::Update(const nzUInt8* pixels, unsigned int srcWidth, unsigned int
GetLevelSize(m_sharedImage->depth, level), GetLevelSize(m_sharedImage->depth, level),
0, 0, 0, 0,
srcWidth, srcHeight); srcWidth, srcHeight);
return true;
} }
void NzImage::Update(const nzUInt8* pixels, const NzBoxui& box, unsigned int srcWidth, unsigned int srcHeight, nzUInt8 level) bool NzImage::Update(const nzUInt8* pixels, const NzBoxui& box, unsigned int srcWidth, unsigned int srcHeight, nzUInt8 level)
{ {
#if NAZARA_UTILITY_SAFE #if NAZARA_UTILITY_SAFE
if (m_sharedImage == &emptyImage) if (m_sharedImage == &emptyImage)
{ {
NazaraError("Image must be valid"); NazaraError("Image must be valid");
return; return false;
} }
if (!pixels) if (!pixels)
{ {
NazaraError("Invalid pixel source"); NazaraError("Invalid pixel source");
return; return false;
} }
if (level >= m_sharedImage->levelCount) if (level >= m_sharedImage->levelCount)
{ {
NazaraError("Level out of bounds (" + NzString::Number(level) + " >= " + NzString::Number(m_sharedImage->levelCount) + ')'); NazaraError("Level out of bounds (" + NzString::Number(level) + " >= " + NzString::Number(m_sharedImage->levelCount) + ')');
return; return false;
} }
#endif #endif
@ -1260,7 +1248,7 @@ void NzImage::Update(const nzUInt8* pixels, const NzBoxui& box, unsigned int src
if (!box.IsValid()) if (!box.IsValid())
{ {
NazaraError("Invalid box"); NazaraError("Invalid box");
return; return false;
} }
unsigned int depth = (m_sharedImage->type == nzImageType_Cubemap) ? 6 : GetLevelSize(m_sharedImage->depth, level); unsigned int depth = (m_sharedImage->type == nzImageType_Cubemap) ? 6 : GetLevelSize(m_sharedImage->depth, level);
@ -1268,7 +1256,7 @@ void NzImage::Update(const nzUInt8* pixels, const NzBoxui& box, unsigned int src
(m_sharedImage->type == nzImageType_Cubemap && box.depth > 1)) // Nous n'autorisons pas de modifier plus d'une face du cubemap à la fois (m_sharedImage->type == nzImageType_Cubemap && box.depth > 1)) // Nous n'autorisons pas de modifier plus d'une face du cubemap à la fois
{ {
NazaraError("Box dimensions are out of bounds"); NazaraError("Box dimensions are out of bounds");
return; return false;
} }
#endif #endif
@ -1281,9 +1269,11 @@ void NzImage::Update(const nzUInt8* pixels, const NzBoxui& box, unsigned int src
box.width, box.height, box.depth, box.width, box.height, box.depth,
width, height, width, height,
srcWidth, srcHeight); srcWidth, srcHeight);
return true;
} }
void NzImage::Update(const nzUInt8* pixels, const NzRectui& rect, unsigned int z, unsigned int srcWidth, unsigned int srcHeight, nzUInt8 level) bool NzImage::Update(const nzUInt8* pixels, const NzRectui& rect, unsigned int z, unsigned int srcWidth, unsigned int srcHeight, nzUInt8 level)
{ {
return Update(pixels, NzBoxui(rect.x, rect.y, z, rect.width, rect.height, 1), srcWidth, srcHeight, level); return Update(pixels, NzBoxui(rect.x, rect.y, z, rect.width, rect.height, 1), srcWidth, srcHeight, level);
} }