Skip to content

Commit

Permalink
Code tidy - CompressedSlice class
Browse files Browse the repository at this point in the history
  • Loading branch information
alanspencer committed Oct 28, 2018
1 parent b747425 commit d28d18c
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 97 deletions.
166 changes: 87 additions & 79 deletions SPIERSview/src/compressedslice.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,64 @@
#include "compressedslice.h"
//#include <vtk_zlib.h>
#include <zlib.h>
#include <stdlib.h>
#include "spv.h"
#include "svobject.h"
#include "globals.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>

CompressedSlice::CompressedSlice(SVObject *parent, bool IsEmpty)
#include "spv.h"
#include "svobject.h"
#include "globals.h"

/**
* @brief CompressedSlice::CompressedSlice
* @param parent
* @param isEmpty
*/
CompressedSlice::CompressedSlice(SVObject *parent, bool isEmpty)
{
empty = IsEmpty; //for now
Object = parent;
data = 0; //null pointer
empty = isEmpty; //for now
svObject = parent;
data = nullptr;
datasize = 0;
grid = (unsigned char *)malloc(parent->spv->GridSize);
for (int i = 0; i < parent->spv->GridSize; i++) grid[i] = 0;
grid = static_cast<unsigned char *>(malloc(static_cast<size_t>(parent->spv->GridSize)));

for (int i = 0; i < parent->spv->GridSize; i++)
grid[i] = 0;
}

/**
* @brief CompressedSlice::~CompressedSlice
*/
CompressedSlice::~CompressedSlice()
{
if (data) free (data);
free (grid);
}

void CompressedSlice::Dump(QString Filename)
/**
* @brief CompressedSlice::dump
* Dumps the slice as a BMP
* @param filename
*/
void CompressedSlice::dump(QString filename)
{
//dump the slice as a BMP
// Work out checksum of data and datasize
int size = svObject->spv->size;
uLongf asize = static_cast<uLong>(size);

//work out checksum of data and datasize
int size = Object->spv->size;
uLongf asize = size;

//work out checksum of data and datasize
// Work out checksum of data and datasize
long total = 0;
for (int i = 0; i < datasize; i++) total += (long)(data[i]);
for (int i = 0; i < datasize; i++) total += static_cast<long>(data[i]);

int tot = (int)total;
int tot = static_cast<int>(total);
QString fn = QString("_%1_%2_.bmp").arg(datasize).arg(tot);
filename += fn;

Filename += fn;
unsigned char *mergearray = (unsigned char *)malloc(size); //THIS slice
uncompress(mergearray, &asize, data, datasize); //uncompress THIS slice
unsigned char *mergearray = static_cast<unsigned char *>(malloc(static_cast<size_t>(size))); //THIS slice
uncompress(mergearray, &asize, data, static_cast<uLong>(datasize)); //uncompress THIS slice

int width = Object->spv->iDim;
int height = Object->spv->jDim;
int width = svObject->spv->iDim;
int height = svObject->spv->jDim;

QImage bmp(width, height, QImage::Format_RGB32);
bmp.fill(0);
Expand All @@ -56,92 +69,87 @@ void CompressedSlice::Dump(QString Filename)
if (mergearray[y * width + x]) bmp.setPixel(x, y, mergearray[y * width + x]);
}

bmp.save(Filename);
bmp.save(filename);
free (mergearray);
}


