Cleanup: move unit char/short/float to functions
authorCampbell Barton <ideasman42@gmail.com>
Mon, 7 May 2018 15:14:28 +0000 (17:14 +0200)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 7 May 2018 15:50:25 +0000 (17:50 +0200)
This caused GCC 8.1 to crash at build time,
but was also not very nice use of macros.

source/blender/alembic/intern/abc_customdata.cc
source/blender/blenlib/BLI_utildefines.h
source/blender/blenlib/intern/math_base_inline.c
source/blender/compositor/operations/COM_PreviewOperation.cpp
source/blender/imbuf/intern/filter.c
source/blender/imbuf/intern/scaling.c
source/blender/makesrna/intern/rna_ID.c
source/blender/makesrna/intern/rna_image.c

index b3b015c7abf6332fd1c7da802f57541e3c4d8791..3b6c87ea1a0167b6dcd3aa58c33936647de5e618 100644 (file)
@@ -31,6 +31,7 @@ extern "C" {
 #include "DNA_customdata_types.h"
 #include "DNA_meshdata_types.h"
 
+#include "BLI_math_base.h"
 #include "BKE_customdata.h"
 }
 
index 46b3748c7cef0e5ba984f1e9e4c735f606273a09..65dcbf049137ab3dae43ad227470f5c03192a538 100644 (file)
@@ -224,29 +224,6 @@ extern "C" {
        b = tmp;                                                                  \
 } (void)0
 
-
-#define FTOCHAR(val) ((CHECK_TYPE_INLINE(val, float)), \
-               (char)(((val) <= 0.0f) ? 0 : (((val) > (1.0f - 0.5f / 255.0f)) ? 255 : ((255.0f * (val)) + 0.5f))))
-#define FTOUSHORT(val) ((CHECK_TYPE_INLINE(val, float)), \
-               (unsigned short)((val >= 1.0f - 0.5f / 65535) ? 65535 : (val <= 0.0f) ? 0 : (val * 65535.0f + 0.5f)))
-#define USHORTTOUCHAR(val) ((unsigned char)(((val) >= 65535 - 128) ? 255 : ((val) + 128) >> 8))
-#define F3TOCHAR3(v2, v1) {                                                   \
-               (v1)[0] = FTOCHAR((v2[0]));                                           \
-               (v1)[1] = FTOCHAR((v2[1]));                                           \
-               (v1)[2] = FTOCHAR((v2[2]));                                           \
-} (void)0
-#define F3TOCHAR4(v2, v1) {                                                   \
-               (v1)[0] = FTOCHAR((v2[0]));                                           \
-               (v1)[1] = FTOCHAR((v2[1]));                                           \
-               (v1)[2] = FTOCHAR((v2[2]));                                           \
-               (v1)[3] = 255;                                                        \
-} (void)0
-#define F4TOCHAR4(v2, v1) {                                                   \
-               (v1)[0] = FTOCHAR((v2[0]));                                           \
-               (v1)[1] = FTOCHAR((v2[1]));                                           \
-               (v1)[2] = FTOCHAR((v2[2]));                                           \
-               (v1)[3] = FTOCHAR((v2[3]));                                           \
-} (void)0
 #define VECCOPY(v1, v2) {                                                     \
                *(v1) =   *(v2);                                                      \
                *(v1 + 1) = *(v2 + 1);                                                \
index 4bedcbdf5bf1ab23f9cb25475777003525a383c6..82ed6cd1cd11de303c9cd2e6a9756900284d4cd1 100644 (file)
@@ -513,6 +513,38 @@ MALWAYS_INLINE __m128 _bli_math_blend_sse(const __m128 mask,
        return _mm_or_ps(_mm_and_ps(mask, a), _mm_andnot_ps(mask, b));
 }
 
