#define NULL 0
char *malloc();

void MREAD(fd,buf,nbytes,routine) 
     int fd,nbytes;
     char *buf,*routine;
{
  int cc;
  char tmpstr[132];
  cc = read(fd,buf,nbytes);
  if (cc < 0) 
    {
      sprintf(tmpstr,"%s: read error",routine);
      perror(tmpstr);
      exit(-1);
    }
}

char *
readqdv(infile,xsize,ysize,pal,palsize)
     int infile,*palsize,*xsize,*ysize;
     char *pal;
{
  short x,y;
  char palsc;
  int palbytes,nspace;
  static char *imageP;
  static char rname[] = "readqdv";

  MREAD(infile,&x,2,rname);
  MREAD(infile,&y,2,rname);
  MREAD(infile,&palsc,1,rname);
  *xsize = x;
  *ysize = y;
  *palsize = (unsigned int) ( 0xff & palsc);

  palbytes = 3 * (*palsize);
  nspace = *xsize * *ysize;
  if ((imageP = malloc((unsigned)nspace)) == NULL)
    {
      perror("qdvread: Can't malloc image space\n");
      exit(0);
    }
  MREAD(infile,pal,palbytes,rname);
  MREAD(infile,imageP,nspace,rname);
  return(imageP);
}

