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