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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
/*******************************************************************************
* hdr.cpp
*
* This module contains the code to read and write files in Radiance HDRI format
* (sometimes known as 'RGBE' format).
*
* Author: Christopher Cason
* Based on MegaPOV HDR code written by Mael and Christoph Hormann
*
* ---------------------------------------------------------------------------
* Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
* Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
*
* POV-Ray is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* POV-Ray is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------------
* POV-Ray is based on the popular DKB raytracer version 2.12.
* DKBTrace was originally written by David K. Buck.
* DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
* ---------------------------------------------------------------------------
* $File: //depot/public/povray/3.x/source/base/image/hdr.cpp $
* $Revision: #1 $
* $Change: 6069 $
* $DateTime: 2013/11/06 11:59:40 $
* $Author: chrisc $
*******************************************************************************/
#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>
#include <string>
// configbase.h must always be the first POV file included within base *.cpp files
#include "base/configbase.h"
#include "base/image/image.h"
#include "base/fileinputoutput.h"
#include "base/image/hdr.h"
#include "base/types.h"
#include "metadata.h"
// this must be the last file included
#include "base/povdebug.h"
namespace pov_base
{
namespace HDR
{
/*****************************************************************************
* Local preprocessor defines
******************************************************************************/
#define MINELEN 8 /* minimum scanline length for encoding */
#define MAXELEN 0x7fff /* maximum scanline length for encoding */
#define MINRUN 4 /* minimum run length */
/*****************************************************************************
* Local typedefs
******************************************************************************/
struct Messages
{
vector<string> warnings;
string error;
};
typedef unsigned char RGBE[4]; // red, green, blue, exponent
void GetRGBE(RGBE rgbe, const Image *image, int col, int row, const GammaCurvePtr& gamma, DitherHandler* dither);
void SetRGBE(const unsigned char *scanline, Image *image, int row, int width, const GammaCurvePtr& gamma);
void ReadOldLine(unsigned char *scanline, int width, IStream *file);
/*****************************************************************************
* Code
******************************************************************************/
void GetRGBE(RGBE rgbe, const Image *image, int col, int row, const GammaCurvePtr& gamma, DitherHandler* dh)
{
float r, g, b, d;
int e;
DitherHandler::OffsetInfo linOff, encOff;
dh->getOffset(col,row,linOff,encOff);
GetEncodedRGBValue(image, col, row, gamma, r, g, b);
r += linOff.red;
g += linOff.green;
b += linOff.blue;
if((d = max3(r, g, b)) <= 1.0e-32)
{
rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
return;
}
d = frexp(d, &e) * 256.0 / d;
rgbe[0] = (unsigned char)(clip(r * d + encOff.red, 0.0f, 255.0f));
rgbe[1] = (unsigned char)(clip(g * d + encOff.green, 0.0f, 255.0f));
rgbe[2] = (unsigned char)(clip(b * d + encOff.blue, 0.0f, 255.0f));
rgbe[3] = (unsigned char)(clip(e + 128.0f, 0.0f, 255.0f));
linOff.red = r - (double(rgbe[0]) + 0.5) / d;
linOff.green = g - (double(rgbe[1]) + 0.5) / d;
linOff.blue = b - (double(rgbe[2]) + 0.5) / d;
dh->setError(col,row,linOff);
}
void SetRGBE(const unsigned char *scanline, Image *image, int row, int width, const GammaCurvePtr& gamma)
{
for(int i = 0; i < width; i++)
{
double v = ldexp(1.0, int(scanline[3]) - (128 + 8));
float r = (double(scanline[0]) + 0.5) * v;
float g = (double(scanline[1]) + 0.5) * v;
float b = (double(scanline[2]) + 0.5) * v;
SetEncodedRGBValue(image, i, row, gamma, r, g, b);
scanline += 4;
}
}
void ReadOldLine(unsigned char *scanline, int width, IStream *file)
{
int rshift = 0;
unsigned char b;
while(width > 0)
{
scanline[0] = file->Read_Byte();
scanline[1] = file->Read_Byte();
scanline[2] = file->Read_Byte();
// NB EOF won't be set at this point even if the last read obtained the
// final byte in the file (we need to read another byte for that to happen).
if(!*file)
throw POV_EXCEPTION(kFileDataErr, "Invalid HDR file (unexpected EOF)");
if(file->Read_Byte(b).eof())
return;
scanline[3] = b;
if((scanline[0] == 1) && (scanline[1] == 1) && (scanline[2] == 1))
{
for(int i = scanline[3] << rshift; i > 0; i--)
{
memcpy(scanline, scanline - 4, 4);
scanline += 4;
width--;
}
rshift += 8;
}
else
{
scanline += 4;
width--;
rshift = 0;
}
}
}
Image *Read(IStream *file, const Image::ReadOptions& options)
{
char line[2048];
char *s;
char s1[3];
char s2[3];
unsigned char b;
unsigned char val;
float e;
float exposure = 1.0;
unsigned int width;
unsigned int height;
Image *image = NULL;
Image::ImageDataType imagetype = options.itype;
// Radiance HDR files store linear color values by default, so never convert unless the user overrides
// (e.g. to handle a non-compliant file).
GammaCurvePtr gamma;
if (options.gammacorrect)
{
if (options.gammaOverride)
gamma = TranscodingGammaCurve::Get(options.workingGamma, options.defaultGamma);
else
gamma = TranscodingGammaCurve::Get(options.workingGamma, NeutralGammaCurve::Get());
}
while(*file)
{
if(!file->getline(line, sizeof(line)) || (line[0] == '-') || (line[0] == '+'))
break;
// TODO: what do we do with exposure?
if(strncmp(line, "EXPOSURE", 8) == 0)
{
if((s = strchr(line, '=')) != NULL)
{
if(sscanf(s + 1, "%f", &e) == 1)
exposure *= e;
}
}
}
if(sscanf(line, "%2[+-XY] %u %2[+-XY] %u\n", s1, &height, s2, &width) != 4)
throw POV_EXCEPTION(kFileDataErr, "Bad HDR file header");
if(imagetype == Image::Undefined)
imagetype = Image::RGBFT_Float;
image = Image::Create(width, height, imagetype);
// NB: HDR files don't use alpha, so premultiplied vs. non-premultiplied is not an issue
boost::scoped_array<unsigned char> scanline(new unsigned char[4 * width]);
for(int row = 0; row < height; row++)
{
// determine scanline type
if((width < MINELEN) | (width > MAXELEN))
{
ReadOldLine(scanline.get(), width, file);
SetRGBE(scanline.get(), image, row, width, gamma);
continue;
}
if(!file->Read_Byte(b))
throw POV_EXCEPTION(kFileDataErr, "Incomplete HDR file");
if(b != 2)
{
file->UnRead_Byte(b);
ReadOldLine(scanline.get(), width, file);
SetRGBE(scanline.get(), image, row, width, gamma);
continue;
}
scanline[1] = file->Read_Byte();
scanline[2] = file->Read_Byte();
if(!file->Read_Byte(b))
throw POV_EXCEPTION(kFileDataErr, "Incomplete or invalid HDR file");
if((scanline[1] != 2) || ((scanline[2] & 128) != 0))
{
scanline[0] = 2;
scanline[3] = b;
ReadOldLine(scanline.get() + 4, width - 1, file);
SetRGBE(scanline.get(), image, row, width, gamma);
continue;
}
if((((int) scanline[2] << 8) | b) != width)
throw POV_EXCEPTION(kFileDataErr, "Invalid HDR file (length mismatch)");
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < width; )
{
if(!file->Read_Byte(b))
throw POV_EXCEPTION(kFileDataErr, "Invalid HDR file (unexpected EOF)");
if(b > 128)
{
// run
b &= 127;
if(!file->Read_Byte(val))
throw POV_EXCEPTION(kFileDataErr, "Invalid HDR file (unexpected EOF)");
while(b--)
scanline[j++ * 4 + i] = (unsigned char) val;
}
else
{
while(b--)
{
if(!file->Read_Byte(val))
throw POV_EXCEPTION(kFileDataErr, "Invalid HDR file (unexpected EOF)");
scanline[j++ * 4 + i] = (unsigned char) val;
}
}
}
}
SetRGBE(scanline.get(), image, row, width, gamma);
}
return image;
}
void Write(OStream *file, const Image *image, const Image::WriteOptions& options)
{
int width = image->GetWidth();
int height = image->GetHeight();
int cnt = 1;
int c2;
RGBE rgbe;
GammaCurvePtr gamma = TranscodingGammaCurve::Get(options.workingGamma, NeutralGammaCurve::Get());
DitherHandler* dither = options.dither.get();
Metadata meta;
file->printf("#?RADIANCE\n");
file->printf("SOFTWARE=%s\n", meta.getSoftware().c_str());
file->printf("CREATION_TIME=%s\n" ,meta.getDateTime().c_str());
if (!meta.getComment1().empty())
file->printf("COMMENT=%s\n", meta.getComment1().c_str());
if (!meta.getComment2().empty())
file->printf("COMMENT=%s\n", meta.getComment2().c_str());
if (!meta.getComment3().empty())
file->printf("COMMENT=%s\n", meta.getComment3().c_str());
if (!meta.getComment4().empty())
file->printf("COMMENT=%s\n", meta.getComment4().c_str());
file->printf("FORMAT=32-bit_rle_rgbe\n");
file->printf("\n");
file->printf("-Y %d +X %d\n", height, width);
boost::scoped_array<RGBE> scanline(new RGBE[width]);
for(int row = 0; row < height; row++)
{
if((width < MINELEN) | (width > MAXELEN))
{
for(int col = 0; col < width; col++)
{
GetRGBE(rgbe, image, col, row, gamma, dither);
if(!file->write(&rgbe, sizeof(RGBE)))
throw POV_EXCEPTION(kFileDataErr, "Failed to write data to HDR file");
}
}
else
{
// put magic header
file->Write_Byte(2);
file->Write_Byte(2);
file->Write_Byte(width >> 8);
file->Write_Byte(width & 255);
// convert pixels
for(int col = 0; col < width; col++)
GetRGBE(scanline[col], image, col, row, gamma, dither);
// put components seperately
for(int i = 0; i < 4; i++)
{
for(int col = 0; col < width; col += cnt)
{
int beg = 0;
// find next run
for(beg = col; beg < width; beg += cnt)
{
cnt = 1;
while((cnt < 127) && (beg + cnt < width) && (scanline[beg + cnt][i] == scanline[beg][i]))
cnt++;
// long enough ?
if(cnt >= MINRUN)
break;
}
if(beg - col > 1 && beg - col < MINRUN)
{
c2 = col + 1;
while(scanline[c2++][i] == scanline[col][i])
{
if(c2 == beg)
{
// short run
file->Write_Byte(128 + beg - col);
if(!file->Write_Byte(scanline[col][i]))
throw POV_EXCEPTION(kFileDataErr, "Failed to write data to HDR file");
col = beg;
break;
}
}
}
while(col < beg)
{
// write non-run
if((c2 = beg - col) > 128)
c2 = 128;
file->Write_Byte(c2);
while(c2--)
{
if(!file->Write_Byte(scanline[col++][i]))
throw POV_EXCEPTION(kFileDataErr, "Failed to write data to HDR file");
}
}
if(cnt >= MINRUN)
{
// write run
file->Write_Byte(128 + cnt);
if(!file->Write_Byte(scanline[beg][i]))
throw POV_EXCEPTION(kFileDataErr, "Failed to write data to HDR file");
}
else
cnt = 0;
}
}
}
}
}
} // end of namespace HDR
} // end of namespace pov_base
|