Lib.xs 44.2 KB
Newer Older
Marc Lehmann's avatar
Marc Lehmann committed
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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
#include "config.h"

#include <assert.h>
#include <stdio.h>

#include <libgimp/gimp.h>
#if UI
#include <libgimp/gimpui.h>
#endif

/* FIXME */
/* sys/param.h is redefining these! */
#undef MIN
#undef MAX

/* dunno where this comes from */
#undef VOIDUSED

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

/* I actually do care a bit about older perls... */
#ifndef ERRSV
# define ERRSV GvSV(errgv)
#endif

/* FIXME */
/* dirty is used in gimp.h.  */
#undef dirty

#include "extradefs.h"

#if GIMP_MAJOR_VERSION>1 || (GIMP_MAJOR_VERSION==1 && GIMP_MINOR_VERSION>=1)
#define GIMP11 1
#define GIMP_PARASITE 1
#endif

#define GIMP_PKG	"Gimp::"	/* the package name */

#define PKG_COLOR	GIMP_PKG "Color"
#define PKG_REGION	GIMP_PKG "Region"
#define PKG_DISPLAY	GIMP_PKG "Display"
#define PKG_IMAGE	GIMP_PKG "Image"
#define PKG_LAYER	GIMP_PKG "Layer"
#define PKG_CHANNEL	GIMP_PKG "Channel"
#define PKG_DRAWABLE	GIMP_PKG "Drawable"
#define PKG_SELECTION	GIMP_PKG "Selection"
#define PKG_REGION	GIMP_PKG "Region"
#if GIMP_PARASITE
#define PKG_PARASITE	GIMP_PKG "Parasite"
#endif

#define PKG_GDRAWABLE	GIMP_PKG "GDrawable"
#define PKG_TILE	GIMP_PKG "Tile"
#define PKG_PIXELRGN	GIMP_PKG "PixelRgn"

#define PKG_ANY		((char *)0)

static int trace = TRACE_NONE;

typedef gint32 IMAGE;
typedef gint32 LAYER;
typedef gint32 CHANNEL;
typedef gint32 DRAWABLE;
typedef gint32 SELECTION;
typedef gint32 DISPLAY;
typedef gint32 REGION;
typedef gint32 COLOR;

/* new SV with len len.  There _must_ be a better way, but newSV doesn't work.  */
SV *newSVn (int len)
{
  SV *sv = newSVpv ("", 0);
  
  SvUPGRADE (sv, SVt_PV);
  SvGROW (sv, len);
  SvCUR_set (sv, len);
  
  return sv;
}

/* magic stuff.  literally.  */
int gdrawable_free (SV *obj, MAGIC *mg)
{
  return gimp_drawable_detach ((GDrawable *)SvIV(obj)), 0;
}

MGVTBL vtbl_gdrawable = {0, 0, 0, 0, gdrawable_free};

/* drawable/tile/region stuff.  */
SV *new_tile (GTile *tile, SV *gdrawable)
{
  static HV *stash;
  HV *hv = newHV ();
  
  hv_store (hv, "_tile"		, 5, newSViv ((IV)tile)		, 0);
  hv_store (hv, "_ewidth"	, 7, newSViv (tile->ewidth)	, 0);
  hv_store (hv, "_eheight"	, 8, newSViv (tile->eheight)	, 0);
  hv_store (hv, "_bpp"		, 4, newSViv (tile->bpp)	, 0);
  hv_store (hv, "_shadow"	, 7, newSViv (tile->shadow)	, 0);
  hv_store (hv, "_gdrawable"	,10, newSVsv (gdrawable)	, 0);
  
  if (!stash)
    stash = gv_stashpv (PKG_TILE, 1);
  
  return sv_bless (newRV_noinc ((SV*)hv), stash);
}

GDrawable *old_gdrawable (SV *sv)
{
  if (!(sv_derived_from (sv, PKG_GDRAWABLE)
        && SvROK (sv)
        && SvTYPE (SvRV (sv)) == SVt_PVHV))
    croak ("argument is not of type " PKG_GDRAWABLE);
  
  /* the next line lacks any type of checking.  */
  return (GDrawable *)SvIV(*(hv_fetch ((HV*)SvRV(sv), "_gdr", 4, 0)));
}

GTile *old_tile (SV *sv)
{
  if (!sv_derived_from (sv, PKG_TILE))
    croak ("argument is not of type " PKG_TILE);
  
  /* the next line lacks any type of checking.  */
  return (GTile *)SvIV(*(hv_fetch ((HV*)SvRV(sv), "_tile", 5, 0)));
}

GPixelRgn *old_pixelrgn (SV *sv)
{
  if (!sv_derived_from (sv, PKG_PIXELRGN))
    croak ("argument is not of type " PKG_PIXELRGN);
  
  /* the next line lacks any type of checking.  */
  return (GPixelRgn *)SvPV(*(hv_fetch ((HV*)SvRV(sv), "_rgn", 4, 0)),na);
}

/* tracing stuff.  */
static SV *trace_var = 0;
static PerlIO *trace_file = 0; /* FIXME: unportable.  */

static void
trace_init ()
{
  if (!trace_file)
    SvCUR_set (trace_var, 0);
}

#ifndef __STDC_VERSION__
#warn You need to compile with an ansi-c compiler!!!
#warn Compilation will continue at your own risk!!
#define __STDC_VERSION__ 0
#endif

#if __STDC_VERSION__ > 199900
#define trace_printf(...) \
	if (trace_file) PerlIO_printf (trace_file, __VA_ARGS__); \
	else		sv_catpvf (trace_var, __VA_ARGS__)
