summaryrefslogtreecommitdiff
path: root/engines/mice/generate.c
blob: 9645290ca122bdadcc1ca7648504c9a28ce9a12b (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* $Id$ */
/*-
 * Copyright (c) 2003-2004 Benedikt Meurer <benny@xfce.org>
 * All rights reserved.
 *
 * 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, 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 Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include <limits.h>
#include <errno.h>

#include <gtk/gtk.h>


static GdkPixbuf*
create_slide (GdkPixbuf *base, int steps)
{
  GdkPixbuf *result;
  int rw, rh;
  int bw, bh;
  int i;
  guchar *pixels, *p;
  int rowstride;
  int n_channels;
  int x, y;

  bw = gdk_pixbuf_get_width (base);
  bh = gdk_pixbuf_get_height (base);

  rw = bw * steps;
  rh = bh;

  result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, rw, rh);
  rowstride = gdk_pixbuf_get_rowstride (result);
  pixels = gdk_pixbuf_get_pixels (result);
  n_channels = gdk_pixbuf_get_n_channels (result);

  for (i = 0; i < steps; ++i)
    {
      gdk_pixbuf_copy_area (base, 0, 0, bw, bh,
                            result, i * bw, 0);

      for (x = 0; x < bw; ++x)
        {
          for (y = 0; y < bh; ++y)
            {
              p = pixels + y * rowstride + (i * bw + x) * n_channels;
              p[3] = ((i + 1) * p[3]) / (steps + 1);
            }
        }
    }

  return result;
}


int main (int argc, char **argv)
{
  GdkPixbuf *base;
  GdkPixbuf *result;
  glong val;

  gtk_init (&argc, &argv);

  if (argc != 3)
    {
      fprintf (stderr, "Usage: generate <file> <steps>\n");
      return EXIT_FAILURE;
    }

  base = gdk_pixbuf_new_from_file (argv[1], NULL);
  if (base == NULL)
    {
      fprintf (stderr, "generate: Unable to open file %s\n", argv[1]);
      return EXIT_FAILURE;
    }

  val = strtol (argv[2], NULL, 10);

  /* Error checking for untrusted input */
  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0))
    {
        perror("strtol");
        exit(EXIT_FAILURE);
    }

  /* Sanity checks */
  if (val > INT_MAX)
    val = INT_MAX;

  if (val < 0)
    val = 0;

  result = create_slide (base, val);

  gdk_pixbuf_save (result, "slide.png", "png", NULL, NULL);

  printf ("generate: New slide written to slide.png successfully.\n");

  return EXIT_SUCCESS;
}