/*****************************************************************************
 *  File: move_image.c
 *
 *  This file is the top level of the application selector.
 *
 ****************************************************************************/


#define LIVE_IMAGE_W (640*3/4)
#define LIVE_IMAGE_H (480*3/4)

//#include <string.h>
//#include <io.h>

//#include <stdio.h>
//#include <stdlib.h>
//#include <unistd.h>

//#include "system.h"

//#include "altera_avalon_pio_regs.h"

#include "VIP_ctrl\vip_wrapper_for_c_func.h"

//#define ALT_VIP_CUSTOM_TPG_0_BASE 0x0a000200

#define FM 1
#define  MOVE_STEP 1

const int w_max=800; // Max screen width 
const int h_max=480; // Max screen height

static int x1; // frame origin x
static int y1; // frame origin y
static int w1; // frame width
static int h1; // frame height

int motion_count=0;

void  
move_image(int xini, int yini, int wini, int hini, int direcini){

static int direc=1;  // 0:stop, 1:right, 2:down, 3:left, 4:up, 5:zoom out, 6: zoom up

  if (xini>0) x1 = xini;
  if (yini>0) y1 = yini;
  if (wini>0) w1 = wini;
  if (hini>0) h1 = hini;
  if (direcini>=0) direc = direcini;
   
  if (x1> (w_max-w1 -4) ) x1 = (w_max-w1 -4);
  if (y1> (h_max-h1 ) ) y1 = (h_max-h1 );

  switch(direc) {
  case 1:
    // move right
    if (x1 < (w_max-w1 -4)) {
      Mixer_set_layer_position(FM, x1, y1);
      x1+=MOVE_STEP;
    }
    else {
      x1 = (w_max-w1 -4);
      direc = 2;
    }
    break;

  case 2:
    // move down
    if (y1 < (h_max-h1 )) {
      Mixer_set_layer_position(FM, x1, y1);
      y1+=MOVE_STEP;
    }
    else {
      y1 = (h_max-h1 ) ;
      direc = 3;
    }
    break;

  case 3:
    // move left
    if ( x1> 0){
      Mixer_set_layer_position(FM, x1, y1);
      x1-=MOVE_STEP;
    }
    else {
      x1 = 0;
      direc = 4;
    }
    break;

  case 4:
    // move up
    if (y1> 0){
      Mixer_set_layer_position(FM, x1, y1);
      y1-=MOVE_STEP;
    }
    else {
      y1 = 0;
      direc = 1;
      motion_count++;
    }
    break;
  }

}


void
move_image_center(int xini, int yini, int wini, int hini, int direcini){

static int direc=5;  // 0:stop, 1:right, 2:down, 3:left, 4:up, 5:zoom out, 6: zoom up

  if (xini>0) x1 = xini;
  if (yini>0) y1 = yini;
  if (wini>0) w1 = wini;
  if (hini>0) h1 = hini;
  if (direcini>=0) direc = direcini;

int center_x;
int center_y;
  center_x = (w_max-w1)>>1;
  center_y = (h_max-h1)>>1;

  if (x1 < center_x) direc=1;
  else if (x1 > center_x) direc = 3;
  else if (y1 < center_y) direc = 2;
  else if (y1 > center_y) direc = 4;
  else {motion_count++; return;}
   
  switch(direc) {
  case 1:
    x1+=MOVE_STEP;
    // move right
    if (x1 < (w_max-w1 )/2) {
      Mixer_set_layer_position(FM, x1, y1);
    }
    else {
      x1 = (w_max-w1 )/2;
      direc = 2;
    }
    break;

  case 2:
    y1+=MOVE_STEP;
    if (y1 < (h_max-h1 )/2) {
      Mixer_set_layer_position(FM, x1, y1);
    }
    else {
      y1 = (h_max-h1 )/2;
      direc = 1;
      motion_count++;
    }
    break;

  case 3:
    x1-=MOVE_STEP;
    // move left
    if ( x1> (w_max-w1 )/2){
      Mixer_set_layer_position(FM, x1, y1);
    }
    else {
      x1= (w_max-w1 )/2;
      direc = 4;
    }
    break;

  case 4:
    y1-=MOVE_STEP;
    // move up
    if (y1> (h_max-h1 )/2){
      Mixer_set_layer_position(FM, x1, y1);
    }
    else {
      y1= (h_max-h1 )/2;
      direc = 1;
      motion_count++;
    }
    break;
  }
}

void 
zoom_image(int xini, int yini, int wini, int hini, int direcini){

static int direc=5;  // 0:stop, 1:right, 2:down, 3:left, 4:up, 5:reduce size, 6: enlarge size , 7: reduce to default,
 
 int w_step = 4;
 int h_step = 3;

  if (xini>0) x1 = xini;
  if (yini>0) y1 = yini;
  if (wini>0) w1 = wini;
  if (hini>0) h1 = hini;
  if (direcini>=0) direc = direcini;

  switch(direc) {
  case 5:
    // make smaller size 
    if (w1>50 && h1>40) {
      x1 = (w_max - w1)/2;
      y1 = (h_max - h1)/2;

      Scaler_set_output_size( w1, h1);
      Mixer_set_layer_position(FM, x1, y1);

      w1-=w_step, h1-=h_step;
    }
    else {
      direc = 6;
    }
    break;

  case 6:
    // make lager size
    if (w1<w_max && h1<h_max) {
      x1 = (w_max - w1)/2;
      y1 = (h_max - h1)/2;

      Scaler_set_output_size( w1, h1);
      Mixer_set_layer_position(FM, x1, y1);

      w1+=w_step, h1+=h_step;
    }
    else {
      direc = 7;
      w1=h_max*4/3;h1=h_max;
    }
    break;

  case 7:
    // make normal size
    if (w1> LIVE_IMAGE_W  && h1> LIVE_IMAGE_H) {
      x1 = (w_max - w1)/2;
      y1 = (h_max - h1)/2;

      Scaler_set_output_size( w1, h1);
      Mixer_set_layer_position(FM, x1, y1);

      w1-=w_step, h1-=h_step;
    }
    else {
      direc = 5;
      w1 = LIVE_IMAGE_W;
      h1 = LIVE_IMAGE_H;
      motion_count++;
    }
    break;

  }

}
