/*
 * This file was generated automatically by ExtUtils::ParseXS version 3.34 from the
 * contents of WinPhoto.xs. Do not edit this file, edit WinPhoto.xs instead.
 *
 *    ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "WinPhoto.xs"
/*
  Copyright (c) 1995-2004 Nick Ing-Simmons. All rights reserved.
  This program is free software; you can redistribute it and/or
  modify it under the same terms as Perl itself.
*/
#define PERL_NO_GET_CONTEXT
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>

#include <tkGlue.def>

#include <pTk/tkPort.h>
#include <pTk/tkInt.h>
#include <tkGlue.h>
#include <tkGlue.m>
#include <pTk/tkImgPhoto.h>
#include <pTk/tkImgPhoto.m>
#include "pTk/imgInt.h"
#include "pTk/imgInt.m"
#include <pTk/tkVMacro.h>

#undef memcpy

/*
 * The format record for the Window file format:
 */

static int      FileMatchWindow _ANSI_ARGS_((Tcl_Channel chan, Tcl_Obj * fileName,
		    Tcl_Obj * formatString, int *widthPtr, int *heightPtr, Tcl_Interp *interp));
static int      FileReadWindow  _ANSI_ARGS_((Tcl_Interp *interp,
		    Tcl_Channel chan, Tcl_Obj * fileName, Tcl_Obj * formatString,
		    Tk_PhotoHandle imageHandle, int destX, int destY,
		    int width, int height, int srcX, int srcY));
static int	StringMatchWindow _ANSI_ARGS_((Tcl_Obj *dataObj,
		    Tcl_Obj * formatString, int *widthPtr, int *heightPtr, Tcl_Interp *interp));
static int	StringReadWindow _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Obj *dataObj,
		    Tcl_Obj * formatString, Tk_PhotoHandle imageHandle,
		    int destX, int destY, int width, int height,
		    int srcX, int srcY));

Tk_PhotoImageFormat tkImgFmtWindow = {
	"Window",	   /* name */
	FileMatchWindow,   /* fileMatchProc */
	StringMatchWindow, /* stringMatchProc */
	FileReadWindow,    /* fileReadProc */
	StringReadWindow,  /* stringReadProc */
	NULL,              /* fileWriteProc */
	NULL,              /* stringWriteProc */
};

static int
FileMatchWindow(chan, fileName, formatString, widthPtr, heightPtr, interp)
    Tcl_Channel chan;		/* The image file, open for reading. */
    Tcl_Obj * fileName;		/* The name of the image file. */
    Tcl_Obj * formatString;		/* User-specified format string, or NULL. */
    int *widthPtr, *heightPtr;	/* The dimensions of the image are
				 * returned here if the file is a valid
				 * raw Window file. */
    Tcl_Interp *interp;
{
	return 0;
}


static int
FileReadWindow(interp, chan, fileName, formatString, imageHandle, destX, destY,
	width, height, srcX, srcY)
    Tcl_Interp *interp;		/* Interpreter to use for reporting errors. */
    Tcl_Channel chan;		/* The image file, open for reading. */
    Tcl_Obj * fileName;		/* The name of the image file. */
    Tcl_Obj * formatString;		/* User-specified format string, or NULL. */
    Tk_PhotoHandle imageHandle;	/* The photo image to write into. */
    int destX, destY;		/* Coordinates of top-left pixel in
				 * photo image to be written to. */
    int width, height;		/* Dimensions of block of photo image to
				 * be written to. */
    int srcX, srcY;		/* Coordinates of top-left pixel to be used
				 * in image being read. */
{
 return TCL_ERROR;
}


static int
StringMatchWindow(dataObj, formatString, widthPtr, heightPtr, interp)
    Tcl_Obj *dataObj;		/* the object containing the image data */
    Tcl_Obj * formatString;		/* the image format string */
    int *widthPtr;		/* where to put the string width */
    int *heightPtr;		/* where to put the string height */
    Tcl_Interp *interp;
{
 long val = 0;
 if (Tcl_GetLongFromObj(interp, dataObj, &val) == TCL_OK)
  {
   Tk_Window tkwin = Tk_MainWindow(interp);
   Display *dpy = Tk_Display(tkwin);
   Window  win = val;
   XWindowAttributes attr;
   XGetWindowAttributes(dpy, win, &attr);
   *widthPtr  = attr.width;
   *heightPtr = attr.height;
   return 1;
  }
 return 0;
}

