texture: Rework error enum

1. Change INSUFFICIENT_MEMORY to TOO_LARGE
   GTK crashes on insufficient memory, we don't emit GErrors.

2. Split UNSUPPORTED into UNSUPPORTED_CONTENT and UNSUPPORTED_FORMAT
   So we know if you need to find an RPM with a loader or curse and
   the weird file.

3. Translate error messages, they are meant for end users.
This commit is contained in:
Benjamin Otte 2021-09-17 03:25:35 +02:00
parent e58f70d7bb
commit b271a94f92
4 changed files with 35 additions and 38 deletions

View File

@ -45,17 +45,22 @@ GQuark gdk_texture_error_quark (void);
/** /**
* GdkTextureError: * GdkTextureError:
* @GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY: Not enough memory to handle this image * @GDK_TEXTURE_ERROR_TOO_LARGE: Not enough memory to handle this image
* @GDK_TEXTURE_ERROR_CORRUPT_IMAGE: The image data appears corrupted * @GDK_TEXTURE_ERROR_CORRUPT_IMAGE: The image data appears corrupted
* @GDK_TEXTURE_ERROR_UNSUPPORTED: The image format is not supported * @GDK_TEXTURE_ERROR_UNSUPPORTED_CONTENT: The image contains features
* that cannot be loaded
* @GDK_TEXTURE_ERROR_UNSUPPORTED_FORMAT: The image format is not supported
* *
* Possible errors that can be returned by `GdkTexture` constructors. * Possible errors that can be returned by `GdkTexture` constructors.
*
* Since: 4.6
*/ */
typedef enum typedef enum
{ {
GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY, GDK_TEXTURE_ERROR_TOO_LARGE,
GDK_TEXTURE_ERROR_CORRUPT_IMAGE, GDK_TEXTURE_ERROR_CORRUPT_IMAGE,
GDK_TEXTURE_ERROR_UNSUPPORTED, GDK_TEXTURE_ERROR_UNSUPPORTED_CONTENT,
GDK_TEXTURE_ERROR_UNSUPPORTED_FORMAT,
} GdkTextureError; } GdkTextureError;
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL

View File

@ -19,8 +19,10 @@
#include "gdkjpegprivate.h" #include "gdkjpegprivate.h"
#include "gdkintl.h"
#include "gdktexture.h" #include "gdktexture.h"
#include "gdkmemorytextureprivate.h" #include "gdkmemorytextureprivate.h"
#include <jpeglib.h> #include <jpeglib.h>
#include <jerror.h> #include <jerror.h>
#include <setjmp.h> #include <setjmp.h>
@ -53,10 +55,8 @@ fatal_error_handler (j_common_ptr cinfo)
if (errmgr->error && *errmgr->error == NULL) if (errmgr->error && *errmgr->error == NULL)
g_set_error (errmgr->error, g_set_error (errmgr->error,
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR,
cinfo->err->msg_code == JERR_OUT_OF_MEMORY GDK_TEXTURE_ERROR_CORRUPT_IMAGE,
? GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY _("Error interpreting JPEG image file (%s)"), buffer);
: GDK_TEXTURE_ERROR_CORRUPT_IMAGE,
"Error interpreting JPEG image file (%s)", buffer);
siglongjmp (errmgr->setjmp_buffer, 1); siglongjmp (errmgr->setjmp_buffer, 1);
@ -213,17 +213,17 @@ gdk_load_jpeg (GBytes *input_bytes,
break; break;
default: default:
g_set_error (error, g_set_error (error,
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED_CONTENT,
"Unsupported colorspace in jpeg (%d)", info.out_color_space); _("Unsupported JPEG colorspace (%d)"), info.out_color_space);
jpeg_destroy_decompress (&info); jpeg_destroy_decompress (&info);
return NULL; return NULL;
} }
if (!data) if (!data)
{ {
g_set_error_literal (error, g_set_error (error,
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_TOO_LARGE,
"Not enough memory to load jpeg"); _("Not enough memory for image size %ux%u"), width, height);
jpeg_destroy_decompress (&info); jpeg_destroy_decompress (&info);
return NULL; return NULL;
} }

