removed all exit() calls from functions compiled into shared library. palette failures result in white palettes, smooth interpolation falls back to linear instead of exiting on first/last segments, etc.

git-svn-id: https://flam3.googlecode.com/svn/trunk@35 77852712-ef1d-11de-8684-7d64432d61a3
This commit is contained in:
Erik Reckase 2010-11-18 04:16:25 +00:00 committed by Scott Draves
parent ac95d6e473
commit 26f5c53f09
4 changed files with 87 additions and 44 deletions

View File

@ -704,6 +704,7 @@ void flam3_interpolate(flam3_genome cps[], int ncps,
int i1, i2;
double c[2];
flam3_genome cpi[4];
int smoothflag = 0;
if (1 == ncps) {
flam3_copy(result, &(cps[0]));
@ -735,17 +736,25 @@ void flam3_interpolate(flam3_genome cps[], int ncps,
/* and ensure that they both have the same number before progressing */
if (flam3_interpolation_linear == cps[i1].interpolation) {
flam3_align(&cpi[0], &cps[i1], 2);
smoothflag = 0;
} else {
if (0 == i1) {
fprintf(stderr, "error: cannot use smooth interpolation on first segment.\n");
exit(1);
fprintf(stderr, "error: cannot use smooth interpolation on first segment.\n");
fprintf(stderr, "reverting to linear interpolation.\n");
flam3_align(&cpi[0], &cps[i1], 2);
smoothflag = 0;
}
if (ncps-1 == i2) {
fprintf(stderr, "error: cannot use smooth interpolation on last segment.\n");
exit(1);
fprintf(stderr, "error: cannot use smooth interpolation on last segment.\n");
fprintf(stderr, "reverting to linear interpolation.\n");
flam3_align(&cpi[0], &cps[i1], 2);
smoothflag = 0;
}
flam3_align(&cpi[0], &cps[i1-1], 4);
smoothflag = 1;
}
/* Clear the destination cp */
@ -763,7 +772,7 @@ void flam3_interpolate(flam3_genome cps[], int ncps,
result->interpolation_type = cpi[0].interpolation_type;
result->palette_interpolation = flam3_palette_interpolation_hsv;
if (flam3_interpolation_linear == cps[i1].interpolation) {
if (!smoothflag) {
flam3_interpolate_n(result, 2, cpi, c, stagger);
} else {
interpolate_catmull_rom(cpi, c[1], result);
@ -1355,9 +1364,10 @@ int flam3_count_nthreads(void) {
kr = host_info(host, HOST_BASIC_INFO, (host_info_t)&hi, &size);
if (kr != KERN_SUCCESS) {
mach_error("host_info():", kr);
exit(EXIT_FAILURE);
}
nthreads = hi.avail_cpus;
/* set threads to 1 on error */
nthreads = 1;
} else
nthreads = hi.avail_cpus;
#else
#ifndef _SC_NPROCESSORS_ONLN
char line[MAXBUF];
@ -1501,9 +1511,8 @@ flam3_genome * flam3_parse_from_file(FILE *f, char *fname, int default_flag, int
slen *= 2;
snew = realloc(s, slen);
if (snew==NULL) {
fprintf(stderr,"XML file too large to be read - aborting.\n");
free(s);
exit(1);
fprintf(stderr,"XML file too large to be read. continuing with partial file.\n");
break;
} else
s = snew;
}
@ -2882,7 +2891,11 @@ void flam3_mutate(flam3_genome *cp, int mutate_mode, int *ivars, int ivars_n, in
} else { /* randomize palette only */
cp->palette_index = flam3_get_palette(flam3_palette_random, cp->palette, cp->hue_rotation);
add_to_action(action,"mutate color palette");
/* if our palette retrieval fails, skip the mutation */
if (cp->palette_index >= 0)
add_to_action(action,"mutate color palette");
else
fprintf(stderr,"failure getting random palette, palette set to white\n");
}
} else if (mutate_mode == MUTATE_DELETE_XFORM) {
@ -2947,6 +2960,8 @@ void flam3_random(flam3_genome *cp, int *ivars, int ivars_n, int sym, int spec_x
cp->hue_rotation = (random()&7) ? 0.0 : flam3_random01();
cp->palette_index = flam3_get_palette(flam3_palette_random, cp->palette, cp->hue_rotation);
if (cp->palette_index < 0)
fprintf(stderr,"error getting palette from xml file, setting to all white\n");
cp->time = 0.0;
cp->interpolation = flam3_interpolation_linear;
cp->palette_interpolation = flam3_palette_interpolation_hsv;

View File

@ -150,10 +150,14 @@ void interpolate_cmap(flam3_palette cmap, double blend,
int index0, double hue0, int index1, double hue1) {
flam3_palette p0,p1;
int i, j;
int i, j, rcode;
flam3_get_palette(index0, p0, hue0);
flam3_get_palette(index1, p1, hue1);
rcode = flam3_get_palette(index0, p0, hue0);
if (rcode<0)
fprintf(stderr,"unable to retrieve palette %d, setting to white\n", index0);
rcode = flam3_get_palette(index1, p1, hue1);
if (rcode<0)
fprintf(stderr,"unable to retrieve palette %d, setting to white\n", index1);
for (i = 0; i < 256; i++) {
double t[5], s[5];

View File

@ -26,6 +26,7 @@ static void parse_palettes(xmlNode *node) {
xmlAttrPtr attr;
char *val;
lib_palette *pal;
int hex_error;
while (node) {
if (node->type == XML_ELEMENT_NODE && !xmlStrcmp(node->name, (const xmlChar *)"palette")) {
@ -44,12 +45,14 @@ static void parse_palettes(xmlNode *node) {
c_idx=0;
col_count = 0;
hex_error = 0;
do {
sscanf_ret = sscanf((char *)&(val[c_idx]),"00%2x%2x%2x",&r,&g,&b);
if (sscanf_ret != 3) {
printf("Error: Problem reading hexadecimal color data.\n");
exit(1);
fprintf(stderr,"error: problem reading hexadecimal color data '%8s'\n",&val[c_idx]);
hex_error = 1;
break;
}
c_idx += 8;
while (isspace( (int)val[c_idx]))
@ -72,8 +75,10 @@ static void parse_palettes(xmlNode *node) {
attr = attr->next;
}
npalettes++;
the_palettes = realloc(the_palettes, (1 + npalettes) * sizeof(lib_palette));
if (hex_error == 0) {
npalettes++;
the_palettes = realloc(the_palettes, (1 + npalettes) * sizeof(lib_palette));
}
} else
parse_palettes(node->children);
@ -85,14 +90,14 @@ static int init_palettes(char *filename) {
FILE *fp;
xmlDocPtr doc;
xmlNode *rootnode;
int i, c, slen = 5000;
char *s;
int i, c, slen = 5000;
char *s;
fp = fopen(filename, "rb");
if (NULL == fp) {
fprintf(stderr, "flam3: could not open palette file ");
perror(filename);
exit(1);
fprintf(stderr, "flam3: could not open palette file ");
perror(filename);
return(-1);
}
/* Incrementally read XML file into a string */
@ -101,10 +106,10 @@ static int init_palettes(char *filename) {
do {
c = getc(fp);
if (EOF == c) {
if (ferror(fp)) {
perror(filename);
exit(1);
}
if (ferror(fp)) {
perror(filename);
return(-1);
}
break;
}
s[i++] = c;
@ -120,7 +125,7 @@ static int init_palettes(char *filename) {
doc = xmlReadMemory(s, (int)strlen(s), filename, NULL, XML_PARSE_NONET);
if (NULL == doc) {
fprintf(stderr, "error parsing %s (%s).\n", filename, s);
exit(1);
return(-1);
}
rootnode = xmlDocGetRootElement(doc);
the_palettes = malloc(sizeof(lib_palette));
@ -135,12 +140,22 @@ static int init_palettes(char *filename) {
int flam3_get_palette(int n, flam3_palette c, double hue_rotation) {
int cmap_len = 256;
int idx, i, j;
int idx, i, j, rcode;
if (NULL == the_palettes) {
// set palette to all white in case there are problems
for (i = 0; i < cmap_len; i++) {
c[i].index = i;
for (j = 0; j < 4; j++)
c[i].color[j] = 1.0;
}
if (NULL == the_palettes) {
char *d = getenv("flam3_palettes");
init_palettes(d ? d : (PACKAGE_DATA_DIR "/flam3-palettes.xml"));
rcode = init_palettes(d ? d : (PACKAGE_DATA_DIR "/flam3-palettes.xml"));
if (rcode<0) {
fprintf(stderr,"error reading xml palette file, setting to all white\n");
return(-1);
}
}
if (flam3_palette_random == n)
@ -176,13 +191,7 @@ int flam3_get_palette(int n, flam3_palette c, double hue_rotation) {
fprintf(stderr, "warning: palette number %d not found, using white.\n", n);
for (i = 0; i < cmap_len; i++) {
c[i].index = i;
for (j = 0; j < 4; j++)
c[i].color[j] = 1.0;
}
return flam3_palette_random;
return(-1);
}
/* rgb 0 - 1,
@ -398,7 +407,7 @@ static double try_colors(flam3_genome *g, int color_resolution) {
image = (unsigned char *) calloc(g->width * g->height, 3);
if (flam3_render(&f, image, flam3_field_both, 3, 0, &stats)) {
fprintf(stderr,"Error rendering test image for trycolors. Aborting.\n");
exit(1);
return(-1);
}
hist = calloc(sizeof(int), res3);
@ -443,7 +452,7 @@ static double try_colors(flam3_genome *g, int color_resolution) {
/* Free xform storage */
clear_cp(&saved,flam3_defaults_on);
return (double) hits / res3;
return (double) (hits / res3);
}
static void change_colors(flam3_genome *g, int change_palette) {
@ -452,6 +461,8 @@ static void change_colors(flam3_genome *g, int change_palette) {
if (change_palette) {
g->hue_rotation = 0.0;
g->palette_index = flam3_get_palette(flam3_palette_random, g->palette, 0.0);
if (g->palette_index < 0)
fprintf(stderr,"error retrieving random palette, setting to all white\n");
}
for (i = 0; i < g->num_xforms; i++) {
g->xform[i].color = flam3_random01();
@ -468,16 +479,27 @@ void flam3_improve_colors(flam3_genome *g, int ntries, int change_palette, int c
flam3_genome best_genome;
memset(&best_genome, 0, sizeof(flam3_genome));
best = try_colors(g, color_resolution);
if (best<0) {
fprintf(stderr,"error in try_colors, skipping flam3_improve_colors\n");
return;
}
flam3_copy(&best_genome,g);
for (i = 0; i < ntries; i++) {
change_colors(g, change_palette);
b = try_colors(g, color_resolution);
if (b < 0) {
fprintf(stderr,"error in try_colors, aborting tries\n");
break;
}
if (b > best) {
best = b;
flam3_copy(&best_genome,g);
}
}
flam3_copy(g,&best_genome);
clear_cp(&best_genome,flam3_defaults_on);
}

View File

@ -253,9 +253,11 @@ void scan_for_flame_nodes(xmlNode *cur_node, char *parent_file, int default_flag
/* Clear out the realloc'd memory */
memset(&(genome_storage[*all_ncps]),0,sizeof(flam3_genome));
if (loc_current_cp.palette_index != flam3_palette_random) {
flam3_get_palette(loc_current_cp.palette_index, loc_current_cp.palette,
if (loc_current_cp.palette_index != -1) {
col_success = flam3_get_palette(loc_current_cp.palette_index, loc_current_cp.palette,
loc_current_cp.hue_rotation);
if (col_success < 0)
fprintf(stderr,"error retrieving palette %d, setting to all white\n",loc_current_cp.palette_index);
}
col_success = flam3_interp_missing_colors(&loc_current_cp);