static int
StringReadWindow(interp,dataObj,formatString,imageHandle,
	destX, destY, width, height, srcX, srcY)
    Tcl_Interp *interp;		/* interpreter for reporting errors in */
    Tcl_Obj *dataObj;		/* object containing the image */
    Tcl_Obj * formatString;		/* format string if any */
    Tk_PhotoHandle imageHandle;	/* the image to write this data into */
    int destX, destY;		/* The rectangular region of the  */
    int  width, height;		/*   image to copy */
    int srcX, srcY;
{
 long val = 0;
 if (Tcl_GetLongFromObj(interp, dataObj, &val) == TCL_OK)
  {
   int x;
   int y;
   unsigned char *p;
   Tk_PhotoImageBlock block;
   Tk_Window tkwin = Tk_MainWindow(interp);
   Display *dpy = Tk_Display(tkwin);
   Window  win = val;
   XWindowAttributes attr;
   XImage *img;
   XColor color;
   Tcl_HashTable ctable;
   XGetWindowAttributes(dpy, win, &attr);
   Tcl_InitHashTable(&ctable, TCL_ONE_WORD_KEYS);
   if (srcX+width > attr.width)
    {
     width = attr.width - srcX;
    }
   if (srcY+height > attr.height)
    {
     height = attr.height - srcY;
    }
   if (width <= 0 || height <= 0)
    {
     return TCL_ERROR;
    }
   img = XGetImage(dpy, win, srcX, srcY, width, height,
                   (unsigned long) -1, XYPixmap);

   Tk_PhotoGetImage(imageHandle, &block);
   block.offset[3] = (block.pixelSize > 3) ? 3 : 0;
   block.width = width;
   block.pitch = block.pixelSize * width;
   block.height = height;
   block.pixelPtr = (unsigned char *)
                         ckalloc((unsigned) block.pixelSize * width * height);
   p = block.pixelPtr;
   for (y = 0; y < height; y++)
    {
     for (x = 0; x < width; x++)
      {
       unsigned char *p = block.pixelPtr+(y*block.pitch)+(x*block.pixelSize);
       Tcl_HashEntry *he;
       int new = 0;
       ClientData cd = 0;
       color.pixel = (*img->f.get_pixel)(img, srcX+x, srcY+y);
       he = Tcl_CreateHashEntry(&ctable,(char *) color.pixel, &new);
       if (new)
        {
         XQueryColors(dpy, attr.colormap, &color, 1);
         p[0] = color.red   >> 8;
         p[1] = color.green >> 8;
         p[2] = color.blue  >> 8;
         if (block.pixelSize > 3)
          p[3] = 255;
         Copy(p,&cd,block.pixelSize,unsigned char);
         Tcl_SetHashValue(he, cd);
        }
       else
        {
         cd = Tcl_GetHashValue(he);
         Copy(&cd, p, block.pixelSize,unsigned char);
        }
      }
    }
   Tk_PhotoExpand(imageHandle, destX + width, destY + height);
   Tk_PhotoPutBlock(imageHandle, &block, destX, destY, width, height, TK_PHOTO_COMPOSITE_SET);
   Tcl_DeleteHashTable(&ctable);
   (*img->f.destroy_image)(img);
   ckfree((char *) block.pixelPtr);
  }
 else
  {
   croak("Cannot get window from %"SVf,dataObj);
  }
 return TCL_OK;
}


DECLARE_VTABLES;
DECLARE_PHOTO;

#line 213 "WinPhoto.c"
#ifndef PERL_UNUSED_VAR
#  define PERL_UNUSED_VAR(var) if (0) var = var
#endif

#ifndef dVAR
#  define dVAR		dNOOP
#endif


/* This stuff is not part of the API! You have been warned. */
#ifndef PERL_VERSION_DECIMAL
#  define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#endif
#ifndef PERL_DECIMAL_VERSION
#  define PERL_DECIMAL_VERSION \
	  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#endif
#ifndef PERL_VERSION_GE
#  define PERL_VERSION_GE(r,v,s) \
	  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
