Page 1 of 1

Image::loadRawData error

Posted: Thu Mar 14, 2019 9:09 am
by mlacht
Hey there,

I have a images with different widths and heights I want to add them to a Ogre::TexturePtr texture. This worked so far for some images for me which all had the same size. However when using imgs of different sizes I get this runtime error:

Code: Select all

[ WARN] [1552550498.076344280]: OGRE EXCEPTION(2:InvalidParametersException): Stream size does not match calculated image size in Image::loadRawData at /build/ogre-1.9-mqY1
wq/ogre-1.9-1.9.0+dfsg1/OgreMain/src/OgreImage.cpp (line 283)
terminate called after throwing an instance of 'Ogre::InvalidParametersException'
  what():  OGRE EXCEPTION(2:InvalidParametersException): Stream size does not match calculated image size in Image::loadRawData at /build/ogre-1.9-mqY1wq/ogre-1.9-1.9.0+dfs
g1/OgreMain/src/OgreImage.cpp (line 283)
I currently use:

Code: Select all

  Ogre::TexturePtr texture =          textureFromImage(tile.image(), "texture_" + name_suffix) ;

Code: Select all

Ogre::TexturePtr textureFromImage(const QImage &image,
                                  const std::string &name) {
  //  convert to 24bit rgb
  QImage converted = image.convertToFormat(QImage::Format_RGB888).mirrored();

  //  create texture
  Ogre::TexturePtr texture;
  Ogre::DataStreamPtr data_stream;
  data_stream.bind(new Ogre::MemoryDataStream((void *)converted.constBits(),
                                              converted.byteCount()));

  const Ogre::String res_group =
      Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME;
  Ogre::TextureManager &texture_manager = Ogre::TextureManager::getSingleton();
  //  swap byte order when going from QImage to Ogre
  texture = texture_manager.loadRawData(name, res_group, data_stream,
                                        converted.width(), converted.height(),
                                        Ogre::PF_B8G8R8, Ogre::TEX_TYPE_2D, 0);
  return texture;
}
any ideas how I have to adapt my textureFromImage method?

Re: Image::loadRawData error

Posted: Thu Mar 14, 2019 12:03 pm
by paroj
dont set faces to zero

Re: Image::loadRawData error

Posted: Thu Mar 14, 2019 8:26 pm
by mlacht
Hm what do you mean by faces? PIxelFormat?

I did not found faces in the definition:

Code: Select all

virtual TexturePtr Ogre::TextureManager::loadRawData 	( 	const String &  	name,
		const String &  	group,
		DataStreamPtr &  	stream,
		ushort  	uWidth,
		ushort  	uHeight,
		PixelFormat  	format,
		TextureType  	texType = TEX_TYPE_2D,
		int  	iNumMipmaps = MIP_DEFAULT,
		Real  	gamma = 1.0f,
		bool  	hwGammaCorrection = false }

Re: Image::loadRawData error

Posted: Thu Mar 14, 2019 10:59 pm
by paroj
sorry, I thought you were using Image::loadRawData directly.

Then verify whether data_stream->size() == converted.width()*converted.height()*3

Re: Image::loadRawData error

Posted: Fri Mar 15, 2019 8:37 am
by mlacht
Thanks that worked fine for me :)

That is my final code:

Code: Select all

Ogre::TexturePtr textureFromImage(const QImage &image,
                                  const std::string &name) {
  //  convert to 24bit rgb
  QImage converted = image.convertToFormat(QImage::Format_RGB888).mirrored();
 
  int size_of_stream = (converted.width()*converted.height()*3);

  //  create texture
  Ogre::TexturePtr texture;
  Ogre::DataStreamPtr data_stream;
   data_stream.bind(new Ogre::MemoryDataStream((void *)converted.constBits(),
                                               size_of_stream));
  std::cout<<" data_stream Size: "<<data_stream->size()<<"conv wi: "<<converted.width()<<" conv hi"<< converted.height()<<"  img wi:"<< image.width()<<" img_h: "<<image.height()<<std::endl;
 // Size: 196608conv wi: 256 conv hi256  img wi:256 img_h: 256  -> works
 // Size: 119660conv wi: 257 conv hi155  img wi:257 img_h: 155  -> fails with above mentioned error

  bool stream_size = (data_stream->size()) == (converted.width()*converted.height()*3);  
  std::cout<<stream_size<<" "<<data_stream->size()<<" "<<(converted.width()*converted.height()*3)<<std::endl;

  const Ogre::String res_group =
      Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME;
  Ogre::TextureManager &texture_manager = Ogre::TextureManager::getSingleton();
  //  swap byte order when going from QImage to Ogre
  texture = texture_manager.loadRawData(name, res_group, data_stream,
                                        converted.width(), converted.height(),
                                        Ogre::PF_B8G8R8, Ogre::TEX_TYPE_2D, 0);
  return texture;
}