# include "data.h"

int roll (int argc, char **argv) {
  
  int Nbytes, Nextra;
  int dX, dx, dy, nx, ny;
  Buffer *buf;

  if (argc != 4) {
    gprint (GP_ERR, "USAGE: roll <buffer> dx dy\n");
    return (FALSE);
  }

  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);

  dx = atof (argv[2]);
  dy = atof (argv[3]);
  if (dy != 0) {
    gprint (GP_ERR, "only x rolls implemented for now\n");
  }

  /* if (dx < 0), we are moving the start position back by dx pixels,
     if (dx > 0), we are moving the start position forward by dx pixels */
  
  nx = buf[0].matrix.Naxis[0];
  ny = buf[0].matrix.Naxis[1];

  dX = abs(dx);
  Nbytes = nx * ny * sizeof (float);
  Nextra = (nx * ny + dX) * sizeof (float);

  REALLOCATE (buf[0].matrix.buffer, char, Nextra);

  if (dx < 0) {
    memmove (buf[0].matrix.buffer, &buf[0].matrix.buffer[dX*sizeof(float)], Nbytes);
  } else {
    memmove (&buf[0].matrix.buffer[dX*sizeof(float)], buf[0].matrix.buffer, Nbytes);
  }

  REALLOCATE (buf[0].matrix.buffer, char, Nbytes);

  return (TRUE);

}

