/*
Copyright Notice:
	Copyright 1990 Robert C. Hood
	This software is distributed "as is" without warranty.  Use this software at your OWN RISK.  Source may be distributed freely, at NO CHARGE, but must be accompanied by this documentation and copyright notice.
*/


#include 	<stdio.h>
#import 	"HipsImage.h"
#import 	"HipsView.h"

char 	*read_line(FILE *fp,int *size);

@implementation HipsImage: Object

+ getimagefromfile: (FILE *) fp
{
	int	i,j,k=0;
	int	ich;
	int	sz;
	char 	*buf;

  self = [super new];

  if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  orig_name = (char *)malloc((strlen(buf)+1)*sizeof(char));
  strcpy(orig_name,buf);

  if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  seq_name = (char *)malloc((strlen(buf)+1)*sizeof(char));
  strcpy(seq_name,buf);

  if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  num_frame = atoi(buf);

  if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  orig_date = (char *)malloc((strlen(buf)+1)*sizeof(char));
  strcpy(orig_date,buf);

  if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  rows = atoi(buf);

  if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  cols = atoi(buf);

  if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  bits_per_pixel = atoi(buf);

  if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  bit_packing = atoi(buf);

  if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  pixel_format = atoi(buf);

  seq_history = (char *)malloc(sizeof(char));
  strcpy(seq_history,"\0");
  seq_desc = (char *)malloc(sizeof(char));
  strcpy(seq_desc,"\0");

  if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  while (sz > 1 && buf[sz-1] == '|' && buf[sz] == '\\' ) {
    seq_history = (char *)realloc(seq_history,(strlen(seq_history)+sz+2)*sizeof(char));
    seq_history = (char *)strcat(seq_history,buf);
    if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  }
  if(sz > 1) {
    seq_history = (char *)realloc(seq_history,(strlen(seq_history)+sz+2)*sizeof(char));
    seq_history = (char *)strcat(seq_history,buf);
    if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  }
  while ( strcmp( buf, "." ) ) {
    seq_desc = (char *)realloc(seq_desc,(strlen(seq_desc)+sz+2)*sizeof(char));
    seq_desc = (char *)strcat(seq_desc,buf);
    if((buf = read_line(fp,&sz)) == NULL) [self errormsg: "Error reading file"];
  }
  if(strcmp(buf,".")) {
    [self errormsg: "Error reading header"];
    return(nil);
  }

  data = (char *)malloc(rows*cols*sizeof(char));

  switch(pixel_format) {
    case HIPS_PFBYTE:
      if(bits_per_pixel != 8) {
        [self errormsg: "Can't handle unusual bits per pixel"];
        return(nil);
      }
      if(bit_packing != 0) {
        [self errormsg: "Can't handle bit packing"];
        return(nil);
      }
      break;
    defualt:
      [self errormsg: "Error reading image"];
      return(nil);
      break;
  }
  for(i=0;i<rows;i++) {
    for(j=0;j<cols;j++) {
      ich = getc(fp);
      if(ich == EOF) {
        [self errormsg: "EOF reached prematurely"];
        return(nil);
      }
      data[k] = (char)ich;
      k++;
    }
  }
  fclose(fp);
  return(self);
}



- writehipsheader: (FILE *) fp
{
  fprintf(fp,"orig_name: %s\n",orig_name);
  fprintf(fp,"seq_name: %s\n",seq_name);
  fprintf(fp,"num_frame: %d\n",num_frame);
  fprintf(fp,"orig_date: %s\n",orig_date);
  fprintf(fp,"rows: %d\n",rows);
  fprintf(fp,"cols: %d\n",cols);
  fprintf(fp,"bits_per_pixel: %d\n",bits_per_pixel);
  fprintf(fp,"bit_packing: %d\n",bit_packing);
  fprintf(fp,"pixel_format: %d\n",pixel_format);
  fprintf(fp,"seq_history: %s\n",seq_history);
  fprintf(fp,"seq_desc: %s\n",seq_desc);
  return(self);
}



- (char *) getorig_name
{
  return(orig_name);
}


- (char *) getseq_name
{
  return(seq_name);
}


- (int) getnum_frame
{
  return(num_frame);
}


- (char *) getorig_date
{
  return(orig_date);
}


- (int) getrows
{
  return(rows);
}


- (int) getcols
{
  return(cols);
}


- (int) getbits_per_pixel
{
  return(bits_per_pixel);
}


- (int) getbit_packing
{
  return(bit_packing);
}


- (int) getpixel_format
{
  return(pixel_format);
}


- (char *) getseq_history
{
  return(seq_history);
}


- (char *) getseq_desc
{
  return(seq_desc);
}


- (char *) getdata
{
  return(data);
}


- errormsg: (char *)msg
{
  NXRunAlertPanel("HipsImage",msg,"OK",NULL,NULL);
}


- free
{
  free(orig_name);
  free(seq_name);
  free(orig_date);
  free(orig_name);
  free(seq_history);
  free(seq_desc);
  free(data);
  return self;
}
	
@end



char *read_line(FILE *fp,int *size)
{
	int 	i=0, c;
	char 	buf[5000];

  c = fgetc(fp);
  while( (c != '\n') && !feof(fp) && (i < 5000)) {
    buf[i++] = c;
    c = fgetc(fp);
  }
  if(!i) {
    buf[0] = ' '; buf[1] = '\0';
  }
  else
    buf[i] = '\0';
  if(feof(fp)) {
    NXRunAlertPanel("HIPS ERROR","EOF reached prematurely",NULL,NULL,NULL);
  }

  *size = strlen(buf);
  return(buf);
}
