From 6f6c44446c575aadb315bd493cd629911353db88 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 6 Nov 2013 09:23:21 +0000 Subject: [PATCH] Introduced a cast(T) method into the ImageUtils template functor to allow code reading images to handle casting from source data to a form that a user can use more conviently --- examples/osgvolume/osgvolume.cpp | 2 +- include/osg/ImageUtils | 65 ++++++++++++++++++++++ include/osg/io_utils | 56 +++++++++++++++++++ src/osg/ImageUtils.cpp | 4 +- src/osgPlugins/dicom/ReaderWriterDICOM.cpp | 2 +- src/osgVolume/Layer.cpp | 2 +- 6 files changed, 126 insertions(+), 5 deletions(-) diff --git a/examples/osgvolume/osgvolume.cpp b/examples/osgvolume/osgvolume.cpp index 362f3032a..1c899687b 100644 --- a/examples/osgvolume/osgvolume.cpp +++ b/examples/osgvolume/osgvolume.cpp @@ -132,7 +132,7 @@ struct ScaleOperator inline void rgba(float& r,float& g,float& b,float& a) const { r*= _scale; g*=_scale; b*=_scale; a*=_scale; } }; -struct RecordRowOperator +struct RecordRowOperator : public osg::CastAndScaleToFloatOperation { RecordRowOperator(unsigned int num):_colours(num),_pos(0) {} diff --git a/include/osg/ImageUtils b/include/osg/ImageUtils index ba3ab70c6..5c7d1bd74 100644 --- a/include/osg/ImageUtils +++ b/include/osg/ImageUtils @@ -20,6 +20,68 @@ namespace osg { +template +void _readRow(unsigned int num, GLenum pixelFormat, const T* data, O& operation) +{ + switch(pixelFormat) + { + case(GL_INTENSITY): { for(unsigned int i=0;i +void readRow(unsigned int num, GLenum pixelFormat, GLenum dataType, const unsigned char* data, O& operation) +{ + switch(dataType) + { + case(GL_BYTE): _readRow(num, pixelFormat, (const char*)data, operation); break; + case(GL_UNSIGNED_BYTE): _readRow(num, pixelFormat, (const unsigned char*)data, operation); break; + case(GL_SHORT): _readRow(num, pixelFormat, (const short*) data, operation); break; + case(GL_UNSIGNED_SHORT): _readRow(num, pixelFormat, (const unsigned short*)data, operation); break; + case(GL_INT): _readRow(num, pixelFormat, (const int*) data, operation); break; + case(GL_UNSIGNED_INT): _readRow(num, pixelFormat, (const unsigned int*) data, operation); break; + case(GL_FLOAT): _readRow(num, pixelFormat, (const float*) data, operation); break; + case(GL_DOUBLE): _readRow(num, pixelFormat, (const double*) data, operation); break; + } +} + + +template +void readImage(const osg::Image* image, O& operation) +{ + if (!image) return; + + for(int r=0;rr();++r) + { + for(int t=0;tt();++t) + { + readRow(image->s(), image->getPixelFormat(), image->getDataType(), image->data(0,t,r), operation); + } + } +} + +/** Convinience method for making it easy to cast all pixel channels types to a unit float RGBA form.*/ +struct CastAndScaleToFloatOperation +{ + float cast(char v) { return static_cast(v)*(1.0f/128.0f); } + float cast(unsigned char v) { return static_cast(v)*(1.0f/255.0f); } + float cast(short v) { return static_cast(v)*(1.0f/32768.0f); } + float cast(unsigned short v) { return static_cast(v)*(1.0f/65535.0f); } + float cast(int v) { return static_cast(v)*(1.0f/2147483648.0f); } + float cast(unsigned int v) { return static_cast(v)*(1.0f/4294967295.0f); } + float cast(float v) { return v; } + float cast(double v) { return static_cast(v); } +}; + +#if 0 template void _readRow(unsigned int num, GLenum pixelFormat, const T* data,float scale, O& operation) { @@ -63,6 +125,9 @@ void readImage(const osg::Image* image, O& operation) } } } +#endif + + // example ModifyOperator // struct ModifyOperator diff --git a/include/osg/io_utils b/include/osg/io_utils index ee42b30b5..6f6d9aaf0 100644 --- a/include/osg/io_utils +++ b/include/osg/io_utils @@ -25,6 +25,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -240,6 +243,59 @@ inline std::istream& operator >> (std::istream& input, Vec4s& vec) } + +////////////////////////////////////////////////////////////////////////// +// Vec2i steaming operators. +inline std::ostream& operator << (std::ostream& output, const Vec2i& vec) +{ + output << vec._v[0] << " " + << vec._v[1]; + return output; // to enable cascading +} + +inline std::istream& operator >> (std::istream& input, Vec2i& vec) +{ + input >> vec._v[0] >> std::ws >> vec._v[1]; + return input; +} + +////////////////////////////////////////////////////////////////////////// +// Vec3i steaming operators. +inline std::ostream& operator << (std::ostream& output, const Vec3i& vec) +{ + output << vec._v[0] << " " + << vec._v[1] << " " + << vec._v[2]; + return output; // to enable cascading +} + +inline std::istream& operator >> (std::istream& input, Vec3i& vec) +{ + input >> vec._v[0] >> std::ws >> vec._v[1] >> std::ws >> vec._v[2]; + return input; +} + +////////////////////////////////////////////////////////////////////////// +// Vec4i steaming operators. +inline std::ostream& operator << (std::ostream& output, const Vec4i& vec) +{ + output << vec._v[0] << " " + << vec._v[1] << " " + << vec._v[2] << " " + << vec._v[3]; + return output; // to enable cascading +} + +inline std::istream& operator >> (std::istream& input, Vec4i& vec) +{ + input >> vec._v[0] >> std::ws + >> vec._v[1] >> std::ws + >> vec._v[2] >> std::ws + >> vec._v[3]; + return input; +} + + ////////////////////////////////////////////////////////////////////////// // Matrixf steaming operators. inline std::ostream& operator<< (std::ostream& os, const Matrixf& m ) diff --git a/src/osg/ImageUtils.cpp b/src/osg/ImageUtils.cpp index 5feb0bb85..0f59b7322 100644 --- a/src/osg/ImageUtils.cpp +++ b/src/osg/ImageUtils.cpp @@ -22,7 +22,7 @@ namespace osg { -struct FindRangeOperator +struct FindRangeOperator : public CastAndScaleToFloatOperation { FindRangeOperator(): _rmin(FLT_MAX), @@ -168,7 +168,7 @@ void _copyRowAndScale(const unsigned char* src, GLenum srcDataType, unsigned cha } } -struct RecordRowOperator +struct RecordRowOperator : public CastAndScaleToFloatOperation { RecordRowOperator(unsigned int num):_colours(num),_pos(0) {} diff --git a/src/osgPlugins/dicom/ReaderWriterDICOM.cpp b/src/osgPlugins/dicom/ReaderWriterDICOM.cpp index 00f1872d7..1fd23b054 100644 --- a/src/osgPlugins/dicom/ReaderWriterDICOM.cpp +++ b/src/osgPlugins/dicom/ReaderWriterDICOM.cpp @@ -168,7 +168,7 @@ class ReaderWriterDICOM : public osgDB::ReaderWriter tile->setVolumeTechnique(new osgVolume::RayTracedTechnique()); osg::ref_ptr layer= new osgVolume::ImageLayer(result.getImage()); - layer->rescaleToZeroToOneRange(); + //layer->rescaleToZeroToOneRange(); osgVolume::SwitchProperty* sp = new osgVolume::SwitchProperty; sp->setActiveProperty(0); diff --git a/src/osgVolume/Layer.cpp b/src/osgVolume/Layer.cpp index 90e4fb0ee..d67b8542f 100644 --- a/src/osgVolume/Layer.cpp +++ b/src/osgVolume/Layer.cpp @@ -491,7 +491,7 @@ osg::Image* osgVolume::createNormalMapTexture(osg::Image* image_3d) // // applyTransferFunction // -struct ApplyTransferFunctionOperator +struct ApplyTransferFunctionOperator : public osg::CastAndScaleToFloatOperation { ApplyTransferFunctionOperator(osg::TransferFunction1D* tf, unsigned char* data): _tf(tf),