libs: sync ctx with upstream
[gegl.git] / perf / test-compression.c
blob69aee8bb42d80735a08a1697fda0ad85da8b5540
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 3 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, see <https://www.gnu.org/licenses/>.
15 * Copyright (C) 2018 Ell
18 #include "test-common.h"
19 #include "buffer/gegl-compression.h"
21 #define SUCCESS 0
22 #define FAILURE -1
24 static gpointer
25 load_png (const gchar *path,
26 const Babl *format,
27 gint *n)
29 GeglNode *node;
30 GeglNode *node_source;
31 GeglNode *node_sink;
32 GeglBuffer *buffer = NULL;
33 gpointer data;
35 node = gegl_node_new ();
37 node_source = gegl_node_new_child (node,
38 "operation", "gegl:load",
39 "path", path,
40 NULL);
41 node_sink = gegl_node_new_child (node,
42 "operation", "gegl:buffer-sink",
43 "buffer", &buffer,
44 NULL);
46 gegl_node_link (node_source, node_sink);
48 gegl_node_process (node_sink);
50 g_object_unref (node);
52 *n = gegl_buffer_get_width (buffer) * gegl_buffer_get_height (buffer);
53 data = g_malloc (*n * babl_format_get_bytes_per_pixel (format));
55 gegl_buffer_get (buffer, NULL, 1.0, format, data,
56 GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
58 g_object_unref (buffer);
60 return data;
63 gint
64 main (gint argc,
65 gchar **argv)
67 const Babl *format;
68 gint bpp;
69 gchar *path;
70 gpointer data;
71 gint n;
72 gint size;
73 guint8 *compressed;
74 gint max_compressed_size;
75 guint8 *decompressed;
76 const gchar **algorithms;
77 gint i;
78 gint result = FAILURE;
80 gegl_init (&argc, &argv);
82 format = babl_format ("R'G'B'A u8");
83 bpp = babl_format_get_bytes_per_pixel (format);
85 path = g_build_filename (g_getenv ("ABS_TOP_SRCDIR"),
86 "tests", "compositions", "data", "car-stack.png",
87 NULL);
89 data = load_png (path, format, &n);
90 size = n * bpp;
92 g_free (path);
94 max_compressed_size = 2 * n * bpp;
95 compressed = g_malloc (max_compressed_size);
96 decompressed = g_malloc (size);
98 algorithms = gegl_compression_list ();
100 for (i = 0; algorithms[i]; i++)
102 const GeglCompression *compression = gegl_compression (algorithms[i]);
103 gchar *id;
104 gint compressed_size;
105 gint j;
107 id = g_strdup_printf ("%s compress", algorithms[i]);
108 test_start ();
110 for (j = 0; j < ITERATIONS && converged < BAIL_COUNT; j++)
112 test_start_iter();
114 if (! gegl_compression_compress (compression, format,
115 data, n,
116 compressed, &compressed_size,
117 max_compressed_size))
119 goto end;
122 test_end_iter();
125 test_end (id, (gdouble) size * ITERATIONS);
126 g_free (id);
128 id = g_strdup_printf ("%s decompress", algorithms[i]);
129 test_start ();
131 for (j = 0; j < ITERATIONS && converged < BAIL_COUNT; j++)
133 test_start_iter();
135 if (! gegl_compression_decompress (compression, format,
136 decompressed, n,
137 compressed, compressed_size))
139 goto end;
142 test_end_iter();
145 test_end (id, (gdouble) size * ITERATIONS);
146 g_free (id);
149 result = SUCCESS;
151 end:
152 g_free (algorithms);
154 g_free (compressed);
155 g_free (decompressed);
157 g_free (data);
159 gegl_exit ();
161 return result;