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;
2714void 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\n Name: %s\n Columns: 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