xref: /reactos/dll/win32/gdiplus/imageattributes.c (revision 8e659192)
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include "gdiplus_private.h"
20 
21 GpStatus WINGDIPAPI GdipCloneImageAttributes(GDIPCONST GpImageAttributes *imageattr,
22     GpImageAttributes **cloneImageattr)
23 {
24     GpStatus stat = Ok;
25     struct color_remap_table remap_tables[ColorAdjustTypeCount] = {{0}};
26     int i;
27 
28     TRACE("(%p, %p)\n", imageattr, cloneImageattr);
29 
30     if(!imageattr || !cloneImageattr)
31         return InvalidParameter;
32 
33     for (i=0; i<ColorAdjustTypeCount; i++)
34     {
35         if (imageattr->colorremaptables[i].enabled)
36         {
37             remap_tables[i].enabled = TRUE;
38             remap_tables[i].mapsize = imageattr->colorremaptables[i].mapsize;
39             remap_tables[i].colormap = heap_alloc(sizeof(ColorMap) * remap_tables[i].mapsize);
40 
41             if (remap_tables[i].colormap)
42             {
43                 memcpy(remap_tables[i].colormap, imageattr->colorremaptables[i].colormap,
44                     sizeof(ColorMap) * remap_tables[i].mapsize);
45             }
46             else
47             {
48                 stat = OutOfMemory;
49                 break;
50             }
51         }
52     }
53 
54     if (stat == Ok)
55         stat = GdipCreateImageAttributes(cloneImageattr);
56 
57     if (stat == Ok)
58     {
59         **cloneImageattr = *imageattr;
60 
61         memcpy((*cloneImageattr)->colorremaptables, remap_tables, sizeof(remap_tables));
62     }
63 
64     if (stat != Ok)
65     {
66         for (i=0; i<ColorAdjustTypeCount; i++)
67             heap_free(remap_tables[i].colormap);
68     }
69 
70     return stat;
71 }
72 
73 GpStatus WINGDIPAPI GdipCreateImageAttributes(GpImageAttributes **imageattr)
74 {
75     if(!imageattr)
76         return InvalidParameter;
77 
78     *imageattr = heap_alloc_zero(sizeof(GpImageAttributes));
79     if(!*imageattr)    return OutOfMemory;
80 
81     (*imageattr)->wrap = WrapModeClamp;
82 
83     TRACE("<-- %p\n", *imageattr);
84 
85     return Ok;
86 }
87 
88 GpStatus WINGDIPAPI GdipDisposeImageAttributes(GpImageAttributes *imageattr)
89 {
90     int i;
91 
92     TRACE("(%p)\n", imageattr);
93 
94     if(!imageattr)
95         return InvalidParameter;
96 
97     for (i=0; i<ColorAdjustTypeCount; i++)
98         heap_free(imageattr->colorremaptables[i].colormap);
99 
100     heap_free(imageattr);
101 
102     return Ok;
103 }
104 
105 GpStatus WINGDIPAPI GdipGetImageAttributesAdjustedPalette(GpImageAttributes *imageattr,
106     ColorPalette *palette, ColorAdjustType type)
107 {
108     TRACE("(%p,%p,%u)\n", imageattr, palette, type);
109 
110     if (!imageattr || !palette || !palette->Count ||
111         type >= ColorAdjustTypeCount || type == ColorAdjustTypeDefault)
112         return InvalidParameter;
113 
114     apply_image_attributes(imageattr, (LPBYTE)palette->Entries, palette->Count, 1, 0,
115         type, PixelFormat32bppARGB);
116 
117     return Ok;
118 }
119 
120 GpStatus WINGDIPAPI GdipSetImageAttributesColorKeys(GpImageAttributes *imageattr,
121     ColorAdjustType type, BOOL enableFlag, ARGB colorLow, ARGB colorHigh)
122 {
123     TRACE("(%p,%u,%i,%08x,%08x)\n", imageattr, type, enableFlag, colorLow, colorHigh);
124 
125     if(!imageattr || type >= ColorAdjustTypeCount)
126         return InvalidParameter;
127 
128     imageattr->colorkeys[type].enabled = enableFlag;
129     imageattr->colorkeys[type].low = colorLow;
130     imageattr->colorkeys[type].high = colorHigh;
131 
132     return Ok;
133 }
134 
135 GpStatus WINGDIPAPI GdipSetImageAttributesColorMatrix(GpImageAttributes *imageattr,
136     ColorAdjustType type, BOOL enableFlag, GDIPCONST ColorMatrix* colorMatrix,
137     GDIPCONST ColorMatrix* grayMatrix, ColorMatrixFlags flags)
138 {
139     TRACE("(%p,%u,%i,%p,%p,%u)\n", imageattr, type, enableFlag, colorMatrix,
140         grayMatrix, flags);
141 
142     if(!imageattr || type >= ColorAdjustTypeCount || flags > ColorMatrixFlagsAltGray)
143         return InvalidParameter;
144 
145     if (enableFlag)
146     {
147         if (!colorMatrix)
148             return InvalidParameter;
149 
150         if (flags == ColorMatrixFlagsAltGray)
151         {
152             if (!grayMatrix)
153                 return InvalidParameter;
154 
155             imageattr->colormatrices[type].graymatrix = *grayMatrix;
156         }
157 
158         imageattr->colormatrices[type].colormatrix = *colorMatrix;
159         imageattr->colormatrices[type].flags = flags;
160     }
161 
162     imageattr->colormatrices[type].enabled = enableFlag;
163 
164     return Ok;
165 }
166 
167 GpStatus WINGDIPAPI GdipSetImageAttributesWrapMode(GpImageAttributes *imageAttr,
168     WrapMode wrap, ARGB argb, BOOL clamp)
169 {
170     TRACE("(%p,%u,%08x,%i)\n", imageAttr, wrap, argb, clamp);
171 
172     if(!imageAttr || wrap > WrapModeClamp)
173         return InvalidParameter;
174 
175     imageAttr->wrap = wrap;
176     imageAttr->outside_color = argb;
177     imageAttr->clamp = clamp;
178 
179     return Ok;
180 }
181 
182 GpStatus WINGDIPAPI GdipSetImageAttributesCachedBackground(GpImageAttributes *imageAttr,
183     BOOL enableFlag)
184 {
185     static int calls;
186 
187     TRACE("(%p,%i)\n", imageAttr, enableFlag);
188 
189     if(!(calls++))
190         FIXME("not implemented\n");
191 
192     return NotImplemented;
193 }
194 
195 GpStatus WINGDIPAPI GdipSetImageAttributesGamma(GpImageAttributes *imageAttr,
196     ColorAdjustType type, BOOL enableFlag, REAL gamma)
197 {
198     TRACE("(%p,%u,%i,%0.2f)\n", imageAttr, type, enableFlag, gamma);
199 
200     if (!imageAttr || (enableFlag && gamma <= 0.0) || type >= ColorAdjustTypeCount)
201         return InvalidParameter;
202 
203     imageAttr->gamma_enabled[type] = enableFlag;
204     imageAttr->gamma[type] = gamma;
205 
206     return Ok;
207 }
208 
209 GpStatus WINGDIPAPI GdipSetImageAttributesNoOp(GpImageAttributes *imageAttr,
210     ColorAdjustType type, BOOL enableFlag)
211 {
212     TRACE("(%p,%u,%i)\n", imageAttr, type, enableFlag);
213 
214     if (type >= ColorAdjustTypeCount)
215         return InvalidParameter;
216 
217     imageAttr->noop[type] = enableFlag ? IMAGEATTR_NOOP_SET : IMAGEATTR_NOOP_CLEAR;
218 
219     return Ok;
220 }
221 
222 GpStatus WINGDIPAPI GdipSetImageAttributesOutputChannel(GpImageAttributes *imageAttr,
223     ColorAdjustType type, BOOL enableFlag, ColorChannelFlags channelFlags)
224 {
225     static int calls;
226 
227     TRACE("(%p,%u,%i,%x)\n", imageAttr, type, enableFlag, channelFlags);
228 
229     if(!(calls++))
230         FIXME("not implemented\n");
231 
232     return NotImplemented;
233 }
234 
235 GpStatus WINGDIPAPI GdipSetImageAttributesOutputChannelColorProfile(GpImageAttributes *imageAttr,
236     ColorAdjustType type, BOOL enableFlag,
237     GDIPCONST WCHAR *colorProfileFilename)
238 {
239     static int calls;
240 
241     TRACE("(%p,%u,%i,%s)\n", imageAttr, type, enableFlag, debugstr_w(colorProfileFilename));
242 
243     if(!(calls++))
244         FIXME("not implemented\n");
245 
246     return NotImplemented;
247 }
248 
249 GpStatus WINGDIPAPI GdipSetImageAttributesRemapTable(GpImageAttributes *imageAttr,
250     ColorAdjustType type, BOOL enableFlag, UINT mapSize,
251     GDIPCONST ColorMap *map)
252 {
253     ColorMap *new_map;
254 
255     TRACE("(%p,%u,%i,%u,%p)\n", imageAttr, type, enableFlag, mapSize, map);
256 
257     if(!imageAttr || type >= ColorAdjustTypeCount)
258 	return InvalidParameter;
259 
260     if (enableFlag)
261     {
262         if(!map || !mapSize)
263 	    return InvalidParameter;
264 
265         new_map = heap_alloc_zero(sizeof(*map) * mapSize);
266 
267         if (!new_map)
268             return OutOfMemory;
269 
270         memcpy(new_map, map, sizeof(*map) * mapSize);
271 
272         heap_free(imageAttr->colorremaptables[type].colormap);
273 
274         imageAttr->colorremaptables[type].mapsize = mapSize;
275         imageAttr->colorremaptables[type].colormap = new_map;
276     }
277     else
278     {
279         heap_free(imageAttr->colorremaptables[type].colormap);
280         imageAttr->colorremaptables[type].colormap = NULL;
281     }
282 
283     imageAttr->colorremaptables[type].enabled = enableFlag;
284 
285     return Ok;
286 }
287 
288 GpStatus WINGDIPAPI GdipSetImageAttributesThreshold(GpImageAttributes *imageAttr,
289     ColorAdjustType type, BOOL enableFlag, REAL threshold)
290 {
291     static int calls;
292 
293     TRACE("(%p,%u,%i,%0.2f)\n", imageAttr, type, enableFlag, threshold);
294 
295     if(!(calls++))
296         FIXME("not implemented\n");
297 
298     return NotImplemented;
299 }
300 
301 GpStatus WINGDIPAPI GdipSetImageAttributesToIdentity(GpImageAttributes *imageAttr,
302     ColorAdjustType type)
303 {
304     static int calls;
305 
306     TRACE("(%p,%u)\n", imageAttr, type);
307 
308     if(!(calls++))
309         FIXME("not implemented\n");
310 
311     return NotImplemented;
312 }
313 
314 GpStatus WINGDIPAPI GdipResetImageAttributes(GpImageAttributes *imageAttr,
315     ColorAdjustType type)
316 {
317     TRACE("(%p,%u)\n", imageAttr, type);
318 
319     if(!imageAttr || type >= ColorAdjustTypeCount)
320         return InvalidParameter;
321 
322     memset(&imageAttr->colormatrices[type], 0, sizeof(imageAttr->colormatrices[type]));
323     GdipSetImageAttributesColorKeys(imageAttr, type, FALSE, 0, 0);
324     GdipSetImageAttributesRemapTable(imageAttr, type, FALSE, 0, NULL);
325     GdipSetImageAttributesGamma(imageAttr, type, FALSE, 0.0);
326     imageAttr->noop[type] = IMAGEATTR_NOOP_UNDEFINED;
327 
328     return Ok;
329 }
330