summaryrefslogtreecommitdiff
path: root/tests/test-common.c
blob: 27af942120e2053e55ed53cc1329c42b677b7819 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* -*- Mode: C; c-basic-offset: 2; -*- */
/* GdkPixbuf library - test loaders
 *
 * Copyright (C) 2014 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * Author: Matthias Clasen
 */

#include "config.h"
#include "test-common.h"
#include "gdk-pixbuf/gdk-pixbuf.h"

#include <string.h>

/* Checkerboard of black and white pxels (actually (1,1,1) and (255,255,255)
 * so they average to (128,128,128)) */
GdkPixbuf *
make_checkerboard (int width, int height)
{
  GdkPixbuf *checkerboard;
  guint x, y;
  guchar *row;   /* Pointer to start of row of pixels within the image */
  guchar *pixel; /* Pointer to current pixel data in row */

  checkerboard = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, width, height);
  g_assert_nonnull (checkerboard);

  for (y = 0, row = gdk_pixbuf_get_pixels (checkerboard);
       y < height;
       y++, row += gdk_pixbuf_get_rowstride (checkerboard))
    {
      for (x = 0, pixel = row;
           x < width;
           x++, pixel += gdk_pixbuf_get_n_channels (checkerboard))
        {
          pixel[0] = pixel[1] = pixel[2] = (x ^ y) & 1 ? 1 : 255;
        }
    }

  return checkerboard;
}

/* Image where all the pixels have different colours */
GdkPixbuf *
make_rg (int width, int height)
{
  GdkPixbuf *pixbuf;
  guint x, y;
  guchar *row;   /* Pointer to start of row of pixels within the image */
  guchar *pixel; /* Pointer to current pixel data in row */

  /* Make a source image whose pixels are all of different colors */
  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, width, height);
  g_assert_nonnull (pixbuf);

  for (y = 0, row = gdk_pixbuf_get_pixels (pixbuf);
       y < height;
       y++, row += gdk_pixbuf_get_rowstride (pixbuf))
    {
      for (x = 0, pixel = row;
           x < width;
           x++, pixel += gdk_pixbuf_get_n_channels (pixbuf))
        {
          pixel[0] = x & 255; pixel[1] = y & 255;
          /* If image > 256 pixels wide/high put the extra bits in the last pixel */
          pixel[2] = ((x >> 8) & 15) | ((( y >> 8) & 15) << 4);
        }
    }

  return pixbuf;
}

gboolean
find_format (const gchar *filename, gchar **found_format)
{
  GSList *formats, *l;
  gboolean retval;

  retval = FALSE;
  formats = gdk_pixbuf_get_formats ();
  for (l = formats; l; l = l->next)
    {
      GdkPixbufFormat *format = l->data;
      char **extensions = gdk_pixbuf_format_get_extensions (format);
      gint i;

      for (i = 0; extensions[i]; i++)
        {
          if (g_str_has_suffix (filename, extensions[i]))
            {
              if (found_format != NULL)
                *found_format = gdk_pixbuf_format_get_name (format);
              retval = TRUE;
              break;
            }
        }

      g_strfreev (extensions);
      if (retval)
        break;
    }
  g_slist_free (formats);

  return retval;
}

gboolean
format_supported (const gchar *filename)
{
  return find_format(filename, NULL);
}

gboolean
file_supported (GFile *file)
{
  char *uri;
  gboolean result;

  uri = g_file_get_uri (file);

  result = format_supported (uri);

  g_free (uri);

  return result;
}

gboolean
skip_if_insufficient_memory (GError **err)
{
  if (*err && g_error_matches (*err, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY))
  {
      g_test_skip ((*err)->message);
      g_error_free (*err);
      *err = NULL;
      return TRUE;
  }

  return FALSE;
}