View File

@ -19,9 +19,10 @@
#include "gdkpngprivate.h" #include "gdkpngprivate.h"
#include "gdkintl.h"
#include "gdkmemorytextureprivate.h"
#include "gdktexture.h" #include "gdktexture.h"
#include "gdktextureprivate.h" #include "gdktextureprivate.h"
#include "gdkmemorytextureprivate.h"
#include "gsk/ngl/fp16private.h" #include "gsk/ngl/fp16private.h"
#include <png.h> #include <png.h>
#include <stdio.h> #include <stdio.h>
@ -113,7 +114,7 @@ png_simple_error_callback (png_structp png,
if (error && !*error) if (error && !*error)
g_set_error (error, g_set_error (error,
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE,
"Error reading png (%s)", error_msg); _("Error reading png (%s)"), error_msg);
longjmp (png_jmpbuf (png), 1); longjmp (png_jmpbuf (png), 1);
} }
@ -320,22 +321,11 @@ gdk_load_png (GBytes *bytes,
png_malloc_callback, png_malloc_callback,
png_free_callback); png_free_callback);
if (png == NULL) if (png == NULL)
{ g_error ("Out of memory");
g_set_error (error,
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY,
"Failed to parse png image");
return NULL;
}
info = png_create_info_struct (png); info = png_create_info_struct (png);
if (info == NULL) if (info == NULL)
{ g_error ("Out of memory");
png_destroy_read_struct (&png, NULL, NULL);
g_set_error (error,
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY,
"Failed to parse png image");
return NULL;
}
png_set_read_fn (png, &io, png_read_func); png_set_read_fn (png, &io, png_read_func);
@ -385,8 +375,8 @@ gdk_load_png (GBytes *bytes,
{ {
png_destroy_read_struct (&png, &info, NULL); png_destroy_read_struct (&png, &info, NULL);
g_set_error (error, g_set_error (error,
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED_CONTENT,
"Failed to parse png image"); _("Failed to parse png image"));
return NULL; return NULL;
} }
@ -437,9 +427,9 @@ gdk_load_png (GBytes *bytes,
g_free (buffer); g_free (buffer);
g_free (row_pointers); g_free (row_pointers);
png_destroy_read_struct (&png, &info, NULL); png_destroy_read_struct (&png, &info, NULL);
g_set_error_literal (error, g_set_error (error,
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_TOO_LARGE,
"Not enough memory to load png"); _("Not enough memory for image size %ux%u"), width, height);
return NULL; return NULL;
} }

View File

@ -19,9 +19,11 @@
#include "gdktiffprivate.h" #include "gdktiffprivate.h"
#include "gdkintl.h"
#include "gdkmemorytextureprivate.h"
#include "gdktexture.h" #include "gdktexture.h"
#include "gdktextureprivate.h" #include "gdktextureprivate.h"
#include "gdkmemorytextureprivate.h"
#include <tiffio.h> #include <tiffio.h>
/* Our main interest in tiff as an image format is that it is /* Our main interest in tiff as an image format is that it is
@ -374,7 +376,7 @@ load_fallback (TIFF *tif,
{ {
g_set_error_literal (error, g_set_error_literal (error,
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE,
"Failed to load RGB data from TIFF file"); _("Failed to load RGB data from TIFF file"));
g_free (data); g_free (data);
return NULL; return NULL;
} }
@ -471,8 +473,8 @@ gdk_load_tiff (GBytes *input_bytes,
if (!data) if (!data)
{ {
g_set_error (error, g_set_error (error,
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_TOO_LARGE,
"Not enough memory to read tiff"); _("Not enough memory for image size %ux%u"), width, height);
TIFFClose (tif); TIFFClose (tif);
return NULL; return NULL;
} }
@ -484,7 +486,7 @@ gdk_load_tiff (GBytes *input_bytes,
{ {
g_set_error (error, g_set_error (error,
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE,
"Reading data failed at row %d", y); _("Reading data failed at row %d"), y);
TIFFClose (tif); TIFFClose (tif);
g_free (data); g_free (data);
return NULL; return NULL;