This page contains 2 programs:
  1. Unix code that passes parameters to and calls the c code
  2. c code that extracts the data


Unix script run_cd

#! /bin/csh -f
# read_cd IS A SCRIPT THAT READS A WINDOW FROM A EDC CD-ROM
# TO RUN IT YOU NEED TO:
#	1. CHANGE THE INPUT FILE NAME IN THE FIRST LINE,
#		OR CALL IT WITH THE NAME OF THE INPUT FILE
#	2. CHANGE THE OUTPUT FILE NAME IN THE SECOND LINE (OPTIONAL)
#	3. MODIFY THE UPPER-LEFT  COL/ROW LOCATIONS 
#		IN THE 5TH ROW FROM THE BOTTOM. 
#	4. MODIFY THE LOWER-RIGHT COL/ROW LOCATIONS 
#		IN THE 4TH ROW FROM THE BOTTOM. 
# LEE DE COLA
# 93/11/30

if ($#argv != 1) then
  set filename = /pdd/cdrom/
else 
  set filename = $1
endif 

set outfile = test_out.bin
set filesize = `ls -g $filename | cut -c24-32`

echo EXTRACTING FROM: $filename 
echo OUTPUT FILE IS:  $outfile

if (($filesize == 13313003) | ($filesize == 26625494)) then
  @ header  = 512
  @ padding = 21 
else
  @ header  = 0
  @ padding = 0
endif

if ($filesize > 20000000) then
  @ filetype = 16 
  echo OUTPUT FILE IS 16-BIT
else
  @ filetype = 8
endif

/home/resdgw11/ldecola/c/extract/extract << EOF > temp 
$filename
$header
4587 2889 
$padding
$filetype
$outfile  
3940 1020
4003 1083
EOF
tail -1 temp
echo DONE...

extract.c code

/*
  EXTRACT.C EXTRACTS DATA FROM AN IMAGE
  YOU MUST SUPPLY FILE NAME, HEADER LENGTH, 
  UPPER-LEFT COL/ROW AND LOWER-RIGHT COL/ROW
  LEE DE COLA
  93/10/27
*/

#include 
#include 
#define HOMEDIR "/home/resdgw11/ldecola" 
/*  THE NEXT 2 CONSTANTS ARE FOR READING 1- OR 2-BYTE FILES */
#define EIGHT   1 
#define SIXTEEN 2 


struct file_struct {
  char name[80] ;
  int header_length ;
  int cols ;
  int rows ;
  int padding_length ;
  int bit_type ;
  FILE  *pointer ;
} ;

struct file_struct infile, outfile ;

struct point_struct {
  int col ;
  int row ;
} ;

struct point_struct first ;
struct point_struct last  ;

void open_infile(struct file_struct *file) 
{
  printf("INPUT FILE NAME?\n") ;
  scanf("%s", file->name) ;
  if ( (file->pointer = fopen(file->name, "r") ) == (FILE *) NULL) {
    printf("COULDN'T OPEN INPUT FILE: %s\n", file->name) ;
    exit(0) ;
  }

  printf("INPUT FILE HEADER LENGTH? \n") ;
  scanf("%d", &file->header_length) ;

  printf("INPUT FILE COLS AND ROWS? \n") ;
  scanf("%d%d", &file->cols, &file->rows) ;

  printf("INPUT FILE PADDING LENGTH? \n") ;
  scanf("%d", &file->padding_length) ;

  file->cols = file->cols + file->padding_length ;

  printf("INPUT FILE 8 OR 16 BIT?\n") ;
  scanf("%d", &file->bit_type) ;
}

void open_outfile(struct file_struct *file)
{
  printf("OUTPUT FILE? \n") ;
  scanf("%s", file->name) ;
  printf("%s\n", file->name) ;
  if ( (file->pointer = fopen(file->name, "w") ) == (FILE *) NULL) {
    printf("COULDN'T OPEN OUT FILE: %s\n", file->name) ;
    exit(0) ;
  }
}
  
void subset() 
{
  int row, type, string_length, rows_to_read, start_position ;
  /*  ALLOCATE ENOUGH MEMORY FOR A 2-BYTE LINE */
  char *instring = (char *) calloc(infile.cols, SIXTEEN) ;

  if (infile.bit_type == 16) 
    type = SIXTEEN ;
  else 
    type = EIGHT ;
    
  printf ("START COL AND ROW? \n") ;
  scanf("%d%d", &first.col, &first.row) ;

  printf ("END COL AND ROW? \n") ;
  scanf("%d%d", &last.col, &last.row) ;

  string_length = last.col - first.col + 1;
  rows_to_read =  last.row - first.row ;
  printf("OUTPUT COLS AND ROWS: %d %d\n", string_length, rows_to_read + 1) ;

  /* POSITION TO READ POINT IN IN FILE */
  start_position = infile.header_length + 
    type * ((first.row  - 1) * infile.cols + first.col - 1) ;

  if ((fseek(infile.pointer, start_position, 0)) != 0){
    printf("ERROR POSITIONING INPUT FILE\n") ;
    exit(0) ;
  } 
  /*  READ AND WRITE THE DATA */
  for (row = 0 ; row <= rows_to_read ; row++) {
    fread (instring, type, infile.cols,    infile.pointer) ;
    fwrite(instring, type, string_length, outfile.pointer) ;
  }
}

main () {
  open_infile(&infile) ;
  open_outfile(&outfile) ;
  subset() ;
}

end of code