#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>

#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/xf86vmode.h>
#include <math.h>

int main(int argc, char **argv) {

  /* XF86 substitution table */
  uint16_t rtable[256];


  /* read input */

  uint32_t lowcut = atoi(argv[1]);
  uint32_t highcut = (255-atoi(argv[2]));

  float slope = 257;

  float range = 65535 - (lowcut*257 + highcut*257);

  printf("range = %f\n", range);

  if(range != 0) {
    slope *= 65535/range;
  }

  printf("slope = %f\n", slope);


  /* compute profile */

  for(int i=0; i<256; i++) {

    rtable[i] = (uint16_t) (slope*(i-lowcut));

    if(i<=lowcut) {
      rtable[i] = 0;
    }

    if(i>=255-highcut) {
      rtable[i] = 0xffff;
    }

    printf("%05i ", rtable[i]);
    if(i%16 == 15 && i>1) {
      printf("\n");
    }

  }


  /* X11 magic */

  Display *dpy = NULL;
  dpy = XOpenDisplay (NULL);
  int screen = DefaultScreen (dpy);

  XF86VidModeSetGammaRamp (dpy, screen, 256, rtable, rtable, rtable);

}

