Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'source/blender/blenlib/intern/math_color.c')
-rw-r--r--source/blender/blenlib/intern/math_color.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c
index 8f5366b6317..3627b38a091 100644
--- a/source/blender/blenlib/intern/math_color.c
+++ b/source/blender/blenlib/intern/math_color.c
@@ -30,7 +30,11 @@
#include <assert.h>
+#include "MEM_guardedalloc.h"
+
#include "BLI_math.h"
+#include "BLI_rand.h"
+#include "BLI_utildefines.h"
void hsv_to_rgb(float h, float s, float v, float *r, float *g, float *b)
{
@@ -480,3 +484,79 @@ void rgb_byte_set_hue_float_offset(unsigned char rgb[3], float hue_offset)
rgb_float_set_hue_float_offset(rgb_float, hue_offset);
rgb_float_to_byte(rgb_float, rgb);
}
+
+
+/* fast sRGB conversion
+ * LUT from linear float to 16-bit short
+ * based on http://mysite.verizon.net/spitzak/conversion/
+ */
+
+float BLI_color_from_srgb_table[256];
+unsigned short BLI_color_to_srgb_table[0x10000];
+
+static unsigned short hipart(const float f)
+{
+ union {
+ float f;
+ unsigned short us[2];
+ } tmp;
+
+ tmp.f = f;
+
+#ifdef __BIG_ENDIAN__
+ return tmp.us[0];
+#else
+ return tmp.us[1];
+#endif
+}
+
+static float index_to_float(const unsigned short i)
+{
+ union {
+ float f;
+ unsigned short us[2];
+ } tmp;
+
+ /* positive and negative zeros, and all gradual underflow, turn into zero: */
+ if (i<0x80 || (i >= 0x8000 && i < 0x8080)) return 0;
+ /* All NaN's and infinity turn into the largest possible legal float: */
+ if (i>=0x7f80 && i<0x8000) return FLT_MAX;
+ if (i>=0xff80) return -FLT_MAX;
+
+#ifdef __BIG_ENDIAN__
+ tmp.us[0] = i;
+ tmp.us[1] = 0x8000;
+#else
+ tmp.us[0] = 0x8000;
+ tmp.us[1] = i;
+#endif
+
+ return tmp.f;
+}
+
+void BLI_init_srgb_conversion(void)
+{
+ static int initialized= 0;
+ int i, b;
+
+ if (initialized) return;
+ initialized = 1;
+
+ /* Fill in the lookup table to convert floats to bytes: */
+ for (i = 0; i < 0x10000; i++) {
+ float f = linearrgb_to_srgb(index_to_float(i))*255.0f;
+ if (f <= 0) BLI_color_to_srgb_table[i] = 0;
+ else if (f < 255) BLI_color_to_srgb_table[i] = (unsigned short)(f*0x100+.5);
+ else BLI_color_to_srgb_table[i] = 0xff00;
+ }
+
+ /* Fill in the lookup table to convert bytes to float: */
+ for (b = 0; b <= 255; b++) {
+ float f = srgb_to_linearrgb(((float)b)*(1.0f/255.0f));
+ BLI_color_from_srgb_table[b] = f;
+ i = hipart(f);
+ /* replace entries so byte->float->byte does not change the data: */
+ BLI_color_to_srgb_table[i] = b*0x100;
+ }
+}
+