1
/* Hack for grayscaling an image */
4
* Copyright (C) 2002 Red Hat Inc.
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License as
8
* published by the Free Software Foundation; either version 2 of the
9
* License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22
#include <gdk-pixbuf/gdk-pixbuf.h>
27
#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
30
grayscale_pixbuf (GdkPixbuf *pixbuf)
40
gray = gdk_pixbuf_copy (pixbuf);
41
rowstride = gdk_pixbuf_get_rowstride (gray);
42
pixstride = gdk_pixbuf_get_has_alpha (gray) ? 4 : 3;
44
pixels = gdk_pixbuf_get_pixels (gray);
45
n_rows = gdk_pixbuf_get_height (gray);
46
width = gdk_pixbuf_get_width (gray);
51
guchar *p = pixels + row * rowstride;
52
guchar *end = p + (pixstride * width);
56
double v = INTENSITY (p[0], p[1], p[2]);
72
main (int argc, char **argv)
80
g_printerr ("specify a single image on the command line\n");
87
pixbuf = gdk_pixbuf_new_from_file (argv[1], &err);
90
g_printerr ("failed to load image: %s\n", err->message);
95
gray = grayscale_pixbuf (pixbuf);
98
gdk_pixbuf_save (gray, "grayscale.png", "png", &err, NULL);
101
g_printerr ("failed to save image: %s\n", err->message);
106
g_print ("wrote grayscale.png\n");