📄 file_and_time.c
字号:
/*** Source file for blobdetect. (c) 2004 Per-Erik Forssen**** 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 <stdlib.h>#include <math.h>#include "file_and_time.h"#include "pnmio.h"/* * Write difference between two clock_t structs * to standard output. * * strg Message preceeding time text * t0 start time * t1 end time * */void write_time_diff(char *strg,clock_t t0,clock_t t1) { printf("%s %g\n",strg,(double)(t1-t0)/CLOCKS_PER_SEC);}/* * Read a file from disk using PNMIO * * fname Name of file to read * pname Name of program (for error message) * bf_image Place to store resultant image buffer * ssfl Subsample image if non-zero * */void read_pnm_file(char *fname,char *pname,buffer **bf_image,int ssfl) {FILE *image_fid; int format,rows,cols,rows2,cols2,ndim,step;int bytes_read,k,l,m; fpnum *img;unsigned char *bytebuffer; image_fid=pnm_readhead(fname, &format, &rows, &cols); switch (format) { case RPPM_FORMAT: ndim = 3; break; case RPGM_FORMAT: ndim = 1; break; default: fprintf(stderr, "%s: PNM type %d not supported.\n",pname,format); exit(1); } bytebuffer=(unsigned char *)calloc(rows*ndim*cols,1); bytes_read = (int)fread((void *)bytebuffer, 1, rows*ndim*cols, image_fid); if (bytes_read != rows*ndim*cols) { fprintf(stderr, "%s: File size and PNM header mismatch for file %s\n",pname,fname); exit(1); } fclose(image_fid); step=1; if(ssfl) { step=2; } rows2=rows/step; cols2=cols/step; /* Allocate buffer, and convert PPM data to buffer */ bf_image[0]=buffer_new(rows2,cols2,ndim); img=bf_image[0]->data; for(m=0;m<ndim;m++) { for(k=0;k<rows2;k++) { for(l=0;l<cols2;l++) { img[k+l*rows2+m*rows2*cols2]= (fpnum)bytebuffer[m+step*ndim*(l+k*cols)]/255.0; } } } free(bytebuffer);}/* * Save an image to disk using PNMIO * * fname Name of file to read * pname Name of program (for error message) * bf_image Image buffer to save * ssfl Subsample image if non-zero * This is useful e.g. to quickly get only one of * the fields in an interleaved image. * * Returns 0 on success, 1 on failure */int write_pnm_file(char *fname,char *pname,buffer *bf_image) {FILE *image_fid; int format,rows,cols,ndim;int pixels,k,l,m; fpnum *img;unsigned char *bytebuffer; rows=bf_image->rows; cols=bf_image->cols; ndim=bf_image->ndim; pixels=rows*cols; switch(ndim) { case 1: format = RPGM_FORMAT; break; case 3: format = RPPM_FORMAT; break; default: printf("ERROR:Only grey-scale and RGB images are supported.\n"); return(1); /* Signal failure */ } image_fid = pnm_writehead(fname, format, rows, cols); /* Convert buffer to PPM format */ bytebuffer=(unsigned char *)calloc(ndim*pixels,1); img=bf_image->data; for(m=0;m<ndim;m++) { for(k=0;k<rows;k++) { for(l=0;l<cols;l++) { bytebuffer[m+ndim*(l+k*cols)] = (unsigned char)floor(img[k+l*rows+m*pixels]*255.5); } } } fwrite((void *)bytebuffer, 1, ndim*pixels, image_fid); fclose(image_fid); free(bytebuffer); return(0); /* No error code */}/* * Dump an array to file in ascii form * suitable to be read as an .m file * in Matlab. * * out_fid open file stream * vname string containing variable name * bf_var array holding data * */void dump_to_file(FILE *out_fid,char *vname,buffer *bf_var) { int rows,cols,k,l; rows=bf_var->rows; cols=bf_var->cols; fprintf(out_fid,"%s=[",vname); for(k=0;k<cols;k++) { for(l=0;l<rows;l++) { fprintf(out_fid,"%g ",bf_var->data[l+k*rows]); } if(k<cols-1) fprintf(out_fid,";\n"); else fprintf(out_fid,"];\n"); } fprintf(out_fid,"%s=%s';",vname,vname);}void idump_to_file(FILE *out_fid,char *vname,ibuffer *bf_var) { int rows,cols,k,l; rows=bf_var->rows; cols=bf_var->cols; fprintf(out_fid,"%s=[",vname); for(k=0;k<cols;k++) { for(l=0;l<rows;l++) { fprintf(out_fid,"%d ",bf_var->data[l+k*rows]); } if(k<cols-1) fprintf(out_fid,";\n"); else fprintf(out_fid,"];\n"); } fprintf(out_fid,"%s=%s';",vname,vname);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -