/* bee-effect.c - create bee visual effect images
 * Copyright (C) 2005 Tim Janik
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include <gtk/gtk.h>


#include <stdio.h>

static void
copy_area (guint8 *d, guint dr, const guint8 *s, guint sr, gint dx, gint dy);


int
main (int   argc,
      char *argv[])
{
  gtk_init (&argc, &argv);
  if (argc != 3)
    g_error ("usage: %s <source-image> <dest-image>", argv[0]);
  gchar *src_file = argv[1];
  gchar *dest_file = argv[2];
  GError *gerror = NULL;
  GdkPixbuf *src = gdk_pixbuf_new_from_file (src_file, &gerror);
  if (gerror)
    {
      g_error ("Failed to load image \"%s\": %s\n", src_file, gerror->message);
      g_clear_error (&gerror);
    }
  
  GdkPixbuf *dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 640, 480);
  
  if (0)
    gdk_pixbuf_copy_area (src, 0, 0, 128, 96, dest, 0, 0);
  else
    {
      guint j, k;
      for (k = 0; k < 480 + 48; k += 96)
        for (j = 0; j < 640; j += 128 + 64)
          {
            copy_area (gdk_pixbuf_get_pixels (dest), gdk_pixbuf_get_rowstride (dest),
                       gdk_pixbuf_get_pixels (src), gdk_pixbuf_get_rowstride (src),
                       j, k);
            copy_area (gdk_pixbuf_get_pixels (dest), gdk_pixbuf_get_rowstride (dest),
                       gdk_pixbuf_get_pixels (src), gdk_pixbuf_get_rowstride (src),
                       j + 96, k + 48);
          }
    }
  
  if (!gdk_pixbuf_save (dest, dest_file, "png", &gerror, NULL))
    {
      g_error ("Failed to save image \"%s\": %s\n", dest_file, gerror->message);
      g_clear_error (&gerror);
    }

  return 0;
}


static void
copy_area (guint8 *d, guint dr, const guint8 *s, guint sr, gint dx, gint dy)
{
  guint x, y;
  dx -= 32;
  dy -= 48;
  for (y = 0; y < 96; y++)
    for (x = 0; x < 128; x++)
      {
        double f = 1;
        /* check upper left corner */
        if (f >= 1)
          f = x - (32 - y / 1.5);
        /* check upper right corner */
        if (f >= 1)
          f = (96 + y / 1.5) - x;
        /* check lower left corner */
        if (f >= 1)
          f = x - (32 - (96 - y) / 1.5);
        /* check lower right corner */
        if (f >= 1)
          f = (96 + (96 - y) / 1.5) - x;
        if (f < 0)
          continue;
        f = MIN (f, 1);
        /* copy pixel */
        if (dy + y >= 0 && dy + y < 480 &&
            dx + x >= 0 && dx + x < 640)
          {
            d[(dy + y) * dr + (dx + x) * 3 + 0] = f * s[y * sr + x * 3 + 0];
            d[(dy + y) * dr + (dx + x) * 3 + 1] = f * s[y * sr + x * 3 + 1];
            d[(dy + y) * dr + (dx + x) * 3 + 2] = f * s[y * sr + x * 3 + 2];
          }
      }
}
