root/OpenSceneGraph/trunk/src/osgPlugins/hdr/hdrwriter.h
@
12292
| Revision 12292, 3.2 kB (checked in by robert, 2 years ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | /* |
| 2 | The following code was based on code from the following location: |
| 3 | http://www.graphics.cornell.edu/online/formats/rgbe/ |
| 4 | |
| 5 | It includes the following information : |
| 6 | |
| 7 | "This file contains code to read and write four byte rgbe file format |
| 8 | developed by Greg Ward. It handles the conversions between rgbe and |
| 9 | pixels consisting of floats. The data is assumed to be an array of floats. |
| 10 | By default there are three floats per pixel in the order red, green, blue. |
| 11 | (RGBE_DATA_??? values control this.) Only the mimimal header reading and |
| 12 | writing is implemented. Each routine does error checking and will return |
| 13 | a status value as defined below. This code is intended as a skeleton so |
| 14 | feel free to modify it to suit your needs. |
| 15 | |
| 16 | (Place notice here if you modified the code.) |
| 17 | posted to http://www.graphics.cornell.edu/~bjw/ |
| 18 | written by Bruce Walter (bjw@graphics.cornell.edu) 5/26/95 |
| 19 | based on code written by Greg Ward" |
| 20 | |
| 21 | modified for OSG September 2007 david.spilling@gmail.com |
| 22 | */ |
| 23 | |
| 24 | #ifndef HDRWRITER_H |
| 25 | #define HDRWRITER_H |
| 26 | |
| 27 | #include <osg/Image> |
| 28 | |
| 29 | #include <iosfwd> |
| 30 | #include <math.h> |
| 31 | |
| 32 | class HDRWriter { |
| 33 | public: |
| 34 | // all return "true" for success, "false" for failure. |
| 35 | static bool writeRLE(const osg::Image *img, std::ostream& fout); |
| 36 | static bool writeRAW(const osg::Image *img, std::ostream& fout); |
| 37 | static bool writeHeader(const osg::Image *img, std::ostream& fout); |
| 38 | |
| 39 | protected: |
| 40 | |
| 41 | // can read or write pixels in chunks of any size including single pixels |
| 42 | static bool writePixelsNoRLE( std::ostream& fout, float* data, int numpixels); |
| 43 | static bool writePixelsRAW( std::ostream& fout, unsigned char* data, int numpixels); |
| 44 | |
| 45 | // read or write run length encoded files |
| 46 | // must be called to read or write whole scanlines |
| 47 | static bool writeBytesRLE(std::ostream& fout, unsigned char *data, int numbytes); |
| 48 | static bool writePixelsRLE( std::ostream& fout, float* data, int scanline_width, int num_scanlines ); |
| 49 | |
| 50 | // inline conversions |
| 51 | inline static void float2rgbe(unsigned char rgbe[4], float red, float green, float blue); |
| 52 | inline static void rgbe2float(float *red, float *green, float *blue, unsigned char rgbe[4]); |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | /* standard conversion from float pixels to rgbe pixels */ |
| 57 | /* note: you can remove the "inline"s if your compiler complains about it */ |
| 58 | inline void HDRWriter::float2rgbe(unsigned char rgbe[4], float red, float green, float blue) |
| 59 | { |
| 60 | float v; |
| 61 | int e; |
| 62 | |
| 63 | v = red; |
| 64 | if (green > v) v = green; |
| 65 | if (blue > v) v = blue; |
| 66 | if (v < 1e-32) { |
| 67 | rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0; |
| 68 | } |
| 69 | else { |
| 70 | v = frexp(v,&e) * 256.0/v; |
| 71 | rgbe[0] = (unsigned char) (red * v); |
| 72 | rgbe[1] = (unsigned char) (green * v); |
| 73 | rgbe[2] = (unsigned char) (blue * v); |
| 74 | rgbe[3] = (unsigned char) (e + 128); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* standard conversion from rgbe to float pixels */ |
| 79 | /* note: Ward uses ldexp(col+0.5,exp-(128+8)). However we wanted pixels */ |
| 80 | /* in the range [0,1] to map back into the range [0,1]. */ |
| 81 | inline void HDRWriter::rgbe2float(float *red, float *green, float *blue, unsigned char rgbe[4]) |
| 82 | { |
| 83 | float f; |
| 84 | |
| 85 | if (rgbe[3]) { /*nonzero pixel*/ |
| 86 | f = ldexp(1.0,rgbe[3]-(int)(128+8)); |
| 87 | *red = rgbe[0] * f; |
| 88 | *green = rgbe[1] * f; |
| 89 | *blue = rgbe[2] * f; |
| 90 | } |
| 91 | else |
| 92 | *red = *green = *blue = 0.0; |
| 93 | } |
| 94 | |
| 95 | #endif |
Note: See TracBrowser
for help on using the browser.
