| 1 | #include <osgVolume/Layer> |
|---|
| 2 | |
|---|
| 3 | #include <iostream> |
|---|
| 4 | #include <string> |
|---|
| 5 | |
|---|
| 6 | #include <osg/Vec3> |
|---|
| 7 | #include <osg/Vec4> |
|---|
| 8 | #include <osg/io_utils> |
|---|
| 9 | |
|---|
| 10 | #include <osgDB/ReadFile> |
|---|
| 11 | #include <osgDB/Registry> |
|---|
| 12 | #include <osgDB/Input> |
|---|
| 13 | #include <osgDB/Output> |
|---|
| 14 | #include <osgDB/ParameterOutput> |
|---|
| 15 | #include <osgDB/FileNameUtils> |
|---|
| 16 | #include <osgDB/FileUtils> |
|---|
| 17 | |
|---|
| 18 | #include <osgVolume/VolumeTile> |
|---|
| 19 | |
|---|
| 20 | bool ImageLayer_readLocalData(osg::Object &obj, osgDB::Input &fr); |
|---|
| 21 | bool ImageLayer_writeLocalData(const osg::Object &obj, osgDB::Output &fw); |
|---|
| 22 | |
|---|
| 23 | REGISTER_DOTOSGWRAPPER(ImageLayer_Proxy) |
|---|
| 24 | ( |
|---|
| 25 | new osgVolume::ImageLayer, |
|---|
| 26 | "ImageLayer", |
|---|
| 27 | "Object Layer ImageLayer", |
|---|
| 28 | ImageLayer_readLocalData, |
|---|
| 29 | ImageLayer_writeLocalData |
|---|
| 30 | ); |
|---|
| 31 | |
|---|
| 32 | bool ImageLayer_readLocalData(osg::Object& obj, osgDB::Input &fr) |
|---|
| 33 | { |
|---|
| 34 | osgVolume::ImageLayer& layer = static_cast<osgVolume::ImageLayer&>(obj); |
|---|
| 35 | |
|---|
| 36 | bool itrAdvanced = false; |
|---|
| 37 | |
|---|
| 38 | if (fr.matchSequence("file %w") || fr.matchSequence("file %s")) |
|---|
| 39 | { |
|---|
| 40 | std::string filename = fr[1].getStr(); |
|---|
| 41 | if (!filename.empty()) |
|---|
| 42 | { |
|---|
| 43 | bool deferExternalLayerLoading = false; |
|---|
| 44 | |
|---|
| 45 | layer.setFileName(filename); |
|---|
| 46 | |
|---|
| 47 | if (!deferExternalLayerLoading) |
|---|
| 48 | { |
|---|
| 49 | |
|---|
| 50 | osgDB::FileType fileType = osgDB::fileType(filename); |
|---|
| 51 | if (fileType == osgDB::FILE_NOT_FOUND) |
|---|
| 52 | { |
|---|
| 53 | filename = osgDB::findDataFile(filename, fr.getOptions()); |
|---|
| 54 | fileType = osgDB::fileType(filename); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | osg::ref_ptr<osg::Image> image; |
|---|
| 58 | if (fileType == osgDB::DIRECTORY) |
|---|
| 59 | { |
|---|
| 60 | image = osgDB::readRefImageFile(filename+".dicom"); |
|---|
| 61 | |
|---|
| 62 | } |
|---|
| 63 | else if (fileType == osgDB::REGULAR_FILE) |
|---|
| 64 | { |
|---|
| 65 | image = osgDB::readRefImageFile( filename ); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | if (image.valid()) |
|---|
| 70 | { |
|---|
| 71 | osg::notify(osg::INFO)<<"osgVolume::ImageLayer image read: "<<filename<<" pixelFormat "<<std::hex<<image->getPixelFormat()<<" textureFormat "<<image->getInternalTextureFormat()<<" dataType "<<image->getDataType()<<std::dec<<std::endl; |
|---|
| 72 | |
|---|
| 73 | osg::ref_ptr<osgVolume::ImageDetails> details = dynamic_cast<osgVolume::ImageDetails*>(image->getUserData()); |
|---|
| 74 | osg::ref_ptr<osg::RefMatrix> matrix = details ? details->getMatrix() : dynamic_cast<osg::RefMatrix*>(image->getUserData()); |
|---|
| 75 | |
|---|
| 76 | layer.setImage(image.get()); |
|---|
| 77 | |
|---|
| 78 | if (details) |
|---|
| 79 | { |
|---|
| 80 | layer.setTexelOffset(details->getTexelOffset()); |
|---|
| 81 | layer.setTexelScale(details->getTexelScale()); |
|---|
| 82 | } |
|---|
| 83 | if (matrix) |
|---|
| 84 | { |
|---|
| 85 | layer.setLocator(new osgVolume::Locator(*matrix)); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | layer.rescaleToZeroToOneRange(); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | fr += 2; |
|---|
| 94 | itrAdvanced = true; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | return itrAdvanced; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | bool ImageLayer_writeLocalData(const osg::Object& obj, osgDB::Output& fw) |
|---|
| 102 | { |
|---|
| 103 | const osgVolume::ImageLayer& layer = static_cast<const osgVolume::ImageLayer&>(obj); |
|---|
| 104 | |
|---|
| 105 | if (!layer.getFileName().empty()) |
|---|
| 106 | { |
|---|
| 107 | fw.indent()<<"file "<< layer.getFileName() << std::endl; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | return true; |
|---|
| 111 | } |
|---|