Skip to content

Instantly share code, notes, and snippets.

@embedded-creations
Last active May 8, 2020 14:14
Show Gist options
  • Save embedded-creations/c2f6707af52de1c8e777b41e67265a58 to your computer and use it in GitHub Desktop.
Save embedded-creations/c2f6707af52de1c8e777b41e67265a58 to your computer and use it in GitHub Desktop.
Gamma Table Calculator adapted from adafruit/RGB-matrix-Panel
// THIS IS NOT ARDUINO CODE -- DON'T INCLUDE IN YOUR SKETCH. It's a
// command-line tool that outputs a gamma correction table to stdout;
// redirect or copy and paste the results into header file for the
// RGBmatrixPanel library code.
//
// Optional 2 parameters:
// - bit depth (default=4, for 16 output levels).
// - tableSize (default=256 for gamma_table[256])
//
// typical usage:
// gcc -o gamma gamma.c
// ./gamma 4 256
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define GAMMA 2.5
int planes = 4;
int tableSize = 256;
int main(int argc, char *argv[])
{
int i, maxval, hexWidth;
if(argc > 1) planes = atoi(argv[1]);
if(argc > 2) tableSize = atoi(argv[2]);
maxval = (1 << planes) - 1;
int tempVal = maxval;
hexWidth = 0;
while(tempVal) {
hexWidth++;
tempVal /= 0x10;
}
(void)printf(
"#ifndef _GAMMA_H_\n"
"#define _GAMMA_H_\n\n"
"#include <avr/pgmspace.h>\n\n"
"static const uint8_t PROGMEM gamma_table[] = {\n ");
for(i=0; i<tableSize; i++) {
(void)printf("0x%0*x",hexWidth,(int)(pow((float)i / (float)tableSize, GAMMA) *
(float)maxval + 0.5));
if(i < tableSize-1) (void)printf(((i & 7) == 7) ? ",\n " : ",");
}
(void)puts(
"\n};\n\n"
"#endif // _GAMMA_H_");
return 0;
}
@embedded-creations
Copy link
Author

@embedded-creations
Copy link
Author

Added ability to pass in tableSize parameter, set leading zeros for hex digit based on maxval

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment