void filterIcy(Img *img, double size, int withWind) {
if (!img->data) return;
size = fabs(size);
if (!(size > IMG_PRECISION)) withWind = FALSE;
const int optNoiseWidth0 = (int)round(size/4);
const int optNoiseWidth1 = (int)round(size);
const double optNoise0 = 1;
const double optNoise1 = 0.75;
const double optBright0 = 1.1;
const double optBright1 = 1.4;
const double optWind = 0.75;
const int optWindWidth0 = (int)round(size/4);
const int optWindWidth1 = (int)round(size/2);
const double optWindNoise0 = 0.5;
const double optWindNoise1 = 0.75;
const double optWindAtt = withWind ? pow(0.95, 4/size) : 0;
double curve[][3] = {
{ 0 , 0 , 0 },
{ 0.15, 0.65, 0 },
{ 0.35, 0.15, 0 },
{ 0.65, 0.85, 0 },
{ 0.85, 0.35, 0 },
{ 1 , 1 , 0 } };
double curveColor[][5] = {
{ 0 , 0 , 0 , 0 , 1 },
{ 0.6, 0.4, 0.4, 0.6, 1 },
{ 1 , 1 , 1 , 1 , 1 } };
double blurSize = size/2;
if (optNoiseWidth0) filterMulNoiseGray(img, img->w/optNoiseWidth0, img->h/optNoiseWidth0, 1-optNoise0, 1);
if (optNoiseWidth1) filterMulNoiseGray(img, img->w/optNoiseWidth1, img->h/optNoiseWidth1, 1-optNoise1, 1);
filterFillCh(img, 3, 1);
filterBlurGauss(img, blurSize, blurSize);
ClMat m;
clmMulColor(&m, optBright0);
filterMatrix(img, &m);
Img imgC = {};
imgInit(&imgC, 512, 1);
curve[0][0] = curve[1][0]*(1 - 4/size);
filterRasterizeTCurve(&imgC, curve[0], sizeof(curve)/sizeof(*curve), curve[0][0], 1);
filterFillCh(&imgC, 3, 1);
filterCurveByCh(img, &imgC, 0);
clmMulColor(&m, optBright1);
filterMatrix(img, &m);
if (withWind) {
Img wind = {};
imgCopy(&wind, img);
Img pat = {};
imgInit(&pat, img->w, 1);
double clOne[] = {1,1,1,1};
filterFill(&pat, clOne);
if (optWindWidth0) filterMulNoiseGray(&pat, img->w/optWindWidth0, 1, 1-optWindNoise0, 1);
if (optWindWidth1) filterMulNoiseGray(&pat, img->w/optWindWidth1, 1, 1-optWindNoise1, 1);
filterWind(&wind, &pat, optWindAtt);
imgDestroy(&pat);
filterBlend(&wind, img, &bfuncSum, -1);
filterBlend(img, &wind, &bfuncSum, optWind);
imgDestroy(&wind);
}
filterRasterizeCurveColor(&imgC, curveColor[0], sizeof(curveColor)/sizeof(*curveColor), 0, 1);
filterCurveByCh(img, &imgC, 0);
imgDestroy(&imgC);
//clmScreenToCompositeYUV(&m);
//filterMatrix(img, &m);
filterScreenToCompositeHSV(img);
}
int icyGenerate(Img *img, int argc, char **argv) {
if (img) imgDestroy(img);
char *inFile = "", *outFile = "";
double size = 4;
int withWind = TRUE;
int withoutWind = FALSE;
ArgDesc descs[] = {
{ 'i', &inFile, &pargString, "path to input image" },
{ 'o', &outFile, &pargString, "path to output image" },
{ 's', &size, &pargDouble, "effect size" },
{ 'w', &withWind, NULL, "add icicles (default)" },
{ 'n', &withoutWind, NULL, "no icicles" },
{}
};
if (!parseArgs(descs, argc, argv) || !inFile[0] || !outFile[0]) {
printUsage(descs);
return FALSE;
}
if (withoutWind) withWind = FALSE;
Img imgC = {};
if (!imgLoad(&imgC, inFile))
return FALSE;
filterIcy(&imgC, size, withWind);
if (!imgSave(&imgC, outFile))
imgDestroy(&imgC);
int success = !!imgC.data;
if (img) imgSwap(img, &imgC);
imgDestroy(&imgC);
return success;
}