Skip to content

Commit

Permalink
Refactor Material, Texture, Color && ColorFormat
Browse files Browse the repository at this point in the history
- Removed type alias Mipmap. class Texture now represents Mipmap pyramid image.
  Added new functions to Texture class: 
    - convert / converted: converts texture to a new ColorFormat
    - clearMipmaps: removes mipmap chain to LOD 0
    - generateMipmap / makeMipmap: generates new mipmap chain
    - mipmap: returns texture at mipmap level number
    - scale / scaled: scales texture using box filtering algorithm (pixel averaging)
- Added class TextureView
- Refactored class Material which is represented now as a container of celluloid textures.
- Refactored ColorFormat and renamed/added new ColorFormat global constants: 
    RGB555, RGB555be
    RGB565, RGB565be
    RGBA4444, RGBA4444be, ARGBA4444, ARGBA4444be
    RGBA5551, RGBA5551be, ARGBA5551, ARGBA5551be
    RGB24, RGB24be,
    RGBA32, RGBA32be, ARGBA32, ARGBA32be
- Refactored class AbstractColor
- Added classes Color and ColorRgb which represent non-linear (gamma space) 8-bit/component RGB and RGBA color.
- Added abstract class AbstractLinearColor which represens linear (space 32-bit/component color
- Refactored classes LinearColor & LinearColorRgb to inherit from AbstractLinearColor class
- Added texture utility functions for saving/loading texture to/from BMP and PNG image file formats.
- Renamed Bitmap & BitmapPtr aliases to  PixData & PixDataPtr
- Added Pixdata utility functions
  • Loading branch information
smlu committed Jul 18, 2020
1 parent 46ee801 commit f4a7276
Show file tree
Hide file tree
Showing 19 changed files with 2,710 additions and 585 deletions.
35 changes: 0 additions & 35 deletions libraries/libim/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ namespace libim {
using byte_t = uint8_t;
using ByteArray = std::vector<byte_t>;

using Bitmap = ByteArray;
using BitmapPtr = std::shared_ptr<Bitmap>;

inline BitmapPtr makeBitmapPtr(std::size_t size) {
return std::make_shared<Bitmap>(size);
}


template <typename T,
typename R = std::enable_if_t<std::is_integral<T>::value,
typename std::make_unsigned<T>::type>>
Expand All @@ -74,33 +66,6 @@ namespace libim {
return bits / BYTE_BIT;
}

inline constexpr uint32_t getBitmapSize(int32_t width, int32_t height, uint32_t bpp)
{
return abs(height * width) * bbs(bpp);
}

inline constexpr uint32_t getRowSize(int32_t width, uint32_t bpp)
{
return abs(width) * bbs(bpp);
}

inline constexpr uint32_t getMipmapPixelDataSize(std::size_t numTextures, int32_t width, int32_t height, uint32_t bpp)
{
uint32_t size = 0;
while(numTextures --> 0)
{
size += getBitmapSize(width, height, bpp);
width = width >> 1;
height = height >> 1;
}
return size;
}

inline constexpr uint32_t rgbMask(uint32_t bitsPerColor, uint32_t colorLeftShift)
{
return ((1 << bitsPerColor) - 1) << colorLeftShift;
}

static std::vector<std::string> splitString(const std::string& string, const std::string& delim)
{
std::vector<std::string> tokens;
Expand Down
151 changes: 0 additions & 151 deletions libraries/libim/content/asset/material/bmp.h

This file was deleted.

48 changes: 33 additions & 15 deletions libraries/libim/content/asset/material/colorformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace libim::content::asset {

struct ColorFormat final
{
ColorMode colorMode;
ColorMode mode;
uint32_t bpp; // Color bit depth per pixel

uint32_t redBPP ;
Expand All @@ -33,23 +33,41 @@ namespace libim::content::asset {
};
static_assert(sizeof(ColorFormat) == 56);


// Predefined color formats
static constexpr ColorFormat RGB_565 { ColorMode::RGB, 16, 5, 6, 5, 11, 5, 0, 3, 2, 3, 0, 0, 0 };
static constexpr ColorFormat RGBA_4444 { ColorMode::RGBA, 16, 4, 4, 4, 12, 8, 4, 4, 4, 4, 4, 0, 4 };
static constexpr ColorFormat ARGB_4444 { ColorMode::RGBA, 16, 4, 4, 4, 8, 4, 0, 4, 4, 4, 4, 12, 4 };
static constexpr ColorFormat RGBA_5551 { ColorMode::RGBA, 16, 5, 5, 5, 11, 6, 1, 3, 3, 3, 1, 0, 7 };
static constexpr ColorFormat ARGB_5551 { ColorMode::RGBA, 16, 5, 5, 5, 10, 5, 0, 3, 3, 3, 1, 15, 7 };
static constexpr ColorFormat RGB_888 { ColorMode::RGB, 24, 8, 8, 8, 16, 8, 0, 0, 0, 0, 0, 0, 0 };
static constexpr ColorFormat BGR_888 { ColorMode::RGB, 24, 8, 8, 8, 0, 8, 16, 0, 0, 0, 0, 0, 0 };
static constexpr ColorFormat RGBA_8888 { ColorMode::RGBA, 32, 8, 8, 8, 24, 16, 8, 0, 0, 0, 8, 0, 0 };
static constexpr ColorFormat ARGB_8888 { ColorMode::RGBA, 32, 8, 8, 8, 16, 8, 0, 0, 0, 0, 8, 24, 0 };
static constexpr ColorFormat ABGR_8888 { ColorMode::RGBA, 32, 8, 8, 8, 0, 8, 16, 0, 0, 0, 8, 24, 0 };

// Helper functions
// Naming is done in little-endian byte order.
// Suffix 'be' names color format in big-endian byte order
static constexpr ColorFormat RGB555 { ColorMode::RGB, 16, 5, 5, 5, 10, 5, 0, 3, 3, 3, 0, 0, 0 };
static constexpr ColorFormat RGB555be { ColorMode::RGB, 16, 5, 5, 5, 0, 5, 10, 3, 3, 3, 0, 0, 0 };

static constexpr ColorFormat RGB565 { ColorMode::RGB, 16, 5, 6, 5, 11, 5, 0, 3, 2, 3, 0, 0, 0 };
static constexpr ColorFormat RGB565be { ColorMode::RGB, 16, 5, 6, 5, 0, 5, 11, 3, 2, 3, 0, 0, 0 };

static constexpr ColorFormat RGBA4444 { ColorMode::RGBA, 16, 4, 4, 4, 12, 8, 4, 4, 4, 4, 4, 0, 4 };
static constexpr ColorFormat RGBA4444be { ColorMode::RGBA, 16, 4, 4, 4, 0, 4, 8, 4, 4, 4, 4, 12, 4 };
static constexpr ColorFormat ARGB4444 { ColorMode::RGBA, 16, 4, 4, 4, 8, 4, 0, 4, 4, 4, 4, 12, 4 };
static constexpr ColorFormat ARGB4444be { ColorMode::RGBA, 16, 4, 4, 4, 4, 8, 12, 4, 4, 4, 4, 0, 4 };

static constexpr ColorFormat RGBA5551 { ColorMode::RGBA, 16, 5, 5, 5, 11, 6, 1, 3, 3, 3, 1, 0, 7 };
static constexpr ColorFormat RGBA5551be { ColorMode::RGBA, 16, 5, 5, 5, 0, 5, 10, 3, 3, 3, 1, 15, 7 };
static constexpr ColorFormat ARGB1555 { ColorMode::RGBA, 16, 5, 5, 5, 10, 5, 0, 3, 3, 3, 1, 15, 7 };
static constexpr ColorFormat ARGB1555be { ColorMode::RGBA, 16, 5, 5, 5, 1, 6, 11, 3, 3, 3, 1, 0, 7 };

// RGB888
static constexpr ColorFormat RGB24 { ColorMode::RGB, 24, 8, 8, 8, 16, 8, 0, 0, 0, 0, 0, 0, 0 };
static constexpr ColorFormat RGB24be { ColorMode::RGB, 24, 8, 8, 8, 0, 8, 16, 0, 0, 0, 0, 0, 0 };

// RGBA8888
static constexpr ColorFormat RGBA32 { ColorMode::RGBA, 32, 8, 8, 8, 24, 16, 8, 0, 0, 0, 8, 0, 0 };
static constexpr ColorFormat RGBA32be { ColorMode::RGBA, 32, 8, 8, 8, 0, 8, 16, 0, 0, 0, 8, 24, 0 };
static constexpr ColorFormat ARGB32 { ColorMode::RGBA, 32, 8, 8, 8, 16, 8, 0, 0, 0, 0, 8, 24, 0 };
static constexpr ColorFormat ARGB32be { ColorMode::RGBA, 32, 8, 8, 8, 8, 16, 24, 0, 0, 0, 8, 0, 0 };

// ColorFormat helper functions
inline bool operator ==(const ColorFormat& lci, const ColorFormat& rci)
{
return lci.colorMode == rci.colorMode &&
lci.bpp == rci.bpp &&
return lci.mode == rci.mode &&
lci.bpp == rci.bpp &&

lci.redBPP == rci.redBPP &&
lci.redShl == rci.redShl &&
Expand Down
Loading

0 comments on commit f4a7276

Please sign in to comment.