#endif
#ifndef PERL_VERSION_LE
#  define PERL_VERSION_LE(r,v,s) \
	  (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
#endif

/* XS_INTERNAL is the explicit static-linkage variant of the default
 * XS macro.
 *
 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
 * "STATIC", ie. it exports XSUB symbols. You probably don't want that
 * for anything but the BOOT XSUB.
 *
 * See XSUB.h in core!
 */


/* TODO: This might be compatible further back than 5.10.0. */
#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
#  undef XS_EXTERNAL
#  undef XS_INTERNAL
#  if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
#    define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
#    define XS_INTERNAL(name) STATIC XSPROTO(name)
#  endif
#  if defined(__SYMBIAN32__)
#    define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
#    define XS_INTERNAL(name) EXPORT_C STATIC XSPROTO(name)
#  endif
#  ifndef XS_EXTERNAL
#    if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
#      define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
#      define XS_INTERNAL(name) STATIC void name(pTHX_ CV* cv __attribute__unused__)
#    else
#      ifdef __cplusplus
#        define XS_EXTERNAL(name) extern "C" XSPROTO(name)
#        define XS_INTERNAL(name) static XSPROTO(name)
#      else
#        define XS_EXTERNAL(name) XSPROTO(name)
#        define XS_INTERNAL(name) STATIC XSPROTO(name)
#      endif
#    endif
#  endif
#endif

/* perl >= 5.10.0 && perl <= 5.15.1 */


/* The XS_EXTERNAL macro is used for functions that must not be static
 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
 * macro defined, the best we can do is assume XS is the same.
 * Dito for XS_INTERNAL.
 */
#ifndef XS_EXTERNAL
#  define XS_EXTERNAL(name) XS(name)
#endif
#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) XS(name)
#endif

/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
 * internal macro that we're free to redefine for varying linkage due
 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
 */

#undef XS_EUPXS
#if defined(PERL_EUPXS_ALWAYS_EXPORT)
#  define XS_EUPXS(name) XS_EXTERNAL(name)
#else
   /* default to internal */
#  define XS_EUPXS(name) XS_INTERNAL(name)
#endif

#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)

/* prototype to pass -Wmissing-prototypes */
STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params);

STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params)
{
    const GV *const gv = CvGV(cv);

    PERL_ARGS_ASSERT_CROAK_XS_USAGE;

    if (gv) {
        const char *const gvname = GvNAME(gv);
        const HV *const stash = GvSTASH(gv);
        const char *const hvname = stash ? HvNAME(stash) : NULL;

        if (hvname)
	    Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
        else
	    Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
	Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#define croak_xs_usage        S_croak_xs_usage

#endif

/* NOTE: the prototype of newXSproto() is different in versions of perls,
 * so we define a portable version of newXSproto()
 */
#ifdef newXS_flags
#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
#else
#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
#endif /* !defined(newXS_flags) */

#if PERL_VERSION_LE(5, 21, 5)
#  define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
#else
#  define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
#endif

#line 357 "WinPhoto.c"
#ifdef __cplusplus
extern "C"
#endif
XS_EXTERNAL(boot_Tk__WinPhoto); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_Tk__WinPhoto)
{
#if PERL_VERSION_LE(5, 21, 5)
    dVAR; dXSARGS;
#else
    dVAR; dXSBOOTARGSXSAPIVERCHK;
#endif

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#if PERL_VERSION_LE(5, 21, 5)
    XS_VERSION_BOOTCHECK;
#  ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#  endif
#endif


    /* Initialisation Section */

#line 208 "WinPhoto.xs"
 {
  IMPORT_VTABLES;
  IMPORT_PHOTO;
  Tk_CreatePhotoImageFormat(&tkImgFmtWindow);
 }

#line 389 "WinPhoto.c"

    /* End of Initialisation Section */

#if PERL_VERSION_LE(5, 21, 5)
#  if PERL_VERSION_GE(5, 9, 0)
    if (PL_unitcheckav)
        call_list(PL_scopestack_ix, PL_unitcheckav);
#  endif
    XSRETURN_YES;
#else
    Perl_xs_boot_epilog(aTHX_ ax);
#endif
}

