00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 #include <stdio.h>
00089 #include <stddef.h>
00090 #include <stdlib.h>
00091 #include <string.h>
00092
00093
00094
00095
00096
00097
00098 static long get_mbi(FILE *infile) {
00099 int c;
00100 long result = 0;
00101
00102 do {
00103 c = getc(infile);
00104 if (c < 0) return -1;
00105 result = (result << 7) | (c & 0x7f);
00106 } while (c & 0x80);
00107
00108 return result;
00109 }
00110
00111
00112
00113
00114
00115 static int skip_mbi(FILE *infile) {
00116 int c;
00117
00118 do {
00119 c = getc(infile);
00120 if (c < 0) return -1;
00121 } while (c & 0x80);
00122
00123 return 0;
00124 }
00125
00126 static int show_image_from_file(char *bmpname, FILE *bmpfile,
00127 long width, long height) {
00128 long w, h;
00129
00130 for (h = 0; h < height; h++) {
00131
00132 for (w = 0; w < width; ) {
00133 int c;
00134 unsigned int bit;
00135
00136 c = getc(bmpfile);
00137 if (c < 0) {
00138 perror(bmpname);
00139 return -1;
00140 }
00141
00142 for (bit = 0x80; bit > 0 && w < width; bit >>= 1, w++) {
00143 putc((c & bit) ? '*' : ' ', stdout);
00144 }
00145 }
00146 putc('\n', stdout);
00147 }
00148
00149 return 0;
00150 }
00151
00152
00153
00154 struct parm {
00155 struct parm *next;
00156 char *name;
00157 char *value;
00158 };
00159
00160 struct parm *extparms = NULL;
00161
00162 static void clear_extparms(void) {
00163 while (extparms) {
00164 struct parm *tmp = extparms;
00165 extparms = extparms->next;
00166 free(tmp->name);
00167 free(tmp->value);
00168 free(tmp);
00169 }
00170 }
00171
00172
00173
00174 static int new_extparm(char *name, char *value) {
00175 struct parm *new;
00176 struct parm *p;
00177
00178
00179 new = (struct parm *)malloc(sizeof(struct parm));
00180 if (!new)
00181 return -1;
00182
00183 new->name = name;
00184 new->value = value;
00185 new->next = NULL;
00186
00187
00188 if (!extparms) {
00189 extparms = new;
00190 } else {
00191 p = extparms;
00192 while (p->next) {
00193 p = p->next;
00194 }
00195 p->next = new;
00196 }
00197
00198 return 0;
00199 }
00200
00201 static void print_extparms(FILE *outfile) {
00202 struct parm *p;
00203
00204 for (p = extparms; p; p = p->next) {
00205 fprintf(outfile, "%s=%s\n", p->name, p->value);
00206 }
00207 }
00208
00209 static int parse_headers(FILE *bmpfile) {
00210 int c;
00211 int exttype;
00212
00213 clear_extparms();
00214
00215 c = getc(bmpfile);
00216 if (c < 0) return -1;
00217 if (!(c & 0x80)) {
00218
00219 return 0;
00220 }
00221 exttype = (c >> 5) & 0x03;
00222
00223
00224 switch (exttype) {
00225 case 0:
00226
00227
00228
00229 if (skip_mbi(bmpfile) < 0) return -1;
00230 break;
00231 case 1: case 2:
00232
00233 return -1;
00234 case 3:
00235
00236 do {
00237 int namelen, valuelen;
00238 char *name, *value;
00239 c = getc(bmpfile);
00240 if (c < 0) return -1;
00241
00242 namelen = (c >> 4) & 0x07;
00243 name = malloc(namelen + 1);
00244 if (!name) return -1;
00245 if (fread(name, namelen, 1, bmpfile) < (size_t) namelen)
00246 return -1;
00247
00248 valuelen = c & 0x0f;
00249 value = malloc(valuelen + 1);
00250 if (!value) { free(name); return -1; }
00251 if (fread(value, valuelen, 1, bmpfile) < (size_t) valuelen)
00252 return -1;
00253
00254 new_extparm(name, value);
00255 } while (c & 0x80);
00256 break;
00257 }
00258 return 0;
00259 }
00260
00261
00262 static int show_wbmp_from_file(char *bmpname, FILE *bmpfile) {
00263 long typefield;
00264 long width, height;
00265
00266 typefield = get_mbi(bmpfile);
00267 if (typefield < 0) {
00268 perror(bmpname);
00269 return -1;
00270 }
00271
00272 if (parse_headers(bmpfile) < 0) {
00273 fprintf(stderr, "%s: format error in headers\n", bmpname);
00274 return -1;
00275 }
00276
00277 width = get_mbi(bmpfile);
00278 height = get_mbi(bmpfile);
00279 if (width < 0 || height < 0) {
00280 fprintf(stderr, "%s: error reading height and width\n",
00281 bmpname);
00282 return -1;
00283 }
00284
00285 switch (typefield) {
00286 case 0:
00287 printf("%s, %ldx%ld B/W bitmap, no compression\n",
00288 bmpname, width, height);
00289 print_extparms(stdout);
00290 if (show_image_from_file(bmpname, bmpfile,
00291 width, height) < 0) {
00292 return -1;
00293 }
00294 break;
00295 default:
00296 fprintf(stderr, "%s: cannot handle level %ld wbmp\n",
00297 bmpname, typefield);
00298 return -1;
00299 }
00300
00301 return 0;
00302 }
00303
00304 int main(int argc, char *argv[]) {
00305 int i;
00306
00307 int exitvalue = 0;
00308
00309 if (argc > 1) {
00310 for (i = 1; i < argc; i++) {
00311 FILE *bmpfile;
00312
00313 bmpfile = fopen(argv[i], "r");
00314 if (bmpfile) {
00315 if (show_wbmp_from_file(argv[i], bmpfile) < 0) {
00316
00317 exitvalue = 1;
00318 }
00319 if (fclose(bmpfile) < 0) {
00320 perror(argv[i]);
00321 exitvalue = 1;
00322 }
00323 } else {
00324 perror(argv[i]);
00325 exitvalue = 1;
00326 }
00327 if (i < argc - 1) {
00328
00329 printf("\n");
00330 }
00331 }
00332 } else {
00333
00334 if (show_wbmp_from_file("stdin", stdin)) {
00335 exitvalue = 1;
00336 }
00337 }
00338
00339 return exitvalue;
00340 }
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.