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

Revision 12912, 3.1 kB (checked in by robert, 6 years ago)

Added support for using GL_UNPACK_ROW_LENGTH in conjunction with texture's + osg::Image via new RowLength?
parameter in osg::Image. To support this Image::setData(..) now has a new optional rowLength parameter which
defaults to 0, which provides the original behaviour, Image::setRowLength(int) and int Image::getRowLength() are also provided.

With the introduction of RowLength? support in osg::Image it is now possible to create a sub image where
the t size of the image are smaller than the row length, useful for when you have a large image on the CPU
and which to use a small portion of it on the GPU. However, when these sub images are created the data
within the image is no longer contiguous so data access can no longer assume that all the data is in
one block. The new method Image::isDataContiguous() enables the user to check whether the data is contiguous,
and if not one can either access the data row by row using Image::data(column,row,image) accessor, or use the
new Image::DataIterator? for stepping through each block on memory assocatied with the image.

To support the possibility of non contiguous osg::Image usage of image objects has had to be updated to
check DataContiguous? and handle the case or use access via the DataIerator? or by row by row. To achieve
this a relatively large number of files has had to be modified, in particular the texture classes and
image plugins that doing writing.

  • Property svn:eol-style set to native
2 The following code was based on code from the following location:
5 It includes the following information :
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.
16    (Place notice here if you modified the code.)
17    posted to
18    written by Bruce Walter  (  5/26/95
19    based on code written by Greg Ward"
21 modified for OSG September 2007
24#ifndef HDRWRITER_H
25#define HDRWRITER_H
27#include <osg/Image>
29#include <iosfwd>
30#include <math.h>
32class HDRWriter {
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);
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);
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);
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]);
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)
59  float v;
60  int e;
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  }
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])
82  float f;
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;
Note: See TracBrowser for help on using the browser.