+/* Low level conversion functions */
+/* TODO: name sensibly. */
+MINLINE unsigned char FTOCHAR(float val)
+{
+       return (unsigned char)(((val <= 0.0f) ? 0 : ((val > (1.0f - 0.5f / 255.0f)) ? 255 : ((255.0f * val) + 0.5f))));
+}
+#define FTOCHAR(val) ((CHECK_TYPE_INLINE(val, float)), FTOCHAR(val))
+
+MINLINE unsigned short FTOUSHORT(float val)
+{
+       return (unsigned short)((val >= 1.0f - 0.5f / 65535) ? 65535 : (val <= 0.0f) ? 0 : (val * 65535.0f + 0.5f));
+}
+#define FTOUSHORT(val) ((CHECK_TYPE_INLINE(val, float)), FTOUSHORT(val))
+
+MINLINE unsigned char USHORTTOUCHAR(unsigned short val)
+{
+       return (unsigned char)(((val) >= 65535 - 128) ? 255 : ((val) + 128) >> 8);
+}
+#define USHORTTOUCHAR(val) ((CHECK_TYPE_INLINE(val, unsigned short)), USHORTTOUCHAR(val))
+
+#define F3TOCHAR3(v2, v1) {                                                   \
+               (v1)[0] = FTOCHAR((v2[0]));                                           \
+               (v1)[1] = FTOCHAR((v2[1]));                                           \
+               (v1)[2] = FTOCHAR((v2[2]));                                           \
+} ((void)0)
+#define F4TOCHAR4(v2, v1) {                                                   \
+               (v1)[0] = FTOCHAR((v2[0]));                                           \
+               (v1)[1] = FTOCHAR((v2[1]));                                           \
+               (v1)[2] = FTOCHAR((v2[2]));                                           \
+               (v1)[3] = FTOCHAR((v2[3]));                                           \
+} ((void)0)
+
 #endif  /* __SSE2__ */
 
 #endif /* __MATH_BASE_INLINE_C__ */
index aa667884de64d9c9b71cedfac4941c86fc431e73..2845b972f65486a8999e704dd1fef40510bbbce2 100644 (file)
@@ -105,7 +105,7 @@ void PreviewOperation::executeRegion(rcti *rect, unsigned int /*tileNumber*/)
                        color[3] = 1.0f;
                        this->m_input->readSampled(color, rx, ry, COM_PS_NEAREST);
                        IMB_colormanagement_processor_apply_v4(cm_processor, color);
-                       F4TOCHAR4(color, this->m_outputBuffer + offset);
+                       rgba_float_to_uchar(this->m_outputBuffer + offset, color);
                        offset += 4;
                }
        }
index 38609d0a342825cbe4711963948a56aa045e2836..27711618ceeb5005a671b16d060d58c7723de9b5 100644 (file)
@@ -34,6 +34,7 @@
 #include "MEM_guardedalloc.h"
 
 #include "BLI_utildefines.h"
+#include "BLI_math_base.h"
 
 #include "IMB_imbuf_types.h"
 #include "IMB_imbuf.h"
index ff92ce158118f2cd1c6da87720812f8df3d1e657..1c0b47f7942832c9ab823954f1ab32527d1dbb17 100644 (file)
@@ -332,9 +332,9 @@ MINLINE void premul_ushort_to_straight_uchar(unsigned char *result, const unsign
        else {
                unsigned short alpha = color[3] / 256;
 
-               result[0] = USHORTTOUCHAR(color[0] / alpha * 256);
-               result[1] = USHORTTOUCHAR(color[1] / alpha * 256);
-               result[2] = USHORTTOUCHAR(color[2] / alpha * 256);
+               result[0] = USHORTTOUCHAR((ushort)(color[0] / alpha * 256));
+               result[1] = USHORTTOUCHAR((ushort)(color[1] / alpha * 256));
+               result[2] = USHORTTOUCHAR((ushort)(color[2] / alpha * 256));
                result[3] = USHORTTOUCHAR(color[3]);
        }
 }
index bc2c26c2b2b87805df6b390dce0824d361e78e79..7357712aaef03f0b242a150a4278c8dcd1dba0c8 100644 (file)
@@ -33,6 +33,7 @@
 #include "DNA_object_types.h"
 
 #include "BLI_utildefines.h"
+#include "BLI_math_base.h"
 
 #include "BKE_icons.h"
 #include "BKE_object.h"
index cb71cf756ec996de6f313399b877527d003fd059..5bb38f2930947bdf24b400b97108cc77e1eff4e0 100644 (file)
@@ -30,6 +30,7 @@
 #include "DNA_scene_types.h"
 
 #include "BLI_utildefines.h"
+#include "BLI_math_base.h"
 
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"