|
| 1 | +#if defined(_WIN32) || defined(_WIN64) |
| 2 | +#include <BaseTsd.h> |
| 3 | +#define LINE_MAX 4096 |
| 4 | +#define _CRT_SECURE_NO_WARNINGS 1 |
| 5 | +typedef SSIZE_T ssize_t; |
| 6 | +#endif |
| 7 | + |
| 8 | +#include <cstring> |
| 9 | +#include <climits> |
| 10 | +#include <iostream> |
| 11 | +#include <Magick++.h> |
| 12 | + |
| 13 | +#if defined(_WIN32) || defined(_WIN64) |
| 14 | +#define WIN32_LEAN_AND_MEAN |
| 15 | +#include <stdlib.h> |
| 16 | +#endif |
| 17 | + |
| 18 | +/* Used to convert text describing colors into gif palette |
| 19 | + * |
| 20 | + * This specific program is not designed with security in mind (there is no return code checks, out-of-bound checks or |
| 21 | + * whatsoever) and is only used to generate static gif files at build time. |
| 22 | + * |
| 23 | + * There's probably no point installing this program after build phase, * since code changes are required to support |
| 24 | + * new minecraft versions. |
| 25 | + */ |
| 26 | + |
| 27 | +void usage() { |
| 28 | + using namespace std; |
| 29 | + cout << "Usage:" << endl; |
| 30 | + cout << " gen_color_map <input.txt> <output.gif>" << endl; |
| 31 | +#if defined(_WIN32) || defined(_WIN64) |
| 32 | + printf("Executable built at %s %s", __DATE__, __TIME__); |
| 33 | +#endif |
| 34 | +} |
| 35 | + |
| 36 | +void run_colors(ssize_t& num_colors, FILE* input_file, unsigned char *colors) { |
| 37 | + bool dry_run = false; |
| 38 | + if (num_colors == 0 || colors == nullptr) { |
| 39 | + // Dry run mode |
| 40 | + dry_run = true; |
| 41 | + num_colors = 0; |
| 42 | + } |
| 43 | + auto *line_buffer = new char[LINE_MAX]; |
| 44 | + |
| 45 | + int i = 0; |
| 46 | + while(fgets(line_buffer, LINE_MAX, input_file) != nullptr) { |
| 47 | + if (line_buffer[0] == '\n' || line_buffer[0] == '#') { |
| 48 | + continue; |
| 49 | + } |
| 50 | + if (dry_run) { |
| 51 | + num_colors++; |
| 52 | + } else { |
| 53 | + unsigned char r, g, b, a; |
| 54 | + sscanf(line_buffer, "{%hhd, %hhd, %hhd, %hhd},", &r, &g, &b, &a); |
| 55 | + colors[i * 4] = r; |
| 56 | + colors[i * 4 + 1] = g; |
| 57 | + colors[i * 4 + 2] = b; |
| 58 | + colors[i * 4 + 3] = a; |
| 59 | + i++; |
| 60 | + } |
| 61 | + //printf("Index %i:\t%i\t%i\t%i\t%i\n", i , r, g, b, a); |
| 62 | + } |
| 63 | + rewind(input_file); |
| 64 | + |
| 65 | + delete line_buffer; |
| 66 | +} |
| 67 | + |
| 68 | +int main(int argc, char **argv) { |
| 69 | +#if defined(_WIN32) || defined(_WIN64) |
| 70 | + _wputenv_s(L"MAGICK_CODER_MODULE_PATH", WIN_MAGICK_CODER_MODULE_PATH); |
| 71 | +#endif |
| 72 | + if (argc != 3) { |
| 73 | + usage(); |
| 74 | + return 1; |
| 75 | + } |
| 76 | + std::string input_filename = argv[1]; |
| 77 | + std::string output_filename = argv[2]; |
| 78 | + |
| 79 | + FILE *input_file = fopen(input_filename.c_str(), "r"); |
| 80 | + if (input_file == NULL) { |
| 81 | + fprintf(stderr, "%s\n", "Fail to open file"); |
| 82 | + return 1; |
| 83 | + } |
| 84 | + // Determine number of lines in input |
| 85 | + ssize_t num_colors = 0; |
| 86 | + run_colors(num_colors, input_file, nullptr); |
| 87 | +#if defined(_WIN32) || defined(_WIN64) |
| 88 | + printf("Number of colors in %s: %lli\n", input_filename.c_str(), num_colors); |
| 89 | +#else |
| 90 | + printf("Number of colors in %s: %li\n", input_filename.c_str(), num_colors); |
| 91 | +#endif |
| 92 | + |
| 93 | + // Parse to array of colors |
| 94 | + auto *colors = new unsigned char[sizeof(short) * 4 * num_colors]; |
| 95 | + run_colors(num_colors, input_file, colors); |
| 96 | + fclose(input_file); |
| 97 | + |
| 98 | + // Generate palette image |
| 99 | + { |
| 100 | + Magick::InitializeMagick(*argv); |
| 101 | + ssize_t height = num_colors / 4; |
| 102 | + Magick::Image palette_img = Magick::Image(4, height, "RGBA", MagickCore::CharPixel, colors); |
| 103 | + try { |
| 104 | + Magick::Blob output_buf; |
| 105 | + palette_img.write(&output_buf, "GIF"); |
| 106 | + FILE* output_fd = fopen(output_filename.c_str(), "wb"); |
| 107 | + fwrite(output_buf.data(), output_buf.length(), 1, output_fd); |
| 108 | + fclose(output_fd); |
| 109 | + } |
| 110 | + catch (Magick::Exception &e) { |
| 111 | + std::cerr << "Error when writing palette image: " << e.what() << std::endl; |
| 112 | + exit(1); |
| 113 | + } |
| 114 | + } |
| 115 | + return 0; |
| 116 | +} |
0 commit comments