#elif __GNUC__
#define trace_printf(frmt,args...) \
	if (trace_file) PerlIO_printf (trace_file, frmt, ## args); \
	else		sv_catpvf (trace_var, frmt, ## args)
#elif __STDC__

/* sigh */
#include <stdarg.h>
void trace_printf (char *frmt, ...)
{
  va_list args;
  char buffer[4096]; /* sorry... */
  
  va_start (args, frmt);
#ifdef HAVE_VSNPRINTF
  vsnprintf (buffer, sizeof buffer, frmt, args);
#else
  vsprintf (buffer, frmt, args);
#endif
  if (trace_file) PerlIO_printf (trace_file, "%s", buffer);
  else		  sv_catpv (trace_var, buffer);
}

#else
#error need ansi compiler, maybe try c89?
error need_ansi_compiler__maybe_try_c89
#endif

static int
is_array (GParamType typ)
{
  return typ == PARAM_INT32ARRAY
      || typ == PARAM_INT16ARRAY
      || typ == PARAM_INT8ARRAY
      || typ == PARAM_FLOATARRAY
      || typ == PARAM_STRINGARRAY;
}

static int
perl_param_count (GParam *arg, int count)
{
  GParam *end = arg + count;
  
  while (arg < end)
    if (is_array (arg++->type))
      count--;
  
  return count;
}

/*
 * count actual parameter number
 */
static int
perl_paramdef_count (GParamDef *arg, int count)
{
  GParamDef *end = arg + count;
  
  while (arg < end)
    if (is_array (arg++->type))
      count--;
  
  return count;
}

/* horrors!  c wasn't designed for this!  */
#define dump_printarray(args,index,ctype,datatype,frmt) {\
  int j; \
  trace_printf ("["); \
  if (args[index].data.datatype || !args[index-1].data.d_int32) \
    { \
      for (j = 0; j < args[index-1].data.d_int32; j++) \
        trace_printf (frmt ## "%s", (ctype) args[index].data.datatype[j], \
                      j < args[index-1].data.d_int32 - 1 ? ", " : ""); \
    } \
  else \
    trace_printf ("(UNINITIALIZED)"); \
  trace_printf ("]"); \
}

static void
dump_params (int nparams, GParam *args, GParamDef *params)
{
  static char *ptype[PARAM_END+1] = {
    "INT32"      , "INT16"      , "INT8"      , "FLOAT"      , "STRING"     ,
    "INT32ARRAY" , "INT16ARRAY" , "INT8ARRAY" , "FLOATARRAY" , "STRINGARRAY",
    "COLOR"      , "REGION"     , "DISPLAY"   , "IMAGE"      , "LAYER"      ,
    "CHANNEL"    , "DRAWABLE"   , "SELECTION" , "BOUNDARY"   , "PATH"       ,
#if GIMP_PARASITE
    "PARASITE"   ,
#endif
    "STATUS"     , "END"
  };
  int i;
  
  trace_printf ("(");
  
  if ((trace & TRACE_DESC) == TRACE_DESC)
    trace_printf ("\n\t");
  
  for (i = 0; i < nparams; i++)
    {
      if ((trace & TRACE_TYPE) == TRACE_TYPE)
        if (params[i].type >= 0 && params[i].type < PARAM_END+1)
          trace_printf ("%s ", ptype[params[i].type]);
        else
          trace_printf ("T%d ", params[i].type);
      
      if ((trace & TRACE_NAME) == TRACE_NAME)
        trace_printf ("%s=", params[i].name);
      
      switch (args[i].type)
        {
          case PARAM_INT32:	trace_printf ("%d", args[i].data.d_int32); break;
          case PARAM_INT16:	trace_printf ("%d", args[i].data.d_int16); break;
          case PARAM_INT8:	trace_printf ("%d", (guint8) args[i].data.d_int8); break;
          case PARAM_FLOAT:	trace_printf ("%f", args[i].data.d_float); break;
          case PARAM_STRING:	trace_printf ("\"%s\"", args[i].data.d_string); break;
          case PARAM_DISPLAY:	trace_printf ("%d", args[i].data.d_display); break;
          case PARAM_IMAGE:	trace_printf ("%d", args[i].data.d_image); break;
          case PARAM_LAYER:	trace_printf ("%d", args[i].data.d_layer); break;
          case PARAM_CHANNEL:	trace_printf ("%d", args[i].data.d_channel); break;
          case PARAM_DRAWABLE:	trace_printf ("%d", args[i].data.d_drawable); break;
          case PARAM_SELECTION:	trace_printf ("%d", args[i].data.d_selection); break;
          case PARAM_BOUNDARY:	trace_printf ("%d", args[i].data.d_boundary); break;
          case PARAM_PATH:	trace_printf ("%d", args[i].data.d_path); break;
          case PARAM_STATUS:	trace_printf ("%d", args[i].data.d_status); break;
          case PARAM_INT32ARRAY:	dump_printarray (args, i, gint32, d_int32array, "%d"); break;
          case PARAM_INT16ARRAY:	dump_printarray (args, i, gint16, d_int16array, "%d"); break;
          case PARAM_INT8ARRAY:		dump_printarray (args, i, guint8, d_int8array , "%d"); break;
          case PARAM_FLOATARRAY:	dump_printarray (args, i, gfloat, d_floatarray, "%f"); break;
          case PARAM_STRINGARRAY:	dump_printarray (args, i, char* , d_stringarray, "'%s'"); break;
          
          case PARAM_COLOR:
            trace_printf ("[%d,%d,%d]",
                          args[i].data.d_color.red,
                          args[i].data.d_color.green,
                          args[i].data.d_color.blue);
            break;
          
          case PARAM_PARASITE:
            {
              gint32 found = 0;
              
              trace_printf ("[%s, ", args[i].data.d_parasite.name);
              if (args[i].data.d_parasite.flags & PARASITE_PERSISTANT)
                {
                  trace_printf ("PARASITE_PERSISTANT");
                  found |= PARASITE_PERSISTANT;
                }
              
              if (args[i].data.d_parasite.flags & ~found)
                {
                  if (found)
                    trace_printf ("|");
                  trace_printf ("%d", args[i].data.d_parasite.flags & ~found);
                }
              
              trace_printf (", %d bytes data]", args[i].data.d_parasite.size);
            }
            break;
            
          default:
            trace_printf ("(?%d?)", args[i].type);
        }
      
      if ((trace & TRACE_DESC) == TRACE_DESC)
        trace_printf ("\t\"%s\"\n\t", params[i].description);
      else if (i < nparams - 1)
        trace_printf (", ");
      
    }
  
  trace_printf (")");
}

static int
convert_array2paramdef (AV *av, GParamDef **res)
{
  int count = 0;
  GParamDef *def = 0;
  
  if (av_len (av) >= 0)
    for(;;)
      {
        int idx;
        
        for (idx = 0; idx <= av_len (av); idx++)
          {
            SV *sv = *av_fetch (av, idx, 0);
            SV *type = 0;
            SV *name = 0;
            SV *help = 0;
        
            if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV)
              {
                AV *av = (AV *)SvRV(sv);
                SV **x;
                
                if ((x = av_fetch (av, 0, 0))) type = *x;
                if ((x = av_fetch (av, 1, 0))) name = *x;
                if ((x = av_fetch (av, 2, 0))) help = *x;
              }
            else if (SvIOK(sv))
              type = sv;

            if (type)
              {
                if (def)
                  {
                    if (is_array (SvIV (type)))
                      {
                        def->type = PARAM_INT32;
                        def->name = "array_size";
                        def->description = "the size of the following array";
                        def++;
                      }
                    
                    def->type = SvIV (type);
                    def->name = name ? SvPV (name, na) : 0;
                    def->description = help ? SvPV (help, na) : 0;
                    def++;
                  }
                else
                  count += 1 + !!is_array (SvIV (type));
              }
            else
              croak ("malformed paramdef, expected [PARAM_TYPE,\"NAME\",\"DESCRIPTION\"] or PARAM_TYPE");
          }
        
        if (def)
          break;
        
        *res = def = g_new (GParamDef, count);
      }
  else
    *res = 0;
  
  return count;
}

static HV *
param_stash (GParamType type)
{
  static HV *bless_hv[PARAM_END]; /* initialized to zero */
  static char *bless[PARAM_END] = {
                            0		, 0		, 0		, 0		, 0		,
                            0		, 0		, 0		, 0		, 0		,
                            PKG_COLOR	, PKG_REGION	, PKG_DISPLAY	, PKG_IMAGE	, PKG_LAYER	,
                            PKG_CHANNEL	, PKG_DRAWABLE	, PKG_SELECTION	, 0		, 0		,
#if GIMP_PARASITE
                            PKG_PARASITE,
#endif
                            0
                           };
  
  if (bless [type] && !bless_hv [type])
    bless_hv [type] = gv_stashpv (bless [type], 1);
  
  return bless_hv [type];
}
  
