root/OpenSceneGraph/trunk/src/osgPlugins/ive/Image.cpp @ 11807

Revision 11807, 4.7 kB (checked in by robert, 4 years ago)

From Craig Bosma, "Attached is an updated Image.cpp that fixes a very specific
compatability regression that was introduced almost 2 years ago in
r8834. The IVE version number was bumped to 32 because of the change
in binary layout, but the guard for reading/writing the new field was
checked against 31. Of course this only causes a problem (as for us)
when you've produced IVE files at version 31, which no longer load (or
crash) when loaded by newer OSG/IVE versions."

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/**********************************************************************
2 *
3 *    FILE:            Image.cpp
4 *
5 *    DESCRIPTION:    Read/Write osg::Image in binary format to disk.
6 *
7 *    CREATED BY:        Auto generated by iveGenerated
8 *                    and later modified by Rune Schmidt Jensen.
9 *
10 *    HISTORY:        Created 20.3.2003
11 *
12 *    Copyright 2003 VR-C
13 **********************************************************************/
14
15#include "Exception.h"
16#include "Image.h"
17#include "Object.h"
18
19#include <osg/Notify>
20
21using namespace ive;
22
23void Image::write(DataOutputStream* out)
24{
25    // Write Image's identification.
26    out->writeInt(IVEIMAGE);
27    // If the osg class is inherited by any other class we should also write this to file.
28    osg::Object*  obj = dynamic_cast<osg::Object*>(this);
29    if(obj){
30        ((ive::Object*)(obj))->write(out);
31    }
32    else
33        out_THROW_EXCEPTION("Image::write(): Could not cast this osg::Image to an osg::Object.");
34    // Write Image's properties.
35
36    // Write name
37    out->writeString(getFileName());
38
39    if ( out->getVersion() >= VERSION_0032)
40    {
41        out->writeInt((int)getWriteHint());
42    }       
43
44    // Write width, height, depth of image.
45    out->writeInt(s());
46    out->writeInt(t());
47    out->writeInt(r());
48   
49   
50    OSG_DEBUG << "image written '" << getFileName()<<"'\t"<<s()<<"\t"<<t()<<std::endl;
51
52    // Write formats, type and packing
53    out->writeInt(getInternalTextureFormat());
54    out->writeInt(getPixelFormat());
55    out->writeInt(getDataType());
56    out->writeInt(getPacking());
57
58    // Write modified tag.
59    out->writeInt(getModifiedCount());
60
61    // Write mipmapdata vector
62    int size = _mipmapData.size();
63    out->writeInt(size);
64    for(int i=0;i<size;i++)
65        out->writeInt(_mipmapData[i]);
66
67    // Write image data if any
68    out->writeBool(data()!=0);
69    if(data())
70    {
71        // Compute the size of image data and write this.
72        unsigned int size = getTotalSizeInBytesIncludingMipmaps();
73        out->writeInt(size);
74        // Write the data
75        out->writeCharArray((char*)data(), size);
76    }
77
78}
79
80void Image::read(DataInputStream* in)
81{
82    // Peek Image's identification.
83    int id = in->peekInt();
84    if(id == IVEIMAGE){
85        // Read Image's identification.
86        id = in->readInt();
87        // If the osg class is inherited by any other class we should also read this from file.
88        osg::Object*  obj = dynamic_cast<osg::Object*>(this);
89        if(obj){
90            ((ive::Object*)(obj))->read(in);
91        }
92        else
93            in_THROW_EXCEPTION("Image::read(): Could not cast this osg::Image to an osg::Object.");
94        // Read Image's properties.
95
96        // Read name
97        setFileName(in->readString());
98       
99        if ( in->getVersion() >= VERSION_0032)
100        {
101            setWriteHint((osg::Image::WriteHint)in->readInt());
102        }       
103
104        // Read width, height, depth of image.
105        int is=in->readInt();
106        int it=in->readInt();
107        int ir=in->readInt();
108
109        // Read formats, type and packing
110        GLint internalTextureFormat = (GLint) in->readInt();
111        GLenum pixelFormat = (GLenum) in->readInt();
112        GLenum dataType = (GLenum) in->readInt();;
113        unsigned int packing = (unsigned int)in->readInt();
114
115        // Read modified tag.
116        setModifiedCount((unsigned int)in->readInt());
117
118        // Read mipmapdata vector       
119        int size = in->readInt();
120        MipmapDataType mipmapData(size);
121
122        //if (size) std::cout<<"is mip mapped ";
123
124        for(int i=0;i<size;i++)
125        {
126            mipmapData[i]=(unsigned int)in->readInt();
127            //std::cout<<_mipmapData[i]<<"\t";
128        }
129        //std::cout<<std::endl;
130           
131
132        // Read image data if any
133        if(in->readBool())
134        {
135            unsigned int dataSize = (unsigned int)in->readInt();
136           
137            //static int totalSize = 0;
138           
139            //totalSize += dataSize;
140            //std::cout<<getFileName()<<" "<<is<<" "<<it<<std::endl;
141            //std::cout<<" dataSize = "<<dataSize<<"\tTotalSize = "<<totalSize<<std::endl;
142           
143           
144            //char* data = (char*)malloc (dataSize);
145            char* data =  new char[dataSize];
146            if(!data)
147                in_THROW_EXCEPTION("Image::read(): Unable to allocate memory for image data.");
148            in->readCharArray(data,dataSize);
149            setImage(is, it, ir, internalTextureFormat, pixelFormat,
150                dataType, (unsigned char* ) data, osg::Image::USE_NEW_DELETE, packing);
151        }
152
153        _mipmapData.swap(mipmapData);
154    }
155    else{
156        in_THROW_EXCEPTION("Image::read(): Expected Image identification.");
157    }
158}
Note: See TracBrowser for help on using the browser.