Skip to content

Commit

Permalink
simplify a lot libibl's Image class
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelflinger committed Mar 13, 2019
1 parent 13d3ade commit a8a37a5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 61 deletions.
20 changes: 6 additions & 14 deletions libs/ibl/include/ibl/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,10 @@
namespace filament {
namespace ibl {

/**
* \deprecated
* We are phasing out this class in favor of LinearImage. The latter has a stable and well-defined
* pixel format, making it easier to implement image-based algorithms.
*/
class Image {
public:
Image();
Image(std::unique_ptr<uint8_t[]> data, size_t w, size_t h,
size_t bpr, size_t bpp, size_t channels = 3);
Image(size_t w, size_t h, size_t stride = 0);

void reset();

Expand All @@ -51,24 +45,22 @@ class Image {

size_t getBytesPerRow() const { return mBpr; }

size_t getBytesPerPixel() const { return mBpp; }
size_t getBytesPerPixel() const { return sizeof(math::float3); }

void* getData() const { return mData; }

void* getPixelRef(size_t x, size_t y) const;

private:
std::unique_ptr<uint8_t[]> mOwnedData;
void* mData = nullptr;
size_t mBpr = 0;
size_t mWidth = 0;
size_t mHeight = 0;
size_t mBpr = 0;
size_t mBpp = 0;
size_t mChannels = 0;
std::unique_ptr<uint8_t[]> mOwnedData;
void* mData = nullptr;
};

inline void* Image::getPixelRef(size_t x, size_t y) const {
return static_cast<uint8_t*>(mData) + y * mBpr + x * mBpp;
return static_cast<uint8_t*>(mData) + y * getBytesPerRow() + x * getBytesPerPixel();
}

} // namespace ibl
Expand Down
12 changes: 6 additions & 6 deletions libs/ibl/src/CubemapIBL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static double3 UTILS_UNUSED hemisphereUniformSample(double2 u) { // pdf = 1.0 /
/*
*
* Importance sampling Charlie
* ------------------------------------------
* ---------------------------
*
* In order to pick the most significative samples and increase the convergence rate, we chose to
* rely on Charlie's distribution function for the pdf as we do in hemisphereImportanceSampleDggx.
Expand Down Expand Up @@ -167,7 +167,7 @@ static double Visibility(double NoV, double NoL, double a) {
return 0.5 / (GGXV + GGXL);
}

static double UTILS_UNUSED VisibilityAshikhmin(double NoV, double NoL, double a) {
static double UTILS_UNUSED VisibilityAshikhmin(double NoV, double NoL, double /*a*/) {
// Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
return 1 / (4 * (NoL + NoV - NoL * NoV));
}
Expand Down Expand Up @@ -814,7 +814,7 @@ static double DFV_Charlie_Uniform(double NoV, double linearRoughness, size_t num
/*
*
* Importance sampling Charlie
* ------------------------------------------
* ---------------------------
*
* Important samples are chosen to integrate DCharlie() * cos(theta) over the hemisphere.
*
Expand Down Expand Up @@ -884,14 +884,14 @@ static double DFV_Charlie_Uniform(double NoV, double linearRoughness, size_t num
*
* It results that:
*
* 1 4 <v•h>
* 1 4 <v•h>
* Er() = --- ∑ DCharlie(h) V(v, l) ------------ <n•l>
* N h DCharlie(h) <n•h>
*
*
* +---------------------------------------+
* | 4 <v•h> |
* | Er() = --- ∑ V(v, l) --- <n•l> |
* | Er() = --- ∑ V(v, l) ------- <n•l> |
* | N h <n•h> |
* +---------------------------------------+
*
Expand Down Expand Up @@ -947,7 +947,7 @@ void CubemapIBL::DFG(Image& dst, bool multiscatter, bool cloth) {
auto dfvFunction = multiscatter ? DFV_Multiscatter : DFV;
JobSystem& js = CubemapUtils::getJobSystem();
auto job = jobs::parallel_for<char>(js, nullptr, nullptr, uint32_t(dst.getHeight()),
[&dst, dfvFunction, cloth](char* d, size_t c) {
[&dst, dfvFunction, cloth](char const* d, size_t c) {
const size_t width = dst.getWidth();
const size_t height = dst.getHeight();
size_t y0 = size_t(d);
Expand Down
8 changes: 2 additions & 6 deletions libs/ibl/src/CubemapUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,8 @@ Image CubemapUtils::createCubemapImage(size_t dim, bool horizontal) {
std::swap(width, height);
}

size_t bpr = width * sizeof(Cubemap::Texel);
bpr = (bpr + 31) & ~31;
size_t bufSize = bpr * height;
std::unique_ptr<uint8_t[]> data(new uint8_t[bufSize]);
memset(data.get(), 0, bufSize);
Image image(std::move(data), width, height, bpr, sizeof(Cubemap::Texel));
Image image(width, height);
memset(image.getData(), 0, image.getBytesPerRow() * height);
return image;
}

Expand Down
22 changes: 6 additions & 16 deletions libs/ibl/src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,19 @@ namespace ibl {

Image::Image() = default;

Image::Image(std::unique_ptr<uint8_t[]> data,
size_t w, size_t h, size_t bpr, size_t bpp, size_t channels)
: mOwnedData(std::move(data)),
mData(mOwnedData.get()),
mWidth(w),
mHeight(h),
mBpr(bpr),
mBpp(bpp),
mChannels(channels)
{
Image::Image(size_t w, size_t h, size_t stride)
: mBpr((stride ? stride : w) * sizeof(math::float3)),
mWidth(w),
mHeight(h),
mOwnedData(new uint8_t[mBpr * h]),
mData(mOwnedData.get()) {
}

void Image::reset() {
mOwnedData.release();
mWidth = 0;
mHeight = 0;
mBpr = 0;
mBpp = 0;
mChannels = 0;
mData = nullptr;
}

Expand All @@ -50,8 +44,6 @@ void Image::set(Image const& image) {
mWidth = image.mWidth;
mHeight = image.mHeight;
mBpr = image.mBpr;
mBpp = image.mBpp;
mChannels = image.mChannels;
mData = image.mData;
}

Expand All @@ -60,8 +52,6 @@ void Image::subset(Image const& image, size_t x, size_t y, size_t w, size_t h) {
mWidth = w;
mHeight = h;
mBpr = image.mBpr;
mBpp = image.mBpp;
mChannels = image.mChannels;
mData = static_cast<uint8_t*>(image.getPixelRef(x, y));
}

Expand Down
28 changes: 9 additions & 19 deletions tools/cmgen/src/cmgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,9 @@ int main(int argc, char* argv[]) {
}

// Convert from LinearImage to the deprecated Image object which is used throughout cmgen.
std::unique_ptr<uint8_t[]> buf(new uint8_t[
linputImage.getWidth() * linputImage.getHeight() * sizeof(float3)]);
const size_t width = linputImage.getWidth(), height = linputImage.getHeight();
const size_t bpp = sizeof(float) * 3, bpr = bpp * width;
memcpy(buf.get(), linputImage.getPixelRef(), height * bpr);
Image inputImage(std::move(buf), width, height, bpr, bpp);
Image inputImage(width, height);
memcpy(inputImage.getData(), linputImage.getPixelRef(), height * inputImage.getBytesPerRow());

CubemapUtils::clamp(inputImage);

Expand Down Expand Up @@ -782,8 +779,7 @@ void iblMipmapPrefilter(const utils::Path& iname,

if (g_type == OutputType::EQUIRECT) {
size_t dim = dst.getDimensions();
std::unique_ptr<uint8_t[]> buf(new uint8_t[dim * 2 * dim * sizeof(float3)]);
Image image(std::move(buf), dim * 2, dim, dim * 2 * sizeof(float3), sizeof(float3));
Image image(dim * 2, dim);
CubemapUtils::cubemapToEquirectangular(image, dst);
std::string filename = outputDir + ("is_m" + std::to_string(level) + ext);
saveImage(filename, g_format, image, g_compression);
Expand All @@ -792,8 +788,7 @@ void iblMipmapPrefilter(const utils::Path& iname,

if (g_type == OutputType::OCTAHEDRON) {
size_t dim = dst.getDimensions();
std::unique_ptr<uint8_t[]> buf(new uint8_t[dim * dim * sizeof(float3)]);
Image image(std::move(buf), dim, dim, dim * sizeof(float3), sizeof(float3));
Image image(dim, dim);
CubemapUtils::cubemapToOctahedron(image, dst);
std::string filename = outputDir + ("is_m" + std::to_string(level) + ext);
saveImage(filename, g_format, image, g_compression);
Expand Down Expand Up @@ -898,17 +893,15 @@ void iblRoughnessPrefilter(const utils::Path& iname,
}

if (g_type == OutputType::EQUIRECT) {
std::unique_ptr<uint8_t[]> buf(new uint8_t[dim * 2 * dim * sizeof(float3)]);
Image outImage(std::move(buf), dim * 2, dim, dim * 2 * sizeof(float3), sizeof(float3));
Image outImage(dim * 2, dim);
CubemapUtils::cubemapToEquirectangular(outImage, dst);
std::string filename = outputDir + ("m" + std::to_string(level) + ext);
saveImage(filename, g_format, outImage, g_compression);
continue;
}

if (g_type == OutputType::OCTAHEDRON) {
std::unique_ptr<uint8_t[]> buf(new uint8_t[dim * dim * sizeof(float3)]);
Image outImage(std::move(buf), dim, dim, dim * sizeof(float3), sizeof(float3));
Image outImage(dim, dim);
CubemapUtils::cubemapToOctahedron(outImage, dst);
std::string filename = outputDir + ("m" + std::to_string(level) + ext);
saveImage(filename, g_format, outImage, g_compression);
Expand Down Expand Up @@ -1009,8 +1002,7 @@ static bool isIncludeFile(const utils::Path& filename) {
}

void iblLutDfg(const utils::Path& filename, size_t size, bool multiscatter, bool cloth) {
std::unique_ptr<uint8_t[]> buf(new uint8_t[size * size * sizeof(float3)]);
Image image(std::move(buf), size, size, size * sizeof(float3), sizeof(float3));
Image image(size, size);
CubemapIBL::DFG(image, multiscatter, cloth);

utils::Path outputDir(filename.getAbsolutePath().getParent());
Expand Down Expand Up @@ -1089,8 +1081,7 @@ void extractCubemapFaces(const utils::Path& iname, const Cubemap& cm, const util

if (g_type == OutputType::EQUIRECT) {
size_t dim = cm.getDimensions();
std::unique_ptr<uint8_t[]> buf(new uint8_t[dim * 2 * dim * sizeof(float3)]);
Image image(std::move(buf), dim * 2, dim, dim * 2 * sizeof(float3), sizeof(float3));
Image image(dim * 2, dim);
CubemapUtils::cubemapToEquirectangular(image, cm);
std::string filename = outputDir + ("skybox" + ext);
saveImage(filename, g_format, image, g_compression);
Expand All @@ -1099,8 +1090,7 @@ void extractCubemapFaces(const utils::Path& iname, const Cubemap& cm, const util

if (g_type == OutputType::OCTAHEDRON) {
size_t dim = cm.getDimensions();
std::unique_ptr<uint8_t[]> buf(new uint8_t[dim * dim * sizeof(float3)]);
Image image(std::move(buf), dim, dim, dim * sizeof(float3), sizeof(float3));
Image image(dim, dim);
CubemapUtils::cubemapToOctahedron(image, cm);
std::string filename = outputDir + ("skybox" + ext);
saveImage(filename, g_format, image, g_compression);
Expand Down

0 comments on commit a8a37a5

Please sign in to comment.