Skip to content

Instantly share code, notes, and snippets.

@FloooD
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FloooD/e653137600967759cfea to your computer and use it in GitHub Desktop.
Save FloooD/e653137600967759cfea to your computer and use it in GitHub Desktop.
basically a wrapper for Get/SetDeviceGammaRamp
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(int argc, char **argv)
{
if (argc < 2 || argv[1][0] == 'h') {
printf("usage: %s [arguments]\n"
"arguments:\n"
" c [slope] reset lut to linear function. (default: 256)\n"
" default windows slope is 256. dispwin -c slope is 257.\n"
" g r|g|b|a [num] get lut value for r/g/b/all for input num. default: all\n"
" s r|g|b|a num val set lut value for r/g/b/all for input num to val.\n"
" allowable ranges: 0 <= num <= 255, 0 <= val <= 65535\n"
" r file set lut from a file formatted like output of %s g a\n"
"example:\n"
" %s g a > mylutfile\n"
" *modify mylutfile to whatever*\n"
" %s r mylutfile\n",
argv[0], argv[0], argv[0], argv[0]
);
return 0;
}
WORD vals[3][256];
double slope = 256.0;
int col, in;
int val;
FILE *lut_file;
HDC hdc = GetDC(NULL);
switch (argv[1][0]) {
case 'c':
if (argc > 2) slope = atof(argv[2]);
if (slope < 128.0 || slope > 512.0) {
printf("slope must be between 128 and 512...\n");
return 1;
}
for (in = 0; in < 256; in++) {
val = slope * in;
val = (val > 65535) ? 65535 : val;
vals[0][in] = val;
vals[1][in] = val;
vals[2][in] = val;
}
if (SetDeviceGammaRamp(hdc, vals) == 0) {
printf("set failed. val must roughly be between num * 257 - 33000 and num * 257 + 33000\n");
return 0;
}
break;
case 'r':
if (argc < 3) {
printf("needs file name. run with no arguments for help.\n");
return 0;
}
lut_file = fopen(argv[2], "r");
if (lut_file == NULL) {
printf("couldn't open file %s", argv[2]);
return 0;
}
for (in = 0; in < 256; in++)
if (fscanf(lut_file, "%hu\t%hu\t%hu", &vals[0][in], &vals[1][in], &vals[2][in]) != 3) {
printf("wrong file format. format it like output of %s g a", argv[0]);
return 0;
}
fclose(lut_file);
if (SetDeviceGammaRamp(hdc, vals) == 0) {
printf("set failed. val must roughly be between num * 257 - 33000 and num * 257 + 33000\n");
return 1;
}
break;
case 'g':
if (argc < 3) {
printf("needs color arguments. run with no arguments for help.\n");
return 0;
}
col = argv[2][0];
col = (col == 'r') ? 0 : (col == 'g') ? 1 : (col == 'b') ? 2 : (col == 'a') ? 3 : -1;
if (col == -1) {
printf("invalid color argument. run with no arguments for help.\n");
return 0;
}
if (GetDeviceGammaRamp(hdc, vals) == 0) {
printf("getting lut failed o.O\n");
return 0;
}
if (argc == 3) { //no num, print everything.
if (col == 3)
for (in = 0; in < 256; in++)
printf("%u\t%u\t%u\n", vals[0][in], vals[1][in], vals[2][in]);
else
for (in = 0; in < 256; in++)
printf("%u\n", vals[col][in]);
break;
}
in = atoi(argv[3]);
in = (in > 255) ? 255 : (in < 0) ? 0 : in;
if (col == 3)
printf("%u\t%u\t%u\n", vals[0][in], vals[1][in], vals[2][in]);
else
printf("%u\n", vals[col][in]);
break;
case 's':
if (argc < 5) {
printf("needs more arguments. run with no arguments for help.\n");
return 0;
}
col = argv[2][0];
col = (col == 'r') ? 0 : (col == 'g') ? 1 : (col == 'b') ? 2 : (col == 'a') ? 3 : -1;
if (col == -1) {
printf("invalid color argument. run with no arguments for help.\n");
return 0;
}
in = atoi(argv[3]);
in = (in > 255) ? 255 : (in < 0) ? 0 : in;
val = atoi(argv[4]);
val = (val > 65535) ? 65535 : (val < 0) ? 0 : val;
if (GetDeviceGammaRamp(hdc, vals) == 0) {
printf("getting lut failed o.O\n");
return 1;
}
if (col == 3) {
vals[0][in] = val;
vals[1][in] = val;
vals[2][in] = val;
} else
vals[col][in] = val;
if (SetDeviceGammaRamp(hdc, vals) == 0) {
printf("set failed. val must roughly be between num * 257 - 33000 and num * 257 + 33000\n");
return 1;
}
break;
default:
printf("invalid argument. run with no arguments for help.\n");
return 0;
}
return 0;
}
all:
gcc lut.c -mconsole -mwindows -o lut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment