Separate images from data in bucketer

This commit is contained in:
Henrik Rydgård committed 2025-09-17 10:28:08 -06:00
1 parent d2512817bd
commit 0298536efa
4 files changed
+53 -44

No files matched your search

+40 -25
View File
@@ -71,19 +71,41 @@ void Image::SaveZIM(const char *zim_name, int zim_format) {
fclose(f);
}
void Bucket::AddImage(Image &&img, int id) {
Data dat{};
dat.id = id;
dat.sx = 0;
dat.sy = 0;
dat.ex = (int)img.width();
dat.ey = (int)img.height();
dat.w = dat.ex;
dat.h = dat.ey;
dat.redToWhiteAlpha = false;
images.emplace_back(img);
data.push_back(dat);
}
inline bool CompareByID(const Data &lhs, const Data &rhs) {
return lhs.id < rhs.id; // should be unique
}
inline bool CompareByArea(const Data& lhs, const Data& rhs) {
return lhs.w * lhs.h > rhs.w * rhs.h;
}
std::vector<Data> Bucket::Resolve(int image_width, Image &dest) {
// Place all the little images - whatever they are.
// Uses greedy fill algorithm. Slow but works surprisingly well, CPUs are fast.
ImageU8 masq;
masq.resize(image_width, 1);
dest.resize(image_width, 1);
sort(items.begin(), items.end());
for (int i = 0; i < (int)items.size(); i++) {
std::sort(data.begin(), data.end(), CompareByArea);
for (int i = 0; i < (int)data.size(); i++) {
if ((i + 1) % 2000 == 0) {
printf("Resolving (%i / %i)\n", i, (int)items.size());
printf("Resolving (%i / %i)\n", i, (int)data.size());
}
int idx = (int)items[i].first.width();
int idy = (int)items[i].first.height();
int idx = (int)data[i].w;
int idy = (int)data[i].h;
if (idx > 1 && idy > 1) {
assert(idx <= image_width);
for (int ty = 0; ty < 2047; ty++) {
@@ -93,7 +115,7 @@ std::vector<Data> Bucket::Resolve(int image_width, Image &dest) {
dest.resize(image_width, ty + idy + 16);
}
// Brute force packing.
int sz = (int)items[i].first.width();
int sz = (int)data[i].w;
auto &masq_ty = masq.dat[ty];
auto &masq_idy = masq.dat[ty + idy - 1];
for (int tx = 0; tx < image_width - sz; tx++) {
@@ -108,11 +130,11 @@ std::vector<Data> Bucket::Resolve(int image_width, Image &dest) {
}
masq.set(tx, ty, tx + idx + 1, ty + idy + 1, 255);
items[i].second.sx = tx;
items[i].second.sy = ty;
data[i].sx = tx;
data[i].sy = ty;
items[i].second.ex = tx + idx;
items[i].second.ey = ty + idy;
data[i].ex = tx + idx;
data[i].ey = ty + idy;
// printf("Placed %d at %dx%d-%dx%d\n", items[i].second.id, tx, ty, tx + idx, ty + idy);
goto found;
@@ -131,16 +153,15 @@ std::vector<Data> Bucket::Resolve(int image_width, Image &dest) {
exit(1);
}
// Sort the data back by ID.
std::sort(data.begin(), data.end(), CompareByID);
// Actually copy the image data in place, after doing the layout.
for (int i = 0; i < (int)items.size(); i++) {
dest.copyfrom(items[i].first, items[i].second.sx, items[i].second.sy, items[i].second.redToWhiteAlpha);
for (int i = 0; i < (int)data.size(); i++) {
dest.copyfrom(images[i], data[i].sx, data[i].sy, data[i].redToWhiteAlpha);
}
// Output the glyph data.
std::vector<Data> dats;
for (int i = 0; i < (int)items.size(); i++)
dats.push_back(items[i].second);
return dats;
return data;
}
bool LoadImage(const char *imagefile, Bucket *bucket, int &global_id) {
@@ -157,14 +178,8 @@ bool LoadImage(const char *imagefile, Bucket *bucket, int &global_id) {
}
}
Data dat{};
dat.id = global_id++;
dat.sx = 0;
dat.sy = 0;
dat.ex = (int)img.width();
dat.ey = (int)img.height();
dat.redToWhiteAlpha = false;
bucket->AddItem(std::move(img), dat);
bucket->AddImage(std::move(img), global_id);
global_id++;
return true;
}
+7 -10
View File
@@ -69,6 +69,8 @@ private:
struct Data {
// item ID
int id;
// item width and height
int w, h;
// dimensions of its spot in the world
int sx, sy, ex, ey;
// offset from the origin
@@ -81,22 +83,17 @@ struct Data {
int charNum;
};
inline bool operator<(const Data &lhs, const Data &rhs) {
return lhs.id < rhs.id; // should be unique
}
struct Bucket {
std::vector<std::pair<Image, Data> > items;
std::vector<Image> images;
std::vector<Data> data;
void AddItem(Image &&img, const Data &dat) {
items.emplace_back(std::move(img), dat);
images.emplace_back(std::move(img));
data.emplace_back(dat);
}
void AddImage(Image &&img, int id);
std::vector<Data> Resolve(int image_width, Image &dest);
};
inline bool operator<(const Image &lhs, const Image &rhs) {
return lhs.width() * lhs.height() > rhs.width() * rhs.height();
}
struct ImageDesc {
std::string name;
std::string fileName;
+2 -5
View File
@@ -383,12 +383,12 @@ Draw::Texture *GenerateUIAtlas(Draw::DrawContext *draw, Atlas *atlas) {
} else {
name.append(image.fileName);
}
image.result_index = (int)bucket.items.size();
image.result_index = (int)bucket.data.size();
if (!LoadImage(name.c_str(), &bucket, global_id)) {
ERROR_LOG(Log::G3D, "Failed to load image %s\n", image.fileName.c_str());
}
}
INFO_LOG(Log::G3D, " - Loaded %zu images in %.2f ms\n", bucket.items.size(), start.ElapsedMs());
INFO_LOG(Log::G3D, " - Loaded %zu images in %.2f ms\n", bucket.data.size(), start.ElapsedMs());
int image_width = 512;
Image dest;
@@ -397,9 +397,6 @@ Draw::Texture *GenerateUIAtlas(Draw::DrawContext *draw, Atlas *atlas) {
std::vector<Data> results = bucket.Resolve(image_width, dest);
INFO_LOG(Log::G3D, " - Bucketed %zu images in %.2f ms\n", results.size(), now.ElapsedMs());
// Need to sort the results by ID, after the bucket messed up the order.
sort(results.begin(), results.end());
// Fill out the atlas structure.
std::vector<AtlasImage> genAtlasImages;
genAtlasImages.reserve(ARRAY_SIZE(images));
+4 -4
View File
@@ -277,6 +277,8 @@ void RasterizeFonts(const FontReferenceList &fontRefs, vector<CharRange> &ranges
dat.sy = 0;
dat.ex = (int)img.width();
dat.ey = (int)img.height();
dat.w = dat.ex;
dat.h = dat.ey;
dat.ox = (float)font->glyph->metrics.horiBearingX / 64 / supersample - bord;
dat.oy = -(float)font->glyph->metrics.horiBearingY / 64 / supersample - bord;
dat.voffset = vertOffset;
@@ -661,7 +663,7 @@ int GenerateFromScript(const char *script_file, const char *atlas_name, bool hig
// Script fully read, now read images and rasterize the fonts.
for (auto &image : images) {
image.result_index = (int)bucket.items.size();
image.result_index = (int)bucket.data.size();
if (!LoadImage(image.fileName.c_str(), &bucket, global_id)) {
fprintf(stderr, "Failed to load image %s\n", image.fileName.c_str());
}
@@ -669,7 +671,7 @@ int GenerateFromScript(const char *script_file, const char *atlas_name, bool hig
for (auto it = fontRefs.begin(), end = fontRefs.end(); it != end; ++it) {
FontDesc fnt;
fnt.first_char_id = (int)bucket.items.size();
fnt.first_char_id = (int)bucket.data.size();
vector<CharRange> finalRanges;
float metrics_height;
@@ -704,8 +706,6 @@ int GenerateFromScript(const char *script_file, const char *atlas_name, bool hig
dest.SavePNG((image_name + ".png").c_str());
printf("Done. Outputting source and meta files %s_atlas.cpp/h/meta.\n", out_prefix.c_str());
// Sort items by ID.
sort(results.begin(), results.end());
// Save all the metadata.
{