fileops.pdb 18.2 KB
Newer Older
1
# GIMP - The GNU Image Manipulation Program
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# Copyright (C) 1995, 1996, 1997 Spencer Kimball and Peter Mattis
# Copyright (C) 1997 Josh MacDonald

# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# "Perlized" from C source by Manish Singh <yosh@gimp.org>

sub file_load {
22
    $blurb = 'Loads an image file by invoking the right load handler.';
23 24

    $help = <<'HELP';
25 26 27 28 29 30
This procedure invokes the correct file load handler using magic if possible,
and falling back on the file's extension and/or prefix if not. The name of
the file to load is typically a full pathname, and the name entered is what
the user actually typed before prepending a directory path. The reason for
this is that if the user types http://www.xcf/~gimp/ he wants to fetch a URL,
and the full pathname will not look like a URL."
31 32
HELP

33
    &josh_pdb_misc('1997');
34 35

    @inargs = (
36 37 38
        { name => 'run_mode',
          type => 'enum GimpRunMode (no GIMP_RUN_WITH_LAST_VALS)',
          desc => 'The run mode' },
39
        { name => 'filename', type => 'string', allow_non_utf8 => 1,
40
          desc => 'The name of the file to load' },
41
        { name => 'raw_filename', type => 'string', allow_non_utf8 => 1,
42
          desc => 'The name as entered by the user' }
43 44
    );

45
    @outargs = (
46 47
        { name => 'image', type => 'image',
          desc => 'The output image' }
48
    );
49 50

    %invoke = (
51 52
        no_marshalling => 1,
        code => <<'CODE'
53
{
54 55 56 57 58 59
  GValueArray         *new_args;
  GValueArray         *return_vals;
  GimpPlugInProcedure *file_proc;
  GimpProcedure       *proc;
  gchar               *uri;
  gint                 i;
60

61
  uri = file_utils_filename_to_uri (gimp,
62
                                    g_value_get_string (&args->values[1]),
63
                                    NULL);
64 65

  if (! uri)
66
    return gimp_procedure_get_return_values (procedure, FALSE);
67

68
  file_proc =
69
    file_procedure_find (gimp->plug_in_manager->load_procs, uri, NULL);
70 71

  g_free (uri);
Michael Natterer's avatar
Michael Natterer committed
72 73

  if (! file_proc)
74
    return gimp_procedure_get_return_values (procedure, FALSE);
75

76
  proc = GIMP_PROCEDURE (file_proc);
77

78
  new_args = gimp_procedure_get_arguments (proc);
79 80

  for (i = 0; i < 3; i++)
81
    g_value_transform (&args->values[i], &new_args->values[i]);
82 83

  for (i = 3; i < proc->num_args; i++)
84
    if (G_IS_PARAM_SPEC_STRING (proc->args[i]))
85
      g_value_set_static_string (&new_args->values[i], "");
86

87 88 89 90 91
  return_vals =
    gimp_pdb_execute_procedure_by_name_args (gimp->pdb,
                                             context, progress,
                                             GIMP_OBJECT (proc)->name,
                                             new_args);
92

93
  g_value_array_free (new_args);
94 95

  return return_vals;
96 97 98 99 100
}
CODE
    );
}

101
sub file_load_layer {
102
    $blurb = 'Loads an image file as a layer for an existing image.';
103 104 105

    $help = <<'HELP';
This procedure behaves like the file-load procedure but opens the specified
106 107
image as a layer for an existing image. The returned layer needs to be
added to the existing image with gimp_image_add_layer().
108 109
HELP

110
    &neo_pdb_misc('2005', '2.4');
111 112

    @inargs = (
113 114 115 116 117
        { name => 'run_mode',
          type => 'enum GimpRunMode (no GIMP_RUN_WITH_LAST_VALS)',
          desc => 'The run mode' },
        { name => 'image', type => 'image',
          desc => 'Destination image' },
118
        { name => 'filename', type => 'string', allow_non_utf8 => 1,
119
          desc => 'The name of the file to load' }
120 121 122
    );

    @outargs = (
123 124
        { name => 'layer', type => 'layer',
          desc => 'The layer created when loading the image file' }
125 126 127
    );

    %invoke = (
128
        code => <<'CODE'
129
{
130
  gchar *uri = file_utils_filename_to_uri (gimp, filename, NULL);
131

132 133
  if (uri)
    {
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
      GList             *layers;
      GimpPDBStatusType  status;

      layers = file_open_layers (gimp, context, progress,
                                 image, FALSE,
                                 uri, run_mode, NULL, &status, NULL);

      if (layers)
        {
          layer = layers->data;
          g_list_free (layers);
        }
      else
        success = FALSE;
    }
  else
    success = FALSE;
}
CODE
    );
}

