Authors: Mike McCormack <mike@codeweavers.com>, Aric Stewart <aric@codeweavers.com...
[wine] / dlls / mscms / handle.c
1 /*
2  * MSCMS - Color Management System for Wine
3  *
4  * Copyright 2004 Hans Leidekker
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "icm.h"
29
30 #include "mscms_priv.h"
31
32 #ifdef HAVE_LCMS_H
33
34 static CRITICAL_SECTION MSCMS_handle_cs;
35 static CRITICAL_SECTION_DEBUG MSCMS_handle_cs_debug =
36 {
37     0, 0, &MSCMS_handle_cs,
38     { &MSCMS_handle_cs_debug.ProcessLocksList,
39       &MSCMS_handle_cs_debug.ProcessLocksList },
40       0, 0, { 0, (DWORD)(__FILE__ ": MSCMS_handle_cs") }
41 };
42 static CRITICAL_SECTION MSCMS_handle_cs = { &MSCMS_handle_cs_debug, -1, 0, 0, 0, 0 };
43
44 /*  A simple structure to tie together a pointer to an icc profile, an lcms
45  *  color profile handle and a Windows file handle. Windows color profile 
46  *  handles are built from indexes into an array of these structures. If
47  *  the profile is memory based the file handle field is NULL.
48  */
49
50 struct handlemap
51 {
52     HANDLE file;
53     icProfile *iccprofile;
54     cmsHPROFILE cmsprofile;
55 };
56
57 #define CMSMAXHANDLES 0x80
58
59 static struct handlemap handlemaptable[CMSMAXHANDLES];
60
61 HPROFILE MSCMS_handle2hprofile( HANDLE file )
62 {
63     HPROFILE profile = NULL;
64     unsigned int i;
65
66     if (!file) return NULL;
67
68     EnterCriticalSection( &MSCMS_handle_cs );
69
70     for (i = 0; i <= CMSMAXHANDLES; i++)
71     {
72         if (handlemaptable[i].file == file)
73         {
74             profile = (HPROFILE)(i + 1); goto out;
75         }
76     }
77
78 out:
79     LeaveCriticalSection( &MSCMS_handle_cs );
80
81     return profile;
82 }
83
84 HANDLE MSCMS_hprofile2handle( HPROFILE profile )
85 {
86     HANDLE file;
87     unsigned int i;
88
89     EnterCriticalSection( &MSCMS_handle_cs );
90
91     i = (unsigned int)profile - 1;
92     file = handlemaptable[i].file;
93
94     LeaveCriticalSection( &MSCMS_handle_cs );
95
96     return file;
97 }
98
99 HPROFILE MSCMS_cmsprofile2hprofile( cmsHPROFILE cmsprofile )
100 {
101     HPROFILE profile = NULL;
102     unsigned int i;
103
104     if (!cmsprofile) return NULL;
105
106     EnterCriticalSection( &MSCMS_handle_cs );
107
108     for (i = 0; i <= CMSMAXHANDLES; i++)
109     {
110         if (handlemaptable[i].cmsprofile == cmsprofile)
111         {
112             profile = (HPROFILE)(i + 1); goto out;
113         }
114     }
115
116 out:
117     LeaveCriticalSection( &MSCMS_handle_cs );
118
119     return profile;
120 }
121
122 cmsHPROFILE MSCMS_hprofile2cmsprofile( HPROFILE profile )
123 {
124     cmsHPROFILE cmshprofile;
125     unsigned int i;
126
127     EnterCriticalSection( &MSCMS_handle_cs );
128
129     i = (unsigned int)profile - 1;
130     cmshprofile = handlemaptable[i].cmsprofile;
131
132     LeaveCriticalSection( &MSCMS_handle_cs );
133
134     return cmshprofile;
135 }
136
137 HPROFILE MSCMS_iccprofile2hprofile( icProfile *iccprofile )
138 {
139     HPROFILE profile = NULL;
140     unsigned int i;
141
142     if (!iccprofile) return NULL;
143
144     EnterCriticalSection( &MSCMS_handle_cs );
145
146     for (i = 0; i <= CMSMAXHANDLES; i++)
147     {
148         if (handlemaptable[i].iccprofile == iccprofile)
149         {
150             profile = (HPROFILE)(i + 1); goto out;
151         }
152     }
153
154 out:
155     LeaveCriticalSection( &MSCMS_handle_cs );
156
157     return profile;
158 }
159
160 icProfile *MSCMS_hprofile2iccprofile( HPROFILE profile )
161 {
162     icProfile *iccprofile;
163     unsigned int i;
164
165     EnterCriticalSection( &MSCMS_handle_cs );
166
167     i = (unsigned int)profile - 1;
168     iccprofile = handlemaptable[i].iccprofile;
169
170     LeaveCriticalSection( &MSCMS_handle_cs );
171
172     return iccprofile;
173 }
174
175 HPROFILE MSCMS_create_hprofile_handle( HANDLE file, icProfile *iccprofile, cmsHPROFILE cmsprofile )
176 {
177     HPROFILE profile = NULL;
178     unsigned int i;
179
180     if (!cmsprofile || !iccprofile) return NULL;
181
182     EnterCriticalSection( &MSCMS_handle_cs );
183
184     for (i = 0; i <= CMSMAXHANDLES; i++)
185     {
186         if (handlemaptable[i].iccprofile == 0)
187         {
188             handlemaptable[i].file = file;
189             handlemaptable[i].iccprofile = iccprofile;
190             handlemaptable[i].cmsprofile = cmsprofile;
191
192             profile = (HPROFILE)(i + 1); goto out;
193         }
194     }
195
196 out:
197     LeaveCriticalSection( &MSCMS_handle_cs );
198
199     return profile;
200 }
201
202 void MSCMS_destroy_hprofile_handle( HPROFILE profile )
203 {
204     unsigned int i;
205
206     if (profile)
207     {
208         EnterCriticalSection( &MSCMS_handle_cs );
209
210         i = (unsigned int)profile - 1;
211         memset( &handlemaptable[i], 0, sizeof(struct handlemap) );
212
213         LeaveCriticalSection( &MSCMS_handle_cs );
214     }
215 }
216
217 #endif /* HAVE_LCMS_H */