File: cmsnamed.c

package info (click to toggle)
foo2zjs 20050217-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,364 kB
  • ctags: 2,122
  • sloc: ansic: 15,074; xml: 1,751; makefile: 503; sh: 269; perl: 102
file content (181 lines) | stat: -rwxr-xr-x 4,771 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
//  Little cms
//  Copyright (C) 1998-2002 Marti Maria
//
// THIS SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL MARTI MARIA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
// INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
// LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
// OF THIS SOFTWARE.
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

// Named color support

#include "lcms.h"


LPcmsNAMEDCOLORLIST  cdecl cmsAllocNamedColorList(void);
void				 cdecl cmsFreeNamedColorList(LPcmsNAMEDCOLORLIST List);
BOOL				 cdecl cmsAppendNamedColor(cmsHTRANSFORM xform, const char* Name, WORD PCS[3], WORD Colorant[MAXCHANNELS]);


// ---------------------------------------------------------------------------------

static
BOOL GrowNamedColorList(LPcmsNAMEDCOLORLIST v, int ByElements)
{
	LPcmsNAMEDCOLOR NewList;
	int NewElements;
	
	if (ByElements > v ->Allocated) {

		if (v ->Allocated == 0)
			NewElements = 64;	// Initial guess
		else
			NewElements = v ->Allocated;

		while (ByElements > NewElements)
				NewElements *= 2;
		
		NewList = (LPcmsNAMEDCOLOR) malloc(sizeof(cmsNAMEDCOLOR) * NewElements);

		if (NewList == NULL) {
			cmsSignalError(LCMS_ERRC_ABORTED, "Out of memory reallocating named color list");
			return FALSE;
		}
		else {

			if (v -> List) {
				CopyMemory(NewList, v -> List, v ->nColors* sizeof(cmsNAMEDCOLOR));
				free(v -> List);				
			}

			v -> List = NewList;
			v -> Allocated = NewElements;
			return TRUE;
		}
	}

	return TRUE;
}


LPcmsNAMEDCOLORLIST cmsAllocNamedColorList(void)
{
	LPcmsNAMEDCOLORLIST v = (LPcmsNAMEDCOLORLIST) malloc(sizeof(cmsNAMEDCOLORLIST));

	if (v == NULL) {
		cmsSignalError(LCMS_ERRC_ABORTED, "Out of memory creating named color list");
		return NULL;
	}

	ZeroMemory(v, sizeof(cmsNAMEDCOLORLIST));

	v ->nColors   = 0;
	v ->Allocated = 0;	
	v ->Prefix[0] = 0;
	v ->Suffix[0] = 0;	
	v -> List	  = NULL; 
	
	
	return v;
}

void cmsFreeNamedColorList(LPcmsNAMEDCOLORLIST v)
{
	if (v == NULL) {
		cmsSignalError(LCMS_ERRC_RECOVERABLE, "Couldn't free a NULL named color list");
		return;
	}
				
	if (v -> List) free(v->List);
	free(v);
}	




BOOL cmsAppendNamedColor(cmsHTRANSFORM xform, const char* Name, WORD PCS[3], WORD Colorant[MAXCHANNELS])
{
	_LPcmsTRANSFORM v = (_LPcmsTRANSFORM) xform;
	LPcmsNAMEDCOLORLIST List = v ->NamedColorList;
	int i;

	if (List == NULL) return FALSE;
	if (!GrowNamedColorList(List, List ->nColors + 1)) return FALSE;
	
	for (i=0; i < MAXCHANNELS; i++)
		List ->List[List ->nColors].DeviceColorant[i] = Colorant[i];

	for (i=0; i < 3; i++)
		List ->List[List ->nColors].PCS[i] = PCS[i];

	strncpy(List ->List[List ->nColors].Name, Name, MAX_PATH-1);

	List ->nColors++;
	return TRUE;
}


// Returns named color count 

int LCMSEXPORT cmsNamedColorCount(cmsHTRANSFORM xform)
{
	 _LPcmsTRANSFORM v = (_LPcmsTRANSFORM) xform;

	 if (v ->NamedColorList == NULL) return 0;
	 return v ->NamedColorList ->nColors;
}


BOOL LCMSEXPORT cmsNamedColorInfo(cmsHTRANSFORM xform, int nColor, char* Name, char* Prefix, char* Suffix)
{
	_LPcmsTRANSFORM v = (_LPcmsTRANSFORM) xform;


	 if (v ->NamedColorList == NULL) return FALSE;

	 if (nColor < 0 || nColor >= cmsNamedColorCount(xform)) return FALSE;

	 if (Name) strncpy(Name, v ->NamedColorList->List[nColor].Name, 31);
	 if (Prefix) strncpy(Name, v ->NamedColorList->Prefix, 31);
	 if (Suffix) strncpy(Name, v ->NamedColorList->Suffix, 31);

	 return TRUE;
}


int  LCMSEXPORT cmsNamedColorIndex(cmsHTRANSFORM xform, const char* Name)
{
	_LPcmsTRANSFORM v = (_LPcmsTRANSFORM) xform;	
	int i, n;

		 if (v ->NamedColorList == NULL) return -1;

		n = cmsNamedColorCount(xform);
		for (i=0; i < n; i++) {
			if (stricmp(Name,  v ->NamedColorList->List[i].Name) == 0)
					return i;
		}

		return -1;
}