/*! * sarrayRead() * * Input: filename * Return: sarray, or null on error */ SARRAY * sarrayRead(const char *filename) { FILE *fp; SARRAY *sa; PROCNAME("sarrayRead"); if (!filename) return (SARRAY *)ERROR_PTR("filename not defined", procName, NULL); if ((fp = fopenReadStream(filename)) == NULL) return (SARRAY *)ERROR_PTR("stream not opened", procName, NULL); if ((sa = sarrayReadStream(fp)) == NULL) { fclose(fp); return (SARRAY *)ERROR_PTR("sa not read", procName, NULL); } fclose(fp); return sa; }
/*! * \brief recogReadStream() * * \param[in] fp file stream * \return recog, or NULL on error */ L_RECOG * recogReadStream(FILE *fp) { l_int32 version, setsize, threshold, scalew, scaleh, linew; l_int32 maxyshift, nc; L_DNA *dna_tochar; PIXAA *paa; L_RECOG *recog; SARRAY *sa_text; PROCNAME("recogReadStream"); if (!fp) return (L_RECOG *)ERROR_PTR("stream not defined", procName, NULL); if (fscanf(fp, "\nRecog Version %d\n", &version) != 1) return (L_RECOG *)ERROR_PTR("not a recog file", procName, NULL); if (version != RECOG_VERSION_NUMBER) return (L_RECOG *)ERROR_PTR("invalid recog version", procName, NULL); if (fscanf(fp, "Size of character set = %d\n", &setsize) != 1) return (L_RECOG *)ERROR_PTR("setsize not read", procName, NULL); if (fscanf(fp, "Binarization threshold = %d\n", &threshold) != 1) return (L_RECOG *)ERROR_PTR("binary thresh not read", procName, NULL); if (fscanf(fp, "Maxyshift = %d\n", &maxyshift) != 1) return (L_RECOG *)ERROR_PTR("maxyshift not read", procName, NULL); if (fscanf(fp, "Scale to width = %d\n", &scalew) != 1) return (L_RECOG *)ERROR_PTR("width not read", procName, NULL); if (fscanf(fp, "Scale to height = %d\n", &scaleh) != 1) return (L_RECOG *)ERROR_PTR("height not read", procName, NULL); if (fscanf(fp, "Normalized line width = %d\n", &linew) != 1) return (L_RECOG *)ERROR_PTR("line width not read", procName, NULL); if ((recog = recogCreate(scalew, scaleh, linew, threshold, maxyshift)) == NULL) return (L_RECOG *)ERROR_PTR("recog not made", procName, NULL); if (fscanf(fp, "\nLabels for character set:\n") != 0) { recogDestroy(&recog); return (L_RECOG *)ERROR_PTR("label intro not read", procName, NULL); } l_dnaDestroy(&recog->dna_tochar); if ((dna_tochar = l_dnaReadStream(fp)) == NULL) { recogDestroy(&recog); return (L_RECOG *)ERROR_PTR("dna_tochar not read", procName, NULL); } recog->dna_tochar = dna_tochar; sarrayDestroy(&recog->sa_text); if ((sa_text = sarrayReadStream(fp)) == NULL) { recogDestroy(&recog); return (L_RECOG *)ERROR_PTR("sa_text not read", procName, NULL); } recog->sa_text = sa_text; if (fscanf(fp, "\nPixaa of all samples in the training set:\n") != 0) { recogDestroy(&recog); return (L_RECOG *)ERROR_PTR("pixaa intro not read", procName, NULL); } if ((paa = pixaaReadStream(fp)) == NULL) { recogDestroy(&recog); return (L_RECOG *)ERROR_PTR("pixaa not read", procName, NULL); } recog->setsize = setsize; nc = pixaaGetCount(paa, NULL); if (nc != setsize) { recogDestroy(&recog); pixaaDestroy(&paa); L_ERROR("(setsize = %d) != (paa count = %d)\n", procName, setsize, nc); return NULL; } recogAddAllSamples(&recog, paa, 0); /* this finishes */ pixaaDestroy(&paa); if (!recog) return (L_RECOG *)ERROR_PTR("bad templates", procName, NULL); return recog; }
/*! * gplotRead() * * Input: filename * Return: gplot, or NULL on error */ GPLOT * gplotRead(const char *filename) { char buf[L_BUF_SIZE]; char *rootname, *title, *xlabel, *ylabel, *ignores; l_int32 outformat, ret, version, ignore; FILE *fp; GPLOT *gplot; PROCNAME("gplotRead"); if (!filename) return (GPLOT *)ERROR_PTR("filename not defined", procName, NULL); if ((fp = fopenReadStream(filename)) == NULL) return (GPLOT *)ERROR_PTR("stream not opened", procName, NULL); ret = fscanf(fp, "Gplot Version %d\n", &version); if (ret != 1) { fclose(fp); return (GPLOT *)ERROR_PTR("not a gplot file", procName, NULL); } if (version != GPLOT_VERSION_NUMBER) { fclose(fp); return (GPLOT *)ERROR_PTR("invalid gplot version", procName, NULL); } ignore = fscanf(fp, "Rootname: %s\n", buf); rootname = stringNew(buf); ignore = fscanf(fp, "Output format: %d\n", &outformat); ignores = fgets(buf, L_BUF_SIZE, fp); /* Title: ... */ title = stringNew(buf + 7); title[strlen(title) - 1] = '\0'; ignores = fgets(buf, L_BUF_SIZE, fp); /* X axis label: ... */ xlabel = stringNew(buf + 14); xlabel[strlen(xlabel) - 1] = '\0'; ignores = fgets(buf, L_BUF_SIZE, fp); /* Y axis label: ... */ ylabel = stringNew(buf + 14); ylabel[strlen(ylabel) - 1] = '\0'; if (!(gplot = gplotCreate(rootname, outformat, title, xlabel, ylabel))) { fclose(fp); return (GPLOT *)ERROR_PTR("gplot not made", procName, NULL); } FREE(rootname); FREE(title); FREE(xlabel); FREE(ylabel); sarrayDestroy(&gplot->cmddata); sarrayDestroy(&gplot->datanames); sarrayDestroy(&gplot->plotdata); sarrayDestroy(&gplot->plottitles); numaDestroy(&gplot->plotstyles); ignore = fscanf(fp, "Commandfile name: %s\n", buf); stringReplace(&gplot->cmdname, buf); ignore = fscanf(fp, "\nCommandfile data:"); gplot->cmddata = sarrayReadStream(fp); ignore = fscanf(fp, "\nDatafile names:"); gplot->datanames = sarrayReadStream(fp); ignore = fscanf(fp, "\nPlot data:"); gplot->plotdata = sarrayReadStream(fp); ignore = fscanf(fp, "\nPlot titles:"); gplot->plottitles = sarrayReadStream(fp); ignore = fscanf(fp, "\nPlot styles:"); gplot->plotstyles = numaReadStream(fp); ignore = fscanf(fp, "Number of plots: %d\n", &gplot->nplots); ignore = fscanf(fp, "Output file name: %s\n", buf); stringReplace(&gplot->outname, buf); ignore = fscanf(fp, "Axis scaling: %d\n", &gplot->scaling); fclose(fp); return gplot; }