gboolean
pixdata_equal (GdkPixbuf  *test,
               GdkPixbuf  *ref,
               GError    **error)
{
  if (gdk_pixbuf_get_colorspace (test) != gdk_pixbuf_get_colorspace (ref))
    {
      g_set_error (error, GDK_PIXBUF_ERROR, 0, "Image colorspace is %d but should be %d",
                   gdk_pixbuf_get_colorspace (test), gdk_pixbuf_get_colorspace (ref));
      return FALSE;
    }

  if (gdk_pixbuf_get_n_channels (test) != gdk_pixbuf_get_n_channels (ref))
    {
      g_set_error (error, GDK_PIXBUF_ERROR, 0,
                   "has %u channels but should have %u",
                   gdk_pixbuf_get_n_channels (test), gdk_pixbuf_get_n_channels (ref));
      return FALSE;
    }

  if (gdk_pixbuf_get_bits_per_sample (test) != gdk_pixbuf_get_bits_per_sample (ref))
    {
      g_set_error (error, GDK_PIXBUF_ERROR, 0,
                   "Image is %u bits per sample but should be %u bits per sample",
                   gdk_pixbuf_get_bits_per_sample (test), gdk_pixbuf_get_bits_per_sample (ref));
      return FALSE;
    }

  if (gdk_pixbuf_get_width (test) != gdk_pixbuf_get_width (ref) ||
      gdk_pixbuf_get_height (test) != gdk_pixbuf_get_height (ref))
    {
      g_set_error (error, GDK_PIXBUF_ERROR, 0,
                   "Image size is %dx%d but should be %dx%d",
                   gdk_pixbuf_get_width (test), gdk_pixbuf_get_height (test),
                   gdk_pixbuf_get_width (ref), gdk_pixbuf_get_height (ref));
      return FALSE;
    }

  if (gdk_pixbuf_get_rowstride (test) != gdk_pixbuf_get_rowstride (ref))
    {
      g_set_error (error, GDK_PIXBUF_ERROR, 0,
                   "Image rowstrides is %u bytes but should be %u bytes",
                   gdk_pixbuf_get_rowstride (test), gdk_pixbuf_get_rowstride (ref));
      return FALSE;
    }

  if (memcmp (gdk_pixbuf_get_pixels (test), gdk_pixbuf_get_pixels (ref),
          gdk_pixbuf_get_byte_length (test)) != 0)
    {
      gint x, y, width, height, n_channels, rowstride;
      const guchar *test_pixels, *ref_pixels;

      rowstride = gdk_pixbuf_get_rowstride (test);
      n_channels = gdk_pixbuf_get_n_channels (test);
      width = gdk_pixbuf_get_width (test);
      height = gdk_pixbuf_get_height (test);
      test_pixels = gdk_pixbuf_get_pixels (test);
      ref_pixels = gdk_pixbuf_get_pixels (ref);

      g_assert_cmpint (width, >=, 0);
      g_assert_cmpint (height, >=, 0);
      g_assert_cmpint (n_channels, >=, 0);

      for (y = 0; y < height; y++)
        {
          for (x = 0; x < width; x++)
            {
              if (memcmp (&test_pixels[x * n_channels], &ref_pixels[x * n_channels], n_channels) != 0)
                {
                  if (n_channels == 4)
                    {
                      g_set_error (error, GDK_PIXBUF_ERROR, 0, "Image data at %ux%u is #%02X%02X%02X%02X, but should be #%02X%02X%02X%02X",
                                   x, y,
                                   test_pixels[x * n_channels + 0], test_pixels[x * n_channels + 1], test_pixels[x * n_channels + 2], test_pixels[x * n_channels + 3],
                                   ref_pixels[x * n_channels + 0], ref_pixels[x * n_channels + 1], ref_pixels[x * n_channels + 2], ref_pixels[x * n_channels + 3]);
                    }
                  else if (n_channels == 3)
                    {
                      g_set_error (error, GDK_PIXBUF_ERROR, 0, "Image data at %ux%u is #%02X%02X%02X, but should be #%02X%02X%02X",
                                   x, y,
                                   test_pixels[x * n_channels + 0], test_pixels[x * n_channels + 1], test_pixels[x * n_channels + 2],
                                   ref_pixels[x * n_channels + 0], ref_pixels[x * n_channels + 1], ref_pixels[x * n_channels + 2]);
                    }
                  else
                    {
                      g_set_error (error, GDK_PIXBUF_ERROR, 0, "Image data differ at %ux%u", x, y);
                    }
                  return FALSE;
                }
            }
          test_pixels += rowstride;
          ref_pixels += rowstride;
        }
    }

  return TRUE;
}

static int
compare_files (gconstpointer a, gconstpointer b)
{
  GFile *file1 = G_FILE (a);
  GFile *file2 = G_FILE (b);
  char *uri1, *uri2;
  int result;

  uri1 = g_file_get_uri (file1);
  uri2 = g_file_get_uri (file2);

  result = strcmp (uri1, uri2);

  g_free (uri1);
  g_free (uri2);

  return result;
}

void
add_test_for_all_images (const gchar   *prefix,
                         GFile         *base,
                         GFile         *file,
                         GTestDataFunc  test_func,
                         AddTestFunc    add_test_func)
{
  GFileEnumerator *enumerator;
  GFileInfo *info;
  GList *l, *files;
  GError *error = NULL;


  if (g_file_query_file_type (file, 0, NULL) != G_FILE_TYPE_DIRECTORY)
    {
      gchar *test_path;
      gchar *relative_path;

      if (base)
        relative_path = g_file_get_relative_path (base, file);
      else
        relative_path = g_file_get_path (file);

      test_path = g_strconcat (prefix, "/", relative_path, NULL);
      
      g_test_add_data_func_full (test_path, g_object_ref (file), test_func, g_object_unref);
      g_free (relative_path);
      g_free (test_path);
      return;
    }


  enumerator = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
  g_assert_no_error (error);
  files = NULL;

  while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
    {
      GFile *next_file = g_file_get_child (file, g_file_info_get_name (info));

      if (add_test_func == NULL || add_test_func (next_file))
        {
          files = g_list_prepend (files, g_object_ref (next_file));
        }

      g_object_unref (next_file);
      g_object_unref (info);
    }
  
  g_assert_no_error (error);
  g_object_unref (enumerator);

  files = g_list_sort (files, compare_files);

  for (l = files; l; l = l->next)
    {
      add_test_for_all_images (prefix, base, l->data, test_func, add_test_func);
    }

  g_list_free_full (files, g_object_unref);
}