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
22 #include "wine/debug.h"
32 #include "mscms_priv.h"
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 );
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 )
69 EnterCriticalSection( &MSCMS_handle_cs );
71 index = (DWORD_PTR)handle - 1;
72 if (index > num_profile_handles)
74 LeaveCriticalSection( &MSCMS_handle_cs );
77 return &profiletable[index];
80 void release_profile( struct profile *profile )
82 LeaveCriticalSection( &MSCMS_handle_cs );
85 struct transform *grab_transform( HTRANSFORM handle )
89 EnterCriticalSection( &MSCMS_handle_cs );
91 index = (DWORD_PTR)handle - 1;
92 if (index > num_transform_handles)
94 LeaveCriticalSection( &MSCMS_handle_cs );
97 return &transformtable[index];
100 void release_transform( struct transform *transform )
102 LeaveCriticalSection( &MSCMS_handle_cs );
105 static HPROFILE alloc_profile_handle( void )
109 unsigned int count = 128;
111 for (index = 0; index < num_profile_handles; index++)
113 if (!profiletable[index].iccprofile) return (HPROFILE)(index + 1);
117 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(struct profile) );
121 count = num_profile_handles * 2;
122 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, profiletable, count * sizeof(struct profile) );
127 num_profile_handles = count;
129 return (HPROFILE)(index + 1);
132 HPROFILE create_profile( struct profile *profile )
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 );
147 BOOL close_profile( HPROFILE handle )
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 );
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 ) ||
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 );
186 static HTRANSFORM alloc_transform_handle( void )
190 unsigned int count = 128;
192 for (index = 0; index < num_transform_handles; index++)
194 if (!transformtable[index].cmstransform) return (HTRANSFORM)(index + 1);
198 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(struct transform) );
202 count = num_transform_handles * 2;
203 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, transformtable, count * sizeof(struct transform) );
208 num_transform_handles = count;
210 return (HTRANSFORM)(index + 1);
213 HTRANSFORM create_transform( struct transform *transform )
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 );
228 BOOL close_transform( HTRANSFORM handle )
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 );
241 transform = &transformtable[index];
243 cmsDeleteTransform( transform->cmstransform );
244 memset( transform, 0, sizeof(struct transform) );
246 LeaveCriticalSection( &MSCMS_handle_cs );
250 #endif /* HAVE_LCMS */