/* automatically bless SV into PARAM_type.  */
/* for what it's worth, we cache the stashes.  */
static SV *
autobless (SV *sv, int type)
{
  HV *stash = param_stash (type);
  
  if (stash)
    sv = sv_bless (newRV_noinc (sv), stash);
  
  return sv;
}

/* return gint32 from object, wether iv or rv.  */
static gint32
unbless (SV *sv, char *type, char *croak_str)
{
  if (SvROK (sv))
    if (type == PKG_ANY || sv_derived_from (sv, type))
      {
        if (SvTYPE (SvRV (sv)) == SVt_PVMG)
          return SvIV (SvRV (sv));
        else
          croak ("only blessed scalars accepted here");
      }
    else
      if (croak_str)
        sprintf (croak_str, "argument type %s expected", type);
      else
        croak ("argument type %s expected", type);
  else
    return SvIV (sv);
  
  abort (); /* calm down gcc */
}

static void
canonicalize_colour (char *err, SV *sv, GParamColor *c)
{
  dSP;
  
  ENTER;
  SAVETMPS;
  
  PUSHMARK(SP);
  XPUSHs (sv);
  PUTBACK;
  
  if (perl_call_pv ("Gimp::canonicalize_colour", G_SCALAR) != 1)
    croak ("canonicalize_colour did not return a value!");
  
  SPAGAIN;
  
  sv = POPs;
  if (SvROK(sv))
    {
      if (SvTYPE(SvRV(sv)) == SVt_PVAV)
        {
          AV *av = (AV *)SvRV(sv);
          if (av_len(av) == 2)
            {
              c->red   = SvIV(*av_fetch(av, 0, 0));
              c->green = SvIV(*av_fetch(av, 1, 0));
              c->blue  = SvIV(*av_fetch(av, 2, 0));
            }
          else
            sprintf (err, "a color must have three components (array elements)");
        }
      else
        sprintf (err, "illegal type for colour specification");
    }
  else
    sprintf (err, "unable to grok colour specification");
  
  PUTBACK;
  FREETMPS;
  LEAVE;
}

/* replacement newSVpv with only one argument.  */
#define neuSVpv(arg) newSVpv((arg),0)

/* replacement newSViv which casts to unsigned char.  */
#define newSVu8(arg) newSViv((unsigned char)(arg))

/* create sv's using newsv, from the array arg.  */
#define push_gimp_av(arg,datatype,newsv,as_ref) {		\
  int j;							\
  AV *av;							\
  if (as_ref)							\
    av = newAV ();						\
  else								\
    EXTEND (sp, arg[-1].data.d_int32);				\
  for (j = 0; j < arg[-1].data.d_int32; j++)			\
    if (as_ref)							\
      av_push (av, newsv (arg->data.datatype[j]));		\
    else							\
      PUSHs (sv_2mortal (newsv (arg->data.datatype[j])));	\
  if (as_ref)							\
    PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));		\
  sv = 0;							\
}

void
push_gimp_sv (GParam *arg, int array_as_ref)
{
  SV *sv;
  dSP;
  
  switch (arg->type)
    {
      case PARAM_INT32:		sv = newSViv(arg->data.d_int32	); break;
      case PARAM_INT16:		sv = newSViv(arg->data.d_int16	); break;
      case PARAM_INT8:		sv = newSVu8(arg->data.d_int8	); break;
      case PARAM_FLOAT:		sv = newSVnv(arg->data.d_float	); break;
      case PARAM_DISPLAY:	sv = newSViv(arg->data.d_display); break;
      case PARAM_IMAGE:		sv = newSViv(arg->data.d_image	); break;
      case PARAM_LAYER:		sv = newSViv(arg->data.d_layer	); break;
      case PARAM_CHANNEL:	sv = newSViv(arg->data.d_channel); break;
      case PARAM_DRAWABLE:	sv = newSViv(arg->data.d_drawable); break;
      case PARAM_SELECTION:	sv = newSViv(arg->data.d_selection); break;
      case PARAM_BOUNDARY:	sv = newSViv(arg->data.d_boundary); break;
      case PARAM_PATH:		sv = newSViv(arg->data.d_path	); break;
      case PARAM_STATUS:	sv = newSViv(arg->data.d_status	); break;
      case PARAM_STRING:
        sv = arg->data.d_string ? neuSVpv(arg->data.d_string)
                                : &sv_undef;
        break;
        
      case PARAM_COLOR:
        {
          /* difficult */
          AV *av = newAV ();
          av_push (av, newSViv (arg->data.d_color.red));
          av_push (av, newSViv (arg->data.d_color.green));
          av_push (av, newSViv (arg->data.d_color.blue));
          sv = (SV *)av; /* no newRV, since we're getting autoblessed! */
        }
        break;

#if GIMP_PARASITE
      case PARAM_PARASITE:
        {
          AV *av = newAV ();
          av_push (av, neuSVpv (arg->data.d_parasite.name));
          av_push (av, newSViv (arg->data.d_parasite.flags));
          av_push (av, newSVpv (arg->data.d_parasite.data, arg->data.d_parasite.size));
          sv = (SV *)av; /* no newRV, since we're getting autoblessed! */
        }
        break;
#endif
      
      /* did I say difficult before????  */
      case PARAM_INT32ARRAY:	push_gimp_av (arg, d_int32array , newSViv, array_as_ref); break;
      case PARAM_INT16ARRAY:	push_gimp_av (arg, d_int16array , newSViv, array_as_ref); break;
      case PARAM_INT8ARRAY:	push_gimp_av (arg, d_int8array  , newSVu8, array_as_ref); break;
      case PARAM_FLOATARRAY:	push_gimp_av (arg, d_floatarray , newSVnv, array_as_ref); break;
      case PARAM_STRINGARRAY:	push_gimp_av (arg, d_stringarray, neuSVpv, array_as_ref); break;
        
      default:
        croak ("dunno how to return param type %d", arg->type);
/*        sv = sv_newmortal ();*/
        abort ();
    }
  
  if (sv)
    PUSHs (sv_2mortal (autobless (sv, arg->type)));
  
  PUTBACK;
}

#define SvPv(sv) SvPV((sv), na)
#define Sv32(sv) unbless ((sv), PKG_ANY, croak_str)

#define av2gimp(arg,sv,datatype,type,svxv) { \
  if (SvROK (sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) \
    { \
      int i; \
      AV *av = (AV *)SvRV(sv); \
      arg[-1].data.d_int32 = av_len (av) + 1; \
      arg->data.datatype = g_new (type, av_len (av) + 1); \
      for (i = 0; i <= av_len (av); i++) \
        arg->data.datatype[i] = svxv (*av_fetch (av, i, 0)); \
    } \
  else \
    { \
      sprintf (croak_str, "perl-arrayref required as datatype for a gimp-array"); \
      arg->data.datatype = 0; \
    } \
}

