1. 读入hdr数据
void readData(char *filename, uint *sizes, float *dists, void **data)
{
struct dsr hdr;
FILE *fp;
int dataTypeSize = 0;
char rawFilename[100];
size_t size;
if(!(fp=fopen(filename,"rb"))){
perror("opening .hdr file failed");
exit(1);
}
fread(&hdr,sizeof(struct dsr),1,fp);
fclose(fp);
sizes[0] = hdr.dime.dim[1]; sizes[1] = hdr.dime.dim[2]; sizes[2] = hdr.dime.dim[3];
dists[0] = hdr.dime.pixdim[1]; dists[1] = hdr.dime.pixdim[2]; dists[2] = hdr.dime.pixdim[3];
dataTypeSize = sizeof(short);
strcpy(rawFilename, filename);
*strrchr(rawFilename, '.') = 0;
strcat(rawFilename, ".img");
//读入裸数据
if (! (fp = fopen(rawFilename, "rb")))
{
perror("opening .img file failed");
exit(1);
}
size = sizes[0] * sizes[1] * sizes[2];
if (! (*data = malloc(size * dataTypeSize)))
{
fprintf(stderr, "not enough memory for volume data\n");
exit(1);
}
if (fread(*data, dataTypeSize, size, fp) != size)
{
fprintf(stderr, "reading volume data failed");
exit(1);
}
fclose(fp);
}
2.根据窗宽、窗位映射表
void GetLut(uchar*& pLut, double wl, double ww)
{
// 计算最大最小值
minValue = USHRT_MAX;
maxValue = 0;
ushort* ptr = (ushort*)data;
for(uint i=0; i<sizes[0]*sizes[1]*sizes[2]; i++){
if (ptr[i] > maxValue) maxValue = ptr[i];
if (ptr[i] < minValue) minValue = ptr[i];
}
std::cout << "min,max value: " << minValue << ", " << maxValue << std::endl;
//当wl和ww为0时,由图像数据自动计算合适的窗宽窗位
if(wl==0 && ww==0){
wl = minValue + (maxValue-minValue)/2.0f;
ww = maxValue - minValue;
}
//uint lutSize = maxValue-minValue+1;
uint lutSize = maxValue;
pLut = new uchar[lutSize];
double dColor1 = 0;
double dColor2 = 255;
double dValue1 = wl - ww/2.0f;
double dValue2 = dValue1 + ww;
double dK = (dColor2-dColor1+1)/ww;
//dValue1 -= minValue;
//dValue2 -= minValue;
for(int i=0; i<l