From 30b6f4fd1aa0d019295187f4afbb8d66b3318f29 Mon Sep 17 00:00:00 2001 From: Chlumsky Date: Sat, 31 May 2025 13:17:46 +0200 Subject: [PATCH] Image format information #128, warning fix #129 --- README.md | 6 +++++- msdf-atlas-gen/DynamicAtlas.hpp | 14 +++----------- msdf-atlas-gen/GridAtlasPacker.cpp | 23 +++++------------------ msdf-atlas-gen/bitmap-blit.h | 1 - msdf-atlas-gen/main.cpp | 14 +++++++------- msdf-atlas-gen/utils.hpp | 22 ++++++++++++++++++++++ msdfgen | 2 +- 7 files changed, 43 insertions(+), 39 deletions(-) create mode 100644 msdf-atlas-gen/utils.hpp diff --git a/README.md b/README.md index 80c0fb3..ca6b8c7 100644 --- a/README.md +++ b/README.md @@ -79,13 +79,17 @@ If no character set or glyph set is provided, and `-allglyphs` is not used, the - `png` – a compressed PNG image - `bmp` – an uncompressed BMP image - `tiff` – an uncompressed floating-point TIFF image +- `rgba` – an uncompressed [RGBA](https://github.com/bzotto/rgba_bitmap) file +- `fl32` – an uncompressed floating-point FL32 file - `text` – a sequence of pixel values in plain text - `textfloat` – a sequence of floating-point pixel values in plain text - `bin` – a sequence of pixel values encoded as raw bytes of data -- `binfloat` – a sequence of pixel values encoded as raw 32-bit floating-point values +- `binfloat` – a sequence of pixel values encoded as raw 32-bit floating-point values (little endian, `binfloatbe` for big endian) If format is not specified, it may be deduced from the extension of the `-imageout` argument or other clues. +Please note that all color values must be interpreted as if they were linear (not sRGB) like the alpha channel, even if the image format implies otherwise. + ### Atlas dimensions `-dimensions ` – sets fixed atlas dimensions diff --git a/msdf-atlas-gen/DynamicAtlas.hpp b/msdf-atlas-gen/DynamicAtlas.hpp index 4c3ed3c..c555caa 100644 --- a/msdf-atlas-gen/DynamicAtlas.hpp +++ b/msdf-atlas-gen/DynamicAtlas.hpp @@ -1,24 +1,16 @@ #include "DynamicAtlas.h" -namespace msdf_atlas { +#include "utils.hpp" -static int ceilPOT(int x) { - if (x > 0) { - int y = 1; - while (y < x) - y <<= 1; - return y; - } - return 0; -} +namespace msdf_atlas { template DynamicAtlas::DynamicAtlas() : side(0), spacing(0), glyphCount(0), totalArea(0) { } template template -DynamicAtlas::DynamicAtlas(int minSide, ARGS... args) : side(ceilPOT(minSide)), spacing(0), glyphCount(0), totalArea(0), packer(side+spacing, side+spacing), generator(side, side, args...) { } +DynamicAtlas::DynamicAtlas(int minSide, ARGS... args) : side(minSide > 0 ? ceilToPOT(minSide) : 0), spacing(0), glyphCount(0), totalArea(0), packer(side+spacing, side+spacing), generator(side, side, args...) { } template DynamicAtlas::DynamicAtlas(AtlasGenerator &&generator) : side(0), spacing(0), glyphCount(0), totalArea(0), generator((AtlasGenerator &&) generator) { } diff --git a/msdf-atlas-gen/GridAtlasPacker.cpp b/msdf-atlas-gen/GridAtlasPacker.cpp index 35d279e..a783c5d 100644 --- a/msdf-atlas-gen/GridAtlasPacker.cpp +++ b/msdf-atlas-gen/GridAtlasPacker.cpp @@ -2,23 +2,10 @@ #include "GridAtlasPacker.h" #include +#include "utils.hpp" namespace msdf_atlas { -static int floorPOT(int x) { - int y = 1; - while (x >= y && y<<1) - y <<= 1; - return y>>1; -} - -static int ceilPOT(int x) { - int y = 1; - while (x > y && y<<1) - y <<= 1; - return y; -} - static bool squareConstraint(DimensionsConstraint constraint) { switch (constraint) { case DimensionsConstraint::SQUARE: @@ -51,9 +38,9 @@ void GridAtlasPacker::lowerToConstraint(int &width, int &height, DimensionsConst case DimensionsConstraint::POWER_OF_TWO_RECTANGLE: case DimensionsConstraint::POWER_OF_TWO_SQUARE: if (width > 0) - width = floorPOT(width); + width = floorToPOT(width); if (height > 0) - height = floorPOT(height); + height = floorToPOT(height); break; } } @@ -76,9 +63,9 @@ void GridAtlasPacker::raiseToConstraint(int &width, int &height, DimensionsConst case DimensionsConstraint::POWER_OF_TWO_RECTANGLE: case DimensionsConstraint::POWER_OF_TWO_SQUARE: if (width > 0) - width = ceilPOT(width); + width = ceilToPOT(width); if (height > 0) - height = ceilPOT(height); + height = ceilToPOT(height); break; } } diff --git a/msdf-atlas-gen/bitmap-blit.h b/msdf-atlas-gen/bitmap-blit.h index b186d12..34b889b 100644 --- a/msdf-atlas-gen/bitmap-blit.h +++ b/msdf-atlas-gen/bitmap-blit.h @@ -8,7 +8,6 @@ namespace msdf_atlas { /* * Copies a rectangular section from source bitmap to destination bitmap. - * Width and height are not checked and must not exceed bitmap bounds! */ void blit(const msdfgen::BitmapRef &dst, const msdfgen::BitmapConstRef &src, int dx, int dy, int sx, int sy, int w, int h); diff --git a/msdf-atlas-gen/main.cpp b/msdf-atlas-gen/main.cpp index 297432e..6466a29 100644 --- a/msdf-atlas-gen/main.cpp +++ b/msdf-atlas-gen/main.cpp @@ -451,27 +451,27 @@ int main(int argc, const char *const *argv) { #endif if (ARG_IS("bmp")) config.imageFormat = ImageFormat::BMP; - else if (ARG_IS("tiff")) + else if (ARG_IS("tiff") || ARG_IS("tif")) config.imageFormat = ImageFormat::TIFF; else if (ARG_IS("rgba")) config.imageFormat = ImageFormat::RGBA; else if (ARG_IS("fl32")) config.imageFormat = ImageFormat::FL32; - else if (ARG_IS("text")) + else if (ARG_IS("text") || ARG_IS("txt")) config.imageFormat = ImageFormat::TEXT; - else if (ARG_IS("textfloat")) + else if (ARG_IS("textfloat") || ARG_IS("txtfloat")) config.imageFormat = ImageFormat::TEXT_FLOAT; - else if (ARG_IS("bin")) + else if (ARG_IS("bin") || ARG_IS("binary")) config.imageFormat = ImageFormat::BINARY; - else if (ARG_IS("binfloat")) + else if (ARG_IS("binfloat") || ARG_IS("binfloatle")) config.imageFormat = ImageFormat::BINARY_FLOAT; else if (ARG_IS("binfloatbe")) config.imageFormat = ImageFormat::BINARY_FLOAT_BE; else { #ifndef MSDFGEN_DISABLE_PNG - ABORT("Invalid image format. Valid formats are: png, bmp, tiff, rgba, fl32, text, textfloat, bin, binfloat"); + ABORT("Invalid image format. Valid formats are: png, bmp, tiff, rgba, fl32, text, textfloat, bin, binfloat, binfloatbe"); #else - ABORT("Invalid image format. Valid formats are: bmp, tiff, rgba, fl32, text, textfloat, bin, binfloat"); + ABORT("Invalid image format. Valid formats are: bmp, tiff, rgba, fl32, text, textfloat, bin, binfloat, binfloatbe"); #endif } imageFormatName = arg; diff --git a/msdf-atlas-gen/utils.hpp b/msdf-atlas-gen/utils.hpp new file mode 100644 index 0000000..b0f5bbf --- /dev/null +++ b/msdf-atlas-gen/utils.hpp @@ -0,0 +1,22 @@ + +#pragma once + +namespace msdf_atlas { + +/// Floor positive integer to nearest lower or equal power of two +inline int floorToPOT(int x) { + int y = 1; + while (x >= y && y<<1) + y <<= 1; + return y>>1; +} + +/// Ceil positive integer to nearest higher or equal power of two +inline int ceilToPOT(int x) { + int y = 1; + while (x > y && y<<1) + y <<= 1; + return y; +} + +} diff --git a/msdfgen b/msdfgen index 5a88b0c..0388956 160000 --- a/msdfgen +++ b/msdfgen @@ -1 +1 @@ -Subproject commit 5a88b0c2b95033f7eee826b27c48d399d544d814 +Subproject commit 03889564a50452fa2e0b0a60973b5057001b391b