Don't call unicode functions of file API as they fail on Win9x.
[wine] / dlls / mscms / handle.c
1 /*
2  * MSCMS - Color Management System for Wine
3  *
4  * Copyright 2004, 2005 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
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, { (DWORD_PTR)(__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. The 'access'
48  *  field records the access parameter supplied to an OpenColorProfile()
49  *  call, i.e. PROFILE_READ or PROFILE_READWRITE.
50  */
51
52 struct profile
53 {
54     HANDLE file;
55     DWORD access;
56     icProfile *iccprofile;
57     cmsHPROFILE cmsprofile;
58 };
59
60 struct transform
61 {
62     cmsHTRANSFORM cmstransform;
63 };
64
65 #define CMSMAXHANDLES 0x80
66
67 static struct profile profiletable[CMSMAXHANDLES];
68 static struct transform transformtable[CMSMAXHANDLES];
69
70 HPROFILE MSCMS_handle2hprofile( HANDLE file )
71 {
72     HPROFILE profile = NULL;
73     unsigned int i;
74
75     if (!file) return NULL;
76
77     EnterCriticalSection( &MSCMS_handle_cs );
78
79     for (i = 0; i <= CMSMAXHANDLES; i++)
80     {
81         if (profiletable[i].file == file)
82         {
83             profile = (HPROFILE)(i + 1); goto out;
84         }
85     }
86
87 out:
88     LeaveCriticalSection( &MSCMS_handle_cs );
89     return profile;
90 }
91
92 HANDLE MSCMS_hprofile2handle( HPROFILE profile )
93 {
94     HANDLE file;
95     unsigned int i;
96
97     EnterCriticalSection( &MSCMS_handle_cs );
98
99     i = (unsigned int)profile - 1;
100     file = profiletable[i].file;
101
102     LeaveCriticalSection( &MSCMS_handle_cs );
103     return file;
104 }
105
106 DWORD MSCMS_hprofile2access( HPROFILE profile )
107 {
108     DWORD access;
109     unsigned int i;
110
111     EnterCriticalSection( &MSCMS_handle_cs );
112
113     i = (unsigned int)profile - 1;
114     access = profiletable[i].access;
115
116     LeaveCriticalSection( &MSCMS_handle_cs );
117     return access;
118 }
119
120 HPROFILE MSCMS_cmsprofile2hprofile( cmsHPROFILE cmsprofile )
121 {
122     HPROFILE profile = NULL;
123     unsigned int i;
124
125     if (!cmsprofile) return NULL;
126
127     EnterCriticalSection( &MSCMS_handle_cs );
128
129     for (i = 0; i <= CMSMAXHANDLES; i++)
130     {
131         if (profiletable[i].cmsprofile == cmsprofile)
132         {
133             profile = (HPROFILE)(i + 1); goto out;
134         }
135     }
136
137 out:
138     LeaveCriticalSection( &MSCMS_handle_cs );
139     return profile;
140 }
141
142 cmsHPROFILE MSCMS_hprofile2cmsprofile( HPROFILE profile )
143 {
144     cmsHPROFILE cmsprofile;
145     unsigned int i;
146
147     EnterCriticalSection( &MSCMS_handle_cs );
148
149     i = (unsigned int)profile - 1;
150     cmsprofile = profiletable[i].cmsprofile;
151
152     LeaveCriticalSection( &MSCMS_handle_cs );
153     return cmsprofile;
154 }
155
156 HPROFILE MSCMS_iccprofile2hprofile( icProfile *iccprofile )
157 {
158     HPROFILE profile = NULL;
159     unsigned int i;
160
161     if (!iccprofile) return NULL;
162
163     EnterCriticalSection( &MSCMS_handle_cs );
164
165     for (i = 0; i <= CMSMAXHANDLES; i++)
166     {
167         if (profiletable[i].iccprofile == iccprofile)
168         {
169             profile = (HPROFILE)(i + 1); goto out;
170         }
171     }
172
173 out:
174     LeaveCriticalSection( &MSCMS_handle_cs );
175     return profile;
176 }
177
178 icProfile *MSCMS_hprofile2iccprofile( HPROFILE profile )
179 {
180     icProfile *iccprofile;
181     unsigned int i;
182
183     EnterCriticalSection( &MSCMS_handle_cs );
184
185     i = (unsigned int)profile - 1;
186     iccprofile = profiletable[i].iccprofile;
187
188     LeaveCriticalSection( &MSCMS_handle_cs );
189     return iccprofile;
190 }
191
192 HPROFILE MSCMS_create_hprofile_handle( HANDLE file, icProfile *iccprofile,
193                                        cmsHPROFILE cmsprofile, DWORD access )
194 {
195     HPROFILE profile = NULL;
196     unsigned int i;
197
198     if (!cmsprofile || !iccprofile) return NULL;
199
200     EnterCriticalSection( &MSCMS_handle_cs );
201
202     for (i = 0; i <= CMSMAXHANDLES; i++)
203     {
204         if (profiletable[i].iccprofile == 0)
205         {
206             profiletable[i].file = file;
207             profiletable[i].access = access;
208             profiletable[i].iccprofile = iccprofile;
209             profiletable[i].cmsprofile = cmsprofile;
210
211             profile = (HPROFILE)(i + 1); goto out;
212         }
213     }
214
215 out:
216     LeaveCriticalSection( &MSCMS_handle_cs );
217     return profile;
218 }
219
220 void MSCMS_destroy_hprofile_handle( HPROFILE profile )
221 {
222     unsigned int i;
223
224     if (profile)
225     {
226         EnterCriticalSection( &MSCMS_handle_cs );
227
228         i = (unsigned int)profile - 1;
229         memset( &profiletable[i], 0, sizeof(struct profile) );
230
231         LeaveCriticalSection( &MSCMS_handle_cs );
232     }
233 }
234
235 cmsHTRANSFORM MSCMS_htransform2cmstransform( HTRANSFORM transform )
236 {
237     cmsHTRANSFORM cmstransform;
238     unsigned int i;
239
240     EnterCriticalSection( &MSCMS_handle_cs );
241
242     i = (unsigned int)transform - 1;
243     cmstransform = transformtable[i].cmstransform;
244
245     LeaveCriticalSection( &MSCMS_handle_cs );
246     return cmstransform;
247 }
248
249 HTRANSFORM MSCMS_create_htransform_handle( cmsHTRANSFORM cmstransform )
250 {
251     HTRANSFORM transform = NULL;
252     unsigned int i;
253
254     if (!cmstransform) return NULL;
255
256     EnterCriticalSection( &MSCMS_handle_cs );
257
258     for (i = 0; i <= CMSMAXHANDLES; i++)
259     {
260         if (transformtable[i].cmstransform == 0)
261         {
262             transformtable[i].cmstransform = cmstransform;
263             transform = (HTRANSFORM)(i + 1); goto out;
264         }
265     }
266
267 out:
268     LeaveCriticalSection( &MSCMS_handle_cs );
269     return transform;
270 }
271
272 void MSCMS_destroy_htransform_handle( HTRANSFORM transform )
273 {
274     unsigned int i;
275
276     if (transform)
277     {
278         EnterCriticalSection( &MSCMS_handle_cs );
279
280         i = (unsigned int)transform - 1;
281         memset( &transformtable[i], 0, sizeof(struct transform) );
282
283         LeaveCriticalSection( &MSCMS_handle_cs );
284     }
285 }
286
287 #endif /* HAVE_LCMS */