|
|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T i
Length: 12970 (0x32aa)
Types: TextFile
Names: »image.c.old«
└─⟦a0efdde77⟧ Bits:30001252 EUUGD11 Tape, 1987 Spring Conference Helsinki
└─⟦this⟧ »EUUGD11/euug-87hel/sec1/image/varc++/image.c.old«
/*
This file contains the routines that interface to the maryland
vision package
*/
#include <stdio.h>
#include "utils.h"
#include "image.h"
/*
Creates an image Data Structure
*/
image_pt
create_image(number_rows,number_cols)
card number_rows; /* Number of rows */
card number_cols; /* Number of cols */
{
register card i;
/* Allocate space for the image data structure */
image_pt output = (image_pt) MALLOC(sizeof(image));
if(output == NULL)
{
perror("CreateImage:MALLOC failed");
abort();
}
/* Should set up defaults properly */
mhead(&(output->head), number_cols, number_rows);
/* Make sure defaults are correct */
output->head.cv_lcom = 0;
output->head.cv_type = 0;
output->head.cv_dims = 2;
output->head.cv_hpls = 1;
output->head.cv_bnds = 1;
output->head.cv_bp = sizeof(image_elt)*8;
output->head.cv_ebb = 0;
output->head.cv_sbb = sizeof(image_elt)*8;
output->head.cv_bb = sizeof(image_elt)*8;
output->head.cv_sbp = sizeof(image_elt)*8;
output->head.cv_ebp = 0;
/* Allocate the dope vector for the image array */
output->array = (image_elt **) MALLOC
(
sizeof(image_elt *)
* output->head.cv_rows
);
/* Allocate the storage areas for the image array */
for(i = 0; i < output->head.cv_rows; i++)
{
output->array[i] = (image_elt *) MALLOC
(
sizeof(image_elt)
* output->head.cv_cols
);
if(output->array[i] == NULL)
{
perror("CreateImage:MALLOC failed");
abort();
}
}
/* Describe there being 0 lines of comments */
output->comment.number_lines = 0;
output->comment.line_lengths = NULL;
output->comment.lines = NULL;
/* return the processed output */
return output;
}
/*
Reads in an image from a file
*/
image_pt
read_image(f)
int f; /* file descriptor to read image from */
{
register card i;
/* Allocate space for the image data structure */
image_pt output = (image_pt) MALLOC(sizeof(image));
/* Create the mv header */
ihead(f,&(output->head));
/* Read in the image comments */
read_comments(f,output);
/* Allocate the dope vector for the image array */
output->array = (image_elt **) MALLOC
(
sizeof(image_elt *)
* output->head.cv_rows
);
if(output == NULL)
{
perror("ReadImage:MALLOC failed");
abort();
}
/* Allocate the storage areas for the image array */
for(i = 0; i < output->head.cv_rows; i++)
{
output->array[i] = (image_elt *) MALLOC
(
sizeof(image_elt)
* output->head.cv_cols
);
if(output->array[i] == NULL)
{
perror("ReadImage:MALLOC failed");
abort();
}
/* Read in a row of the image into the new array */
rrow8(f,output->array[i],&(output->head));
#ifdef DEBUG
fprintf(stderr,"read row %d 1st element is %d\n",i,output->array[i][0]);
#endif DEBUG
}
/* return the processed output */
return output;
}
/*
Writes an image out to a file
*/
void
write_image(f,input)
int f; /* file to write image to */
image_pt input; /* image to write */
{
register card i;
/* Write out image header */
ohead(f,&(input->head));
/* Write comments to file */
write_comments(f,&(input->comment));
/* Write out image array */
for(i = 0; i < input->head.cv_rows; i++)
{
/* Write out a row of the image from the image array */
wrow8(f,input->array[i],&(input->head));
}
}
/*
routines to manipulate floating images
*/
/*
Creates an floating image Data Structure
*/
float_image_pt
float_create_image(number_rows,number_cols)
card number_rows; /* Number of rows */
card number_cols; /* Number of cols */
{
register card i;
/* Allocate space for the image data structure */
float_image_pt output = (float_image_pt) MALLOC(sizeof(float_image));
if(output == NULL)
{
perror("CreateImage:MALLOC failed");
abort();
}
/* Should set up defaults properly */
mhead(&(output->head), number_cols, number_rows);
/* Make sure defaults are correct */
output->head.cv_lcom = 0;
output->head.cv_type = 0;
output->head.cv_dims = 2;
output->head.cv_hpls = 1;
output->head.cv_bnds = 1;
output->head.cv_bp = sizeof(double)*8;
output->head.cv_ebb = 8;
output->head.cv_sbb = sizeof(double)*8;
output->head.cv_bb = sizeof(double)*8;
output->head.cv_sbp = sizeof(double)*8;
output->head.cv_ebp = 8;
/* Allocate the dope vector for the image array */
output->array = (double **) MALLOC
(
sizeof(double *)
* output->head.cv_rows
);
/* Allocate the storage areas for the image array */
for(i = 0; i < output->head.cv_rows; i++)
{
output->array[i] = (double *) MALLOC
(
sizeof(double)
* output->head.cv_cols
);
if(output->array[i] == NULL)
{
perror("CreateImage:MALLOC failed");
abort();
}
}
/* Describe there being 0 lines of comments */
output->comment.number_lines = 0;
output->comment.line_lengths = NULL;
output->comment.lines = NULL;
/* return the processed output */
return output;
}
/*
Reads in an floating image from a file
*/
float_image_pt
float_read_image(f)
int f; /* file descriptor to read image from */
{
register card i;
/* Allocate space for the image data structure */
float_image_pt output = (float_image_pt) MALLOC(sizeof(float_image));
/* Create the mv header */
ihead(f,&(output->head));
/* Read in the image comments */
read_comments(f,output);
/* Allocate the dope vector for the image array */
output->array = (double **) MALLOC
(
sizeof(double *)
* output->head.cv_rows
);
if(output == NULL)
{
perror("ReadImage:MALLOC failed");
abort();
}
/* Allocate the storage areas for the image array */
for(i = 0; i < output->head.cv_rows; i++)
{
output->array[i] = (double *) MALLOC
(
sizeof(double)
* output->head.cv_cols
);
if(output->array[i] == NULL)
{
perror("ReadImage:MALLOC failed");
abort();
}
#ifndef MVWORKS
/* This code should work but doesn't so ... */
/* Read in a row of the image into the new array */
rrowd(f,output->array[i],&(output->head));
#else
/* This only works for reading floating point images */
read(f,(char *) output->array[i],output->head.cv_cols * sizeof(double));
#endif MVWORKS
}
/* return the processed output */
return output;
}
/*
Writes an floating image out to a file
*/
void
float_write_image(f,input)
int f; /* file to write image to */
float_image_pt input; /* image to write */
{
register card i;
/* Write out image header */
ohead(f,&(input->head));
/* Write comments to file */
write_comments(f,&(input->comment));
/* Write out image array */
for(i = 0; i < input->head.cv_rows; i++)
{
#ifndef MVWORKS
/* This code should work but doesn't so ... */
/* Write out a row of the image from the image array */
wrowd(f,input->array[i],&(input->head));
#else
/* This only works for writing floating point images */
write(f,(char *) input->array[i],input->head.cv_cols * sizeof(double));
#endif MVWORKS
}
}
/*
Reads comments from image
*/
void
read_comments(f,input)
int f; /* file descriptor */
image_pt input; /* image */
{
char *buffer;
int read_result;
int read_total = 0;
if (input->head.cv_lcom == 0)
{
input->comment.number_lines = 0;
input->comment.line_lengths = NULL;
input->comment.lines = NULL;
return;
}
buffer = (char *) MALLOC(input->head.cv_lcom * sizeof(char));
/* If there is a problem reading the comments */
while(input->head.cv_lcom != ( read_total += (read_result = read(f,buffer+read_total,input->head.cv_lcom - read_total))))
{
if ( read_result == -1 )
{
perror("Could not read comments");
abort();
}
else if( read_result == 0 )
{
fprintf(stderr,"Could only read %d bytes of comments when there is supposed to be %d bytes!\n",read_total,input->head.cv_lcom);
abort();
}
}
/* Allocates things for comments and reads them */
input->comment.number_lines = find_number_of_lines(buffer,(card) input->head.cv_lcom);
input->comment.line_lengths = (card *) MALLOC((input->comment.number_lines*sizeof(card)));
input->comment.lines = (char **) MALLOC(input->comment.number_lines*sizeof(char *));
allocate_lines(buffer,(card) input->head.cv_lcom,input->comment.line_lengths,input->comment.lines);
}
/*
finds number of lines in comment
*/
card
find_number_of_lines(buffer,size)
char buffer[];
card size;
{
register card i;
register char *pc = buffer;
card num_lines = 1;
for(i = 0 ; i < size; i++)
{
if(*pc == '\n') num_lines++;
pc++;
}
return num_lines;
}
/*
allocates space and inserts the lines of the image into the right
places in lines
*/
void
allocate_lines(buffer,size,line_lengths,lines)
char buffer[];
card size;
card line_lengths[];
char *lines[];
{
register card i,j;
register char *pc = buffer;
register char *begin_line = buffer;
card current_line = 0;
card current_size = 0;
for(i = 0 ; i < size; i++)
{
/* If at end of line */
if(*pc == '\n')
{
/* fill in size of line */
line_lengths[current_line] = current_size;
/* allocate space for line */
lines[current_line] = MALLOC((current_size + 1)*sizeof(char));
/* read line into lines array */
for(j = 0 ; j < current_size ; j++)
{
lines[current_line][j] = *begin_line;
begin_line++;
}
/* set end marker in lines array */
lines[current_line][j] = 0;
/* set current size to 0 */
current_size = 0;
/* set begin_line to the beginning of the next line */
begin_line++;
/* set current_line to the next line */
current_line++;
}
else
{
/* current size is increased */
current_size++;
}
pc++;
}
/* Do last line in image */
/* fill in size of line */
line_lengths[current_line] = current_size;
/* allocate space for line */
lines[current_line] = MALLOC((current_size + 1)*sizeof(char));
/* read line into lines array */
for(j = 0 ; j < current_size ; j++)
{
lines[current_line][j] = *begin_line;
begin_line++;
}
/* set end marker in lines array */
lines[current_line][j] = 0;
}
/*
Writes comments to image
*/
void
write_comments(file,comment)
int file;
image_comment *comment;
{
register card i;
int write_return;
for(i = 0;i < comment->number_lines; i++)
{
write_return = write(file,comment->lines[i],(int) comment->line_lengths[i]);
if(write_return != comment->line_lengths[i])
{
if(write_return == -1)
{
perror("Could not write comments to file");
}
else
{
fprintf(stderr,"Something went wrong only could write %d bytes of comment line to file\n",write_return);
}
abort();
}
if(i == comment->number_lines - 1) break;
write_return = write(file,"\n",1);
if(write_return != 1)
{
if(write_return == -1)
{
perror("Could not write comments to file");
}
else
{
fprintf(stderr,"Something went wrong could not write \\n to file\n",write_return);
}
abort();
}
}
}
/*
Adds a line to comments;
*/
void
add_comment(line,im)
char *line;
image_pt im;
{
card length = (card) strlen(line);
char *strcpy();
/*
add in the length of the line and the carriage return
to the comment length
*/
im->head.cv_lcom += (im->head.cv_lcom == 0) ? length : length + 1;
/* increment the number of lines */
im->comment.number_lines++;
/* allocates space for new line length */
im->comment.line_lengths = (card *) realloc
(
(char *) (im->comment.line_lengths),
(unsigned) (im->comment.number_lines*sizeof(card))
);
/* store new line length */
im->comment.line_lengths[im->comment.number_lines-1] = length;
/* allocates space for ptr to new line */
im->comment.lines = (char **) realloc
(
(char *) (im->comment.lines),
(unsigned) (im->comment.number_lines*sizeof(char *))
);
/* allocates space and copies in new line */
im->comment.lines[im->comment.number_lines-1] = strcpy
(
(char *) MALLOC((length+1)*sizeof(char)),
line
);
}
/*
Appends the comments from one image to the comments of another
im1 has im2's comments appended to it
*/
void
append_comments(im1,im2)
image_pt im1,im2;
{
register card i;
/* iterate through the lines of im2 adding them 1 at a time */
for(i = 0 ; i < im2->comment.number_lines ; i++)
{
add_comment(im2->comment.lines[i],im1);
}
}
/*
Prints comments to file
*/
void
print_comments(fptr,im)
FILE *fptr;
image_pt im;
{
card numlines = im->comment.number_lines;
register card i;
for(i = 0; i < numlines; i++)
{
fprintf(fptr,"%s\n",im->comment.lines[i]);
}
}