/*
 * convert a perl scalar into a GParam, return true if
 * the argument has been consumed.
 */
static int
convert_sv2gimp (char *croak_str, GParam *arg, SV *sv)
{
  switch (arg->type)
    {
      case PARAM_INT32:		arg->data.d_int32	= SvIV(sv); break;
      case PARAM_INT16:		arg->data.d_int16	= SvIV(sv); break;
      case PARAM_INT8:		arg->data.d_int8	= SvIV(sv); break;
      case PARAM_FLOAT:		arg->data.d_float	= SvNV(sv); break;
      case PARAM_STRING:	arg->data.d_string	= SvPv(sv); break;
      case PARAM_DISPLAY:	arg->data.d_display	= unbless(sv, PKG_DISPLAY  , croak_str); break;
      case PARAM_LAYER:		arg->data.d_layer	= unbless(sv, PKG_ANY      , croak_str); break;
      case PARAM_CHANNEL:	arg->data.d_channel	= unbless(sv, PKG_ANY      , croak_str); break;
      case PARAM_DRAWABLE:	arg->data.d_drawable	= unbless(sv, PKG_ANY      , croak_str); break;
      case PARAM_SELECTION:	arg->data.d_selection	= unbless(sv, PKG_SELECTION, croak_str); break;
      case PARAM_BOUNDARY:	arg->data.d_boundary	= SvIV(sv); break;
      case PARAM_PATH:		arg->data.d_path	= SvIV(sv); break;
      case PARAM_STATUS:	arg->data.d_status	= SvIV(sv); break;
      case PARAM_IMAGE:
      	if (sv_derived_from (sv, PKG_DRAWABLE))
      	  arg->data.d_image = gimp_drawable_image_id    (unbless(sv, PKG_DRAWABLE, croak_str));
      	else if (sv_derived_from (sv, PKG_LAYER   ))
      	  arg->data.d_image = gimp_layer_get_image_id   (unbless(sv, PKG_LAYER   , croak_str));
      	else if (sv_derived_from (sv, PKG_CHANNEL ))
      	  arg->data.d_image = gimp_channel_get_image_id (unbless(sv, PKG_CHANNEL , croak_str));
        else if (sv_derived_from (sv, PKG_IMAGE) || !SvROK (sv))
          {
      	                        arg->data.d_image	=unbless(sv, PKG_IMAGE   , croak_str); break;
      	  }
      	else
      	  strcpy (croak_str, "argument incompatible with type IMAGE");
      	
      	return 0;
      	break;
      	
      case PARAM_COLOR:
        canonicalize_colour (croak_str, sv, &arg->data.d_color);
        break;

#if GIMP_PARASITE
      case PARAM_PARASITE:
        if (SvROK(sv))
          {
            if (SvTYPE(SvRV(sv)) == SVt_PVAV)
              {
                AV *av = (AV *)SvRV(sv);
                if (av_len(av) == 2)
                  {
                    arg->data.d_parasite.name  = SvPv(*av_fetch(av, 0, 0));
                    arg->data.d_parasite.flags = SvIV(*av_fetch(av, 1, 0));
                    arg->data.d_parasite.data  = SvPV(*av_fetch(av, 2, 0), arg->data.d_parasite.size);
                  }
                else
                  sprintf (croak_str, "illegal parasite specification, expected three array members");
              }
            else
              sprintf (croak_str, "illegal parasite specification, arrayref expected");
          }
        else
          sprintf (croak_str, "illegal parasite specification, reference expected");
        
        break;
#endif
      
      case PARAM_INT32ARRAY:	av2gimp (arg, sv, d_int32array , gint32 , Sv32); break;
      case PARAM_INT16ARRAY:	av2gimp (arg, sv, d_int16array , gint16 , SvIV); break;
      case PARAM_INT8ARRAY:	av2gimp (arg, sv, d_int8array  , gint8  , SvIV); break;
      case PARAM_FLOATARRAY:	av2gimp (arg, sv, d_floatarray , gdouble, SvNV); break;
      case PARAM_STRINGARRAY:	av2gimp (arg, sv, d_stringarray, gchar *, SvPv); break;
        
      default:
        sprintf (croak_str, "dunno how to pass arg type %d", arg->type);
    }
  
  return 1;
}

/* only free array pointers, but not actual array values.  */
static void
destroy_params (GParam *arg, int count)
{
  int i;
  
  for (i = 0; i < count; i++)
    switch (arg[i].type)
      {
        case PARAM_INT32ARRAY:	g_free (arg[i].data.d_int32array); break;
        case PARAM_INT16ARRAY:	g_free (arg[i].data.d_int16array); break;
        case PARAM_INT8ARRAY:	g_free (arg[i].data.d_int8array); break;
        case PARAM_FLOATARRAY:	g_free (arg[i].data.d_floatarray); break;
        case PARAM_STRINGARRAY:	g_free (arg[i].data.d_stringarray); break;
          
        default: ;
      }
  
  g_free (arg);
}

/* first check wether the procedure exists at all.  */
static void try_call (char *name, int req)
{
  CV *cv = perl_get_cv (name, 0);
  
  /* it's not an error if the callback doesn't exist.  */
  if (cv) {
    dSP;
    PUSHMARK(sp);
    perl_call_sv ((SV *)cv, G_DISCARD | G_NOARGS);
  } else if (req)
    croak ("required callback '%s' not found", name);
}

static void pii_init (void) { try_call ("init" ,0); }
static void pii_query(void) { try_call ("query",1); }
static void pii_quit (void) { try_call ("quit" ,0); }

