| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | #include <osg/Image> |
|---|
| 30 | #include <osg/Notify> |
|---|
| 31 | #include <osg/Geode> |
|---|
| 32 | #include <osg/GL> |
|---|
| 33 | |
|---|
| 34 | #include <osgDB/Registry> |
|---|
| 35 | #include <osgDB/FileNameUtils> |
|---|
| 36 | #include <osgDB/FileUtils> |
|---|
| 37 | #include <osgDB/fstream> |
|---|
| 38 | |
|---|
| 39 | #include <stdio.h> |
|---|
| 40 | #include <assert.h> |
|---|
| 41 | #include <string.h> |
|---|
| 42 | #include <stdlib.h> |
|---|
| 43 | #include <sstream> |
|---|
| 44 | |
|---|
| 45 | #include "hdrloader.h" |
|---|
| 46 | #include "hdrwriter.h" |
|---|
| 47 | |
|---|
| 48 | #ifndef GL_RGBA8 |
|---|
| 49 | #define GL_RGBA8 0x8058 |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | #ifndef GL_RGB8 |
|---|
| 53 | #define GL_RGB8 0x8051 |
|---|
| 54 | #endif |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | class ReaderWriterHDR : public osgDB::ReaderWriter |
|---|
| 58 | { |
|---|
| 59 | public: |
|---|
| 60 | ReaderWriterHDR() |
|---|
| 61 | { |
|---|
| 62 | supportsExtension("hdr","High Dynamic Range image format"); |
|---|
| 63 | supportsOption("RGBMUL",""); |
|---|
| 64 | supportsOption("RGB8",""); |
|---|
| 65 | supportsOption("RAW",""); |
|---|
| 66 | supportsOption("YFLIP",""); |
|---|
| 67 | supportsOption("NO_YFLIP",""); |
|---|
| 68 | } |
|---|
| 69 | virtual const char* className() { return "HDR Image Reader"; } |
|---|
| 70 | |
|---|
| 71 | virtual ReadResult readImage(const std::string &_file, const osgDB::ReaderWriter::Options *_opts) const |
|---|
| 72 | { |
|---|
| 73 | std::string filepath = osgDB::findDataFile(_file, _opts); |
|---|
| 74 | if (filepath.empty()) |
|---|
| 75 | return ReadResult::FILE_NOT_FOUND; |
|---|
| 76 | |
|---|
| 77 | if (!HDRLoader::isHDRFile(filepath.c_str())) |
|---|
| 78 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 79 | |
|---|
| 80 | float mul = 1.0f; |
|---|
| 81 | bool bYFlip = false; |
|---|
| 82 | bool convertToRGB8 = false; |
|---|
| 83 | bool rawRGBE = false; |
|---|
| 84 | if(_opts) |
|---|
| 85 | { |
|---|
| 86 | std::istringstream iss(_opts->getOptionString()); |
|---|
| 87 | std::string opt; |
|---|
| 88 | while (iss >> opt) |
|---|
| 89 | { |
|---|
| 90 | if(opt == "RGBMUL") |
|---|
| 91 | { |
|---|
| 92 | iss >> mul; |
|---|
| 93 | } |
|---|
| 94 | else if(opt == "RGB8") |
|---|
| 95 | { |
|---|
| 96 | convertToRGB8 = true; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | else if(opt == "RAW") |
|---|
| 111 | { |
|---|
| 112 | rawRGBE = true; |
|---|
| 113 | } |
|---|
| 114 | else if(opt == "YFLIP") |
|---|
| 115 | { |
|---|
| 116 | bYFlip = true; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | HDRLoaderResult res; |
|---|
| 122 | bool ret = HDRLoader::load(filepath.c_str(), rawRGBE, res); |
|---|
| 123 | if (!ret) |
|---|
| 124 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | osg::Image *img = new osg::Image; |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | if (convertToRGB8) |
|---|
| 131 | { |
|---|
| 132 | int nbPixs = res.width * res.height; |
|---|
| 133 | int nbElements = nbPixs * 3; |
|---|
| 134 | unsigned char *rgb = new unsigned char[ nbElements ]; |
|---|
| 135 | unsigned char *tt = rgb; |
|---|
| 136 | float *cols = res.cols; |
|---|
| 137 | |
|---|
| 138 | for (int i = 0; i < nbElements; i++) { |
|---|
| 139 | float element = *cols++; |
|---|
| 140 | element *= mul; |
|---|
| 141 | if (element < 0) element = 0; |
|---|
| 142 | else if (element > 1) element = 1; |
|---|
| 143 | int intElement = (int) (element * 255.0f); |
|---|
| 144 | *tt++ = intElement; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | delete [] res.cols; |
|---|
| 148 | |
|---|
| 149 | int pixelFormat = GL_RGB; |
|---|
| 150 | int dataType = GL_UNSIGNED_BYTE; |
|---|
| 151 | |
|---|
| 152 | img->setFileName(filepath.c_str()); |
|---|
| 153 | img->setImage( res.width, res.height, 1, |
|---|
| 154 | 3, |
|---|
| 155 | pixelFormat, |
|---|
| 156 | dataType, |
|---|
| 157 | (unsigned char*) rgb, |
|---|
| 158 | osg::Image::USE_NEW_DELETE); |
|---|
| 159 | } |
|---|
| 160 | else |
|---|
| 161 | { |
|---|
| 162 | int internalFormat; |
|---|
| 163 | int pixelFormat; |
|---|
| 164 | int dataType = GL_FLOAT; |
|---|
| 165 | |
|---|
| 166 | if (rawRGBE) |
|---|
| 167 | { |
|---|
| 168 | internalFormat = GL_RGBA8; |
|---|
| 169 | pixelFormat = GL_RGBA; |
|---|
| 170 | } else { |
|---|
| 171 | internalFormat = GL_RGB32F_ARB; |
|---|
| 172 | pixelFormat = GL_RGB; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | img->setFileName(filepath.c_str()); |
|---|
| 176 | img->setImage( res.width, res.height, 1, |
|---|
| 177 | internalFormat, |
|---|
| 178 | pixelFormat, |
|---|
| 179 | dataType, |
|---|
| 180 | (unsigned char*) res.cols, |
|---|
| 181 | osg::Image::USE_NEW_DELETE); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | if(bYFlip==true) img->flipVertical(); |
|---|
| 186 | |
|---|
| 187 | return img; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | virtual WriteResult writeImage(const osg::Image &image,const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 193 | { |
|---|
| 194 | std::string ext = osgDB::getFileExtension(file); |
|---|
| 195 | if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 196 | |
|---|
| 197 | osgDB::ofstream fout(file.c_str(), std::ios::out | std::ios::binary); |
|---|
| 198 | if(!fout) return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 199 | |
|---|
| 200 | return writeImage(image,fout,options); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | virtual WriteResult writeImage(const osg::Image& image,std::ostream& fout,const Options* options) const |
|---|
| 204 | { |
|---|
| 205 | |
|---|
| 206 | bool bYFlip = true; |
|---|
| 207 | bool rawRGBE = false; |
|---|
| 208 | |
|---|
| 209 | if(options) |
|---|
| 210 | { |
|---|
| 211 | std::istringstream iss(options->getOptionString()); |
|---|
| 212 | std::string opt; |
|---|
| 213 | while (iss >> opt) |
|---|
| 214 | { |
|---|
| 215 | if (opt=="NO_YFLIP") |
|---|
| 216 | { |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | bYFlip = false; |
|---|
| 222 | } |
|---|
| 223 | else if(opt=="RAW") |
|---|
| 224 | { |
|---|
| 225 | rawRGBE = true; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | if(rawRGBE==false) |
|---|
| 244 | { |
|---|
| 245 | if(image.getInternalTextureFormat()!=GL_RGB32F_ARB) |
|---|
| 246 | { |
|---|
| 247 | return WriteResult::FILE_NOT_HANDLED; |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | else |
|---|
| 251 | { |
|---|
| 252 | if(image.getInternalTextureFormat()!=GL_RGBA8) |
|---|
| 253 | { |
|---|
| 254 | return WriteResult::FILE_NOT_HANDLED; |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | osg::ref_ptr<osg::Image> source = new osg::Image(image,osg::CopyOp::DEEP_COPY_ALL); |
|---|
| 260 | |
|---|
| 261 | if(bYFlip==true) source->flipVertical(); |
|---|
| 262 | |
|---|
| 263 | bool success; |
|---|
| 264 | success = HDRWriter::writeHeader(source.get(),fout); |
|---|
| 265 | if(!success) |
|---|
| 266 | { |
|---|
| 267 | source = 0; |
|---|
| 268 | return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | success = HDRWriter::writeRLE(source.get(), fout); |
|---|
| 272 | |
|---|
| 273 | source = 0; |
|---|
| 274 | return (success)? WriteResult::FILE_SAVED : WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 275 | } |
|---|
| 276 | }; |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | REGISTER_OSGPLUGIN(hdr, ReaderWriterHDR) |
|---|