aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@member.fsf.org>2016-01-02 07:19:16 +0100
committerMattias Andrée <maandree@member.fsf.org>2016-01-02 07:19:16 +0100
commitb841ac15a3a39532c6f48d921d0817bd61b80f55 (patch)
tree76ca4d5611f7a67a57a85b344fec42a48dfa48b9
parentparser for 10deg (diff)
downloadradharc-b841ac15a3a39532c6f48d921d0817bd61b80f55.tar.gz
radharc-b841ac15a3a39532c6f48d921d0817bd61b80f55.tar.bz2
radharc-b841ac15a3a39532c6f48d921d0817bd61b80f55.tar.xz
blackbody
Signed-off-by: Mattias Andrée <maandree@member.fsf.org>
Diffstat (limited to '')
-rw-r--r--src/blackbody.c152
-rw-r--r--src/blackbody.h38
-rw-r--r--src/parse_10deg.c4
3 files changed, 193 insertions, 1 deletions
diff --git a/src/blackbody.c b/src/blackbody.c
new file mode 100644
index 0000000..ca337ae
--- /dev/null
+++ b/src/blackbody.c
@@ -0,0 +1,152 @@
+/**
+ * Copyright © 2016 Mattias Andrée <maandree@member.fsf.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#include "blackbody.h"
+#include <unistd.h>
+#include <math.h>
+
+
+
+/**
+ * The highest colour temperature in the table.
+ */
+#define HIGHEST 40000
+
+/**
+ * The lowest colour temperature in the table.
+ */
+#define LOWEST 1000
+
+/**
+ * The temperature difference between the colours in the table.
+ */
+#define DELTA 100
+
+
+
+/**
+ * Convert from CIE xyY to [0, 1] sRGB.
+ *
+ * @param x The 'x' component.
+ * @param y The 'y' component.
+ * @param Y The 'Y' component.
+ * @param r Output parameter for the “red” value.
+ * (Seriously, sRGB red is orange, just look at it fullscreen.)
+ * @param g Output parameter for the green value.
+ * @param b Output parameter for the blue value.
+ */
+static void
+ciexyy_to_srgb(double x, double y, double Y, double *r, double *g, double *b)
+{
+#define SRGB(C) (((C) <= 0.0031308) ? (12.92 * (C)) : ((1.0 + 0.055) * pow((C), 1.0 / 2.4) - 0.055))
+ double X, Z;
+
+ /* Convert CIE xyY to CIE XYZ. */
+ X = Y * (y == 0 ? 0 : (x / y));
+ Z = Y * (y == 0 ? 0 : ((1 - x - y) / y));
+
+ /* Convert CIE XYZ to [0, 1] linear RGB. (ciexyz_to_linear) */
+ *r = ( 3.240450 * X) + (-1.537140 * Y) + (-0.4985320 * Z);
+ *g = (-0.969266 * X) + ( 1.876010 * Y) + ( 0.0415561 * Z);
+ *b = (0.0556434 * X) + (-0.204026 * Y) + ( 1.0572300 * Z);
+
+ /* Convert [0, 1] linear RGB to [0, 1] sRGB. */
+ SRGB(*r), SRGB(*g), SRGB(*b);
+}
+
+
+/**
+ * Perform linear interpolation (considered very good)
+ * between the CIE xyY values for two colour temperatures
+ * and convert the result to sRGB. The two colours should
+ * be the closest below the desired colour temperature,
+ * and the closest above the desired colour temperature.
+ *
+ * @param x1 The 'x' component for the low colour.
+ * @param y1 The 'y' component for the low colour.
+ * @param x2 The 'x' component for the high colour.
+ * @param y2 The 'y' component for the high colour.
+ * @param temp The desired colour temperature.
+ * @param r Output parameter for the “red” value.
+ * @param g Output parameter for the green value.
+ * @param b Output parameter for the blue value.
+ */
+static void
+interpolate(double x1, double y1, double x2, double y2, double temp, double *r, double *g, double *g)
+{
+ double weight = fmod(temp, (double)DELTA) / (double)DELTA;
+ double x = x1 * (1 - weight) + x2 * weight;
+ double y = y1 * (1 - weight) + y2 * weight;
+ ciexyy_to_srgb(x, y, 1.0, r, g, b);
+}
+
+
+/**
+ * Get the [0, 1] sRGB values of a colour temperature.
+ *
+ * @param fd File descriptor for the colour table.
+ * @param temp The desired colour temperature.
+ * @param r Output parameter for the “red” value.
+ * @param g Output parameter for the green value.
+ * @param b Output parameter for the blue value.
+ * @return 0 on succeess, -1 on error.
+ *
+ * @throws 0 The file did not have the expected size.
+ * @throws EDOM The selected temperature is below 1000 K.
+ */
+int
+get_colour(int fd, int temp, double *r, double *g, double *b)
+{
+ double values[10]; /* low:x,y,r,g,b + high:x,y,r,g,b */
+ off_t offset;
+ double max;
+
+ /* We do not have any values for above 40 000 K, but
+ * the differences will be unnoticeable, perhaps even
+ * unencodeable. */
+ if (temp > HIGHEST) temp = HIGHEST;
+ /* Things do not glow below 1000 K. Yes, fire is hot! */
+ if (temp < LOWEST) return EDOM, -1;
+
+ /* Read table. */
+ offset = ((off_t)temp - LOWEST) / DELTA;
+ offset *= (off_t)(5 * sizeof(double));
+ errno = 0;
+ if (pread(fd, values, sizeof(values), offset) < sizeof(values))
+ return -1;
+
+ /* Get colour. */
+ if (temp % DELTA)
+ interpolate(values[0], values[1], values[6], values[7], (double)temp, r, g, b);
+ else
+ *r = values[2], *g = values[3], *b = values[4];
+
+ /* Adjust colours for use. */
+ max = fmax(fmax(fabs(*r), fabs(*g)), fabs(*b));
+ if (max != 0)
+ *r /= max, *g /= max, *b /= max;
+ *r = *r > 0.0 ? *r : 0.0;
+ *g = *g > 0.0 ? *g : 0.0;
+ *b = *b > 0.0 ? *b : 0.0;
+
+ return 0;
+}
+
diff --git a/src/blackbody.h b/src/blackbody.h
new file mode 100644
index 0000000..832305c
--- /dev/null
+++ b/src/blackbody.h
@@ -0,0 +1,38 @@
+/**
+ * Copyright © 2016 Mattias Andrée <maandree@member.fsf.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+
+/**
+ * Get the [0, 1] sRGB values of a colour temperature.
+ *
+ * @param fd File descriptor for the colour table.
+ * @param temp The desired colour temperature.
+ * @param r Output parameter for the “red” value.
+ * @param g Output parameter for the green value.
+ * @param b Output parameter for the blue value.
+ * @return 0 on succeess, -1 on error.
+ *
+ * @throws 0 The file did not have the expected size.
+ * @throws EDOM The selected temperature is below 1000 K.
+ */
+int get_colour(int fd, int temp, double *r, double *g, double *b);
+
diff --git a/src/parse_10deg.c b/src/parse_10deg.c
index 0b34050..e562be0 100644
--- a/src/parse_10deg.c
+++ b/src/parse_10deg.c
@@ -61,9 +61,11 @@ int main(void)
if (write(1, xyrgb, sizeof(xyrgb)) < (ssize_t)sizeof(xyrgb))
return perror(""), 1;
}
+ if (write(1, xyrgb, sizeof(xyrgb)) < (ssize_t)sizeof(xyrgb)) /* sugar */
+ return perror(""), 1;
if (fstat(1, &attr))
return perror(""), 1;
- if ((size_t)(attr.st_size) != EXPECTED_ELEMENTS * 5 * sizeof(double))
+ if ((size_t)(attr.st_size) != (EXPECTED_ELEMENTS + 1) * 5 * sizeof(double))
return 1;
return 0;