static void pii_run(char *name, int nparams, GParam *param, int *xnreturn_vals, GParam **xreturn_vals)
{
  static GParam *return_vals;
  static int nreturn_vals;
  
  dSP;
  int i, count;
  GParamDef *return_defs;
  char *err_msg = 0;
  
  char *proc_blurb;
  char *proc_help;
  char *proc_author;
  char *proc_copyright;
  char *proc_date;
  int proc_type;
  int _nparams;
  GParamDef *params;
  
  if (return_vals) /* the libgimp is soooooooo braindamaged. */
    {
      destroy_params (return_vals, nreturn_vals);
      return_vals = 0;
    }
  
  if (gimp_query_procedure (name, &proc_blurb, &proc_help, &proc_author,
                           &proc_copyright, &proc_date, &proc_type, &_nparams, &nreturn_vals,
                           &params, &return_defs) == TRUE)
    {
      g_free (proc_blurb);
      g_free (proc_help);
      g_free (proc_author);
      g_free (proc_copyright);
      g_free (proc_date);
      g_free (params);
      
      ENTER;
      SAVETMPS;
      
      PUSHMARK(sp);
      
      if (nparams)
        {
          EXTEND (sp, perl_param_count (param, nparams));
          PUTBACK;
          for (i = 0; i < nparams; i++)
            {
              if (i < nparams-1 && is_array (param[i+1].type))
                i++;
              
              push_gimp_sv (param+i, nparams > 2);
            }
          
          SPAGAIN;
        }
      
      count = perl_call_pv (name, G_EVAL
                            | (nparams ? 0 : G_NOARGS)
                            | (nreturn_vals == 0 ? G_VOID|G_DISCARD : nreturn_vals == 1 ? G_SCALAR : G_ARRAY));
      
      SPAGAIN;
      
      if (count == 1 && !SvOK (TOPs))
        {
          POPs;
          count = 0;
        }
      
      if (SvTRUE (ERRSV))
        err_msg = g_strdup (SvPV (ERRSV, na));
      else
        {
          int i;
          char err_msg [1024];
          err_msg [0] = 0;
          
          return_vals = (GParam *) g_new0 (GParam, nreturn_vals + 1);
          return_vals->type = PARAM_STATUS;
          return_vals->data.d_status = STATUS_SUCCESS;
          *xnreturn_vals = nreturn_vals;
          *xreturn_vals = return_vals++;
          
          for (i = nreturn_vals; i-- && count; )
             {
               return_vals[i].type = return_defs[i].type;
               if ((i >= nreturn_vals-1 || !is_array (return_defs[i+1].type))
                   && convert_sv2gimp (err_msg, &return_vals[i], TOPs))
                 {
                   --count;
                   POPs;
                 }
               
               if (err_msg [0])
                 croak (err_msg);
             }
          
          if (count)
            croak ("callback returned %s more values than expected", count);
        }
      
      while (count--)
        POPs;
      
      g_free (return_defs);
      
      FREETMPS;
      LEAVE;
    }
  else
    croak ("being called as '%s', but '%s' not registered in the pdb", name, name);
  
  if (err_msg)
    {
      gimp_message (err_msg);
      g_free (err_msg);
      
      if (return_vals)
        destroy_params (return_vals, nreturn_vals);
      
      nreturn_vals = 1;
      return_vals = g_new (GParam, 1);
      return_vals->type = PARAM_STATUS;
      return_vals->data.d_status = STATUS_EXECUTION_ERROR;
      *xnreturn_vals = nreturn_vals;
      *xreturn_vals = return_vals;
    }
  
}

GPlugInInfo PLUG_IN_INFO = { pii_init, pii_quit, pii_query, pii_run };

#if UI
static void gimp_pattern_select_widget_callback (gchar *name, gint width,
	gint height, gint bpp, gchar *mask, gint closing, gpointer nameref)
{
  SV *sv = (SV *)nameref;
  sv_setpv (sv, name);
}
#endif#

MODULE = Gimp::Lib	PACKAGE = Gimp::Lib

PROTOTYPES: ENABLE

#
# usage:
# set_trace (int new_trace_mask);
# set_trace (\$variable_to_trace_into);
# set_trace (*STDOUT);
#
Marc Lehmann's avatar
Marc Lehmann committed
883
I32
Marc Lehmann's avatar
Marc Lehmann committed
884 885 886 887 888
set_trace (var)
	CODE:
	{
		SV *sv = ST (0);
		
Marc Lehmann's avatar
Marc Lehmann committed
889 890
		RETVAL = trace;
		
Marc Lehmann's avatar
Marc Lehmann committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
		if (SvROK (sv) || SvTYPE (sv) == SVt_PVGV)
		  {
	            if (trace_var)
		      SvREFCNT_dec (trace_var), trace_var = 0;
		    
		    if (SvTYPE (sv) == SVt_PVGV) /* pray it's a filehandle!  */
		      trace_file = IoOFP (GvIO (sv));
		    else
		      {
		        trace_file = 0;
		        sv = SvRV (sv);
		        SvREFCNT_inc (sv);
		        SvUPGRADE (sv, SVt_PV);
		        trace_var = sv;
		      }
		  }
		else
		  trace = SvIV (ST (0));
	}
Marc Lehmann's avatar
Marc Lehmann committed
910 911
	OUTPUT:
	RETVAL
Marc Lehmann's avatar
Marc Lehmann committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028

SV *
_autobless (sv,type)
	SV *	sv
	gint32	type
	CODE:
	RETVAL = autobless (newSVsv (sv), type);
	OUTPUT:
	RETVAL

PROTOTYPES: DISABLE

int
gimp_main(...)
	PREINIT:
	CODE:
		SV *sv;
		
		if ((sv = perl_get_sv ("Gimp::help", FALSE)) && SvTRUE (sv))
		  RETVAL = 0;
		else
		  {
		    char *argv [10];
		    int argc = 0;
		    
		    if (items == 0)
		      {
		        AV *av = perl_get_av ("ARGV", FALSE);
		        
		        argv [argc++] = SvPV (perl_get_sv ("0", FALSE), na);
		        if (av && av_len (av) < 10-1)
		          {
		            while (argc-1 <= av_len (av))
		              argv [argc] = SvPV (*av_fetch (av, argc-1, 0), na),
		              argc++;
		          }
		        else
		          croak ("internal error (please report): too many main arguments");
		      }
		    else
		      croak ("arguments to main not yet supported!");
		    
		    RETVAL = gimp_main (argc, argv);
		  }
	OUTPUT:
	RETVAL

PROTOTYPES: ENABLE

# checks wether a gimp procedure exists
int
_gimp_procedure_available(proc_name)
	char * proc_name
	CODE:
	{
		char *proc_blurb;	
		char *proc_help;
		char *proc_author;
		char *proc_copyright;
		char *proc_date;
		int proc_type;
		int nparams;
		int nreturn_vals;
		GParamDef *params;
		GParamDef *return_vals;
		
		if (gimp_query_procedure (proc_name, &proc_blurb, &proc_help, &proc_author,
		    &proc_copyright, &proc_date, &proc_type, &nparams, &nreturn_vals,
		    &params, &return_vals) == TRUE)
		  {
		    g_free (proc_blurb);
		    g_free (proc_help);
		    g_free (proc_author);
		    g_free (proc_copyright);
		    g_free (proc_date);
		    g_free (params);
		    g_free (return_vals);
		    RETVAL = TRUE;
		  }
		else
		  RETVAL = FALSE;
		    
	}
	OUTPUT:
	RETVAL

