| 1 | /*
|
|---|
| 2 |
|
|---|
| 3 | gbmref.c - Reflect General Bitmap
|
|---|
| 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 | #include "gbmmir.h"
|
|---|
| 32 |
|
|---|
| 33 | /*...vgbm\46\h:0:*/
|
|---|
| 34 | /*...vgbmmir\46\h:0:*/
|
|---|
| 35 | /*...e*/
|
|---|
| 36 |
|
|---|
| 37 | static char progname[] = "gbmref";
|
|---|
| 38 |
|
|---|
| 39 | /*...sfatal:0:*/
|
|---|
| 40 | static void fatal(const char *fmt, ...)
|
|---|
| 41 | {
|
|---|
| 42 | va_list vars;
|
|---|
| 43 | char s[256+1];
|
|---|
| 44 |
|
|---|
| 45 | va_start(vars, fmt);
|
|---|
| 46 | vsprintf(s, fmt, vars);
|
|---|
| 47 | va_end(vars);
|
|---|
| 48 | fprintf(stderr, "%s: %s\n", progname, s);
|
|---|
| 49 | exit(1);
|
|---|
| 50 | }
|
|---|
| 51 | /*...e*/
|
|---|
| 52 | /*...susage:0:*/
|
|---|
| 53 | static void usage(void)
|
|---|
| 54 | {
|
|---|
| 55 | int ft, n_ft;
|
|---|
| 56 |
|
|---|
| 57 | fprintf(stderr, "usage: %s [-h] [-v] [-t] fn1.ext{,opt} [--] [fn2.ext{,opt}]\n", progname);
|
|---|
| 58 | fprintf(stderr, "flags: -h reflect horizontally\n");
|
|---|
| 59 | fprintf(stderr, " -v reflect vertically\n");
|
|---|
| 60 | fprintf(stderr, " -t transpose x by y\n");
|
|---|
| 61 | fprintf(stderr, " fn1.ext{,opt} input filename (with any format specific options)\n");
|
|---|
| 62 | fprintf(stderr, " fn2.ext{,opt} optional output filename (or will use fn1 if not present)\n");
|
|---|
| 63 | fprintf(stderr, " ext's are used to deduce desired bitmap file formats\n");
|
|---|
| 64 |
|
|---|
| 65 | gbm_init();
|
|---|
| 66 | gbm_query_n_filetypes(&n_ft);
|
|---|
| 67 | for ( ft = 0; ft < n_ft; ft++ )
|
|---|
| 68 | {
|
|---|
| 69 | GBMFT gbmft;
|
|---|
| 70 |
|
|---|
| 71 | gbm_query_filetype(ft, &gbmft);
|
|---|
| 72 | fprintf(stderr, " %s when ext in [%s]\n",
|
|---|
| 73 | gbmft.short_name, gbmft.extensions);
|
|---|
| 74 | }
|
|---|
| 75 | gbm_deinit();
|
|---|
| 76 |
|
|---|
| 77 | fprintf(stderr, " opt's bitmap format specific options\n");
|
|---|
| 78 |
|
|---|
| 79 | exit(1);
|
|---|
| 80 | }
|
|---|
| 81 | /*...e*/
|
|---|
| 82 | /*...smain:0:*/
|
|---|
| 83 | int main(int argc, char *argv[])
|
|---|
| 84 | {
|
|---|
| 85 | BOOLEAN horz = FALSE, vert = FALSE, trans = FALSE;
|
|---|
| 86 | char fn_src[500+1], fn_dst[500+1], *opt_src, *opt_dst;
|
|---|
| 87 | int fd, ft_src, ft_dst, i, stride, bytes, flag;
|
|---|
| 88 | GBM_ERR rc;
|
|---|
| 89 | GBMFT gbmft;
|
|---|
| 90 | GBM gbm;
|
|---|
| 91 | GBMRGB gbmrgb[0x100];
|
|---|
| 92 | byte *data;
|
|---|
| 93 |
|
|---|
| 94 | for ( i = 1; i < argc; i++ )
|
|---|
| 95 | {
|
|---|
| 96 | if ( argv[i][0] != '-' )
|
|---|
| 97 | break;
|
|---|
| 98 | else if ( argv[i][1] == '-' )
|
|---|
| 99 | { ++i; break; }
|
|---|
| 100 | switch ( argv[i][1] )
|
|---|
| 101 | {
|
|---|
| 102 | case 'h': horz = TRUE; break;
|
|---|
| 103 | case 'v': vert = TRUE; break;
|
|---|
| 104 | case 't': trans = TRUE; break;
|
|---|
| 105 | default: usage(); break;
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | if ( i == argc )
|
|---|
| 110 | usage();
|
|---|
| 111 | strcpy(fn_src, argv[i++]);
|
|---|
| 112 | strcpy(fn_dst, ( i == argc ) ? fn_src : argv[i++]);
|
|---|
| 113 | if ( i < argc )
|
|---|
| 114 | usage();
|
|---|
| 115 |
|
|---|
| 116 | if ( (opt_src = strchr(fn_src, ',')) != NULL )
|
|---|
| 117 | *opt_src++ = '\0';
|
|---|
| 118 | else
|
|---|
| 119 | opt_src = "";
|
|---|
| 120 |
|
|---|
| 121 | if ( (opt_dst = strchr(fn_dst, ',')) != NULL )
|
|---|
| 122 | *opt_dst++ = '\0';
|
|---|
| 123 | else
|
|---|
| 124 | opt_dst = "";
|
|---|
| 125 |
|
|---|
| 126 | gbm_init();
|
|---|
| 127 |
|
|---|
| 128 | if ( gbm_guess_filetype(fn_src, &ft_src) != GBM_ERR_OK )
|
|---|
| 129 | fatal("can't guess bitmap file format for %s", fn_src);
|
|---|
| 130 |
|
|---|
| 131 | if ( gbm_guess_filetype(fn_dst, &ft_dst) != GBM_ERR_OK )
|
|---|
| 132 | fatal("can't guess bitmap file format for %s", fn_dst);
|
|---|
| 133 |
|
|---|
| 134 | if ( (fd = gbm_io_open(fn_src, O_RDONLY|O_BINARY)) == -1 )
|
|---|
| 135 | fatal("can't open %s", fn_src);
|
|---|
| 136 |
|
|---|
| 137 | if ( (rc = gbm_read_header(fn_src, fd, ft_src, &gbm, opt_src)) != GBM_ERR_OK )
|
|---|
| 138 | {
|
|---|
| 139 | gbm_io_close(fd);
|
|---|
| 140 | fatal("can't read header of %s: %s", fn_src, gbm_err(rc));
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | gbm_query_filetype(ft_dst, &gbmft);
|
|---|
| 144 | switch ( gbm.bpp )
|
|---|
| 145 | {
|
|---|
| 146 | case 24: flag = GBM_FT_W24; break;
|
|---|
| 147 | case 8: flag = GBM_FT_W8; break;
|
|---|
| 148 | case 4: flag = GBM_FT_W4; break;
|
|---|
| 149 | case 1: flag = GBM_FT_W1; break;
|
|---|
| 150 | }
|
|---|
| 151 | if ( (gbmft.flags & flag) == 0 )
|
|---|
| 152 | {
|
|---|
| 153 | gbm_io_close(fd);
|
|---|
| 154 | fatal("output bitmap format %s does not support writing %d bpp data",
|
|---|
| 155 | gbmft.short_name, gbm.bpp);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | if ( (rc = gbm_read_palette(fd, ft_src, &gbm, gbmrgb)) != GBM_ERR_OK )
|
|---|
| 159 | {
|
|---|
| 160 | gbm_io_close(fd);
|
|---|
| 161 | fatal("can't read palette of %s: %s", fn_src, gbm_err(rc));
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | stride = ( ((gbm.w * gbm.bpp + 31)/32) * 4 );
|
|---|
| 165 | bytes = stride * gbm.h;
|
|---|
| 166 | if ( (data = malloc((size_t) bytes)) == NULL )
|
|---|
| 167 | {
|
|---|
| 168 | gbm_io_close(fd);
|
|---|
| 169 | fatal("out of memory allocating %d bytes for bitmap", bytes);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | if ( (rc = gbm_read_data(fd, ft_src, &gbm, data)) != GBM_ERR_OK )
|
|---|
| 173 | {
|
|---|
| 174 | gbm_io_close(fd);
|
|---|
| 175 | fatal("can't read bitmap data of %s: %s", fn_src, gbm_err(rc));
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | gbm_io_close(fd);
|
|---|
| 179 |
|
|---|
| 180 | if ( vert )
|
|---|
| 181 | if ( !gbm_ref_vert(&gbm, data) )
|
|---|
| 182 | fatal("unable to reflect vertically");
|
|---|
| 183 |
|
|---|
| 184 | if ( horz )
|
|---|
| 185 | if ( !gbm_ref_horz(&gbm, data) )
|
|---|
| 186 | fatal("unable to reflect horizontally");
|
|---|
| 187 |
|
|---|
| 188 | if ( trans )
|
|---|
| 189 | {
|
|---|
| 190 | int t, stride_t = ((gbm.h * gbm.bpp + 31) / 32) * 4;
|
|---|
| 191 | byte *data_t;
|
|---|
| 192 |
|
|---|
| 193 | if ( (data_t = malloc((size_t) (gbm.w * stride_t))) == NULL )
|
|---|
| 194 | fatal("out of memory allocating %u bytes", gbm.w * stride_t);
|
|---|
| 195 |
|
|---|
| 196 | gbm_transpose(&gbm, data, data_t);
|
|---|
| 197 | t = gbm.w; gbm.w = gbm.h; gbm.h = t;
|
|---|
| 198 | free(data);
|
|---|
| 199 | data = data_t;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | if ( (fd = gbm_io_create(fn_dst, O_WRONLY|O_BINARY)) == -1 )
|
|---|
| 203 | fatal("can't create %s", fn_dst);
|
|---|
| 204 |
|
|---|
| 205 | if ( (rc = gbm_write(fn_dst, fd, ft_dst, &gbm, gbmrgb, data, opt_dst)) != GBM_ERR_OK )
|
|---|
| 206 | {
|
|---|
| 207 | gbm_io_close(fd);
|
|---|
| 208 | remove(fn_dst);
|
|---|
| 209 | fatal("can't write %s: %s", fn_dst, gbm_err(rc));
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | gbm_io_close(fd);
|
|---|
| 213 |
|
|---|
| 214 | free(data);
|
|---|
| 215 |
|
|---|
| 216 | gbm_deinit();
|
|---|
| 217 |
|
|---|
| 218 | return 0;
|
|---|
| 219 | }
|
|---|
| 220 | /*...e*/
|
|---|