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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/*
* Copyright (C) 2011,2021 Red Hat Inc.
*
* Author:
* Benjamin Otte <otte@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "reftest-compare.h"
static gboolean
memory_format_is_high_depth (GdkMemoryFormat format)
{
switch (format)
{
case GDK_MEMORY_R8G8B8:
case GDK_MEMORY_B8G8R8:
case GDK_MEMORY_B8G8R8A8_PREMULTIPLIED:
case GDK_MEMORY_A8R8G8B8_PREMULTIPLIED:
case GDK_MEMORY_R8G8B8A8_PREMULTIPLIED:
case GDK_MEMORY_A8B8G8R8_PREMULTIPLIED:
case GDK_MEMORY_B8G8R8A8:
case GDK_MEMORY_A8R8G8B8:
case GDK_MEMORY_R8G8B8A8:
case GDK_MEMORY_A8B8G8R8:
case GDK_MEMORY_B8G8R8X8:
case GDK_MEMORY_X8R8G8B8:
case GDK_MEMORY_R8G8B8X8:
case GDK_MEMORY_X8B8G8R8:
case GDK_MEMORY_G8:
case GDK_MEMORY_G8A8:
case GDK_MEMORY_G8A8_PREMULTIPLIED:
case GDK_MEMORY_A8:
case GDK_MEMORY_G8_B8R8_420:
case GDK_MEMORY_G8_R8B8_420:
case GDK_MEMORY_G8_B8R8_422:
case GDK_MEMORY_G8_R8B8_422:
case GDK_MEMORY_G8_B8R8_444:
case GDK_MEMORY_G8_R8B8_444:
case GDK_MEMORY_G8_B8_R8_410:
case GDK_MEMORY_G8_R8_B8_410:
case GDK_MEMORY_G8_B8_R8_411:
case GDK_MEMORY_G8_R8_B8_411:
case GDK_MEMORY_G8_B8_R8_420:
case GDK_MEMORY_G8_R8_B8_420:
case GDK_MEMORY_G8_B8_R8_422:
case GDK_MEMORY_G8_R8_B8_422:
case GDK_MEMORY_G8_B8_R8_444:
case GDK_MEMORY_G8_R8_B8_444:
case GDK_MEMORY_G8B8G8R8_422:
case GDK_MEMORY_G8R8G8B8_422:
case GDK_MEMORY_R8G8B8G8_422:
case GDK_MEMORY_B8G8R8G8_422:
return FALSE;
case GDK_MEMORY_R16G16B16:
case GDK_MEMORY_R16G16B16A16_PREMULTIPLIED:
case GDK_MEMORY_R16G16B16A16:
case GDK_MEMORY_G16:
case GDK_MEMORY_G16A16:
case GDK_MEMORY_G16A16_PREMULTIPLIED:
case GDK_MEMORY_A16:
case GDK_MEMORY_R16G16B16_FLOAT:
case GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED:
case GDK_MEMORY_R16G16B16A16_FLOAT:
case GDK_MEMORY_A16_FLOAT:
case GDK_MEMORY_R32G32B32_FLOAT:
case GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED:
case GDK_MEMORY_R32G32B32A32_FLOAT:
case GDK_MEMORY_A32_FLOAT:
case GDK_MEMORY_G10X6_B10X6R10X6_420:
case GDK_MEMORY_G12X4_B12X4R12X4_420:
case GDK_MEMORY_G16_B16R16_420:
case GDK_MEMORY_X6G10_X6B10_X6R10_420:
case GDK_MEMORY_X6G10_X6B10_X6R10_422:
case GDK_MEMORY_X6G10_X6B10_X6R10_444:
case GDK_MEMORY_X4G12_X4B12_X4R12_420:
case GDK_MEMORY_X4G12_X4B12_X4R12_422:
case GDK_MEMORY_X4G12_X4B12_X4R12_444:
case GDK_MEMORY_G16_B16_R16_420:
case GDK_MEMORY_G16_B16_R16_422:
case GDK_MEMORY_G16_B16_R16_444:
return TRUE;
case GDK_MEMORY_N_FORMATS:
default:
g_assert_not_reached ();
return FALSE;
}
}
/* Compares two GDK_MEMORY_DEFAULT buffers, returning NULL if the
* buffers are equal or a surface containing a diff between the two
* surfaces.
*
* This function is originally from cairo:test/buffer-diff.c.
* Copyright © 2004 Richard D. Worth
*/
static GdkTexture *
buffer_diff_u8 (GdkColorState *color_state,
const guchar *buf_a,
gsize stride_a,
const guchar *buf_b,
gsize stride_b,
int width,
int height,
guint *max_diff_out,
guint *pixels_changed_out)
{
int x, y;
guchar *buf_diff = NULL;
gsize stride_diff = 0;
GdkTexture *diff = NULL;
guint max_diff = 0;
guint pixels_changed = 0;
for (y = 0; y < height; y++)
{
const guint32 *row_a = (const guint32 *) (buf_a + y * stride_a);
const guint32 *row_b = (const guint32 *) (buf_b + y * stride_b);
guint32 *row = (guint32 *) (buf_diff + y * stride_diff);
for (x = 0; x < width; x++)
{
int channel;
guint32 diff_pixel = 0;
/* check if the pixels are the same */
if (row_a[x] == row_b[x])
continue;
/* even if they're not literally the same, fully-transparent
* pixels are effectively the same regardless of colour */
if ((row_a[x] & 0xff000000) == 0 && (row_b[x] & 0xff000000) == 0)
continue;
if (diff == NULL)
{
GdkMemoryTextureBuilder *builder;
GBytes *bytes;
stride_diff = 4 * width;
buf_diff = g_malloc0_n (stride_diff, height);
bytes = g_bytes_new_take (buf_diff, stride_diff * height);
builder = gdk_memory_texture_builder_new ();
gdk_memory_texture_builder_set_width (builder, width);
gdk_memory_texture_builder_set_height (builder, height);
gdk_memory_texture_builder_set_format (builder, GDK_MEMORY_DEFAULT);
gdk_memory_texture_builder_set_color_state (builder, color_state);
gdk_memory_texture_builder_set_bytes (builder, bytes);
gdk_memory_texture_builder_set_stride (builder, stride_diff);
diff = gdk_memory_texture_builder_build (builder);
g_object_unref (builder);
row = (guint32 *) (buf_diff + y * stride_diff);
}
/* calculate a difference value for all 4 channels */
for (channel = 0; channel < 4; channel++)
{
int value_a = (row_a[x] >> (channel*8)) & 0xff;
int value_b = (row_b[x] >> (channel*8)) & 0xff;
guint channel_diff;
channel_diff = ABS (value_a - value_b);
if (channel_diff > max_diff)
max_diff = channel_diff;
channel_diff *= 4; /* emphasize */
if (channel_diff)
channel_diff += 128; /* make sure it's visible */
if (channel_diff > 255)
channel_diff = 255;
diff_pixel |= channel_diff << (channel * 8);
}
pixels_changed++;
if ((diff_pixel & 0x00ffffff) == 0)
{
/* alpha only difference, convert to luminance */
guint8 alpha = diff_pixel >> 24;
diff_pixel = alpha * 0x010101;
}
/* make the pixel fully opaque */
diff_pixel |= 0xff000000;
row[x] = diff_pixel;
}
}
if (max_diff_out != NULL)
*max_diff_out = max_diff;
if (pixels_changed_out != NULL)
*pixels_changed_out = pixels_changed;
return diff;
}
/* Compares two FLOAT buffers, returning NULL if the
* buffers are equal or a surface containing a diff between the two
* surfaces.
*/
static GdkTexture *
buffer_diff_float (GdkColorState *color_state,
const guchar *buf_a,
int stride_a,
const guchar *buf_b,
int stride_b,
int width,
int height,
guint *max_diff_out,
guint *pixels_changed_out)
{
int x, y;
guchar *buf_diff = NULL;
int stride_diff = 0;
GdkTexture *diff = NULL;
float max_diff = 0.0;
guint pixels_changed = 0;
for (y = 0; y < height; y++)
{
const float *row_a = (const float *) (buf_a + y * stride_a);
const float *row_b = (const float *) (buf_b + y * stride_b);
float *row = (float *) (buf_diff + y * stride_diff);
for (x = 0; x < width; x++)
{
int channel;
/* check if the pixels are the same */
if (G_APPROX_VALUE (row_a[4 * x + 0], row_b[4 * x + 0], 1. / 255) &&
G_APPROX_VALUE (row_a[4 * x + 1], row_b[4 * x + 1], 1. / 255) &&
G_APPROX_VALUE (row_a[4 * x + 2], row_b[4 * x + 2], 1. / 255) &&
G_APPROX_VALUE (row_a[4 * x + 3], row_b[4 * x + 3], 1. / 255))
continue;
/* even if they're not literally the same, fully-transparent
* pixels are effectively the same regardless of colour */
if (G_APPROX_VALUE (row_a[4 * x + 3], 0.0, 1. / 255) &&
G_APPROX_VALUE (row_b[4 * x + 3], 0.0, 1. / 255))
continue;
if (diff == NULL)
{
GdkMemoryTextureBuilder *builder;
GBytes *bytes;
stride_diff = 4 * width * sizeof (float);
buf_diff = g_malloc0_n (stride_diff, height);
bytes = g_bytes_new_take (buf_diff, stride_diff * height);
builder = gdk_memory_texture_builder_new ();
gdk_memory_texture_builder_set_width (builder, width);
gdk_memory_texture_builder_set_height (builder, height);
gdk_memory_texture_builder_set_format (builder, GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED);
gdk_memory_texture_builder_set_color_state (builder, color_state);
gdk_memory_texture_builder_set_bytes (builder, bytes);
gdk_memory_texture_builder_set_stride (builder, stride_diff);
diff = gdk_memory_texture_builder_build (builder);
g_object_unref (builder);
row = (float *) (buf_diff + y * stride_diff);
}
/* calculate a difference value for all 4 channels */
for (channel = 0; channel < 4; channel++)
{
float value_a = row_a[4 * x + channel];
float value_b = row_b[4 * x + channel];\
float channel_diff;
channel_diff = ABS (value_a - value_b);
if (channel_diff > max_diff)
max_diff = channel_diff;
channel_diff *= 4; /* emphasize */
if (channel_diff)
channel_diff += 0.5; /* make sure it's visible */
if (channel_diff > 1.0)
channel_diff = 1.0;
row[4 * x + channel] = channel_diff;
}
pixels_changed++;
if (row[4 * x + 0] < 0.5 &&
row[4 * x + 1] < 0.5 &&
row[4 * x + 2] < 0.5)
{
/* alpha only difference, convert to luminance */
row[4 * x + 0] = row[4 * x + 3];
row[4 * x + 1] = row[4 * x + 3];
row[4 * x + 2] = row[4 * x + 3];
}
/* make the pixel fully opaque */
row[4 * x + 3] = 1.0;
}
}
/* Scale the maximum difference we report so it's on the same 0-255
* scale as integer textures */
if (max_diff_out != NULL)
*max_diff_out = max_diff * 255;
if (pixels_changed_out != NULL)
*pixels_changed_out = pixels_changed;
return diff;
}
GdkTexture *
reftest_compare_textures (GdkTexture *texture1,
GdkTexture *texture2,
guint *max_diff_out,
guint *pixels_changed_out,
guint *pixels_out)
{
int w, h, stride;
guchar *data1, *data2;
GdkTextureDownloader *downloader;
GdkColorState *color_state;
GdkTexture *diff;
gboolean high_depth;
w = MAX (gdk_texture_get_width (texture1), gdk_texture_get_width (texture2));
h = MAX (gdk_texture_get_height (texture1), gdk_texture_get_height (texture2));
color_state = gdk_texture_get_color_state (texture1);
downloader = gdk_texture_downloader_new (texture1);
gdk_texture_downloader_set_color_state (downloader, color_state);
high_depth = memory_format_is_high_depth (gdk_texture_get_format (texture1)) ||
memory_format_is_high_depth (gdk_texture_get_format (texture1));
if (high_depth)
{
gdk_texture_downloader_set_format (downloader, GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED);
stride = w * 4 * sizeof (float);
}
else
{
gdk_texture_downloader_set_format (downloader, GDK_MEMORY_DEFAULT);
stride = w * 4 * sizeof (float);
}
data1 = g_malloc_n (stride, h);
gdk_texture_downloader_download_into (downloader, data1, stride);
data2 = g_malloc_n (stride, h);
gdk_texture_downloader_set_texture (downloader, texture2);
gdk_texture_downloader_download_into (downloader, data2, stride);
if (high_depth)
{
diff = buffer_diff_float (color_state,
data1, stride,
data2, stride,
w, h,
max_diff_out, pixels_changed_out);
}
else
{
diff = buffer_diff_u8 (color_state,
data1, stride,
data2, stride,
w, h,
max_diff_out, pixels_changed_out);
}
g_free (data1);
g_free (data2);
if (pixels_out != NULL)
*pixels_out = w * h;
return diff;
}
|