Skip to content

Commit 83253af

Browse files
committed
meta: add a tool to generate palette for use in GIMP
1 parent 4955bb7 commit 83253af

3 files changed

Lines changed: 40 additions & 40 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ option(BUILD_TESTS "Build library test harness" OFF)
3333
option(BUILD_PAMENIM "Build Pamenim" ON)
3434
3535
set(MINEMAP_PALETTE_DIR ${CMAKE_INSTALL_DATADIR}/minemap/palettes CACHE STRING "Directory to place minemap palette GIFs relative to prefix")
36+
set(GIMP_PALETTE_DIR ${CMAKE_INSTALL_DATADIR}/gimp/2.0/palettes CACHE STRING "Directory to place GIMP palettes")
3637
add_compile_definitions(MINEMAP_PALETTE_DIR=\"${CMAKE_INSTALL_PREFIX}/${MINEMAP_PALETTE_DIR}\")
3738
3839
# External

misc/CMakeLists.txt

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,32 @@ target_link_libraries(gen_color_map PUBLIC ${ImageMagick_LIBRARIES})
33
target_include_directories(gen_color_map PUBLIC ${ImageMagick_INCLUDE_DIRS})
44
target_compile_options(gen_color_map PUBLIC ${ImageMagick_CFLAGS_OTHER})
55

6-
file(GLOB PALETTE_FILES
7-
LIST_DIRECTORIES FALSE
8-
RELATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}"
9-
"rgba-*.txt"
10-
)
6+
add_executable(gen_gimp_palette gen_gimp_palette.cpp)
7+
target_link_libraries(gen_gimp_palette PUBLIC ${ImageMagick_LIBRARIES})
8+
target_include_directories(gen_gimp_palette PUBLIC ${ImageMagick_INCLUDE_DIRS})
9+
target_compile_options(gen_gimp_palette PUBLIC ${ImageMagick_CFLAGS_OTHER})
1110

12-
foreach(PALETTE_FILE IN LISTS PALETTE_FILES)
13-
get_filename_component(PALETTE_FILE_NAME "${PALETTE_FILE}" NAME)
14-
string(REPLACE ".txt" ".gif" PALETTE_FILE_OUTPUT "${PALETTE_FILE_NAME}")
15-
add_custom_target("gen_palettes_${PALETTE_FILE_NAME}" ALL
11+
set(MC_VERSIONS
12+
1.8
13+
1.12
14+
1.16
15+
1.17
16+
)
17+
18+
foreach(MC_VERSION IN LISTS MC_VERSIONS)
19+
set(PALETTE_FILE_NAME "rgba-${MC_VERSION}.txt" )
20+
set(GIF_FILE_OUTPUT "rgba-${MC_VERSION}.gif")
21+
set(GIMP_FILE_PREFIX "Minecraft${MC_VERSION}")
22+
add_custom_target("gen_gif_palettes_${MC_VERSION}" ALL
1623
DEPENDS gen_color_map
17-
COMMAND $<TARGET_FILE:gen_color_map> "${PALETTE_FILE}" "${PALETTE_FILE_OUTPUT}"
24+
COMMAND $<TARGET_FILE:gen_color_map> "${CMAKE_CURRENT_SOURCE_DIR}/${PALETTE_FILE_NAME}" "${GIF_FILE_OUTPUT}"
1825
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1926
)
20-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PALETTE_FILE_OUTPUT}" DESTINATION ${MINEMAP_PALETTE_DIR})
27+
add_custom_target("gen_gimp_palettes_${MC_VERSION}" ALL
28+
DEPENDS gen_gimp_palette
29+
COMMAND $<TARGET_FILE:gen_gimp_palette> "${CMAKE_CURRENT_SOURCE_DIR}/${PALETTE_FILE_NAME}" "${GIMP_FILE_PREFIX}"
30+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
31+
)
32+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${GIF_FILE_OUTPUT}" DESTINATION ${MINEMAP_PALETTE_DIR})
33+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${GIMP_FILE_PREFIX}.gpl" DESTINATION ${GIMP_PALETTE_DIR})
2134
endforeach()

misc/gen_gimp_palette.cpp

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
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-
81
#include <cstring>
92
#include <climits>
103
#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
174

18-
/* Used to convert text describing colors into gif palette
5+
/* Used to convert text describing colors into GIMP palette
196
*
207
* This specific program is not designed with security in mind (there is no return code checks, out-of-bound checks or
218
* whatsoever) and is only used to generate static gif files at build time.
@@ -27,7 +14,7 @@ typedef SSIZE_T ssize_t;
2714
void usage() {
2815
using namespace std;
2916
cout << "Usage:" << endl;
30-
cout << " gen_color_map <input.txt> <output.gif>" << endl;
17+
cout << " gen_gimp_palette <input.txt> <output-prefix>" << endl;
3118
#if defined(_WIN32) || defined(_WIN64)
3219
printf("Executable built at %s %s", __DATE__, __TIME__);
3320
#endif
@@ -74,7 +61,8 @@ int main(int argc, char **argv) {
7461
return 1;
7562
}
7663
std::string input_filename = argv[1];
77-
std::string output_filename = argv[2];
64+
std::string gpl_prefix = argv[2];
65+
std::string gpl_filename = gpl_prefix + ".gpl";
7866

7967
FILE *input_file = fopen(input_filename.c_str(), "r");
8068
if (input_file == NULL) {
@@ -95,22 +83,20 @@ int main(int argc, char **argv) {
9583
run_colors(num_colors, input_file, colors);
9684
fclose(input_file);
9785

98-
// Generate palette image
86+
// Output to gimp palette gpl
9987
{
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);
88+
FILE *output_file = fopen(gpl_filename.c_str(), "w");
89+
if (output_file == NULL) {
90+
fprintf(stderr, "%s\n", "Fail to open file");
91+
return 1;
10992
}
110-
catch (Magick::Exception &e) {
111-
std::cerr << "Error when writing palette image: " << e.what() << std::endl;
112-
exit(1);
93+
// Write header
94+
fprintf(output_file, "GIMP Palette\nName: %s\nColumns: 4\n#\n", gpl_prefix.c_str());
95+
// Write out colors
96+
for (int i = 0; i < num_colors; i++) {
97+
fprintf(output_file, "%3i %3i %3i #%i\n", colors[i * 4], colors[i * 4 + 1], colors[i * 4 + 2], i);
11398
}
99+
fclose(output_file);
114100
}
115101
return 0;
116102
}

0 commit comments

Comments
 (0)