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 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
/*************************************************************************
This project implements a complete(!) JPEG (Recommendation ITU-T
T.81 | ISO/IEC 10918-1) codec, plus a library that can be used to
encode and decode JPEG streams.
It also implements ISO/IEC 18477 aka JPEG XT which is an extension
towards intermediate, high-dynamic-range lossy and lossless coding
of JPEG. In specific, it supports ISO/IEC 18477-3/-6/-7/-8 encoding.
Note that only Profiles C and D of ISO/IEC 18477-7 are supported
here. Check the JPEG XT reference software for a full implementation
of ISO/IEC 18477-7.
Copyright (C) 2012-2018 Thomas Richter, University of Stuttgart and
Accusoft. (C) 2019-2020 Thomas Richter, Fraunhofer IIS.
This program is available under two licenses, GPLv3 and the ITU
Software licence Annex A Option 2, RAND conditions.
For the full text of the GPU license option, see README.license.gpl.
For the full text of the ITU license option, see README.license.itu.
You may freely select between these two options.
For the GPL option, please note the following:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
/*
** A couple of generic TMO related functions: Estimate TMO from LDR and HDR
** image pair, build a gamma mapping.
**
** $Id: tmo.cpp,v 1.18 2016/09/20 11:40:05 thor Exp $
**
*/
/// Includes
#include "cmd/tmo.hpp"
#include "cmd/iohelpers.hpp"
#include "std/stdlib.hpp"
#include "std/string.hpp"
#include "std/math.hpp"
///
/// Defines
//#define SAVE_ITMO
///
/// InvertTable
// Invert a tabulated tone mapping table. To be on the fair side,
// this uses the same algorithm the library uses to build the forwards
// table from the backwards table provided. Otherwise, I could also
// invert numerically the (parametric) table.
void InvertTable(UWORD input[65536],UWORD output[65536],UBYTE inbits,UBYTE outbits)
{
LONG j,lastj,lastanchor;
LONG last,current,mid;
LONG outmax = (1L << outbits) - 1;
LONG inmax = (1L << inbits) - 1;
bool lastfilled;
// Nnot guaranteed that the mapping is surjective onto the output
// range. There is nothing that says how to handle this case. We just define
// "undefined" outputs to zero, and try our best to continue the missing parts
// continously along the output range.
memset(output,0,(1 << outbits) * sizeof(UWORD));
//
// Loop over positive coefficients.
lastj = inmax;
lastanchor = inmax;
lastfilled = false;
j = inmax;
last = input[j];
//
// Try some guesswork whether we should extend this to fullrange.
// This avoids trouble in case the backwards mapping is not
// onto, but we find the corresponding out-of-range pixels in the
// input image.
if (last < (((outmax + 1) * 3) >> 2)) {
last = outmax;
}
//
//
// Go from max to zero. This direction is intentional.
do {
// Get the next possible output for the given input.
// Swap back to get the proper endianness.
current = input[j];
// If the function jumps, fill in half the values with the old
// the other half with the new values. The output is never
// swapped here, otherwise the table would grow out of range
// too easily.
if (current == last) {
// Found a "flat" area, i.e. had the same external value for
// similar internal values. If so, fill in the midpoint
// into the table. If lastanchor + j overflows, then our
// tables are far too huge in first place.
output[last] = (lastanchor + j) >> 1;
lastfilled = true;
} else {
// Found a "steep" part of the output curve.
// If the function jumps, fill in half the values with the old
// the other half with the new values. The output is never
// swapped here, otherwise the table would grow out of range
// too easily.
if (last > current) {
mid = ((current + last + 1) >> 1) - 1;
} else {
mid = ((current + last - 1) >> 1) - 1;
}
while(last != mid) {
if (lastfilled == false) // Do not overwrite the flat area from the last time.
output[last] = lastj;
if (last > mid) last--;
else last++;
lastfilled = false;
}
while(last != current) {
if (lastfilled == false) // Do not overwrite the flat area from the last time.
output[last] = j;
if (last > current) last--;
else last++;
lastfilled = false;
}
lastanchor = j;
}
lastj = j;
last = current;
} while(j--); // This includes the case j = 0 in the inner loop
// Now we could have the situation that "lastfilled" is still false,
// thus lut[last] is not yet filled. j is now -1 and thus not
// usable, lastj == 0, and there is no further point to extrapolate
// to. Thus, set to the exact end-point.
if (lastfilled == false)
output[last] = lastj;
//
// Fixup the ends of the table. If the start or the end of the LUT have a very low slope,
// we will find jumps in the table that are likely undesired. Fix them up here to avoid
// artefacts in the image.
if (outmax > 4) {
LONG i1,i2,i3;
LONG d1,d2;
//
i1 = output[0];
i2 = output[1];
i3 = output[2];
//
d1 = (i1 > i2)?(i1 - i2):(i2 - i1);
d2 = (i3 > i2)?(i3 - i2):(i2 - i3);
//
// If the first jump is too large, clip it.
if (d1 > 2 * d2)
output[0] = 2 * i2 - i3;
//
// Now at the other end. Note that max is inclusive.
i1 = output[outmax];
i2 = output[outmax - 1];
i3 = output[outmax - 2];
//
d1 = (i1 > i2)?(i1 - i2):(i2 - i1);
d2 = (i3 > i2)?(i3 - i2):(i2 - i3);
//
if (d1 > 2 * d2)
output[outmax] = 2 * i2 - i3;
}
}
///
/// SaveHistogram
#ifdef SAVE_ITMO
static void save_histogram(const char *filename,double hist[256])
{
FILE *out = fopen(filename,"w");
if (out) {
for(int i = 0;i < 256;i++) {
if (hist[i] >= 0.0) {
fprintf(out,"%d\t%g\n",i,hist[i]);
}
}
fclose(out);
}
}
#endif
///
/// BuildIntermediateTable
// Build an intermediate table from a histogram.
void BuildIntermediateTable(int **hists,int offs,int hdrcnt,
UWORD ldrtohdr[65536],int hiddenbits,
bool median,bool &fullrange,bool flt,
int smooth)
{
int i,j,k;
double intermed[256];
LONG absmin = hdrcnt;
LONG absmax = 0;
//
// For each LDR value (first index), find a suitable
// HDR value to map to.
// Several methods could be used here.
// As a starter, use the average.
for(i = 0;i < 256;i++) {
int count = 0;
int min = -1;
int max = -1;
for(j = 0;j < hdrcnt;j++) {
count += hists[i+offs][j];
if (hists[i+offs][j] > 0) {
if (min == -1) min = j;
max = j;
}
}
if (count > 0) {
if (max > absmax)
absmax = max;
if (min < absmin)
absmin = min;
if (max - min > (hdrcnt >> 1)) {
fullrange = true;
intermed[i] = (max - min) >> 1;
} else {
if (median && count > 1) {
int median = 0;
for(j = 0;j < hdrcnt;j++) {
median += hists[i+offs][j];
if (median >= (count >> 1))
break;
}
intermed[i] = j;
} else {
double total = 0.0;
double sum = 0.0;
for(j = 0;j < hdrcnt;j++) {
sum += double(hists[i+offs][j]) * j;
total += double(hists[i+offs][j]);
}
intermed[i] = sum / total;
}
}
} else {
intermed[i] = -1;
}
}
//
#ifdef SAVE_ITMO
save_histogram("histogram.plot",intermed);
#endif
//
// Fill in "holes" in the intermediate map.
if (absmin == hdrcnt)
absmin = 0;
if (absmax == 0)
absmax = hdrcnt;
double cur = absmin;
double nex = 0.0;
int v = 0;
for(i = 0;i < 256;i++) {
if (intermed[i] < 0.0) {
// Find the next filled slot.
for(j = i;j < 256;j++) {
if (intermed[j] >= 0.0) {
nex = intermed[j];
break;
}
}
if (j == 256)
nex = absmax;
// Use a linear interpolation to fill the gaps.
for(k = i;k < j;k++) {
intermed[i] = double(k - v)/double(j - v) * (nex - cur) + cur;
}
} else {
cur = intermed[i];
v = i;
}
}
//
#ifdef SAVE_ITMO
save_histogram("histogram-filled.plot",intermed);
#endif
//
// Make the map monotonical. First find the minimum.
double min = hdrcnt;
double max = 0;
for (i = 0;i < 256;i++) {
if (intermed[i] < min)
min = intermed[i];
if (intermed[i] > max)
max = intermed[i];
}
// Second, map everything below the minimum to the minimum value. This will be extended later.
if (intermed[0] > min) {
for (i = 0;i < 256;i++) {
if (intermed[i] >= min + i) {
intermed[i] = min + i;
} else break;
}
}
//
int now = intermed[0] - 1;
for(i = 0;i < 256;i++) {
if (intermed[i] <= now) {
if (now + 1 < max) {
intermed[i] = now + 1;
} else {
intermed[i] = max;
}
}
now = intermed[i];
}
//
if (max > intermed[255]) {
now = intermed[255] + 1;
for(i = 255;i >= 0;i--) {
if (intermed[i] >= now) {
if (now - 1 > min) {
intermed[i] = now - 1;
} else {
intermed[i] = min + i;
}
}
now = intermed[i];
}
}
//
#ifdef SAVE_ITMO
save_histogram("histogram-monotonic.plot",intermed);
#endif
//
// Now smoothen the values, outwards in.
for(i = 0;i < smooth;i++) {
for(j = 1;j < 255;j+=2) {
intermed[j] = 0.25 * intermed[j-1] + 0.5 * intermed[j] + 0.25 * intermed[j+1];
}
for(j = 2;j < 255;j+=2) {
intermed[j] = 0.25 * intermed[j-1] + 0.5 * intermed[j] + 0.25 * intermed[j+1];
}
}
//
#ifdef SAVE_ITMO
save_histogram("histogram-smooth.plot",intermed);
#endif
// Use a very simple interpolation to fill in the final
// output map. Note that this might expand the output by "hiddenbits".
for(i = 0;i < (256 << hiddenbits);i++) {
j = i >> hiddenbits;
k = j + 1;
if (k >= 256)
k = 255;
if (k > j) {
ldrtohdr[i] = double(i - (j << hiddenbits)) /
double((k << hiddenbits) - double(j << hiddenbits)) *
(intermed[k] - intermed[j]) + intermed[j];
} else {
ldrtohdr[i] = intermed[j];
}
// If this is floating point, invert negative values
// to create a continuous map.
if (flt) {
if (ldrtohdr[i] & 0x8000)
ldrtohdr[i] ^= 0x7fff;
}
}
//
}
///
/// BuildToneMappingFromLDR (for FLOAT)
// Build an inverse tone mapping from a hdr/ldr image pair, though generate it as
// a floating point table. This requires floating point input.
void BuildToneMappingFromLDR(FILE *in,FILE *ldrin,int w,int h,int count,
FLOAT ldrtohdr[256],
bool bigendian,bool median,bool fullrange,
int smooth)
{
int i;
UWORD tmp[65536];
DOUBLE scale;
//
// Call the generic function. This returns half-float values we still have to cast to float.
BuildToneMappingFromLDR(in,ldrin,w,h,16,count,tmp,true,bigendian,false,0,median,fullrange,smooth);
//
// Potentially scale the map so we avoid clamping. This is necessary because the output
// of this map goes into the 2nd base trafo, which implies input clamping. Profile A can
// compensate for this by the mu-map. Profile B has the output transformation exposure
// value and hence can compensate for it, too. The 65535.0 comes from the output
// transformation.
scale = 65535.0 / HalfToDouble(tmp[255]);
//
// Now convert the sample values to float.
for(i = 0;i < 256;i++) {
ldrtohdr[i] = HalfToDouble(tmp[i]) * scale; // This is the output transformation.
}
}
///
/// BuildToneMappingFromLDR
// Build an inverse tone mapping from a hdr/ldr image pair
void BuildToneMappingFromLDR(FILE *in,FILE *ldrin,int w,int h,int depth,int count,
UWORD ldrtohdr[65536],bool flt,
bool bigendian,bool xyz,int hiddenbits,bool median,bool &fullrange,
int smooth)
{
long hpos = ftell(in);
long lpos = ftell(ldrin);
int **hists = NULL; // Histograms for each LDR pixel value.
int i;
int hdrcnt = (flt)?(65536):(1 << depth);
int x,y;
bool warn = false;
hists = (int **)malloc(sizeof(int *) * 256);
fullrange = false;
if (hists) {
memset(hists,0,sizeof(int *) * 256);
for(i = 0;i < 256;i++) {
hists[i] = (int *)malloc(sizeof(int) * hdrcnt);
if (hists[i] == NULL)
break;
memset(hists[i],0,sizeof(int) * hdrcnt);
}
if (i == 256) {
for(y = 0;y < h;y++) {
for(x = 0;x < w;x++) {
// Read the HDR image parameters.
int r,g,b;
int rl,gl,bl;
double y;
//
warn |= ReadRGBTriple(in,r,g,b,y,depth,count,flt,bigendian,xyz);
/*
r = y * (hdrcnt - 1) + 0.5;
if (r < 0) r = 0;
if (r >= hdrcnt) r = hdrcnt - 1;
*/
//
// Read the LDR parameters.
ReadRGBTriple(ldrin,rl,gl,bl,y,8,count,false,false,false);
/*
rl = y * 255 + 0.5;
if (rl < 0) rl = 0;
if (rl > 255) rl = 255;
*/
// Update the histogram.
// Actually, here it might make sense to collect
// three histograms, not one. The coding core
// would actually even support this, though this
// frontend is currently limited.
hists[rl][r]++;
hists[gl][g]++;
hists[bl][b]++;
}
}
//
// Build tables for each component.
BuildIntermediateTable(hists,0,hdrcnt,ldrtohdr,hiddenbits,median,fullrange,flt,smooth);
//
// Release the temporary storage for the histogram.
for(i = 0;i < 256;i++) {
free(hists[i]);
}
}
free(hists);
}
fseek(in ,hpos,SEEK_SET);
fseek(ldrin,lpos,SEEK_SET);
if (warn)
fprintf(stderr,"Warning: Input image contains out of gamut values, clamping it.\n");
}
///
/// BuildRGBToneMappingFromLDR
// Build an inverse tone mapping from a hdr/ldr image pair
void BuildRGBToneMappingFromLDR(FILE *in,FILE *ldrin,int w,int h,int depth,int count,
UWORD red[65536],UWORD green[65536],UWORD blue[65536],
bool flt,bool bigendian,bool xyz,int hiddenbits,
bool median,bool &fullrange,int smooth)
{
long hpos = ftell(in);
long lpos = ftell(ldrin);
int **hists = NULL; // Histograms for each LDR pixel value.
int i;
int hdrcnt = (flt)?(65536):(1 << depth);
int x,y;
bool warn = false;
fullrange = false;
hists = (int **)malloc(sizeof(int *) * 256 * 3);
if (hists) {
memset(hists,0,sizeof(int *) * 256 * 3);
for(i = 0;i < 256 * 3;i++) {
hists[i] = (int *)malloc(sizeof(int) * hdrcnt);
if (hists[i] == NULL)
break;
memset(hists[i],0,sizeof(int) * hdrcnt);
}
if (i == 256 * 3) {
for(y = 0;y < h;y++) {
for(x = 0;x < w;x++) {
// Read the HDR image parameters.
int r,g,b;
int rl,gl,bl;
double y;
//
warn |= ReadRGBTriple(in,r,g,b,y,depth,count,flt,bigendian,xyz);
//
// Read the LDR parameters.
ReadRGBTriple(ldrin,rl,gl,bl,y,8,count,false,false,false);
// Update the histogram.
// Actually, here it might make sense to collect
// three histograms, not one. The coding core
// would actually even support this, though this
// frontend is currently limited.
hists[rl + (0<<0)][r]++;
hists[gl + (1<<8)][g]++;
hists[bl + (2<<8)][b]++;
}
}
//
// Build tables for each component.
BuildIntermediateTable(hists,0 << 8,hdrcnt,red ,hiddenbits,median,fullrange,flt,smooth);
BuildIntermediateTable(hists,1 << 8,hdrcnt,green,hiddenbits,median,fullrange,flt,smooth);
BuildIntermediateTable(hists,2 << 8,hdrcnt,blue ,hiddenbits,median,fullrange,flt,smooth);
//
// Release the temporary storage for the histogram.
for(i = 0;i < 256;i++) {
free(hists[i]);
}
}
free(hists);
}
fseek(in ,hpos,SEEK_SET);
fseek(ldrin,lpos,SEEK_SET);
if (warn)
fprintf(stderr,"Warning: Input image contains out of gamut values, clamping it.\n");
}
///
/// BuildGammaMapping
// Build a static gamma mapping to map the HDR to the LDR domain.
void BuildGammaMapping(double gamma,double exposure,UWORD ldrtohdr[65536],
bool flt,int max,int hiddenbits)
{
int i;
int outmax = (flt)?(0x7bff):(max); // 0x7c00 is INF in half-float
int inmax = 256 << hiddenbits;
double knee = 0.04045;
double divs = pow((knee + 0.055) / 1.055,gamma) / knee;
double shift = 1.0 / (1 << (12 + hiddenbits));
for(i = 0;i < inmax;i++) {
double in = i / double(inmax - 1);
double out = 0.0;
int int_out;
if (gamma != 1.0) {
if (in > knee) {
out = pow((in + 0.055) / 1.055,gamma) / exposure;
} else {
out = in * divs / exposure;
}
if (flt) {
int_out = DoubleToHalf(out + shift);
} else {
int_out = outmax * (out + shift) + 0.5;
}
} else {
if (flt) {
int_out = DoubleToHalf(out + shift);
} else {
int_out = outmax * (in + shift) + 0.5;
}
}
if (int_out > outmax) int_out = outmax;
if (int_out < 0 ) int_out = 0;
ldrtohdr[i] = int_out;
}
}
///
/// LoadLTable
// Load an inverse tone mapping table from a file.
void LoadLTable(const char *ltable,UWORD ldrtohdr[65536],bool flt,int max,int hiddenbits)
{
FILE *in = fopen(ltable,"r");
if (in) {
char buffer[256];
int outmax = (flt)?(0x7bff):(max); // 0x7c00 is INF in half-float
int inmax = 256 << hiddenbits;
int line = 0;
int i = 0;
while(!feof(in) && !ferror(in)) {
char *end;
long value;
fgets(buffer,sizeof(buffer),in);line++;
if (buffer[0] == '#' || buffer[0] == '\n' || buffer[0] == '\0')
continue;
value = strtol(buffer,&end,0);
if (end <= buffer) {
fprintf(stderr,"junk in LUT table definition file %s at line %d, ignoring this line.\n",ltable,line);
continue;
}
if (*end != '\n') {
fprintf(stderr,"junk in LUT table definition file %s behind line %d, ignoring the junk.\n",ltable,line);
}
if (i > inmax) {
fprintf(stderr,"too many lines in file %s, line %d is superfluos. Expected only %d inputs.\n",ltable,line,inmax);
} else {
if (value > long(outmax)) {
fprintf(stderr,"input value %ld at line %d in file %s is too large, maximum value is %d, clipping it.\n",
value,line,ltable,max);
value = outmax;
} else if (value < 0) {
fprintf(stderr,"input value %ld at line %d in file %s is too small, minimum value is %d, clipping it.\n",
value,line,ltable,0);
value = 0;
}
ldrtohdr[i++] = value;
}
}
if (i < inmax) {
fprintf(stderr,"file %s only defined %d out of %d values, extending the table by adding the maximum.\n",
ltable,i,inmax);
while(i < inmax) {
ldrtohdr[i++] = outmax;
}
}
fclose(in);
}
}
///
|