aboutsummaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2017-05-16 20:14:22 +0200
committerMattias Andrée <maandree@kth.se>2017-05-16 20:14:22 +0200
commit0f03cc378e6ce48f17a20e409f93bfc11345a6ed (patch)
treef4768c6fe96e1ac2a9c5833dee897924300d4a45 /src/util
parentAdd blind-tempral-mean (diff)
downloadblind-0f03cc378e6ce48f17a20e409f93bfc11345a6ed.tar.gz
blind-0f03cc378e6ce48f17a20e409f93bfc11345a6ed.tar.bz2
blind-0f03cc378e6ce48f17a20e409f93bfc11345a6ed.tar.xz
Fix errors and warnings and make the code cleaner
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/util')
-rw-r--r--src/util/colour.h296
-rw-r--r--src/util/io.h4
2 files changed, 104 insertions, 196 deletions
diff --git a/src/util/colour.h b/src/util/colour.h
index 7622af0..33f0347 100644
--- a/src/util/colour.h
+++ b/src/util/colour.h
@@ -7,210 +7,118 @@
#define D65_XYZ_X (D65_XYY_X / D65_XYY_Y)
#define D65_XYZ_Z (1 / D65_XYY_Y - 1 - D65_XYZ_X)
-static inline double
-srgb_encode(double t)
-{
- double sign = 1;
- if (t < 0) {
- t = -t;
- sign = -1;
+#define SRGB_ENCODE(TYPE, NAME_SUFFIX, MATH_SUFFIX)\
+ static inline TYPE\
+ srgb_encode##NAME_SUFFIX(TYPE t)\
+ {\
+ TYPE sign = 1;\
+ if (t < 0) {\
+ t = -t;\
+ sign = -1;\
+ }\
+ t = t <= (TYPE)0.0031306684425217108\
+ ? (TYPE)12.92 * t\
+ : (TYPE)1.055 * pow##MATH_SUFFIX(t, (TYPE)1.0 / (TYPE)2.4) - (TYPE)0.055;\
+ return t * sign;\
}
- t = t <= 0.0031306684425217108
- ? 12.92 * t
- : 1.055 * pow(t, 1 / 2.4) - 0.055;
- return t * sign;
-}
+SRGB_ENCODE(double, _d,)
+SRGB_ENCODE(float, _f, f)
+#undef SRGB_ENCODE
-static inline float
-srgb_encode_f(float t)
-{
- float sign = 1;
- if (t < 0) {
- t = -t;
- sign = -1;
+#define SRGB_DECODE(TYPE, NAME_SUFFIX, MATH_SUFFIX)\
+ static inline TYPE\
+ srgb_decode##NAME_SUFFIX(TYPE t)\
+ {\
+ TYPE sign = 1;\
+ if (t < 0) {\
+ t = -t;\
+ sign = -1;\
+ }\
+ t = t <= (TYPE)0.0031306684425217108 * (TYPE)12.92\
+ ? t / (TYPE)12.92\
+ : pow##MATH_SUFFIX((t + (TYPE)0.055) / (TYPE)1.055, (TYPE)2.4);\
+ return t * sign;\
}
- t = t <= (float)0.0031306684425217108
- ? (float)12.92 * t
- : (float)1.055 * powf(t, 1 / (float)2.4) - (float)0.055;
- return t * sign;
-}
+SRGB_DECODE(double, _d,)
+SRGB_DECODE(float, _f, f)
+#undef SRGB_DECODE
-static inline double
-srgb_decode(double t)
-{
- double sign = 1;
- if (t < 0) {
- t = -t;
- sign = -1;
- }
- t = t <= 0.0031306684425217108 * 12.92
- ? t / 12.92
- : pow((t + 0.055) / 1.055, 2.4);
- return t * sign;
-}
-static inline float
-srgb_decode_f(float t)
-{
- float sign = 1;
- if (t < 0) {
- t = -t;
- sign = -1;
+#define MATRIX_MULTIPLY_FUNCTION(FUNCTION, TYPE, R1C1, R1C2, R1C3, R2C1, R2C2, R2C3, R3C1, R3C2, R3C3)\
+ static inline void\
+ FUNCTION(TYPE ia, TYPE ib, TYPE ic, TYPE *oa, TYPE *ob, TYPE *oc)\
+ {\
+ *oa = (TYPE)(R1C1) * ia + (TYPE)(R1C2) * ib + (TYPE)(R1C3) * ic;\
+ *ob = (TYPE)(R2C1) * ia + (TYPE)(R2C2) * ib + (TYPE)(R2C3) * ic;\
+ *oc = (TYPE)(R3C1) * ia + (TYPE)(R3C2) * ib + (TYPE)(R3C3) * ic;\
}
- t = t <= (float)0.0031306684425217108 * (float)12.92
- ? t / (float)12.92
- : powf((t + (float)0.055) / (float)1.055, (float)2.4);
- return t * sign;
-}
-
-static inline void
-yuv_to_srgb(double y, double u, double v, double *r, double *g, double *b)
-{
-#define MULTIPLY(CY, CU, CV) ((CY) * y + (CU) * u + (CV) * v)
- *r = MULTIPLY(1, 0.00028328010485821202317155420580263580632163211703, 1.14070449590558520291949662350816652178764343261719);
- *g = MULTIPLY(1, -0.39630886669497211727275498560629785060882568359375, -0.58107364288228224857846271333983168005943298339844);
- *b = MULTIPLY(1, 2.03990003507541306504435851820744574069976806640625, 0.00017179031692307700847528739718228507626918144524);
-#undef MULTIPLY
-}
-
-static inline void
-yuv_to_srgb_f(float y, float u, float v, float *r, float *g, float *b)
-{
-#define MULTIPLY(CY, CU, CV) ((float)(CY) * y + (float)(CU) * u + (float)(CV) * v)
- *r = MULTIPLY(1, 0.00028328010485821202317155420580263580632163211703, 1.14070449590558520291949662350816652178764343261719);
- *g = MULTIPLY(1, -0.39630886669497211727275498560629785060882568359375, -0.58107364288228224857846271333983168005943298339844);
- *b = MULTIPLY(1, 2.03990003507541306504435851820744574069976806640625, 0.00017179031692307700847528739718228507626918144524);
-#undef MULTIPLY
-}
-
-static inline void
-srgb_to_yuv(double r, double g, double b, double *y, double *u, double *v)
-{
-#define MULTIPLY(CR, CG, CB) ((CR) * r + (CG) * g + (CB) * b)
- *y = MULTIPLY(0.299, 0.587, 0.114);
- *u = MULTIPLY(-0.14662756598240470062854967636667424812912940979004,
- -0.28771586836102963635752871596196200698614120483398,
- 0.43434343434343436474165400795754976570606231689453);
- *v = MULTIPLY( 0.61456892577224520035628074765554629266262054443359,
- -0.51452282157676354490405401520547457039356231689453,
- -0.10004610419548178035231700278018251992762088775635);
-#undef MULTIPLY
-}
-
-static inline void
-srgb_to_yuv_f(float r, float g, float b, float *y, float *u, float *v)
-{
-#define MULTIPLY(CR, CG, CB) ((float)(CR) * r + (float)(CG) * g + (float)(CB) * b)
- *y = MULTIPLY(0.299, 0.587, 0.114);
- *u = MULTIPLY(-0.14662756598240470062854967636667424812912940979004,
- -0.28771586836102963635752871596196200698614120483398,
- 0.43434343434343436474165400795754976570606231689453);
- *v = MULTIPLY( 0.61456892577224520035628074765554629266262054443359,
- -0.51452282157676354490405401520547457039356231689453,
- -0.10004610419548178035231700278018251992762088775635);
-#undef MULTIPLY
-}
-
-static inline void
-ciexyz_to_srgb(double x, double y, double z, double *r, double *g, double *b)
-{
-#define MULTIPLY(CX, CY, CZ) ((CX) * x + (CY) * y + (CZ) * z)
- *r = MULTIPLY(3.240446254647737500675930277794, -1.537134761820080575134284117667, -0.498530193022728718155178739835);
- *g = MULTIPLY(-0.969266606244679751469561779231, 1.876011959788370209167851498933, 0.041556042214430065351304932619);
- *b = MULTIPLY(0.055643503564352832235773149705, -0.204026179735960239147729566866, 1.057226567722703292062647051353);
-#undef MULTIPLY
-}
+#define MATRIX_MULTIPLY_FUNCTIONS(FUNCTION_BASE, ...)\
+ MATRIX_MULTIPLY_FUNCTION(FUNCTION_BASE##_d, double, __VA_ARGS__)\
+ MATRIX_MULTIPLY_FUNCTION(FUNCTION_BASE##_f, float, __VA_ARGS__)
-static inline void
-ciexyz_to_srgb_f(float x, float y, float z, float *r, float *g, float *b)
-{
-#define MULTIPLY(CX, CY, CZ) ((float)(CX) * x + (float)(CY) * y + (float)(CZ) * z)
- *r = MULTIPLY(3.240446254647737500675930277794, -1.537134761820080575134284117667, -0.498530193022728718155178739835);
- *g = MULTIPLY(-0.969266606244679751469561779231, 1.876011959788370209167851498933, 0.041556042214430065351304932619);
- *b = MULTIPLY(0.055643503564352832235773149705, -0.204026179735960239147729566866, 1.057226567722703292062647051353);
-#undef MULTIPLY
-}
+MATRIX_MULTIPLY_FUNCTIONS(yuv_to_srgb,
+ 1,
+ 0.00028328010485821202317155420580263580632163211703,
+ 1.14070449590558520291949662350816652178764343261719,
+ 1,
+ -0.39630886669497211727275498560629785060882568359375,
+ -0.58107364288228224857846271333983168005943298339844,
+ 1,
+ 2.03990003507541306504435851820744574069976806640625,
+ 0.00017179031692307700847528739718228507626918144524)
-static inline void
-srgb_to_ciexyz(double r, double g, double b, double *x, double *y, double *z)
-{
-#define MULTIPLY(CR, CG, CB) ((CR) * r + (CG) * g + (CB) * b)
- *x = MULTIPLY(0.412457445582367576708548995157, 0.357575865245515878143578447634, 0.180437247826399665973085006954);
- *y = MULTIPLY(0.212673370378408277403536885686, 0.715151730491031756287156895269, 0.072174899130559869164791564344);
- *z = MULTIPLY(0.019333942761673460208893260415, 0.119191955081838593666354597644, 0.950302838552371742508739771438);
-#undef MULTIPLY
-}
+MATRIX_MULTIPLY_FUNCTIONS(srgb_to_yuv,
+ 0.299, 0.587, 0.114,
+ -0.14662756598240470062854967636667424812912940979004,
+ -0.28771586836102963635752871596196200698614120483398,
+ 0.43434343434343436474165400795754976570606231689453,
+ 0.61456892577224520035628074765554629266262054443359,
+ -0.51452282157676354490405401520547457039356231689453,
+ -0.10004610419548178035231700278018251992762088775635)
-static inline void
-srgb_to_ciexyz_f(float r, float g, float b, float *x, float *y, float *z)
-{
-#define MULTIPLY(CR, CG, CB) ((float)(CR) * r + (float)(CG) * g + (float)(CB) * b)
- *x = MULTIPLY(0.412457445582367576708548995157, 0.357575865245515878143578447634, 0.180437247826399665973085006954);
- *y = MULTIPLY(0.212673370378408277403536885686, 0.715151730491031756287156895269, 0.072174899130559869164791564344);
- *z = MULTIPLY(0.019333942761673460208893260415, 0.119191955081838593666354597644, 0.950302838552371742508739771438);
-#undef MULTIPLY
-}
+MATRIX_MULTIPLY_FUNCTIONS(ciexyz_to_srgb,
+ 3.240446254647737500675930277794,
+ -1.537134761820080575134284117667,
+ -0.498530193022728718155178739835,
+ -0.969266606244679751469561779231,
+ 1.876011959788370209167851498933,
+ 0.041556042214430065351304932619,
+ 0.055643503564352832235773149705,
+ -0.204026179735960239147729566866,
+ 1.057226567722703292062647051353)
-static inline void
-scaled_yuv_to_ciexyz(double y, double u, double v, double *xp, double *yp, double *zp)
-{
-#define MULTIPLY(CY, CU, CV) ((CY) * y + (CU) * u + (CV) * v)
- *xp = MULTIPLY( 0.00001450325106667098632156481796684488472237717360,
- 0.00000345586790639342739093228633329157872822179343,
- 0.00000400923398630552893485111398685916128670214675);
- *yp = MULTIPLY( 0.00001525902189669641837040624243737596543724066578,
- -0.00000207722814409390653614547427030512238843584782,
- -0.00000263898607692305410302407824019166326934282552);
- *zp = MULTIPLY( 0.00001661446153041708825425643025752719950105529279,
- 0.00002885925752619118069149627137104374696718878113,
- -0.00000071781086875769179526501342566979779746816348);
-#undef MULTIPLY
-}
+MATRIX_MULTIPLY_FUNCTIONS(srgb_to_ciexyz,
+ 0.412457445582367576708548995157,
+ 0.357575865245515878143578447634,
+ 0.180437247826399665973085006954,
+ 0.212673370378408277403536885686,
+ 0.715151730491031756287156895269,
+ 0.072174899130559869164791564344,
+ 0.019333942761673460208893260415,
+ 0.119191955081838593666354597644,
+ 0.950302838552371742508739771438)
-static inline void
-scaled_yuv_to_ciexyz_f(float y, float u, float v, float *xp, float *yp, float *zp)
-{
-#define MULTIPLY(CY, CU, CV) ((float)(CY) * y + (float)(CU) * u + (float)(CV) * v)
- *xp = MULTIPLY( 0.00001450325106667098632156481796684488472237717360,
- 0.00000345586790639342739093228633329157872822179343,
- 0.00000400923398630552893485111398685916128670214675);
- *yp = MULTIPLY( 0.00001525902189669641837040624243737596543724066578,
- -0.00000207722814409390653614547427030512238843584782,
- -0.00000263898607692305410302407824019166326934282552);
- *zp = MULTIPLY( 0.00001661446153041708825425643025752719950105529279,
- 0.00002885925752619118069149627137104374696718878113,
- -0.00000071781086875769179526501342566979779746816348);
-#undef MULTIPLY
-}
+MATRIX_MULTIPLY_FUNCTIONS(scaled_yuv_to_ciexyz,
+ 0.00001450325106667098632156481796684488472237717360,
+ 0.00000345586790639342739093228633329157872822179343,
+ 0.00000400923398630552893485111398685916128670214675,
+ 0.00001525902189669641837040624243737596543724066578,
+ -0.00000207722814409390653614547427030512238843584782,
+ -0.00000263898607692305410302407824019166326934282552,
+ 0.00001661446153041708825425643025752719950105529279,
+ 0.00002885925752619118069149627137104374696718878113,
+ -0.00000071781086875769179526501342566979779746816348)
-static inline void
-ciexyz_to_scaled_yuv(double x, double y, double z, double *yp, double *up, double *vp)
-{
-#define MULTIPLY(CX, CY, CZ) ((CX) * x + (CY) * y + (CZ) * z)
- *yp = MULTIPLY( 26625.38231027395886485464870929718017578125,
- 40524.0090949436053051613271236419677734375,
- -271.5313105642117079696618020534515380859375);
- *up = MULTIPLY( -11278.3751445417292416095733642578125,
- -26409.91773157499847002327442169189453125,
- 34100.5706543184860493056476116180419921875);
- *vp = MULTIPLY( 162829.60100012840121053159236907958984375,
- -123829.313212639070115983486175537109375,
- -28411.65702312920984695665538311004638671875);
-#undef MULTIPLY
-}
+MATRIX_MULTIPLY_FUNCTIONS(ciexyz_to_scaled_yuv,
+ 26625.38231027395886485464870929718017578125,
+ 40524.0090949436053051613271236419677734375,
+ -271.5313105642117079696618020534515380859375,
+ -11278.3751445417292416095733642578125,
+ -26409.91773157499847002327442169189453125,
+ 34100.5706543184860493056476116180419921875,
+ 162829.60100012840121053159236907958984375,
+ -123829.313212639070115983486175537109375,
+ -28411.65702312920984695665538311004638671875)
-static inline void
-ciexyz_to_scaled_yuv_f(float x, float y, float z, float *yp, float *up, float *vp)
-{
-#define MULTIPLY(CX, CY, CZ) ((float)(CX) * x + (float)(CY) * y + (float)(CZ) * z)
- *yp = MULTIPLY( 26625.38231027395886485464870929718017578125,
- 40524.0090949436053051613271236419677734375,
- -271.5313105642117079696618020534515380859375);
- *up = MULTIPLY( -11278.3751445417292416095733642578125,
- -26409.91773157499847002327442169189453125,
- 34100.5706543184860493056476116180419921875);
- *vp = MULTIPLY( 162829.60100012840121053159236907958984375,
- -123829.313212639070115983486175537109375,
- -28411.65702312920984695665538311004638671875);
-#undef MULTIPLY
-}
+#undef MATRIX_MULTIPLY_FUNCTIONS
+#undef MATRIX_MULTIPLY_FUNCTION
diff --git a/src/util/io.h b/src/util/io.h
index 44c0a92..2548a3c 100644
--- a/src/util/io.h
+++ b/src/util/io.h
@@ -40,10 +40,10 @@ enreadall(int status, int fd, void *buf, size_t n, const char *fname)
return (size_t)r;
}
-int pwriteall(int fd, void *buf, size_t n, size_t ptr);
+int pwriteall(int fd, void *buf, size_t n, off_t ptr);
static inline void
-enpwriteall(int status, int fd, void *buf, size_t n, size_t ptr, const char *fname)
+enpwriteall(int status, int fd, void *buf, size_t n, off_t ptr, const char *fname)
{
if (pwriteall(fd, buf, n, ptr))
enprintf(status, "pwrite %s:", fname);