#include <dirent.h>
#include <sys/stat.h>
#include "private.h"
#include "drawing.h"
int heliInitialized;
HeliArray heliObjectsSet;
struct _Directory {
size_t pathLen;
HeliArray files;
};
static const float yuvToRgb[3][3] = {
{ 1.0, 0.0 , 1.402 },
{ 1.0, -0.344136, -0.714136 },
{ 1.0, 1.772 , 0.0 } };
static const float rgbToYuv[3][3] = {
{ 0.299 , 0.587 , 0.114 },
{ -0.168736, -0.331264, 0.5 },
{ 0.5 , -0.418688, -0.081312 } };
#include "colors.inc.h"
int randomNumber(int min, int max)
{ return max <= min ? min : rand()%(max - min + 1) + min; }
double randomFloat()
{ return (double)rand()/(double)RAND_MAX; }
void rotateVector(double *x, double *y, double angle) {
double a = angle*PI/180.0;
double xx = x ? *x : 0.0;
double yy = y ? *y : 0.0;
double c = cos(a);
double s = sin(a);
if (x) *x = xx*c - yy*s;
if (y) *y = xx*s + yy*c;
}
Directory openDirectoryEx(
const char *path,
const char *prefix,
const char *suffix,
int caseSensitive,
int showFiles,
int showDirectories )
{
if (!heliInitialized) return NULL;
if (!path) return NULL;
DIR *dir = opendir(path);
if (!dir) return NULL;
Directory d = calloc(1, sizeof(*d));
d->pathLen = strlen(path);
size_t prefixLen = prefix ? strlen(prefix) : 0u;
size_t suffixLen = suffix ? strlen(suffix) : 0u;
size_t bufLen = d->pathLen + 1024;
char *buf = calloc(1, bufLen);
strcpy(buf, path);
if (d->pathLen > 0 && buf[d->pathLen - 1] != '\\' && buf[d->pathLen - 1] != '/') {
char lastSlash = '/';
for(const char *c = buf; *c; ++c)
if (*c == '\\' || *c == '/') lastSlash = *c;
buf[d->pathLen++] = lastSlash;
}
struct dirent *ent;
while((ent = readdir(dir)) != NULL) {
if ( !ent->d_name[0]
|| strcmp(ent->d_name, ".") == 0
|| strcmp(ent->d_name, "..") == 0 ) continue;
if (prefixLen) {
if ( caseSensitive ? strncmp(prefix, ent->d_name, prefixLen)
: heliStringCompareNCi(prefix, ent->d_name, prefixLen) )
continue;
}
size_t nameLen = strlen(ent->d_name);
if (suffixLen) {
if (nameLen < suffixLen) continue;
const char *sub = ent->d_name + nameLen - suffixLen;
if ( caseSensitive ? strcmp(suffix, sub)
: heliStringCompareCi(suffix, sub) )
continue;
}
if (bufLen <= d->pathLen + nameLen) {
bufLen = d->pathLen + nameLen + 1024;
buf = realloc(buf, bufLen);
}
strcpy(buf + d->pathLen, ent->d_name);
if ( (showFiles && fileExists(buf))
|| (showDirectories && directoryExists(buf)) )
heliStringmapAdd(&d->files, buf, NULL, NULL);
}
closedir(dir);
heliObjectRegister(d, (HeliFreeCallback)&closeDirectory);
return d;
}
Directory openDirectory(const char *path)
{ return openDirectoryEx(path, NULL, NULL, TRUE, TRUE, TRUE); }
void closeDirectory(Directory directory) {
if (!directory) return;
heliObjectUnregister(directory);
heliArrayDestroy(&directory->files);
free(directory);
}
int directoryGetCount(Directory directory)
{ return directory->files.count; }
const char* directoryGetFull(Directory directory, int i)
{ return (const char*)heliArrayGetKey(&directory->files, i); }
const char* directoryGet(Directory directory, int i) {
const char *path = directoryGetFull(directory, i);
return path ? path + directory->pathLen : NULL;
}
int fileExists(const char *path) {
struct stat s = {};
return stat(path, &s) == 0 && !(s.st_mode & S_IFDIR);
}
int directoryExists(const char *path) {
struct stat s = {};
return stat(path, &s) == 0 && (s.st_mode & S_IFDIR);
}
char* heliStringCopy(const char *x) {
int len = strlen(x) + 1;
char *cp = malloc(len);
memcpy(cp, x, len);
return cp;
}
char* heliStringCopyLower(const char *x) {
char *xx = heliStringCopy(x);
heliStringLower(xx);
return xx;
}
char* heliStringConcat(const char *a, const char *b) {
int la = strlen(a);
int lb = strlen(b);
char *s = malloc(la + lb + 1);
memcpy(s, a, la);
memcpy(s + la, b, lb);
s[la + lb] = 0;
return s;
}
char* heliStringConcat3(const char *a, const char *b, const char *c) {
int la = strlen(a);
int lb = strlen(b);
int lc = strlen(c);
char *s = malloc(la + lb + lc + 1);
memcpy(s, a, la);
memcpy(s + la, b, lb);
memcpy(s + la + lb, c, lc);
s[la + lb + lc] = 0;
return s;
}
int heliStringCompareNCi(const char *a, const char *b, size_t n) {
if (!a && !b) return 0;
if (!a) return -1;
if (!b) return 1;
while((*a || *b) && n) {
if (!a) return -1;
if (!b) return 1;
char aa = tolower(*a);
char bb = tolower(*b);
if (aa < bb) return -1;
if (bb < aa) return 1;
++a, ++b, --n;
}
return 0;
}
int heliStringCompareCi(const char *a, const char *b)
{ return heliStringCompareNCi(a, b, SIZE_MAX); }
int heliStringEndsWithLowcase(const char *s, const char *tail) {
int ls = strlen(s);
int lt = strlen(tail);
if (lt > ls) return FALSE;
for(int i = 0; i < lt; ++i)
if (tolower(s[ls - i]) != tolower(tail[lt - i]))
return FALSE;
return TRUE;
}
void heliStringLower(char *x)
{ while(*x) { *x = tolower(*x); ++x; } }
static double colorClamp(double c)
{ return c > 0 ? (c < 1 ? c : 1) : 0; }
static unsigned int colorToInt(double c)
{ return (unsigned int)floor(colorClamp(c)*255.99); }
unsigned int colorWithAlpha(unsigned int colorCode, double a)
{ return (0xffffff00 & colorCode) | colorToInt(colorGetAlpha(colorCode)*a); }
unsigned int colorByRGBA(double r, double g, double b, double a) {
return (colorToInt(r) << 24)
| (colorToInt(g) << 16)
| (colorToInt(b) << 8)
| colorToInt(a);
}
unsigned int colorByYUVA(double y, double u, double v, double a) {
return colorByRGBA(
yuvToRgb[0][0]*y + yuvToRgb[0][1]*u + yuvToRgb[0][2]*v,
yuvToRgb[1][0]*y + yuvToRgb[1][1]*u + yuvToRgb[1][2]*v,
yuvToRgb[2][0]*y + yuvToRgb[2][1]*u + yuvToRgb[2][2]*v,
a );
}
unsigned int colorByHSVA(double h, double s, double v, double a) {
h -= floor(h/360)*360;
h /= 60.0;
int i = (int)h;
double f = h - i;
double p = v*(1 - s);
double q = v*(1 - s*f);
double t = v*(1 - s*(1 - f));
switch(i) {
case 0: return colorByRGBA(v, t, p, a);
case 1: return colorByRGBA(q, v, p, a);
case 2: return colorByRGBA(p, v, t, a);
case 3: return colorByRGBA(p, q, v, a);
case 4: return colorByRGBA(t, p, v, a);
case 5: return colorByRGBA(v, p, q, a);
}
return colorByRGBA(v, t, p, a);
}
unsigned int colorByRGB(double r, double g, double b)
{ return colorByRGBA(r, g, b, 1); }
unsigned int colorByHSV(double h, double s, double v)
{ return colorByHSVA(h, s, v, 1); }
unsigned int colorByYUV(double y, double u, double v)
{ return colorByYUVA(y, u, v, 1); }
double colorGetRed (unsigned int colorCode) { return ( colorCode >> 24 )/255.0; }
double colorGetGreen(unsigned int colorCode) { return ((colorCode >> 16) & 0xFFu)/255.0; }
double colorGetBlue (unsigned int colorCode) { return ((colorCode >> 8) & 0xFFu)/255.0; }
double colorGetAlpha(unsigned int colorCode) { return ( colorCode & 0xFFu)/255.0; }
void heliColorToDouble(unsigned int code, double *rgba) {
rgba[0] = colorGetRed (code);
rgba[1] = colorGetGreen(code);
rgba[2] = colorGetBlue (code);
rgba[3] = colorGetAlpha(code);
}
double colorGetYLuminance(unsigned int colorCode) {
return rgbToYuv[0][0]*colorGetRed (colorCode)
+ rgbToYuv[0][1]*colorGetGreen(colorCode)
+ rgbToYuv[0][2]*colorGetBlue (colorCode);
}
double colorGetUChrominance(unsigned int colorCode) {
return rgbToYuv[1][0]*colorGetRed (colorCode)
+ rgbToYuv[1][1]*colorGetGreen(colorCode)
+ rgbToYuv[1][2]*colorGetBlue (colorCode);
}
double colorGetVChrominance(unsigned int colorCode) {
return rgbToYuv[1][0]*colorGetRed (colorCode)
+ rgbToYuv[1][1]*colorGetGreen(colorCode)
+ rgbToYuv[1][2]*colorGetBlue (colorCode);
}
static int colorMin(double *c)
{ return c[0] < c[1] ? (c[0] < c[2] ? 0 : 2) : (c[1] < c[2] ? 1 : 2); }
static int colorMax(double *c)
{ return c[0] > c[1] ? (c[0] > c[2] ? 0 : 2) : (c[1] > c[2] ? 1 : 2); }
double colorGetHue(unsigned int colorCode) {
double rgb[] = { colorGetRed (colorCode),
colorGetGreen(colorCode),
colorGetBlue (colorCode) };
int cmin = colorMin(rgb);
int cmax = colorMax(rgb);
double d = rgb[cmax] - rgb[cmin];
double h = 0;
if (d > HELI_PRECISION) {
d = 1.0/d;
switch(cmax){
case 0: h = (rgb[1] - rgb[2])*d + 0; break;
case 1: h = (rgb[2] - rgb[0])*d + 2; break;
case 2: h = (rgb[0] - rgb[1])*d + 4; break;
}
h /= 6; h -= floor(h); h *= 360;
}
return h;
}
double colorGetSaturation(unsigned int colorCode) {
double rgb[] = { colorGetRed (colorCode),
colorGetGreen(colorCode),
colorGetBlue (colorCode) };
int cmin = colorMin(rgb);
int cmax = colorMax(rgb);
return rgb[cmax] > HELI_PRECISION ? (rgb[cmax] - rgb[cmin])/rgb[cmax] : 0;
}
double colorGetValue(unsigned int colorCode) {
double rgb[] = { colorGetRed (colorCode),
colorGetGreen(colorCode),
colorGetBlue (colorCode) };
return rgb[colorMax(rgb)];
}
unsigned int colorByNameA(const char *colorName, double a)
{ return colorWithAlpha(colorByName(colorName), a); }
unsigned int colorByName(const char *colorName) {
unsigned int code = 0;
const char *x = colorName;
if (*x == '#') {
++x;
int hex[8] = { 0, 0, 0, 0, 0, 0, 15, 15 };
for(int i = 0; *x && i < 8; ++i, ++x) {
char c = tolower(*x);
if (c >= '0' && c <= '9') hex[i] = c - '0';
if (c >= 'a' && c <= 'f') hex[i] = c - 'a' + 10;
}
for(int i = 0; i < 6; ++i)
code |= ( hex[i] << ((7-i)*4) );
code |= colorToInt( (hex[6]*16 + hex[7])/255.0 );
} else
if (isalpha(*x)) {
int count = (int)(sizeof(colors)/sizeof(*colors));
for(int i = 0; i < count; ++i)
if (heliStringCompareCi(x, colors[i].name) == 0)
return colors[i].code;
} else {
double r = 0, g = 0, b = 0, a = 1;
sscanf(x, "%lf %lf %lf %lf", &r, &g, &b, &a);
code = colorByRGBA(r, g, b, a);
}
return code;
}
static int objectsCompare(const void *a, const void *b)
{ return a < b ? -1 : b < a ? 1 : 0; }
static void* objectsKeyClone(void *k)
{ return k; }
void heliObjectRegister(void *o, HeliFreeCallback fo)
{ heliMapAdd(&heliObjectsSet, o, &objectsCompare, (HeliCloneCallback)objectsKeyClone, fo, NULL, NULL); }
void heliObjectUnregister(void *o) {
int i;
HeliPair *item = heliMapFind(&heliObjectsSet, o, &objectsCompare, &i);
if (item) {
item->freeKey = NULL;
heliArrayRemove(&heliObjectsSet, i);
}
}