loadras.c
来自「细胞自动机的一个源代码」· C语言 代码 · 共 178 行
C
178 行
/*procedure to put raster file into byte/int array*/#include <stdio.h>#include <sys/file.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include "loadras.h"int getrasheader(struct rasterheader *rashead, int from) { int i, j, k, n; char rbuf[4], buf[4]; /* char magic[4] = {0x59a66a95};*/ n=read(from, buf, 4); for (j=0; j<4; j++) { rbuf[j] = buf[4-j-1]; rashead->ras_magic[j]=buf[j]; } k = (int)*( (int *)rbuf); rashead->irasmagic=k; n=read(from, buf, 4); for (j=0; j<4; j++) { rbuf[j] = buf[4-j-1]; rashead->ras_width[j]=buf[j]; } k = (int)*( (int *)rbuf); rashead->iraswidth=k; n=read(from, buf, 4); for (j=0; j<4; j++) { rbuf[j] = buf[4-j-1]; rashead->ras_height[j]=buf[j]; } k = (int)*( (int *)rbuf); rashead->irasheight=k; n=read(from, buf, 4); for (j=0; j<4; j++) { rbuf[j] = buf[4-j-1]; rashead->ras_depth[j]=buf[j]; } k = (int)*( (int *)rbuf); rashead->irasdepth=k; n=read(from, buf, 4); for (j=0; j<4; j++) { rbuf[j] = buf[4-j-1]; rashead->ras_length[j]=buf[j]; } k = (int)*( (int *)rbuf); rashead->iraslength=k; n=read(from, buf, 4); for (j=0; j<4; j++) { rbuf[j] = buf[4-j-1]; rashead->ras_type[j]=buf[j]; } k = (int)*( (int *)rbuf); rashead->irastype=k; n=read(from, buf, 4); for (j=0; j<4; j++) { rbuf[j] = buf[4-j-1]; rashead->ras_maptype[j]=buf[j]; } k = (int)*( (int *)rbuf); rashead->irasmaptype=k; n=read(from, buf, 4); for (j=0; j<4; j++) { rbuf[j] = buf[4-j-1]; rashead->ras_maplength[j]=buf[j]; } k = (int)*( (int *)rbuf); rashead->irasmaplength=k;}/*AS FAR AS I KNOW, THIS WILL ONLY WORK ON INTEL MACHINES. BE WARNED*/int writeras(struct aberas *src, char *fn) { int i, j, k; int to; char buf[4], rbuf[4]; if ((to = open(fn, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))<0) { perror(fn); return(-1); } write(to, src->header.ras_magic, 4); write(to, src->header.ras_width, 4); write(to, src->header.ras_height, 4); write(to, src->header.ras_depth, 4); write(to, src->header.ras_length, 4); write(to, src->header.ras_type, 4); write(to, src->header.ras_maptype, 4); write(to, src->header.ras_maplength, 4); k=src->header.iraswidth * src->header.irasheight; for (i=0; i<k; i++) { write(to, (char *)(&(src->brgba[i*4])), 3); } close(to);}int loadras(struct aberas *dest, char *filename) { int i, j, k; int from; char buf[4], rbuf[4]; if ((from = open(filename, O_RDONLY))<0) { perror(filename); exit(1); } getrasheader(&dest->header, from); k = 4*((dest->header.iraswidth) * (dest->header.irasheight)); dest->brgba = (unsigned char *) malloc(k); dest->height=dest->header.irasheight; dest->width=dest->header.iraswidth; k = ((dest->header.iraswidth) * (dest->header.irasheight)); /* mother fucking raster files pad odd widths with a byte! */ for (i=0; i<k; i++) { read(from, buf, 3); dest->brgba[i*4]=(unsigned char)buf[2]; dest->brgba[(i*4)+1]=(unsigned char)buf[1]; dest->brgba[(i*4)+2]=(unsigned char)buf[0]; dest->brgba[(i*4)+3]=255; if (((dest->header.iraswidth)%2)==1) { if (((i+1)%(dest->header.iraswidth))==0) { //if width is odd, and we've read a full //width, line, throw away padded byte read(from, buf, 1); } } } return(1);}/*void main(int argc, char **argv) { int i, j, k; struct aberas t; char fn[80] = {"blargh"}; if (argc!=2) exit(1); loadras(&t, argv[1]); printf("\n\n%i\n", t.header.irasmagic); printf("%i\n", t.header.iraswidth); printf("%i\n", t.header.irasheight); printf("%i\n", t.header.irasdepth); printf("%i\n", t.header.iraslength); printf("%i\n", t.header.irastype); printf("%i\n", t.header.irasmaptype); printf("%i\n", t.header.irasmaplength); free(t.brgba);}*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?