fileops.pdb 11 KB
Newer Older
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
# The GIMP -- an image manipulation program
# 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 pdb_misc {
    $author = $copyright = 'Josh MacDonald';
    $date = '1997';
}

sub list_arg {
    my ($type, $name, $desc, $example) = @_;
    push @inargs, { name => $name, type => 'string', no_success => 1,
		    desc => qq/comma separated list of $desc this handler
			       can $type (i.e. "$example")/ }
}

sub handler_args {
    my $type = shift;

    my $action = $type;
    $action =~ s/e$//;
    $action .= 'ing';

    push @inargs, { name => 'procedure_name', type => 'string',
		    desc => "The name of the procedure to be used for $action",
		    alias => 'name' };

    foreach ([ 'extensions', 'jpg,jpeg' ], [ 'prefixes', 'http:,ftp:' ]) {
	&list_arg($type, $_->[0], @$_);
    }
}

# The defs

sub file_load {
    $blurb = 'Loads a file by extension.';

    $help = <<'HELP';
This procedure invokes the correct file load handler according to the file's
extension and/or prefix. 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.
HELP

    &pdb_misc;

    @inargs = (
	{ name => 'run_mode',
	  type => 'enum RunModeType (no RUN_WITH_LAST_VALS)',
	  desc => 'The run mode: %%desc%%' },
	{ name => 'filename', type => 'string',
	  desc => 'The name of the file to load' },
	{ name => 'raw_filename', type => 'string',
	  desc => 'The name entered' }
    );

    @outargs = ( &std_image_arg );
    $outargs[0]->{desc} = 'The output image';

    %invoke = (
	proc => [ 'proc->name', 'args' ],
	vars => [ 'PlugInProcDef *file_proc', 'ProcRecord *proc' ],
	code => <<'CODE'
{
  file_proc = file_proc_find (load_procs, %%raw_filename%%);
  if (!file_proc)
    return %%fail%%;

  proc = &file_proc->db_info;

  return %%exec%%;
}
CODE
    );
}

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

    &pdb_misc;

    @inargs = (
	{ name => 'run_mode',
	  type => 'enum RunModeType (no RUN_WITH_LAST_VALS)',
	  desc => 'The run mode: %%desc%%' },
	{ name => 'image', type => 'image',
	  desc => 'Input image' },
	{ name => 'drawable', type => 'drawable',
	  desc => 'Drawable to save' },
	{ name => 'filename', type => 'string',
	  desc => 'The name of the file to save the image in' },
	{ name => 'raw_filename', type => 'string',
	  desc => 'The name of the file to save the image in' }
    );


    %invoke = (
	headers => [ qw(<string.h>) ],
	proc => [ 'proc->name', 'new_args' ],
	args => [ 'new_args', 'return_vals' ],
128
	vars => [ 'PlugInProcDef *file_proc', 'ProcRecord *proc', 'gint i' ],
129 130
	code => <<'CODE'
{
Manish Singh's avatar
Manish Singh committed
131
  file_proc = file_proc_find (save_procs, %%raw_filename%%);
132 133 134 135 136 137 138 139 140
  if (!file_proc) 
    return %%fail%%;

  proc = &file_proc->db_info;

  new_args = g_new (%%argtype%%, proc->num_args);
  memset (new_args, 0, sizeof (%%argtype%%) * proc->num_args);
  memcpy (new_args, args, sizeof (%%argtype%%) * 5);

141 142 143 144 145 146 147
  for (i=5; i<proc->num_args; i++)
  {
    new_args[i].arg_type = proc->args[i].arg_type;
    if (proc->args[i].arg_type == PDB_STRING)
      new_args[i].value.pdb_pointer = g_strdup("\0");
  }

148 149 150 151 152 153 154 155 156
  return_vals = %%exec%%;
  g_free (new_args);

  return return_vals;
}
CODE
    );
}

157 158 159 160 161 162 163 164 165 166 167 168 169
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 
you need a thumbnail of an already opened image, use gimp_image_thumbnail 
instead.
HELP

    $author = $copyright = 'Adam D. Moss, Sven Neumann';
170
    $date = '1999-2000';
171 172 173 174 175 176 177 178 179 180 181 182

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

    @outargs = ( 
	{ name => 'width', type => 'int32', init => 1,
	  desc => 'The width of the thumbnail' },
	{ name => 'height', type => 'int32', init => 1, 
	  desc => 'The height of the thumbnail' },
	{ name => 'thumb_data', type => 'int8array', init => 1,
183 184 185 186
	  desc => 'The thumbnail data', 
          array =>  { name => 'thumbnail_data_count',
		      desc => 'The number of bytes in thumbnail data',
		      alias => 'num_bytes', init => 1 } },
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    );

    %invoke = (
        headers => [ qw("fileopsP.h") ],
	vars => [ 'gchar *pname', 'gchar *fname', 'gchar *tname',
		  'guchar *raw_thumb', 'gchar *imginfo = NULL', 'gint i' ],
	code => <<'CODE'
{
  pname = g_dirname (filename);
  fname = g_basename (filename);
  tname = g_strconcat (pname, G_DIR_SEPARATOR_S, ".xvpics", G_DIR_SEPARATOR_S,
		       fname,
		       NULL);
  g_free (pname);
  raw_thumb = readXVThumb (tname, &width, &height, &imginfo);
  g_free (tname);

  if (raw_thumb)
    {
206 207
      num_bytes = 3 * width * height;
      thumb_data = g_malloc (num_bytes);
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
	  
      for (i=0; i<width*height; i++)
        {
	  thumb_data[i*3  ] = ((raw_thumb[i]>>5)*255)/7;
	  thumb_data[i*3+1] = (((raw_thumb[i]>>2)&7)*255)/7;
	  thumb_data[i*3+2] = (((raw_thumb[i])&3)*255)/3;
	}
      g_free (raw_thumb);
      success = TRUE;
    }
  else
    success = FALSE;
}
CODE
    );
}

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

    $help = <<'HELP';
