| 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 | #include <stdlib.h> |
|---|
| 29 | |
|---|
| 30 | #include "hdrwriter.h" |
|---|
| 31 | |
|---|
| 32 | #include <sstream> |
|---|
| 33 | #include <ostream> |
|---|
| 34 | #include <string> |
|---|
| 35 | #include <math.h> |
|---|
| 36 | #include <ctype.h> |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | bool HDRWriter::writeRAW(const osg::Image *img, std::ostream& fout) |
|---|
| 40 | { |
|---|
| 41 | bool result = true; |
|---|
| 42 | for(int row=0; result && row<img->t(); ++row) |
|---|
| 43 | { |
|---|
| 44 | result = writePixelsRAW(fout,(unsigned char*) img->data(0, row), img->s()); |
|---|
| 45 | } |
|---|
| 46 | return result; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | #define RGBE_DATA_SIZE 3 |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | #define R 0 |
|---|
| 54 | #define G 1 |
|---|
| 55 | #define B 2 |
|---|
| 56 | #define E 3 |
|---|
| 57 | |
|---|
| 58 | #define MINELEN 8 // minimum scanline length for encoding |
|---|
| 59 | #define MAXELEN 0x7fff // maximum scanline length for encoding |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | bool HDRWriter::writeHeader(const osg::Image *img, std::ostream& fout) |
|---|
| 63 | { |
|---|
| 64 | std::stringstream stream; |
|---|
| 65 | |
|---|
| 66 | stream << "#?RADIANCE\n"; |
|---|
| 67 | |
|---|
| 68 | stream << "FORMAT=32-bit_rle_rgbe\n\n"; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | stream << "-Y "<<img->s()<<" +X "<<img->t()<<"\n"; |
|---|
| 76 | |
|---|
| 77 | fout.write(stream.str().c_str(), stream.str().length()); |
|---|
| 78 | |
|---|
| 79 | return true; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | bool HDRWriter::writeNoRLE( std::ostream& fout, const osg::Image* img) |
|---|
| 86 | { |
|---|
| 87 | unsigned char rgbe[4]; |
|---|
| 88 | |
|---|
| 89 | for(int row=0; row<img->t(); ++row) |
|---|
| 90 | { |
|---|
| 91 | float* data = (float*)img->data(0,row); |
|---|
| 92 | for(int column=0; column<img->s(); ++column) |
|---|
| 93 | { |
|---|
| 94 | float2rgbe( |
|---|
| 95 | rgbe, |
|---|
| 96 | data[R], |
|---|
| 97 | data[G], |
|---|
| 98 | data[B] |
|---|
| 99 | ); |
|---|
| 100 | data += RGBE_DATA_SIZE; |
|---|
| 101 | fout.write(reinterpret_cast<const char*>(rgbe), sizeof(rgbe)); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | return true; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | bool HDRWriter::writePixelsRAW( std::ostream& fout, unsigned char* data, int numpixels) |
|---|
| 109 | { |
|---|
| 110 | unsigned char rgbe[4]; |
|---|
| 111 | |
|---|
| 112 | while (numpixels-- > 0) |
|---|
| 113 | { |
|---|
| 114 | rgbe[0] = (unsigned char) *(data+R); |
|---|
| 115 | rgbe[1] = (unsigned char) *(data+G); |
|---|
| 116 | rgbe[2] = (unsigned char) *(data+B); |
|---|
| 117 | rgbe[3] = (unsigned char) *(data+E); |
|---|
| 118 | data += RGBE_DATA_SIZE; |
|---|
| 119 | fout.write(reinterpret_cast<const char*>(rgbe), sizeof(rgbe)); |
|---|
| 120 | } |
|---|
| 121 | return true; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | bool HDRWriter::writeBytesRLE(std::ostream& fout, unsigned char *data, int numbytes) |
|---|
| 129 | { |
|---|
| 130 | #define MINRUNLENGTH 4 |
|---|
| 131 | int cur, beg_run, run_count, old_run_count, nonrun_count; |
|---|
| 132 | unsigned char buf[2]; |
|---|
| 133 | |
|---|
| 134 | cur = 0; |
|---|
| 135 | while(cur < numbytes) |
|---|
| 136 | { |
|---|
| 137 | beg_run = cur; |
|---|
| 138 | |
|---|
| 139 | run_count = old_run_count = 0; |
|---|
| 140 | while((run_count < MINRUNLENGTH) && (beg_run < numbytes)) |
|---|
| 141 | { |
|---|
| 142 | beg_run += run_count; |
|---|
| 143 | old_run_count = run_count; |
|---|
| 144 | run_count = 1; |
|---|
| 145 | while((data[beg_run] == data[beg_run + run_count]) |
|---|
| 146 | && (beg_run + run_count < numbytes) |
|---|
| 147 | && (run_count < 127)) |
|---|
| 148 | { |
|---|
| 149 | run_count++; |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | if ((old_run_count > 1)&&(old_run_count == beg_run - cur)) |
|---|
| 154 | { |
|---|
| 155 | buf[0] = 128 + old_run_count; |
|---|
| 156 | buf[1] = data[cur]; |
|---|
| 157 | fout.write(reinterpret_cast<const char*>(buf), sizeof(buf[0])*2); |
|---|
| 158 | |
|---|
| 159 | cur = beg_run; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | while(cur < beg_run) |
|---|
| 163 | { |
|---|
| 164 | nonrun_count = beg_run - cur; |
|---|
| 165 | if (nonrun_count > 128) nonrun_count = 128; |
|---|
| 166 | buf[0] = nonrun_count; |
|---|
| 167 | fout.write(reinterpret_cast<const char*>(buf),sizeof(buf[0])); |
|---|
| 168 | |
|---|
| 169 | fout.write(reinterpret_cast<const char*>(&data[cur]),sizeof(data[0])*nonrun_count); |
|---|
| 170 | |
|---|
| 171 | cur += nonrun_count; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | if (run_count >= MINRUNLENGTH) |
|---|
| 175 | { |
|---|
| 176 | buf[0] = 128 + run_count; |
|---|
| 177 | buf[1] = data[beg_run]; |
|---|
| 178 | fout.write(reinterpret_cast<const char*>(buf),sizeof(buf[0])*2); |
|---|
| 179 | |
|---|
| 180 | cur += run_count; |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | return true; |
|---|
| 184 | #undef MINRUNLENGTH |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | bool HDRWriter::writeRLE( const osg::Image* img, std::ostream& fout) |
|---|
| 188 | { |
|---|
| 189 | int scanline_width = img->s(); |
|---|
| 190 | int num_scanlines = img->t(); |
|---|
| 191 | |
|---|
| 192 | unsigned char rgbe[4]; |
|---|
| 193 | unsigned char *buffer; |
|---|
| 194 | |
|---|
| 195 | if ((scanline_width < MINELEN)||(scanline_width > MAXELEN)) |
|---|
| 196 | |
|---|
| 197 | return writeNoRLE(fout,img); |
|---|
| 198 | |
|---|
| 199 | buffer = (unsigned char *)malloc(sizeof(unsigned char)*4*scanline_width); |
|---|
| 200 | if (buffer == NULL) |
|---|
| 201 | |
|---|
| 202 | return writeNoRLE(fout,img); |
|---|
| 203 | |
|---|
| 204 | for(int row = 0; row<num_scanlines; ++row) |
|---|
| 205 | { |
|---|
| 206 | float* data = (float*) img->data(0, row); |
|---|
| 207 | |
|---|
| 208 | rgbe[0] = 2; |
|---|
| 209 | rgbe[1] = 2; |
|---|
| 210 | rgbe[2] = scanline_width >> 8; |
|---|
| 211 | rgbe[3] = scanline_width & 0xFF; |
|---|
| 212 | |
|---|
| 213 | fout.write(reinterpret_cast<const char*>(rgbe), sizeof(rgbe)); |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | for(int i=0;i<scanline_width;i++) |
|---|
| 222 | { |
|---|
| 223 | float2rgbe(rgbe,data[R], data[G],data[B]); |
|---|
| 224 | buffer[i] = rgbe[0]; |
|---|
| 225 | buffer[i+scanline_width] = rgbe[1]; |
|---|
| 226 | buffer[i+2*scanline_width] = rgbe[2]; |
|---|
| 227 | buffer[i+3*scanline_width] = rgbe[3]; |
|---|
| 228 | data += RGBE_DATA_SIZE; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | for(int i=0;i<4;i++) |
|---|
| 233 | { |
|---|
| 234 | if (writeBytesRLE(fout,&buffer[i*scanline_width],scanline_width) != true) |
|---|
| 235 | { |
|---|
| 236 | free(buffer); |
|---|
| 237 | return false; |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | free(buffer); |
|---|
| 243 | return true; |
|---|
| 244 | } |
|---|