Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / lcms / src / cmsnamed.c
blobe500a0f68d7146f1e3aa598c90d337df4813c8e8
1 //
2 // Little cms
3 // Copyright (C) 1998-2007 Marti Maria
4 //
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
26 #include "lcms.h"
30 static
31 LPcmsNAMEDCOLORLIST GrowNamedColorList(LPcmsNAMEDCOLORLIST v, int ByElements)
33 if (ByElements > v ->Allocated) {
35 LPcmsNAMEDCOLORLIST TheNewList;
36 int NewElements;
37 size_t size;
39 if (v ->Allocated == 0)
40 NewElements = 64; // Initial guess
41 else
42 NewElements = v ->Allocated;
44 while (ByElements > NewElements)
45 NewElements *= 2;
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");
53 return NULL;
55 else {
56 ZeroMemory(TheNewList, size);
57 CopyMemory(TheNewList, v, sizeof(cmsNAMEDCOLORLIST) + (v ->nColors - 1) * sizeof(cmsNAMEDCOLOR));
58 TheNewList -> Allocated = NewElements;
60 _cmsFree(v);
61 return TheNewList;
65 return v;
69 LPcmsNAMEDCOLORLIST cmsAllocNamedColorList(int n)
71 size_t size = sizeof(cmsNAMEDCOLORLIST) + (n - 1) * sizeof(cmsNAMEDCOLOR);
73 LPcmsNAMEDCOLORLIST v = (LPcmsNAMEDCOLORLIST) _cmsMalloc(size);
76 if (v == NULL) {
77 cmsSignalError(LCMS_ERRC_ABORTED, "Out of memory creating named color list");
78 return NULL;
81 ZeroMemory(v, size);
83 v ->nColors = n;
84 v ->Allocated = n;
85 v ->Prefix[0] = 0;
86 v ->Suffix[0] = 0;
88 return v;
91 void cmsFreeNamedColorList(LPcmsNAMEDCOLORLIST v)
93 if (v == NULL) {
94 cmsSignalError(LCMS_ERRC_RECOVERABLE, "Couldn't free a NULL named color list");
95 return;
98 _cmsFree(v);
101 LCMSBOOL cmsAppendNamedColor(cmsHTRANSFORM xform, const char* Name, WORD PCS[3], WORD Colorant[MAXCHANNELS])
103 _LPcmsTRANSFORM v = (_LPcmsTRANSFORM) xform;
104 LPcmsNAMEDCOLORLIST List;
105 int i;
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);
121 List ->nColors++;
122 return TRUE;
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);
151 return TRUE;
155 int LCMSEXPORT cmsNamedColorIndex(cmsHTRANSFORM xform, const char* Name)
157 _LPcmsTRANSFORM v = (_LPcmsTRANSFORM) xform;
158 int i, n;
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)
165 return i;
168 return -1;