Ignotus-mago / PixelAudio

PixelAudio maps arrays of audio samples onto arrays of pixel values.
Other
0 stars 0 forks source link

Gamma Correction #7

Closed Ignotus-mago closed 4 months ago

Ignotus-mago commented 4 months ago

In the WaveSynth code, gamma correction really slows animation down. With two operators and gamma = 1.0 (no correction) it seems to run close to real time for a 1024 x 1024 image. Add gamma and it crawls.

I think it should be possible to create a gamma table instead of calculation gamma each time. That will help even with step-time animation.

Ignotus-mago commented 4 months ago

Pretty easy to create a table, and notably faster than calculation:

public void setGamma(float gamma) {
  this.gamma = gamma;
  if (gamma != 1.0) {
    this.gammaTable = new int[256];
    for (int i = 0; i < gammaTable.length; i++) {
      float c = i/(float)(gammaTable.length - 1);
      gammaTable[i] = (int) Math.round(Math.pow(c, gamma) * (gammaTable.length - 1));
    }
  }
}

The SimpleWaveSynth example shows how to set different gamma values for the WaveSynth class. You can turn the table lookup function off by setting WaveSynth's useGammaTable variable to false (it's true by default), but there's probably no reason to do that.