Debian Codegraph @

spectro/madvrwin.c

raw symbols SCIP
1
2
3 /*
4 * Argyll Color Management System
5 * madVR Display target patch window
6 *
7 * Author: Graeme W. Gill
8 * Date: 3/4/12
9 *
10 * Copyright 2013 Graeme W. Gill
11 * All rights reserved.
12 *
13 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
14 * see the License.txt file for licencing details.
15 */
16
17 #include <stdio.h>
18 #include <string.h>
19 #ifdef NT
20 # include <winsock2.h>
21 #endif
22 #include "copyright.h"
23 #include "aconfig.h"
24 #include "icc.h"
25 #include "numsup.h"
26 #include "cgats.h"
27 #include "conv.h"
28 #include "dispwin.h"
29 #include "madvrwin.h"
30 #include "conv.h"
31 #include "mongoose.h"
32
33 #define ENABLE_RAMDAC
34
35 #undef DEBUG
36
37 #ifdef DEBUG
38 #define errout stderr
39 # define debug(xx) fprintf(errout, xx )
40 # define debug2(xx) fprintf xx
41 # define debugr(xx) fprintf(errout, xx )
42 # define debugr2(xx) fprintf xx
43 # define debugrr(xx) fprintf(errout, xx )
44 # define debugrr2(xx) fprintf xx
45 # define debugrr2l(lev, xx) fprintf xx
46 #else
47 #define errout stderr
48 # define debug(xx)
49 # define debug2(xx)
50 # define debugr(xx) if (p->ddebug) fprintf(errout, xx )
51 # define debugr2(xx) if (p->ddebug) fprintf xx
52 # define debugrr(xx) if (callback_ddebug) fprintf(errout, xx )
53 # define debugrr2(xx) if (callback_ddebug) fprintf xx
54 # define debugrr2l(lev, xx) if (callback_ddebug >= lev) fprintf xx
55 #endif
56
57 /* ----------------------------------------------- */
58 /* To work, MadTPG.exe has to be running */
59 /* ----------------------------------------------- */
60
61 /* MadVR functions */
62
63 #ifndef KEY_WOW64_32KEY
64 # define KEY_WOW64_32KEY (0x0200)
65 #endif
66
67 /* Avoid shlwapi.h since it's inclusion in SDK's is variable */
68 #include <pshpack1.h>
69
70 typedef struct _DLLVERSIONINFO {
71 DWORD cbSize;
72 DWORD dwMajorVersion;
73 DWORD dwMinorVersion;
74 DWORD dwBuildNumber;
75 DWORD dwPlatformID;
76 } DLLVERSIONINFO;
77
78 typedef struct _ADLLVERSIONINFO2 {
79 DLLVERSIONINFO info1;
80 DWORD dwFlags;
81 ULONGLONG ullVersion;
82 } ADLLVERSIONINFO2;
83 #include <poppack.h>
84
85 HMODULE HcNetDll = NULL;
86 BOOL (WINAPI *madVR_BlindConnect)(BOOL searchLan, DWORD timeOut) = NULL;
87 BOOL (WINAPI *madVR_GetVersion)(DWORD *version);
88 BOOL (WINAPI *madVR_SetOsdText)(LPCWSTR text);
89 BOOL (WINAPI *madVR_Disable3dlut)() = NULL;
90 BOOL (WINAPI *madVR_GetDeviceGammaRamp)(LPVOID ramp) = NULL;
91 BOOL (WINAPI *madVR_SetDeviceGammaRamp)(LPVOID ramp) = NULL;
92 BOOL (WINAPI *madVR_GetPatternConfig)(int *patternAreaInPercent, int *backgroundLevelInPercent,
93 int *backgroundMode, int *blackBorderWidth) = NULL;
94 BOOL (WINAPI *madVR_SetPatternConfig)(int patternAreaInPercent, int backgroundLevelInPercent,
95 int backgroundMode, int blackBorderWidth) = NULL;
96 BOOL (WINAPI *madVR_ShowRGB)(double r, double g, double b) = NULL;
97 BOOL (WINAPI *madVR_SetProgressBarPos)(int currentPos, int maxPos);
98 BOOL (WINAPI *madVR_Disconnect)() = NULL;
99
100 /* Locate and populate the MadVR functions */
101 /* Return NZ on failure */
102 static int initMadVR(dispwin *p) {
103 wchar_t *dllname;
104 WCHAR modname[MAX_PATH];
105 DWORD hnd;
106 DWORD len = 0;
107 WORD v1 = 0, v2 = 0, v3 = 0, v4 = 0; /* MadVR version */
108
109 if (sizeof(dllname) > 4) /* Compiled as 64 bit */
110 dllname = L"madHcNet64.dll";
111 else
112 dllname = L"madHcNet32.dll";
113
114 if ((HcNetDll = LoadLibraryW(dllname)) == NULL) {
115 HKEY hk1;
116 debugr2((errout,"MadVR LoadLibrary('%ls') failed - looking up registry\n",dllname));
117 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, L"CLSID\\{E1A8B82A-32CE-4B0D-BE0D-AA68C772E423}\\InprocServer32", 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &hk1) == ERROR_SUCCESS) {
118 DWORD size;
119 LPWSTR us1;
120 int i1;
121 size = MAX_PATH * 2 + 2;
122 us1 = (LPWSTR) LocalAlloc(LPTR, size + 20);
123 i1 = RegQueryValueExW(hk1, NULL, NULL, NULL, (LPBYTE) us1, &size);
124 if (i1 == ERROR_MORE_DATA) {
125 LocalFree(us1);
126 us1 = (LPWSTR) LocalAlloc(LPTR, size + 20);
127 i1 = RegQueryValueExW(hk1, NULL, NULL, NULL, (LPBYTE) us1, &size);
128 }
129 if (i1 == ERROR_SUCCESS) {
130 for (i1 = lstrlenW(us1) - 2; i1 > 0; i1--)
131 if (us1[i1] == L'\\') {
132 us1[i1 + 1] = 0;
133 break;
134 }
135 wcscat(us1, dllname);
136 HcNetDll = LoadLibraryW(us1);
137 if (HcNetDll != NULL)
138 debugr2((errout,"MadVR LoadLibrary('%ls') succeeded\n",us1));
139 else
140 debugr2((errout,"MadVR LoadLibrary('%ls') failed\n",us1));
141 }
142 LocalFree(us1);
143 RegCloseKey(hk1);
144 }
145 }
146
147
148 if (HcNetDll == NULL) {
149 debugr2((errout,"Failed to load %ls\n",dllname));
150 return 1;
151 }
152
153 *(FARPROC*)&madVR_BlindConnect = GetProcAddress(HcNetDll, "madVR_BlindConnect");
154 *(FARPROC*)&madVR_GetVersion = GetProcAddress(HcNetDll, "madVR_GetVersion");
155 *(FARPROC*)&madVR_SetOsdText = GetProcAddress(HcNetDll, "madVR_SetOsdText");
156 *(FARPROC*)&madVR_Disable3dlut = GetProcAddress(HcNetDll, "madVR_Disable3dlut");
157 *(FARPROC*)&madVR_GetDeviceGammaRamp = GetProcAddress(HcNetDll, "madVR_GetDeviceGammaRamp");
158 *(FARPROC*)&madVR_SetDeviceGammaRamp = GetProcAddress(HcNetDll, "madVR_SetDeviceGammaRamp");
159 *(FARPROC*)&madVR_GetPatternConfig = GetProcAddress(HcNetDll, "madVR_GetPatternConfig");
160 *(FARPROC*)&madVR_SetPatternConfig = GetProcAddress(HcNetDll, "madVR_SetPatternConfig");
161 *(FARPROC*)&madVR_ShowRGB = GetProcAddress(HcNetDll, "madVR_ShowRGB");
162 *(FARPROC*)&madVR_SetProgressBarPos = GetProcAddress(HcNetDll, "madVR_SetProgressBarPos");
163 *(FARPROC*)&madVR_Disconnect = GetProcAddress(HcNetDll, "madVR_Disconnect");
164
165 if (madVR_BlindConnect
166 && madVR_GetVersion
167 && madVR_SetOsdText
168 && madVR_Disable3dlut
169 && madVR_GetDeviceGammaRamp
170 && madVR_SetDeviceGammaRamp
171 && madVR_GetPatternConfig
172 && madVR_SetPatternConfig
173 && madVR_ShowRGB
174 && madVR_SetProgressBarPos
175 && madVR_Disconnect) {
176 DWORD ver = 0;
177
178 /* Return value is unclear */
179 if (!madVR_GetVersion(&ver))
180 debugr2((errout,"MadVR_GetVersion failed - can't determine MadVR version\n"));
181
182 debugr2((errout,"Found all required functions in %ls V%d.%d.%d.%d MadVR V%x.%x.%x.%x functions\n",dllname,v1,v2,v3,v4, 0xff & (ver >> 24), 0xff & (ver >> 16), 0xff & (ver >> 8), 0xff & ver));
183 return 0;
184 }
185 debugr2((errout,"Failed to locate MadVR function in %ls %d.%d.%d.%d\n",dllname,v1,v2,v3,v4));
186 FreeLibrary(HcNetDll);
187 HcNetDll = NULL;
188
189 return 1;
190 }
191
192
193 static void deinitMadVR(dispwin *p) {
194
195 if (HcNetDll != NULL) {
196
197 madVR_Disconnect();
198
199 /* This seems to crash in madHcNet64.dll32/64.dll when a lot */
200 /* of patches have been displayed ( > 11500 ?) */
201 // FreeLibrary(HcNetDll);
202
203 HcNetDll = NULL;
204 }
205 }
206
207 /* ----------------------------------------------- */
208
209 // ~~~ this needs fixing to allow for new madVR having more than 256 entries ?
210
211 /* Get RAMDAC values. ->del() when finished. */
212 /* Return NULL if not possible */
213 static ramdac *madvrwin_get_ramdac(dispwin *p) {
214 ramdac *r = NULL;
215
216 #ifdef ENABLE_RAMDAC
217 WORD vals[3][256]; /* 256 x 16 bit elements (Quantize) */
218 int i, j;
219
220 debugr("madvrwin_get_ramdac called\n");
221
222 /* Allocate a ramdac */
223 if ((r = (ramdac *)calloc(sizeof(ramdac), 1)) == NULL) {
224 debugr("madvrwin_get_ramdac failed on malloc()\n");
225 return NULL;
226 }
227 r->fdepth = p->fdepth;
228 r->rdepth = p->rdepth;
229 r->ndepth = p->ndepth;
230 r->nent = p->nent;
231
232 r->clone = dispwin_clone_ramdac;
233 r->setlin = dispwin_setlin_ramdac;
234 r->sane = dispwin_sane_ramdac;
235 r->del = dispwin_del_ramdac;
236
237 for (j = 0; j < 3; j++) {
238
239 if ((r->v[j] = (double *)calloc(sizeof(double), r->nent)) == NULL) {
240 for (j--; j >= 0; j--)
241 free(r->v[j]);
242 free(r);
243 debugr("madvrwin_get_ramdac failed on malloc()\n");
244 return NULL;
245 }
246 }
247
248 /* GetDeviceGammaRamp() is hard coded for 3 x 256 entries (Quantize) */
249 if (r->nent != 256) {
250 free(r);
251 debugr2((errout,"GetDeviceGammaRamp() is hard coded for nent == 256, and we've got nent = %d!\n",r->nent));
252 return NULL;
253 }
254
255 if (!madVR_GetDeviceGammaRamp(vals)) {
256 free(r);
257 debugr2((errout,"madvrwin_get_ramdac failed on madVR_GetDeviceGammaRamp()\n"));
258 return NULL;
259 }
260
261 for (j = 0; j < 3; j++) {
262 for (i = 0; i < r->nent; i++) {
263 r->v[j][i] = vals[j][i]/65535.0;
264 }
265 }
266 #endif // ENABLE_RAMDAC
267 debugr2((errout,"madvrwin_get_ramdac returning %p\n",r));
268
269 return r;
270 }
271
272 /* Set the RAMDAC values. */
273 /* Return nz if not possible */
274 static int madvrwin_set_ramdac(dispwin *p, ramdac *r, int persist) {
275 int rv = 1;
276
277 #ifdef ENABLE_RAMDAC
278 int i, j;
279 WORD vals[3][256]; /* 16 bit elements */
280
281 debugr("madvrwin_set_ramdac called\n");
282
283 for (j = 0; j < 3; j++) {
284 for (i = 0; i < r->nent; i++) {
285 double vv = r->v[j][i];
286 if (vv < 0.0)
287 vv = 0.0;
288 else if (vv > 1.0)
289 vv = 1.0;
290 vals[j][i] = (int)(65535.0 * vv + 0.5);
291 }
292 }
293
294 if (!madVR_SetDeviceGammaRamp(vals)) {
295 debugr2((errout,"madvrwin_set_ramdac failed on madVR_SetDeviceGammaRamp()\n"));
296 rv = 1;
297 } else {
298 debugr("madvrwin_set_ramdac set\n");
299 rv = 0;
300 }
301 #endif // ENABLE_RAMDAC
302 return rv;
303 }
304
305 /* ----------------------------------------------- */
306 /* Install a display profile and make */
307 /* it the default for this display. */
308 /* Return nz if failed */
309 int madvrwin_install_profile(dispwin *p, char *fname, ramdac *r, p_scope scope) {
310 debugr("madVRdisp doesn't support installing profiles\n");
311 return 1;
312 }
313
314 /* Un-Install a display profile */
315 /* Return nz if failed, */
316 int madvrwin_uninstall_profile(dispwin *p, char *fname, p_scope scope) {
317 debugr("madVRdisp doesn't support uninstalling profiles\n");
318 return 1;
319 }
320
321 /* Get the currently installed display profile. */
322 /* Return NULL if failed. */
323 icmFile *madvrwin_get_profile(dispwin *p, char *name, int mxlen) {
324 debugr("madVRdisp doesn't support getting the current profile\n");
325 return NULL;
326 }
327
328 /* ----------------------------------------------- */
329
330 /* Change the window color. */
331 /* Return 1 on error, 2 on window being closed */
332 static int madvrwin_set_color(
333 dispwin *p,
334 double r, double g, double b /* Color values 0.0 - 1.0 */
335 ) {
336 int j;
337 double orgb[3]; /* Previous RGB value */
338 double kr, kf;
339 int update_delay = 0;
340
341 debugr("madvrwin_set_color called\n");
342
343 if (p->nowin) {
344 debugr2((errout,"no window\n"));
345 return 1;
346 }
347
348 orgb[0] = p->rgb[0]; p->rgb[0] = r;
349 orgb[1] = p->rgb[1]; p->rgb[1] = g;
350 orgb[2] = p->rgb[2]; p->rgb[2] = b;
351
352 if (!madVR_ShowRGB(p->rgb[0], p->rgb[1], p->rgb[2])) {
353 debugr2((errout,"madVR_ShowRGB failed\n"));
354 return 1;
355 }
356
357 /* Allow for display update & instrument delays */
358 update_delay = dispwin_compute_delay(p, orgb);
359 debugr2((errout, "madvrwin_set_color delaying %d msec\n",update_delay));
360 msec_sleep(update_delay);
361
362 return 0;
363 }
364
365 /* Set/unset the full screen black flag */
366 /* Return nz on error */
367 static int madvrwin_set_fc(dispwin *p, int fullscreen) {
368 int perc, bgperc, bgmode, border;
369
370 p->fullscreen = fullscreen;
371
372 /* Parameters that shouldn't change can be set to -1, but this doesn't seem */
373 /* to work for background level, so get current background level */
374 if (!madVR_GetPatternConfig(&perc, &bgperc, &bgmode, &border)) {
375 debugr2((errout,"madVR_GetPatternConfig failed\n"));
376 return 1;
377 }
378 debugr2((errout,"madvrwin_set_fc: got pattern config %i, %i, %i, %i\n",
379 perc, bgperc, bgmode, border));
380
381 /* Default test window is 10% of the width/height = 1% of the area*/
382 perc = (int)((p->width/100.0 * 0.1 * p->height/100.0 * 0.1) * 100.0 + 0.5);
383
384 /* Background level is 1..100 in percent */
385 debugr2((errout,"madvrwin_set_fc: setting pattern config %i, %i\n",
386 perc, fullscreen ? 0 : bgperc));
387 if (!madVR_SetPatternConfig(perc, fullscreen ? 0 : bgperc, -1, -1)) {
388 debugr2((errout,"madVR_SetPatternConfig failed\n"));
389 return 1;
390 }
391
392 return 0;
393 }
394
395 /* ----------------------------------------------- */
396 /* set patch info */
397 /* Return nz on error */
398 static int madvrwin_set_pinfo(dispwin *p, int pno, int tno) {
399
400 if (!madVR_SetProgressBarPos(pno, tno))
401 return 1;
402
403 return 0;
404 }
405
406 /* ----------------------------------------------- */
407 /* Set the shell set color callout */
408 void madvrwin_set_callout(
409 dispwin *p,
410 char *callout
411 ) {
412 debugr2((errout,"madvrwin_set_callout called with '%s'\n",callout));
413
414 p->callout = strdup(callout);
415 }
416
417 /* ----------------------------------------------- */
418 /* Destroy ourselves */
419 static void madvrwin_del(
420 dispwin *p
421 ) {
422
423 debugr("madvrwin_del called\n");
424
425 if (p == NULL)
426 return;
427
428 deinitMadVR(p);
429
430 if (p->name != NULL)
431 free(p->name);
432 if (p->description != NULL)
433 free(p->description);
434 if (p->callout != NULL)
435 free(p->callout);
436
437 /* Since we don't restore the display, delete these here */
438 if (p->oor != NULL) {
439 p->oor->del(p->oor);
440 p->oor = NULL;
441 }
442 if (p->or != NULL) {
443 p->or->del(p->or);
444 p->or = NULL;
445 }
446 if (p->r != NULL) {
447 p->r->del(p->r);
448 p->r = NULL;
449 }
450
451 free(p);
452 }
453
454 /* ----------------------------------------------- */
455
456 /* Create a web display test window, default grey */
457 dispwin *new_madvrwin(
458 double width, double height, /* Width and height in mm - turned into % of screen */
459 double hoff, double voff, /* Offset from center in fraction of screen - ignored */
460 int nowin, /* NZ if no window should be created - RAMDAC access only */
461 int native, /* X0 = use current per channel calibration curve */
462 /* X1 = set native linear output and use ramdac high precn. */
463 /* 0X = use current color management cLut (MadVR) */
464 /* 1X = disable color management cLUT (MadVR) */
465 int *noramdac, /* Return nz if no ramdac access. native is set to X0 */
466 int *nocm, /* Return nz if no CM cLUT access. native is set to 0X */
467 double icalmax, /* Scale instrument calibration test values by this (0.0 .. 1.0) */
468 int out_tvenc, /* 1 = use RGB Video Level encoding */
469 int fullscreen, /* NZ if whole screen should be filled with black */
470 int verb, /* NZ for verbose prompts */
471 int ddebug /* >0 to print debug statements to stderr */
472 ) {
473 dispwin *p = NULL;
474 char *cp;
475 const char *options[3];
476 char port[50];
477
478 debug("new_madvrwin called\n");
479
480 if (out_tvenc) {
481 if (ddebug) fprintf(stderr,"new_madvrwin failed because out_tvenc set\n");
482 return NULL;
483 }
484
485 if ((p = (dispwin *)calloc(sizeof(dispwin), 1)) == NULL) {
486 if (ddebug) fprintf(stderr,"new_madvrwin failed because malloc failed\n");
487 return NULL;
488 }
489
490 /* !!!! Make changes in dispwin.c & webwin.c as well !!!! */
491 p->name = strdup("Web Window");
492 p->width = width;
493 p->height = height;
494 p->nowin = nowin;
495 p->native = native;
496 p->icalmax = icalmax;
497 p->out_tvenc = 0;
498 p->fullscreen = fullscreen;
499 p->ddebug = ddebug;
500 p->get_ramdac = madvrwin_get_ramdac;
501 p->set_ramdac = madvrwin_set_ramdac;
502 p->install_profile = madvrwin_install_profile;
503 p->uninstall_profile = madvrwin_uninstall_profile;
504 p->get_profile = madvrwin_get_profile;
505 p->set_color = madvrwin_set_color;
506 p->set_fc = madvrwin_set_fc;
507 p->set_pinfo = madvrwin_set_pinfo;
508 p->set_update_delay = dispwin_set_update_delay;
509 p->set_settling_delay = dispwin_set_settling_delay;
510 p->enable_update_delay = dispwin_enable_update_delay;
511 p->set_callout = madvrwin_set_callout;
512 p->del = madvrwin_del;
513
514 debugr2((errout, "new_madvrwin got native = %d\n",native));
515
516 #ifndef ENABLE_RAMDAC
517 if (noramdac != NULL)
518 *noramdac = 1;
519 p->native &= ~1;
520 #endif
521
522 p->rgb[0] = p->rgb[1] = p->rgb[2] = 0.5 * icalmax; /* Set Grey as the initial test color */
523
524 dispwin_set_default_delays(p);
525
526 p->fdepth = 8; /* Assume this */
527 p->rdepth = p->fdepth; /* Assumed */
528 p->ndepth = p->rdepth; /* Assumed */
529 #ifdef ENABLE_RAMDAC
530 p->nent = (1 << p->ndepth);
531 #else
532 p->nent = 0; /* No ramdac */
533 #endif
534 p->edepth = 16; /* Assumed */
535
536 if (initMadVR(p)) {
537 debugr2((errout,"Failed to locate MadVR .dll or functions\n"));
538 free(p);
539 return NULL;
540 }
541
542 if (!madVR_BlindConnect(1, 1000)) {
543 debugr2((errout,"Failed to connect to MadVR\n"));
544 free(p);
545 return NULL;
546 }
547
548 if (p->native & 2) {
549 debugr2((errout,"new_madvrwin: disbling 3dLuts\n"));
550 madVR_Disable3dlut();
551 }
552
553 p->set_fc(p, fullscreen);
554
555 /* Create a suitable description */
556 {
557 char buf[1000];
558
559 sprintf(buf,"ArgyllCMS Patches");
560 p->description = strdup(buf);
561
562 if (verb)
563 printf("Created MadVR window\n");
564
565 madVR_SetOsdText(L"ArgyllCMS Patches");
566 }
567
568 #ifdef ENABLE_RAMDAC
569
570 /* Save the original ramdac, which gets restored on exit */
571 if ((p->or = p->get_ramdac(p)) != NULL) {
572
573 debugr("Saved original VideoLUT\n");
574
575 if (noramdac != NULL)
576 *noramdac = 0;
577
578 /* Copy original ramdac that never gets altered */
579 if ((p->oor = p->or->clone(p->or)) == NULL) {
580 madvrwin_del(p);
581 debugr("ramdac clone failed - memory ?\n");
582 return NULL;
583 }
584
585 /* Create a working ramdac for native or other use */
586 if ((p->r = p->or->clone(p->or)) == NULL) {
587 madvrwin_del(p);
588 debugr("ramdac clone failed - memory ?\n");
589 return NULL;
590 }
591
592 /* Setup for native mode == linear RAMDAC */
593 if ((p->native & 1) == 1) {
594 int ii = 0;
595 debug("About to setup native mode\n");
596
597 /* Since we MadVR does dithering, we don't need to use the VideoLUTs */
598 /* to emulate higher precision, so simply set it to linear here. */
599 if ((ii = madVR_SetDeviceGammaRamp(NULL)) == 0) {
600 madvrwin_del(p);
601 debugr("Clear gamma ramp failed\n");
602 return NULL;
603 }
604 }
605
606 } else {
607 debugr2((errout,"Unable to access VideoLUT\n"));
608 if (noramdac != NULL)
609 *noramdac = 1;
610 p->oor = p->or = p->r = NULL;
611 }
612
613 if (!p->nowin) {
614
615 /* Make sure initial test color is displayed */
616 madvrwin_set_color(p, p->rgb[0], p->rgb[1], p->rgb[2]);
617 }
618 #endif
619
620 debugr("new_madvrwin: return successfully\n");
621
622 return p;
623 }
624