stamp loading CPU and disk interleaved

This commit is contained in:
Albert Cahalan 2005-01-16 05:46:50 +00:00
parent 094fb9f8aa
commit 6fabe5f456

View file

@ -6496,120 +6496,35 @@ static void load_brush_dir(const char * const dir)
} }
static unsigned default_stamp_size;
static void loadstamp_callback(const char *restrict const dir, unsigned dirlen, tp_ftw_str *files, unsigned i) static void loadstamp_finisher(int i)
{ {
qsort(files, i, sizeof *files, compare_ftw_str);
while(i--)
{
show_progress_bar();
if (strstr(files[i].str, ".png") && !strstr(files[i].str, "_mirror.png"))
{
char fname[512];
snprintf(fname, sizeof fname, "%s/%s", dir, files[i].str);
img_stamps[num_stamps] = loadimage(fname);
txt_stamps[num_stamps] = loaddesc(fname);
inf_stamps[num_stamps] = loadinfo(fname);
img_stamps_premirror[num_stamps] = loadaltimage(fname);
#ifndef NOSOUND
if (use_sound)
snd_stamps[num_stamps] = loadsound(fname);
#endif
num_stamps++;
}
free(files[i].str);
}
free(files);
}
static void load_stamp_dir(const char * const dir)
{
char buf[TP_FTW_PATHSIZE];
unsigned dirlen = strlen(dir);
memcpy(buf,dir,dirlen);
tp_ftw(buf, dirlen, 0, loadstamp_callback);
}
static void load_stamps(void)
{
int i;
char * homedirdir = get_fname("stamps");
load_stamp_dir(homedirdir);
load_stamp_dir(DATA_PREFIX "stamps");
#ifdef __APPLE__
load_stamp_dir("/Library/Application Support/TuxPaint/stamps");
#endif
if (num_stamps == 0)
{
fprintf(stderr,
"\nWarning: No stamps found in " DATA_PREFIX "stamps/\n"
"or %s\n\n", homedirdir);
}
free(homedirdir);
unsigned default_stamp_size = compute_default_scale_factor(1.0);
/* Create stamp thumbnails: */
for (i = 0; i < num_stamps; i++)
{
if (img_stamps[i]->w > 40 ||
img_stamps[i]->h > 40)
{
img_stamp_thumbs[i] = thumbnail(img_stamps[i], 40, 40, 1);
}
else
{
img_stamp_thumbs[i] = NULL;
}
if (img_stamps_premirror[i] != NULL && !disable_stamp_controls)
{
/* Also thumbnail the pre-drawn mirror version, if any: */
if (img_stamps_premirror[i]->w > 40 ||
img_stamps_premirror[i]->h > 40)
{
img_stamp_thumbs_premirror[i] =
thumbnail(img_stamps_premirror[i], 40, 40, 1);
}
else
{
img_stamp_thumbs_premirror[i] = NULL;
}
}
else
{
img_stamps_premirror[i] = NULL;
}
state_stamps[i] = malloc(sizeof(state_type)); state_stamps[i] = malloc(sizeof(state_type));
if (inf_stamps[i] == NULL) if (img_stamps[i]->w > 40 || img_stamps[i]->h > 40)
{ img_stamp_thumbs[i] = thumbnail(img_stamps[i], 40, 40, 1);
/* Didn't load one for this stamp, assume defaults: */ else
img_stamp_thumbs[i] = NULL;
inf_stamps[i] = malloc(sizeof(info_type)); if (img_stamps_premirror[i])
inf_stamps[i]->tintable = 0; {
inf_stamps[i]->colorable = 0; /* Also thumbnail the pre-drawn mirror version, if any: */
inf_stamps[i]->mirrorable = 1; if (img_stamps_premirror[i]->w > 40 || img_stamps_premirror[i]->h > 40)
inf_stamps[i]->flipable = 1; img_stamp_thumbs_premirror[i] = thumbnail(img_stamps_premirror[i], 40, 40, 1);
inf_stamps[i]->ratio = 1.0; else
inf_stamps[i]->tinter = TINTER_NORMAL; img_stamp_thumbs_premirror[i] = NULL;
} }
{ /* If Tux Paint is in mirror-image-by-default mode, mirror, if we can: */
if (mirrorstamps && inf_stamps[i]->mirrorable)
state_stamps[i]->mirrored = 1;
else
state_stamps[i]->mirrored = 0;
state_stamps[i]->flipped = 0;
unsigned int upper = HARD_MAX_STAMP_SIZE; unsigned int upper = HARD_MAX_STAMP_SIZE;
unsigned int lower = 0; unsigned int lower = 0;
@ -6629,7 +6544,6 @@ static void load_stamps(void)
} }
while (--upper); while (--upper);
do do
{ {
scaleparams *s = &scaletable[lower]; scaleparams *s = &scaletable[lower];
@ -6643,7 +6557,6 @@ static void load_stamps(void)
} }
while (++lower < HARD_MAX_STAMP_SIZE); while (++lower < HARD_MAX_STAMP_SIZE);
if(upper<lower) if(upper<lower)
{ {
// this, if it ever happens, is very bad // this, if it ever happens, is very bad
@ -6664,20 +6577,86 @@ static void load_stamps(void)
state_stamps[i]->min = lower; state_stamps[i]->min = lower;
state_stamps[i]->size = mid; state_stamps[i]->size = mid;
state_stamps[i]->max = upper; state_stamps[i]->max = upper;
} }
/* If Tux Paint is in mirror-image-by-default mode, mirror, if we can: */ static void loadstamp_callback(const char *restrict const dir, unsigned dirlen, tp_ftw_str *files, unsigned i)
{
if (mirrorstamps && inf_stamps[i]->mirrorable) qsort(files, i, sizeof *files, compare_ftw_str);
state_stamps[i]->mirrored = 1; while(i--)
else {
state_stamps[i]->mirrored = 0;
state_stamps[i]->flipped = 0;
show_progress_bar(); show_progress_bar();
if (strstr(files[i].str, ".png") && !strstr(files[i].str, "_mirror.png"))
{
char fname[512];
snprintf(fname, sizeof fname, "%s/%s", dir, files[i].str);
txt_stamps[num_stamps] = loaddesc(fname);
inf_stamps[num_stamps] = loadinfo(fname);
img_stamps[num_stamps] = NULL;
if(!mirrorstamps || !disable_stamp_controls || !inf_stamps[num_stamps]->mirrorable)
img_stamps[num_stamps] = loadimage(fname);
img_stamps_premirror[num_stamps] = NULL;
if((mirrorstamps || !disable_stamp_controls) && inf_stamps[num_stamps]->mirrorable)
img_stamps_premirror[num_stamps] = loadaltimage(fname);
if(img_stamps[num_stamps] || img_stamps_premirror[num_stamps])
{
// we have a stamp; finalize it
#ifndef NOSOUND
if (use_sound)
snd_stamps[num_stamps] = loadsound(fname);
#endif
loadstamp_finisher(num_stamps);
num_stamps++; // FIXME: no limit and no resizing right now...
} }
else
{
// we have a failure, abort mission
free(txt_stamps[num_stamps]);
free(inf_stamps[num_stamps]);
free(img_stamps[num_stamps]);
free(img_stamps_premirror[num_stamps]);
}
}
free(files[i].str);
}
free(files);
}
static void load_stamp_dir(const char * const dir)
{
char buf[TP_FTW_PATHSIZE];
unsigned dirlen = strlen(dir);
memcpy(buf,dir,dirlen);
tp_ftw(buf, dirlen, 0, loadstamp_callback);
}
static void load_stamps(void)
{
default_stamp_size = compute_default_scale_factor(1.0);
char * homedirdir = get_fname("stamps");
load_stamp_dir(homedirdir);
load_stamp_dir(DATA_PREFIX "stamps");
#ifdef __APPLE__
load_stamp_dir("/Library/Application Support/TuxPaint/stamps");
#endif
if (num_stamps == 0)
{
fprintf(stderr,
"\nWarning: No stamps found in " DATA_PREFIX "stamps/\n"
"or %s\n\n", homedirdir);
}
free(homedirdir);
} }
@ -10561,51 +10540,39 @@ static char * loaddesc(const char * const fname)
/* Load a file's info: */ /* Load a file's info: */
static info_type * loadinfo(const char * const fname) static info_type * loadinfo(const char * const fname)
{ {
char * dat_fname; char * dat_fname;
char buf[256]; char buf[256];
info_type inf;
info_type * inf_ret;
FILE * fi; FILE * fi;
info_type *inf = malloc(sizeof(info_type));
inf->ratio = 1.0;
/* Clear info struct first: */ inf->colorable = 0;
inf->tintable = 0;
inf.ratio = 1.0; inf->mirrorable = 1;
inf.colorable = 0; inf->flipable = 1;
inf.tintable = 0; inf->tinter = TINTER_NORMAL;
inf.mirrorable = 1;
inf.flipable = 1;
inf.tinter = TINTER_NORMAL;
/* Load info! */
dat_fname = strdup(fname); dat_fname = strdup(fname);
char *pngptr = strstr(dat_fname, ".png");
if (strstr(dat_fname, ".png") != NULL) if (!pngptr) // TODO: see if this can ever happen
{ {
strcpy(strstr(dat_fname, ".png"), ".dat"); free(dat_fname);
return inf;
}
memcpy(pngptr+1, "dat", 3);
fi = fopen(dat_fname, "r"); fi = fopen(dat_fname, "r");
if (!fi)
if (fi == NULL)
{ {
/*
fprintf(stderr, "\nWarning: Couldn't open an info file:\n");
perror(txt_fname);
fprintf(stderr, "\n");
*/
free(dat_fname); free(dat_fname);
return inf;
return NULL;
} }
free(dat_fname); free(dat_fname);
do do
{ {
fgets(buf, sizeof(buf), fi); fgets(buf, sizeof(buf), fi);
@ -10615,9 +10582,9 @@ static info_type * loadinfo(const char * const fname)
strip_trailing_whitespace(buf); strip_trailing_whitespace(buf);
if (strcmp(buf, "colorable") == 0) if (strcmp(buf, "colorable") == 0)
inf.colorable = 1; inf->colorable = 1;
else if (strcmp(buf, "tintable") == 0) else if (strcmp(buf, "tintable") == 0)
inf.tintable = 1; inf->tintable = 1;
else if (!memcmp(buf, "scale", 5) && (isspace(buf[5]) || buf[5]=='=')) else if (!memcmp(buf, "scale", 5) && (isspace(buf[5]) || buf[5]=='='))
{ {
double tmp, tmp2; double tmp, tmp2;
@ -10628,7 +10595,7 @@ static info_type * loadinfo(const char * const fname)
{ {
tmp = strtod(cp,NULL) / 100.0; tmp = strtod(cp,NULL) / 100.0;
if (tmp > 0.0001 && tmp < 10000.0) if (tmp > 0.0001 && tmp < 10000.0)
inf.ratio = tmp; inf->ratio = tmp;
} }
else if (strchr(cp,'/')) else if (strchr(cp,'/'))
{ {
@ -10637,7 +10604,7 @@ static info_type * loadinfo(const char * const fname)
cp++; cp++;
tmp2 = strtod(cp,NULL); tmp2 = strtod(cp,NULL);
if (tmp>0.0001 && tmp<10000.0 && tmp2>0.0001 && tmp2<10000.0 && tmp/tmp2>0.0001 && tmp/tmp2<10000.0) if (tmp>0.0001 && tmp<10000.0 && tmp2>0.0001 && tmp2<10000.0 && tmp/tmp2>0.0001 && tmp/tmp2<10000.0)
inf.ratio = tmp/tmp2; inf->ratio = tmp/tmp2;
} }
else if (strchr(cp,':')) else if (strchr(cp,':'))
{ {
@ -10646,13 +10613,13 @@ static info_type * loadinfo(const char * const fname)
cp++; cp++;
tmp2 = strtod(cp,NULL); tmp2 = strtod(cp,NULL);
if (tmp>0.0001 && tmp<10000.0 && tmp2>0.0001 && tmp2<10000.0 && tmp2/tmp>0.0001 && tmp2/tmp<10000.0) if (tmp>0.0001 && tmp<10000.0 && tmp2>0.0001 && tmp2<10000.0 && tmp2/tmp>0.0001 && tmp2/tmp<10000.0)
inf.ratio = tmp2/tmp; inf->ratio = tmp2/tmp;
} }
else else
{ {
tmp = strtod(cp,NULL); tmp = strtod(cp,NULL);
if (tmp > 0.0001 && tmp < 10000.0) if (tmp > 0.0001 && tmp < 10000.0)
inf.ratio = 1.0 / tmp; inf->ratio = 1.0 / tmp;
} }
} }
else if (!memcmp(buf, "tinter", 6) && (isspace(buf[6]) || buf[6]=='=')) else if (!memcmp(buf, "tinter", 6) && (isspace(buf[6]) || buf[6]=='='))
@ -10662,19 +10629,19 @@ static info_type * loadinfo(const char * const fname)
cp++; cp++;
if (!strcmp(cp,"anyhue")) if (!strcmp(cp,"anyhue"))
{ {
inf.tinter = TINTER_ANYHUE; inf->tinter = TINTER_ANYHUE;
} }
else if (!strcmp(cp,"narrow")) else if (!strcmp(cp,"narrow"))
{ {
inf.tinter = TINTER_NARROW; inf->tinter = TINTER_NARROW;
} }
else if (!strcmp(cp,"normal")) else if (!strcmp(cp,"normal"))
{ {
inf.tinter = TINTER_NORMAL; inf->tinter = TINTER_NORMAL;
} }
else if (!strcmp(cp,"vector")) else if (!strcmp(cp,"vector"))
{ {
inf.tinter = TINTER_VECTOR; inf->tinter = TINTER_VECTOR;
} }
else else
{ {
@ -10682,34 +10649,20 @@ static info_type * loadinfo(const char * const fname)
} }
} }
else if (strcmp(buf, "nomirror") == 0) else if (strcmp(buf, "nomirror") == 0)
inf.mirrorable = 0; inf->mirrorable = 0;
else if (strcmp(buf, "noflip") == 0) else if (strcmp(buf, "noflip") == 0)
inf.flipable = 0; inf->flipable = 0;
} }
} }
while (!feof(fi)); while (!feof(fi));
fclose(fi); fclose(fi);
return inf;
/* Return the info: */
inf_ret = malloc(sizeof(info_type));
/* FIXME: Check for errors! */
memcpy(inf_ret, &inf, sizeof(info_type));
return(inf_ret);
}
else
{
return NULL;
}
} }
/* Load a file's alternative image: */ /* Load a file's alternative image: */
static SDL_Surface * loadaltimage(const char * const fname) static SDL_Surface * loadaltimage(const char * const fname)
{ {
char * alt_fname; char * alt_fname;