/* this file contains code to scale  Pixmaps 
 * Copyright (C) 1998 Sasha Vasko <sashav@sprintmail.com> 
 * 
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "../../configure.h"
#include "../../include/aftersteplib.h"

/*
   #define LOG1(a)       fprintf( stderr, a );
   #define LOG2(a,b)    fprintf( stderr, a, b );
   #define LOG3(a,b,c)    fprintf( stderr, a, b, c );
   #define LOG4(a,b,c,d)    fprintf( stderr, a, b, c, d );
 */

#define LOG1(a)
#define LOG2(a,b)
#define LOG3(a,b,c)
#define LOG4(a,b,c,d)


#ifdef PAGER_BACKGROUND

#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/xpm.h>

#include "../../include/afterstep.h"
#include "../../include/XImage_utils.h"

#define max(a,b) ((a>=b)?a:b)
#define min(a,b) ((a<=b)?a:b)

extern Display *dpy;

Pixmap 
ScalePixmapToWin (Pixmap src, Drawable to_win, unsigned int to_depth)
{
  XImage *srcImage, *trgImage;
  unsigned int src_width = 0, src_height = 0, trg_width = 0, trg_height = 0;
  GC tmp_gc;
  Window root;
  Pixmap trg = 0;

  unsigned int dum;
  int dummy;

  if (src != 0)
    {
      if (XGetGeometry (dpy, src, &root, &dummy, &dummy,
			&src_width, &src_height, &dum, &dum) == 0)
	return None;
      if (XGetGeometry (dpy, to_win, &root, &dummy, &dummy,
			&trg_width, &trg_height, &dum, &dum) == 0)
	return None;

      if ((srcImage = XGetImage (dpy, src, 0, 0, src_width, src_height, AllPlanes, ZPixmap)) != NULL)
	{
	  trg_width = min (trg_width, src_width);
	  trg_height = min (trg_height, src_height);
	  LOG3 ("\n Source width = %u, height = %u", src_width, src_height)
	    LOG3 ("\n Target width = %u, height = %u", trg_width, trg_height)
	    if ((trgImage = ScaleXImageToSize (srcImage, (Dimension) trg_width, (Dimension) trg_height)) != NULL)
	    {
	      /*GC XCreateGC(display, d, valuemask, values) */
	      if ((trg = XCreatePixmap (dpy, to_win, trg_width, trg_height, to_depth)) != 0)
		{
		  if ((tmp_gc = XCreateGC (dpy, trg, 0, NULL)) != NULL)
		    {
		      XPutImage (dpy, trg, tmp_gc, trgImage, 0, 0, 0, 0, trg_width, trg_height);
		      XFreeGC (dpy, tmp_gc);
		    }
		  else
		    {
		      XFreePixmap (dpy, trg);
		      trg = 0;
		    }
		}
	      XDestroyImage (trgImage);
	    }
	  XDestroyImage (srcImage);
	}
    }
  return trg;
}

#endif /*PAGER_BACKGROUND */
