]> Pileus Git - ~andy/gtk/blob - tests/pixbuf-randomly-modified.c
Deprecation cleanup
[~andy/gtk] / tests / pixbuf-randomly-modified.c
1 /* -*- Mode: C; c-basic-offset: 2; -*- */
2 /* GdkPixbuf library - test loaders
3  *
4  * Copyright (C) 2001 Søren Sandmann (sandmann@daimi.au.dk)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU 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 02111-1307, USA.
19  */
20
21 #include <config.h>
22 #include "gdk-pixbuf/gdk-pixbuf.h"
23 #include "glib.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <string.h>
28
29 static void
30 disaster (const char *what)
31 {
32   perror (what);
33   exit (EXIT_FAILURE);
34 }
35
36 static void
37 randomly_modify (const guchar *image, guint size)
38 {
39   int i;
40
41   guchar *img_copy = g_malloc (size);
42   g_memmove (img_copy, image, size);
43   
44   for (i = 0; i < size / 4; i++)
45     {
46       FILE *f;
47       GdkPixbufLoader *loader;
48       
49       guint index = g_random_int_range (0, size);
50       guchar byte = g_random_int_range (0, 256);
51       
52       img_copy[index] = byte;
53       f = fopen ("pixbuf-randomly-modified-image", "w");
54       if (!f)
55         disaster ("fopen");
56       fwrite (img_copy, size, 1, f);
57       if (ferror (f))
58         disaster ("fwrite");
59       fclose (f);
60
61       loader = gdk_pixbuf_loader_new ();
62       gdk_pixbuf_loader_write (loader, img_copy, size, NULL);
63       gdk_pixbuf_loader_close (loader, NULL);
64       g_object_unref (loader);
65     }
66   g_free (img_copy);
67 }
68
69 static void
70 write_seed (int seed)
71 {
72   FILE *f;
73   /* write this so you can reproduce failed tests */
74   f = fopen ("pixbuf-randomly-modified-seed", "w");
75
76   if (!f)
77     disaster ("fopen");
78
79   if (fprintf (f, "%d\n", seed) < 0)
80     disaster ("fprintf");
81
82   if (fclose (f) < 0)
83     disaster ("fclose");
84
85   g_print ("seed: %d\n", seed);
86 }
87
88 static void
89 usage (void)
90 {
91   g_print ("usage: pixbuf-randomly-modified [-s <seed>] <files> ... \n");
92   exit (EXIT_FAILURE);
93 }
94
95 int
96 main (int argc, char **argv)
97 {
98   int seed, i;
99   gboolean got_seed = FALSE;
100   GPtrArray *files = g_ptr_array_new ();
101
102   if (argc == 1)
103     usage ();
104   
105   seed = time (NULL);
106
107   for (i = 1; i < argc; ++i)
108     {
109       if (strncmp (argv[i], "-s", 2) == 0)
110         {
111           if (strlen (argv[i]) > 2)
112             usage();
113           if (i+1 < argc)
114             {
115               seed = atoi (argv[i+1]);
116               got_seed = TRUE;
117               ++i;
118             }
119           else
120             usage();
121         }
122       else
123         g_ptr_array_add (files, strdup (argv[i]));
124     }
125
126   if (!got_seed)
127     write_seed (seed);
128
129   g_random_set_seed (seed);
130
131   g_print ("the last tested image is saved to pixbuf-randomly-modified-image\n");
132   
133   g_type_init ();
134   g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
135
136   for (;;)
137     for (i = 0; i < files->len; ++i)
138       {
139         gchar *contents;
140         gsize size;
141         GError *err = NULL;
142         
143         fflush (stdout);
144         if (!g_file_get_contents (files->pdata[i], &contents, &size, &err))
145           {
146             g_print ("%s: error: %s\n", (char *)files->pdata[i], err->message);
147           }
148         else
149           {
150             g_print ("%s\t\t", (char *)files->pdata[i]);
151             randomly_modify (contents, size);
152             g_print ("done\n");
153             
154             g_free (contents);
155           }
156       }
157   
158   return 0;
159 }