1 /*
2  * GdiPlusImageAttributes.h
3  *
4  * Windows GDI+
5  *
6  * This file is part of the w32api package.
7  *
8  * THIS SOFTWARE IS NOT COPYRIGHTED
9  *
10  * This source code is offered for use in the public domain. You may
11  * use, modify or distribute it freely.
12  *
13  * This code is distributed in the hope that it will be useful but
14  * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
15  * DISCLAIMED. This includes but is not limited to warranties of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  */
18 
19 #ifndef _GDIPLUSIMAGEATTRIBUTES_H
20 #define _GDIPLUSIMAGEATTRIBUTES_H
21 
22 class ImageAttributes : public GdiplusBase
23 {
24   public:
25     friend class TextureBrush;
26 
27     ImageAttributes() : nativeImageAttr(NULL)
28     {
29         lastStatus = DllExports::GdipCreateImageAttributes(&nativeImageAttr);
30     }
31 
32     ~ImageAttributes()
33     {
34         DllExports::GdipDisposeImageAttributes(nativeImageAttr);
35     }
36 
37     Status
38     ClearBrushRemapTable()
39     {
40         return ClearRemapTable(ColorAdjustTypeBrush);
41     }
42 
43     Status
44     ClearColorKey(ColorAdjustType type = ColorAdjustTypeDefault)
45     {
46         return SetStatus(DllExports::GdipSetImageAttributesColorKeys(nativeImageAttr, type, FALSE, NULL, NULL));
47     }
48 
49     Status
50     ClearColorMatrices(ColorAdjustType type = ColorAdjustTypeDefault)
51     {
52         return SetStatus(DllExports::GdipSetImageAttributesColorMatrix(
53             nativeImageAttr, type, FALSE, NULL, NULL, ColorMatrixFlagsDefault));
54     }
55 
56     Status
57     ClearColorMatrix(ColorAdjustType type = ColorAdjustTypeDefault)
58     {
59         return SetStatus(DllExports::GdipSetImageAttributesColorMatrix(
60             nativeImageAttr, type, FALSE, NULL, NULL, ColorMatrixFlagsDefault));
61     }
62 
63     Status
64     ClearGamma(ColorAdjustType type = ColorAdjustTypeDefault)
65     {
66         return SetStatus(DllExports::GdipSetImageAttributesGamma(nativeImageAttr, type, FALSE, 0.0));
67     }
68 
69     Status
70     ClearNoOp(ColorAdjustType type = ColorAdjustTypeDefault)
71     {
72         return SetStatus(DllExports::GdipSetImageAttributesNoOp(nativeImageAttr, type, FALSE));
73     }
74 
75     Status
76     ClearOutputChannel(ColorAdjustType type = ColorAdjustTypeDefault)
77     {
78         return SetStatus(
79             DllExports::GdipSetImageAttributesOutputChannel(nativeImageAttr, type, FALSE, ColorChannelFlagsLast));
80     }
81 
82     Status
83     ClearOutputChannelColorProfile(ColorAdjustType type = ColorAdjustTypeDefault)
84     {
85         return SetStatus(
86             DllExports::GdipSetImageAttributesOutputChannelColorProfile(nativeImageAttr, type, FALSE, NULL));
87     }
88 
89     Status
90     ClearRemapTable(ColorAdjustType type = ColorAdjustTypeDefault)
91     {
92         return SetStatus(DllExports::GdipSetImageAttributesRemapTable(nativeImageAttr, type, FALSE, 0, NULL));
93     }
94 
95     Status
96     ClearThreshold(ColorAdjustType type = ColorAdjustTypeDefault)
97     {
98         return SetStatus(DllExports::GdipSetImageAttributesThreshold(nativeImageAttr, type, FALSE, 0.0));
99     }
100 
101     ImageAttributes *
102     Clone()
103     {
104         GpImageAttributes *clone = NULL;
105         SetStatus(DllExports::GdipCloneImageAttributes(nativeImageAttr, &clone));
106         if (lastStatus != Ok)
107             return NULL;
108 
109         ImageAttributes *newImageAttr = new ImageAttributes(clone, lastStatus);
110         if (newImageAttr == NULL)
111             SetStatus(DllExports::GdipDisposeImageAttributes(clone));
112 
113         return newImageAttr;
114     }
115 
116     Status
117     GetAdjustedPalette(ColorPalette *colorPalette, ColorAdjustType colorAdjustType)
118     {
119         return SetStatus(
120             DllExports::GdipGetImageAttributesAdjustedPalette(nativeImageAttr, colorPalette, colorAdjustType));
121     }
122 
123     Status
124     GetLastStatus()
125     {
126         return lastStatus;
127     }
128 
129     Status
130     Reset(ColorAdjustType type = ColorAdjustTypeDefault)
131     {
132         return SetStatus(DllExports::GdipResetImageAttributes(nativeImageAttr, type));
133     }
134 
135     Status
136     SetBrushRemapTable(UINT mapSize, ColorMap *map)
137     {
138         return SetRemapTable(mapSize, map, ColorAdjustTypeBrush);
139     }
140 
141     Status
142     SetColorKey(const Color &colorLow, const Color &colorHigh, ColorAdjustType type = ColorAdjustTypeDefault)
143     {
144         return SetStatus(DllExports::GdipSetImageAttributesColorKeys(
145             nativeImageAttr, type, TRUE, colorLow.GetValue(), colorHigh.GetValue()));
146     }
147 
148     Status
149     SetColorMatrices(
150         const ColorMatrix *colorMatrix,
151         const ColorMatrix *grayMatrix,
152         ColorMatrixFlags mode = ColorMatrixFlagsDefault,
153         ColorAdjustType type = ColorAdjustTypeDefault)
154     {
155         return SetStatus(
156             DllExports::GdipSetImageAttributesColorMatrix(nativeImageAttr, type, TRUE, colorMatrix, grayMatrix, mode));
157     }
158 
159     Status
160     SetColorMatrix(
161         const ColorMatrix *colorMatrix,
162         ColorMatrixFlags mode = ColorMatrixFlagsDefault,
163         ColorAdjustType type = ColorAdjustTypeDefault)
164     {
165         return SetStatus(
166             DllExports::GdipSetImageAttributesColorMatrix(nativeImageAttr, type, TRUE, colorMatrix, NULL, mode));
167     }
168 
169     Status
170     SetGamma(REAL gamma, ColorAdjustType type = ColorAdjustTypeDefault)
171     {
172         return SetStatus(DllExports::GdipSetImageAttributesGamma(nativeImageAttr, type, TRUE, gamma));
173     }
174 
175     Status
176     SetNoOp(ColorAdjustType type = ColorAdjustTypeDefault)
177     {
178         return SetStatus(DllExports::GdipSetImageAttributesNoOp(nativeImageAttr, type, TRUE));
179     }
180 
181     Status
182     SetOutputChannel(ColorChannelFlags channelFlags, ColorAdjustType type = ColorAdjustTypeDefault)
183     {
184         return SetStatus(DllExports::GdipSetImageAttributesOutputChannel(nativeImageAttr, type, TRUE, channelFlags));
185     }
186 
187     Status
188     SetOutputChannelColorProfile(const WCHAR *colorProfileFilename, ColorAdjustType type = ColorAdjustTypeDefault)
189     {
190         return SetStatus(DllExports::GdipSetImageAttributesOutputChannelColorProfile(
191             nativeImageAttr, type, TRUE, colorProfileFilename));
192     }
193 
194     Status
195     SetRemapTable(UINT mapSize, const ColorMap *map, ColorAdjustType type = ColorAdjustTypeDefault)
196     {
197         return SetStatus(DllExports::GdipSetImageAttributesRemapTable(nativeImageAttr, type, TRUE, mapSize, map));
198     }
199 
200     Status
201     SetThreshold(REAL threshold, ColorAdjustType type = ColorAdjustTypeDefault)
202     {
203         return SetStatus(DllExports::GdipSetImageAttributesThreshold(nativeImageAttr, type, TRUE, threshold));
204     }
205 
206     Status
207     SetToIdentity(ColorAdjustType type = ColorAdjustTypeDefault)
208     {
209         return SetStatus(DllExports::GdipSetImageAttributesToIdentity(nativeImageAttr, type));
210     }
211 
212     Status
213     SetWrapMode(WrapMode wrap, const Color &color = Color(), BOOL clamp = FALSE)
214     {
215         ARGB argb = color.GetValue();
216         return SetStatus(DllExports::GdipSetImageAttributesWrapMode(nativeImageAttr, wrap, argb, clamp));
217     }
218 
219   protected:
220     GpImageAttributes *nativeImageAttr;
221     mutable Status lastStatus;
222 
223     ImageAttributes(GpImageAttributes *imageAttr, Status status) : nativeImageAttr(imageAttr), lastStatus(status)
224     {
225     }
226 
227     VOID
228     SetNativeImageAttr(GpImageAttributes *imageAttr)
229     {
230         nativeImageAttr = imageAttr;
231     }
232 
233     Status
234     SetStatus(Status status) const
235     {
236         if (status != Ok)
237             lastStatus = status;
238         return status;
239     }
240 
241   private:
242     // ImageAttributes is not copyable
243     ImageAttributes(const ImageAttributes &);
244     ImageAttributes &
245     operator=(const ImageAttributes &);
246 
247     // get native
248     friend inline GpImageAttributes *&
249     getNat(const ImageAttributes *ia)
250     {
251         return const_cast<ImageAttributes *>(ia)->nativeImageAttr;
252     }
253 };
254 
255 #endif /* _GDIPLUSIMAGEATTRIBUTES_H */
256