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
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* Quite OK Image (QOI) plug-in
*
* Copyright (C) 2023 Alex S.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <errno.h>
#include <glib/gstdio.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#define QOI_IMPLEMENTATION
#include "qoi.h"
#include "libgimp/stdplugins-intl.h"
#define LOAD_PROC "file-qoi-load"
#define EXPORT_PROC "file-qoi-export"
#define PLUG_IN_BINARY "file-qoi"
#define PLUG_IN_ROLE "gimp-file-qoi"
typedef struct _Qoi Qoi;
typedef struct _QoiClass QoiClass;
struct _Qoi
{
GimpPlugIn parent_instance;
};
struct _QoiClass
{
GimpPlugInClass parent_class;
};
#define QOI_TYPE (qoi_get_type ())
#define QOI(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), QOI_TYPE, Qoi))
GType qoi_get_type (void) G_GNUC_CONST;
static GList * qoi_query_procedures (GimpPlugIn *plug_in);
static GimpProcedure * qoi_create_procedure (GimpPlugIn *plug_in,
const gchar *name);
static GimpValueArray * qoi_load (GimpProcedure *procedure,
GimpRunMode run_mode,
GFile *file,
GimpMetadata *metadata,
GimpMetadataLoadFlags *flags,
GimpProcedureConfig *config,
gpointer run_data);
static GimpValueArray * qoi_export (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GFile *file,
GimpExportOptions *options,
GimpMetadata *metadata,
GimpProcedureConfig *config,
gpointer run_data);
static GimpImage * load_image (GFile *file,
GObject *config,
GimpRunMode run_mode,
GError **error);
static gboolean export_image (GFile *file,
GimpImage *image,
GimpDrawable *drawable,
GError **error);
G_DEFINE_TYPE (Qoi, qoi, GIMP_TYPE_PLUG_IN)
GIMP_MAIN (QOI_TYPE)
DEFINE_STD_SET_I18N
static void
qoi_class_init (QoiClass *klass)
{
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
plug_in_class->query_procedures = qoi_query_procedures;
plug_in_class->create_procedure = qoi_create_procedure;
plug_in_class->set_i18n = STD_SET_I18N;
}
static void
qoi_init (Qoi *qoi)
{
}
static GList *
qoi_query_procedures (GimpPlugIn *plug_in)
{
GList *list = NULL;
list = g_list_append (list, g_strdup (LOAD_PROC));
list = g_list_append (list, g_strdup (EXPORT_PROC));
return list;
}
static GimpProcedure *
qoi_create_procedure (GimpPlugIn *plug_in,
const gchar *name)
{
GimpProcedure *procedure = NULL;
if (! strcmp (name, LOAD_PROC))
{
procedure = gimp_load_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN,
qoi_load, NULL, NULL);
gimp_procedure_set_menu_label (procedure,
N_("Quite OK Image"));
gimp_procedure_set_documentation (procedure,
_("Load file in the QOI file format"),
_("Load file in the QOI file format "
"(Quite OK Image)"),
name);
gimp_procedure_set_attribution (procedure,
"Alex S.",
"Alex S.",
"2023");
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
"image/qoi");
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
"qoi");
gimp_file_procedure_set_magics (GIMP_FILE_PROCEDURE (procedure),
"0,string,qoif");
}
else if (! strcmp (name, EXPORT_PROC))
{
procedure = gimp_export_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN,
FALSE, qoi_export, NULL, NULL);
gimp_procedure_set_image_types (procedure, "*");
gimp_procedure_set_menu_label (procedure, _("Quite OK Image"));
gimp_procedure_set_documentation (procedure,
_("Export image in the QOI file format"),
_("Export image in the QOI file format "
"(Quite OK Image)"),
name);
gimp_procedure_set_attribution (procedure,
"Alex S.",
"Alex S.",
"2023");
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
"image/qoi");
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
"qoi");
gimp_export_procedure_set_capabilities (GIMP_EXPORT_PROCEDURE (procedure),
GIMP_EXPORT_CAN_HANDLE_RGB |
GIMP_EXPORT_CAN_HANDLE_GRAY |
GIMP_EXPORT_CAN_HANDLE_INDEXED |
GIMP_EXPORT_CAN_HANDLE_ALPHA,
NULL, NULL, NULL);
}
return procedure;
}
static GimpValueArray *
qoi_load (GimpProcedure *procedure,
GimpRunMode run_mode,
GFile *file,
GimpMetadata *metadata,
GimpMetadataLoadFlags *flags,
GimpProcedureConfig *config,
gpointer run_data)
{
GimpValueArray *return_vals;
GimpImage *image;
GError *error = NULL;
gegl_init (NULL, NULL);
image = load_image (file, G_OBJECT (config), run_mode, &error);
if (! image)
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
return_vals = gimp_procedure_new_return_values (procedure,
GIMP_PDB_SUCCESS,
NULL);
GIMP_VALUES_SET_IMAGE (return_vals, 1, image);
return return_vals;
}
static GimpValueArray *
qoi_export (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GFile *file,
GimpExportOptions *options,
GimpMetadata *metadata,
GimpProcedureConfig *config,
gpointer run_data)
{
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
GimpExportReturn export = GIMP_EXPORT_IGNORE;
GList *drawables;
GError *error = NULL;
gegl_init (NULL, NULL);
export = gimp_export_options_get_image (options, &image);
drawables = gimp_image_list_layers (image);
if (! export_image (file, image, drawables->data, &error))
status = GIMP_PDB_EXECUTION_ERROR;
if (export == GIMP_EXPORT_EXPORT)
gimp_image_delete (image);
g_list_free (drawables);
return gimp_procedure_new_return_values (procedure, status, error);
}
static GimpImage *
load_image (GFile *file,
GObject *config,
GimpRunMode run_mode,
GError **error)
{
GimpImage *image = NULL;
GimpLayer *layer;
GeglBuffer *buffer;
qoi_desc desc;
void *pixels = NULL;
const Babl *format = NULL;
FILE *fp;
fp = g_fopen (g_file_peek_path (file), "rb");
if (! fp)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Could not open '%s' for reading: %s"),
gimp_file_get_utf8_name (file), g_strerror (errno));
return NULL;
}
fclose (fp);
pixels = qoi_read (g_file_peek_path (file), &desc, 0);
if (! pixels)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Failed to read QOI file"));
return NULL;
}
image = gimp_image_new_with_precision (desc.width, desc.height, GIMP_RGB,
desc.colorspace ?
GIMP_PRECISION_U8_LINEAR :
GIMP_PRECISION_U8_NON_LINEAR);
layer = gimp_layer_new (image, _("Background"), desc.width, desc.height,
(desc.channels == 4) ? GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE,
100, gimp_image_get_default_new_layer_mode (image));
gimp_image_insert_layer (image, layer, NULL, 0);
format = babl_format ((desc.channels == 4) ? "R'G'B'A u8" : "R'G'B' u8");
buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (layer));
gegl_buffer_set (buffer, GEGL_RECTANGLE (0, 0, desc.width, desc.height), 0,
format, pixels, GEGL_AUTO_ROWSTRIDE);
g_object_unref (buffer);
g_free (pixels);
return image;
}
static gboolean
export_image (GFile *file,
GimpImage *image,
GimpDrawable *drawable,
GError **error)
{
GeglBuffer *buffer;
const Babl *format;
qoi_desc desc;
void *pixels = NULL;
gboolean has_alpha;
gint success;
has_alpha = gimp_drawable_has_alpha (drawable);
buffer = gimp_drawable_get_buffer (drawable);
desc.width = gegl_buffer_get_width (buffer);
desc.height = gegl_buffer_get_height (buffer);
desc.channels = has_alpha ? 4 : 3;
switch (gimp_image_get_precision (image))
{
case GIMP_PRECISION_U8_LINEAR:
case GIMP_PRECISION_U16_LINEAR:
case GIMP_PRECISION_U32_LINEAR:
case GIMP_PRECISION_HALF_LINEAR:
case GIMP_PRECISION_FLOAT_LINEAR:
case GIMP_PRECISION_DOUBLE_LINEAR:
desc.colorspace = QOI_LINEAR;
break;
default:
desc.colorspace = QOI_SRGB;
break;
}
gimp_progress_init_printf (_("Exporting '%s'"),
gimp_file_get_utf8_name (file));
format = babl_format (has_alpha ? "R'G'B'A u8" : "R'G'B' u8");
pixels = (guchar *) g_malloc (desc.width * desc.height * desc.channels);
gegl_buffer_get (buffer, GEGL_RECTANGLE (0, 0, desc.width, desc.height),
1.0, format, pixels,
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
success = qoi_write (g_file_peek_path (file), pixels, &desc);
g_object_unref (buffer);
g_free (pixels);
if (! success)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Writing to file '%s' failed: %s"),
gimp_file_get_utf8_name (file), g_strerror (errno));
return FALSE;
}
return TRUE;
}
|