1 | /*
|
---|
2 |
|
---|
3 | gbmsub.c - Subrectangle of 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 "gbmrect.h"
|
---|
32 |
|
---|
33 | /*...vgbm\46\h:0:*/
|
---|
34 | /*...vgbmrect\46\h:0:*/
|
---|
35 | /*...e*/
|
---|
36 |
|
---|
37 | static char progname[] = "gbmsub";
|
---|
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 [-x x] [-y y] [-w w] [-h h] [--] fn1.ext{,opt} [fn2.ext{,opt}]\n", progname);
|
---|
58 | fprintf(stderr, "flags: -x x left edge of rectangle (default 0)\n");
|
---|
59 | fprintf(stderr, " -y y bottom edge of rectangle (default 0)\n");
|
---|
60 | fprintf(stderr, " -w w width of rectangle (default width of image - x)\n");
|
---|
61 | fprintf(stderr, " -h h height of rectangle (default height of image - y)\n");
|
---|
62 | fprintf(stderr, " fn1.ext{,opt} input filename (with any format specific options)\n");
|
---|
63 | fprintf(stderr, " fn2.ext{,opt} optional output filename (or will use fn1 if not present)\n");
|
---|
64 | fprintf(stderr, " ext's are used to deduce desired bitmap file formats\n");
|
---|
65 |
|
---|
66 | gbm_init();
|
---|
67 | gbm_query_n_filetypes(&n_ft);
|
---|
68 | for ( ft = 0; ft < n_ft; ft++ )
|
---|
69 | {
|
---|
70 | GBMFT gbmft;
|
---|
71 |
|
---|
72 | gbm_query_filetype(ft, &gbmft);
|
---|
73 | fprintf(stderr, " %s when ext in [%s]\n",
|
---|
74 | gbmft.short_name, gbmft.extensions);
|
---|
75 | }
|
---|
76 | gbm_deinit();
|
---|
77 |
|
---|
78 | fprintf(stderr, " opt's bitmap format specific options\n");
|
---|
79 |
|
---|
80 | exit(1);
|
---|
81 | }
|
---|
82 | /*...e*/
|
---|
83 | /*...sget_opt_value:0:*/
|
---|
84 | static int get_opt_value(const char *s, const char *name)
|
---|
85 | {
|
---|
86 | int v;
|
---|
87 |
|
---|
88 | if ( s == NULL )
|
---|
89 | fatal("missing %s argument", name);
|
---|
90 | if ( !isdigit(s[0]) )
|
---|
91 | fatal("bad %s argument", name);
|
---|
92 | if ( s[0] == '0' && tolower(s[1]) == 'x' )
|
---|
93 | sscanf(s + 2, "%x", &v);
|
---|
94 | else
|
---|
95 | v = atoi(s);
|
---|
96 |
|
---|
97 | return v;
|
---|
98 | }
|
---|
99 | /*...e*/
|
---|
100 | /*...sget_opt_value_pos:0:*/
|
---|
101 | static int get_opt_value_pos(const char *s, const char *name)
|
---|
102 | {
|
---|
103 | int n;
|
---|
104 |
|
---|
105 | if ( (n = get_opt_value(s, name)) < 0 )
|
---|
106 | fatal("%s should not be negative", name);
|
---|
107 | return n;
|
---|
108 | }
|
---|
109 | /*...e*/
|
---|
110 | /*...smain:0:*/
|
---|
111 | int main(int argc, char *argv[])
|
---|
112 | {
|
---|
113 | int x = 0, y = 0, w = -1, h = -1;
|
---|
114 | char fn_src[500+1], fn_dst[500+1], *opt_src, *opt_dst;
|
---|
115 | int fd, ft_src, ft_dst, i, stride_src, flag, bytes;
|
---|
116 | GBM_ERR rc;
|
---|
117 | GBMFT gbmft;
|
---|
118 | GBM gbm;
|
---|
119 | GBMRGB gbmrgb[0x100];
|
---|
120 | byte *data;
|
---|
121 |
|
---|
122 | for ( i = 1; i < argc; i++ )
|
---|
123 | {
|
---|
124 | if ( argv[i][0] != '-' )
|
---|
125 | break;
|
---|
126 | else if ( argv[i][1] == '-' )
|
---|
127 | { ++i; break; }
|
---|
128 | switch ( argv[i][1] )
|
---|
129 | {
|
---|
130 | case 'x':
|
---|
131 | if ( ++i == argc ) usage();
|
---|
132 | x = get_opt_value_pos(argv[i], "x");
|
---|
133 | break;
|
---|
134 | case 'y':
|
---|
135 | if ( ++i == argc ) usage();
|
---|
136 | y = get_opt_value_pos(argv[i], "y");
|
---|
137 | break;
|
---|
138 | case 'w':
|
---|
139 | if ( ++i == argc ) usage();
|
---|
140 | w = get_opt_value_pos(argv[i], "w");
|
---|
141 | break;
|
---|
142 | case 'h':
|
---|
143 | if ( ++i == argc ) usage();
|
---|
144 | h = get_opt_value_pos(argv[i], "h");
|
---|
145 | break;
|
---|
146 | default:
|
---|
147 | usage();
|
---|
148 | break;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | if ( i == argc )
|
---|
153 | usage();
|
---|
154 | strcpy(fn_src, argv[i++]);
|
---|
155 | strcpy(fn_dst, ( i == argc ) ? fn_src : argv[i++]);
|
---|
156 | if ( i < argc )
|
---|
157 | usage();
|
---|
158 |
|
---|
159 | if ( (opt_src = strchr(fn_src, ',')) != NULL )
|
---|
160 | *opt_src++ = '\0';
|
---|
161 | else
|
---|
162 | opt_src = "";
|
---|
163 |
|
---|
164 | if ( (opt_dst = strchr(fn_dst, ',')) != NULL )
|
---|
165 | *opt_dst++ = '\0';
|
---|
166 | else
|
---|
167 | opt_dst = "";
|
---|
168 |
|
---|
169 | gbm_init();
|
---|
170 |
|
---|
171 | if ( gbm_guess_filetype(fn_src, &ft_src) != GBM_ERR_OK )
|
---|
172 | fatal("can't guess bitmap file format for %s", fn_src);
|
---|
173 |
|
---|
174 | if ( gbm_guess_filetype(fn_dst, &ft_dst) != GBM_ERR_OK )
|
---|
175 | fatal("can't guess bitmap file format for %s", fn_dst);
|
---|
176 |
|
---|
177 | if ( (fd = gbm_io_open(fn_src, O_RDONLY|O_BINARY)) == -1 )
|
---|
178 | fatal("can't open %s", fn_src);
|
---|
179 |
|
---|
180 | if ( (rc = gbm_read_header(fn_src, fd, ft_src, &gbm, opt_src)) != GBM_ERR_OK )
|
---|
181 | {
|
---|
182 | gbm_io_close(fd);
|
---|
183 | fatal("can't read header of %s: %s", fn_src, gbm_err(rc));
|
---|
184 | }
|
---|
185 |
|
---|
186 | if ( w == -1 )
|
---|
187 | w = gbm.w - x;
|
---|
188 |
|
---|
189 | if ( h == -1 )
|
---|
190 | h = gbm.h - y;
|
---|
191 |
|
---|
192 | if ( w == 0 ) { gbm_io_close(fd); fatal("w = 0"); }
|
---|
193 | if ( h == 0 ) { gbm_io_close(fd); fatal("h = 0"); }
|
---|
194 | if ( x >= gbm.w ) { gbm_io_close(fd); fatal("x >= bitmap width"); }
|
---|
195 | if ( y >= gbm.h ) { gbm_io_close(fd); fatal("y >= bitmap height"); }
|
---|
196 | if ( x + w > gbm.w ) { gbm_io_close(fd); fatal("x+w > bitmap width"); }
|
---|
197 | if ( y + h > gbm.h ) { gbm_io_close(fd); fatal("y+h > bitmap height"); }
|
---|
198 |
|
---|
199 | gbm_query_filetype(ft_dst, &gbmft);
|
---|
200 | switch ( gbm.bpp )
|
---|
201 | {
|
---|
202 | case 24: flag = GBM_FT_W24; break;
|
---|
203 | case 8: flag = GBM_FT_W8; break;
|
---|
204 | case 4: flag = GBM_FT_W4; break;
|
---|
205 | case 1: flag = GBM_FT_W1; break;
|
---|
206 | }
|
---|
207 | if ( (gbmft.flags & flag) == 0 )
|
---|
208 | {
|
---|
209 | gbm_io_close(fd);
|
---|
210 | fatal("output bitmap format %s does not support writing %d bpp data",
|
---|
211 | gbmft.short_name, gbm.bpp);
|
---|
212 | }
|
---|
213 |
|
---|
214 | if ( (rc = gbm_read_palette(fd, ft_src, &gbm, gbmrgb)) != GBM_ERR_OK )
|
---|
215 | {
|
---|
216 | gbm_io_close(fd);
|
---|
217 | fatal("can't read palette of %s: %s", fn_src, gbm_err(rc));
|
---|
218 | }
|
---|
219 |
|
---|
220 | stride_src = ( ((gbm.w * gbm.bpp + 31)/32) * 4 );
|
---|
221 | bytes = stride_src * gbm.h;
|
---|
222 | if ( (data = malloc((size_t) bytes)) == NULL )
|
---|
223 | {
|
---|
224 | gbm_io_close(fd);
|
---|
225 | fatal("out of memory allocating %d bytes for bitmap", bytes);
|
---|
226 | }
|
---|
227 |
|
---|
228 | if ( (rc = gbm_read_data(fd, ft_src, &gbm, data)) != GBM_ERR_OK )
|
---|
229 | {
|
---|
230 | gbm_io_close(fd);
|
---|
231 | fatal("can't read bitmap data of %s: %s", fn_src, gbm_err(rc));
|
---|
232 | }
|
---|
233 |
|
---|
234 | gbm_io_close(fd);
|
---|
235 |
|
---|
236 | gbm_subrectangle(&gbm, x, y, w, h, data, data);
|
---|
237 |
|
---|
238 | if ( (fd = gbm_io_create(fn_dst, O_WRONLY|O_BINARY)) == -1 )
|
---|
239 | fatal("can't create %s", fn_dst);
|
---|
240 |
|
---|
241 | gbm.w = w;
|
---|
242 | gbm.h = h;
|
---|
243 |
|
---|
244 | if ( (rc = gbm_write(fn_dst, fd, ft_dst, &gbm, gbmrgb, data, opt_dst)) != GBM_ERR_OK )
|
---|
245 | {
|
---|
246 | gbm_io_close(fd);
|
---|
247 | remove(fn_dst);
|
---|
248 | fatal("can't write %s: %s", fn_dst, gbm_err(rc));
|
---|
249 | }
|
---|
250 |
|
---|
251 | gbm_io_close(fd);
|
---|
252 |
|
---|
253 | free(data);
|
---|
254 |
|
---|
255 | gbm_deinit();
|
---|
256 |
|
---|
257 | return 0;
|
---|
258 | }
|
---|
259 | /*...e*/
|
---|