aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.h
blob: 3adc1b5d09f04111e9cb42b8b9408984b07a17cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* See LICENSE file for copyright and license details. */

#include <math.h>
#include <stdio.h>

#define ELEMENTSOF(ARRAY) (sizeof(ARRAY) / sizeof(*(ARRAY)))

void eprintf(const char *fmt, ...);
void enprintf(int status, const char *fmt, ...);
void weprintf(const char *fmt, ...);

int tollu(const char *s, unsigned long long int min, unsigned long long int max, unsigned long long int *out);
int tolli(const char *s, long long int min, long long int max, long long int *out);
#define DEF_STR_TO_INT(FNAME, INTTYPE, INTER_FNAME, INTER_INTTYPE)\
	static inline int\
	FNAME(const char *s, INTTYPE min, INTTYPE max, INTTYPE *out)\
	{\
		INTER_INTTYPE inter;\
		if (INTER_FNAME(s, (INTER_INTTYPE)min, (INTER_INTTYPE)max, &inter))\
			return -1;\
		*out = (INTTYPE)inter;\
		return 0;\
	}
DEF_STR_TO_INT(tolu, unsigned long int, tollu, unsigned long long int)
DEF_STR_TO_INT(tou, unsigned int, tollu, unsigned long long int)
DEF_STR_TO_INT(toli, long int, tolli, long long int)
DEF_STR_TO_INT(toi, int, tolli, long long int)
#undef DEF_STR_TO_INT
#define tozu tolu
#define tozi toli
#define toju tollu
#define toji tolli

int fshut(FILE *fp, const char *fname);
void enfshut(int status, FILE *fp, const char *fname);
void efshut(FILE *fp, const char *fname);

static inline double
srgb_encode(double t)
{
	double sign = 1;
	if (t < 0) {
		t = -t;
		sign = -1;
	}
	t = t <= 0.0031306684425217108 ? 12.92 * t : 1.055 * pow(t, 1 / 2.4) - 0.055;
	return t * sign;
}

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;
}