sub file_load_layers {
    $blurb = 'Loads an image file as layers for an existing image.';

    $help = <<'HELP';
This procedure behaves like the file-load procedure but opens the specified
image as layers for an existing image. The returned layers needs to be
added to the existing image with gimp_image_add_layer().
HELP

    &mitch_pdb_misc('2006', '2.4');

    @inargs = (
168 169 170 171 172
        { name => 'run_mode',
          type => 'enum GimpRunMode (no GIMP_RUN_WITH_LAST_VALS)',
          desc => 'The run mode' },
        { name => 'image', type => 'image',
          desc => 'Destination image' },
173
        { name => 'filename', type => 'string', allow_non_utf8 => 1,
174
          desc => 'The name of the file to load' }
175 176 177
    );

    @outargs = (
178 179 180 181
        { name => 'layer_ids', type => 'int32array',
          desc => 'The list of loaded layers',
          array => { name => 'num_layers',
                     desc => 'The number of loaded layers' } }
182 183 184
    );

    %invoke = (
185
        code => <<'CODE'
186
{
187
  gchar *uri = file_utils_filename_to_uri (gimp, filename, NULL);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

  if (uri)
    {
      GList             *layers;
      GimpPDBStatusType  status;

      layers = file_open_layers (gimp, context, progress,
                                 image, FALSE,
                                 uri, run_mode, NULL, &status, NULL);

      if (layers)
        {
          GList *list;
          gint i;

          num_layers = g_list_length (layers);

          layer_ids = g_new (gint32, num_layers);
206

207 208 209 210
          for (i = 0, list = layers;
               i < num_layers;
               i++, list = g_list_next (list))
            layer_ids[i] = gimp_item_get_ID (GIMP_ITEM (list->data));
211

212 213 214
          g_list_free (layers);
        }
      else
215 216 217 218
        success = FALSE;
    }
  else
    success = FALSE;
219 220 221 222 223
}
CODE
    );
}

