/* * If the file name contains size information encoded in it, * then that size is returned, even if it differs from the actual * file dimensions. (It might have been truncated). * Otherwise, the actual file size is passed to fb_common_image_size() * to see if this is a plausible image size. * * Returns - * 0 size unknown * 1 width and height returned */ int fb_common_file_size(size_t *widthp, size_t *heightp, const char *filename, int pixel_size) /* pointer to returned width */ /* pointer to returned height */ /* image file to stat */ /* bytes per pixel */ { struct stat sbuf; size_t size; register const char *cp; *widthp = *heightp = 0; /* sanity */ if (pixel_size <= 0) { return 0; } if (filename == NULL || *filename == '\0') { return 0; } /* Skip over directory names, if any */ cp = strchr(filename, '/'); if (cp) { cp++; /* skip over slash */ } else { cp = filename; /* no slash */ } if (fb_common_name_size(widthp, heightp, cp)) return 1; if (stat(filename, &sbuf) < 0) return 0; size = (size_t)(sbuf.st_size / pixel_size); return fb_common_image_size(widthp, heightp, size); }
int main(int argc, char **argv) { int ret = 0; int nsamp; if ( !get_args( argc, argv ) || bytes_per_sample <= 0 ) { (void)fputs(usage, stderr); return 1; } if ( !file_name && file_length <= 0 ) { (void)fputs(usage, stderr); return 1; } if ( file_name ) { if ( !fb_common_file_size(&width, &height, file_name, bytes_per_sample) ) { fprintf(stderr, "pixautosize: unable to autosize file '%s'\n", file_name); ret = 1; /* ERROR */ } } else { nsamp = file_length/bytes_per_sample; if ( !fb_common_image_size(&width, &height, nsamp) ) { fprintf(stderr, "pixautosize: unable to autosize nsamples=%d\n", nsamp); ret = 2; /* ERROR */ } } /* * Whether or not an error message was printed to stderr above, * print out the width and height on stdout. * They will be zero on error. */ printf("WIDTH=%lu; HEIGHT=%lu\n", width, height); return ret; }