void
gimp_call_procedure (proc_name, ...)
	char *	proc_name
	PPCODE:
	{
		char croak_str[300] = "";
		char *proc_blurb;	
		char *proc_help;
		char *proc_author;
		char *proc_copyright;
		char *proc_date;
		int proc_type;
		int nparams;
		int nreturn_vals;
		int i, j;
		GParam *args = 0;
		GParam *values = 0;
		int nvalues;
		GParamDef *params;
		GParamDef *return_vals;
		
		if (trace)
		  trace_init ();
		
		if (trace & TRACE_CALL)
		  trace_printf ("%s", proc_name);
		
		if (gimp_query_procedure (proc_name, &proc_blurb, &proc_help, &proc_author,
		    &proc_copyright, &proc_date, &proc_type, &nparams, &nreturn_vals,
		    &params, &return_vals) == TRUE)
		  {
Marc Lehmann's avatar
Marc Lehmann committed
1029 1030 1031 1032
		    int no_runmode = !nparams
		                     || params[0].type != PARAM_INT32
		                     || strcmp (params[0].name, "run_mode");
		    
Marc Lehmann's avatar
Marc Lehmann committed
1033 1034 1035 1036 1037 1038 1039 1040 1041
		    g_free (proc_blurb);
		    g_free (proc_help);
		    g_free (proc_author);
		    g_free (proc_copyright);
		    g_free (proc_date);
		    
		    if (nparams)
		      args = (GParam *) g_new (GParam, nparams);
    		    
Marc Lehmann's avatar
Marc Lehmann committed
1042
		    for(;items;)
Marc Lehmann's avatar
Marc Lehmann committed
1043
		      {
Marc Lehmann's avatar
Marc Lehmann committed
1044
		        j = 0;
Marc Lehmann's avatar
Marc Lehmann committed
1045
		        
Marc Lehmann's avatar
Marc Lehmann committed
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
		        if (no_runmode || !SvROK (ST(1)))
    		          for (i = 0; i < nparams && j < items; i++)
		            {
		              args[i].type = params[i].type;
		              if (!i && no_runmode == 2)
		                args->data.d_int32 = RUN_NONINTERACTIVE;
		              else if ((!SvROK (ST(j+1)) || i >= nparams-1 || !is_array (params[i+1].type))
		                  && convert_sv2gimp (croak_str, &args[i], ST(j+1)))
		                j++;
		          
		              if (croak_str [0])
		                {
		                  if (!no_runmode)
		                    {
		                      croak_str [0]=0;
		                      break;
		                    }
		                  
		                  if (trace & TRACE_CALL)
		                    {
		                      dump_params (i, args, params);
		                      trace_printf (" = [argument error]\n");
		                    }
		              
		                  goto error;
		                }
		            }
		          
		        if (no_runmode || j >= items-1)
		          break;
		        
		        /* very costly, do better! */
		        no_runmode = 2;
		        destroy_params (args, nparams);
		        args = (GParam *) g_new (GParam, nparams);
Marc Lehmann's avatar
Marc Lehmann committed
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
		      }
		    
		    if (trace & TRACE_CALL)
		      {
		        dump_params (i, args, params);
		        trace_printf (" = ");
		      }
    		    
		    if (j != items-1)
		      {
		        if (trace & TRACE_CALL)
		          trace_printf ("[unfinished]\n");
		        
		        sprintf (croak_str, "%s arguments (%d) for function '%s'",
		                 j == items ? "not enough" : "too many", (int)items-1, proc_name);
    		        
    		        if (nparams)
    		          destroy_params (args, nparams);
		      }
		    else
		      {
                        values = gimp_run_procedure2 (proc_name, &nvalues, nparams, args);
                        
    		        if (nparams)
    		          destroy_params (args, nparams);
    		    
			if (trace & TRACE_CALL)
			  {
			    dump_params (nvalues-1, values+1, return_vals);
			    trace_printf ("\n");
			  }
			
                        if (values && values[0].type == PARAM_STATUS)
                          {
                            if (values[0].data.d_status == STATUS_EXECUTION_ERROR)
                              sprintf (croak_str, "%s: procedural database execution failed", proc_name);
                            else if (values[0].data.d_status == STATUS_CALLING_ERROR)
                              sprintf (croak_str, "%s: procedural database execution failed on invalid input arguments", proc_name);
                            else if (values[0].data.d_status == STATUS_SUCCESS)
                              {
                                EXTEND(sp, perl_paramdef_count (return_vals, nvalues-1));
                                PUTBACK;
                                for (i = 0; i < nvalues-1; i++)
                                  {
	                            if (i < nvalues-2 && is_array (values[i+2].type))
	                              i++;
	                            
	                            push_gimp_sv (values+i+1, nvalues > 2+1);
                                  }
                                
                                SPAGAIN;
                              }
                            else
                              sprintf (croak_str, "unsupported status code: %d\n", values[0].data.d_status);
                          }
                        else
                          sprintf (croak_str, "gimp returned, well.. dunno how to interpret that...");
		        
                      }
		    
		    error:
		    
		    if (values)
		      gimp_destroy_params (values, nreturn_vals);
		    
		    g_free (return_vals);
		    g_free (params);
		    
		    if (croak_str[0])
		      croak (croak_str);
		  }
		else
		  croak ("gimp procedure '%s' not found", proc_name);
	}

void
gimp_install_procedure(name, blurb, help, author, copyright, date, menu_path, image_types, type, params, return_vals)
	char *	name
	char *	blurb
	char *	help
	char *	author
	char *	copyright
	char *	date
	char *	menu_path
	char *	image_types
	int	type
	SV *	params
	SV *	return_vals
	ALIAS:
		gimp_install_temp_proc = 1
	CODE:
	{
		if (SvROK(params) && SvTYPE(SvRV(params)) == SVt_PVAV
		    && SvROK(return_vals) && SvTYPE(SvRV(return_vals)) == SVt_PVAV)
		  {
		    GParamDef *apd; int nparams;
		    GParamDef *rpd; int nreturn_vals;
		    
		    nparams      = convert_array2paramdef ((AV *)SvRV(params)     , &apd);
		    nreturn_vals = convert_array2paramdef ((AV *)SvRV(return_vals), &rpd);
		    
		    if (ix)
		      gimp_install_temp_proc(name,blurb,help,author,copyright,date,menu_path,image_types,
		                             type,nparams,nreturn_vals,apd,rpd,pii_run);
		    else
		      gimp_install_procedure(name,blurb,help,author,copyright,date,menu_path,image_types,
		                             type,nparams,nreturn_vals,apd,rpd);
		    
		    g_free (rpd);
		    g_free (apd);
		  }
		else
		  croak ("params and return_vals must be array refs (even if empty)!");
	}

void
gimp_uninstall_temp_proc(name)
	char *	name

void
gimp_lib_quit()
	CODE:
	gimp_quit ();

void
gimp_set_data(id, data)
	SV *	id
	SV *	data;
	CODE:
	{
		STRLEN dlen;
		STRLEN len;
		void *dta;
		
		dta = SvPV (data, dlen);

		/* do not remove this comment */
#ifdef HAVE_GET_DATA_SIZE
		gimp_set_data (SvPV (id, na), dta, dlen);
#else
		{
		  char str[1024]; /* hack */
		  SvUPGRADE (id, SVt_PV);
		  len = SvCUR (id);
		  Copy (SvPV (id, na), str, len, char);
		  str[len+1] = 0;
		  str[len] = 'S'; gimp_set_data (str, &dlen, sizeof (STRLEN));
		  str[len] = 'C'; gimp_set_data (str, dta, dlen);
		}
#endif
	}