224 225 226 227 228 229 230 231 232 233 234 235
sub file_save {
    $blurb = 'Saves a file by extension.';

    $help = <<'HELP';
This procedure invokes the correct file save handler according to the file's
extension and/or prefix. The name of the file to save is typically a full
pathname, and the name entered is what the user actually typed before
prepending a directory path. The reason for this is that if the user types
http://www.xcf/~gimp/ she wants to fetch a URL, and the full pathname will not
look like a URL.
HELP

236
    &josh_pdb_misc('1997');
237 238

    @inargs = (
239 240 241 242 243 244
        { name => 'run_mode', type => 'enum GimpRunMode',
          desc => 'The run mode' },
        { name => 'image', type => 'image',
          desc => 'Input image' },
        { name => 'drawable', type => 'drawable',
          desc => 'Drawable to save' },
245
        { name => 'filename', type => 'string', allow_non_utf8 => 1,
246
          desc => 'The name of the file to save the image in' },
247
        { name => 'raw_filename', type => 'string', allow_non_utf8 => 1,
248
          desc => 'The name as entered by the user' }
249 250 251
    );

    %invoke = (
252 253 254
        headers => [ qw(<string.h>) ],
        no_marshalling => 1,
        code => <<'CODE'
255
{
256 257 258 259 260 261
  GValueArray         *new_args;
  GValueArray         *return_vals;
  GimpPlugInProcedure *file_proc;
  GimpProcedure       *proc;
  gchar               *uri;
  gint                 i;
262

263
  uri = file_utils_filename_to_uri (gimp,
264
                                    g_value_get_string (&args->values[3]),
265
                                    NULL);
266 267

  if (! uri)
268
    return gimp_procedure_get_return_values (procedure, FALSE);
269

270
  file_proc =
271
    file_procedure_find (gimp->plug_in_manager->save_procs, uri, NULL);
272 273

  g_free (uri);
Michael Natterer's avatar
Michael Natterer committed
274

275
  if (! file_proc)
276
    return gimp_procedure_get_return_values (procedure, FALSE);
277

278
  proc = GIMP_PROCEDURE (file_proc);
279

280
  new_args = gimp_procedure_get_arguments (proc);
281 282

  for (i = 0; i < 5; i++)
283
    g_value_transform (&args->values[i], &new_args->values[i]);
284

285
  for (i = 5; i < proc->num_args; i++)
286 287
    if (G_IS_PARAM_SPEC_STRING (proc->args[i]))
      g_value_set_static_string (&new_args->values[i], "");
288

289 290 291 292
  return_vals = gimp_pdb_execute_procedure_by_name_args (gimp->pdb,
                                                         context, progress,
                                                         GIMP_OBJECT (proc)->name,
                                                         new_args);
293

294
  g_value_array_free (new_args);
295 296 297 298 299 300 301

  return return_vals;
}
CODE
    );
}

302 303 304 305 306 307 308 309
sub file_load_thumbnail {
    $blurb = 'Loads the thumbnail for a file.';

    $help = <<'HELP';
This procedure tries to load a thumbnail that belongs to the file with
the given filename. This name is a full pathname. The returned data is 
an array of colordepth 3 (RGB), regardless of the image type. Width and 
height of the thumbnail are also returned. Don't use this function if 
310
you need a thumbnail of an already opened image, use gimp_image_thumbnail()
311 312 313 314
instead.
HELP

    $author = $copyright = 'Adam D. Moss, Sven Neumann';
315
    $date = '1999-2003';
316 317

    @inargs = (
318
        { name => 'filename', type => 'string', allow_non_utf8 => 1,
319
          desc => 'The name of the file that owns the thumbnail to load' }
320 321
    );

322
    @outargs = (
323 324 325 326 327 328
        { name => 'width', type => 'int32',
          desc => 'The width of the thumbnail' },
        { name => 'height', type => 'int32',
          desc => 'The height of the thumbnail' },
        { name => 'thumb_data', type => 'int8array',
          desc => 'The thumbnail data',
329
          array =>  { name => 'thumb_data_count',
330
                      desc => 'The number of bytes in thumbnail data' } }
331 332 333
    );

    %invoke = (
334
        code => <<'CODE'
335
{
336
  GdkPixbuf *pixbuf = file_utils_load_thumbnail (filename);
337

338
  if (pixbuf)
339
    {
340 341
      width            = gdk_pixbuf_get_width (pixbuf);
      height           = gdk_pixbuf_get_height (pixbuf);
342 343 344
      thumb_data_count = 3 * width * height;
      thumb_data       = g_memdup (gdk_pixbuf_get_pixels (pixbuf),
                                   thumb_data_count);
345

346
      g_object_unref (pixbuf);
347 348
    }
  else
349
    success = FALSE;
350 351 352 353 354 355 356 357 358
}
CODE
    );
}

