quartz: Free two assert calls from having side effects.
[wine/testsucceed.git] / dlls / mscms / handle.c
blob84f1d8ba30f070e3af7d711c05050dc5501b99cc
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_LCMS
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;
65 struct profile *grab_profile( HPROFILE handle )
67 DWORD_PTR index;
69 EnterCriticalSection( &MSCMS_handle_cs );
71 index = (DWORD_PTR)handle - 1;
72 if (index > num_profile_handles)
74 LeaveCriticalSection( &MSCMS_handle_cs );
75 return NULL;
77 return &profiletable[index];
80 void release_profile( struct profile *profile )
82 LeaveCriticalSection( &MSCMS_handle_cs );
85 struct transform *grab_transform( HTRANSFORM handle )
87 DWORD_PTR index;
89 EnterCriticalSection( &MSCMS_handle_cs );
91 index = (DWORD_PTR)handle - 1;
92 if (index > num_transform_handles)
94 LeaveCriticalSection( &MSCMS_handle_cs );
95 return NULL;
97 return &transformtable[index];
100 void release_transform( struct transform *transform )
102 LeaveCriticalSection( &MSCMS_handle_cs );
105 static HPROFILE alloc_profile_handle( void )
107 DWORD_PTR index;
108 struct profile *p;
109 unsigned int count = 128;
111 for (index = 0; index < num_profile_handles; index++)
113 if (!profiletable[index].iccprofile) return (HPROFILE)(index + 1);
115 if (!profiletable)
117 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(struct profile) );
119 else
121 count = num_profile_handles * 2;
122 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, profiletable, count * sizeof(struct profile) );
124 if (!p) return NULL;
126 profiletable = p;
127 num_profile_handles = count;
129 return (HPROFILE)(index + 1);
132 HPROFILE create_profile( struct profile *profile )
134 HPROFILE handle;
136 EnterCriticalSection( &MSCMS_handle_cs );
138 if ((handle = alloc_profile_handle()))
140 DWORD_PTR index = (DWORD_PTR)handle - 1;
141 memcpy( &profiletable[index], profile, sizeof(struct profile) );
143 LeaveCriticalSection( &MSCMS_handle_cs );
144 return handle;
147 BOOL close_profile( HPROFILE handle )
149 DWORD_PTR index;
150 struct profile *profile;
152 EnterCriticalSection( &MSCMS_handle_cs );
154 index = (DWORD_PTR)handle - 1;
155 if (index > num_profile_handles)
157 LeaveCriticalSection( &MSCMS_handle_cs );
158 return FALSE;
160 profile = &profiletable[index];
162 if (profile->file != INVALID_HANDLE_VALUE)
164 if (profile->access & PROFILE_READWRITE)
166 DWORD written, size = MSCMS_get_profile_size( profile->iccprofile );
168 if (SetFilePointer( profile->file, 0, NULL, FILE_BEGIN ) ||
169 !WriteFile( profile->file, profile->iccprofile, size, &written, NULL ) ||
170 written != size)
172 ERR( "Unable to write color profile\n" );
175 CloseHandle( profile->file );
177 cmsCloseProfile( profile->cmsprofile );
178 HeapFree( GetProcessHeap(), 0, profile->iccprofile );
180 memset( profile, 0, sizeof(struct profile) );
182 LeaveCriticalSection( &MSCMS_handle_cs );
183 return TRUE;
186 static HTRANSFORM alloc_transform_handle( void )
188 DWORD_PTR index;
189 struct transform *p;
190 unsigned int count = 128;
192 for (index = 0; index < num_transform_handles; index++)
194 if (!transformtable[index].cmstransform) return (HTRANSFORM)(index + 1);
196 if (!transformtable)
198 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(struct transform) );
200 else
202 count = num_transform_handles * 2;
203 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, transformtable, count * sizeof(struct transform) );
205 if (!p) return NULL;
207 transformtable = p;
208 num_transform_handles = count;
210 return (HTRANSFORM)(index + 1);
213 HTRANSFORM create_transform( struct transform *transform )
215 HTRANSFORM handle;
217 EnterCriticalSection( &MSCMS_handle_cs );
219 if ((handle = alloc_transform_handle()))
221 DWORD_PTR index = (DWORD_PTR)handle - 1;
222 memcpy( &transformtable[index], transform, sizeof(struct transform) );
224 LeaveCriticalSection( &MSCMS_handle_cs );
225 return handle;
228 BOOL close_transform( HTRANSFORM handle )
230 DWORD_PTR index;
231 struct transform *transform;
233 EnterCriticalSection( &MSCMS_handle_cs );
235 index = (DWORD_PTR)handle - 1;
236 if (index > num_transform_handles)
238 LeaveCriticalSection( &MSCMS_handle_cs );
239 return FALSE;
241 transform = &transformtable[index];
243 cmsDeleteTransform( transform->cmstransform );
244 memset( transform, 0, sizeof(struct transform) );
246 LeaveCriticalSection( &MSCMS_handle_cs );
247 return TRUE;
250 #endif /* HAVE_LCMS */