📄 pnmio.c
字号:
/*** Source file for blobdetect. (c) 2004 Johan Wiklund**** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.** ** See the file COPYING for details.***/#include <stdio.h>#include <stdlib.h>#include <string.h>#ifndef WIN32#include <strings.h>#endif#include "pnmio.h"static charpnm_getc(FILE *file){ register int ich; register char ch; ich= getc(file); if (ich == EOF) { fprintf(stderr, "pnm_getc: EOF / read error\n"); exit(1); } ch= (char)ich; if (ch == '#') { do { ich= getc(file); if (ich == EOF) { fprintf(stderr, "pnm_getc: EOF / read error\n"); exit(1); } ch= (char)ich; } while (ch != '\n' && ch != '\r'); } return ch;}static intpnm_getint(FILE *file){ register char ch; register int i; do { ch= pnm_getc(file); } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); if (ch < '0' || ch > '9') { fprintf(stderr, "pnm_getint: junk in file where an integer should be\n"); exit(1); } i= 0; do { i= i * 10 + ch - '0'; ch= pnm_getc(file); } while (ch >= '0' && ch <= '9'); return i;}static intpnm_readmagicnumber(FILE* file){ int ich1, ich2; ich1= getc(file); if (ich1 == EOF) { fprintf(stderr, "EOF / read error reading magic number\n"); exit(1); } ich2= getc(file); if (ich2 == EOF) { fprintf(stderr, "EOF / read error reading magic number\n"); exit(1); } return ich1 * 256 + ich2;}static voidpnm_readinitrest(FILE *file, int *colsP, int *rowsP, int *maxvalP){ /* Read size. */ *colsP= pnm_getint(file); *rowsP= pnm_getint(file); /* Read maxval. */ *maxvalP= pnm_getint(file);}/* File open/close that handles "-" as stdin and checks errors. */static FILE*pnm_openr(char *name){ FILE* f; if (strcmp(name, "-") == 0) f= stdin; else {#ifdef WIN32 f= fopen(name, "rb");#else /*WIN32*/#ifndef VMS f= fopen(name, "r");#else f= fopen(name, "r", "ctx=stm");#endif#endif /*WIN32*/ if (f == NULL) { perror(name); exit(1); } } return f;}static FILE*pnm_openw(char *name){ FILE* f;#ifdef WIN32 f= fopen(name, "wb");#else /*WIN32*/#ifndef VMS f= fopen(name, "w");#else f= fopen(name, "w", "mbc=32", "mbf=2"); /* set buffer factors */#endif#endif /*WIN32*/ if (f == NULL) { perror(name); exit(1); } return f;}voidpnm_close(FILE *f){ fflush(f); if (ferror(f)) fprintf(stderr, "pnm_close: A file read or write error occurred at some point" ); if (f != stdin) { if (fclose(f) != 0) { perror("fclose"); exit(1); } }}FILE *pnm_readhead(char *name, int *format, int *hgh, int *wid){ FILE *f; int maxvalP; f= pnm_openr(name); *format= pnm_readmagicnumber(f); pnm_readinitrest(f, wid, hgh, &maxvalP); return(f);}FILE *pnm_writehead(char *name, int format, int hgh, int wid){ FILE *f; int maxval= 255; f= pnm_openw(name); switch (format) { case RPPM_FORMAT: fprintf(f, "%c%c\n%d %d\n%d\n", PPM_MAGIC1, RPPM_MAGIC2, wid, hgh, maxval); break; case RPGM_FORMAT: fprintf(f, "%c%c\n%d %d\n%d\n", PGM_MAGIC1, RPGM_MAGIC2, wid, hgh, maxval); break; default: fprintf(stderr, "pnm_writehead: Can only write P5 and P6 types\n"); exit(1); break; } return(f);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -