-/* $Id: colormac.h,v 1.2 2000/10/29 18:12:14 brianp Exp $ */
+/**
+ * \file colormac.h
+ * Color-related macros
+ */
/*
* Mesa 3-D graphics library
- * Version: 3.5
+ * Version: 5.1
*
- * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
+ * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
-/*
- * Color-related macros
- */
-
#ifndef COLORMAC_H
#define COLORMAC_H
-#include "glheader.h"
+#include "imports.h"
#include "config.h"
#include "macros.h"
-#include "mmath.h"
-
-
-
-/*
- * Integer / float conversion for colors, normals, etc.
- */
-
-#define BYTE_TO_UBYTE(b) (b < 0 ? 0 : (GLubyte) b)
-#define SHORT_TO_UBYTE(s) (s < 0 ? 0 : (GLubyte) (s >> 7))
-#define USHORT_TO_UBYTE(s) (GLubyte) (s >> 8)
-#define INT_TO_UBYTE(i) (i < 0 ? 0 : (GLubyte) (i >> 23))
-#define UINT_TO_UBYTE(i) (GLubyte) (i >> 24)
-/* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
-#define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F))
+/** \def BYTE_TO_CHAN
+ * Convert from GLbyte to GLchan */
-/* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
-#define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
+/** \def UBYTE_TO_CHAN
+ * Convert from GLubyte to GLchan */
-/* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
-#define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
+/** \def SHORT_TO_CHAN
+ * Convert from GLshort to GLchan */
+/** \def USHORT_TO_CHAN
+ * Convert from GLushort to GLchan */
-/* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
-#define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
+/** \def INT_TO_CHAN
+ * Convert from GLint to GLchan */
-/* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
-#define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
+/** \def UINT_TO_CHAN
+ * Convert from GLuint to GLchan */
+/** \def CHAN_TO_UBYTE
+ * Convert from GLchan to GLubyte */
-/* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
-#define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
+/** \def CHAN_TO_FLOAT
+ * Convert from GLchan to GLfloat */
-/* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
-#define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
+/** \def CLAMPED_FLOAT_TO_CHAN
+ * Convert from GLclampf to GLchan */
+/** \def UNCLAMPED_FLOAT_TO_CHAN
+ * Convert from GLfloat to GLchan */
-/* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
-#define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F))
-
-/* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
-#define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
-
-
-/* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
-#define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
-
-/* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
-/* causes overflow:
-#define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
-*/
-/* a close approximation: */
-#define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
-
+/** \def COPY_CHAN4
+ * Copy a GLchan[4] array */
+/** \def CHAN_PRODUCT
+ * Scaled product (usually approximated) between two GLchan arguments */
#if CHAN_BITS == 8
#define INT_TO_CHAN(i) ((i) < 0 ? 0 : (GLchan) ((i) >> 23))
#define UINT_TO_CHAN(i) ((GLchan) ((i) >> 24))
+#define CHAN_TO_UBYTE(c) (c)
#define CHAN_TO_FLOAT(c) UBYTE_TO_FLOAT(c)
-#define FLOAT_COLOR_TO_CHAN(c, f) FLOAT_COLOR_TO_UBYTE_COLOR(c, f)
+#define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_UBYTE(c, f)
+#define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_UBYTE(c, f)
#define COPY_CHAN4(DST, SRC) COPY_4UBV(DST, SRC)
-#define CHAN_PRODUCT(a, b) ( (GLubyte) (((GLint)(a) * ((GLint)(b) + 1)) >> 8) )
+#define CHAN_PRODUCT(a, b) ((GLubyte) (((GLint)(a) * ((GLint)(b) + 1)) >> 8))
#elif CHAN_BITS == 16
-#define BYTE_TO_CHAN(b) ((b) < 0 ? 0 : (GLchan) ((b) * 516))
-#define UBYTE_TO_CHAN(b) ((GLchan) (((b) << 8) | (b)))
+#define BYTE_TO_CHAN(b) ((b) < 0 ? 0 : (((GLchan) (b)) * 516))
+#define UBYTE_TO_CHAN(b) ((((GLchan) (b)) << 8) | ((GLchan) (b)))
#define SHORT_TO_CHAN(s) ((s) < 0 ? 0 : (GLchan) (s))
#define USHORT_TO_CHAN(s) (s)
#define INT_TO_CHAN(i) ((i) < 0 ? 0 : (GLchan) ((i) >> 15))
#define UINT_TO_CHAN(i) ((GLchan) ((i) >> 16))
+#define CHAN_TO_UBYTE(c) ((c) >> 8)
#define CHAN_TO_FLOAT(c) ((GLfloat) ((c) * (1.0 / CHAN_MAXF)))
-#define FLOAT_COLOR_TO_CHAN(c, f) \
- c = ((GLchan) FloatToInt(CLAMP(f, 0.0F, 1.0F) * CHAN_MAXF + 0.5F))
+#define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_USHORT(c, f)
+#define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_USHORT(c, f)
#define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
-#define CHAN_PRODUCT(a, b) ( (GLchan) ((((GLint) (a)) * ((GLint) (b))) / 65535) )
+#define CHAN_PRODUCT(a, b) ((GLchan) ((((GLuint) (a)) * ((GLuint) (b))) / 65535))
#elif CHAN_BITS == 32
#define INT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 2147483647.0F)))
#define UINT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 4294967295.0F)))
+#define CHAN_TO_UBYTE(c) FLOAT_TO_UBYTE(c)
#define CHAN_TO_FLOAT(c) (c)
-#define FLOAT_COLOR_TO_CHAN(c, f) c = f
+#define CLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
+#define UNCLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
#define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
#define CHAN_PRODUCT(a, b) ((a) * (b))
-#else
+#else
#error unexpected CHAN_BITS size
#endif
-#define FLOAT_RGB_TO_CHAN_RGB(dst, f) \
+/**
+ * Convert 3 channels at once.
+ *
+ * \param dst pointer to destination GLchan[3] array.
+ * \param f pointer to source GLfloat[3] array.
+ *
+ * \sa #UNCLAMPED_FLOAT_TO_CHAN.
+ */
+#define UNCLAMPED_FLOAT_TO_RGB_CHAN(dst, f) \
do { \
- FLOAT_COLOR_TO_CHAN(dst[0], f[0]); \
- FLOAT_COLOR_TO_CHAN(dst[1], f[1]); \
- FLOAT_COLOR_TO_CHAN(dst[2], f[2]); \
-} while(0)
+ UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
+ UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
+ UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
+} while (0)
-#define FLOAT_RGBA_TO_CHAN_RGBA(c, f) \
+
+/**
+ * Convert 4 channels at once.
+ *
+ * \param dst pointer to destination GLchan[4] array.
+ * \param f pointer to source GLfloat[4] array.
+ *
+ * \sa #UNCLAMPED_FLOAT_TO_CHAN.
+ */
+#define UNCLAMPED_FLOAT_TO_RGBA_CHAN(dst, f) \
do { \
- FLOAT_COLOR_TO_CHAN(c[0], f[0]); \
- FLOAT_COLOR_TO_CHAN(c[1], f[1]); \
- FLOAT_COLOR_TO_CHAN(c[2], f[2]); \
- FLOAT_COLOR_TO_CHAN(c[3], f[3]); \
-} while(0)
+ UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
+ UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
+ UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
+ UNCLAMPED_FLOAT_TO_CHAN(dst[3], f[3]); \
+} while (0)
-#if CHAN_BITS == 32
-#define FLOAT_TO_CHAN(f) (f)
-#define DOUBLE_TO_CHAN(f) ((GLfloat) (f))
-#define UNCLAMPED_FLOAT_TO_CHAN(f) (f)
-#define UNCLAMPED_DOUBLE_TO_CHAN(f) ((GLfloat) (f))
+/**
+ * \name Generic color packing macros
+ *
+ * \todo We may move these into texutil.h at some point.
+ */
+/*@{*/
-#else
+#define PACK_COLOR_8888( a, b, c, d ) \
+ (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
-#define FLOAT_TO_CHAN(f) ( (GLchan) FloatToInt((f) * CHAN_MAXF + 0.5F) )
-#define DOUBLE_TO_CHAN(f) ( (GLchan) FloatToInt((f) * CHAN_MAXF + 0.5F) )
+#define PACK_COLOR_888( a, b, c ) \
+ (((a) << 16) | ((b) << 8) | (c))
-#define UNCLAMPED_FLOAT_TO_CHAN(f) \
- ( (GLchan) FloatToInt( CLAMP(f, 0.0, 1.0) * CHAN_MAXF + 0.5F) )
+#define PACK_COLOR_565( a, b, c ) \
+ ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
-#define UNCLAMPED_DOUBLE_TO_CHAN(f) \
- ( (GLchan) FloatToInt( CLAMP(f, 0.0, 1.0) * CHAN_MAXF + 0.5F) )
+#define PACK_COLOR_1555( a, b, c, d ) \
+ ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) | \
+ ((a) ? 0x8000 : 0))
-#endif
+#define PACK_COLOR_4444( a, b, c, d ) \
+ ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
+
+#define PACK_COLOR_88( a, b ) \
+ (((a) << 8) | (b))
+
+#define PACK_COLOR_332( a, b, c ) \
+ (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
+
+
+#ifdef MESA_BIG_ENDIAN
+
+#define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( d, c, b, a )
+
+#define PACK_COLOR_565_LE( a, b, c ) \
+ (((a) & 0xf8) | (((b) & 0xe0) >> 5) | (((b) & 0x1c) << 11) | \
+ (((c) & 0xf8) << 5))
+
+#define PACK_COLOR_1555_LE( a, b, c, d ) \
+ ((((b) & 0xf8) >> 1) | (((c) & 0xc0) >> 6) | (((c) & 0x38) << 10) | \
+ (((d) & 0xf8) << 5) | ((a) ? 0x80 : 0))
+
+#define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( c, d, a, b )
+
+#define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( b, a )
+
+#else /* little endian */
+
+#define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( a, b, c, d )
+
+#define PACK_COLOR_565_LE( a, b, c ) PACK_COLOR_565( a, b, c )
+
+#define PACK_COLOR_1555_LE( a, b, c, d ) PACK_COLOR_1555( a, b, c, d )
+
+#define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( a, b, c, d )
+
+#define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( a, b )
+
+#endif /* endianness */
+
+/*@}*/
#endif /* COLORMAC_H */