/*
* $Source: /net/users/roe/onderzoek/morphology/skeletons/code/democode/RCS/volume.c,v $
* $Revision: 1.1 $
* $Date: 2009/06/16 14:18:59 $
* $State: Exp $
* $Author: roe $
*
* ------------------------------------------------------------*/
#include <math.h>
#include "volume.h"
#include <malloc.h>
#include "avs_io.h"
#define EPS 0.0000001
unsigned int to_int(unsigned char bytes[4]) {
return bytes[3]+256*(bytes[2]+256*(bytes[1]+256*bytes[0]));
}
unsigned short to_short(unsigned char bytes[2]) {
return bytes[1]+256*bytes[0];
}
void int_to_bytes(int nr, unsigned char bytes[4])
{
bytes[3] = nr%256;
nr = nr/256;
bytes[2] = nr%256;
nr = nr/256;
bytes[1] = nr%256;
bytes[0] = nr/256;
}
void from_short(unsigned short nr, unsigned char bytes[2])
{
bytes[1] = nr%256;
bytes[0] = nr/256;
}
Volume *Volume_New(int xdim, int ydim, int zdim)
{
long i, j;
Volume *v;
v = (Volume *) malloc(sizeof(Volume));
if (v == NULL) {
printf("Out of memory!\n");
exit(0);
}
/* printf("In Volume_New: xdim=%d,ydim=%d,zdim=%d\n",xdim,ydim,zdim); */
v->xdim = xdim;
v->ydim = ydim;
v->zdim = zdim;
v->data = (VOXEL ***) calloc(xdim,sizeof(VOXEL **));
if (!v->data) {
printf("Out of memory!\n");
exit(-1);
}
v->data[0] = (VOXEL **) calloc(xdim * ydim,sizeof(VOXEL *));
if (!v->data[0]) {
printf("Out of memory!\n");
exit(-1);
}
v->data[0][0] = (VOXEL *) calloc(xdim * ydim * zdim,sizeof(VOXEL));
if (!v->data[0][0]) {
printf("Out of memory!\n");
exit(-1);
}
for (j=1; j<ydim; j++)
v->data[0][j] = v->data[0][j-1] + zdim;
for (i=1; i<xdim; i++) {
v->data[i] = v->data[i-1] + ydim;
v->data[i][0] = v->data[i-1][0] + ydim * zdim;
for (j=1; j<ydim; j++)
v->data[i][j] = v->data[i][j-1] + zdim;
}
return v;
}
Volume *Volume_ReadSFF(char filename[])
{
int i,j,k;
Volume *temp;
FILE *f;
unsigned int txdim, tydim, tzdim;
unsigned char bytes[4], bytes2[2];
int xdim, ydim, zdim;
int xoff, yoff, zoff;
unsigned char ftype, dims, delim, byteval;
unsigned short shortval;
VOXEL val, min, max;
f = fopen(filename, "rb");
if (f == NULL) {
printf("Error opening the file %s\n", filename);
exit(0);
/* return temp;*/
}
fread(&ftype, sizeof(unsigned char), 1, f);
fread(&dims, sizeof(unsigned char), 1, f);
if (dims != 3) {
printf("Wrong dimensions\n");
exit(0);
/* return temp;*/
}
fread(bytes, sizeof(unsigned char), 4, f);
txdim = to_int(bytes);
fread(bytes, sizeof(unsigned char), 4, f);
tydim = to_int(bytes);
fread(bytes, sizeof(unsigned char), 4, f);
tzdim = to_int(bytes);
xdim = txdim;
ydim = tydim;
zdim = tzdim;
printf("In Volume_ReadSFF: xdim=%d,ydim=%d,zdim=%d\n", xdim,ydim,zdim);
temp = Volume_New(xdim, ydim, zdim);
xoff = (xdim-txdim)/2;
yoff = (ydim-tydim)/2;
zoff = (zdim-tzdim)/2;
/* skip comments */
delim = fgetc(f);
while (delim != '\f')
delim = fgetc(f);
if (ftype == 1) {
printf("Reading SFF file: %s... DataType: Bytes\n",filename);
for (k=0; k<tzdim; k++)
for (j=0; j<tydim; j++)
for (i=0; i<txdim; i++) {
fread(&byteval, sizeof(unsigned char), 1, f);
val = (VOXEL) byteval;
temp->data[i+xoff][j+yoff][k+zoff] = val;
}
} else if (ftype == 2) {
printf("Reading SFF file: %s... DataType: Shorts\n",filename);
for (k=0; k<tzdim; k++)
for (j=0; j<tydim; j++)
for (i=0; i<txdim; i++) {
fread(bytes2, sizeof(unsigned char), 2, f);
shortval = to_short(bytes2);
temp->data[i+xoff][j+yoff][k+zoff] = (VOXEL) shortval;
}
} else {
printf("Unknown datatype\n");
exit(0);
/* return temp;*/
}
fclose(f);
Volume_MinMax(temp, &min, &max);
printf("In Volume_ReadSFF of %s: min=%d, max=%d\n", filename, min, max);
return temp;
}
void Volume_WriteSFF(Volume *v, const char filename[])
{
unsigned char nrdims, bytes2[2], bytes[4], byteval;
FILE *f;
unsigned char ftype, dims, delim;
int x, y, z;
int i, j, k;
unsigned short shortval;
VOXEL min, max;
Volume_MinMax(v, &min, &max);
printf("In VolumeData_Write of %s: min=%d, max=%d\n", filename, min, max);
f = fopen(filename, "wb");
if (max <= 255) {
ftype = 1;
printf("Writing SFF file: %s... DataType: Bytes\n",filename);
ftype = 1;
fwrite(&ftype, sizeof(unsigned char), 1, f);
dims = 3;
fwrite(&dims, sizeof(unsigned char), 1, f);
int_to_bytes(v->xdim, bytes);
fwrite(bytes, sizeof(unsigned char), 4, f);
int_to_bytes(v->ydim, bytes);
fwrite(bytes, sizeof(unsigned char), 4, f);
int_to_bytes(v->zdim, bytes);
fwrite(bytes, sizeof(unsigned char), 4, f);
delim = '\f';
fwrite(&delim, sizeof(unsigned char), 1, f);
for (k=0; k<v->zdim; k++)
for (j=0; j<v->ydim; j++)
for (i=0; i<v->xdim; i++) {
byteval = v->data[i][j][k];
fwrite(&byteval, sizeof(unsigned char), 1, f);
}
}
else {
ftype = 2;
printf("Writing SFF file: %s... DataType: Shorts\n",filename);
fwrite(&ftype, sizeof(unsigned char), 1, f);
nrdims = 3;
fwrite(&nrdims, sizeof(unsigned char), 1, f);
int_to_bytes(v->xdim, bytes);
fwrite(bytes, sizeof(unsigned char), 4, f);
int_to_bytes(v->ydim, bytes);
fwrite(bytes, sizeof(unsigned char), 4, f);
int_to_bytes(v->zdim, bytes);
fwrite(bytes, sizeof(unsigned char), 4, f);
delim = '\f';
fwrite(&delim, sizeof(unsigned char), 1, f);
for (z=0; z<v->zdim; z++)
for (y=0; y<v->ydim; y++)
for (x=0; x<v->xdim; x++) {
shortval = v->data[x][y][z];/* VOL(z,y,x);*/
from_short(shortval, bytes2);
fwrite(bytes2, sizeof(unsigned char), 2, f);
}
}
fclose(f);
}
void Volume_Delete(Volume *v)
{
free(v->data[0][0]);
free(v->data[0]);
free(v->data);
}
ByteVolume *ByteVolume_New(int xdim, int ydim, int zdim)
{
long i, j;
ByteVolume *v;
v = (ByteVolume *) malloc(sizeof(ByteVolume));
if (v == NULL) {
printf("Out of memory!\n");
exit(0);
}
v->xdim = xdim;
v->ydim = ydim;
v->zdim = zdim;
v->data = (BYTE ***) calloc(xdim,sizeof(BYTE **));
if (!v->data) {
printf("Out of memory!\n");
exit(-1);
}
v->data[0] = (BYTE **) calloc(xdim * ydim,sizeof(BYTE *));
if (!v->data[0]) {
printf("Out of memory!\n");
exit(-1);
}
v->data[0][0] = (BYTE *) calloc(xdim * ydim * zdim,sizeof(BYTE));
if (!v->data[0][0]) {
printf("Out of memory!\n");
exit(-1);
}
for (j=1; j<ydim; j++)
v->data[0][j] = v->data[0][j-1] + zdim;
for (i=1; i<xdim; i++) {
v->data[i] = v->data[i-1] + ydim;
v->data[i][0] = v->data[i-1][0] + ydim * zdim;
for (j=1; j<ydim; j++)
v->data[i][j] = v->data[i][j-1] + zdim;
}
return v;
}
void ByteVolume_Delete(ByteVolume *v)
{
free(v->data[0][0]);
free(v->data[0]);
free(v->data);
}
ByteVolume *ByteVolume_ReadSFF(char filename[])
{
int i,j,k;
ByteVolume *temp;
FILE *f;
unsigned char bytes[4];
unsigned int txdim, tydim, tzdim;
int xdim, ydim, zdim;
int xoff, yoff, zoff;
unsigned char ftype, dims, delim, byteval;
f = fopen(filename, "rb");
if (f == NULL) {
printf("Error opening the file %s\n", filename);
exit(0);
}
fread(&ftype, sizeof(unsigned char), 1, f);
fread(&dims, sizeof(unsigned char), 1, f);
if (dims != 3) {
printf("Wrong dimensions\n");
exit(0);
}
fread(bytes, sizeof(unsigned char), 4, f);
txdim = to_int(bytes);
fread(bytes, sizeof(unsigned char), 4, f);
tydim = to_int(bytes);
fread(bytes, sizeof(unsigned char), 4, f);
tzdim = to_int(bytes);
xdim = txdim;
ydim = tydim;
zdim = tzdim;
temp = ByteVolume_New(xdim, ydim, zdim);
xoff = (xdim-txdim)/2;
yoff = (ydim-tydim)/2;
zoff = (zdim-tzdim)/2;
/* skip comments */
delim = fgetc(f);
while (delim != '\f')
delim = fgetc(f);
if (ftype == 1) {
for (k=0; k<tzdim; k++)
for (j=0; j<tydim; j++)
for (i=0; i<txdim; i++) {
fre