void CompressedSlice::Merge(CompressedSlice *s, QString fn)
/**
* @brief CompressedSlice::merge
* @param s
* @param fn
*/
void CompressedSlice::merge(CompressedSlice *slice, QString filename)
{
if (s->empty) return;
//QFile logfile("c:/logfile.txt"); logfile.open(QIODevice::Append);
//QTextStream log(&logfile);
//log<<"New not empty\n";
Q_UNUSED(filename)

if (slice->empty) return;

if (empty)
{
//log<<"Old is empty\n";
//I'm empty, this one isn't, so can just copy data
datasize = s->datasize;
data = (unsigned char *)malloc(datasize);
memcpy(data, s->data, datasize); //copy it
datasize = slice->datasize;
data = static_cast<unsigned char *>(malloc(static_cast<size_t>(datasize)));
memcpy(data, slice->data, static_cast<size_t>(datasize)); //copy it
empty = false;

//and copy the grid
for (int i = 0; i < (Object->spv->GridSize); i++) grid[i] = s->grid[i];
for (int i = 0; i < (svObject->spv->GridSize); i++)
grid[i] = slice->grid[i];

return;
}

//Both have data.
//Decompress both arrays, merge, recompress
int size = Object->spv->size;
uLongf asize = size;

//log<<"Neither empty\n";

unsigned char *mergearray = (unsigned char *)malloc(size); //THIS slice
unsigned char *mergearray2 = (unsigned char *)malloc(size); //for the new one to be merged in

for (int i = 0; i < size; i++) mergearray[i] = (unsigned char) 0;
for (int i = 0; i < size; i++) mergearray2[i] = (unsigned char) 0;
int size = svObject->spv->size;
uLongf asize = static_cast<uLong>(size);

uncompress(mergearray, &asize, data, datasize); //uncompress THIS slice
unsigned char *mergearray = static_cast<unsigned char *>(malloc(static_cast<size_t>(size))); //THIS slice
unsigned char *mergearray2 = static_cast<unsigned char *>(malloc(static_cast<size_t>(size))); //for the new one to be merged in

asize = size;
uncompress(mergearray2, &asize, s->data, s->datasize); //uncompress the new slice
for (int i = 0; i < size; i++) mergearray[i] = static_cast<unsigned char>(0);
for (int i = 0; i < size; i++) mergearray2[i] = static_cast<unsigned char>(0);

//Do the merge
for (int i = 0; i < size; i++) if (mergearray[i]) mergearray2[i] = (unsigned char)255; //New array (merge2) will be the joint one
uncompress(mergearray, &asize, data, static_cast<uLong>(datasize)); //uncompress THIS slice

//Write as a bmp
/* int width=Object->spv->iDim;
int height=Object->spv->jDim;
asize = static_cast<uLong>(size);
uncompress(mergearray2, &asize, slice->data, static_cast<uLong>(slice->datasize)); //uncompress the new slice

QImage bmp(width,height,QImage::Format_RGB32);
bmp.fill(0);
for (int x=0; x<width; x++)
for (int y=0; y<height; y++)
{
if (mergearray2[y*width+x]) bmp.setPixel(x,y,255);
}
bmp.save(fn);
*/

//and recompress it
// Do the merge
for (int i = 0; i < size; i++)
if (mergearray[i])
mergearray2[i] = static_cast<unsigned char>(255); //New array (merge2) will be the joint one

// ... and recompress it
uLongf tempsize;

compress(mergearray, &tempsize, mergearray2, size); //compress it back to old array
datasize = (int)tempsize;
// Compress it back to old array
compress(mergearray, &tempsize, mergearray2, static_cast<uLong>(size));

datasize = static_cast<int>(tempsize);
free(data); //free up old memory
free(mergearray2);
data = (unsigned char *)malloc(datasize);
memcpy(data, mergearray, datasize);
//Dump(fn);
//data =(unsigned char *)realloc(mergearray,datasize); //squeeze to size
data = static_cast<unsigned char *>(malloc(static_cast<size_t>(datasize)));
memcpy(data, mergearray, static_cast<size_t>(datasize));

free(mergearray);
//merge the grids
for (int i = 0; i < (Object->spv->GridSize); i++) if (s->grid[i]) grid[i] = s->grid[i]; //copy non-zeroes

// Merge the grids
for (int i = 0; i < (svObject->spv->GridSize); i++)
if (slice->grid[i])
grid[i] = slice->grid[i]; //copy non-zeroes

return;
}

void CompressedSlice::GetFullData(unsigned char *d)
/**
* @brief CompressedSlice::getFullData
* @param dest
*/
void CompressedSlice::getFullData(unsigned char *dest)
{
int size = Object->spv->size;
uLongf asize = size;
//unsigned char *outarray=(unsigned char *)malloc(size);
uncompress(d, &asize, data, datasize);
int size = svObject->spv->size;
uLongf asize = static_cast<uLongf>(size);
uncompress(dest, &asize, data, static_cast<uLong>(datasize));
return;
}
20 changes: 13 additions & 7 deletions SPIERSview/src/compressedslice.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
#ifndef COMPRESSEDSLICE_H
#define COMPRESSEDSLICE_H

#include <QString>

class SVObject;

