#include "stdafx.h" #include "DS_2b_cube.h" DS_2b_cube::DS_2b_cube(unsigned short int nwZ, unsigned short int nwY, unsigned short int nwX) { unsigned short int y, z; wZ = nwZ; wY = nwY; wX = nwX; cwX = (unsigned short int)ceil( (double)wX/4 ); data = new unsigned char ** [wZ]; rows = new unsigned char * [wZ*wY]; memory = new unsigned char [wZ*wY*cwX]; //make sure memory allocation is successful if (!data || !rows || !memory) { write_to_log("DS_cube: out of memory while creating cube."); data = NULL; return; } //make pointers point into correct places for (z = 0; z < wZ; z++) { for (y = 0; y < wY; y++) //link row point into memory rows rows[z*wY + y] = &memory[z*wY*cwX + y*cwX]; data[z] = &rows[z*wY]; //link final data pointers into rows } } DS_2b_cube::DS_2b_cube(DS_1b_cube* DS_1_bit_cube) { unsigned short int x, y, z; wZ = DS_1_bit_cube->wZ; wY = DS_1_bit_cube->wY; wX = DS_1_bit_cube->wX; cwX = (unsigned short int)ceil( (double)wX/4 ); data = new unsigned char ** [wZ]; rows = new unsigned char * [wZ*wY]; memory = new unsigned char [wZ*wY*cwX]; //make sure memory allocation is successful if (!data || !rows || !memory) { write_to_log("DS_cube: out of memory while creating cube."); data = NULL; return; } //make pointers point into correct places for (z = 0; z < wZ; z++) { for (y = 0; y < wY; y++) //link row point into memory rows rows[z*wY + y] = &memory[z*wY*cwX + y*cwX]; data[z] = &rows[z*wY]; //link final data pointers into rows } this->clear(); for (z = 0; z < wZ; z++) for (y = 0; y < wY; y++) for (x = 0; x < wX; x++) if (DS_1_bit_cube->get_spot(z, y, x)) this->set_spot(3, z, y, x); } DS_2b_cube::~DS_2b_cube(void) { delete data; delete rows; delete memory; } /* function that uses above functions, updates log if errors occur */ DS_2b_cube* DS_2b_cube::rotate_XY_to_YZ() { unsigned short int ox, oy, nx, ny, nz; int oz; //cause it needs to be -1 at one point unsigned short int nwX, nwY, nwZ; /* define parameters of the new DS */ nwX = wZ; nwY = wY; nwZ = wX; /* make a new cDSinfo struct */ DS_2b_cube * new_ds = new DS_2b_cube(nwZ, nwY, nwX); if (!new_ds){ return NULL; } new_ds->clear(); /* copy the YZ face of original to the XY face of new */ nz = 0; for (ox = 0; ox < wX; ox++){ //slice ny = 0; for (oy = 0; oy < wY; oy++){//row nx = 0; for (oz = (wZ-1); oz >= 0; oz--){//col /* transfer the memory BIT, since cDS is cleared on creation, only do so if '1'*/ new_ds->set_spot(this->get_spot((unsigned short)oz, oy, ox), nz, ny, nx); nx++; } ny++; } nz++; } return new_ds; } /* function that uses above functions, updates log if errors occur */ DS_2b_cube* DS_2b_cube::rotate_XY_to_ZX() { unsigned short int ox, oy, nx, ny, nz; int oz; //cause it needs to be -1 at one point unsigned short int nwX, nwY, nwZ; /* define parameters of the new DS */ nwX = wX; nwY = wZ; nwZ = wY; /* make a new cDSinfo struct */ DS_2b_cube * new_ds = new DS_2b_cube(nwZ, nwY, nwX); if (!new_ds){ return NULL; } new_ds->clear(); /* copy the XZ face of original to the XY face of new */ nz = 0; for (oy = 0; oy < wY; oy++){ //slice ny = 0; for (oz = (wZ-1); oz >= 0; oz--){//row nx = 0; for (ox = 0; ox < wX; ox++){//col /* transfer the memory BIT, since cDS is cleared on creation, only do so if '1'*/ new_ds->set_spot(this->get_spot((unsigned short)oz, oy, ox), nz, ny, nx); nx++; } ny++; } nz++; } return new_ds; } int DS_2b_cube::to_DS_cube(DS_cube * result_cube, unsigned short int first_slice, unsigned short int last_slice) { if ( (wX != result_cube->wX) || (wY != result_cube->wY) || (wZ != result_cube->wZ)) { write_to_log("DS_2b_cube to DS_cube: mismatched parameters"); return 1; } for (unsigned short int i = first_slice; i <= last_slice; i++) for (unsigned short int j = 0; j < wY; j++) for (unsigned short int k = 0; k < wX; k++) if (this->get_spot(i, j, k) == 3) result_cube->data[first_slice - i][j][k] = 0xFF; else if (this->get_spot(i, j, k) == 0) result_cube->data[first_slice - i][j][k] = 0x00; else if (this->get_spot(i, j, k) == 2) result_cube->data[first_slice - i][j][k] = 0xC0; else result_cube->data[first_slice - i][j][k] = 0x60; return 0; } //convert a 2 bit cube to a 1 bit cube, 0 or 1 vals become 0, 2 or 3 become 1 int DS_2b_cube::to_DS_1b_cube(DS_1b_cube * result_cube, unsigned short int first_slice, unsigned short int last_slice) { if ( (wX != result_cube->wX) || (wY != result_cube->wY) || (first_slice < 0) || (last_slice >= wZ)) { write_to_log("DS_2b_cube to DS_1b_cube: mismatched parameters"); return 1; } for (unsigned short int i = first_slice; i <= last_slice; i++) for (unsigned short int j = 0; j < wY; j++) for (unsigned short int k = 0; k < wX; k++) if (this->get_spot(i, j, k) == 3) result_cube->set_spot_1(i - first_slice, j, k); else if (this->get_spot(i, j, k) == 0) result_cube->set_spot_0(i - first_slice, j, k); else if (this->get_spot(i, j, k) == 2) result_cube->set_spot_1(i - first_slice, j, k); else result_cube->set_spot_0(i - first_slice, j, k); return 0; } bool DS_2b_cube::write_to_RAW_STACK(char * out_file) { ofstream fout(out_file, ios_base::binary); if(!fout.is_open()) { char text[512]; sprintf(text,"DS_1b_cube::write_to_RAW_STACK: error opening cBIN stack file %s", out_file); write_to_log(text); return false; } unsigned char val; for (unsigned short int i = 0; i < wZ; i++) for (unsigned short int j = 0; j < wY; j++) for (unsigned short int k = 0; k < wX; k++) { val = this->get_spot(i, j, k); if (val == 0) fout.put(0x00); else if (val == 1) fout.put(0x60); else if (val == 2) fout.put((unsigned char)0xC0); else fout.put((unsigned char)0xFF); } fout.close(); return true; //success } void DS_2b_cube::clear() { //for (unsigned short int i = 0; i < wZ; i++) // for (unsigned short int j = 0; j < wY; j++) memset(memory, 0, wZ*wY*cwX); }