📄 feretio.c
字号:
fprintf(stdout,"readImagePGM: Warning, max value %d for pixels in image %s is not 255\n", max, filename); } im = (UCH*) malloc(sizeof(UCH)*width*height); if (!im) syserr("readImagePGM", "malloc of im failed", ""); i = 0; val = fgetc(infile); while (!(val == EOF)) { im[i] = (UCH) val; i++; val = fgetc(infile); } if (verbose) fprintf(stdout,"Read in %d Pixel Values\n", i); fclose(infile); return( im );} void writeImagePGM(const char *fn, float *data, int numpix,int w, int h){ const char *routine = "writeImagePGM"; float min, max, sum, scale; int i, val; FILE *fp; min = data[0]; max = data[0]; sum = 0.0; for (i = 1; i < numpix; i++) { if (data[i] < min) min = data[i]; if (data[i] > max) max = data[i]; sum = sum + data[i]; } scale = 255.0 / (max - min); fp = (FILE*) fopen_with_error(fn, "wb", routine); fprintf(fp,"P5\n"); fprintf(fp,"%d %d\n", w, h); fprintf(fp,"255\n"); for (i = 0; i < numpix; i++) { val = (int) ((data[i] - min) * scale); fputc(val,fp); } fclose(fp);}/*======================================================================== Contents of what was the readstrs.c file in NIST library ========================================================================*/char **read_strings(const char *fn, int *numlines){ const char *routine = "read_strings"; char aline[512]; char **values = 0; char **newvalues = 0; int i; int numread = 0; int numalloc = 0; FILE *fp; fp = (FILE*) fopen_with_error(fn, "r", routine); while (fgets(aline, 512, fp) != NULL) { if (numread == numalloc) { numalloc = 16 + 11*numalloc/10; newvalues = (char**) malloc(sizeof(char *)*numalloc); if (!newvalues) exit(fprintf(stderr, "%s: new char ** [%d] failed\n", routine, numalloc)); for ( i = 0 ; i < numread ; i++ ) newvalues[i] = values[i]; for ( i = numread ; i < numalloc ; i++ ) newvalues[i] = 0; if(values!=NULL) free(values); values = newvalues; } { char *linereturn = strrchr(aline, '\n'); if (linereturn) *linereturn = '\0'; /* chomp */ } values[numread++] = strclone(aline); } fclose(fp); *numlines = numread; return values;}void free_strings(char **x, const int n){ int i; for ( i = 0 ; i < n ; i++ ) free(x[i]); free(x);}// make a stand alone concatenation of s1+s2char *strconc(const char *s1, const char *s2){ int n1, n2, nreq; char *result; n1 = strlen(s1); n2 = strlen(s2); nreq = n1+n2+1; result = (char*) malloc(sizeof(char)*nreq); if (!result) syserr("strconc", "new char []", "space for string"); memcpy(&result[0 ], s1, n1); memcpy(&result[n1], s2, n2); result[n1+n2] = '\0'; return result;}char *strclone(const char *si){ int ni; char *so; ni = strlen(si); so = (char*) malloc(sizeof(char)*(ni+1)); if (!so) syserr("strclone", "malloc for string ", "failed"); memcpy(so, si, ni); so[ni] = '\0'; return so;}void strnullchecks(const char *fn, const char *name, const char *routine){ if (fn == NULL) { fprintf(stderr, "routine \"%s\" was supplied with NULL variable \"%s\"\n", routine, name); exit(-1); }}// make all the characters of the input string lower case, if applicable// changes the input stringchar *strlower(char *x){ char *result; result = x ; strnullchecks(x, "x", "strlower"); for ( ; *x ; x++ ) *x = (char)tolower((int)*x); return result;}// make all the characters of the input string lower case, if applicable// changes the input stringchar *strupper(char *x){ char *result; result = x ; strnullchecks(x, "x", "strupper"); for ( ; *x ; x++ ) *x = (char)toupper((int)*x); return result;}// take two strings: strip the final extension from the first// if any then add the second to the first.// note the first string's address is altered, it's memory is// freed and it points to newly allocated memory afterwards.char *newextlong(char **filename, const char *extension){ if (extension) { char *result = 0; char *finalperiod = strrchr(*filename, '.'); if (!finalperiod) // add an extension where there wasn't one { char *periodized = strconc(".", extension); result = strconc(*filename, periodized); free(periodized); } else { // replace the existing extension finalperiod[1] = '\0'; // NULL terminate the string result = strconc(*filename, extension); } free(*filename); *filename = result; } return *filename;}static void bailer(const char *s1, const char *s2, const char *s3){ fprintf(stderr,"ERROR"); if (s1) fprintf(stderr,": %s", s1); if (s2) fprintf(stderr,": %s", s2); if (s3) fprintf(stderr,": %s", s3); fprintf(stderr,"\n"); fflush(stderr);}void syserr(const char *funcname, const char *syscall, const char *msg){ bailer(funcname, syscall, msg); exit(-1);}FILE *fopen_with_error(const char *fn, const char *mode, const char *routine){ FILE *fp; // for simplicity try the filename as it came in fp = fopen(fn, mode); if (fp != NULL) return fp; // then try different case variations, failing if that doesn't work return fopen_anycase(fn, mode, routine);}FILE *fopen_anycase(const char *fn, const char *mode, const char *routine){ FILE *fp, *lp, *up; char *lower, *upper; // for simplicity try the filename as it came in fp = fopen(fn, mode); if (fp != NULL) return fp; // try the filename (including any path) all forced to lower case lower = strlower(strclone(fn)); lp = fopen(lower, mode); free(lower); if (lp != NULL) return lp; // try the filename (including any path) all forced to upper case upper = strupper(strclone(fn)); up = fopen(upper, mode); free(upper); if (up != NULL) return up; // else bail totally unless { fprintf(stderr, "%s: cannot open(mode \"%s\") file %s\n", routine ? routine : "fopen_anycase", mode, fn); exit(-1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -