| 1 | /*
|
|---|
| 2 |
|
|---|
| 3 | gbmhdr.c - Display General Bitmaps header
|
|---|
| 4 |
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /*...sincludes:0:*/
|
|---|
| 8 | #include <stdio.h>
|
|---|
| 9 | #include <ctype.h>
|
|---|
| 10 | #include <string.h>
|
|---|
| 11 | #include <stddef.h>
|
|---|
| 12 | #include <stdlib.h>
|
|---|
| 13 | #include <stdarg.h>
|
|---|
| 14 | #if defined(AIX) || defined(LINUX) || defined(MAC)
|
|---|
| 15 | #include <unistd.h>
|
|---|
| 16 | #else
|
|---|
| 17 | #include <io.h>
|
|---|
| 18 | #endif
|
|---|
| 19 | #include <fcntl.h>
|
|---|
| 20 | #ifdef MAC
|
|---|
| 21 | #include <types.h>
|
|---|
| 22 | #include <stat.h>
|
|---|
| 23 | #else
|
|---|
| 24 | #include <sys/types.h>
|
|---|
| 25 | #include <sys/stat.h>
|
|---|
| 26 | #endif
|
|---|
| 27 | #ifndef O_BINARY
|
|---|
| 28 | #define O_BINARY 0
|
|---|
| 29 | #endif
|
|---|
| 30 | #include "gbm.h"
|
|---|
| 31 |
|
|---|
| 32 | /*...vgbm\46\h:0:*/
|
|---|
| 33 | /*...e*/
|
|---|
| 34 |
|
|---|
| 35 | static char progname[] = "gbmhdr";
|
|---|
| 36 |
|
|---|
| 37 | /*...susage:0:*/
|
|---|
| 38 | static void usage(void)
|
|---|
| 39 | {
|
|---|
| 40 | int ft, n_ft;
|
|---|
| 41 |
|
|---|
| 42 | fprintf(stderr, "usage: %s [-g] [-s] [--] {fn.ext{,opt}}\n", progname);
|
|---|
| 43 | fprintf(stderr, " -g don't guess bitmap format, try each type\n");
|
|---|
| 44 | fprintf(stderr, " -s be silent about errors\n");
|
|---|
| 45 | fprintf(stderr, " fn.ext{,opt} input filenames (with any format specific options)\n");
|
|---|
| 46 | gbm_init();
|
|---|
| 47 | gbm_query_n_filetypes(&n_ft);
|
|---|
| 48 | for ( ft = 0; ft < n_ft; ft++ )
|
|---|
| 49 | {
|
|---|
| 50 | GBMFT gbmft;
|
|---|
| 51 |
|
|---|
| 52 | gbm_query_filetype(ft, &gbmft);
|
|---|
| 53 | fprintf(stderr, " %s when ext in [%s]\n",
|
|---|
| 54 | gbmft.short_name, gbmft.extensions);
|
|---|
| 55 | }
|
|---|
| 56 | gbm_deinit();
|
|---|
| 57 |
|
|---|
| 58 | fprintf(stderr, " opt bitmap format specific option to pass to bitmap reader\n");
|
|---|
| 59 |
|
|---|
| 60 | exit(1);
|
|---|
| 61 | }
|
|---|
| 62 | /*...e*/
|
|---|
| 63 | /*...smain:0:*/
|
|---|
| 64 | static BOOLEAN guess = TRUE;
|
|---|
| 65 | static BOOLEAN silent = FALSE;
|
|---|
| 66 |
|
|---|
| 67 | /*...sshow_error:0:*/
|
|---|
| 68 | static void show_error(const char *fn, const char *reason)
|
|---|
| 69 | {
|
|---|
| 70 | if ( !silent )
|
|---|
| 71 | fprintf(stderr, "%s: %s - %s\n", progname, fn, reason);
|
|---|
| 72 | }
|
|---|
| 73 | /*...e*/
|
|---|
| 74 | /*...sshow:0:*/
|
|---|
| 75 | /*...sshow_guess:0:*/
|
|---|
| 76 | static void show_guess(const char *fn, const char *opt, int fd)
|
|---|
| 77 | {
|
|---|
| 78 | int ft, rc;
|
|---|
| 79 | long filelen, datalen;
|
|---|
| 80 | GBMFT gbmft;
|
|---|
| 81 | GBM gbm;
|
|---|
| 82 |
|
|---|
| 83 | if ( gbm_guess_filetype(fn, &ft) != GBM_ERR_OK )
|
|---|
| 84 | {
|
|---|
| 85 | gbm_io_close(fd);
|
|---|
| 86 | show_error(fn, "can't guess bitmap file format from extension");
|
|---|
| 87 | return;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | gbm_query_filetype(ft, &gbmft);
|
|---|
| 91 |
|
|---|
| 92 | if ( (rc = gbm_read_header(fn, fd, ft, &gbm, opt)) != GBM_ERR_OK )
|
|---|
| 93 | {
|
|---|
| 94 | char s[100+1];
|
|---|
| 95 | gbm_io_close(fd);
|
|---|
| 96 | sprintf(s, "can't read file header: %s", gbm_err(rc));
|
|---|
| 97 | show_error(fn, s);
|
|---|
| 98 | return;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | filelen = gbm_io_lseek(fd, 0L, SEEK_END);
|
|---|
| 102 | datalen = (gbm.w*gbm.h*gbm.bpp)/8;
|
|---|
| 103 | printf("%4dx%-4d %2dbpp %5ldKb %3d%% %-10s %s\n",
|
|---|
| 104 | gbm.w, gbm.h, gbm.bpp,
|
|---|
| 105 | (filelen+1023)/1024,
|
|---|
| 106 | (filelen*100)/datalen,
|
|---|
| 107 | gbmft.short_name,
|
|---|
| 108 | fn);
|
|---|
| 109 | }
|
|---|
| 110 | /*...e*/
|
|---|
| 111 | /*...sshow_noguess:0:*/
|
|---|
| 112 | static void show_noguess(const char *fn, const char *opt, int fd)
|
|---|
| 113 | {
|
|---|
| 114 | int ft, n_ft;
|
|---|
| 115 | GBMFT gbmft;
|
|---|
| 116 |
|
|---|
| 117 | printf("%5ldKb %s\n",
|
|---|
| 118 | (gbm_io_lseek(fd, 0L, SEEK_END) + 1023) / 1024,
|
|---|
| 119 | fn);
|
|---|
| 120 |
|
|---|
| 121 | if ( gbm_guess_filetype(fn, &ft) == GBM_ERR_OK )
|
|---|
| 122 | {
|
|---|
| 123 | gbm_query_filetype(ft, &gbmft);
|
|---|
| 124 | printf(" file extension suggests bitmap format may be %-10s\n",
|
|---|
| 125 | gbmft.short_name);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | gbm_query_n_filetypes(&n_ft);
|
|---|
| 129 |
|
|---|
| 130 | for ( ft = 0; ft < n_ft; ft++ )
|
|---|
| 131 | {
|
|---|
| 132 | GBM gbm;
|
|---|
| 133 | if ( gbm_read_header(fn, fd, ft, &gbm, opt) == GBM_ERR_OK )
|
|---|
| 134 | {
|
|---|
| 135 | gbm_query_filetype(ft, &gbmft);
|
|---|
| 136 | printf(" reading header suggests bitmap format may be %-10s - %4dx%-4d %2dbpp\n",
|
|---|
| 137 | gbmft.short_name, gbm.w, gbm.h, gbm.bpp);
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 | /*...e*/
|
|---|
| 142 |
|
|---|
| 143 | static void show(const char *fn, const char *opt)
|
|---|
| 144 | {
|
|---|
| 145 | int fd;
|
|---|
| 146 | struct stat buf;
|
|---|
| 147 |
|
|---|
| 148 | if ( stat(fn, &buf) != -1 && (buf.st_mode & S_IFDIR) == S_IFDIR )
|
|---|
| 149 | /* Is a directory */
|
|---|
| 150 | {
|
|---|
| 151 | show_error(fn, "is a directory");
|
|---|
| 152 | return;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | if ( (fd = gbm_io_open(fn, O_RDONLY|O_BINARY)) == -1 )
|
|---|
| 156 | {
|
|---|
| 157 | show_error(fn, "can't open");
|
|---|
| 158 | return;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | if ( guess )
|
|---|
| 162 | show_guess(fn, opt, fd);
|
|---|
| 163 | else
|
|---|
| 164 | show_noguess(fn, opt, fd);
|
|---|
| 165 |
|
|---|
| 166 | gbm_io_close(fd);
|
|---|
| 167 | }
|
|---|
| 168 | /*...e*/
|
|---|
| 169 |
|
|---|
| 170 | int main(int argc, char *argv[])
|
|---|
| 171 | {
|
|---|
| 172 | int i;
|
|---|
| 173 |
|
|---|
| 174 | /*...sprocess command line options:8:*/
|
|---|
| 175 | if ( argc == 1 )
|
|---|
| 176 | usage();
|
|---|
| 177 |
|
|---|
| 178 | for ( i = 1; i < argc; i++ )
|
|---|
| 179 | {
|
|---|
| 180 | if ( argv[i][0] != '-' )
|
|---|
| 181 | break;
|
|---|
| 182 | else if ( argv[i][1] == '-' )
|
|---|
| 183 | { ++i; break; }
|
|---|
| 184 | switch ( argv[i][1] )
|
|---|
| 185 | {
|
|---|
| 186 | case 'g': guess = FALSE; break;
|
|---|
| 187 | case 's': silent = TRUE; break;
|
|---|
| 188 | default: usage(); break;
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 | /*...e*/
|
|---|
| 192 |
|
|---|
| 193 | for ( ; i < argc; i++ )
|
|---|
| 194 | /*...shandle a filename argument:16:*/
|
|---|
| 195 | {
|
|---|
| 196 | char fn[500+1], *opt;
|
|---|
| 197 | strncpy(fn, argv[i], 500);
|
|---|
| 198 | if ( (opt = strchr(fn, ',')) != NULL )
|
|---|
| 199 | *opt++ = '\0';
|
|---|
| 200 | else
|
|---|
| 201 | opt = "";
|
|---|
| 202 | show(fn, opt);
|
|---|
| 203 | }
|
|---|
| 204 | /*...e*/
|
|---|
| 205 |
|
|---|
| 206 | return 0;
|
|---|
| 207 | }
|
|---|
| 208 | /*...e*/
|
|---|