2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgphotoalbum.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2003-12-23 05:05:10 +08:00
|
|
|
*/
|
|
|
|
|
2007-06-12 22:20:16 +08:00
|
|
|
|
2003-12-23 05:05:10 +08:00
|
|
|
#include "ImageReaderWriter.h"
|
|
|
|
|
|
|
|
#include <osg/Texture2D>
|
|
|
|
#include <osg/Geometry>
|
|
|
|
#include <osg/Geode>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
|
|
ImageReaderWriter::DataReference::DataReference():
|
|
|
|
_fileName(),
|
|
|
|
_resolutionX(256),
|
|
|
|
_resolutionY(256),
|
|
|
|
_center(0.625f,0.0f,0.0f),
|
|
|
|
_maximumWidth(1.25f,0.0f,0.0f),
|
|
|
|
_maximumHeight(0.0f,0.0f,1.0f),
|
2013-06-27 17:59:33 +08:00
|
|
|
_numPointsAcross(10),
|
2003-12-23 05:05:10 +08:00
|
|
|
_numPointsUp(10),
|
|
|
|
_backPage(false) {}
|
|
|
|
|
|
|
|
ImageReaderWriter::DataReference::DataReference(const std::string& fileName, unsigned int res, float width, float height,bool backPage):
|
|
|
|
_fileName(fileName),
|
|
|
|
_resolutionX(res),
|
|
|
|
_resolutionY(res),
|
|
|
|
_center(width*0.5f,0.0f,height*0.5f),
|
|
|
|
_maximumWidth(width,0.0f,0.0f),
|
|
|
|
_maximumHeight(0.0f,0.0f,height),
|
2013-06-27 17:59:33 +08:00
|
|
|
_numPointsAcross(10),
|
2003-12-23 05:05:10 +08:00
|
|
|
_numPointsUp(10),
|
|
|
|
_backPage(backPage) {}
|
|
|
|
|
|
|
|
ImageReaderWriter::DataReference::DataReference(const DataReference& rhs):
|
|
|
|
_fileName(rhs._fileName),
|
|
|
|
_resolutionX(rhs._resolutionX),
|
|
|
|
_resolutionY(rhs._resolutionY),
|
|
|
|
_center(rhs._center),
|
|
|
|
_maximumWidth(rhs._maximumWidth),
|
|
|
|
_maximumHeight(rhs._maximumHeight),
|
2013-06-27 17:59:33 +08:00
|
|
|
_numPointsAcross(rhs._numPointsAcross),
|
2003-12-23 05:05:10 +08:00
|
|
|
_numPointsUp(rhs._numPointsUp),
|
|
|
|
_backPage(rhs._backPage) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ImageReaderWriter::ImageReaderWriter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-11-23 23:29:52 +08:00
|
|
|
std::string ImageReaderWriter::local_insertReference(const std::string& fileName, unsigned int res, float width, float height, bool backPage)
|
2003-12-23 05:05:10 +08:00
|
|
|
{
|
|
|
|
std::stringstream ostr;
|
|
|
|
ostr<<"res_"<<res<<"_"<<fileName;
|
|
|
|
|
|
|
|
std::string myReference = ostr.str();
|
|
|
|
_dataReferences[myReference] = DataReference(fileName,res,width,height,backPage);
|
|
|
|
return myReference;
|
|
|
|
}
|
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Image> ImageReaderWriter::readImage_Archive(DataReference& dr, float& s,float& t)
|
2003-12-23 05:05:10 +08:00
|
|
|
{
|
2003-12-24 07:55:06 +08:00
|
|
|
for(PhotoArchiveList::iterator itr=_photoArchiveList.begin();
|
|
|
|
itr!=_photoArchiveList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Image> image = (*itr)->readImage(dr._fileName,dr._resolutionX,dr._resolutionY,s,t);
|
2003-12-24 07:55:06 +08:00
|
|
|
if (image) return image;
|
|
|
|
}
|
2003-12-23 05:05:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Image> ImageReaderWriter::readImage_DynamicSampling(DataReference& dr, float& s,float& t)
|
2003-12-23 05:05:10 +08:00
|
|
|
{
|
|
|
|
// record previous options.
|
|
|
|
osg::ref_ptr<osgDB::ReaderWriter::Options> previousOptions = osgDB::Registry::instance()->getOptions();
|
|
|
|
|
|
|
|
osg::ref_ptr<osgDB::ImageOptions> options = new osgDB::ImageOptions;
|
|
|
|
options->_destinationImageWindowMode = osgDB::ImageOptions::PIXEL_WINDOW;
|
|
|
|
options->_destinationPixelWindow.set(0,0,dr._resolutionX,dr._resolutionY);
|
|
|
|
|
|
|
|
osgDB::Registry::instance()->setOptions(options.get());
|
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(dr._fileName);
|
2003-12-23 05:05:10 +08:00
|
|
|
|
|
|
|
// restore previous options.
|
|
|
|
osgDB::Registry::instance()->setOptions(previousOptions.get());
|
|
|
|
|
|
|
|
s = options.valid()?options->_sourcePixelWindow.windowWidth:1.0f;
|
|
|
|
t = options.valid()?options->_sourcePixelWindow.windowHeight:1.0f;
|
|
|
|
|
|
|
|
return image;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-23 23:29:52 +08:00
|
|
|
osgDB::ReaderWriter::ReadResult ImageReaderWriter::local_readNode(const std::string& fileName, const Options*)
|
2003-12-23 05:05:10 +08:00
|
|
|
{
|
|
|
|
DataReferenceMap::iterator itr = _dataReferences.find(fileName);
|
2004-01-29 23:47:51 +08:00
|
|
|
if (itr==_dataReferences.end()) return osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED;
|
2003-12-23 05:05:10 +08:00
|
|
|
|
|
|
|
DataReference& dr = itr->second;
|
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Image> image;
|
2003-12-23 05:05:10 +08:00
|
|
|
float s=1.0f,t=1.0f;
|
2013-06-27 17:59:33 +08:00
|
|
|
|
2003-12-24 07:55:06 +08:00
|
|
|
// try to load photo from any loaded PhotoArchives
|
|
|
|
if (!_photoArchiveList.empty())
|
2003-12-23 05:05:10 +08:00
|
|
|
image = readImage_Archive(dr,s,t);
|
2013-06-27 17:59:33 +08:00
|
|
|
|
2003-12-24 07:55:06 +08:00
|
|
|
// not loaded yet, so try to load it directly.
|
|
|
|
if (!image)
|
2003-12-23 05:05:10 +08:00
|
|
|
image = readImage_DynamicSampling(dr,s,t);
|
|
|
|
|
|
|
|
|
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
float photoWidth = 0.0f;
|
|
|
|
float photoHeight = 0.0f;
|
|
|
|
float maxWidth = dr._maximumWidth.length();
|
|
|
|
float maxHeight = dr._maximumHeight.length();
|
|
|
|
|
|
|
|
|
|
|
|
if ((s/t)>(maxWidth/maxHeight))
|
|
|
|
{
|
|
|
|
// photo wider than tall relative to the required pictures size.
|
|
|
|
// so need to clamp the width to the maximum width and then
|
|
|
|
// set the height to keep the original photo aspect ratio.
|
|
|
|
|
|
|
|
photoWidth = maxWidth;
|
|
|
|
photoHeight = photoWidth*(t/s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// photo tall than wide relative to the required pictures size.
|
|
|
|
// so need to clamp the height to the maximum height and then
|
|
|
|
// set the width to keep the original photo aspect ratio.
|
|
|
|
|
|
|
|
photoHeight = maxHeight;
|
|
|
|
photoWidth = photoHeight*(s/t);
|
|
|
|
}
|
|
|
|
|
|
|
|
photoWidth*=0.95;
|
|
|
|
photoHeight*=0.95;
|
|
|
|
|
|
|
|
osg::Vec3 halfWidthVector(dr._maximumWidth*(photoWidth*0.5f/maxWidth));
|
|
|
|
osg::Vec3 halfHeightVector(dr._maximumHeight*(photoHeight*0.5f/maxHeight));
|
|
|
|
|
|
|
|
|
|
|
|
// set up the texture.
|
|
|
|
osg::Texture2D* texture = new osg::Texture2D;
|
|
|
|
texture->setImage(image);
|
2013-05-28 22:14:45 +08:00
|
|
|
texture->setResizeNonPowerOfTwoHint(false);
|
2003-12-23 05:05:10 +08:00
|
|
|
texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
|
|
|
|
texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
|
|
|
|
|
|
|
|
// set up the drawstate.
|
|
|
|
osg::StateSet* dstate = new osg::StateSet;
|
|
|
|
dstate->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
|
|
|
dstate->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
|
|
|
|
|
|
|
|
// set up the geoset.
|
|
|
|
osg::Geometry* geom = new osg::Geometry;
|
|
|
|
geom->setStateSet(dstate);
|
|
|
|
|
|
|
|
osg::Vec3Array* coords = new osg::Vec3Array(4);
|
|
|
|
|
|
|
|
if (!dr._backPage)
|
|
|
|
{
|
|
|
|
(*coords)[0] = dr._center - halfWidthVector + halfHeightVector;
|
|
|
|
(*coords)[1] = dr._center - halfWidthVector - halfHeightVector;
|
|
|
|
(*coords)[2] = dr._center + halfWidthVector - halfHeightVector;
|
|
|
|
(*coords)[3] = dr._center + halfWidthVector + halfHeightVector;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(*coords)[3] = dr._center - halfWidthVector + halfHeightVector;
|
|
|
|
(*coords)[2] = dr._center - halfWidthVector - halfHeightVector;
|
|
|
|
(*coords)[1] = dr._center + halfWidthVector - halfHeightVector;
|
|
|
|
(*coords)[0] = dr._center + halfWidthVector + halfHeightVector;
|
|
|
|
}
|
|
|
|
geom->setVertexArray(coords);
|
|
|
|
|
|
|
|
osg::Vec2Array* tcoords = new osg::Vec2Array(4);
|
|
|
|
(*tcoords)[0].set(0.0f,1.0f);
|
|
|
|
(*tcoords)[1].set(0.0f,0.0f);
|
|
|
|
(*tcoords)[2].set(1.0f,0.0f);
|
|
|
|
(*tcoords)[3].set(1.0f,1.0f);
|
|
|
|
geom->setTexCoordArray(0,tcoords);
|
|
|
|
|
|
|
|
osg::Vec4Array* colours = new osg::Vec4Array(1);
|
|
|
|
(*colours)[0].set(1.0f,1.0f,1.0,1.0f);
|
2013-06-27 17:59:33 +08:00
|
|
|
geom->setColorArray(colours, osg::Array::BIND_OVERALL);
|
2003-12-23 05:05:10 +08:00
|
|
|
|
|
|
|
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
|
|
|
|
|
|
|
|
// set up the geode.
|
|
|
|
osg::Geode* geode = new osg::Geode;
|
|
|
|
geode->addDrawable(geom);
|
|
|
|
|
|
|
|
return geode;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-01-29 23:47:51 +08:00
|
|
|
return osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED;
|
2003-12-23 05:05:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|