Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from GlassesNerd/master
Browse files Browse the repository at this point in the history
Rounding for FSB
  • Loading branch information
supsm authored Nov 18, 2021
2 parents a0587bf + c312fc7 commit f8d5370
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 107 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ mcpppp-log.txt
desktop.ini

mcpppp-temp/
x64-Debug/
x64-Debug/
/x64/Debug
/bumpversion.exe
2 changes: 1 addition & 1 deletion Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

//#define GUI

constexpr auto VERSION = "0.5.5"; // MCPPPP version
constexpr auto VERSION = "0.5.6"; // MCPPPP version

#ifdef _WIN32
#define NOMINMAX
Expand Down
160 changes: 56 additions & 104 deletions fsb.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,180 +113,132 @@ class fsb
// convert black to transparent
static void convert(std::vector<uint8_t>& image, const unsigned int& w, const unsigned int& h)
{
for (long long i = 0; i < (w * 4) / 3; i += 4)
for (long long i = 0; i < w; i += 4)
{
for (long long j = 0; j < h / 2; j++)
for (long long j = 0; j < h; j++)
{
// if completely opaque
if (image.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i + 3)) == 255)
if (image.at(static_cast<size_t>(w * h - (j + 1) * w + i + 3)) == 255)
{
double first = image.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i));
double second = image.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i + 1));
double third = image.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i + 2));
double first = image.at(static_cast<size_t>(w * h - (j + 1) * w + i));
double second = image.at(static_cast<size_t>(w * h - (j + 1) * w + i + 1));
double third = image.at(static_cast<size_t>(w * h - (j + 1) * w + i + 2));
rgb2hsv(first, second, third);
const double alpha = third * 51 / 20; // convert 0-100 to 0-255
third = 100;
hsv2rgb(first, second, third);
image.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i)) = static_cast<uint8_t>(first);
image.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i + 1)) = static_cast<uint8_t>(second);
image.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i + 2)) = static_cast<uint8_t>(third);
image.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i + 3)) = static_cast<uint8_t>(alpha);
image.at(static_cast<size_t>(w * h - (j + 1) * w + i)) = static_cast<uint8_t>(first);
image.at(static_cast<size_t>(w * h - (j + 1) * w + i + 1)) = static_cast<uint8_t>(second);
image.at(static_cast<size_t>(w * h - (j + 1) * w + i + 2)) = static_cast<uint8_t>(third);
image.at(static_cast<size_t>(w * h - (j + 1) * w + i + 3)) = static_cast<uint8_t>(alpha);
}
}
}
}

static constexpr void checkError(const unsigned int& i)
{
if (i)
{
out(5) << "FSB: png error: " << lodepng_error_text(i) << std::endl;
}
}

