In my app I need to be able to get and set the video card gamma ramp. In X11 this turned out to be fairly simple with code that looked like this:
gamma_ramp.h
class gamma_ramp
{
public:
int screen;
int size;
unsigned short *red, *green, *blue;
gamma_ramp(int);
~gamma_ramp();
void getGammaRamp();
void setGammaRamp();
void setLinearGammaRamp();
private:
int getGammaRampSize(int);
};
class gamma_ramp
{
public:
int screen;
int size;
unsigned short *red, *green, *blue;
gamma_ramp(int);
~gamma_ramp();
void getGammaRamp();
void setGammaRamp();
void setLinearGammaRamp();
private:
int getGammaRampSize(int);
};
To copy to clipboard, switch view to plain text mode
gamma_ramp-x11.cpp
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
// X11 stuff
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xlibint.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include <X11/extensions/xf86vmode.h>
#include <X11/extensions/dpms.h>
#include <X11/extensions/Xinerama.h>
#include "gamma_ramp.h"
gamma_ramp::gamma_ramp(int Screen)
{
screen = Screen;
size = getGammaRampSize(screen);
// now allocate the arrays
red = new unsigned short[size];
green = new unsigned short[size];
blue = new unsigned short[size];
}
gamma_ramp::~gamma_ramp()
{
delete[] red;
delete[] green;
delete[] blue;
}
int gamma_ramp::getGammaRampSize(int Screen)
{
int size = 0;
int event_base;
int error_base;
Display *dpy;
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
if (!XF86VidModeGetGammaRampSize(dpy, Screen, &size))
{
printf("failed to get XF86VidModeGetGammaRampSize\n");
}
}
}
return size; // will be 0 if failed
}
void gamma_ramp::getGammaRamp()
{
int event_base;
int error_base;
Display *dpy;
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
XF86VidModeGetGammaRamp(dpy, screen, size,
red, green, blue);
}
}
}
void gamma_ramp::setGammaRamp()
{
int event_base;
int error_base;
Display *dpy;
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
XF86VidModeSetGammaRamp(dpy, screen, size,
red, green, blue);
}
}
}
void gamma_ramp::setLinearGammaRamp()
{
int event_base;
int error_base;
Display *dpy;
gamma_ramp* temp;
temp = new gamma_ramp(screen);
for (int i=0; i< temp -> size; i++)
{
temp -> red[i] = temp -> green[i] = temp -> blue[i] = i * 256;
}
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
temp -> setGammaRamp();
}
}
delete temp;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
// X11 stuff
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xlibint.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include <X11/extensions/xf86vmode.h>
#include <X11/extensions/dpms.h>
#include <X11/extensions/Xinerama.h>
#include "gamma_ramp.h"
gamma_ramp::gamma_ramp(int Screen)
{
screen = Screen;
size = getGammaRampSize(screen);
// now allocate the arrays
red = new unsigned short[size];
green = new unsigned short[size];
blue = new unsigned short[size];
}
gamma_ramp::~gamma_ramp()
{
delete[] red;
delete[] green;
delete[] blue;
}
int gamma_ramp::getGammaRampSize(int Screen)
{
int size = 0;
int event_base;
int error_base;
Display *dpy;
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
if (!XF86VidModeGetGammaRampSize(dpy, Screen, &size))
{
printf("failed to get XF86VidModeGetGammaRampSize\n");
}
}
}
return size; // will be 0 if failed
}
void gamma_ramp::getGammaRamp()
{
int event_base;
int error_base;
Display *dpy;
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
XF86VidModeGetGammaRamp(dpy, screen, size,
red, green, blue);
}
}
}
void gamma_ramp::setGammaRamp()
{
int event_base;
int error_base;
Display *dpy;
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
XF86VidModeSetGammaRamp(dpy, screen, size,
red, green, blue);
}
}
}
void gamma_ramp::setLinearGammaRamp()
{
int event_base;
int error_base;
Display *dpy;
gamma_ramp* temp;
temp = new gamma_ramp(screen);
for (int i=0; i< temp -> size; i++)
{
temp -> red[i] = temp -> green[i] = temp -> blue[i] = i * 256;
}
if ((dpy = XOpenDisplay(":0.0")) == NULL)
{
printf("failed to open display :0.0\n");
}
else
{
if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
{
printf("XF86VidModeQueryExtension failed\n");
}
else
{
temp -> setGammaRamp();
}
}
delete temp;
}
To copy to clipboard, switch view to plain text mode
and to get the gamma table I would use code like this:
screen = desktop -> screenNumber(this);
gamma_ramp* gammaRamp = new gamma_ramp(screen);
gammaRamp -> getGammaRamp();
QDesktopWidget* desktop;
desktop = new QDesktopWidget();
screen = desktop -> screenNumber(this);
gamma_ramp* gammaRamp = new gamma_ramp(screen);
gammaRamp -> getGammaRamp();
To copy to clipboard, switch view to plain text mode
Now I need to do this same type of thing for windows. Windows has has an API for this:
BOOL WINAPI SetDeviceGammaRamp(
HDC hDC,
LPVOID lpRamp
);
BOOL WINAPI SetDeviceGammaRamp(
HDC hDC,
LPVOID lpRamp
);
To copy to clipboard, switch view to plain text mode
and
BOOL WINAPI GetDeviceGammaRamp(
HDC hDC,
LPVOID lpRamp
);
BOOL WINAPI GetDeviceGammaRamp(
HDC hDC,
LPVOID lpRamp
);
To copy to clipboard, switch view to plain text mode
How do I get the hDC value so that I am working with the correct gamma table for the monitor where the current widget is located? In Qt I can locate that screen with QDesktopWidget::screenNumber(QWdiget*) can I use that information to somehow get a Windows handle to that device?
Bookmarks