diff --git a/examples/osgvolume/osgvolume.cpp b/examples/osgvolume/osgvolume.cpp index 3f4231264..f119fe239 100644 --- a/examples/osgvolume/osgvolume.cpp +++ b/examples/osgvolume/osgvolume.cpp @@ -2215,7 +2215,6 @@ int main( int argc, char **argv ) } break; } - break; }; } diff --git a/include/osgVolume/Layer b/include/osgVolume/Layer index 195f814ba..2b5a8018c 100644 --- a/include/osgVolume/Layer +++ b/include/osgVolume/Layer @@ -123,6 +123,18 @@ class OSGVOLUME_EXPORT ImageLayer : public Layer /** Return const image associated with layer. */ virtual const osg::Image* getImage() const { return _image.get(); } + + /** Compute the min and max pixel colors.*/ + bool computeMinMax(osg::Vec4& min, osg::Vec4& max); + + /** Apply color transformation to pixels using c' = offset + c * scale .*/ + void offsetAndScaleImage(const osg::Vec4& offset, const osg::Vec4& scale); + + /** Compute the min max range of the image, and then remap this to a 0 to 1 range.*/ + void rescaleToZeroToOneRange(); + + /** Compute the min color component of the image and then translate and pixels by this offset to make the new min component 0.*/ + void translateMinToZero(); virtual void dirty(); virtual void setModifiedCount(unsigned int value); diff --git a/src/osgPlugins/dicom/ReaderWriterDICOM.cpp b/src/osgPlugins/dicom/ReaderWriterDICOM.cpp index ca6bbae81..83a5085ac 100644 --- a/src/osgPlugins/dicom/ReaderWriterDICOM.cpp +++ b/src/osgPlugins/dicom/ReaderWriterDICOM.cpp @@ -122,7 +122,8 @@ class ReaderWriterDICOM : public osgDB::ReaderWriter osg::ref_ptr tile = new osgVolume::VolumeTile; tile->setVolume(volume.get()); - osg::ref_ptr layer= new osgVolume::ImageLayer(result.getImage()); + osg::ref_ptr layer= new osgVolume::ImageLayer(result.getImage()); + layer->rescaleToZeroToOneRange(); tile->setLayer(layer.get()); diff --git a/src/osgPlugins/osgVolume/ImageLayer.cpp b/src/osgPlugins/osgVolume/ImageLayer.cpp index c7c71867f..ef43859c2 100644 --- a/src/osgPlugins/osgVolume/ImageLayer.cpp +++ b/src/osgPlugins/osgVolume/ImageLayer.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include @@ -44,10 +46,31 @@ bool ImageLayer_readLocalData(osg::Object& obj, osgDB::Input &fr) if (!deferExternalLayerLoading) { - osg::ref_ptr image = fr.readImage(filename.c_str()); - if (image.valid()) + + osgDB::FileType fileType = osgDB::fileType(filename); + if (fileType == osgDB::FILE_NOT_FOUND) { + filename = osgDB::findDataFile(filename); + fileType = osgDB::fileType(filename); + } + + osg::ref_ptr image; + if (fileType == osgDB::DIRECTORY) + { + image = osgDB::readImageFile(filename+".dicom"); + + } + else if (fileType == osgDB::REGULAR_FILE) + { + image = osgDB::readImageFile( filename ); + } + + osg::notify(osg::NOTICE)<<"image "<getPixelFormat()<<" textureFormat "<getInternalTextureFormat()<<" dataType "<getDataType()< +#include #include #include using namespace osgVolume; Layer::Layer(): - _minFilter(osg::Texture::LINEAR_MIPMAP_LINEAR), + _minFilter(osg::Texture::LINEAR), _magFilter(osg::Texture::LINEAR) { } @@ -110,7 +111,58 @@ unsigned int ImageLayer::getModifiedCount() const else return _image->getModifiedCount(); } +bool ImageLayer::computeMinMax(osg::Vec4& minValue, osg::Vec4& maxValue) +{ + if (_image.valid()) return osg::computeMinMax(_image.get(), minValue, maxValue); + else return false; +} +void ImageLayer::offsetAndScaleImage(const osg::Vec4& offset, const osg::Vec4& scale) +{ + if (!_image) return; + + osg::offsetAndScaleImage(_image.get(), offset, scale); +} + +void ImageLayer::rescaleToZeroToOneRange() +{ + osg::Vec4 minValue, maxValue; + if (computeMinMax(minValue, maxValue)) + { + float minComponent = minValue[0]; + minComponent = osg::minimum(minComponent,minValue[1]); + minComponent = osg::minimum(minComponent,minValue[2]); + minComponent = osg::minimum(minComponent,minValue[3]); + + float maxComponent = maxValue[0]; + maxComponent = osg::maximum(maxComponent,maxValue[1]); + maxComponent = osg::maximum(maxComponent,maxValue[2]); + maxComponent = osg::maximum(maxComponent,maxValue[3]); + + float scale = 0.99f/(maxComponent-minComponent); + float offset = -minComponent * scale; + + offsetAndScaleImage(osg::Vec4(offset, offset, offset, offset), + osg::Vec4(scale, scale, scale, scale)); + } +} + +void ImageLayer::translateMinToZero() +{ + osg::Vec4 minValue, maxValue; + if (computeMinMax(minValue, maxValue)) + { + float minComponent = minValue[0]; + minComponent = osg::minimum(minComponent,minValue[1]); + minComponent = osg::minimum(minComponent,minValue[2]); + minComponent = osg::minimum(minComponent,minValue[3]); + + float offset = -minComponent; + + offsetAndScaleImage(osg::Vec4(offset, offset, offset, offset), + osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)); + } +} ///////////////////////////////////////////////////////////////////////////// //