/* * Name writepic * Description CVL picture output * Usage C function * Compilation cc -c writepic.c * Author Sergio Antoy * Date Jan 15, 1985 * * Updates Does not modify original comment length. (Jul 12, 1985. S.A.) * Accept zero length comment. (Nov 1, 1985. S.A.) * Close file after writing. (Nov 1, 1985. S.A.) * */ /* Writepic writes data to a file containg a cvl picture from the data stored 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 bidimensional structure is a pointer to a pointer so that the pixels, under proper conditions, can be referred to as picture[][]. The type of the elements of the bidimensional structure does not need to be unsigned char. It is defined in the header. The comment of the picture is extended by means of argv. */ #include #include writepic(header, comment, picture, filename, argv) struct cvl_p *header; char *comment; unsigned char **picture; char *filename; char *argv[]; { /* Return error codes */ #define OPERATION_COMPLETE 0 #define CANNOT_OPEN_FILE -1 #define CANNOT_WRITE_HEADER -2 #define CANNOT_WRITE_COMMENT -3 #define CANNOT_APPEND_COMMENT -4 #define CANNOT_WRITE_PICTURE -5 #define CANNOT_CLOSE_FILE -6 int current_row, bytes_per_pixel, original_length; /* of comment */ #define MAX 80 char text[MAX]; /* of comment to append */ char *destin, *source; /* to reconstruct comment to append */ FILE *file, *fopen(); if (*filename != '\0') { if ((file = fopen(filename, "w")) == NULL) return(CANNOT_OPEN_FILE); } else file = stdout; /* Reconstruct the command line */ destin = text; *destin++ = '\t'; while (source = *argv++) { while (((destin - text) < (MAX - 1)) && (*destin++ = *source++)); *(destin - 1) = ' '; } *(destin - 1) = '\n'; original_length = header->cv_lcom; header->cv_lcom += destin - text; if (fwrite((char *)header, sizeof(*header), 1, file) == 0) return(CANNOT_WRITE_HEADER); header->cv_lcom = original_length; if (original_length > 0) if (fwrite(comment, sizeof(*comment), original_length, file) == 0) return(CANNOT_WRITE_COMMENT); if (fwrite(text, sizeof(*text), (destin - text), file) == 0) return(CANNOT_APPEND_COMMENT); bytes_per_pixel = header->cv_bp / 8; for (current_row = 0; current_row < header->cv_rows; current_row++) if (fwrite((char *)picture[current_row], bytes_per_pixel, header->cv_cols, file) == 0) return(CANNOT_WRITE_PICTURE); if (EOF == fclose(file)) return(CANNOT_CLOSE_FILE); return(OPERATION_COMPLETE); }