Blob Blame Raw

#include <dirent.h>
#include <sys/stat.h>

#include "private.h"
#include "drawing.h"


int heliInitialized;
HeliArray heliObjectsSet;


struct _Directory {
	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 } };

static char *colors[][2] = {
	{ "transparent", "0 0 0 0" },
	
	{ "black",       "0 0 0 1" },
	{ "white",       "1 1 1 1" },
	{ "gray",        "0.5 0.5 0.5 1" },
	
	{ "red",         "1 0 0 1" },
	{ "green",       "0 1 0 1" },
	{ "blue",        "0 0 1 1" },
	
	{ "yellow",      "1 1 0 1" },
	{ "magenta",     "1 0 1 1" },
	{ "cyan",        "0 1 1 1" },
	
	{ "brown",       "0.5 0.5 0 1" },
};


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 openDirectory(const char *path) {
	if (!heliInitialized) return NULL;
	
	DIR *dir = opendir(path);
	if (!dir) return NULL;
	
	Directory d = calloc(1, sizeof(*d));
	
	struct dirent *ent;
	while((ent = readdir(dir)) != NULL) {
		if ( !ent->d_name[0]
		  || strcmp(ent->d_name, ".") == 0
		  || strcmp(ent->d_name, "..") == 0 ) continue;
		heliStringmapAdd(&d->files, ent->d_name, NULL, NULL);
	}
	closedir(dir);
	
	heliObjectRegister(d, (HeliFreeCallback)&closeDirectory);
	return d;
}

void closeDirectory(Directory directory) {
	if (!directory) return;
	heliObjectUnregister(directory);
	heliArrayDestroy(&directory->files);
	free(directory);
}

int directoryGetCount(Directory directory)
	{ return directory->files.count; }

const char* directoryGet(Directory directory, int i)
	{ return (const char*)heliArrayGetKey(&directory->files, i); }


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 heliStringCompareCi(const char *a, const char *b) {
	if (!a && !b) return 0;
	if (!a) return -1;
	if (!b) return 1;
	while(*a || *b) {
		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;
	}
	return 0;
}

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 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 colorByName(const char *colorName)
	{ return colorByNameA(colorName, 1); }
unsigned int colorByNameA(const char *colorName, double a) {
	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( a*(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][0]) == 0)
				return colorByNameA(colors[i][1], a);
	} else {
		double r = 0, g = 0, b = 0, aa = 1;
		sscanf(x, "%lf %lf %lf %lf", &r, &g, &b, &aa);
		code = colorByRGBA(r, g, b, aa*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);
	}
}