| 1 | #include <osg/Image> |
|---|
| 2 | #include <osg/Notify> |
|---|
| 3 | #include <osg/Geode> |
|---|
| 4 | #include <osg/GL> |
|---|
| 5 | #include <osg/Endian> |
|---|
| 6 | |
|---|
| 7 | #include <osgDB/Registry> |
|---|
| 8 | #include <osgDB/FileUtils> |
|---|
| 9 | #include <osgDB/FileNameUtils> |
|---|
| 10 | |
|---|
| 11 | #include <sstream> |
|---|
| 12 | |
|---|
| 13 | using namespace osg; |
|---|
| 14 | |
|---|
| 15 | extern "C" |
|---|
| 16 | { |
|---|
| 17 | #include <zlib.h> |
|---|
| 18 | #include <png.h> |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #define PNG_ALPHA -2 |
|---|
| 24 | #define PNG_SOLID -1 |
|---|
| 25 | #define PNG_STENCIL 0 |
|---|
| 26 | |
|---|
| 27 | typedef struct |
|---|
| 28 | { |
|---|
| 29 | unsigned int Width; |
|---|
| 30 | unsigned int Height; |
|---|
| 31 | unsigned int Depth; |
|---|
| 32 | unsigned int Alpha; |
|---|
| 33 | } pngInfo; |
|---|
| 34 | |
|---|
| 35 | class PNGError |
|---|
| 36 | { |
|---|
| 37 | public: |
|---|
| 38 | PNGError(const char* message) |
|---|
| 39 | { |
|---|
| 40 | _message = "PNG lib error : "; |
|---|
| 41 | _message += message; |
|---|
| 42 | } |
|---|
| 43 | friend std::ostream& operator<<(std::ostream& stream, const PNGError& err) |
|---|
| 44 | { |
|---|
| 45 | stream << err._message; |
|---|
| 46 | return stream; |
|---|
| 47 | } |
|---|
| 48 | private: |
|---|
| 49 | std::string _message; |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | void user_error_fn(png_structp png_ptr, png_const_charp error_msg) |
|---|
| 53 | { |
|---|
| 54 | #ifdef OSG_CPP_EXCEPTIONS_AVAILABLE |
|---|
| 55 | throw PNGError(error_msg); |
|---|
| 56 | #else |
|---|
| 57 | OSG_WARN << "PNG lib warning : " << error_msg << std::endl; |
|---|
| 58 | #endif |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | void user_warning_fn(png_structp png_ptr, png_const_charp warning_msg) |
|---|
| 62 | { |
|---|
| 63 | OSG_WARN << "PNG lib warning : " << warning_msg << std::endl; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | void png_read_istream(png_structp png_ptr, png_bytep data, png_size_t length) |
|---|
| 67 | { |
|---|
| 68 | std::istream *stream = (std::istream*)png_get_io_ptr(png_ptr); |
|---|
| 69 | stream->read((char*)data,length); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void png_write_ostream(png_structp png_ptr, png_bytep data, png_size_t length) |
|---|
| 73 | { |
|---|
| 74 | std::ostream *stream = (std::ostream*)png_get_io_ptr(png_ptr); |
|---|
| 75 | stream->write((char*)data,length); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void png_flush_ostream(png_structp png_ptr) |
|---|
| 79 | { |
|---|
| 80 | std::ostream *stream = (std::ostream*)png_get_io_ptr(png_ptr); |
|---|
| 81 | stream->flush(); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | class ReaderWriterPNG : public osgDB::ReaderWriter |
|---|
| 85 | { |
|---|
| 86 | public: |
|---|
| 87 | ReaderWriterPNG() |
|---|
| 88 | { |
|---|
| 89 | supportsExtension("png","PNG Image format"); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | virtual const char* className() const { return "PNG Image Reader/Writer"; } |
|---|
| 93 | |
|---|
| 94 | WriteResult::WriteStatus writePngStream(std::ostream& fout, const osg::Image& img, int compression_level) const |
|---|
| 95 | { |
|---|
| 96 | png_structp png = NULL; |
|---|
| 97 | png_infop info = NULL; |
|---|
| 98 | int color; |
|---|
| 99 | png_bytep *rows = NULL; |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
|---|
| 103 | if(!png) return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | info = png_create_info_struct(png); |
|---|
| 107 | if(!info) return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | png_set_write_fn(png,&fout,png_write_ostream,png_flush_ostream); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | png_set_compression_level(png, compression_level); |
|---|
| 114 | |
|---|
| 115 | switch(img.getPixelFormat()) { |
|---|
| 116 | case(GL_LUMINANCE): color = PNG_COLOR_TYPE_GRAY; break; |
|---|
| 117 | case(GL_ALPHA): color = PNG_COLOR_TYPE_GRAY; break; |
|---|
| 118 | case(GL_LUMINANCE_ALPHA): color = PNG_COLOR_TYPE_GRAY_ALPHA ; break; |
|---|
| 119 | case(GL_RGB): color = PNG_COLOR_TYPE_RGB; break; |
|---|
| 120 | case(GL_RGBA): color = PNG_COLOR_TYPE_RGB_ALPHA; break; |
|---|
| 121 | case(GL_BGR): color = PNG_COLOR_TYPE_RGB; png_set_bgr(png); break; |
|---|
| 122 | case(GL_BGRA): color = PNG_COLOR_TYPE_RGB_ALPHA; png_set_bgr(png); break; |
|---|
| 123 | default: return WriteResult::ERROR_IN_WRITING_FILE; break; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | rows = new png_bytep[img.t()]; |
|---|
| 128 | for(int i = 0; i < img.t(); ++i) { |
|---|
| 129 | rows[i] = (png_bytep)img.data(0,img.t() - i - 1); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | png_set_IHDR(png, info, img.s(), img.t(), |
|---|
| 134 | 8, color, PNG_INTERLACE_NONE, |
|---|
| 135 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
|---|
| 136 | |
|---|
| 137 | png_write_info(png, info); |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | png_write_image(png, rows); |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | png_write_end(png, NULL); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | png_destroy_write_struct(&png,&info); |
|---|
| 147 | delete [] rows; |
|---|
| 148 | |
|---|
| 149 | return WriteResult::FILE_SAVED; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | ReadResult readPNGStream(std::istream& fin) const |
|---|
| 153 | { |
|---|
| 154 | int trans = PNG_ALPHA; |
|---|
| 155 | pngInfo pInfo; |
|---|
| 156 | pngInfo *pinfo = &pInfo; |
|---|
| 157 | |
|---|
| 158 | unsigned char header[8]; |
|---|
| 159 | png_structp png; |
|---|
| 160 | png_infop info; |
|---|
| 161 | png_infop endinfo; |
|---|
| 162 | png_bytep data; |
|---|
| 163 | png_bytep *row_p; |
|---|
| 164 | double fileGamma; |
|---|
| 165 | |
|---|
| 166 | png_uint_32 width, height; |
|---|
| 167 | int depth, color; |
|---|
| 168 | |
|---|
| 169 | png_uint_32 i; |
|---|
| 170 | png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | png_set_error_fn(png, png_get_error_ptr(png), user_error_fn, user_warning_fn); |
|---|
| 174 | |
|---|
| 175 | #ifdef OSG_CPP_EXCEPTIONS_AVAILABLE |
|---|
| 176 | try |
|---|
| 177 | #endif |
|---|
| 178 | { |
|---|
| 179 | info = png_create_info_struct(png); |
|---|
| 180 | endinfo = png_create_info_struct(png); |
|---|
| 181 | |
|---|
| 182 | fin.read((char*)header,8); |
|---|
| 183 | if (fin.gcount() == 8 && png_sig_cmp(header, 0, 8) == 0) |
|---|
| 184 | png_set_read_fn(png,&fin,png_read_istream); |
|---|
| 185 | else |
|---|
| 186 | { |
|---|
| 187 | png_destroy_read_struct(&png, &info, &endinfo); |
|---|
| 188 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 189 | } |
|---|
| 190 | png_set_sig_bytes(png, 8); |
|---|
| 191 | |
|---|
| 192 | png_read_info(png, info); |
|---|
| 193 | png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL); |
|---|
| 194 | |
|---|
| 195 | if (pinfo != NULL) |
|---|
| 196 | { |
|---|
| 197 | pinfo->Width = width; |
|---|
| 198 | pinfo->Height = height; |
|---|
| 199 | pinfo->Depth = depth; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | OSG_INFO<<"width="<<width<<" height="<<height<<" depth="<<depth<<std::endl; |
|---|
| 203 | if ( color == PNG_COLOR_TYPE_RGB) { OSG_INFO << "color == PNG_COLOR_TYPE_RGB "<<std::endl; } |
|---|
| 204 | if ( color == PNG_COLOR_TYPE_GRAY) { OSG_INFO << "color == PNG_COLOR_TYPE_GRAY "<<std::endl; } |
|---|
| 205 | if ( color == PNG_COLOR_TYPE_GRAY_ALPHA) { OSG_INFO << "color == PNG_COLOR_TYPE_GRAY_ALPHA"<<std::endl; } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | if (depth>8 && getCpuByteOrder()==osg::LittleEndian) |
|---|
| 209 | png_set_swap(png); |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA) |
|---|
| 213 | { |
|---|
| 214 | |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | if (color&PNG_COLOR_MASK_ALPHA && trans != PNG_ALPHA) |
|---|
| 218 | { |
|---|
| 219 | png_set_strip_alpha(png); |
|---|
| 220 | color &= ~PNG_COLOR_MASK_ALPHA; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | if (color == PNG_COLOR_TYPE_PALETTE) |
|---|
| 232 | png_set_palette_to_rgb(png); |
|---|
| 233 | if (color == PNG_COLOR_TYPE_GRAY && depth < 8) |
|---|
| 234 | { |
|---|
| 235 | #if PNG_LIBPNG_VER >= 10209 |
|---|
| 236 | png_set_expand_gray_1_2_4_to_8(png); |
|---|
| 237 | #else |
|---|
| 238 | |
|---|
| 239 | png_set_gray_1_2_4_to_8(png); |
|---|
| 240 | #endif |
|---|
| 241 | } |
|---|
| 242 | if (png_get_valid(png, info, PNG_INFO_tRNS)) |
|---|
| 243 | png_set_tRNS_to_alpha(png); |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | if (depth < 8) |
|---|
| 247 | png_set_packing(png); |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | double screenGamma = 2.2 / 1.0; |
|---|
| 253 | if (png_get_gAMA(png, info, &fileGamma)) |
|---|
| 254 | png_set_gamma(png, screenGamma, fileGamma); |
|---|
| 255 | else |
|---|
| 256 | png_set_gamma(png, screenGamma, 1.0/2.2); |
|---|
| 257 | |
|---|
| 258 | png_read_update_info(png, info); |
|---|
| 259 | |
|---|
| 260 | data = (png_bytep) new unsigned char [png_get_rowbytes(png, info)*height]; |
|---|
| 261 | row_p = new png_bytep [height]; |
|---|
| 262 | |
|---|
| 263 | bool StandardOrientation = true; |
|---|
| 264 | for (i = 0; i < height; i++) |
|---|
| 265 | { |
|---|
| 266 | if (StandardOrientation) |
|---|
| 267 | row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i]; |
|---|
| 268 | else |
|---|
| 269 | row_p[i] = &data[png_get_rowbytes(png, info)*i]; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | png_read_image(png, row_p); |
|---|
| 273 | delete [] row_p; |
|---|
| 274 | png_read_end(png, endinfo); |
|---|
| 275 | |
|---|
| 276 | GLenum pixelFormat = 0; |
|---|
| 277 | GLenum dataType = depth<=8?GL_UNSIGNED_BYTE:GL_UNSIGNED_SHORT; |
|---|
| 278 | switch(color) |
|---|
| 279 | { |
|---|
| 280 | case(PNG_SOLID): pixelFormat = GL_LUMINANCE; break; |
|---|
| 281 | case(PNG_ALPHA): pixelFormat = GL_ALPHA; break; |
|---|
| 282 | case(PNG_COLOR_TYPE_GRAY): pixelFormat =GL_LUMINANCE ; break; |
|---|
| 283 | case(PNG_COLOR_TYPE_GRAY_ALPHA): pixelFormat = GL_LUMINANCE_ALPHA; break; |
|---|
| 284 | case(PNG_COLOR_TYPE_RGB): pixelFormat = GL_RGB; break; |
|---|
| 285 | case(PNG_COLOR_TYPE_PALETTE): pixelFormat = GL_RGB; break; |
|---|
| 286 | case(PNG_COLOR_TYPE_RGB_ALPHA): pixelFormat = GL_RGBA; break; |
|---|
| 287 | default: break; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | if (pixelFormat == GL_RGB && png_get_channels(png, info) == 4) |
|---|
| 296 | pixelFormat = GL_RGBA; |
|---|
| 297 | |
|---|
| 298 | int internalFormat = pixelFormat; |
|---|
| 299 | |
|---|
| 300 | png_destroy_read_struct(&png, &info, &endinfo); |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | if (pixelFormat==0) |
|---|
| 305 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 306 | |
|---|
| 307 | osg::Image* pOsgImage = new osg::Image(); |
|---|
| 308 | |
|---|
| 309 | pOsgImage->setImage(width, height, 1, |
|---|
| 310 | internalFormat, |
|---|
| 311 | pixelFormat, |
|---|
| 312 | dataType, |
|---|
| 313 | data, |
|---|
| 314 | osg::Image::USE_NEW_DELETE); |
|---|
| 315 | |
|---|
| 316 | return pOsgImage; |
|---|
| 317 | |
|---|
| 318 | } |
|---|
| 319 | #ifdef OSG_CPP_EXCEPTIONS_AVAILABLE |
|---|
| 320 | catch (PNGError& err) |
|---|
| 321 | { |
|---|
| 322 | OSG_WARN << err << std::endl; |
|---|
| 323 | png_destroy_read_struct(&png, &info, &endinfo); |
|---|
| 324 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 325 | } |
|---|
| 326 | #endif |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | int getCompressionLevel(const osgDB::ReaderWriter::Options *options) const |
|---|
| 330 | { |
|---|
| 331 | if(options) { |
|---|
| 332 | std::istringstream iss(options->getOptionString()); |
|---|
| 333 | std::string opt; |
|---|
| 334 | while (iss >> opt) { |
|---|
| 335 | if(opt=="PNG_COMPRESSION") { |
|---|
| 336 | int level; |
|---|
| 337 | iss >> level; |
|---|
| 338 | return level; |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | return Z_DEFAULT_COMPRESSION; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | virtual ReadResult readObject(std::istream& fin,const osgDB::ReaderWriter::Options* options =NULL) const |
|---|
| 347 | { |
|---|
| 348 | return readImage(fin, options); |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const |
|---|
| 352 | { |
|---|
| 353 | return readImage(file, options); |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | virtual ReadResult readImage(std::istream& fin,const Options* =NULL) const |
|---|
| 357 | { |
|---|
| 358 | return readPNGStream(fin); |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 362 | { |
|---|
| 363 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 364 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 365 | |
|---|
| 366 | std::string fileName = osgDB::findDataFile( file, options ); |
|---|
| 367 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 368 | |
|---|
| 369 | osgDB::ifstream istream(fileName.c_str(), std::ios::in | std::ios::binary); |
|---|
| 370 | if(!istream) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 371 | ReadResult rr = readPNGStream(istream); |
|---|
| 372 | if(rr.validImage()) rr.getImage()->setFileName(file); |
|---|
| 373 | return rr; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | virtual WriteResult writeImage(const osg::Image& img,std::ostream& fout,const osgDB::ReaderWriter::Options *options) const |
|---|
| 377 | { |
|---|
| 378 | WriteResult::WriteStatus ws = writePngStream(fout,img,getCompressionLevel(options)); |
|---|
| 379 | return ws; |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | virtual WriteResult writeImage(const osg::Image &img,const std::string& fileName, const osgDB::ReaderWriter::Options *options) const |
|---|
| 383 | { |
|---|
| 384 | std::string ext = osgDB::getFileExtension(fileName); |
|---|
| 385 | if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 386 | |
|---|
| 387 | osgDB::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary); |
|---|
| 388 | if(!fout) return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 389 | |
|---|
| 390 | return writeImage(img,fout,options); |
|---|
| 391 | } |
|---|
| 392 | }; |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | REGISTER_OSGPLUGIN(png, ReaderWriterPNG) |
|---|