3 // Copyright (C) 1998-2007 Marti Maria
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the Software
10 // is furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
17 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 // Named color support
31 LPcmsNAMEDCOLORLIST
GrowNamedColorList(LPcmsNAMEDCOLORLIST v
, int ByElements
)
33 if (ByElements
> v
->Allocated
) {
35 LPcmsNAMEDCOLORLIST TheNewList
;
39 if (v
->Allocated
== 0)
40 NewElements
= 64; // Initial guess
42 NewElements
= v
->Allocated
;
44 while (ByElements
> NewElements
)
47 size
= sizeof(cmsNAMEDCOLORLIST
) + (sizeof(cmsNAMEDCOLOR
) * NewElements
);
48 TheNewList
= (LPcmsNAMEDCOLORLIST
) _cmsMalloc(size
);
51 if (TheNewList
== NULL
) {
52 cmsSignalError(LCMS_ERRC_ABORTED
, "Out of memory reallocating named color list");
56 ZeroMemory(TheNewList
, size
);
57 CopyMemory(TheNewList
, v
, sizeof(cmsNAMEDCOLORLIST
) + (v
->nColors
- 1) * sizeof(cmsNAMEDCOLOR
));
58 TheNewList
-> Allocated
= NewElements
;
69 LPcmsNAMEDCOLORLIST
cmsAllocNamedColorList(int n
)
71 size_t size
= sizeof(cmsNAMEDCOLORLIST
) + (n
- 1) * sizeof(cmsNAMEDCOLOR
);
73 LPcmsNAMEDCOLORLIST v
= (LPcmsNAMEDCOLORLIST
) _cmsMalloc(size
);
77 cmsSignalError(LCMS_ERRC_ABORTED
, "Out of memory creating named color list");
91 void cmsFreeNamedColorList(LPcmsNAMEDCOLORLIST v
)
94 cmsSignalError(LCMS_ERRC_RECOVERABLE
, "Couldn't free a NULL named color list");
101 LCMSBOOL
cmsAppendNamedColor(cmsHTRANSFORM xform
, const char* Name
, WORD PCS
[3], WORD Colorant
[MAXCHANNELS
])
103 _LPcmsTRANSFORM v
= (_LPcmsTRANSFORM
) xform
;
104 LPcmsNAMEDCOLORLIST List
;
107 if (v
->NamedColorList
== NULL
) return FALSE
;
109 v
->NamedColorList
= GrowNamedColorList(v
->NamedColorList
, v
->NamedColorList
->nColors
+ 1);
111 List
= v
->NamedColorList
;
113 for (i
=0; i
< MAXCHANNELS
; i
++)
114 List
->List
[List
->nColors
].DeviceColorant
[i
] = Colorant
[i
];
116 for (i
=0; i
< 3; i
++)
117 List
->List
[List
->nColors
].PCS
[i
] = PCS
[i
];
119 strncpy(List
->List
[List
->nColors
].Name
, Name
, MAX_PATH
-1);
127 // Returns named color count
129 int LCMSEXPORT
cmsNamedColorCount(cmsHTRANSFORM xform
)
131 _LPcmsTRANSFORM v
= (_LPcmsTRANSFORM
) xform
;
133 if (v
->NamedColorList
== NULL
) return 0;
134 return v
->NamedColorList
->nColors
;
138 LCMSBOOL LCMSEXPORT
cmsNamedColorInfo(cmsHTRANSFORM xform
, int nColor
, char* Name
, char* Prefix
, char* Suffix
)
140 _LPcmsTRANSFORM v
= (_LPcmsTRANSFORM
) xform
;
143 if (v
->NamedColorList
== NULL
) return FALSE
;
145 if (nColor
< 0 || nColor
>= cmsNamedColorCount(xform
)) return FALSE
;
147 if (Name
) strncpy(Name
, v
->NamedColorList
->List
[nColor
].Name
, 31);
148 if (Prefix
) strncpy(Prefix
, v
->NamedColorList
->Prefix
, 31);
149 if (Suffix
) strncpy(Suffix
, v
->NamedColorList
->Suffix
, 31);
155 int LCMSEXPORT
cmsNamedColorIndex(cmsHTRANSFORM xform
, const char* Name
)
157 _LPcmsTRANSFORM v
= (_LPcmsTRANSFORM
) xform
;
160 if (v
->NamedColorList
== NULL
) return -1;
162 n
= cmsNamedColorCount(xform
);
163 for (i
=0; i
< n
; i
++) {
164 if (stricmp(Name
, v
->NamedColorList
->List
[i
].Name
) == 0)