SV *
gimp_get_data(id)
	SV *	id;
	CODE:
	{
		SV *data;
		STRLEN dlen;
		STRLEN len;
		
		/* do not remove this comment */
#ifdef HAVE_GET_DATA_SIZE
		dlen = gimp_get_data_size (SvPV (id, na));
		/* I count on dlen being zero if "id" doesn't exist.  */
		data = newSVpv ("", 0);
		gimp_get_data (SvPV (id, na), SvGROW (data, dlen+1));
		SvCUR_set (data, dlen);
		*((char *)SvPV (data, na) + dlen) = 0;
		RETVAL = data;
#else
		{
		  char str[1024]; /* hack */
		  SvUPGRADE (id, SVt_PV);
		  len = SvCUR (id);
		  Copy (SvPV (id, na), str, len, char);
		  
		  str[len+1] = 0;
		  dlen = (STRLEN) -1;
		  str[len] = 'S'; gimp_get_data (str, &dlen);
		  
		  if (dlen != (STRLEN)-1)
		    {
		      data = newSVpv ("", 0);
		      str[len] = 'C'; gimp_get_data (str, SvGROW (data, dlen+1));
		      SvCUR_set (data, dlen);
		      *((char *)SvPV (data, na) + dlen) = 0;
		      RETVAL = data;
		    }
		  else
		    RETVAL = &sv_undef;
		}
#endif
	}
	OUTPUT:
	RETVAL

void
gimp_register_magic_load_handler(name, extensions, prefixes, magics)
	char *	name
	char *	extensions
	char *	prefixes
	char *	magics

gdouble
gimp_gamma()

gint
gimp_install_cmap()

gint
gimp_use_xshm()

guchar *
gimp_color_cube()

gchar *
gimp_gtkrc()

SV *
gimp_drawable_get(drawable_ID)
	DRAWABLE	drawable_ID
	CODE:
	{
		static HV *stash;
		SV *sv;
		MAGIC *mg;
		HV *hv = newHV ();
		GDrawable *gdr = gimp_drawable_get (drawable_ID);
		
		sv = newSViv ((IV)gdr);
		sv_magic (sv, 0, '~', 0, 0);
		mg = mg_find (sv, '~');
		mg->mg_virtual = &vtbl_gdrawable;
		
		hv_store (hv, "_gdr"		, 4, sv				, 0);
		hv_store (hv, "_width"		, 6, newSViv (gdr->width)	, 0);
		hv_store (hv, "_height"		, 7, newSViv (gdr->height)	, 0);
		hv_store (hv, "_ntile_rows"	,11, newSViv (gdr->ntile_rows)	, 0);
		hv_store (hv, "_ntile_cols"	,11, newSViv (gdr->ntile_cols)	, 0);
		hv_store (hv, "_bpp"		, 4, newSViv (gdr->bpp)		, 0);
		hv_store (hv, "_id"		, 3, autobless (newSViv (drawable_ID), PARAM_DRAWABLE), 0);
		
		if (!stash)
		  stash = gv_stashpv (PKG_GDRAWABLE, 1);
		
		RETVAL = sv_bless (newRV_noinc ((SV*)hv), stash);
	}
	OUTPUT:
	RETVAL

void
gimp_drawable_detach(drawable)
	GDrawable *	drawable

void
gimp_drawable_flush(drawable)
	GDrawable *	drawable

void
gimp_drawable_delete(drawable)
	GDrawable *	drawable

SV *
gimp_drawable_get_tile(gdrawable, shadow, row, col)
	SV *	gdrawable
	gint	shadow
	gint	row
	gint	col
	CODE:
	RETVAL = new_tile (gimp_drawable_get_tile (old_gdrawable (gdrawable), shadow, row, col), gdrawable);
	OUTPUT:
	RETVAL

SV *
gimp_drawable_get_tile2(gdrawable, shadow, x, y)
	SV *	gdrawable
	gint	shadow
	gint	x
	gint	y
	CODE:
	RETVAL = new_tile (gimp_drawable_get_tile2 (old_gdrawable (gdrawable), shadow, x, y), gdrawable);
	OUTPUT:
	RETVAL

void
gimp_tile_ref(tile)
	GTile *	tile

void
gimp_tile_ref_zero(tile)
	GTile *	tile

void
gimp_tile_unref(tile, dirty)
	GTile *	tile
	int	dirty

void
gimp_tile_flush(tile)
	GTile *	tile

void
gimp_tile_cache_size(kilobytes)
	gulong	kilobytes

void
gimp_tile_cache_ntiles(ntiles)
	gulong	ntiles

guint
gimp_tile_width()

guint
gimp_tile_height()

SV *
gimp_pixel_rgn_init(gdrawable, x, y, width, height, dirty, shadow)
	SV *	gdrawable
	int	x
	int	y
	int	width
	int	height
	int	dirty
	int	shadow
	CODE:
	{
		static HV *stash;
		HV *hv = newHV ();
		SV *sv = newSVn (sizeof(GPixelRgn));
		GPixelRgn *pr = (GPixelRgn *)SvPV (sv,na);
		
		gimp_pixel_rgn_init (pr, old_gdrawable (gdrawable), x, y, width, height, dirty, shadow);
		
		hv_store (hv, "_rgn"	, 4, sv				, 0);
		hv_store (hv, "_x"	, 2, newSViv (pr->x)		, 0);
		hv_store (hv, "_y"	, 2, newSViv (pr->y)		, 0);
		hv_store (hv, "_w"	, 2, newSViv (pr->w)		, 0);
		hv_store (hv, "_h"	, 2, newSViv (pr->h)		, 0);
		hv_store (hv, "_rowstride",10, newSViv (pr->rowstride)	, 0);
		hv_store (hv, "_bpp"	, 4, newSViv (pr->bpp)		, 0);
		hv_store (hv, "_dirty"	, 6, newSViv (pr->dirty)	, 0);
		hv_store (hv, "_shadow"	, 7, newSViv (pr->shadow)	, 0);
		hv_store (hv, "_drawable",9, newSVsv (gdrawable)	, 0);
		
		if (!stash)
		  stash = gv_stashpv (PKG_PIXELRGN, 1);
		
		RETVAL = sv_bless (newRV_noinc ((SV*)hv), stash);
	}
	OUTPUT:
	RETVAL

void
gimp_pixel_rgn_resize(sv, x, y, width, height)
	SV *	sv
	int	x
	int	y
	int	width
	int	height
	CODE:
	{
		GPixelRgn *pr = old_pixelrgn (sv);
		HV *hv = (HV*)SvRV(sv);
		
		gimp_pixel_rgn_resize (pr, x, y, width, height);
		
		hv_store (hv, "_x"	, 2, newSViv (pr->x)		, 0);
		hv_store (hv, "_y"	, 2, newSViv (pr->y)		, 0);
		hv_store (hv, "_w"	, 2, newSViv (pr->w)		, 0);
		hv_store (hv, "_h"	, 2, newSViv (pr->h)		, 0);
		hv_store (hv, "_rowstride",10, newSViv (pr->rowstride)	, 0);
	}

SV *
gimp_pixel_rgn_get_pixel(pr, x, y)
	GPixelRgn *	pr
	int	x
	int	y
	CODE:
	RETVAL = newSVn (pr->bpp);
	gimp_pixel_rgn_get_pixel (pr, SvPV(RETVAL,na), x, y);
	OUTPUT:
	RETVAL

SV *
gimp_pixel_rgn_get_row(pr, x, y, width)
	GPixelRgn *	pr
	int	x
	int	y
	int	width
	CODE:
	RETVAL = newSVn (pr->bpp * width);
	gimp_pixel_rgn_get_row (pr, SvPV(RETVAL,na), x, y, width);
	OUTPUT:
	RETVAL