This procedure saves a thumbnail in the .xvpics format for the given image.
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. 
HELP

    &pdb_misc;

    @inargs = (
	&std_image_arg,
	{ name => 'filename', type => 'string',
	  desc => 'The name of the file the thumbnail belongs to' },
    );

    %invoke = (
        headers => [ qw("fileopsP.h") ],
	vars => [ 'TempBuf *thumb' ],
	code => <<'CODE'
{
  thumb = make_thumb_tempbuf (gimage);
  if (file_save_thumbnail (gimage, filename, thumb))
    success = TRUE;
}
CODE
    );
}

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
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

    &pdb_misc;

    @inargs = (
	{ name => 'extension', type => 'string',
	  desc => 'The extension the file will have' }
    );

    @outargs = (
	{ name => 'name', type => 'string', init => 1,
	  desc => 'The new temp filename' }
    );

    %invoke = (
	headers => [ qw("gimprc.h") ],
	vars => [ 'static gint id = 0', 'static gint pid' ],
	code => <<'CODE'
{
  if (id == 0)
    pid = getpid();

  name = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "gimp_temp.%d%d.%s",
			  temp_path, pid, id++, extension);
}
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 
particular file format using magic file information.
HELP

    &std_pdb_misc;

    &handler_args('load');
    &list_arg('load', 'magics', 'magic file information', '0,string,GIF');

    %invoke = (
	vars => [ 'ProcRecord *proc', 'PlugInProcDef *file_proc' ],
	code => <<'CODE'
{
  success = FALSE;

  proc = procedural_db_lookup (name);

  if (proc && ((proc->num_args < 3) ||
	       (proc->num_values < 1) ||
	       (proc->args[0].arg_type != PDB_INT32) ||
	       (proc->args[1].arg_type != PDB_STRING) ||
	       (proc->args[2].arg_type != PDB_STRING) ||
	       (proc->values[0].arg_type != PDB_IMAGE)))
    {
Sven Neumann's avatar
Sven Neumann committed
319
      g_message ("load handler \"%s\" does not take the standard load handler args",
320 321 322 323 324 325 326 327
		 name);
      goto done;
    }

  file_proc = plug_in_file_handler (name, extensions, prefixes, magics);

  if (!file_proc)
    {
Sven Neumann's avatar
Sven Neumann committed
328 329
      g_message ("attempt to register non-existant load handler \"%s\""),
		 name;
330 331 332 333 334 335 336
      goto done;
    }

  load_procs = g_slist_prepend (load_procs, file_proc);

  success = TRUE;

Tor Lillqvist's avatar
Tor Lillqvist committed
337
done: ;
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
}
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
particular file format.
HELP

    &std_pdb_misc;

    &handler_args('load');

    %invoke = (
	pass_through => 'register_magic_load_handler',
	pass_args => [ 0..2 ],
	make_args => [ { type => 'string', code => '%%arg%% = NULL;' } ]
    );
}

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
particular file format.
HELP

    &std_pdb_misc;

    &handler_args('save');

    %invoke = (
	vars => [ 'ProcRecord *proc', 'PlugInProcDef *file_proc' ],
	code => <<'CODE'
{
  success = FALSE;

  proc = procedural_db_lookup (name);

  if (proc && ((proc->num_args < 5) ||
	       (proc->args[0].arg_type != PDB_INT32) ||
	       (proc->args[1].arg_type != PDB_IMAGE) ||
	       (proc->args[2].arg_type != PDB_DRAWABLE) ||
	       (proc->args[3].arg_type != PDB_STRING) ||
	       (proc->args[4].arg_type != PDB_STRING)))
    {
Sven Neumann's avatar
Sven Neumann committed
389
      g_message ("save handler \"%s\" does not take the standard save handler args",
390 391 392 393 394 395 396 397
		 name);
      goto done;
    }

  file_proc = plug_in_file_handler (name, extensions, prefixes, NULL);

  if (!file_proc)
    {
Sven Neumann's avatar
Sven Neumann committed
398
      g_message ("attempt to register non-existant save handler \"%s\"",
399 400 401 402 403 404 405 406
		 name);
      goto done;
    }

  save_procs = g_slist_prepend (save_procs, file_proc);

  success = TRUE;

Tor Lillqvist's avatar
Tor Lillqvist committed
407
done: ;
408 409 410 411 412
}
CODE
    );
}

Sven Neumann's avatar
Sven Neumann committed
413
@headers = qw("fileops.h" "plug_in.h" "config.h" <unistd.h>);
414

Manish Singh's avatar
Manish Singh committed
415 416 417 418
@procs = qw(file_load file_save file_load_thumbnail file_save_thumbnail
	    temp_name register_magic_load_handler register_load_handler
	    register_save_handler);
%exports = (app => [@procs], lib => [@procs[5..7]]);
419 420 421 422

$desc = 'File Operations';

1;