d3d8: Get rid of the format switching code in d3d8_device_CopyRects().
[wine.git] / dlls / mscms / handle.c
blob10334c65b217bae22227aa35ff1f557157df3981
1 /*
2 * MSCMS - Color Management System for Wine
4 * Copyright 2004, 2005, 2008 Hans Leidekker
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/debug.h"
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "icm.h"
32 #include "mscms_priv.h"
34 #ifdef HAVE_LCMS2
36 static CRITICAL_SECTION mscms_handle_cs;
37 static CRITICAL_SECTION_DEBUG mscms_handle_cs_debug =
39 0, 0, &mscms_handle_cs,
40 { &mscms_handle_cs_debug.ProcessLocksList,
41 &mscms_handle_cs_debug.ProcessLocksList },
42 0, 0, { (DWORD_PTR)(__FILE__ ": mscms_handle_cs") }
44 static CRITICAL_SECTION mscms_handle_cs = { &mscms_handle_cs_debug, -1, 0, 0, 0, 0 };
46 static struct profile *profiletable;
47 static struct transform *transformtable;
49 static unsigned int num_profile_handles;
50 static unsigned int num_transform_handles;
52 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
54 void free_handle_tables( void )
56 HeapFree( GetProcessHeap(), 0, profiletable );
57 profiletable = NULL;
58 num_profile_handles = 0;
60 HeapFree( GetProcessHeap(), 0, transformtable );
61 transformtable = NULL;
62 num_transform_handles = 0;
64 DeleteCriticalSection( &mscms_handle_cs );
67 struct profile *grab_profile( HPROFILE handle )
69 DWORD_PTR index;
71 EnterCriticalSection( &mscms_handle_cs );
73 index = (DWORD_PTR)handle - 1;
74 if (index > num_profile_handles)
76 LeaveCriticalSection( &mscms_handle_cs );
77 return NULL;
79 return &profiletable[index];
82 void release_profile( struct profile *profile )
84 LeaveCriticalSection( &mscms_handle_cs );
87 struct transform *grab_transform( HTRANSFORM handle )
89 DWORD_PTR index;
91 EnterCriticalSection( &mscms_handle_cs );
93 index = (DWORD_PTR)handle - 1;
94 if (index > num_transform_handles)
96 LeaveCriticalSection( &mscms_handle_cs );
97 return NULL;
99 return &transformtable[index];
102 void release_transform( struct transform *transform )
104 LeaveCriticalSection( &mscms_handle_cs );
107 static HPROFILE alloc_profile_handle( void )
109 DWORD_PTR index;
110 struct profile *p;
111 unsigned int count = 128;
113 for (index = 0; index < num_profile_handles; index++)
115 if (!profiletable[index].data) return (HPROFILE)(index + 1);
117 if (!profiletable)
119 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(struct profile) );
121 else
123 count = num_profile_handles * 2;
124 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, profiletable, count * sizeof(struct profile) );
126 if (!p) return NULL;
128 profiletable = p;
129 num_profile_handles = count;
131 return (HPROFILE)(index + 1);
134 HPROFILE create_profile( struct profile *profile )
136 HPROFILE handle;
138 EnterCriticalSection( &mscms_handle_cs );
140 if ((handle = alloc_profile_handle()))
142 DWORD_PTR index = (DWORD_PTR)handle - 1;
143 profiletable[index] = *profile;
145 LeaveCriticalSection( &mscms_handle_cs );
146 return handle;
149 BOOL close_profile( HPROFILE handle )
151 DWORD_PTR index;
152 struct profile *profile;
154 EnterCriticalSection( &mscms_handle_cs );
156 index = (DWORD_PTR)handle - 1;
157 if (index > num_profile_handles)
159 LeaveCriticalSection( &mscms_handle_cs );
160 return FALSE;
162 profile = &profiletable[index];
164 if (profile->file != INVALID_HANDLE_VALUE)
166 if (profile->access & PROFILE_READWRITE)
168 DWORD written;
170 if (SetFilePointer( profile->file, 0, NULL, FILE_BEGIN ) ||
171 !WriteFile( profile->file, profile->data, profile->size, &written, NULL ) ||
172 written != profile->size)
174 ERR( "Unable to write color profile\n" );
177 CloseHandle( profile->file );
179 cmsCloseProfile( profile->cmsprofile );
180 HeapFree( GetProcessHeap(), 0, profile->data );
182 memset( profile, 0, sizeof(struct profile) );
184 LeaveCriticalSection( &mscms_handle_cs );
185 return TRUE;
188 static HTRANSFORM alloc_transform_handle( void )
190 DWORD_PTR index;
191 struct transform *p;
192 unsigned int count = 128;
194 for (index = 0; index < num_transform_handles; index++)
196 if (!transformtable[index].cmstransform) return (HTRANSFORM)(index + 1);
198 if (!transformtable)
200 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(struct transform) );
202 else
204 count = num_transform_handles * 2;
205 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, transformtable, count * sizeof(struct transform) );
207 if (!p) return NULL;
209 transformtable = p;
210 num_transform_handles = count;
212 return (HTRANSFORM)(index + 1);
215 HTRANSFORM create_transform( struct transform *transform )
217 HTRANSFORM handle;
219 EnterCriticalSection( &mscms_handle_cs );
221 if ((handle = alloc_transform_handle()))
223 DWORD_PTR index = (DWORD_PTR)handle - 1;
224 transformtable[index] = *transform;
226 LeaveCriticalSection( &mscms_handle_cs );
227 return handle;
230 BOOL close_transform( HTRANSFORM handle )
232 DWORD_PTR index;
233 struct transform *transform;
235 EnterCriticalSection( &mscms_handle_cs );
237 index = (DWORD_PTR)handle - 1;
238 if (index > num_transform_handles)
240 LeaveCriticalSection( &mscms_handle_cs );
241 return FALSE;
243 transform = &transformtable[index];
245 cmsDeleteTransform( transform->cmstransform );
246 memset( transform, 0, sizeof(struct transform) );
248 LeaveCriticalSection( &mscms_handle_cs );
249 return TRUE;
252 #endif /* HAVE_LCMS2 */