Hi Dirk,
Dirk Reiners wrote:
Hi Thomas,
On Wed, 2005-11-02 at 16:11 +0100, Thomas Beer wrote:
Hi,
I'm trying to load textures from a Qt resource file. Therefore I load
the file into a QByteArray and pass this to the restoreData-Method of
the appropriate ImageFileType.
Grepping the headers restoreData should be implemented for DAT, JPG, MTD
and PNG. However only the PNG-Implementation seems to work (haven't
tried DAT):
MTD: restoreData returns 0, gives no further errors and results in a
black (empty) texture if I use that image.
PNG: restoreData returns 772865 while buffer/filesize was 772881 but
image seems to load ok (no visible errors).
JPG: does not return, program aborts with the following message:
Corrupt JPEG data: 1420 extraneous bytes before marker 0xd8
Invalid JPEG file structure: two SOI markers
I also tried loading with STL-streams (with code copied from
testImageMem), fopen/fread and Win32 native file reading instead of the
Qt-Resources but it makes no difference!
That's weird, they used to work quite well, but I haven't tried that in
a while.
As a workaround I could use the Qt-Image class to load the textures so
far, but I'm wondering whether the restoreData-Implementations are buggy?
They shouldn't be. Could you write a Tutorial-style example that let's
us reproduce it?
Okay, attached.
restoreData returns the bytes of the pixel data it has decoded, not the
number of input bytes processed as I assumed, so the returned value from
PNGImageFiletype was ok.
I already tracked the problem down a bit:
It seems that JPGs with EXIF-headers work when reading from file but
crash when reading from memory.
Thomas
Another one: I also want to load a TTF from memory/resources. Is there a
way to do it or do I have to derive and implement my own font loading
(AFAIR it's just a FT_New_Memory_face(buf) instead of FT_New_face
(file) with freetype).
I can't answer that one. Patrick?
Dirk
#include "OSGConfig.h"
#include <iostream>
#include <fstream>
#include "OSGBaseFunctions.h"
#include "OSGLog.h"
#include "OSGImageFileHandler.h"
#include "OSGImage.h"
#include "OSGImageFields.h"
#include "OSGImageFileType.h"
OSG_USING_NAMESPACE
int main (int argc, char **argv)
{
osgInit(argc,argv);
if(argc != 2)
{
std::cerr << "usage: " << argv[0] << " imagefile" << std::endl;
return -1;
}
ImagePtr img = Image::create();
ImagePtr memImg = Image::create();
if(img->read(argv[1]) == false)
{
std::cerr << "could not load image " << argv[1] << std::endl;
return -2;
}
ImageFileTypeP fType = ImageFileHandler::the().getFileType(NULL,
argv[1]);
if(!fType)
{
// should not happen as we just read that filetype...
return -3;
}
std::ifstream ins(argv[1], std::ios::in | std::ios::binary );
unsigned long readData, dataSize = 0;
OSG::UChar8 * data;
if (ins) {
ins.seekg(0, std::ios::end );
dataSize = ins.tellg();
if (dataSize) {
data = new OSG::UChar8 [dataSize];
ins.seekg ( 0, std::ios::beg );
ins.read ( reinterpret_cast<char*>(data), dataSize );
readData = ins.gcount();
if (readData == dataSize) {
fType->restoreData(memImg, data, dataSize);
}
}
delete [] data;
}
std::cerr << "comparing images. that may take a while if they match..."
<< std::endl;
if(*img.getCPtr() == *memImg.getCPtr())
std::cerr << "Image " << argv[1] << " successfully decoded from
memory!" << std::endl;
return 0;
}