sub file_save_thumbnail {
    $blurb = 'Saves a thumbnail for the given image';

    $help = <<'HELP';
359 360 361 362 363 364
This procedure saves a thumbnail for the given image according to the
Free Desktop Thumbnail Managing Standard. The thumbnail is saved so
that it belongs to the file with the given filename. This means you
have to save the image under this name first, otherwise this procedure
will fail.  This procedure may become useful if you want to
explicitely save a thumbnail with a file.
365 366
HELP

367
    &josh_pdb_misc('1997');
368 369

    @inargs = (
370 371
        { name => 'image', type => 'image',
          desc => 'The image' },
372
        { name => 'filename', type => 'string', allow_non_utf8 => 1,
373
          desc => 'The name of the file the thumbnail belongs to' },
374 375 376
    );

    %invoke = (
377
        code => <<'CODE'
378
{
379
  success = file_utils_save_thumbnail (image, filename);
380 381 382 383 384
}
CODE
    );
}

385 386 387 388 389 390 391
sub temp_name {
    $blurb = 'Generates a unique filename.';

    $help = <<'HELP';
Generates a unique filename using the temp path supplied in the user's gimprc.
HELP

392
    &josh_pdb_misc('1997');
393 394

    @inargs = (
395
        { name => 'extension', type => 'string', allow_non_utf8 => 1,
396
          desc => 'The extension the file will have' }
397 398 399
    );

    @outargs = (
400 401
        { name => 'name', type => 'string',
          desc => 'The new temp filename' }
402 403 404
    );

    %invoke = (
405
        code => <<'CODE'
406
{
407
  name = gimp_get_temp_filename (gimp, extension);
408
}
409 410 411 412 413 414 415 416 417
CODE
    );
}

