📄 ppmfile.c
字号:
/* * functions for ppm image file format * * * Open an old file or new file */#include <imgfile.h>#define PGM_MAGIC1 'P'#define RPGM_MAGIC2 '5'FILE *imgPPMOpen(filename,xflen,yflen,type) char *filename; /* open filename */ long *xflen,*yflen; /* file size */ long type; /* if exsit: type=0 exist,type=1,new file */{ char magic1,magic2; int gray_size; int width,height; int iw,jh; char *zero_space; FILE *fPtr; if(type) { printf("new file name: %s\nsize width x height: %d x %d\n", filename,*xflen,*yflen); width = *xflen; height= *yflen; if ( (fPtr = fopen(filename,"w+")) == NULL) { printf("Error: Open New File \n"); exit(1); } fprintf(fPtr,"%c%c\n%d %d\n%d\n",PGM_MAGIC1,RPGM_MAGIC2, width,height,255); /* init image file ,and allocate enough space */ zero_space = (char *)malloc(width); memset(zero_space,0,width); for (jh = 0;jh < height;jh++) { fwrite(zero_space,1,width,fPtr); } free(zero_space); return fPtr; } else { printf("old file name: %s\n",filename); if( (fPtr = fopen(filename,"r+")) == NULL) { printf("Error: Open Old File \n"); exit(1); } fscanf(fPtr,"%c%c\n%d %d\n%d\n", &magic1,&magic2,&width,&height,&gray_size); printf("size width x height: %d x %d\n",width,height); printf("magic : %c%c\ngray_size: %d\n",magic1,magic2,gray_size); *xflen = width; *yflen = height; return fPtr; } return 0; }/* * Close a file */voidimgPPMClose(fPtr) FILE *fPtr;{ fclose(fPtr);}/* * read a ppm file */longimgPPMRead(fPtr,xBegin,yBegin,xLen,yLen,stenArray) FILE *fPtr; long xBegin,yBegin; long xLen,yLen; char *stenArray;{ char magic1,magic2; int gray_size; int width,height; int ty,tx; fseek(fPtr,0L,SEEK_SET); fscanf(fPtr,"%c%c\n%d %d\n%d\n",&magic1,&magic2,&width,&height,&gray_size);#if 0 printf("imgRead: %c%c %d %d %d\n",magic1,magic2,width,height,gray_size);#endif fseek(fPtr,(long)(yBegin*width+xBegin),SEEK_CUR); for(ty = 0; ty <yLen-1 ;ty++) { fread(stenArray,1,xLen,fPtr); stenArray += xLen; fseek(fPtr,(long)(width - xLen),SEEK_CUR); } fread(stenArray,1,xLen,fPtr); return 0;}/* * imgGet * get image data from memory */longimgPPMGet(sImage,xBegin,yBegin,xLen,yLen,sWidth,sHeight,stenArray) char *sImage; long xBegin,yBegin; long xLen,yLen; long sWidth,sHeight; char *stenArray;{ int tx,ty; int locBegin; yBegin = sHeight - yBegin - 1; locBegin = yBegin*sWidth+xBegin; sImage += locBegin; for(ty = 0;ty <yLen;ty++) { for(tx = 0;tx <xLen;tx++) { *stenArray = *sImage; stenArray++; sImage++; } sImage -= sWidth+xLen; } /* for(tx = 0;tx < xLen;tx++ ) { *stenArray = *sImage; stenArray++; sImage++; }*/ return 0;}/* * write ppm file */longimgPPMWrite(fPtr,xBegin,yBegin,xLen,yLen,stenArray) FILE *fPtr; long xBegin,yBegin; long xLen,yLen; char *stenArray;{ char magic1,magic2; int gray_size; int width,height; int ty; fseek(fPtr,0L,SEEK_SET); fscanf(fPtr,"%c%c\n%d %d\n%d\n",&magic1,&magic2,&width,&height,&gray_size);#if 0 printf("imgWrite: %c%c %d %d %d\n",magic1,magic2,width,height,gray_size);#endif fseek(fPtr,(long)(yBegin*width+xBegin),SEEK_CUR); for(ty = 0; ty <yLen ;ty++) { fwrite(stenArray,1,xLen,fPtr); stenArray += xLen; fseek(fPtr,(long)(width - xLen),SEEK_CUR); } return 0;} /* * imgPut * just for ppm file format */longimgPPMPut(tImage,xBegin,yBegin,xLen,yLen,tWidth,tHeight,stenArray) char *tImage; long xBegin,yBegin; long xLen,yLen; long tWidth,tHeight; char *stenArray;{ int tx,ty; int locBegin; yBegin = tHeight - yBegin - 1; locBegin = yBegin*tWidth+xBegin; tImage += locBegin; for(ty = 0;ty <yLen;ty++) { for(tx = 0;tx <xLen;tx++) { *tImage = *stenArray; stenArray++; tImage++; } tImage -= tWidth + xLen; } return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -