+static void
+init_palette(const char *palette_file)
+{
+ if (palette_file != 0) {
+ int cp;
+
+ all_colors = typeMalloc(RGB_DATA, (unsigned) COLORS);
+ for (cp = 0; cp < COLORS; ++cp) {
+ color_content(cp,
+ &all_colors[cp].red,
+ &all_colors[cp].green,
+ &all_colors[cp].blue);
+ if (palette_file != 0) {
+ char **data = read_file(palette_file);
+ if (data != 0) {
+ int n;
+ int red, green, blue;
+ int scale = 1000;
+ int c;
+ for (n = 0; data[n] != 0; ++n) {
+ if (sscanf(data[n], "scale:%d", &c) == 1) {
+ scale = c;
+ } else if (sscanf(data[n], "%d:%d %d %d",
+ &c,
+ &red,
+ &green,
+ &blue) == 4
+ && okCOLOR(c)
+ && okRGB(red)
+ && okRGB(green)
+ && okRGB(blue)) {
+ /* *INDENT-EQLS* */
+ all_colors[c].red = ScaledColor(red);
+ all_colors[c].green = ScaledColor(green);
+ all_colors[c].blue = ScaledColor(blue);
+ }
+ }
+ free_data(data);
+ }
+ }
+ }
+ } else if (COLORS > 1) {
+ /* *INDENT-EQLS* */
+ int power2 = 1;
+ int shift = 0;
+
+ while (power2 < COLORS) {
+ ++shift;
+ power2 <<= 1;
+ }
+
+ if (power2 != COLORS || (shift % 3) != 0) {
+ giveup("With %d colors, you need a palette-file", COLORS);
+ }
+ }
+}
+
+/*
+ * Map the 24-bit RGB value to a color index if using a palette, otherwise to a
+ * direct color value.
+ */