sub register_magic_load_handler {
    $blurb = 'Registers a file load handler procedure.';

    $help = <<'HELP';
Registers a procedural database procedure to be called to load files of a 
418
particular file format using magic file information.
419 420 421 422
HELP

    &std_pdb_misc;

423
    @inargs = (
424
        { name => 'procedure_name', type => 'string', non_empty => 1,
425
          desc => 'The name of the procedure to be used for loading' },
426
        { name => 'extensions', type => 'string', no_validate => 1,
427 428
          desc => 'comma separated list of extensions this handler
                   can load (i.e. "jpg,jpeg")' },
429
        { name => 'prefixes', type => 'string', no_validate => 1,
430 431
          desc => 'comma separated list of prefixes this handler
                   can load (i.e. "http:,ftp:")' },
432
        { name => 'magics', type => 'string', no_validate => 1,
433
          desc => 'comma separated list of magic file information
434 435
                   this handler can load (i.e. "0,string,GIF")' }
    );
436 437

    %invoke = (
438
        code => <<'CODE'
439
{
440 441
  gchar *canonical = gimp_canonicalize_identifier (procedure_name);

442 443 444
  success = gimp_plug_in_manager_register_load_handler (gimp->plug_in_manager,
                                                        canonical,
                                                        extensions, prefixes, magics);
445 446

  g_free (canonical);
447 448 449 450 451 452 453 454 455 456
}
CODE
    );
}

sub register_load_handler {
    $blurb = 'Registers a file load handler procedure.';

    $help = <<'HELP';
Registers a procedural database procedure to be called to load files of a
457
particular file format.
458 459 460 461
HELP

    &std_pdb_misc;

462
    @inargs = (
463
        { name => 'procedure_name', type => 'string', non_empty => 1,
464
          desc => 'The name of the procedure to be used for loading' },
465
        { name => 'extensions', type => 'string', no_validate => 1,
466 467
          desc => 'comma separated list of extensions this handler
                   can load (i.e. "jpg,jpeg")' },
468
        { name => 'prefixes', type => 'string', no_validate => 1,
469 470
          desc => 'comma separated list of prefixes this handler
                   can load (i.e. "http:,ftp:")' }
471
    );
472

473

474
    %invoke = (
475
        code => <<'CODE'
476
{
477 478
  gchar *canonical = gimp_canonicalize_identifier (procedure_name);

479 480 481
  success = gimp_plug_in_manager_register_load_handler (gimp->plug_in_manager,
                                                        canonical,
                                                        extensions, prefixes, NULL);
482 483

  g_free (canonical);
484 485
}
CODE
486 487 488 489 490 491 492 493
    );
}

sub register_save_handler {
    $blurb = 'Registers a file save handler procedure.';

    $help = <<'HELP';
Registers a procedural database procedure to be called to save files in a
494
particular file format.
495 496 497 498
HELP

    &std_pdb_misc;

499
    @inargs = (
500
        { name => 'procedure_name', type => 'string', non_empty => 1,
501
          desc => 'The name of the procedure to be used for saving' },
502
        { name => 'extensions', type => 'string', no_validate => 1,
503 504
          desc => 'comma separated list of extensions this handler
                   can save (i.e. "jpg,jpeg")' },
505
        { name => 'prefixes', type => 'string', no_validate => 1,
506 507
          desc => 'comma separated list of prefixes this handler
                   can save (i.e. "http:,ftp:")' }
508
    );
509 510

    %invoke = (
511
        code => <<'CODE'
512
{
513
  gchar *canonical = gimp_canonicalize_identifier (procedure_name);
514

515 516 517
  success = gimp_plug_in_manager_register_save_handler (gimp->plug_in_manager,
                                                        canonical,
                                                        extensions, prefixes);
518 519

  g_free (canonical);
520 521 522 523 524
}
CODE
    );
}

525 526 527 528 529 530 531 532 533
sub register_file_handler_mime {
    $blurb = 'Associates a MIME type with a file handler procedure.';

    $help = <<'HELP';
Registers a MIME type for a file handler procedure. This allows GIMP
to determine the MIME type of the file opened or saved using this
procedure.
HELP

534
    &neo_pdb_misc('2004', '2.2');
535 536

    @inargs = (
537
        { name => 'procedure_name', type => 'string', non_empty => 1,
538 539 540
          desc => "The name of the procedure to associate a MIME type with." },
        { name => 'mime_type', type => 'string',
          desc => "A single MIME type, like for example \"image/jpeg\"."     }
541 542 543
    );

    %invoke = (
544
        code => <<'CODE'
545
{
546
  gchar *canonical = gimp_canonicalize_identifier (procedure_name);
547

548 549
  success = gimp_plug_in_manager_register_mime_type (gimp->plug_in_manager,
                                                     canonical, mime_type);
550 551

  g_free (canonical);
552 553 554 555 556
}
CODE
    );
}

557 558 559 560
sub register_thumbnail_loader {
    $blurb = 'Associates a thumbnail loader with a file load procedure.';

    $help = <<'HELP';
561 562 563 564 565 566
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.
567 568
HELP

569
    &neo_pdb_misc('2004', '2.2');
570 571

    @inargs = (
572
        { name => 'load_proc', type => 'string', non_empty => 1,
573
          desc => "The name of the procedure the thumbnail loader with." },
574
        { name => 'thumb_proc', type => 'string', non_empty => 1,
575
          desc => "The name of the thumbnail load procedure." }
576 577
    );
    %invoke = (
578
        code => <<'CODE'
579
{
580
  gchar *canonical = gimp_canonicalize_identifier (load_proc);
581

582 583
  success = gimp_plug_in_manager_register_thumb_loader (gimp->plug_in_manager,
                                                        canonical, thumb_proc);
584 585

  g_free (canonical);
586 587 588 589 590
}
CODE
    );
}

591

592 593 594
@headers = qw("libgimpbase/gimpbase.h"
              "libgimpconfig/gimpconfig.h"
              "core/gimp.h"
595
              "core/gimp-utils.h"
596 597
              "plug-in/gimppluginmanager.h"
              "plug-in/gimppluginmanager-file.h"
598 599
              "file/file-open.h"
              "file/file-save.h"
600
	      "file/file-procedure.h"
601
              "file/file-utils.h");
602

603 604
@procs = qw(file_load file_load_layer file_load_layers
            file_save
605
            file_load_thumbnail file_save_thumbnail
606
            temp_name
607
            register_magic_load_handler register_load_handler
608
            register_save_handler
609
            register_file_handler_mime
610
            register_thumbnail_loader);
611

612
%exports = (app => [@procs], lib => [@procs[0..3,5..11]]);
613 614 615 616

$desc = 'File Operations';

1;