| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include "Exception.h" |
|---|
| 16 | #include "Image.h" |
|---|
| 17 | #include "Object.h" |
|---|
| 18 | |
|---|
| 19 | #include <osg/Notify> |
|---|
| 20 | |
|---|
| 21 | using namespace ive; |
|---|
| 22 | |
|---|
| 23 | void Image::write(DataOutputStream* out) |
|---|
| 24 | { |
|---|
| 25 | |
|---|
| 26 | out->writeInt(IVEIMAGE); |
|---|
| 27 | |
|---|
| 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 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | out->writeString(getFileName()); |
|---|
| 38 | |
|---|
| 39 | if ( out->getVersion() >= VERSION_0032) |
|---|
| 40 | { |
|---|
| 41 | out->writeInt((int)getWriteHint()); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 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 | |
|---|
| 53 | out->writeInt(getInternalTextureFormat()); |
|---|
| 54 | out->writeInt(getPixelFormat()); |
|---|
| 55 | out->writeInt(getDataType()); |
|---|
| 56 | out->writeInt(getPacking()); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | out->writeInt(getModifiedCount()); |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | int size = _mipmapData.size(); |
|---|
| 63 | out->writeInt(size); |
|---|
| 64 | for(int i=0;i<size;i++) |
|---|
| 65 | out->writeInt(_mipmapData[i]); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | out->writeBool(data()!=0); |
|---|
| 69 | if(data()) |
|---|
| 70 | { |
|---|
| 71 | |
|---|
| 72 | unsigned int size = getTotalSizeInBytesIncludingMipmaps(); |
|---|
| 73 | out->writeInt(size); |
|---|
| 74 | |
|---|
| 75 | for(osg::Image::DataIterator itr(this); itr.valid(); ++itr) |
|---|
| 76 | { |
|---|
| 77 | out->writeCharArray((char*)itr.data(), itr.size()); |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void Image::read(DataInputStream* in) |
|---|
| 84 | { |
|---|
| 85 | |
|---|
| 86 | int id = in->peekInt(); |
|---|
| 87 | if(id == IVEIMAGE){ |
|---|
| 88 | |
|---|
| 89 | id = in->readInt(); |
|---|
| 90 | |
|---|
| 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 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | setFileName(in->readString()); |
|---|
| 101 | |
|---|
| 102 | if ( in->getVersion() >= VERSION_0032) |
|---|
| 103 | { |
|---|
| 104 | setWriteHint((osg::Image::WriteHint)in->readInt()); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | int is=in->readInt(); |
|---|
| 109 | int it=in->readInt(); |
|---|
| 110 | int ir=in->readInt(); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 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 | |
|---|
| 119 | setModifiedCount((unsigned int)in->readInt()); |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | int size = in->readInt(); |
|---|
| 123 | MipmapDataType mipmapData(size); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | for(int i=0;i<size;i++) |
|---|
| 128 | { |
|---|
| 129 | mipmapData[i]=(unsigned int)in->readInt(); |
|---|
| 130 | |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | if(in->readBool()) |
|---|
| 137 | { |
|---|
| 138 | unsigned int dataSize = (unsigned int)in->readInt(); |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 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 | } |
|---|