CanvasImage: improve fetching from http and add mime detection.

This commit is contained in:
Thomas Geymayer
2014-03-20 01:44:31 +01:00
parent 01a43b49a5
commit fd51518d92
4 changed files with 56 additions and 17 deletions

View File

@@ -661,31 +661,54 @@ namespace canvas
{
if( req->responseCode() != 200 )
{
SG_LOG( SG_GL,
SG_WARN,
"canvas::Image: failed to download '" << req->url() << "': "
<< req->responseReason() );
SG_LOG(SG_IO, SG_WARN, "failed to download '" << req->url() << "': "
<< req->responseReason());
return;
}
std::string ext = SGPath(req->path()).extension();
SG_LOG(SG_GL, SG_INFO, "canvas::Image: received " << req->url());
const std::string ext = SGPath(req->path()).extension(),
mime = req->responseMime();
osgDB::ReaderWriter* rw =
osgDB::Registry::instance()->getReaderWriterForExtension(ext);
SG_LOG(SG_IO, SG_INFO, "received " << req->url() <<
" (ext=" << ext << ", MIME=" << mime << ")");
std::istringstream img_data(
static_cast<HTTP::MemoryRequest*>(req)->responseBody()
);
const std::string& img_data =
static_cast<HTTP::MemoryRequest*>(req)->responseBody();
osgDB::Registry* reg = osgDB::Registry::instance();
osgDB::ReaderWriter::ReadResult result = rw->readImage(img_data);
// First try to detect image type by extension
osgDB::ReaderWriter* rw = reg->getReaderWriterForExtension(ext);
if( rw && loadImage(*rw, img_data, *req, "extension") )
return;
// Now try with MIME type
rw = reg->getReaderWriterForMimeType(req->responseMime());
if( rw && loadImage(*rw, img_data, *req, "MIME type") )
return;
SG_LOG(SG_IO, SG_WARN, "unable to read image '" << req->url() << "'");
}
//----------------------------------------------------------------------------
bool Image::loadImage( osgDB::ReaderWriter& reader,
const std::string& data,
HTTP::Request& request,
const std::string& type )
{
SG_LOG(SG_IO, SG_DEBUG, "use image reader detected by " << type);
std::istringstream data_strm(data);
osgDB::ReaderWriter::ReadResult result = reader.readImage(data_strm);
if( result.success() )
{
setImage( result.takeImage() );
else
SG_LOG( SG_GL,
SG_WARN,
"canvas::Image: failed to read image '" << req->url() << "': "
<< result.message() );
return true;
}
SG_LOG(SG_IO, SG_WARN, "failed to read image '" << request.url() << "': "
<< result.message());
return false;
}
} // namespace canvas

View File

@@ -105,6 +105,10 @@ namespace canvas
void setQuadUV(size_t index, const SGVec2f& tl, const SGVec2f& br);
void handleImageLoadDone(HTTP::Request*);
bool loadImage( osgDB::ReaderWriter& reader,
const std::string& data,
HTTP::Request& request,
const std::string& type );
osg::ref_ptr<osg::Texture2D> _texture;
// TODO optionally forward events to canvas

View File

@@ -272,6 +272,16 @@ std::string Request::hostAndPort() const
return u.substr(schemeEnd + 3, hostEnd - (schemeEnd + 3));
}
//------------------------------------------------------------------------------
std::string Request::responseMime() const
{
std::string content_type = _responseHeaders.get("content-type");
if( content_type.empty() )
return "application/octet-stream";
return content_type.substr(0, content_type.find(';'));
}
//------------------------------------------------------------------------------
void Request::setResponseLength(unsigned int l)
{

View File

@@ -118,6 +118,8 @@ public:
StringMap const& responseHeaders() const
{ return _responseHeaders; }
std::string responseMime() const;
virtual int responseCode() const
{ return _responseStatus; }