lbm2bin
diff src/main.c @ 0:bfd8aa2ca393
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 15 Jul 2017 13:26:32 +0300 |
parents | |
children | 0b96363742d3 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main.c Sat Jul 15 13:26:32 2017 +0300 1.3 @@ -0,0 +1,156 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <errno.h> 1.8 +#include "image.h" 1.9 + 1.10 +static int proc_image(const char *fname); 1.11 +static const char *modestr(int cmode); 1.12 +static void output_planar(FILE *fp, struct image *img); 1.13 +static void output_chunky(FILE *fp, struct image *img); 1.14 + 1.15 +static int planar = 0; 1.16 + 1.17 +int main(int argc, char **argv) 1.18 +{ 1.19 + int i; 1.20 + 1.21 + for(i=1; i<argc; i++) { 1.22 + if(argv[i][0] == '-') { 1.23 + if(argv[i][2] == 0) { 1.24 + switch(argv[i][1]) { 1.25 + case 'p': 1.26 + planar = 1; 1.27 + break; 1.28 + 1.29 + case 'c': 1.30 + planar = 0; 1.31 + break; 1.32 + 1.33 + case 'h': 1.34 + printf("usage: %s [options] <file 1> <file 2> ... <file n>\n", argv[0]); 1.35 + printf("Options:\n"); 1.36 + printf(" -p: output image in planar format (default)\n"); 1.37 + printf(" -c: output image in chunky format\n"); 1.38 + printf(" -h: print usage and exit\n"); 1.39 + return 0; 1.40 + 1.41 + default: 1.42 + fprintf(stderr, "invalid option: %s\n", argv[i]); 1.43 + return 1; 1.44 + } 1.45 + } else { 1.46 + fprintf(stderr, "invalid option: %s\n", argv[i]); 1.47 + } 1.48 + } else { 1.49 + if(proc_image(argv[i]) == -1) { 1.50 + return 1; 1.51 + } 1.52 + } 1.53 + } 1.54 + return 0; 1.55 +} 1.56 + 1.57 +static int proc_image(const char *fname) 1.58 +{ 1.59 + int i; 1.60 + struct image img; 1.61 + char *outfname, *suffix; 1.62 + FILE *fp; 1.63 + 1.64 + if(load_image(&img, fname) == -1) { 1.65 + fprintf(stderr, "failed to load image: %s\n", fname); 1.66 + return -1; 1.67 + } 1.68 + 1.69 + if(!(outfname = malloc(strlen(fname) + 4))) { 1.70 + perror("failed to allocate output filename"); 1.71 + return -1; 1.72 + } 1.73 + strcpy(outfname, fname); 1.74 + if(!(suffix = strrchr(outfname, '.'))) { 1.75 + suffix = outfname + strlen(outfname); 1.76 + } 1.77 + strcpy(suffix, ".bin"); 1.78 + 1.79 + printf("processing %s -> %s\n", fname, outfname); 1.80 + 1.81 + if(!(fp = fopen(outfname, "wb"))) { 1.82 + fprintf(stderr, "failed to open output file: %s: %s\n", outfname, strerror(errno)); 1.83 + destroy_image(&img); 1.84 + free(outfname); 1.85 + return -1; 1.86 + } 1.87 + 1.88 + printf(" bits per pixel: %d\n", img.bpp); 1.89 + if(img.num_ranges) { 1.90 + printf(" color ranges:\n"); 1.91 + for(i=0; i<img.num_ranges; i++) { 1.92 + printf(" [%d]: %d - %d, rate: %u, mode: %s\n", i, img.range[i].low, img.range[i].high, 1.93 + img.range[i].rate, modestr(img.range[i].cmode)); 1.94 + } 1.95 + } 1.96 + 1.97 + if(planar) { 1.98 + output_planar(fp, &img); 1.99 + } else { 1.100 + output_chunky(fp, &img); 1.101 + } 1.102 + 1.103 + free(outfname); 1.104 + fclose(fp); 1.105 + destroy_image(&img); 1.106 + return 0; 1.107 +} 1.108 + 1.109 +static void output_planar(FILE *fp, struct image *img) 1.110 +{ 1.111 + int i, x, y; 1.112 + unsigned char acc = 0; 1.113 + int acc_bit = 7; 1.114 + unsigned char *pptr = img->pixels; 1.115 + 1.116 + for(i=0; i<img->bpp; i++) { 1.117 + for(y=0; y<img->height; y++) { 1.118 + for(x=0; x<img->width; x++) { 1.119 + acc |= (((*pptr++ >> i) & 1) << acc_bit); 1.120 + if(--acc_bit == 0) { 1.121 + fputc(acc, fp); 1.122 + acc_bit = 7; 1.123 + acc = 0; 1.124 + } 1.125 + } 1.126 + } 1.127 + } 1.128 +} 1.129 + 1.130 +static void output_chunky(FILE *fp, struct image *img) 1.131 +{ 1.132 + int i, j; 1.133 + unsigned char *pptr = img->pixels; 1.134 + 1.135 + for(i=0; i<img->height; i++) { 1.136 + for(j=0; j<img->width; j++) { 1.137 + fputc(*pptr++, fp); 1.138 + } 1.139 + } 1.140 +} 1.141 + 1.142 +static const char *modestr(int cmode) 1.143 +{ 1.144 + switch(cmode) { 1.145 + case CYCLE_NORMAL: 1.146 + return "forward"; 1.147 + case CYCLE_REVERSE: 1.148 + return "reverse"; 1.149 + case CYCLE_PINGPONG: 1.150 + return "ping-pong"; 1.151 + case CYCLE_SINE_HALF: 1.152 + return "half-sine"; 1.153 + case CYCLE_SINE: 1.154 + return "sine"; 1.155 + default: 1.156 + break; 1.157 + } 1.158 + return "unknown"; 1.159 +}