xref: /reactos/dll/win32/mscms/icc.c (revision cc439606)
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "config.h"
22 
23 #include <stdarg.h>
24 
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "wine/winternl.h"
30 #include "icm.h"
31 
32 #include "mscms_priv.h"
33 
34 #ifdef HAVE_LCMS2
35 
36 static inline void adjust_endianness32( ULONG *ptr )
37 {
38 #ifndef WORDS_BIGENDIAN
39     *ptr = RtlUlongByteSwap(*ptr);
40 #endif
41 }
42 
43 void get_profile_header( const struct profile *profile, PROFILEHEADER *header )
44 {
45     unsigned int i;
46 
47     memcpy( header, profile->data, sizeof(PROFILEHEADER) );
48 
49     /* ICC format is big-endian, swap bytes if necessary */
50     for (i = 0; i < sizeof(PROFILEHEADER) / sizeof(ULONG); i++)
51         adjust_endianness32( (ULONG *)header + i );
52 }
53 
54 void set_profile_header( const struct profile *profile, const PROFILEHEADER *header )
55 {
56     unsigned int i;
57 
58     memcpy( profile->data, header, sizeof(PROFILEHEADER) );
59 
60     /* ICC format is big-endian, swap bytes if necessary */
61     for (i = 0; i < sizeof(PROFILEHEADER) / sizeof(ULONG); i++)
62         adjust_endianness32( (ULONG *)profile->data + i );
63 }
64 
65 static BOOL get_adjusted_tag( const struct profile *profile, TAGTYPE type, cmsTagEntry *tag )
66 {
67     DWORD i, num_tags = *(DWORD *)(profile->data + sizeof(cmsICCHeader));
68     cmsTagEntry *entry;
69     ULONG sig;
70 
71     adjust_endianness32( &num_tags );
72     for (i = 0; i < num_tags; i++)
73     {
74         entry = (cmsTagEntry *)(profile->data + sizeof(cmsICCHeader) + sizeof(DWORD) + i * sizeof(*tag));
75         sig = entry->sig;
76         adjust_endianness32( &sig );
77         if (sig == type)
78         {
79             tag->sig    = sig;
80             tag->offset = entry->offset;
81             tag->size   = entry->size;
82             adjust_endianness32( &tag->offset );
83             adjust_endianness32( &tag->size );
84             return TRUE;
85         }
86     }
87     return FALSE;
88 }
89 
90 BOOL get_tag_data( const struct profile *profile, TAGTYPE type, DWORD offset, void *buffer, DWORD *len )
91 {
92     cmsTagEntry tag;
93 
94     if (!get_adjusted_tag( profile, type, &tag )) return FALSE;
95 
96     if (!buffer) offset = 0;
97     if (offset > tag.size) return FALSE;
98     if (*len < tag.size - offset || !buffer)
99     {
100         *len = tag.size - offset;
101         return FALSE;
102     }
103     memcpy( buffer, profile->data + tag.offset + offset, tag.size - offset );
104     *len = tag.size - offset;
105     return TRUE;
106 }
107 
108 BOOL set_tag_data( const struct profile *profile, TAGTYPE type, DWORD offset, const void *buffer, DWORD *len )
109 {
110     cmsTagEntry tag;
111 
112     if (!get_adjusted_tag( profile, type, &tag )) return FALSE;
113 
114     if (offset > tag.size) return FALSE;
115     *len = min( tag.size - offset, *len );
116     memcpy( profile->data + tag.offset + offset, buffer, *len );
117     return TRUE;
118 }
119 
120 #endif /* HAVE_LCMS2 */
121