// convert optifine image format (1 image for all 6 sides) into fsb image format (1 image per side)
static void fsbpng(const std::string& path, const std::string& output, const std::filesystem::directory_entry& png)
{
out(1) << "FSB: Converting " + png.path().filename().u8string() << std::endl;
unsigned int w, h, error;
unsigned int w, h;
std::vector<uint8_t> buffer, image, image1, image2, image3, top; // before h/2: bottom (rotate 90 counterclockwise), top (rotate 90 clockwise), south; h/2 to h: west, north, east
// rotation: w*h - w + 1, w*h - 2*w + 1, ..., w*h - h*w + 1, w*h - w + 2, w*h - 2*w + 2, ..., w*h - w + w, w*h - 2*w + w, ...
std::string filename = png.path().filename().u8string();
lodepng::State state;
state.info_raw.colortype = LCT_RGBA;
state.info_raw.bitdepth = 8;
filename.erase(filename.end() - 4, filename.end());
error = lodepng::load_file(buffer, png.path().u8string());
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
error = lodepng::decode(image, w, h, state, buffer);
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
checkError(lodepng::load_file(buffer, png.path().u8string()));
checkError(lodepng::decode(image, w, h, state, buffer));
if (w % 3 != 0 || h % 2 != 0)
{
out(5) << "FSB: Wrong dimensions: " << png.path().u8string() << std::endl;
return;
out(4) << "FSB: Wrong dimensions: " << png.path().u8string() << std::endl << "will be cropped to proper dimensions" << std::endl;
}
image1.reserve(buffer.size() / 6);
image2.reserve(buffer.size() / 6);
image3.reserve(buffer.size() / 6);
for (size_t i = 0; i < (w * 4) * h / 2; i++)
const unsigned int outw = w / 3 * 4;
const unsigned int outh = h / 2;
for (size_t i = 0; i < (w * 4) * outh; i++)
{
if (i % (w * 4) < (w * 4) / 3)
if (i % (w * 4) < outw)
{
image1.push_back(image.at(i));
}
else if (i % (w * 4) < 2 * (w * 4) / 3)
else if (i % (w * 4) < 2 * outw)
{
image2.push_back(image.at(i));
}
else
else if (i % (w * 4) < 3 * outw)
{
image3.push_back(image.at(i));
}
}

convert(image1, w, h);
convert(image2, w, h);
convert(image3, w, h);
convert(image1, outw, outh);
convert(image2, outw, outh);
convert(image3, outw, outh);

top.reserve(image.size() / 6);
for (long long i = 0; i < (w * 4) / 3; i += 4)
for (long long i = 0; i < outw; i += 4)
{
for (long long j = 0; j < h / 2; j++)
for (long long j = 0; j < outh; j++)
{
top.push_back(image2.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i)));
top.push_back(image2.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i + 1)));
top.push_back(image2.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i + 2)));
top.push_back(image2.at(static_cast<size_t>((w * 4) / 3 * h / 2 - (j + 1) * (w * 4) / 3 + i + 3)));
top.push_back(image2.at(static_cast<size_t>(outw * outh - (j + 1) * outw + i)));
top.push_back(image2.at(static_cast<size_t>(outw * outh - (j + 1) * outw + i + 1)));
top.push_back(image2.at(static_cast<size_t>(outw * outh - (j + 1) * outw + i + 2)));
top.push_back(image2.at(static_cast<size_t>(outw * outh - (j + 1) * outw + i + 3)));
}
}
buffer.clear();
std::filesystem::create_directories(std::filesystem::u8path(path + output));
error = lodepng::encode(buffer, image1, w / 3, h / 2, state);
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
error = lodepng::save_file(buffer, path + output + filename + "_bottom.png");
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
checkError(lodepng::encode(buffer, image1, outw / 4, outh, state));
checkError(lodepng::save_file(buffer, path + output + filename + "_bottom.png"));
buffer.clear();
error = lodepng::encode(buffer, top, h / 2, w / 3, state);
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
error = lodepng::save_file(buffer, path + output + filename + "_top.png");
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
checkError(lodepng::encode(buffer, top, outh, outw / 4, state));
checkError(lodepng::save_file(buffer, path + output + filename + "_top.png"));
buffer.clear();
error = lodepng::encode(buffer, image3, w / 3, h / 2, state);
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
error = lodepng::save_file(buffer, path + output + filename + "_south.png");
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
checkError(lodepng::encode(buffer, image3, outw / 4, outh, state));
checkError(lodepng::save_file(buffer, path + output + filename + "_south.png"));
image1.clear();
image2.clear();
image3.clear();
for (size_t i = (w * 4) * h / 2; i < (w * 4) * h; i++)
for (size_t i = (w * 4) * outh; i < (w * 4) * 2 * outh; i++)
{
if (i % (w * 4) < (w * 4) / 3)
if (i % (w * 4) < outw)
{
image1.push_back(image.at(i));
}
else if (i % (w * 4) < 2 * (w * 4) / 3)
else if (i % (w * 4) < 2 * outw)
{
image2.push_back(image.at(i));
}
else
else if(i % (w * 4) < 3 * outw)
{
image3.push_back(image.at(i));
}
}

convert(image1, w, h);
convert(image2, w, h);
convert(image3, w, h);

convert(image1, outw, outh);
convert(image2, outw, outh);
convert(image3, outw, outh);
buffer.clear();
error = lodepng::encode(buffer, image1, w / 3, h / 2, state);
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
error = lodepng::save_file(buffer, path + output + filename + "_west.png");
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
checkError(lodepng::encode(buffer, image1, outw / 4, outh, state));
checkError(lodepng::save_file(buffer, path + output + filename + "_west.png"));
buffer.clear();
error = lodepng::encode(buffer, image2, w / 3, h / 2, state);
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
error = lodepng::save_file(buffer, path + output + filename + "_north.png");
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
checkError(lodepng::encode(buffer, image2, outw / 4, outh, state));
checkError(lodepng::save_file(buffer, path + output + filename + "_north.png"));
buffer.clear();
error = lodepng::encode(buffer, image3, w / 3, h / 2, state);
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
error = lodepng::save_file(buffer, path + output + filename + "_east.png");
if (error != 0)
{
out(5) << "FSB: png error: " << lodepng_error_text(error) << std::endl;
}
checkError(lodepng::encode(buffer, image3, outw / 4, outh, state));
checkError(lodepng::save_file(buffer, path + output + filename + "_east.png"));
}

// convert optifine properties files into fsb properties json
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.6

# Mod Properties
mod_version = 0.5.5
mod_version = 0.5.6
archives_base_name = mcpppp
maven_group = com.example

Expand Down

0 comments on commit f8d5370

Please sign in to comment.