/*
 * Sample program that shows how to use the prograb.library
 * Copyright (C) 1998 ELSAT s.c. All rights reserved
 *
 * This file was compiled and tested under SAS C version 6.50
 * on an Amiga 1200/030 8MB FAST RAM
 *
 * The prograb.library requires Kickstart 3.0 or higher.
 *
 *
 * This version reworked for GNU compiler by Nicholas Redgrave.
 *
 */

#include <exec/types.h>
#include <exec/memory.h>

#include <libraries/gadtools.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <intuition/gadgetclass.h>
#include <intuition/sghooks.h>
#include <libraries/asl.h>
#include <graphics/displayinfo.h>
#include <resources/misc.h>
#include <graphics/gfxbase.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include <clib/asl_protos.h>
#include <clib/intuition_protos.h>
#include <clib/exec_protos.h>
#include <clib/graphics_protos.h>
#include <clib/misc_protos.h>
#include <clib/icon_protos.h>


#include "prograba.h"
#include "prograbi.h"



struct Screen        *Scr        = 0l;

struct Library *ProGrabBase = NULL;
struct Library *GfxBase = NULL;
struct Library *DOSBase = NULL;
struct Library *IntuitionBase = NULL;

struct TagItem    ScreenTags[] = {
    SA_Left,          0,
    SA_Top,           0,
    SA_Width,         736,
    SA_Height,        512,
    SA_Depth,         8,
    SA_Font,          NULL,
    SA_Type,          CUSTOMSCREEN,
    SA_DisplayID,     HIRES_KEY|LACE,
    SA_Title,         (ULONG)"TEST SCREEN",
    SA_Overscan,      OSCAN_TEXT,
    TAG_DONE };





long InitStuff( void )
{
    DOSBase = OpenLibrary( "dos.library", 37L );
    if( DOSBase == NULL )
        return( 1l );

    IntuitionBase = OpenLibrary("intuition.library", 37L );
    if( IntuitionBase == NULL )
        return( 2l );

    GfxBase = OpenLibrary( "graphics.library", 37L );
    if( GfxBase == NULL )
        return( 1l );


    ProGrabBase = (struct Library *)OpenLibrary("prograb.library", 0L );
    if( ProGrabBase == NULL )
        return( 3l );

    if ( NOT( Scr = OpenScreenTagList( 0l, ScreenTags )))
        return( 4l );


     return 0L;
}

void CleanStuff( void )
{

  if ( Scr        )
      CloseScreen( Scr );

  if( ProGrabBase )
     CloseLibrary( ProGrabBase );

  if( GfxBase )
      CloseLibrary( (struct Library *)GfxBase );

  if( IntuitionBase )
      CloseLibrary( ( struct Library *)IntuitionBase );

  if( DOSBase )
      CloseLibrary( ( struct Library *)DOSBase );

}

void setStandardPalette( struct Screen *scr )
{
    int i, step, colors;
    ULONG value;
    struct RastPort *rp = &scr->RastPort;
    
    colors = 1L<<scr->BitMap.Depth;
    
    {
        step = 256 / colors;
        for(i=0; i<colors; i++ )
        {
           value = step * i;
           value = value<<24;
           
           SetRGB32CM( scr->ViewPort.ColorMap, i, value, value, value );

        }
    }
    
    RemakeDisplay();

}


int main()
{
     struct BitMap *bitmap;
     LONG  bytesPerRow, offset, przesuniecie;
     int i;
     UBYTE *planes[4];
     UBYTE *pmem;

     UBYTE *pBuf;
     LONG BufSize;

     struct EVDigi vdigi;

     if( InitStuff() == 0 )
     {
        /* Set grayscale palette */
        setStandardPalette ( Scr );
        
        /* Initialize private structure of the "prograb.library" */
        vdigi.GfxBase = (struct Library *)GfxBase;
        vdigi.DOSBase = (struct Library *)DOSBase;
        vdigi.IntuitionBase = (struct Library *)IntuitionBase;
        
        /* EVDIGI_Init() will automatically detect the PCMCIA interface. */
                  
        EVDIGI_Init(&vdigi);   

        /* Now that the screen has been opened, we can check
           the computerīs performance in selected screen mode.
           This can take a while. 
           You need to invoke this procedure only if your program
           may be used on accelerated Amigas and/or if it supports 
           Multiscan, DblPAL etc. screen modes.*/

        EVDIGI_TestPerformance( Scr );

        /* Check if there is any video signal connected */
        if( EVDIGI_CheckVideoSignal() )
        {
          /* pmem is a pointer to the decoded 24 bit image */
          pmem = AllocMem( 736*512*3, MEMF_ANY|MEMF_CLEAR);
          if( pmem )
          {
               /* Top edge of the preview will be at line 30 */
               offset = 30 * Scr->BitMap.BytesPerRow;

               /* As we opened the screen in 8 bit,
                  we use the oldest four bitplanes for proper displaying*/

               for(i=0; i<4; i++)
                    planes[i] = (UBYTE *)(Scr->BitMap.Planes[4+i])+offset;

               /* Now, we can grab an image in resolution 736x512.
                  Data wiil not be decoded into RGB but saved at
                  address stored in vdigi.pBuffer. The vdigi.BufferSize
                  contain the data size */

              printf("Capturing first image...\n");
              EVDIGI_GrabImageRaw(&vdigi, EVDIGI_MODE_HIRESLACE_OSCAN);
              pBuf=vdigi.pBuffer;
              BufSize=vdigi.BufferSize;

               /* Display 20 frames.
                  For apprioprate proportions the screen mode should be
                  HIRES because reolution of the preview frame is 320x128 */

               printf("Previev for 20 times...\n");


               for(i=0; i<20; i++)
                    EVDIGI_GrabPreview4bpp( Scr, &planes[0] );


               printf("Capturing & decoding second image...\n");

               /* Now we can change default values for: */
               EVDIGI_SetBrightness(1);      /* default is 0  */
               EVDIGI_SetSaturation(15);     /* default is 10 */
               EVDIGI_SetContrast(11);       /* default is 10 */

               /* Now, we can grab an image in resolution 736x512.
                  RGB data will be saved from the "pmem" address. */
               EVDIGI_GrabImage(&vdigi, EVDIGI_MODE_HIRESLACE_OSCAN, pmem);



               printf("Computing palette...\n");

               /* Compute 256 colors palette and render the image on screen */
               EVDIGI_RenderImage(pmem, Scr, 736, 512);

               vdigi.pBuffer=pBuf;
               vdigi.BufferSize=BufSize;

               /* Now, we decode  grabbed earlier image in resolution 736x512.
                  RGB data will be saved from the "pmem" address.
                  After decoding the raw date buffer memory is returned to
                  the system automatically. If you don not call
                  EVDIGI_DecodeImage you have to free buffer memory
                  yourself. */
               printf("Decoding first captured image...\n");
               EVDIGI_DecodeImage(&vdigi,  pmem);

               printf("Computing palette...\n");

               /* Compute 256 colors palette and render the image on screen */
               EVDIGI_RenderImage(pmem, Scr, 736, 512);


               FreeMem( pmem, 736*512*3 );
          }
        }
        else
          printf("No video signal detected\n");

        printf("Finished\n");

        /* Wait 2 seconds before exiting */
        Delay(100);

        EVDIGI_Cleanup();
     }
     CleanStuff();

}
