/* GEM-View 3.00 PROCESSING Modul: Bild vertikal vertauschen
   (c) 22.10.1993 by Dieter Fiebelkorn
                     Grner Weg 29a
                     D-45768 Marl
                     (GERMANY)
*/

#include <stdio.h>
#include "image.h"
#include "moduls.h"

Image* gvw_main(PROC_Structure *procS, int verbose)
{
    Image  *image;
    char   *data;
    char   *src, *dst;
    long    words, height, planes;
    long    linelen, x, y, p;
    size_t  val;
    
    image = procS->image;
    if (image == NULL)
    {
      procS->events.alert(1, "[3][ | No image for process!  | ][  Abort  ]");
      return(NULL);
    }
    data = image->data;
    if (data == NULL)
    {
      procS->events.alert(1, "[3][ | No data in image!  | ][  Abort  ]");
      return(NULL);
    }

    if (ATARIMONOP(image) || ATARI_RGBP(image))
    {
        linelen = (long)image->width >> 3;
        words   = linelen >> 1;
        height  = (long)image->height;
        planes  = image->depth;
        
        for (p = 0; p < planes; p++)
        {
            src = dst = data + p * linelen * height;
            dst += linelen * height - linelen;
            for (y = height >> 1; y > 0; y--)
            {
                for (x = words >> 1; x > 0; x--)
                    val = *((unsigned long*)src), *((unsigned long*)src)++ = *((unsigned long*)dst), *((unsigned long*)dst)++ = val;
                if ((words & 0x01) != 0)
                    val = *((unsigned short*)src), *((unsigned short*)src)++ = *((unsigned short*)dst), *((unsigned short*)dst)++ = val;
                dst -= linelen + linelen;
            }
        }
    }
    
    if (ATARI__TCP(image))
    {
        linelen = 3L * image->width;
        words   = linelen >> 1;
        height  = (long)image->height;
        
        src = dst = data;
        dst += linelen * height - linelen;
        for (y = height >> 1; y > 0; y--)
        {
            for (x = words >> 1; x > 0; x--)
                val = *((unsigned long*)src), *((unsigned long*)src)++ = *((unsigned long*)dst), *((unsigned long*)dst)++ = val;
            if ((words & 0x01) != 0)
                val = *((unsigned short*)src), *((unsigned short*)src)++ = *((unsigned short*)dst), *((unsigned short*)dst)++ = val;
            dst -= linelen + linelen;
        }
    }
    
    return(image);
}