SV *
gimp_pixel_rgn_get_col(pr, x, y, height)
	GPixelRgn *	pr
	int	x
	int	y
	int	height
	CODE:
	RETVAL = newSVn (pr->bpp * height);
	gimp_pixel_rgn_get_col (pr, SvPV(RETVAL,na), x, y, height);
	OUTPUT:
	RETVAL

SV *
gimp_pixel_rgn_get_rect(pr, x, y, width, height)
	GPixelRgn *	pr
	int	x
	int	y
	int	width
	int	height
	CODE:
	RETVAL = newSVn (pr->bpp * width * height);
	gimp_pixel_rgn_get_rect (pr, SvPV(RETVAL,na), x, y, width, height);
	OUTPUT:
	RETVAL

void
gimp_pixel_rgn_set_pixel(pr, data, x, y)
	GPixelRgn *	pr
	SV *	data
	int	x
	int	y
	CODE:
	if (SvCUR (data) != pr->bpp)
	  croak ("gimp_pixel_rgn_set_pixel called with incorrect datasize");
	gimp_pixel_rgn_set_pixel (pr, SvPV(data,na), x, y);

void
gimp_pixel_rgn_set_row(pr, data, x, y)
	GPixelRgn *	pr
	SV *		data
	int	x
	int	y
	CODE:
	if (SvCUR (data) % pr->bpp)
	  croak ("gimp_pixel_rgn_set_row called with incorrect datasize");
	gimp_pixel_rgn_set_row (pr, SvPV(data,na), x, y, SvCUR (data) / pr->bpp);

void
gimp_pixel_rgn_set_col(pr, data, x, y)
	GPixelRgn *	pr
	SV *		data
	int	x
	int	y
	CODE:
	if (SvCUR (data) % pr->bpp)
	  croak ("gimp_pixel_rgn_set_col called with incorrect datasize");
	gimp_pixel_rgn_set_col (pr, SvPV(data,na), x, y, SvCUR (data) / pr->bpp);

void
gimp_pixel_rgn_set_rect(pr, data, x, y, width)
	GPixelRgn *	pr
	SV *		data
	int	x
	int	y
	int	width
	CODE:
	if (SvCUR (data) % (pr->bpp * width))
	  croak ("gimp_pixel_rgn_set_rect called with incorrect datasize");
	gimp_pixel_rgn_set_rect (pr, SvPV(data,na), x, y, width, SvCUR (data) / (pr->bpp * width));

# ??? any possibility to implement these in perl? maybe replacement functions in Gimp.pm?

#gpointer
#gimp_pixel_rgns_register(nrgns, ...)
#	int	nrgns

#gpointer
#gimp_pixel_rgns_process(pri_ptr)
#	gpointer	pri_ptr

PROTOTYPES: DISABLE

# ??? optimize these two functions so tile_*ref will only be called once on
# construction/destruction.

SV *
get_data(tile)
	GTile *	tile
	CODE:
	gimp_tile_ref (tile);
	RETVAL = (SV *)newSVpvn ((char *)tile->data, gimp_tile_width() * gimp_tile_height() * tile->bpp);
	gimp_tile_unref (tile, 0);
	OUTPUT:
	RETVAL

void
set_data(tile, data)
	GTile *	tile
	SV *	data
	CODE:
	if (SvCUR (data) != gimp_tile_width() * gimp_tile_height() * tile->bpp)
	  croak ("set_data called with incorrect datasize");
	
	gimp_tile_ref_zero (tile);
	memcpy (tile->data, SvPV (data, na), SvCUR (data));
	gimp_tile_unref (tile, 1);

BOOT:
	trace_file = PerlIO_stderr ();

#
# this function overrides a pdb function for speed
#

void
gimp_patterns_get_pattern_data(name)
	SV *	name
	PPCODE:
	{
		GParam *return_vals;
		int nreturn_vals;
		
		return_vals = gimp_run_procedure ("gimp_patterns_get_pattern_data",
		                                  &nreturn_vals,
		                                  PARAM_STRING, SvPV (name, na),
		                                  PARAM_END);
		
		if (nreturn_vals == 7
		    && return_vals[0].data.d_status == STATUS_SUCCESS)
		  {
		    EXTEND (sp, 5);
		    
		    PUSHs (newSVpv (return_vals[1].data.d_string, 0));
		    PUSHs (newSViv (return_vals[2].data.d_int32));
		    PUSHs (newSViv (return_vals[3].data.d_int32));
		    PUSHs (newSViv (return_vals[4].data.d_int32));
		    PUSHs (newSVpvn(return_vals[6].data.d_int8array, return_vals[5].data.d_int32));
		  }
		
		gimp_destroy_params (return_vals, nreturn_vals);
	}

PROTOTYPES: ENABLE

# functions using different calling conventions:
#void
#gimp_channel_get_color(channel_ID, red, green, blue)
#	CHANNEL	channel_ID
#	guchar *	red
#	guchar *	green
#	guchar *	blue
#gint32 *
#gimp_image_get_channels(image_ID, nchannels)
#	IMAGE	image_ID
#	gint *	nchannels
#guchar *
#gimp_image_get_cmap(image_ID, ncolors)
#	IMAGE	image_ID
#	gint *	ncolors
#gint32 *
#gimp_image_get_layers(image_ID, nlayers)
#	IMAGE	image_ID
#	gint *	nlayers
#gint32
#gimp_layer_new(image_ID, name, width, height, type, opacity, mode)
#	gint32	image_ID
#	char *	name
#	guint	width
#	guint	height
#	GDrawableType	type
#	gdouble	opacity
#	GLayerMode	mode
#gint32
#gimp_layer_copy(layer_ID)
#	gint32	layer_ID
#void
#gimp_channel_set_color(channel_ID, red, green, blue)
#	gint32	channel_ID
#	guchar	red
#	guchar	green
#	guchar	blue
#gint
#gimp_drawable_mask_bounds(drawable_ID, x1, y1, x2, y2)
#	DRAWABLE	drawable_ID
#	gint *	x1
#	gint *	y1
#	gint *	x2
#	gint *	y2
#void
#gimp_drawable_offsets(drawable_ID, offset_x, offset_y)
#	DRAWABLE	drawable_ID
#	gint *	offset_x
#	gint *	offset_y

# ??? almost synonymous to gimp_list_images

#gint32 *
#gimp_query_images(nimages)
#	int *	nimages

MODULE = Gimp::Lib	PACKAGE = Gimp::UI

#if UI
#if GIMP11

GtkWidget *
_new_pattern_select(dname, ipattern, nameref)
	gchar *	dname
	gchar *	ipattern
	SV *	nameref;
	CODE:
	{
		if (!SvROK (nameref))
		  croak ("last argument to gimp_pattern_select_widget must be scalar ref");
		
		nameref = SvRV (nameref);
		SvUPGRADE (nameref, SVt_PV);
		
		RETVAL = gimp_pattern_select_widget (dname, ipattern,
				gimp_pattern_select_widget_callback, (gpointer) nameref);
	}
	OUTPUT:
	RETVAL

#endif
#endif