| 33 | | #include <sstream> |
| 34 | | |
| 35 | | class ImageReaderWriter : public osgDB::ReaderWriter |
| 36 | | { |
| 37 | | public: |
| 38 | | virtual const char* className() { return "Image Reader"; } |
| 39 | | |
| 40 | | |
| 41 | | struct DataReference |
| 42 | | { |
| 43 | | DataReference(): |
| 44 | | _fileName(), |
| 45 | | _resolutionX(256), |
| 46 | | _resolutionY(256), |
| 47 | | _center(0.625f,0.0f,0.0f), |
| 48 | | _maximumWidth(1.25f,0.0f,0.0f), |
| 49 | | _maximumHeight(0.0f,0.0f,1.0f), |
| 50 | | _numPointsAcross(10), |
| 51 | | _numPointsUp(10), |
| 52 | | _backPage(false) {} |
| 53 | | |
| 54 | | DataReference(const std::string& fileName, unsigned int res, float width, float height,bool backPage): |
| 55 | | _fileName(fileName), |
| 56 | | _resolutionX(res), |
| 57 | | _resolutionY(res), |
| 58 | | _center(width*0.5f,0.0f,height*0.5f), |
| 59 | | _maximumWidth(width,0.0f,0.0f), |
| 60 | | _maximumHeight(0.0f,0.0f,height), |
| 61 | | _numPointsAcross(10), |
| 62 | | _numPointsUp(10), |
| 63 | | _backPage(backPage) {} |
| 64 | | |
| 65 | | DataReference(const DataReference& rhs): |
| 66 | | _fileName(rhs._fileName), |
| 67 | | _resolutionX(rhs._resolutionX), |
| 68 | | _resolutionY(rhs._resolutionY), |
| 69 | | _center(rhs._center), |
| 70 | | _maximumWidth(rhs._maximumWidth), |
| 71 | | _maximumHeight(rhs._maximumHeight), |
| 72 | | _numPointsAcross(rhs._numPointsAcross), |
| 73 | | _numPointsUp(rhs._numPointsUp), |
| 74 | | _backPage(rhs._backPage) {} |
| 75 | | |
| 76 | | std::string _fileName; |
| 77 | | unsigned int _resolutionX; |
| 78 | | unsigned int _resolutionY; |
| 79 | | osg::Vec3 _center; |
| 80 | | osg::Vec3 _maximumWidth; |
| 81 | | osg::Vec3 _maximumHeight; |
| 82 | | unsigned int _numPointsAcross; |
| 83 | | unsigned int _numPointsUp; |
| 84 | | bool _backPage; |
| 85 | | }; |
| 86 | | |
| 87 | | typedef std::map<std::string,DataReference> DataReferenceMap; |
| 88 | | DataReferenceMap _dataReferences; |
| 89 | | |
| 90 | | std::string insertReference(const std::string& fileName, unsigned int res, float width, float height, bool backPage) |
| 91 | | { |
| 92 | | std::stringstream ostr; |
| 93 | | ostr<<"res_"<<res<<"_"<<fileName; |
| 94 | | |
| 95 | | std::string myReference = ostr.str(); |
| 96 | | _dataReferences[myReference] = DataReference(fileName,res,width,height,backPage); |
| 97 | | return myReference; |
| 98 | | } |
| 99 | | |
| 100 | | |
| 101 | | |
| 102 | | virtual ReadResult readNode(const std::string& fileName, const Options*) |
| 103 | | { |
| 104 | | std::cout<<"Trying to read paged image "<<fileName<<std::endl; |
| 105 | | |
| 106 | | DataReferenceMap::iterator itr = _dataReferences.find(fileName); |
| 107 | | if (itr==_dataReferences.end()) return ReaderWriter::ReadResult::FILE_NOT_HANDLED; |
| 108 | | |
| 109 | | DataReference& dr = itr->second; |
| 110 | | |
| 111 | | // record previous options. |
| 112 | | osg::ref_ptr<osgDB::ReaderWriter::Options> previousOptions = osgDB::Registry::instance()->getOptions(); |
| 113 | | |
| 114 | | osg::ref_ptr<osgDB::ImageOptions> options = new osgDB::ImageOptions; |
| 115 | | options->_destinationImageWindowMode = osgDB::ImageOptions::PIXEL_WINDOW; |
| 116 | | options->_destinationPixelWindow.set(0,0,dr._resolutionX,dr._resolutionY); |
| 117 | | |
| 118 | | osgDB::Registry::instance()->setOptions(options.get()); |
| 119 | | |
| 120 | | osg::Image* image = osgDB::readImageFile(dr._fileName); |
| 121 | | |
| 122 | | // restore previous options. |
| 123 | | osgDB::Registry::instance()->setOptions(previousOptions.get()); |
| 124 | | |
| 125 | | if (image) |
| 126 | | { |
| 127 | | |
| 128 | | float s = options.valid()?options->_sourcePixelWindow.windowWidth:1.0f; |
| 129 | | float t = options.valid()?options->_sourcePixelWindow.windowHeight:1.0f; |
| 130 | | |
| 131 | | float photoWidth = 0.0f; |
| 132 | | float photoHeight = 0.0f; |
| 133 | | float maxWidth = dr._maximumWidth.length(); |
| 134 | | float maxHeight = dr._maximumHeight.length(); |
| 135 | | |
| 136 | | |
| 137 | | if ((s/t)>(maxWidth/maxHeight)) |
| 138 | | { |
| 139 | | // photo wider than tall relative to the required pictures size. |
| 140 | | // so need to clamp the width to the maximum width and then |
| 141 | | // set the height to keep the original photo aspect ratio. |
| 142 | | |
| 143 | | photoWidth = maxWidth; |
| 144 | | photoHeight = photoWidth*(t/s); |
| 145 | | } |
| 146 | | else |
| 147 | | { |
| 148 | | // photo tall than wide relative to the required pictures size. |
| 149 | | // so need to clamp the height to the maximum height and then |
| 150 | | // set the width to keep the original photo aspect ratio. |
| 151 | | |
| 152 | | photoHeight = maxHeight; |
| 153 | | photoWidth = photoHeight*(s/t); |
| 154 | | } |
| 155 | | |
| 156 | | photoWidth*=0.95; |
| 157 | | photoHeight*=0.95; |
| 158 | | |
| 159 | | osg::Vec3 halfWidthVector(dr._maximumWidth*(photoWidth*0.5f/maxWidth)); |
| 160 | | osg::Vec3 halfHeightVector(dr._maximumHeight*(photoHeight*0.5f/maxHeight)); |
| 161 | | |
| 162 | | |
| 163 | | // set up the texture. |
| 164 | | osg::Texture2D* texture = new osg::Texture2D; |
| 165 | | texture->setImage(image); |
| 166 | | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
| 167 | | texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); |
| 168 | | |
| 169 | | // set up the drawstate. |
| 170 | | osg::StateSet* dstate = new osg::StateSet; |
| 171 | | dstate->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
| 172 | | dstate->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); |
| 173 | | |
| 174 | | // set up the geoset. |
| 175 | | osg::Geometry* geom = new osg::Geometry; |
| 176 | | geom->setStateSet(dstate); |
| 177 | | |
| 178 | | osg::Vec3Array* coords = new osg::Vec3Array(4); |
| 179 | | |
| 180 | | if (!dr._backPage) |
| 181 | | { |
| 182 | | (*coords)[0] = dr._center - halfWidthVector + halfHeightVector; |
| 183 | | (*coords)[1] = dr._center - halfWidthVector - halfHeightVector; |
| 184 | | (*coords)[2] = dr._center + halfWidthVector - halfHeightVector; |
| 185 | | (*coords)[3] = dr._center + halfWidthVector + halfHeightVector; |
| 186 | | } |
| 187 | | else |
| 188 | | { |
| 189 | | (*coords)[3] = dr._center - halfWidthVector + halfHeightVector; |
| 190 | | (*coords)[2] = dr._center - halfWidthVector - halfHeightVector; |
| 191 | | (*coords)[1] = dr._center + halfWidthVector - halfHeightVector; |
| 192 | | (*coords)[0] = dr._center + halfWidthVector + halfHeightVector; |
| 193 | | } |
| 194 | | geom->setVertexArray(coords); |
| 195 | | |
| 196 | | osg::Vec2Array* tcoords = new osg::Vec2Array(4); |
| 197 | | (*tcoords)[0].set(0.0f,1.0f); |
| 198 | | (*tcoords)[1].set(0.0f,0.0f); |
| 199 | | (*tcoords)[2].set(1.0f,0.0f); |
| 200 | | (*tcoords)[3].set(1.0f,1.0f); |
| 201 | | geom->setTexCoordArray(0,tcoords); |
| 202 | | |
| 203 | | osg::Vec4Array* colours = new osg::Vec4Array(1); |
| 204 | | (*colours)[0].set(1.0f,1.0f,1.0,1.0f); |
| 205 | | geom->setColorArray(colours); |
| 206 | | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
| 207 | | |
| 208 | | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
| 209 | | |
| 210 | | // set up the geode. |
| 211 | | osg::Geode* geode = new osg::Geode; |
| 212 | | geode->addDrawable(geom); |
| 213 | | |
| 214 | | return geode; |
| 215 | | |
| 216 | | } |
| 217 | | else |
| 218 | | { |
| 219 | | return ReaderWriter::ReadResult::FILE_NOT_HANDLED; |
| 220 | | } |
| 221 | | |
| 222 | | |
| 223 | | } |
| 224 | | |
| 225 | | }; |
| 226 | | |
| 227 | | |
| 228 | | // now register with Registry to instantiate the above |
| 229 | | // reader/writer. |
| | 25 | #include "ImageReaderWriter.h" |
| | 26 | |
| | 27 | // now register with Registry to instantiate the above reader/writer, |
| | 28 | // declaring in main so that the code to set up PagedLOD can get a handle |
| | 29 | // to the ImageReaderWriter's |