~muktupavels/metacity/adwaita-icon-theme-lp-1414613

« back to all changes in this revision

Viewing changes to src/tools/metacity-grayscale.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-10-03 22:44:28 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20051003224428-ft31gkmz12qpzohj
Tags: 1:2.12.1-0ubuntu1
* New upstream release:
  - Thanks to Ray Strode, Havoc Pennington, and Elijah Newren for
    improvements in this release.
  - Truncate ridiculously long titles to avoid crashing or letting the
    pager crash (Ray, Havoc, Elijah) [#315070] (Ubuntu: #15995)
  - Get the tabbing window outline to work with gtk+ 2.8.4 again
    (Elijah) [#317528] (Ubuntu: #16589)
  - Translations: Mahay Alam Khan (bn), Francisco Javier F. Serrador (es), 
    Ivar Smolin (et), I\uffffaki Larra\uffffaga Murgoitio (eu), Luca 
    Ferretti (it), Christian Rose (sv), Clytie Siddall (vi), Funda 
    Wang (zh_CN)
* debian/control.in:
  - Bumped Standards-Version.
* debian/patches/003_bordersdrawingfix.patch:
  - dropped, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Hack for grayscaling an image */
 
2
 
 
3
/* 
 
4
 * Copyright (C) 2002 Red Hat Inc.
 
5
 * 
 
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.
 
10
 *
 
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.
 
15
 * 
 
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
 
19
 * 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include <gdk-pixbuf/gdk-pixbuf.h>
 
23
#include <unistd.h>
 
24
#include <stdlib.h>
 
25
#include <math.h>
 
26
 
 
27
#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
 
28
 
 
29
static GdkPixbuf*
 
30
grayscale_pixbuf (GdkPixbuf *pixbuf)
 
31
{
 
32
  GdkPixbuf *gray;
 
33
  guchar *pixels;
 
34
  int rowstride;
 
35
  int pixstride;
 
36
  int row;
 
37
  int n_rows;
 
38
  int width;
 
39
  
 
40
  gray = gdk_pixbuf_copy (pixbuf);
 
41
  rowstride = gdk_pixbuf_get_rowstride (gray);
 
42
  pixstride = gdk_pixbuf_get_has_alpha (gray) ? 4 : 3;
 
43
  
 
44
  pixels = gdk_pixbuf_get_pixels (gray);
 
45
  n_rows = gdk_pixbuf_get_height (gray);
 
46
  width = gdk_pixbuf_get_width (gray);
 
47
  
 
48
  row = 0;
 
49
  while (row < n_rows)
 
50
    {
 
51
      guchar *p = pixels + row * rowstride;
 
52
      guchar *end = p + (pixstride * width);
 
53
 
 
54
      while (p != end)
 
55
        {
 
56
          double v = INTENSITY (p[0], p[1], p[2]);
 
57
 
 
58
          p[0] = (guchar) v;
 
59
          p[1] = (guchar) v;
 
60
          p[2] = (guchar) v;
 
61
          
 
62
          p += pixstride;
 
63
        }
 
64
      
 
65
      ++row;
 
66
    }
 
67
  
 
68
  return gray;
 
69
}
 
70
 
 
71
int
 
72
main (int argc, char **argv)
 
73
{
 
74
  GdkPixbuf *pixbuf;
 
75
  GdkPixbuf *gray;
 
76
  GError *err;
 
77
  
 
78
  if (argc != 2)
 
79
    {
 
80
      g_printerr ("specify a single image on the command line\n");
 
81
      return 1;
 
82
    }
 
83
 
 
84
  g_type_init ();
 
85
  
 
86
  err = NULL;
 
87
  pixbuf = gdk_pixbuf_new_from_file (argv[1], &err);
 
88
  if (err != NULL)
 
89
    {
 
90
      g_printerr ("failed to load image: %s\n", err->message);
 
91
      g_error_free (err);
 
92
      return 1;
 
93
    }
 
94
 
 
95
  gray = grayscale_pixbuf (pixbuf);
 
96
  
 
97
  err = NULL;
 
98
  gdk_pixbuf_save (gray, "grayscale.png", "png", &err, NULL);
 
99
  if (err != NULL)
 
100
    {
 
101
      g_printerr ("failed to save image: %s\n", err->message);
 
102
      g_error_free (err);
 
103
      return 1;
 
104
    }
 
105
 
 
106
  g_print ("wrote grayscale.png\n");
 
107
  
 
108
  return 0;
 
109
}