shithub: rgbds

Download patch

ref: 91984cb7e715db9693dc8f6542a74d368b31fe51
parent: b8d5dd18240cd12ed3cb5294a8c2c239d13fb145
author: Jakub Kądziołka <[email protected]>
date: Tue Aug 20 10:15:07 EDT 2019

rgbgfx: Add an option to take the CGB's color profile into account

--- a/include/gfx/main.h
+++ b/include/gfx/main.h
@@ -23,6 +23,7 @@
 	bool horizontal;
 	bool mirror;
 	bool unique;
+	bool colorcurve;
 	int trim;
 	char *tilemapfile;
 	bool tilemapout;
--- a/src/gfx/gb.c
+++ b/src/gfx/gb.c
@@ -285,6 +285,30 @@
 		free(opts->attrmapfile);
 }
 
+/*
+ * based on the Gaussian-like curve used by SameBoy since commit
+ * 65dd02cc52f531dbbd3a7e6014e99d5b24e71a4c (Oct 2017)
+ * with ties resolved by comparing the difference of the squares.
+ */
+static int reverse_curve[] = {
+	0,  0,  1,  1,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,
+	5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  6,  6,  6,  6,  7,  7,
+	7,  7,  7,  7,  7,  7,  7,  8,  8,  8,  8,  8,  8,  8,  8,  8,
+	9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  10, 10, 10, 10, 10, 10,
+	10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
+	12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
+	13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14,
+	14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17,
+	17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18,
+	18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+	19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21,
+	21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22,
+	22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24,
+	24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26,
+	26, 27, 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 30, 30, 31,
+};
+
 void output_palette_file(const struct Options *opts,
 			 const struct RawIndexedImage *raw_image)
 {
@@ -297,10 +321,25 @@
 		err(1, "Opening palette file '%s' failed", opts->palfile);
 
 	for (i = 0; i < raw_image->num_colors; i++) {
-		color =
-			raw_image->palette[i].blue  >> 3 << 10 |
-			raw_image->palette[i].green >> 3 <<  5 |
-			raw_image->palette[i].red   >> 3;
+		int r = raw_image->palette[i].red;
+		int g = raw_image->palette[i].green;
+		int b = raw_image->palette[i].blue;
+
+		if (opts->colorcurve) {
+			g = (g * 4 - b) / 3;
+			if (g < 0)
+				g = 0;
+
+			r = reverse_curve[r];
+			g = reverse_curve[g];
+			b = reverse_curve[b];
+		} else {
+			r >>= 3;
+			g >>= 3;
+			b >>= 3;
+		}
+
+		color = b << 10 | g << 5 | r;
 		cur_bytes[0] = color & 0xFF;
 		cur_bytes[1] = color >> 8;
 		fwrite(cur_bytes, 2, 1, f);
--- a/src/gfx/main.c
+++ b/src/gfx/main.c
@@ -45,7 +45,7 @@
 
 	depth = 2;
 
-	while ((ch = getopt(argc, argv, "Aa:Dd:Ffhmo:Tt:uPp:Vvx:")) != -1) {
+	while ((ch = getopt(argc, argv, "Aa:CDd:Ffhmo:Tt:uPp:Vvx:")) != -1) {
 		switch (ch) {
 		case 'A':
 			opts.attrmapout = true;
@@ -52,6 +52,9 @@
 			break;
 		case 'a':
 			opts.attrmapfile = optarg;
+			break;
+		case 'C':
+			opts.colorcurve = true;
 			break;
 		case 'D':
 			opts.debug = true;
--- a/src/gfx/rgbgfx.1
+++ b/src/gfx/rgbgfx.1
@@ -61,6 +61,8 @@
 but the attrmap file output name is made by taking the input filename, removing
 the file extension, and appending
 .Pa .attrmap .
+.It Fl C
+Use the color curve of the Game Boy Color when generating palettes.
 .It Fl D
 Debug features are enabled.
 .It Fl f