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

Revision 13041, 4.7 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • 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        for(osg::Image::DataIterator itr(this); itr.valid(); ++itr)
76        {
77            out->writeCharArray((char*)itr.data(), itr.size());
78        }
79    }
80
81}
82
83void Image::read(DataInputStream* in)
84{
85    // Peek Image's identification.
86    int id = in->peekInt();
87    if(id == IVEIMAGE){
88        // Read Image's identification.
89        id = in->readInt();
90        // If the osg class is inherited by any other class we should also read this from file.
91        osg::Object*  obj = dynamic_cast<osg::Object*>(this);
92        if(obj){
93            ((ive::Object*)(obj))->read(in);
94        }
95        else
96            in_THROW_EXCEPTION("Image::read(): Could not cast this osg::Image to an osg::Object.");
97        // Read Image's properties.
98
99        // Read name
100        setFileName(in->readString());
101
102        if ( in->getVersion() >= VERSION_0032)
103        {
104            setWriteHint((osg::Image::WriteHint)in->readInt());
105        }
106
107        // Read width, height, depth of image.
108        int is=in->readInt();
109        int it=in->readInt();
110        int ir=in->readInt();
111
112        // Read formats, type and packing
113        GLint internalTextureFormat = (GLint) in->readInt();
114        GLenum pixelFormat = (GLenum) in->readInt();
115        GLenum dataType = (GLenum) in->readInt();;
116        unsigned int packing = (unsigned int)in->readInt();
117
118        // Read modified tag.
119        setModifiedCount((unsigned int)in->readInt());
120
121        // Read mipmapdata vector
122        int size = in->readInt();
123        MipmapDataType mipmapData(size);
124
125        //if (size) std::cout<<"is mip mapped ";
126
127        for(int i=0;i<size;i++)
128        {
129            mipmapData[i]=(unsigned int)in->readInt();
130            //std::cout<<_mipmapData[i]<<"\t";
131        }
132        //std::cout<<std::endl;
133
134
135        // Read image data if any
136        if(in->readBool())
137        {
138            unsigned int dataSize = (unsigned int)in->readInt();
139
140            //static int totalSize = 0;
141
142            //totalSize += dataSize;
143            //std::cout<<getFileName()<<" "<<is<<" "<<it<<std::endl;
144            //std::cout<<" dataSize = "<<dataSize<<"\tTotalSize = "<<totalSize<<std::endl;
145
146
147            //char* data = (char*)malloc (dataSize);
148            char* data =  new char[dataSize];
149            if(!data)
150                in_THROW_EXCEPTION("Image::read(): Unable to allocate memory for image data.");
151            in->readCharArray(data,dataSize);
152            setImage(is, it, ir, internalTextureFormat, pixelFormat,
153                dataType, (unsigned char* ) data, osg::Image::USE_NEW_DELETE, packing);
154        }
155
156        _mipmapData.swap(mipmapData);
157    }
158    else{
159        in_THROW_EXCEPTION("Image::read(): Expected Image identification.");
160    }
161}
Note: See TracBrowser for help on using the browser.