|
Packit |
a4058c |
/* -*- Mode: C; c-basic-offset: 2; -*- */
|
|
Packit |
a4058c |
/* GdkPixbuf library - test loaders
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* Copyright (C) 2014 Red Hat, Inc.
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
a4058c |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
a4058c |
* the Free Software Foundation; either version 2 of the License, or
|
|
Packit |
a4058c |
* (at your option) any later version.
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* This program is distributed in the hope that it will be useful,
|
|
Packit |
a4058c |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
a4058c |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
a4058c |
* GNU General Public License for more details.
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* You should have received a copy of the GNU General Public License
|
|
Packit |
a4058c |
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
Packit |
a4058c |
*/
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#include "gdk-pixbuf/gdk-pixbuf.h"
|
|
Packit |
a4058c |
#include "test-common.h"
|
|
Packit |
a4058c |
#include <string.h>
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#ifdef G_OS_UNIX
|
|
Packit |
a4058c |
#include <sys/mman.h>
|
|
Packit |
a4058c |
#include <unistd.h>
|
|
Packit |
a4058c |
#endif
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#ifdef G_OS_UNIX
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* see https://bugzilla.gnome.org/show_bug.cgi?id=741933 */
|
|
Packit |
a4058c |
#ifndef MAP_ANONYMOUS
|
|
Packit |
a4058c |
#define MAP_ANONYMOUS MAP_ANON
|
|
Packit |
a4058c |
#endif
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
typedef struct {
|
|
Packit |
a4058c |
void *buf;
|
|
Packit |
a4058c |
gsize len;
|
|
Packit |
a4058c |
} MappedBuf;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static void
|
|
Packit |
a4058c |
destroy_buf_unmap (gpointer data)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
MappedBuf *buf = data;
|
|
Packit |
a4058c |
int r;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
r = munmap (buf->buf, buf->len);
|
|
Packit |
a4058c |
g_assert_cmpint (r, ==, 0);
|
|
Packit |
a4058c |
g_free (buf);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
#endif
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static GdkPixbuf *
|
|
Packit |
a4058c |
get_readonly_pixbuf (void)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
GdkPixbuf *reference;
|
|
Packit |
a4058c |
GdkPixbuf *result;
|
|
Packit |
a4058c |
GBytes *bytes;
|
|
Packit |
a4058c |
GError *error = NULL;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
reference = gdk_pixbuf_new_from_file (g_test_get_filename (G_TEST_DIST, "test-image.png", NULL), &error);
|
|
Packit |
a4058c |
g_assert_no_error (error);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#ifdef G_OS_UNIX
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
MappedBuf *buf;
|
|
Packit |
a4058c |
int pagesize;
|
|
Packit |
a4058c |
int pages;
|
|
Packit |
a4058c |
int r;
|
|
Packit |
a4058c |
gsize pixlen;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
pagesize = sysconf (_SC_PAGE_SIZE);
|
|
Packit |
a4058c |
g_assert_cmpint (pagesize, >, 0);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
pixlen = gdk_pixbuf_get_byte_length (reference);
|
|
Packit |
a4058c |
pages = pixlen / pagesize + 1;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
buf = g_new0 (MappedBuf, 1);
|
|
Packit |
a4058c |
buf->len = pages * pagesize;
|
|
Packit |
a4058c |
buf->buf = mmap (NULL, buf->len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
Packit |
a4058c |
g_assert (buf->buf != NULL);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
memcpy (buf->buf, gdk_pixbuf_get_pixels (reference), pixlen);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
r = mprotect (buf->buf, buf->len, PROT_READ);
|
|
Packit |
a4058c |
g_assert_cmpint (r, ==, 0);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
bytes = g_bytes_new_with_free_func (buf->buf, buf->len, destroy_buf_unmap, buf);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
#else
|
|
Packit |
a4058c |
bytes = g_bytes_new (gdk_pixbuf_get_pixels (reference), gdk_pixbuf_get_byte_length (reference));
|
|
Packit |
a4058c |
#endif
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
result = gdk_pixbuf_new_from_bytes (bytes,
|
|
Packit |
a4058c |
gdk_pixbuf_get_colorspace (reference),
|
|
Packit |
a4058c |
gdk_pixbuf_get_has_alpha (reference),
|
|
Packit |
a4058c |
gdk_pixbuf_get_bits_per_sample (reference),
|
|
Packit |
a4058c |
gdk_pixbuf_get_width (reference),
|
|
Packit |
a4058c |
gdk_pixbuf_get_height (reference),
|
|
Packit |
a4058c |
gdk_pixbuf_get_rowstride (reference));
|
|
Packit |
a4058c |
g_object_unref (reference);
|
|
Packit |
a4058c |
g_bytes_unref (bytes);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return result;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static void
|
|
Packit |
a4058c |
test_mutate_readonly (void)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
GdkPixbuf *src;
|
|
Packit |
a4058c |
GdkPixbuf *dest;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!format_supported ("png"))
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
g_test_skip ("format not supported");
|
|
Packit |
a4058c |
return;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
src = get_readonly_pixbuf ();
|
|
Packit |
a4058c |
gdk_pixbuf_scale (src, src,
|
|
Packit |
a4058c |
0, 0,
|
|
Packit |
a4058c |
gdk_pixbuf_get_width (src) / 4,
|
|
Packit |
a4058c |
gdk_pixbuf_get_height (src) / 4,
|
|
Packit |
a4058c |
0, 0, 0.5, 0.5,
|
|
Packit |
a4058c |
GDK_INTERP_NEAREST);
|
|
Packit |
a4058c |
g_object_unref (src);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
src = get_readonly_pixbuf ();
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
dest = gdk_pixbuf_scale_simple (src,
|
|
Packit |
a4058c |
gdk_pixbuf_get_width (src) / 4,
|
|
Packit |
a4058c |
gdk_pixbuf_get_height (src) / 4,
|
|
Packit |
a4058c |
GDK_INTERP_NEAREST);
|
|
Packit |
a4058c |
g_object_unref (dest);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
dest = gdk_pixbuf_composite_color_simple (src,
|
|
Packit |
a4058c |
gdk_pixbuf_get_width (src) / 4,
|
|
Packit |
a4058c |
gdk_pixbuf_get_height (src) / 4,
|
|
Packit |
a4058c |
GDK_INTERP_NEAREST,
|
|
Packit |
a4058c |
128,
|
|
Packit |
a4058c |
8,
|
|
Packit |
a4058c |
G_MAXUINT32,
|
|
Packit |
a4058c |
G_MAXUINT32/2);
|
|
Packit |
a4058c |
g_object_unref (dest);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
dest = gdk_pixbuf_rotate_simple (src, 180);
|
|
Packit |
a4058c |
g_object_unref (dest);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
dest = gdk_pixbuf_flip (src, TRUE);
|
|
Packit |
a4058c |
g_object_unref (dest);
|
|
Packit |
a4058c |
dest = gdk_pixbuf_flip (src, FALSE);
|
|
Packit |
a4058c |
g_object_unref (dest);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
g_object_unref (src);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static void
|
|
Packit |
a4058c |
test_read_pixel_bytes (void)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
GdkPixbuf *src;
|
|
Packit |
a4058c |
GBytes *bytes;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!format_supported ("png"))
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
g_test_skip ("format not supported");
|
|
Packit |
a4058c |
return;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
src = get_readonly_pixbuf ();
|
|
Packit |
a4058c |
bytes = gdk_pixbuf_read_pixel_bytes (src);
|
|
Packit |
a4058c |
g_object_unref (src);
|
|
Packit |
a4058c |
g_bytes_unref (bytes);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
src = get_readonly_pixbuf ();
|
|
Packit |
a4058c |
/* Force a mutable conversion */
|
|
Packit |
a4058c |
(void) gdk_pixbuf_get_pixels (src);
|
|
Packit |
a4058c |
bytes = gdk_pixbuf_read_pixel_bytes (src);
|
|
Packit |
a4058c |
g_object_unref (src);
|
|
Packit |
a4058c |
g_bytes_unref (bytes);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
int
|
|
Packit |
a4058c |
main (int argc, char **argv)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
g_test_init (&argc, &argv, NULL);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
g_test_add_func ("/pixbuf/readonly/mutate", test_mutate_readonly);
|
|
Packit |
a4058c |
g_test_add_func ("/pixbuf/readonly/readpixelbytes", test_read_pixel_bytes);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return g_test_run ();
|
|
Packit |
a4058c |
}
|