2
// Copyright (C) 2010 Robert Dyer
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
9
// This program 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
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program. If not, see <http://www.gnu.org/licenses/>.
22
namespace Docky.Services
24
public static class GdkPixbufExtension
26
// these methods all assume that the BitsPerSample is 8 (byte). Pixbuf documentation
27
// states that values from 1-16 are allowed, but currently only 8 bit samples are supported.
28
// http://developer.gimp.org/api/2.0/gdk-pixbuf/gdk-pixbuf-gdk-pixbuf.html#GdkPixbuf--bits-per-sample
31
/// Applies a color transformation to each pixel in a pixbuf.
33
/// <param name="colorTransform">
34
/// A <see cref="Func<Cairo.Color, Cairo.Color>"/>
37
/// A reference to the input Pixbuf (the 'this' reference).
39
public static Pixbuf PixelColorTransform (this Pixbuf source, Func<Cairo.Color, Cairo.Color> colorTransform)
42
int offset = (source.HasAlpha) ? 4 : 3;
45
byte* pixels = (byte*) source.Pixels;
46
for (int i = 0; i < source.Height * source.Width; i++) {
47
r = (double) pixels[0];
48
g = (double) pixels[1];
49
b = (double) pixels[2];
51
Cairo.Color color = new Cairo.Color (r / byte.MaxValue,
55
color = colorTransform.Invoke (color);
57
pixels[0] = (byte) (color.R * byte.MaxValue);
58
pixels[1] = (byte) (color.G * byte.MaxValue);
59
pixels[2] = (byte) (color.B * byte.MaxValue);
64
} catch (Exception e) {
65
Log<DrawingService>.Error ("Error transforming pixbuf: {0}", e.Message);
66
Log<DrawingService>.Debug (e.StackTrace);
72
/// Returns a monochrome version of the supplied pixbuf.
75
/// A reference to the input Pixbuf (the 'this' reference).
77
public static Pixbuf MonochromePixbuf (this Pixbuf source)
79
return source.PixelColorTransform ((c) => c.DarkenBySaturation (0.5).SetSaturation (0));
83
/// Adds a hue shift to the supplpied pixbuf.
85
/// <param name="shift">
86
/// A <see cref="System.Double"/>
89
/// A reference to the input Pixbuf (the 'this' reference).
91
public static Pixbuf AddHueShift (this Pixbuf source, double shift)
94
source.PixelColorTransform ((c) => c.AddHue (shift));
99
/// Scale a pixbuf to the desired width or height maintaining the aspect ratio of the supplied pixbuf.
100
/// Note that due to maintaining the aspect ratio, the returned pixbuf may not have the exact width AND height as is specified.
101
/// Though it is guaranteed that one of these measurements will be correct.
103
/// <param name="width">
104
/// A <see cref="System.Int32"/>
106
/// <param name="height">
107
/// A <see cref="System.Int32"/>
109
/// <param name="pixbuf">
110
/// A <see cref="Pixbuf"/>
114
public static Pixbuf ARScale (this Pixbuf source, int width, int height)
116
double xScale = (double) width / (double) source.Width;
117
double yScale = (double) height / (double) source.Height;
118
double scale = Math.Min (xScale, yScale);
123
Pixbuf tmp = source.ScaleSimple ((int) (source.Width * scale),
124
(int) (source.Height * scale),