summaryrefslogtreecommitdiff
path: root/src/tools/marco-grayscale.c
blob: 7eb3e2b7ed370ac78ceac7fc94468fdf7531786d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Hack for grayscaling an image */

/*
 * Copyright (C) 2002 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>

#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)

static GdkPixbuf*
grayscale_pixbuf (GdkPixbuf *pixbuf)
{
  GdkPixbuf *gray;
  guchar *pixels;
  int rowstride;
  int pixstride;
  int row;
  int n_rows;
  int width;

  gray = gdk_pixbuf_copy (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (gray);
  pixstride = gdk_pixbuf_get_has_alpha (gray) ? 4 : 3;

  pixels = gdk_pixbuf_get_pixels (gray);
  n_rows = gdk_pixbuf_get_height (gray);
  width = gdk_pixbuf_get_width (gray);

  row = 0;
  while (row < n_rows)
    {
      guchar *p = pixels + row * rowstride;
      guchar *end = p + (pixstride * width);

      while (p != end)
        {
          double v = INTENSITY (p[0], p[1], p[2]);

          p[0] = (guchar) v;
          p[1] = (guchar) v;
          p[2] = (guchar) v;

          p += pixstride;
        }

      ++row;
    }

  return gray;
}

int
main (int argc, char **argv)
{
  GdkPixbuf *pixbuf;
  GdkPixbuf *gray;
  GError *err;

  if (argc != 2)
    {
      g_printerr ("specify a single image on the command line\n");
      return 1;
    }

  g_type_init ();

  err = NULL;
  pixbuf = gdk_pixbuf_new_from_file (argv[1], &err);
  if (err != NULL)
    {
      g_printerr ("failed to load image: %s\n", err->message);
      g_error_free (err);
      return 1;
    }

  gray = grayscale_pixbuf (pixbuf);

  err = NULL;
  gdk_pixbuf_save (gray, "grayscale.png", "png", &err, NULL);
  if (err != NULL)
    {
      g_printerr ("failed to save image: %s\n", err->message);
      g_error_free (err);
      return 1;
    }

  g_print ("wrote grayscale.png\n");

  return 0;
}