Starting from 2021-07-01, all LRZ GitLab users will be required to explicitly accept the GitLab Terms of Service. Please see the detailed information at https://doku.lrz.de/display/PUBLIC/GitLab and make sure that your projects conform to the requirements.

Commit c56d5c41 authored by schultezub's avatar schultezub
Browse files

* templated ImageDataRAM

 * added ImageDataRAMTraits
 * changed types in WeaklyTypedPointer

git-svn-id: https://camplinux.in.tum.de/svn/campvis/trunk@177 bb408c1c-ae56-11e1-83d9-df6b3e0c105e
parent 49f29e8a
......@@ -110,23 +110,23 @@ namespace TUMVis {
if (_endianess != localEndianess) {
// This is not the most beautiful design, but unfortunately swapEndian needs to know the number of bytes at compiletime...
switch (_type) {
case WeaklyTypedPointer::UCHAR:
case WeaklyTypedPointer::CHAR:
case WeaklyTypedPointer::UINT8:
case WeaklyTypedPointer::INT8:
for (size_t i = 0; i < numElements; ++i)
data[i] = EndianHelper::swapEndian(data[i]);
break;
case WeaklyTypedPointer::USHORT:
case WeaklyTypedPointer::SHORT: {
short* tmp = reinterpret_cast<short*>(data);
case WeaklyTypedPointer::UINT16:
case WeaklyTypedPointer::INT16: {
int16_t* tmp = reinterpret_cast<int16_t*>(data);
for (size_t i = 0; i < numElements; ++i)
tmp[i] = EndianHelper::swapEndian(tmp[i]);
break;
}
case WeaklyTypedPointer::UINT:
case WeaklyTypedPointer::INT: {
int* tmp = reinterpret_cast<int*>(data);
case WeaklyTypedPointer::UINT32:
case WeaklyTypedPointer::INT32: {
int32_t* tmp = reinterpret_cast<int32_t*>(data);
for (size_t i = 0; i < numElements; ++i)
tmp[i] = EndianHelper::swapEndian(tmp[i]);
break;
......@@ -139,9 +139,9 @@ namespace TUMVis {
break;
}
case WeaklyTypedPointer::ULONG:
case WeaklyTypedPointer::LONG: {
long* tmp = reinterpret_cast<long*>(data);
case WeaklyTypedPointer::UINT64:
case WeaklyTypedPointer::INT64: {
int64_t* tmp = reinterpret_cast<int64_t*>(data);
for (size_t i = 0; i < numElements; ++i)
tmp[i] = EndianHelper::swapEndian(tmp[i]);
break;
......
......@@ -16,7 +16,7 @@ namespace TUMVis {
/**
* Subclass of ImageData offering access to image data stored in binary form on the local harddisk.
*
* \todo
* \todo Number of channels
*/
class ImageDataDisk : public ImageData {
public:
......
#include "imagedataram.h"
#include <cstring>
namespace TUMVis {
/*
const std::string ImageDataRAM::loggerCat_ = "TUMVis.core.datastructures.ImageDataRAM";
ImageDataRAM::ImageDataRAM(size_t dimensionality, const tgt::svec3& size, WeaklyTypedPointer data)
......@@ -10,6 +11,8 @@ namespace TUMVis {
, _data(data)
{
Full<int, 2> f;
f.foo(tgt::ivec2(1, 2));
}
ImageDataRAM::~ImageDataRAM() {
......@@ -56,5 +59,5 @@ namespace TUMVis {
const WeaklyTypedPointer& ImageDataRAM::getImageData() const {
return _data;
}
}*/
}
\ No newline at end of file
......@@ -8,29 +8,37 @@
#include "core/tools/typetraits.h"
#include "core/tools/weaklytypedpointer.h"
#include <cstring>
#include <fstream>
#include <string>
namespace TUMVis {
/**
* Subclass of ImageData storing the image data in the local memory.
* Templated class storing ImageData in the local memory. Each image element is of type \a T.
*
* \note Although you can use ImageDataRAM directly (which works perfectly well), it is
* encouraged to use ImageDataRAMTraits for a clearer approach and better support
* of the ImageData converters.
* \sa ImageDataRAMTraits
* \todo implement padding
* \tparam T base class of elements
*/
template<typename T>
class ImageDataRAM : public ImageData {
public:
/**
* Creates a new ImageData disk representation.
*
* \note ImageDataRam takes ownership of \a data.
* \param dimensionality Dimensionality of data
* \param size Size of this image (number of elements per dimension)
* \param data WeaklyTypedPointer to the image data
* \param data Pointer to the image data, must not be 0, ImageDataRAM takes ownership of that pointer.
*/
ImageDataRAM(
size_t dimensionality,
const tgt::svec3& size,
WeaklyTypedPointer data
T* data = 0
);
/**
......@@ -42,33 +50,168 @@ namespace TUMVis {
/**
* \see AbstractData::clone()
**/
virtual ImageDataRAM* clone() const;
virtual ImageDataRAM<T>* clone() const;
/**
* \see ImageData::getSubImage
*/
virtual ImageDataRAM* getSubImage(const tgt::svec3& llf, const tgt::svec3& urb) const;
virtual ImageDataRAM<T>* getSubImage(const tgt::svec3& llf, const tgt::svec3& urb) const;
/**
* Returns the WeaklyTypedPointer to the image data.
* \return Pointer to the image data.
*/
WeaklyTypedPointer& getImageData();
T* getImageData();
/**
* Returns the WeaklyTypedPointer to the image data.
* \return Pointer to the image data.
*/
const WeaklyTypedPointer& getImageData() const;
const T* getImageData() const;
private:
protected:
WeaklyTypedPointer _data; ///< pointer to image data
T* _data; ///< pointer to image data
static const std::string loggerCat_;
};
// - Template implementation ----------------------------------------------------------------------
template<typename T>
TUMVis::ImageDataRAM<T>::ImageDataRAM(size_t dimensionality, const tgt::svec3& size, T* data)
: ImageData(dimensionality, size)
, _data(data)
{
}
template<typename T>
TUMVis::ImageDataRAM<T>::~ImageDataRAM() {
delete _data;
}
template<typename T>
ImageDataRAM<T>* TUMVis::ImageDataRAM<T>::clone() const {
size_t numElements = tgt::hmul(_size);
T* newData = new T[numElements];
memcpy(newData, _data, numElements * sizeof(T));
return new ImageDataRAM<T>(_dimensionality, _size, newData);
}
template<typename T>
ImageDataRAM<T>* TUMVis::ImageDataRAM<T>::getSubImage(const tgt::svec3& llf, const tgt::svec3& urb) const {
tgtAssert(tgt::hand(tgt::lessThan(llf, urb)), "Coordinates in LLF must be component-wise smaller than the ones in URB!");
tgt::svec3 newSize = urb - llf;
if (newSize == _size) {
// nothing has changed, just provide a copy:
return clone();
}
size_t numBytesPerElement = sizeof(T);
size_t numElements = tgt::hmul(_size);
T* newData = new T[numElements];
// slice image data into new array
size_t index = 0;
for (size_t z = llf.z; z < urb.z; ++z) {
for (size_t y = llf.y; y < urb.y; ++y) {
size_t offset = llf.x + (y * _size.x) + (z * _size.y * _size.x);
memcpy(newData + index, _data + offset, newSize.x * numBytesPerElement);
index += newSize.x;
}
}
return new ImageDataRAM<T>(_dimensionality, newSize, newData);
}
template<typename T>
T* TUMVis::ImageDataRAM<T>::getImageData() {
return _data;
}
template<typename T>
const T* TUMVis::ImageDataRAM<T>::getImageData() const {
return _data;
}
template<typename T>
const std::string TUMVis::ImageDataRAM<T>::loggerCat_ = "TUMVis.core.datastructures.ImageDataRAM";
// - Traits and template specializations ----------------------------------------------------------
/**
* Collection of traits for ImageDataRAM<T> and its stored image data.
* Although you can use ImageDataRAM directly (which works perfectly well), it is encouraged
* to use these traits for a clearer approach and better support of the ImageData converters.
* \sa ImageDataRAM
* \tparam BASETYPE Base data type
* \tparam NUMCHANNELS Number of channels of each element
*/
template<typename BASETYPE, size_t NUMCHANNELS>
struct ImageDataRAMTraits {
/**
* Returns the size of the element base type in bytes.
*/
static const size_t basetypeSize = sizeof(BASETYPE);
/**
* Returns the number of channels per image element.
*/
static const size_t numChannels = NUMCHANNELS;
/**
* Returns the number of bytes required for one image element.
*/
static const size_t elementSize = sizeof(BASETYPE) * NUMCHANNELS;;
};
/**
* Template specialization of ImageDataRAMTraits with one channel.
* \sa ImageDataRAMTraits, ImageDataRAM
* \tparam BASETYPE Base data type
*/
template<typename BASETYPE>
struct ImageDataRAMTraits<BASETYPE, 1> {
typedef ImageDataRAM<BASETYPE> ImageType;
};
/**
* Template specialization of ImageDataRAMTraits with two channels.
* \sa ImageDataRAMTraits, ImageDataRAM
* \tparam BASETYPE Base data type
*/
template<typename BASETYPE>
struct ImageDataRAMTraits<BASETYPE, 2> {
typedef ImageDataRAM< tgt::Vector2<BASETYPE> > ImageType;
};
/**
* Template specialization of ImageDataRAMTraits with three channels.
* \sa ImageDataRAMTraits, ImageDataRAM
* \tparam BASETYPE Base data type
*/
template<typename BASETYPE>
struct ImageDataRAMTraits<BASETYPE, 3> {
typedef ImageDataRAM< tgt::Vector3<BASETYPE> > ImageType;
};
/**
* Template specialization of ImageDataRAMTraits with four channels.
* \sa ImageDataRAMTraits, ImageDataRAM
* \tparam BASETYPE Base data type
*/
template<typename BASETYPE>
class ImageDataRAMTraits<BASETYPE, 4> {
typedef ImageDataRAM< tgt::Vector4<BASETYPE> > ImageType;
};
// - Convenience typedefs -------------------------------------------------------------------------
}
#endif // IMAGEDATARAM_H__
......@@ -2,6 +2,7 @@
#define WEAKLYTYPEDPOINTER_H__
#include "tgt/assert.h"
#include "tgt/types.h"
namespace TUMVis {
......@@ -14,15 +15,15 @@ namespace TUMVis {
* Base data type.
**/
enum PointerType {
UCHAR, ///< unsigned char
CHAR, ///< char
USHORT, ///< unsigned short
SHORT, ///< short
UINT, ///< unsigned int
INT, ///< int
UINT8, ///< unsigned 8 bit integer
INT8, ///< signed 8 bit integer
UINT16, ///< unsigned 16 bit integer
INT16, ///< signed 16 bit integer
UINT32, ///< unsigned 32 bit integer
INT32, ///< signed 32 bit integer
FLOAT, ///< float
ULONG, ///< unsigned long
LONG, ///< long
UINT64, ///< unsigned 64 bit integer
INT64, ///< signed 64 bit integer q
DOUBLE ///< double
};
......@@ -36,24 +37,20 @@ namespace TUMVis {
*/
static size_t numBytes(PointerType pt) {
switch (pt) {
case WeaklyTypedPointer::UCHAR:
return sizeof(unsigned char);
case WeaklyTypedPointer::CHAR:
return sizeof(char);
case WeaklyTypedPointer::USHORT:
return sizeof(unsigned short);
case WeaklyTypedPointer::SHORT:
return sizeof(short);
case WeaklyTypedPointer::UINT:
return sizeof(unsigned int);
case WeaklyTypedPointer::INT:
return sizeof(int);
case WeaklyTypedPointer::UINT8:
case WeaklyTypedPointer::INT8:
return 1;
case WeaklyTypedPointer::UINT16:
case WeaklyTypedPointer::INT16:
return 2;
case WeaklyTypedPointer::UINT32:
case WeaklyTypedPointer::INT32:
return sizeof(4);
case WeaklyTypedPointer::FLOAT:
return sizeof(float);
case WeaklyTypedPointer::ULONG:
return sizeof(unsigned long);
case WeaklyTypedPointer::LONG:
return sizeof(long);
case WeaklyTypedPointer::UINT64:
case WeaklyTypedPointer::INT64:
return sizeof(8);
case WeaklyTypedPointer::DOUBLE:
return sizeof(double);
default:
......@@ -106,33 +103,33 @@ namespace TUMVis {
// - Template Specialization ----------------------------------------------------------------------
template<>
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UCHAR> {
static size_t numBytesLocal() { return sizeof(unsigned char); };
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT8> {
static size_t numBytesLocal() { return 1; };
};
template<>
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::CHAR> {
static size_t numBytes() { return sizeof(char); };
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT8> {
static size_t numBytes() { return 1; };
};
template<>
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::USHORT> {
static size_t numBytes() { return sizeof(unsigned short); };
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT16> {
static size_t numBytes() { return 2; };
};
template<>
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::SHORT> {
static size_t numBytes() { return sizeof(short); };
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT16> {
static size_t numBytes() { return 2; };
};
template<>
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT> {
static size_t numBytes() { return sizeof(unsigned int); };
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT32> {
static size_t numBytes() { return 4; };
};
template<>
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT> {
static size_t numBytes() { return sizeof(int); };
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT32> {
static size_t numBytes() { return 4; };
};
template<>
......@@ -141,13 +138,13 @@ namespace TUMVis {
};
template<>
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::ULONG> {
static size_t numBytes() { return sizeof(unsigned long); };
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT64> {
static size_t numBytes() { return 8; };
};
template<>
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::LONG> {
static size_t numBytes() { return sizeof(long); };
struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT64> {
static size_t numBytes() { return 8; };
};
template<>
......
......@@ -4,6 +4,7 @@
#include "tgt/filesystem.h"
#include "core/datastructures/imagedatadisk.h"
#include "core/datastructures/imagedataram.h"
#include "core/tools/textfileparser.h"
/*
......@@ -18,6 +19,8 @@ namespace TUMVis {
, _url("url", "Image URL", "")
{
_properties.addProperty(&_url);
ImageDataRAMTraits<float, 2>::ImageType* img = new ImageDataRAMTraits<float, 2>::ImageType(2, tgt::svec3(2, 2, 1), 0);
}
MhdImageReader::~MhdImageReader() {
......@@ -57,17 +60,17 @@ namespace TUMVis {
// element type
std::string et = tfp.getString("ElementType");
if (et == "MET_UCHAR")
pt = WeaklyTypedPointer::UCHAR;
pt = WeaklyTypedPointer::UINT8;
else if (et == "MET_CHAR")
pt = WeaklyTypedPointer::CHAR;
pt = WeaklyTypedPointer::INT8;
else if (et == "MET_USHORT")
pt = WeaklyTypedPointer::USHORT;
pt = WeaklyTypedPointer::UINT16;
else if (et == "MET_SHORT")
pt = WeaklyTypedPointer::SHORT;
pt = WeaklyTypedPointer::INT16;
else if (et == "MET_UINT")
pt = WeaklyTypedPointer::UINT;
pt = WeaklyTypedPointer::UINT32;
else if (et == "MET_INT")
pt = WeaklyTypedPointer::INT;
pt = WeaklyTypedPointer::INT32;
else if (et == "MET_FLOAT")
pt = WeaklyTypedPointer::FLOAT;
else if (et == "MET_DOUBLE")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment