archive

- Random tools & helpful resources for IRC
git clone git://git.acid.vegas/archive.git
Log | Files | Refs | Archive

bwu.c (3799B)

      1 /* Copyright (c) 2018 Trollforge. All rights reserved.
      2  *
      3  * Redistribution and use in source and binary forms, with or without
      4  * modification, are permitted provided that the following conditions
      5  * are met:
      6  * 1. Redistributions of source code must retain the above copyright
      7  *    notice, this list of conditions and the following disclaimer.
      8  * 2. Redistributions in binary form must reproduce the above copyright
      9  *    notice, this list of conditions and the following disclaimer in the
     10  *    documentation and/or other materials provided with the distribution.
     11  * 3. Trollforge's name may not be used to endorse or promote products
     12  *    derived from this software without specific prior written permission.
     13  */
     14 
     15 #include <getopt.h>
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 
     19 #define STB_IMAGE_IMPLEMENTATION
     20 #include "stb_image.h"
     21 
     22 #define STB_IMAGE_RESIZE_IMPLEMENTATION
     23 #include "stb_image_resize.h"
     24 
     25 void usage(void);
     26 
     27 #define P(row, col)	(pixel[width * (row) + (col)] > 0x7f)
     28 #define D(x)		printf(x)
     29 
     30 int
     31 main(int argc, char *argv[])
     32 {
     33 	uint8_t *pixel;
     34 	int width;
     35 	int height;
     36 	int channels;
     37 
     38 	int resize_width = 0;
     39 	int resize_height;
     40 	uint8_t *resized;
     41 
     42 	char ch;
     43 
     44 	int use_color = 0;
     45 	int fg = 0;
     46 	int bg = 0;
     47 
     48 	while((ch = getopt(argc, argv, "w:f:b:")) != -1) {
     49 		switch (ch) {
     50 			case 'w':
     51 				resize_width = strtol(optarg, NULL, 10);
     52 				break;
     53 			case 'f':
     54 				use_color = 1;
     55 				fg = strtol(optarg, NULL, 10);
     56 				break;
     57 			case 'b':
     58 				use_color = 1;
     59 				bg = strtol(optarg, NULL, 10);
     60 				break;
     61 			default:
     62 				usage();
     63 				break;
     64 		}
     65 	}
     66 	argc -= optind;
     67 	argv += optind;
     68 
     69 	if (argc < 1) {
     70 		usage();
     71 	}
     72 
     73 	pixel = stbi_load(argv[0], &width, &height, &channels, 1);
     74 
     75 	if (resize_width) {
     76 		resize_height = height * resize_width / width;
     77 		resized = malloc(resize_width * resize_height);
     78 
     79 		stbir_resize_uint8(pixel, width, height, 0,
     80 				   resized, resize_width, resize_height, 0,
     81 				   1);
     82 
     83 		free(pixel);
     84 		pixel = resized;
     85 
     86 		height = resize_height;
     87 		width = resize_width;
     88 	}
     89 
     90 	for (int r = 0; r < height - 1; r += 2) {
     91 
     92 		if (use_color) {
     93 			printf("\03%d,%d", fg, bg);
     94 		}
     95 
     96 		for (int c = 0; c < width - 1; c += 2) {
     97 
     98 			( P(r    , c) &&  P(r    , c + 1) &&
     99 			  P(r + 1, c) &&  P(r + 1, c + 1)) ? D("█"):
    100 
    101 			( P(r    , c) &&  P(r    , c + 1) &&
    102 			  P(r + 1, c) && !P(r + 1, c + 1)) ? D("▛"):
    103 
    104 			( P(r    , c) && !P(r    , c + 1) &&
    105 			  P(r + 1, c) &&  P(r + 1, c + 1)) ? D("▙"):
    106 
    107 			( P(r    , c) && !P(r    , c + 1) &&
    108 			  P(r + 1, c) && !P(r + 1, c + 1)) ? D("▌"):
    109 
    110 			( P(r    , c) &&  P(r    , c + 1) &&
    111 			 !P(r + 1, c) &&  P(r + 1, c + 1)) ? D("▜"):
    112 
    113 			( P(r    , c) &&  P(r    , c + 1) &&
    114 			 !P(r + 1, c) && !P(r + 1, c + 1)) ? D("▀"):
    115 
    116 			( P(r    , c) && !P(r    , c + 1) &&
    117 			 !P(r + 1, c) &&  P(r + 1, c + 1)) ? D("▚"):
    118 
    119 			( P(r    , c) && !P(r    , c + 1) &&
    120 			 !P(r + 1, c) && !P(r + 1, c + 1)) ? D("▘"):
    121 
    122 			(!P(r    , c) &&  P(r    , c + 1) &&
    123 			  P(r + 1, c) &&  P(r + 1, c + 1)) ? D("▟"):
    124 
    125 			(!P(r    , c) &&  P(r    , c + 1) &&
    126 			  P(r + 1, c) && !P(r + 1, c + 1)) ? D("▞"):
    127 
    128 			(!P(r    , c) && !P(r    , c + 1) &&
    129 			  P(r + 1, c) &&  P(r + 1, c + 1)) ? D("▄"):
    130 
    131 			(!P(r    , c) && !P(r    , c + 1) &&
    132 			  P(r + 1, c) && !P(r + 1, c + 1)) ? D("▖"):
    133 
    134 			(!P(r    , c) &&  P(r    , c + 1) &&
    135 			 !P(r + 1, c) &&  P(r + 1, c + 1)) ? D("▐"):
    136 
    137 			(!P(r    , c) &&  P(r    , c + 1) &&
    138 			 !P(r + 1, c) && !P(r + 1, c + 1)) ? D("▝"):
    139 
    140 			(!P(r    , c) && !P(r    , c + 1) &&
    141 			 !P(r + 1, c) &&  P(r + 1, c + 1)) ? D("▗"):
    142 
    143 			D(" ");
    144 		}
    145 		printf("\n");
    146 	}
    147 	return 0;
    148 }
    149 
    150 void usage(void)
    151 {
    152 	fprintf(stderr, "usage: bwu [options] input\n");
    153 	fprintf(stderr, "-w width       set width.\n");
    154 	fprintf(stderr, "-f color       set foreground.\n");
    155 	fprintf(stderr, "-b color       set background.\n");
    156 
    157 	exit(1);
    158 }