/* * Name readpic * Description CVL picture input * Usage C function * Compilation cc -c readpic.c * Author Sergio Antoy * Date Jan 15, 1985 * * Updates Close input file, (Nov 1, 1985, Sergio Antoy) * Accept zero length comment. (Nov 1, 1985. Sergio Antoy) * */ /* Readpic reads data from a file containg a cvl picture and stores tha data into three data structures. The three pieces of information are: 1) header of the picture as defined by , 2) comment of the picture (just a string), 3) a bidimensional structure to contain the pixels of the picture. The data structures are allocated by readpic, hence it is the addresses of three pointers that must be passed to readpic. The bidimensional structure is a pointer to a pointer so that the pixel (row,col), under proper conditions, can be referred to as picture[row][col]. An example of use is the following: struct cvl_p *header; char *comment; short **picture; char *filename; int error; error = readpic(&header, &comment, (unsigned char ***)&picture, filename); ====================================================================== */ #include #include readpic(header, comment, picture, filename) struct cvl_p **header; char **comment; unsigned char ***picture; char *filename; { /* Return error codes */ #define OPERATION_COMPLETE 0 #define CANNOT_OPEN_FILE -1 #define CANNOT_ALLOC_HEADER -2 #define CANNOT_READ_HEADER -3 #define CANNOT_ALLOC_COMMENT -4 #define CANNOT_READ_COMMENT -5 #define CANNOT_ALLOC_PICTURE -6 #define CANNOT_READ_PICTURE -7 #define IS_NOT_CVL_PICTURE -8 #define INTERNAL_CONSISTENCY -9 int current_row, bytes_per_pixel; FILE *file, *fopen(); char *calloc(), **balloc(); if (*filename != '\0') { if ((file = fopen(filename, "r")) == NULL) return(CANNOT_OPEN_FILE); } else file = stdin; if ((*header = (struct cvl_p *)calloc((unsigned)1, (unsigned)sizeof(**header))) == NULL) return(CANNOT_ALLOC_HEADER); if (fread((char *)*header, sizeof(**header), 1, file) == 0) return(CANNOT_READ_HEADER); if ((*header)->cv_tag != 'TCIP' + 020000000000) return(IS_NOT_CVL_PICTURE); if ((*header)->cv_lcom > 0) { if ((*comment = calloc((unsigned)(*header)->cv_lcom, (unsigned)sizeof(**comment))) == NULL) return(CANNOT_ALLOC_COMMENT); if (fread(*comment, sizeof(**comment), (*header)->cv_lcom, file) == 0) return(CANNOT_READ_COMMENT); } else *comment = "\n"; bytes_per_pixel = (*header)->cv_bp / 8; if ((*picture = (unsigned char **)balloc((unsigned)(*header)->cv_rows, (unsigned)(*header)->cv_cols, (unsigned)bytes_per_pixel)) == NULL) return(CANNOT_ALLOC_PICTURE); for (current_row = 0; current_row < (*header)->cv_rows; current_row++) if (fread((char *)(*picture)[current_row], bytes_per_pixel, (*header)->cv_cols, file) == 0) return(CANNOT_READ_PICTURE); if (EOF == fclose(file)) return(INTERNAL_CONSISTENCY); return(OPERATION_COMPLETE); }