/**
* @brief The CompressedSlice class
*/
class CompressedSlice
{
public:
CompressedSlice(SVObject *parent, bool IsEmpty);
CompressedSlice(SVObject *parent, bool isEmpty);
~CompressedSlice();
void Merge(CompressedSlice *s, QString fn);
void Dump(QString Filename);

void GetFullData(unsigned char *);
void merge(CompressedSlice *slice, QString filename);
void dump(QString filename);
void getFullData(unsigned char *);

unsigned char * data; //this is the actual compressed data
int datasize;
unsigned char *data;
unsigned char *grid;
bool empty;
SVObject *Object;
int datasize;
SVObject *svObject;
};

#endif // COMPRESSEDSLICE_H
3 changes: 3 additions & 0 deletions SPIERSview/src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class MainWindow;

#define READTHEDOCS "https://spiersview.readthedocs.io/"

// Test file location
#define TESTDUMPLOCATION "C:/Documents and Settings/mdsutton/Desktop/Test/"

//SPIERSview Defines
#define GRIDSIZE 32
#define SCALE 2
Expand Down
4 changes: 2 additions & 2 deletions SPIERSview/src/marchingcubes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ void MarchingCubes::surfaceChunked()
for (int ii = 0; ii < 4; ii++)
{
if (kDim > ii && object->compressedslices[ii]->empty == false) //no need to copy pointers from backup - not yet touched
object->compressedslices[ii]->GetFullData(slicebuffers[ii + 2]);
object->compressedslices[ii]->getFullData(slicebuffers[ii + 2]);
else //blank them
{
slicebuffers[ii + 2] = blankpointer;
Expand Down Expand Up @@ -484,7 +484,7 @@ void MarchingCubes::surfaceChunked()
if (empty[5] == false)
{
slicebuffers[5] = slicebuffers_copy[5]; //put in data pointer
object->compressedslices[k + 3]->GetFullData((slicebuffers[5]));
object->compressedslices[k + 3]->getFullData((slicebuffers[5]));
}
else slicebuffers[5] = blankpointer;
//qDebug()<<"Slice"<<k+3<<"is"<<empty[5];
Expand Down
18 changes: 9 additions & 9 deletions SPIERSview/src/spvreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ void SPVreader::ReadSPV6(QString Filename)
}
else
{
o->compressedslices[ThisSlice]->Merge(newslice, "c:/test.bmp");
o->compressedslices[ThisSlice]->merge(newslice, QString(TESTDUMPLOCATION) + "test.bmp");
delete newslice;
//qDebug() << "h2";
}
Expand Down Expand Up @@ -1251,18 +1251,18 @@ int SPVreader::ProcessSPV(QString filename, unsigned int index, float *PassedMat
else
{
QString fname;
fname = QString("C:/Documents and Settings/mdsutton/Desktop/Test/cdump_%1_%2_a")
fname = QString(QString(TESTDUMPLOCATION) + "cdump_%1_%2_a")
.arg(p, 3, 10, QChar('0')).arg(SlicePointer, 3, 10, QChar('0'));
thisobj->compressedslices[SlicePointer]->Dump(fname);
fname = QString("C:/Documents and Settings/mdsutton/Desktop/Test/cdump_%1_%2_b")
thisobj->compressedslices[SlicePointer]->dump(fname);
fname = QString(QString(TESTDUMPLOCATION) + "cdump_%1_%2_b")
.arg(p, 3, 10, QChar('0')).arg(SlicePointer, 3, 10, QChar('0'));
s->Dump(fname);
fname = QString("C:/Documents and Settings/mdsutton/Desktop/Test/cdump_%1_%2_d")
s->dump(fname);
fname = QString(QString(TESTDUMPLOCATION) + "cdump_%1_%2_d")
.arg(p, 3, 10, QChar('0')).arg(SlicePointer, 3, 10, QChar('0'));
thisobj->compressedslices[SlicePointer]->Merge(s, fname);
fname = QString("C:/Documents and Settings/mdsutton/Desktop/Test/cdump_%1_%2_c")
thisobj->compressedslices[SlicePointer]->merge(s, fname);
fname = QString(QString(TESTDUMPLOCATION) + "cdump_%1_%2_c")
.arg(p, 3, 10, QChar('0')).arg(SlicePointer, 3, 10, QChar('0'));
thisobj->compressedslices[SlicePointer]->Dump(fname);
thisobj->compressedslices[SlicePointer]->dump(fname);
delete s;
}
SlicePointer++;
Expand Down

0 comments on commit d28d18c

Please sign in to comment.