Upcoming Events


Quick Stats
6090 people currently visiting GDNet.
2211 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!



Link to us

  search:   

High Dynamic Range Environment Mapping On Mainstream Graphics Hardware



Contents
  Introduction
  Background
  Theory
  Implementation
  Future Work
  Appendix A

  Source code
  Printable version
  Discuss this article

Appendix A: Simple Tone Mapping Implementation

//no warranties, expressed or implied, free for re-use
#include "stdafx.h"
#include "math.h"

#define N 11
#define delta 1.0f
#define MIDDLE_GRAY 0.36f
#define MAX_RGB 2048.0f
#define MAX_LUMINANCE ((0.2125f*MAX_RGB)+(0.7154*MAX_RGB)+(0.0721f*MAX_RGB))

// assume we have converted from RGB to luminance as described in the paper
float L[] ={  0.0f, 1.0f, 3.0f, 7.0f, 15.0f, 31.0f, 63.0f, 127.0f, 255.0f,
511.0f, 1023.0f, 2047.0f};

/* L refers to Luminance, wanted to fit on a page */
float L_NormalizedFloats[N];
float scaled_L[N];
float final_L[N];
int final_pixel_vals[N];

void _tmain(int argc, _TCHAR* argv[])
{
  float sum = 0.0f;
  float log_avg_L = 0.0f;
  float a = MIDDLE_GRAY;

  for(int i=0;i<N;i++)
  {
    float max = 0.0f;
    max = (float)MAX_LUMINANCE;
    L_NormalizedFloats[i] = L[i]/max;
  }

  for(int i=0;i<N;i++)
  {
    sum += (float)log((double)(L_NormalizedFloats[i]+1.0f));
  }

  log_avg_L = (1.0f/((float)N));
  log_avg_L *= exp(sum);
  float weight = 0.0f;
  weight = a/log_avg_L;

  for(i=0;i<N;i++)
  {
    scaled_L[i] = (weight*L[i]);
  }

  for(i=0;i<N;i++)
  {
    final_L[i] = scaled_L[i]/(1.0f+scaled_L[i]);
    int intL = (int)(final_L[i]*255.0f);
    printf("[%d] = %f %d\n",i,final_L[i]*255.0f, intL);
  }

}