Beispiel #1
0
/*----------------------------------------------------------------------
            Parameters:

           Description:
              return the number of frames stored in the file 'fname'
----------------------------------------------------------------------*/
int
ImageNumFrames(const char*fname)
{
  IMAGE  I ;
  FILE   *fp ;
  int    frame, type, ecode, nframes ;
  char   buf[100] ;

  ImageUnpackFileName(fname, &frame, &type, buf) ;
  fname = buf ;
  if ((frame >= 0) || (type != HIPS_IMAGE))
    return(1) ;

  fp = fopen(fname, "rb") ;
  if (!fp)
    ErrorReturn(ERROR_NO_FILE,
                (ERROR_NO_FILE, "ImageNumFrame(%s) could not open file\n",
                 fname)) ;

  ecode = fread_header(fp, &I, fname) ;
  if (ecode != HIPS_OK)
    ErrorReturn(ERROR_NO_FILE,
                (ERROR_NO_FILE,
                 "ImageNumFrame: fread_header failed (%d)\n",ecode));

  nframes = I.num_frame ;
  fclose(fp) ;
  free_hdrcon(&I) ;
  return(nframes) ;
}
Beispiel #2
0
void hh_load(value in_filename) {
  CAMLparam1(in_filename);
  FILE* fp = fopen(String_val(in_filename), "rb");

  if (fp == NULL) {
    unix_error(errno, "fopen", in_filename);
  }

  fread_header(fp);

  read_all(fileno(fp), (void*)&heap_init_size, sizeof heap_init_size);

  int compressed_size = 0;
  read_all(fileno(fp), (void*)&compressed_size, sizeof compressed_size);
  char* chunk_start = save_start();

  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  pthread_t thread;
  decompress_args args;
  int thread_started = 0;

  // see hh_save for a description of what we are parsing here.
  while (compressed_size > 0) {
    char* compressed = malloc(compressed_size * sizeof(char));
    assert(compressed != NULL);
    uintptr_t chunk_size = 0;
    read_all(fileno(fp), (void*)&chunk_size, sizeof chunk_size);
    read_all(fileno(fp), compressed, compressed_size * sizeof(char));
    if (thread_started) {
      intptr_t success = 0;
      int rc = pthread_join(thread, (void*)&success);
      free(args.compressed);
      assert(rc == 0);
      assert(success);
    }
    args.compressed = compressed;
    args.compressed_size = compressed_size;
    args.decompress_start = chunk_start;
    args.decompressed_size = chunk_size;
    pthread_create(&thread, &attr, (void* (*)(void*))decompress, &args);
    thread_started = 1;
    chunk_start += chunk_size;
    read_all(fileno(fp), (void*)&compressed_size, sizeof compressed_size);
  }

  if (thread_started) {
    int success;
    int rc = pthread_join(thread, (void*)&success);
    free(args.compressed);
    assert(rc == 0);
    assert(success);
  }

  fclose(fp);
  CAMLreturn0;
}
int list(FILE *archive) {
	unsigned long long bytes = 0;
	fseek(archive, 0, SEEK_SET);
	Header *header = malloc(sizeof(Header));
	while(!fread_header(header, archive)) {
		//fprintf(stderr, "\nNamesize: %d\n", header->namesize);
		fprintf(stderr, "\nName: %s\n", header->filename);
		bytes = header->filesize / 8 + (header->filesize % 8 != 0);
		fprintf(stderr, "Original size: %llu bytes\n", header->originalsize);

		fseek(archive, bytes, SEEK_CUR);
	}
}
Beispiel #4
0
void hh_load_dep_table(value in_filename) {
  CAMLparam1(in_filename);
  struct timeval tv;
  gettimeofday(&tv, NULL);

  FILE* fp = fopen(String_val(in_filename), "rb");

  if (fp == NULL) {
    unix_error(errno, "fopen", in_filename);
  }

  fread_header(fp);

  int compressed_size = 0;
  read_all(fileno(fp), (void*)&compressed_size, sizeof compressed_size);

  char* compressed = malloc(compressed_size * sizeof(char));
  assert(compressed != NULL);
  read_all(fileno(fp), compressed, compressed_size * sizeof(char));

  int actual_compressed_size = LZ4_decompress_fast(
      compressed,
      (char*)deptbl,
      DEP_SIZE_B);
  assert(compressed_size == actual_compressed_size);
  tv = log_duration("Loading file", tv);

  uintptr_t slot = 0;
  unsigned long hash = 0;
  for (slot = 0; slot < DEP_SIZE; ++slot) {
    hash = deptbl[slot];
    if (hash != 0) {
      htable_add(deptbl_bindings, hash, hash);
    }
  }

  fclose(fp);

  log_duration("Bindings", tv);
  CAMLreturn0;
}
Beispiel #5
0
/*----------------------------------------------------------------------
            Parameters:
              fname - the name of the file to read from

           Description:
              read a hips image from a file, and allocate an image
              header and data space for it.  Returns the newly
              allocated image.
----------------------------------------------------------------------*/
int
ImageReadInto(const char*fname, IMAGE *I, int image_no)
{
  FILE   *fp ;
  int    ecode ;

  fp = fopen(fname, "rb") ;
  if (!fp)
    ErrorPrintf(ERROR_NO_FILE, "ImageReadInto(%s) failed\n", fname) ;

  ecode = fread_header(fp, I, fname) ;
  if (ecode != HIPS_OK)
    ErrorExit(ERROR_NO_FILE, "ImageReadInto(%s): fread_header failed (%d)\n",
              fname, ecode) ;
  ecode = fread_image(fp, I, image_no, fname) ;
  if (ecode != HIPS_OK)
    ErrorExit(ERROR_NO_FILE, "ImageReadInto(%s): fread_image failed (%d)\n",
              fname, ecode) ;

  fclose(fp) ;

  return(0) ;
}
Beispiel #6
0
/*----------------------------------------------------------------------
            Parameters:

           Description:
              append an image to the end of a hips sequence file, incrementing
              the number of frames recorded in the header.
----------------------------------------------------------------------*/
int
ImageAppend(IMAGE *I, const char*fname)
{
  FILE   *fp ;
  int    ecode, frame = 0, nframes ;
  IMAGE  Iheader, *Iframe ;
  char   tmpname[200] ;

  fp = fopen(fname, "r+b") ;
#if 0
  if (!fp)
    ErrorReturn(-1, (ERROR_NO_FILE, "ImageAppend(%s) failed\n", fname)) ;
#endif

  if (!fp)
    return(ImageWrite(I, fname)) ;

  ecode = fread_header(fp, &Iheader, fname) ;
  if (ecode != HIPS_OK)
    ErrorExit(ERROR_NO_FILE, "ImageAppend: fread_header failed (%d)\n",ecode);

  /* increment # of frames, and update file header */
  Iheader.num_frame++ ;
  nframes = Iheader.num_frame ;
  if (nframes == 10 || nframes == 100 || nframes == 1000)
  {
    /* header size will grow by 1 byte, must copy whole file (ughhh) */
    fclose(fp) ;
    strcpy(tmpname,FileTmpName(NULL)) ;
    FileRename(fname, tmpname) ;

    /* write out new header */
    fp = fopen(fname, "wb") ;
    if (!fp)
      ErrorReturn(-1, (ERROR_NO_FILE, "ImageAppend(%s) failed\n", fname)) ;

    ecode = fwrite_header(fp, &Iheader, fname) ;
    if (ecode != HIPS_OK)
      ErrorExit(ERROR_NO_FILE,"ImageAppend: fwrite_header failed (%d)\n",ecode);

    nframes = Iheader.num_frame - 1 ;
    for (frame = 0 ; frame < nframes ; frame++)
    {
      Iframe = ImageReadFrames(tmpname, frame, 1) ;
      if (!Iframe)
        ErrorReturn(-3, (ERROR_BADFILE,
                         "ImageAppend: could not read %dth frame", frame)) ;
      ecode = fwrite_image(fp, Iframe, frame, fname) ;
      if (ecode != HIPS_OK)
        ErrorReturn(-4, (ERROR_BADFILE,
                         "ImageAppend: fwrite_image frame %d failed (%d)\n",
                         ecode,frame));
    }
    unlink(tmpname) ;
  }
  else    /* seek back to start and increment # of frames */
  {
    if (fseek(fp, 0L, SEEK_SET) < 0)
      ErrorReturn(-2,(ERROR_BADFILE,"ImageAppend(%s): could not seek to end"));
    ecode = fwrite_header(fp, &Iheader, fname) ;
    if (ecode != HIPS_OK)
      ErrorExit(ERROR_NO_FILE,"ImageAppend: fwrite_header failed (%d)\n",ecode);
  }

  if (fseek(fp, 0L, SEEK_END) < 0)
    ErrorReturn(-2, (ERROR_BADFILE, "ImageAppend(%s): could not seek to end"));

  ecode = fwrite_image(fp, I, frame, "fwrite") ;
  if (ecode != HIPS_OK)
    ErrorReturn(-1, (ERROR_BADFILE,
                     "ImageAppend: fwrite_image frame %d failed (%d)\n",ecode,frame));

  free_hdrcon(&Iheader) ;
  fclose(fp) ;
  return(NO_ERROR) ;
}
Beispiel #7
0
/*-----------------------------------------------------
        Parameters:

        Returns value:

        Description
------------------------------------------------------*/
IMAGE *
ImageFReadHeader(FILE *fp, const char*fname)
{
  IMAGE   *I = NULL ;
  int     ecode ;
  int     type, frame ;
  char    buf[100] ;

  strcpy(buf, fname) ;   /* don't destroy callers string */

  ImageUnpackFileName(buf, &frame, &type, buf) ;

  I = (IMAGE *)calloc(1, sizeof(IMAGE)) ;
  if (!I)
    ErrorExit(ERROR_NO_MEMORY, "ImageReadHeader: could not allocate header\n");

  switch (type)
  {
  case TIFF_IMAGE:
    if (TiffReadHeader(buf, I) == NULL)
      return(NULL) ;
    break ;
  case RGBI_IMAGE:
    RGBReadHeader(buf, I);
    break;
  case MATLAB_IMAGE:
  {
    MATFILE mf ;

    MatReadHeader0(fp, &mf) ;
    init_header(I, "matlab", "seq", 1, "today", (int)mf.mrows, (int)mf.ncols,
                mf.imagf ? PFCOMPLEX : PFFLOAT, 1, "temp") ;
  }
  break ;
  case JPEG_IMAGE:
    JPEGReadHeader(fp, I);
    break ;
  case PGM_IMAGE:
    PGMReadHeader(fp, I);
    break;
  case PPM_IMAGE:
    PPMReadHeader(fp, I);
    break;
  case PBM_IMAGE:
    PBMReadHeader(fp, I);
    break;
  case HIPS_IMAGE:
  default:
    ecode = fread_header(fp, I, buf) ;
    if (ecode != HIPS_OK)
    {
      fclose(fp) ;
      ErrorReturn(NULL, (ERROR_NO_FILE,
                         "ImageReadHeader(%s): fread_header failed (%d)\n",
                         buf, ecode)) ;
    }
    break ;
  }

  return(I) ;
}
Beispiel #8
0
/*-----------------------------------------------------
        Parameters:

        Returns value:

        Description
------------------------------------------------------*/
IMAGE *
ImageFRead(FILE *fp, const char*fname, int start, int nframes)
{
  int    ecode, end_frame, frame, count = 1;
  IMAGE  *I ;
  byte   *startpix, end = END_UNDEF;

  if (!fname)
    fname = "ImageFRead" ;

  I = (IMAGE *)calloc(1, sizeof(IMAGE)) ;
  if (!I)
    ErrorExit(ERROR_NO_MEMORY,"ImageFRead: could not allocate header\n") ;

  ecode = fread_header(fp, I, fname) ;
  if (ecode != HIPS_OK)
    ErrorExit(ERROR_NO_FILE, "ImageFRead: fread_header failed (%d)\n",ecode);

  if (endian == END_UNDEF)
    endian = FindMachineEndian();

  if (findparam(I, "endian"))
    getparam(I, "endian", PFBYTE, &count, &end);

  if (start < 0)    /* read all frames */
  {
    start = 0 ;
    nframes = I->num_frame ;
  }
  else              /* read only specified frames */
  {
    if (fseek(fp, (long)I->sizeimage*(long)start, SEEK_CUR) < 0)
    {
      ImageFree(&I) ;
      ErrorReturn(NULL,
                  (ERROR_BADFILE,
                   "ImageFRead(%s, %d) - could not seek to specified frame",
                   fname, start)) ;
    }
  }

  if (nframes < 0)
    nframes = I->num_frame - start + 1 ;

  end_frame = start + nframes - 1 ;
  if (end_frame >= I->num_frame)
    ErrorReturn(NULL,
                (ERROR_BADFILE,
                 "ImageFRead(%s, %d) - frame out of bounds", fname,end_frame));
  I->num_frame = nframes ;
  if (ImageAllocBuffer(I) != NO_ERROR)
    ErrorExit(Gerror, "ImageAllocBuffer failed") ;

  startpix = I->image ;
  for (frame = start ; frame <= end_frame ; frame++)
  {
    ecode = fread_image(fp, I, frame, fname) ;
    if (ecode != HIPS_OK)
      ErrorExit(ERROR_NO_FILE, "ImageFRead: fread_image failed (%d)\n", ecode);
    I->image += I->sizeimage ;
  }
  I->image = startpix ;

  /* We only swap endians if there wasn't an endian parameter and the image is
     invalid (ie.  values in the image seem to be extreme) OR the endian of
     the machine does not match the endian of the image */

  switch (end)
  {
  case END_UNDEF:
    if (!ImageValid(I))
      ImageSwapEndian(I);
    break;
  case END_BIG:
  case END_SMALL:
    if (end != endian)
      ImageSwapEndian(I);
    break;
  }

  return(I) ;
}
Beispiel #9
0
int
LPAFreadImageAnswer(LPAF *lpaf, int current)
{
  char   *fullname, fname[100] ;
  FILE   *fp ;
  IMAGE  Iheader ;
  int    i, ecode, frame, current_frame ;
  LP_BOX *lpb ;
  struct extpar *xp ;
#ifdef _MSDOS
  long   *parms ;
#else
  int    *parms ;
#endif

  fullname = lpaf->filelist[current] ;

  ImageUnpackFileName(fullname, &current_frame, &i, fname) ;

  fp = fopen(fname, "rb") ;
  if (!fp)
    ErrorReturn(-1,(ERROR_NO_FILE,"LPAFreadImageAnswer(%d): could not open %s",
                    current, fname)) ;

  ecode = fread_header(fp, &Iheader, fname) ;
  fclose(fp) ;
  if (ecode)
    ErrorReturn(-2, (ERROR_BADFILE,
                     "LPAFreadImageAnswer(%s): could not read header",fname));

  if (Iheader.numparam < Iheader.num_frame)
    return(0) ;

  /* read answer from header */
#if 0
  fprintf(stderr, "reading lp values from %dth entry in image file\n",
          current_frame);
#endif
  lpb = &lpaf->coords[current] ;
  for (frame = 0, xp = Iheader.params ; xp ; xp = xp->nextp)
    if (frame++ == current_frame)
      break ;

  /*
   if hips file created on Sun, then the parameters are actually longs.
  */
#ifndef _MSDOS
  parms = xp->val.v_pi ;
#else
  parms = (long *)xp->val.v_pi ;
#endif

#ifndef _MSDOS
  if (parms[0] < 0 || parms[0] >= Iheader.cols)
  {
    parms[0] = swapInt(parms[0]) ;
    parms[1] = swapInt(parms[1]) ;
    for (i = 0 ; i < NPOINTS ; i++)
    {
      parms[2+2*i] = swapInt(parms[2*i]) ;
      parms[2+2*i+1] = swapInt(parms[2*i+1]) ;
    }
  }
#else
  if (parms[0] < 0 || parms[0] >= (long)Iheader.cols)
  {
    parms[0] = swapLong(parms[0]) ;
    parms[1] = swapLong(parms[1]) ;
    for (i = 0 ; i < NPOINTS ; i++)
    {
      parms[2+2*i] = swapLong(parms[2*i]) ;
      parms[2+2*i+1] = swapLong(parms[2*i+1]) ;
    }
  }
#endif

  if ((int)parms[0] == INIT_VAL)  /* not yet written with real value */
    return(0) ;

  lpb->xc = (int)parms[0] ;
  lpb->yc  = (int)parms[1] ;
  for (i = 0 ; i < NPOINTS ; i++)
  {
    lpb->xp[i] = (int)parms[2+2*i] ;
    lpb->yp[i] = (int)parms[2+2*i+1] ;
  }

  if (lpb->xc < 0 || lpb->xc >= Iheader.cols ||
      lpb->yc < 0 || lpb->xc >= Iheader.rows )
    return(0) ;

  return(1) ;
}
Beispiel #10
0
int
LPAFwriteImageAnswer(LPAF *lpaf, int current)
{
  char   *fullname, tmpname[100], fname[100] ;
  IMAGE  Iheader, *I ;
  FILE   *infp, *outfp ;
  int    ecode, frame, nframes, *parms, i, current_frame, type ;
  LP_BOX *lpb ;
  struct extpar *xp ;

  fullname = lpaf->filelist[current] ;

  ImageUnpackFileName(fullname, &current_frame, &type, fname) ;
  if (type != HIPS_IMAGE)
    return(0) ;

  infp = fopen(fname, "rb") ;
  if (!infp)
    ErrorReturn(-1,(ERROR_NO_FILE,
                    "LPAFwriteImageAnswer(%d): could not open %s",
                    current, fname)) ;
  ecode = fread_header(infp, &Iheader, fname) ;
  if (ecode)
  {
    fclose(infp) ;
    ErrorReturn(-2, (ERROR_BADFILE,
                     "LPAFwriteImageAnswer(%d): could not read header", current));
  }
  fclose(infp) ;
  if (Iheader.numparam == 0)  /* must make room for header in image file */
  {
    lpafAllocParms(&Iheader) ;

    /* now copy the old image file to a new one which has room for parms */
    nframes = Iheader.num_frame ;
    strcpy(tmpname, FileTmpName(NULL)) ;
    outfp = fopen(tmpname, "wb") ;
    Iheader.num_frame = 0 ;  /* ImageAppend will bump num_frame on each call */
    ecode = fwrite_header(outfp, &Iheader, tmpname) ;
    fclose(outfp) ;   /* flush file */

    fprintf(stderr, "rewriting image file to make room for parms...\n") ;
    for (frame = 0 ; frame < nframes ; frame++)
    {
      I = ImageReadFrames(fname, frame, 1) ;
      if (!I)
        ErrorExit(ERROR_BADFILE, "LPwriteImageAnswer: could not read frame");
      ImageAppend(I, tmpname) ;
      ImageFree(&I) ;
    }
    FileRename(tmpname, fname) ;
    Iheader.num_frame = nframes ;  /* reset correct # of frames */
  }

  /* now put answer into header */
  lpb = &lpaf->coords[current] ;

  for (frame = 0, xp = Iheader.params ; xp ; xp = xp->nextp)
    if (frame++ == current_frame)
      break ;

  parms = xp->val.v_pi ;
  parms[0] = lpb->xc ;
  parms[1] = lpb->yc ;
  for (i = 0 ; i < NPOINTS ; i++)
  {
    parms[2+2*i] = lpb->xp[i] ;
    parms[2+2*i+1] = lpb->yp[i] ;
  }
  ImageUpdateHeader(&Iheader, fname) ;
  free_hdrcon(&Iheader) ;
  return(1) ;
}