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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimploadprocedure.c
* Copyright (C) 2019 Michael Natterer <mitch@gimp.org>
*
* 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 "gimp.h"
#include "libgimpbase/gimpwire.h" /* FIXME kill this include */
#include "gimploadprocedure.h"
#include "gimppdb_pdb.h"
#include "gimpplugin-private.h"
#include "gimpprocedureconfig-private.h"
#include "libgimp-intl.h"
/**
* GimpLoadProcedure:
*
* A [class@Procedure] subclass that makes it easier to write file load
* procedures.
*
* It automatically adds the standard
*
* ( [enum@RunMode], [iface@Gio.File] )
*
* arguments and the standard
*
* ( [class@Image] )
*
* return value of a load procedure. It is possible to add additional
* arguments.
*
* When invoked via [method@Procedure.run], it unpacks these standard
* arguments and calls @run_func which is a [callback@RunImageFunc]. The
* "args" [struct@ValueArray] of [callback@RunImageFunc] only contains
* additionally added arguments.
*/
typedef struct _GimpLoadProcedurePrivate
{
GimpRunLoadFunc run_func;
gpointer run_data;
GDestroyNotify run_data_destroy;
gboolean handles_raw;
gchar *thumbnail_proc;
} GimpLoadProcedurePrivate;
static void gimp_load_procedure_constructed (GObject *object);
static void gimp_load_procedure_finalize (GObject *object);
static void gimp_load_procedure_install (GimpProcedure *procedure);
static GimpValueArray *
gimp_load_procedure_run (GimpProcedure *procedure,
const GimpValueArray *args);
static GimpProcedureConfig *
gimp_load_procedure_create_config (GimpProcedure *procedure,
GParamSpec **args,
gint n_args);
G_DEFINE_TYPE_WITH_PRIVATE (GimpLoadProcedure, gimp_load_procedure, GIMP_TYPE_FILE_PROCEDURE)
#define parent_class gimp_load_procedure_parent_class
static void
gimp_load_procedure_class_init (GimpLoadProcedureClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpProcedureClass *procedure_class = GIMP_PROCEDURE_CLASS (klass);
object_class->constructed = gimp_load_procedure_constructed;
object_class->finalize = gimp_load_procedure_finalize;
procedure_class->install = gimp_load_procedure_install;
procedure_class->run = gimp_load_procedure_run;
procedure_class->create_config = gimp_load_procedure_create_config;
}
static void
gimp_load_procedure_init (GimpLoadProcedure *procedure)
{
}
static void
gimp_load_procedure_constructed (GObject *object)
{
GimpProcedure *procedure = GIMP_PROCEDURE (object);
G_OBJECT_CLASS (parent_class)->constructed (object);
gimp_procedure_add_file_argument (procedure, "file",
"File",
"The file to load",
GIMP_FILE_CHOOSER_ACTION_OPEN,
FALSE, NULL,
GIMP_PARAM_READWRITE);
gimp_procedure_add_image_return_value (procedure, "image",
"Image",
"Output image",
TRUE,
GIMP_PARAM_READWRITE);
}
static void
gimp_load_procedure_finalize (GObject *object)
{
GimpLoadProcedure *procedure = GIMP_LOAD_PROCEDURE (object);
GimpLoadProcedurePrivate *priv;
priv = gimp_load_procedure_get_instance_private (procedure);
if (priv->run_data_destroy)
priv->run_data_destroy (priv->run_data);
g_clear_pointer (&priv->thumbnail_proc, g_free);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gimp_load_procedure_install (GimpProcedure *procedure)
{
GimpLoadProcedurePrivate *priv;
GimpLoadProcedure *load_proc = GIMP_LOAD_PROCEDURE (procedure);
GimpFileProcedure *file_proc = GIMP_FILE_PROCEDURE (procedure);
const gchar *mime_types;
gint priority;
priv = gimp_load_procedure_get_instance_private (load_proc);
GIMP_PROCEDURE_CLASS (parent_class)->install (procedure);
_gimp_pdb_set_file_proc_load_handler (gimp_procedure_get_name (procedure),
gimp_file_procedure_get_extensions (file_proc),
gimp_file_procedure_get_prefixes (file_proc),
gimp_file_procedure_get_magics (file_proc));
if (gimp_file_procedure_get_handles_remote (file_proc))
_gimp_pdb_set_file_proc_handles_remote (gimp_procedure_get_name (procedure));
mime_types = gimp_file_procedure_get_mime_types (file_proc);
if (mime_types)
_gimp_pdb_set_file_proc_mime_types (gimp_procedure_get_name (procedure),
mime_types);
priority = gimp_file_procedure_get_priority (file_proc);
if (priority != 0)
_gimp_pdb_set_file_proc_priority (gimp_procedure_get_name (procedure),
priority);
if (priv->handles_raw)
_gimp_pdb_set_file_proc_handles_raw (gimp_procedure_get_name (procedure));
if (priv->thumbnail_proc)
_gimp_pdb_set_file_proc_thumbnail_loader (gimp_procedure_get_name (procedure),
priv->thumbnail_proc);
}
#define ARG_OFFSET 2
static GimpValueArray *
gimp_load_procedure_run (GimpProcedure *procedure,
const GimpValueArray *args)
{
GimpLoadProcedurePrivate *priv;
GimpPlugIn *plug_in;
GimpLoadProcedure *load_proc = GIMP_LOAD_PROCEDURE (procedure);
GimpValueArray *remaining;
GimpValueArray *return_values;
GimpProcedureConfig *config;
GimpImage *image = NULL;
GimpMetadata *metadata = NULL;
gchar *mimetype = NULL;
GimpMetadataLoadFlags flags = GIMP_METADATA_LOAD_ALL;
GimpPDBStatusType status = GIMP_PDB_EXECUTION_ERROR;
GimpRunMode run_mode;
GFile *file;
gint i;
priv = gimp_load_procedure_get_instance_private (load_proc);
run_mode = GIMP_VALUES_GET_ENUM (args, 0);
file = GIMP_VALUES_GET_FILE (args, 1);
remaining = gimp_value_array_new (gimp_value_array_length (args) - ARG_OFFSET);
for (i = ARG_OFFSET; i < gimp_value_array_length (args); i++)
{
GValue *value = gimp_value_array_index (args, i);
gimp_value_array_append (remaining, value);
}
config = _gimp_procedure_create_run_config (procedure);
mimetype = (gchar *) gimp_file_procedure_get_mime_types (GIMP_FILE_PROCEDURE (procedure));
if (mimetype != NULL)
{
char *delim;
mimetype = g_strdup (mimetype);
mimetype = g_strstrip (mimetype);
delim = strstr (mimetype, ",");
if (delim)
*delim = '\0';
/* Though docs only writes about the list being comma-separated, our
* code apparently also split by spaces.
*/
delim = strstr (mimetype, " ");
if (delim)
*delim = '\0';
delim = strstr (mimetype, "\t");
if (delim)
*delim = '\0';
metadata = gimp_metadata_load_from_file (file, NULL);
g_free (mimetype);
}
else
{
flags = GIMP_METADATA_LOAD_NONE;
}
if (metadata == NULL)
metadata = gimp_metadata_new ();
_gimp_procedure_config_begin_run (config, image, run_mode, remaining, NULL);
return_values = priv->run_func (procedure, run_mode,
file, metadata, &flags,
config, priv->run_data);
if (return_values != NULL &&
gimp_value_array_length (return_values) > 0 &&
G_VALUE_HOLDS_ENUM (gimp_value_array_index (return_values, 0)))
status = GIMP_VALUES_GET_ENUM (return_values, 0);
_gimp_procedure_config_end_run (config, status);
if (status == GIMP_PDB_SUCCESS)
{
if (gimp_value_array_length (return_values) < 2 ||
! GIMP_VALUE_HOLDS_IMAGE (gimp_value_array_index (return_values, 1)))
{
GError *error = NULL;
status = GIMP_PDB_EXECUTION_ERROR;
g_set_error (&error, GIMP_PLUG_IN_ERROR, 0,
_("This file loading plug-in returned SUCCESS as a status without an image. "
"This is a bug in the plug-in code. Contact the plug-in developer."));
gimp_value_array_unref (return_values);
return_values = gimp_procedure_new_return_values (procedure, status, error);
}
else
{
image = GIMP_VALUES_GET_IMAGE (return_values, 1);
}
}
if (image != NULL && metadata != NULL && flags != GIMP_METADATA_LOAD_NONE)
_gimp_image_metadata_load_finish (image, NULL, metadata, flags);
/* This is debug printing to help plug-in developers figure out best
* practices.
*/
plug_in = gimp_procedure_get_plug_in (procedure);
if (G_OBJECT (config)->ref_count > 1 &&
_gimp_plug_in_manage_memory_manually (plug_in))
g_printerr ("%s: ERROR: the GimpExportProcedureConfig object was refed "
"by plug-in, it MUST NOT do that!\n", G_STRFUNC);
g_object_unref (config);
g_clear_object (&metadata);
gimp_value_array_unref (remaining);
return return_values;
}
static GimpProcedureConfig *
gimp_load_procedure_create_config (GimpProcedure *procedure,
GParamSpec **args,
gint n_args)
{
if (n_args > ARG_OFFSET)
{
args += ARG_OFFSET;
n_args -= ARG_OFFSET;
}
else
{
args = NULL;
n_args = 0;
}
return GIMP_PROCEDURE_CLASS (parent_class)->create_config (procedure,
args,
n_args);
}
/* public functions */
/**
* gimp_load_procedure_new:
* @plug_in: a #GimpPlugIn.
* @name: the new procedure's name.
* @proc_type: the new procedure's #GimpPDBProcType.
* @run_func: the run function for the new procedure.
* @run_data: user data passed to @run_func.
* @run_data_destroy: (nullable): free function for @run_data, or %NULL.
*
* Creates a new load procedure named @name which will call @run_func
* when invoked.
*
* See gimp_procedure_new() for information about @proc_type.
*
* Returns: (transfer full): a new #GimpProcedure.
*
* Since: 3.0
**/
GimpProcedure *
gimp_load_procedure_new (GimpPlugIn *plug_in,
const gchar *name,
GimpPDBProcType proc_type,
GimpRunLoadFunc run_func,
gpointer run_data,
GDestroyNotify run_data_destroy)
{
GimpLoadProcedure *procedure;
GimpLoadProcedurePrivate *priv;
g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), NULL);
g_return_val_if_fail (gimp_is_canonical_identifier (name), NULL);
g_return_val_if_fail (proc_type != GIMP_PDB_PROC_TYPE_INTERNAL, NULL);
g_return_val_if_fail (proc_type != GIMP_PDB_PROC_TYPE_PERSISTENT, NULL);
g_return_val_if_fail (run_func != NULL, NULL);
procedure = g_object_new (GIMP_TYPE_LOAD_PROCEDURE,
"plug-in", plug_in,
"name", name,
"procedure-type", proc_type,
NULL);
priv = gimp_load_procedure_get_instance_private (procedure);
priv->run_func = run_func;
priv->run_data = run_data;
priv->run_data_destroy = run_data_destroy;
return GIMP_PROCEDURE (procedure);
}
/**
* gimp_load_procedure_set_handles_raw:
* @procedure: A load procedure object.
* @handles_raw: The procedure's handles raw flag.
*
* Registers a load procedure as capable of handling raw digital camera loads.
*
* Note that you cannot call this function on [class@VectorLoadProcedure]
* subclass objects.
*
* Since: 3.0
**/
void
gimp_load_procedure_set_handles_raw (GimpLoadProcedure *procedure,
gint handles_raw)
{
GimpLoadProcedurePrivate *priv;
g_return_if_fail (GIMP_IS_LOAD_PROCEDURE (procedure));
priv = gimp_load_procedure_get_instance_private (procedure);
priv->handles_raw = handles_raw;
}
/**
* gimp_load_procedure_get_handles_raw:
* @procedure: A load procedure object.
*
* Returns the procedure's 'handles raw' flag as set with
* [method@GimpLoadProcedure.set_handles_raw].
*
* Returns: The procedure's 'handles raw' flag.
*
* Since: 3.0
**/
gint
gimp_load_procedure_get_handles_raw (GimpLoadProcedure *procedure)
{
GimpLoadProcedurePrivate *priv;
g_return_val_if_fail (GIMP_IS_LOAD_PROCEDURE (procedure), 0);
priv = gimp_load_procedure_get_instance_private (procedure);
return priv->handles_raw;
}
/**
* gimp_load_procedure_set_thumbnail_loader:
* @procedure: A load procedure object.
* @thumbnail_proc: The name of the thumbnail load procedure.
*
* Associates a thumbnail loader with a file load procedure.
*
* Some file formats allow for embedded thumbnails, other file formats
* contain a scalable image or provide the image data in different
* resolutions. A file plug-in for such a format may register a
* special procedure that allows GIMP to load a thumbnail preview of
* the image. This procedure is then associated with the standard
* load procedure using this function.
*
* Since: 3.0
**/
void
gimp_load_procedure_set_thumbnail_loader (GimpLoadProcedure *procedure,
const gchar *thumbnail_proc)
{
GimpLoadProcedurePrivate *priv;
g_return_if_fail (GIMP_IS_LOAD_PROCEDURE (procedure));
priv = gimp_load_procedure_get_instance_private (procedure);
g_free (priv->thumbnail_proc);
priv->thumbnail_proc = g_strdup (thumbnail_proc);
}
/**
* gimp_load_procedure_get_thumbnail_loader:
* @procedure: A load procedure object.
*
* Returns the procedure's thumbnail loader procedure as set with
* [method@GimpLoadProcedure.set_thumbnail_loader].
*
* Returns: The procedure's thumbnail loader procedure
*
* Since: 3.0
**/
const gchar *
gimp_load_procedure_get_thumbnail_loader (GimpLoadProcedure *procedure)
{
GimpLoadProcedurePrivate *priv;
g_return_val_if_fail (GIMP_IS_LOAD_PROCEDURE (procedure), NULL);
priv = gimp_load_procedure_get_instance_private (procedure);
return priv->thumbnail_proc;
}
|