#include "stdafx.h" #include "DS_slice.h" DS_slice::DS_slice(unsigned short int nwY, unsigned short int nwX) { register unsigned short int y; wY = nwY; wX = nwX; //allocate wY rows data = new unsigned char * [wY]; memory = new unsigned char [wY*wX]; //make sure memory allocation is successful if (!data || !memory) { write_to_log("DS_slice: out of memory while creating slice."); data = NULL; return; } //make pointers point into correct places for (y = 0; y < wY; y++) //link row point into memory rows data[y] = &memory[y*wX]; } DS_slice::~DS_slice(void) { //free the slice pointers delete data; delete memory; } void DS_slice::clear() { //for (unsigned short int j = 0; j < wY; j++) memset(memory, 0, wY*wX); } bool DS_slice::write_to_RAW(char* out_file) { ofstream fout(out_file, ios_base::binary); if(!fout.is_open()) { char text[512]; sprintf(text,"DS_slice::write_to_RAW: error opening output file %s", out_file); write_to_log(text); return false; } //for (unsigned short int j = 0; j < wY; j++) fout.write(reinterpret_cast<char*>(memory), wY*wX); fout.close(); return true; //success } bool DS_slice::read_from_file(Photoinfo* info, unsigned short int z_indx) { char* infile; if (z_indx < 0) { write_to_log("DS_slice::read_from_file: z_indx out of range."); return false; } //only way the input is not in stack form, is if user selected a raw file sequence if (info->type == INPUT_IS_RAW) infile = AppendDigitsAndExtension(info->raw_prefix, (info->startofz+z_indx-1), FindNumberOfDigits(info->startofz+info->sizeofz-1), INPUT_IS_RAW); else { infile = (char*) malloc(sizeof(char)*512); sprintf(infile, "%s", info->raw_prefix); } ifstream fin(infile, ios_base::binary); if(!fin.is_open()) { write_to_log("DS_slice: read_from_file: could not open input file."); return false; } //read images into memory fin.clear(); //offset is large if input was a stack if (info->type == INPUT_IS_RAW) fin.seekg(0); else fin.seekg(z_indx * wY * wX ); //for (unsigned short int y = 0; y < wY; y++) // fin.read(reinterpret_cast<char*>( (char*) data[y]), wX); fin.read(reinterpret_cast<char*>( (char*) memory), wY*wX); fin.close(); free(infile); return true; } ///////////////////////////////////////////////////////////////////////////////////////////// // SIGNED SHORT INT SLICE ///////////////////////////////////////////////////////////////////////////////////////////// DS_slice_si::DS_slice_si(unsigned short int nwY, unsigned short int nwX) { register unsigned short int y; wY = nwY; wX = nwX; //allocate wY rows data = new short int * [wY]; memory = new short int [wY*wX]; //make sure memory allocation is successful if (!data || !memory) { write_to_log("DS_slice_si: out of memory while creating slice."); data = NULL; return; } //make pointers point into correct places for (y = 0; y < wY; y++) //link row point into memory rows data[y] = &memory[y*wX]; } DS_slice_si::~DS_slice_si(void) { //free the slice pointers delete data; delete memory; } ///////////////////////////////////////////////////////////////////////////////////////////// // FLOAT SLICE ///////////////////////////////////////////////////////////////////////////////////////////// DS_slice_f::DS_slice_f(unsigned short int nwY, unsigned short int nwX) { register unsigned short int y; wY = nwY; wX = nwX; //allocate wY rows data = new float * [wY]; memory = new float [wY*wX]; //make sure memory allocation is successful if (!data || !memory) { write_to_log("DS_slice_f: out of memory while creating slice."); data = NULL; return; } //make pointers point into correct places for (y = 0; y < wY; y++) //link row point into memory rows data[y] = &memory[y*wX]; } DS_slice_f::~DS_slice_f(void) { //free the slice pointers delete data; delete memory; }