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

Qt Code:
  1. class gamma_ramp
  2. {
  3. public:
  4. int screen;
  5. int size;
  6. unsigned short *red, *green, *blue;
  7.  
  8. gamma_ramp(int);
  9. ~gamma_ramp();
  10. void getGammaRamp();
  11. void setGammaRamp();
  12. void setLinearGammaRamp();
  13.  
  14. private:
  15. int getGammaRampSize(int);
  16. };
To copy to clipboard, switch view to plain text mode 

gamma_ramp-x11.cpp

Qt Code:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <sys/types.h>
  5. #include <time.h>
  6. #include <string.h>
  7.  
  8. // X11 stuff
  9. #include <X11/Xos.h>
  10. #include <X11/Xlib.h>
  11. #include <X11/Xutil.h>
  12. #include <X11/Xlibint.h>
  13. #include <X11/Xproto.h>
  14. #include <X11/Xatom.h>
  15. #include <X11/extensions/xf86vmode.h>
  16. #include <X11/extensions/dpms.h>
  17. #include <X11/extensions/Xinerama.h>
  18. #include "gamma_ramp.h"
  19.  
  20. gamma_ramp::gamma_ramp(int Screen)
  21. {
  22. screen = Screen;
  23. size = getGammaRampSize(screen);
  24. // now allocate the arrays
  25. red = new unsigned short[size];
  26. green = new unsigned short[size];
  27. blue = new unsigned short[size];
  28. }
  29.  
  30. gamma_ramp::~gamma_ramp()
  31. {
  32. delete[] red;
  33. delete[] green;
  34. delete[] blue;
  35. }
  36.  
  37. int gamma_ramp::getGammaRampSize(int Screen)
  38. {
  39. int size = 0;
  40. int event_base;
  41. int error_base;
  42. Display *dpy;
  43.  
  44. if ((dpy = XOpenDisplay(":0.0")) == NULL)
  45. {
  46. printf("failed to open display :0.0\n");
  47. }
  48. else
  49. {
  50. if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
  51. {
  52. printf("XF86VidModeQueryExtension failed\n");
  53. }
  54. else
  55. {
  56. if (!XF86VidModeGetGammaRampSize(dpy, Screen, &size))
  57. {
  58. printf("failed to get XF86VidModeGetGammaRampSize\n");
  59. }
  60. }
  61. }
  62. return size; // will be 0 if failed
  63. }
  64.  
  65. void gamma_ramp::getGammaRamp()
  66. {
  67. int event_base;
  68. int error_base;
  69. Display *dpy;
  70.  
  71. if ((dpy = XOpenDisplay(":0.0")) == NULL)
  72. {
  73. printf("failed to open display :0.0\n");
  74. }
  75. else
  76. {
  77. if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
  78. {
  79. printf("XF86VidModeQueryExtension failed\n");
  80. }
  81. else
  82. {
  83. XF86VidModeGetGammaRamp(dpy, screen, size,
  84. red, green, blue);
  85.  
  86. }
  87. }
  88. }
  89.  
  90. void gamma_ramp::setGammaRamp()
  91. {
  92. int event_base;
  93. int error_base;
  94. Display *dpy;
  95.  
  96. if ((dpy = XOpenDisplay(":0.0")) == NULL)
  97. {
  98. printf("failed to open display :0.0\n");
  99. }
  100. else
  101. {
  102. if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
  103. {
  104. printf("XF86VidModeQueryExtension failed\n");
  105. }
  106. else
  107. {
  108. XF86VidModeSetGammaRamp(dpy, screen, size,
  109. red, green, blue);
  110. }
  111. }
  112. }
  113.  
  114. void gamma_ramp::setLinearGammaRamp()
  115. {
  116. int event_base;
  117. int error_base;
  118. Display *dpy;
  119. gamma_ramp* temp;
  120.  
  121. temp = new gamma_ramp(screen);
  122.  
  123. for (int i=0; i< temp -> size; i++)
  124. {
  125. temp -> red[i] = temp -> green[i] = temp -> blue[i] = i * 256;
  126. }
  127.  
  128. if ((dpy = XOpenDisplay(":0.0")) == NULL)
  129. {
  130. printf("failed to open display :0.0\n");
  131. }
  132. else
  133. {
  134. if (XF86VidModeQueryExtension(dpy, &event_base, &error_base) ==0)
  135. {
  136. printf("XF86VidModeQueryExtension failed\n");
  137. }
  138. else
  139. {
  140. temp -> setGammaRamp();
  141. }
  142. }
  143. delete temp;
  144. }
To copy to clipboard, switch view to plain text mode 

and to get the gamma table I would use code like this:

Qt Code:
  1. QDesktopWidget* desktop;
  2. desktop = new QDesktopWidget();
  3. screen = desktop -> screenNumber(this);
  4. gamma_ramp* gammaRamp = new gamma_ramp(screen);
  5. 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:

Qt Code:
  1. BOOL WINAPI SetDeviceGammaRamp(
  2. HDC hDC,
  3. LPVOID lpRamp
  4. );
To copy to clipboard, switch view to plain text mode 

and

Qt Code:
  1. BOOL WINAPI GetDeviceGammaRamp(
  2. HDC hDC,
  3. LPVOID lpRamp
  4. );
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?