root/OpenSceneGraph/trunk/src/osgPlugins/hdr/hdrwriter.h @ 13041

Revision 13041, 3.1 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
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
32class HDRWriter {
33public:
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
39protected:
40
41// can read or write pixels in chunks of any size including single pixels
42    static bool writeNoRLE( std::ostream& fout, const osg::Image* image);
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
49// inline conversions
50    inline static void float2rgbe(unsigned char rgbe[4], float red, float green, float blue);
51    inline static void rgbe2float(float *red, float *green, float *blue, unsigned char rgbe[4]);
52};
53
54
55/* standard conversion from float pixels to rgbe pixels */
56/* note: you can remove the "inline"s if your compiler complains about it */
57inline void HDRWriter::float2rgbe(unsigned char rgbe[4], float red, float green, float blue)
58{
59  float v;
60  int e;
61
62  v = red;
63  if (green > v) v = green;
64  if (blue > v) v = blue;
65  if (v < 1e-32) {
66    rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
67  }
68  else {
69    v = frexp(v,&e) * 256.0/v;
70    rgbe[0] = (unsigned char) (red * v);
71    rgbe[1] = (unsigned char) (green * v);
72    rgbe[2] = (unsigned char) (blue * v);
73    rgbe[3] = (unsigned char) (e + 128);
74  }
75}
76
77/* standard conversion from rgbe to float pixels */
78/* note: Ward uses ldexp(col+0.5,exp-(128+8)).  However we wanted pixels */
79/*       in the range [0,1] to map back into the range [0,1].            */
80inline void HDRWriter::rgbe2float(float *red, float *green, float *blue, unsigned char rgbe[4])
81{
82  float f;
83
84  if (rgbe[3]) {   /*nonzero pixel*/
85    f = ldexp(1.0,rgbe[3]-(int)(128+8));
86    *red = rgbe[0] * f;
87    *green = rgbe[1] * f;
88    *blue = rgbe[2] * f;
89  }
90  else
91    *red = *green = *blue = 0.0;
92}
93
94#endif
Note: See TracBrowser for help on using the browser.