/*
 *  Copyright (c) 1999 Sasha Vasko <sashav@sprintmail.com>
 *  most of the code were taken from XFree86 sources
 *  below is the copyright notice for XFree86
 * 
 *  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.
 */

/* $TOG: ImUtil.c /main/48 1998/02/06 17:37:38 kaleb $ */
/*

   Copyright 1986, 1998  The Open Group

   All Rights Reserved.

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
   OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

   Except as contained in this notice, the name of The Open Group shall not be
   used in advertising or otherwise to promote the sale, use or other dealings
   in this Software without prior written authorization from The Open Group.

 */

#include "../configure.h"

#include <X11/Intrinsic.h>
#include <X11/Xlibint.h>
#include <X11/Xutil.h>
#include <stdio.h>

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

#ifdef __STDC__
#define Const const
#else
#define Const
/**/
#endif

/*
 * Macros
 * 
 * The ROUNDUP macro rounds up a quantity to the specified boundary,
 * then truncates to bytes.
 *
 */

#define ROUNDUP(nbytes, pad) ((((nbytes) + ((pad)-1)) / (pad)) * ((pad)>>3))

/* 
 * GetXImageDataSize calculates the size of memory needed to store XImage
 * pixel's data
 * 
 */

unsigned long 
GetXImageDataSize (XImage * ximage)
{
  unsigned long dsize;
  dsize = ximage->bytes_per_line * ximage->height;
  if (ximage->format == XYPixmap)
    dsize = dsize * ximage->depth;

  return dsize;
}

/*
 * CreateXImageBySample
 * 
 * Creates a new image that has same characteristics as an existing one.
 * Allocates the memory necessary for the new XImage data structure. 
 * Pointer to new image is returned. Do not copy pixels from one image 
 * to another
 *
 */
void _XInitImageFuncPtrs (XImage *);

static XImage *
CreateXImageBySample (ximage, width, height)
     XImage *ximage;
     unsigned int width;	/* width in pixels of new subimage */
     unsigned int height;	/* height in pixels of new subimage */
{
  register XImage *subimage;
  unsigned long dsize;
  char *data;

  if ((subimage = (XImage *) calloc (1, sizeof (XImage))) == NULL)
    return (XImage *) NULL;
  subimage->width = width;
  subimage->height = height;
  subimage->xoffset = 0;
  subimage->format = ximage->format;
  subimage->byte_order = ximage->byte_order;
  subimage->bitmap_unit = ximage->bitmap_unit;
  subimage->bitmap_bit_order = ximage->bitmap_bit_order;
  subimage->bitmap_pad = ximage->bitmap_pad;
  subimage->bits_per_pixel = ximage->bits_per_pixel;
  subimage->depth = ximage->depth;
  /*
   * compute per line accelerator.
   */
  if (subimage->format == ZPixmap)
    subimage->bytes_per_line =
      ROUNDUP (subimage->bits_per_pixel * width,
	       subimage->bitmap_pad);
  else
    subimage->bytes_per_line =
      ROUNDUP (width, subimage->bitmap_pad);
  subimage->obdata = NULL;
  _XInitImageFuncPtrs (subimage);

  dsize = GetXImageDataSize (subimage);
  if (((data = calloc (1, dsize)) == NULL) && (dsize > 0))
    {
      Xfree ((char *) subimage);
      return (XImage *) NULL;
    }
  subimage->data = data;

  return subimage;
}

XImage *
CreateXImageAndData (Display * dpy, Visual * visual,
		     unsigned int depth, int format, int offset,
		     unsigned int width, unsigned int height)
{
  register XImage *ximage;
  unsigned long dsize;
  char *data;

  ximage = XCreateImage (dpy, visual, depth, format, offset, NULL, width, height,
			 dpy->bitmap_unit, 0);
  if (ximage != NULL)
    {
      dsize = GetXImageDataSize (ximage);
      if (((data = calloc (1, dsize)) == NULL) && (dsize > 0))
	{
	  Xfree ((char *) ximage);
	  ximage = (XImage *) NULL;
	}
      else
	ximage->data = data;
    }
  return ximage;
}

/*
 * This function will create array of size of the target image length/width
 * filled with x/y coordinates for pixel in source image
 */

void 
Scale (Position * target, Position target_size,
       Position src_start, Position src_end)
{
  Position trg_first_half_end, src_first_half_end;

  if (target_size > 1)
    {
      if (src_start == src_end)
	{
	  register int i = 0;
	  for (; i < target_size; i++)
	    *(target + i) = src_start;
	}
      else
	{
	  src_first_half_end = (src_start + src_end) >> 1;
	  trg_first_half_end = target_size >> 1;
	  Scale (target, trg_first_half_end, src_start, src_first_half_end);
	  Scale (target + trg_first_half_end, target_size - trg_first_half_end,
		 src_first_half_end + 1, src_end);
	}
    }
  else if (target_size == 1)
    *target = (src_start + src_end) >> 1;
}

/*
 * This function will actually create scaled image from source image
 */

XImage *
ScaleXImageToSize (src, width, height)
     XImage *src;
     Position width, height;
{
  XImage *dst;
  Position x, y;
  Position *x_net, *y_net;

  if (width == 0 || height == 0)
    return NULL;

  if (width == src->width && height == src->height)
    {
      dst = XSubImage (src, 0, 0, width, height);
    }
  else
    {
      /* It would be nice average the skipped pixels. */
      if ((dst = CreateXImageBySample (src, width, height)) != NULL)
	{
	  x_net = (Position *) safemalloc (sizeof (Position) * width);
	  y_net = (Position *) safemalloc (sizeof (Position) * height);

	  Scale (x_net, width, 0, src->width - 1);
	  Scale (y_net, height, 0, src->height - 1);

	  for (y = 0; y < height; y++)
	    for (x = 0; x < width; x++)
	      XPutPixel (dst, x, y, XGetPixel (src, x_net[x], y_net[y]));
	  free (x_net);
	  free (y_net);
	}
    }
  return (dst);
}

XImage *
ScaleXImage (src, scale_x, scale_y)
     XImage *src;
     double scale_x, scale_y;
{
  return ScaleXImageToSize (src,
			    (Dimension) max (scale_x * src->width, 1),
			    (Dimension) max (scale_y * src->height, 1));
}
