1 #include "Misc.h"
2
3 #include "Config.h"
4 #include "icondoc.h"
5
6 #include <cstring>
7 #include <sys/types.h>
8 #include <cmath>
9
10 #include "common/tpt-minmax.h"
11
12 //Signum function
isign(float i)13 int isign(float i) //TODO: INline or macro
14 {
15 if (i<0)
16 return -1;
17 if (i>0)
18 return 1;
19 return 0;
20 }
21
clamp_flt(float f,float min,float max)22 unsigned clamp_flt(float f, float min, float max) //TODO: Also inline/macro
23 {
24 if (f<min)
25 return 0;
26 if (f>max)
27 return 255;
28 return (int)(255.0f*(f-min)/(max-min));
29 }
30
restrict_flt(float f,float min,float max)31 float restrict_flt(float f, float min, float max) //TODO Inline or macro or something
32 {
33 if (f<min)
34 return min;
35 if (f>max)
36 return max;
37 return f;
38 }
39
40 const static char hex[] = "0123456789ABCDEF";
strcaturl(char * dst,char * src)41 void strcaturl(char *dst, char *src)
42 {
43 char *d;
44 unsigned char *s;
45
46 for (d=dst; *d; d++) ;
47
48 for (s=(unsigned char *)src; *s; s++)
49 {
50 if ((*s>='0' && *s<='9') ||
51 (*s>='a' && *s<='z') ||
52 (*s>='A' && *s<='Z'))
53 *(d++) = *s;
54 else
55 {
56 *(d++) = '%';
57 *(d++) = hex[*s>>4];
58 *(d++) = hex[*s&15];
59 }
60 }
61 *d = 0;
62 }
63
strappend(char * dst,const char * src)64 void strappend(char *dst, const char *src)
65 {
66 char *d;
67 unsigned char *s;
68
69 for (d=dst; *d; d++) ;
70
71 for (s=(unsigned char *)src; *s; s++)
72 {
73 *(d++) = *s;
74 }
75 *d = 0;
76 }
77
file_load(char * fn,int * size)78 void *file_load(char *fn, int *size)
79 {
80 FILE *f = fopen(fn, "rb");
81 void *s;
82
83 if (!f)
84 return NULL;
85 fseek(f, 0, SEEK_END);
86 *size = ftell(f);
87 fseek(f, 0, SEEK_SET);
88 s = malloc(*size);
89 if (!s)
90 {
91 fclose(f);
92 return NULL;
93 }
94 int readsize = fread(s, *size, 1, f);
95 fclose(f);
96 if (readsize != 1)
97 {
98 free(s);
99 return NULL;
100 }
101 return s;
102 }
103
m2d_multiply_m2d(matrix2d m1,matrix2d m2)104 matrix2d m2d_multiply_m2d(matrix2d m1, matrix2d m2)
105 {
106 matrix2d result = {
107 m1.a*m2.a+m1.b*m2.c, m1.a*m2.b+m1.b*m2.d,
108 m1.c*m2.a+m1.d*m2.c, m1.c*m2.b+m1.d*m2.d
109 };
110 return result;
111 }
m2d_multiply_v2d(matrix2d m,vector2d v)112 vector2d m2d_multiply_v2d(matrix2d m, vector2d v)
113 {
114 vector2d result = {
115 m.a*v.x+m.b*v.y,
116 m.c*v.x+m.d*v.y
117 };
118 return result;
119 }
m2d_multiply_float(matrix2d m,float s)120 matrix2d m2d_multiply_float(matrix2d m, float s)
121 {
122 matrix2d result = {
123 m.a*s, m.b*s,
124 m.c*s, m.d*s,
125 };
126 return result;
127 }
128
v2d_multiply_float(vector2d v,float s)129 vector2d v2d_multiply_float(vector2d v, float s)
130 {
131 vector2d result = {
132 v.x*s,
133 v.y*s
134 };
135 return result;
136 }
137
v2d_add(vector2d v1,vector2d v2)138 vector2d v2d_add(vector2d v1, vector2d v2)
139 {
140 vector2d result = {
141 v1.x+v2.x,
142 v1.y+v2.y
143 };
144 return result;
145 }
v2d_sub(vector2d v1,vector2d v2)146 vector2d v2d_sub(vector2d v1, vector2d v2)
147 {
148 vector2d result = {
149 v1.x-v2.x,
150 v1.y-v2.y
151 };
152 return result;
153 }
154
m2d_new(float me0,float me1,float me2,float me3)155 matrix2d m2d_new(float me0, float me1, float me2, float me3)
156 {
157 matrix2d result = {me0,me1,me2,me3};
158 return result;
159 }
v2d_new(float x,float y)160 vector2d v2d_new(float x, float y)
161 {
162 vector2d result = {x, y};
163 return result;
164 }
165
HSV_to_RGB(int h,int s,int v,int * r,int * g,int * b)166 void HSV_to_RGB(int h,int s,int v,int *r,int *g,int *b)//convert 0-255(0-360 for H) HSV values to 0-255 RGB
167 {
168 float hh, ss, vv, c, x;
169 int m;
170 hh = h/60.0f;//normalize values
171 ss = s/255.0f;
172 vv = v/255.0f;
173 c = vv * ss;
174 x = c * ( 1 - fabs(fmod(hh,2.0f) -1) );
175 if(hh<1){
176 *r = (int)(c*255.0);
177 *g = (int)(x*255.0);
178 *b = 0;
179 }
180 else if(hh<2){
181 *r = (int)(x*255.0);
182 *g = (int)(c*255.0);
183 *b = 0;
184 }
185 else if(hh<3){
186 *r = 0;
187 *g = (int)(c*255.0);
188 *b = (int)(x*255.0);
189 }
190 else if(hh<4){
191 *r = 0;
192 *g = (int)(x*255.0);
193 *b = (int)(c*255.0);
194 }
195 else if(hh<5){
196 *r = (int)(x*255.0);
197 *g = 0;
198 *b = (int)(c*255.0);
199 }
200 else if(hh<6){
201 *r = (int)(c*255.0);
202 *g = 0;
203 *b = (int)(x*255.0);
204 }
205 m = (int)((vv-c)*255.0);
206 *r += m;
207 *g += m;
208 *b += m;
209 }
210
RGB_to_HSV(int r,int g,int b,int * h,int * s,int * v)211 void RGB_to_HSV(int r,int g,int b,int *h,int *s,int *v)//convert 0-255 RGB values to 0-255(0-360 for H) HSV
212 {
213 float rr, gg, bb, a,x,c,d;
214 rr = r/255.0f;//normalize values
215 gg = g/255.0f;
216 bb = b/255.0f;
217 a = std::min(rr,gg);
218 a = std::min(a,bb);
219 x = std::max(rr,gg);
220 x = std::max(x,bb);
221 if (a==x)//greyscale
222 {
223 *h = 0;
224 *s = 0;
225 *v = (int)(a*255.0);
226 }
227 else
228 {
229 c = (rr==a) ? gg-bb : ((bb==a) ? rr-gg : bb-rr);
230 d = (rr==a) ? 3 : ((bb==a) ? 1 : 5);
231 *h = (int)(60.0*(d - c/(x - a)));
232 *s = (int)(255.0*((x - a)/x));
233 *v = (int)(255.0*x);
234 }
235 }
236
membwand(void * destv,void * srcv,size_t destsize,size_t srcsize)237 void membwand(void * destv, void * srcv, size_t destsize, size_t srcsize)
238 {
239 size_t i;
240 unsigned char * dest = (unsigned char*)destv;
241 unsigned char * src = (unsigned char*)srcv;
242 for(i = 0; i < destsize; i++){
243 dest[i] = dest[i] & src[i%srcsize];
244 }
245 }
246
247 vector2d v2d_zero = {0,0};
248 matrix2d m2d_identity = {1,0,0,1};
249