⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 level1.c

📁 以Z轴为中心的平行投影. 请注意里面的说明是日语.
💻 C
字号:
/*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+---*/
/*       sample.c                                                             */
/*       Volume Renderer Sample File							 */
/*       Version 1.0  Cross-section (2006.06.15)			*/
/*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+---*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <math.h>
#include "sample.h"

main(argc, argv)
int    argc;
char **argv;
{
	
	int vol[SIZE_X][SIZE_Y][SIZE_Z];
	int nx, ny, nz;
	Image  img;
	Camera cam;
	int cs;
	char ofile[256];
	int i, j, k;
	Pixel  *pix;

	int zbuf[SIZE_X][SIZE_Y];
	
	if(argc != 5) {
		fprintf(stderr, "  Usage: %s [file] [size_x] [size_y] [size_z]\n", argv[0]);
		exit FAILURE;
	}
	
	/* decode volume size */
	nx = atoi(argv[2]);
	ny = atoi(argv[3]);
	nz = atoi(argv[4]);
	printf("  Volume Size= %d, %d, %d\n", nx, ny, nz);
	
	/* read volume data */
	if(readVolume(argv[1], nx, ny, nz, vol) == FAILURE) {
		fprintf(stderr, "File Read Error: %s\n", argv[1]);
		exit FAILURE;
	}
	
	/* read parameters and output file name*/
	printf("\n Cross-section No. = ");
	scanf("%d", &cs);
	printf("\n Output file name = ");
	scanf("%s", ofile);
	
	/* cross-section display */
	/* generate image */
	img.hd.width = nx;
	img.hd.height = ny;
	set_image_header(&img);
	
	//z-buffer initialize
	for (i = 0; i < nx; i ++)
		for (j = 0; j < ny; j ++)
			zbuf[i][j] = nz + 1;

	printf("  Display cross-section No. %d\n", cs);

	for (k = 0; k < cs; k ++) {
		pix = img.pix;
		for(j=0; j < ny; j++) {
			for(i=0; i < nx; i++) {
				if (vol[i][j][k] != 0) {
					if (zbuf[i][j] > k) {
						colorMap(vol[i][j][k], pix);
						zbuf[i][j] = k;
					}
				}
			pix++;
			}
		}
	}
	
	/* output image file */
	imgout(&img, ofile);
	
}


int readVolume(file, nx, ny, nz, vol)
char *file;			/*file name (IN) */
int nx, ny, nz;			/* volume size (IN) */
int vol[SIZE_X][SIZE_Y][SIZE_Z];			/* volume data (OUT) */
{
	FILE  *fpin;
	unsigned char *rawdata, *raw;
	int nbyte, i, j, k;
	
	/* memory allocation to read-buffer */
	nbyte = nx * ny * nz;
	if((rawdata = (unsigned char *)malloc(nbyte)) == NULL){
		fprintf(stderr, " Memory Allocation Error\n");
		exit FAILURE;
	}
	
	/* open input file */
	if ((fpin = fopen(file, "r")) == NULL) {
		fprintf(stderr, "File Open Error: %s\n", file);
		exit FAILURE;
	}
	
	/* read file */
	fread(rawdata, 1, nbyte, fpin);
	close(fpin);
	
	/* initialization */
	raw = rawdata;
	
	/* store voxel values to volume data array */
	for (k=0; k<nz; k++) {
		for (j=0; j<ny; j++) {
			for (i=0; i<nx; i++) {
				vol[i][j][k] = (int)*raw;
				raw++;
				/* printf("  i, j, k = %d, %d, %d,  v= %d\n", i, j, k, vol[i][j][k]); */
			}
		}
	}
	
	/* buffer memory free */
	free(rawdata);
	
}


void colorMap(val, pix)
int val;
Pixel  *pix;
{
	if (val <= 63) {
		pix->r = 0;
		pix->g = (255/63) * val;
		pix->b = 255;
	}
	else {
		if (63 < val && val <= 127) {
			pix->r = 0;
			pix->g = 255;
			pix->b = 255 - (255/63) * (val - 64);
		}
		else {
			if (127 < val && val <= 191) {
				pix->r = (255/63) * (val - 128);
				pix->g = 255;
				pix->b = 0;
			}
			else {
				if (191 < val && val <= 255) {
					pix->r = 255;
					pix->g = 255 - (255/63) * (val - 192);
					pix->b = 0;
				}
			}
		}
	}

}


void set_image_header(img)
Image *img;
{
  Header  *h;

  h = &img->hd;
  h->magic  = 0x59a66a95;
  h->depth  = 255;
  h->length = 3 * h->width * h->height;
  h->type   = 1;
  h->maptype   = 0;
  h->maplength = 0;

  if((img->pix = (Pixel *)
    malloc (h->length * sizeof(Pixel))) == NULL) {
      fprintf(stderr, " Memory Allocation Error\n");
      exit FAILURE;
  }
}


void imgout(img, file)
Image  *img;
char   *file;
{
  FILE  *fpout;
  char ss[256];

  /* open output file */
  if ((fpout = fopen(file, "w")) == NULL) {
    fprintf(stderr, "Error opening file: %s\n", file);
    exit FAILURE;
  }

//  fwrite(&img->hd, sizeof(Header), 1, fpout); 

//  sprintf(ss,"P6\n%d %d\n%d\n%d %d\n %d %d\n",
//		img->hd.width, img->hd.height, 
//		img->hd.depth, img->hd.length, img->hd.type, 
//		img->hd.maptype, img->hd.maplength);

  sprintf(ss,"P6\n%d %d\n255\n",img->hd.width,img->hd.height);
  
  fputs(ss,fpout);
  fwrite(img->pix, img->hd.length, 1, fpout);
  fclose(fpout);
}


/*** EOF ***/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -