~ajpaon/image-to-ansi

7a9107be1a763e2b32c6098321696a5e0085495e — Andrew Paon 1 year, 5 months ago fff8eb7
use half-block to pack two pixels into a character
1 files changed, 18 insertions(+), 6 deletions(-)

M main.c
M main.c => main.c +18 -6
@@ 6,6 6,7 @@
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <wchar.h>

static uint8_t *MapFile(const char *filename) {
  int fd = open(filename, O_RDONLY);


@@ 119,6 120,11 @@ int main(int argc, char *argv[]) {
    return EXIT_FAILURE;
  }

  if (bmp.height % 2 != 0) {
    fputs("Only BMPs with even height are supported.\n", stderr);
    return EXIT_FAILURE;
  }

  const int bytes_per_pixel = 3;

  int bytes_per_row = bytes_per_pixel * bmp.width;


@@ 126,16 132,22 @@ int main(int argc, char *argv[]) {

  /* BMP pixel rows are stored bottom-up, left-to-right. Start on the top row --
   * the last one in the buffer -- and work backwards. */
  uint8_t *p = bmp.pixels + pixel_buffer_size - bytes_per_row - bytes_per_pixel;

  for (int y = 0; y < bmp.height; ++y) {
  uint8_t *hp = bmp.pixels + pixel_buffer_size - bytes_per_row - bytes_per_pixel;
  uint8_t *lp = hp - bytes_per_row;

  for (int y = 0; y < bmp.height / 2; ++y) {
    for (int x = 0; x < bmp.width; ++x) {
      p += bytes_per_pixel;
      printf("\033[48;2;(%u);(%u);(%u) m ", p[2], p[1], p[0]);
      printf("\033[48;2;(%u);(%u);(%u) m ", p[2], p[1], p[0]);
      hp += bytes_per_pixel;
      lp += bytes_per_pixel;
      printf("\033[48;2;%u;%u;%u m", hp[2], hp[1], hp[0]);
      printf("\033[38;2;%u;%u;%u m", lp[2], lp[1], lp[0]);
      fputs("\u2584", stdout);
    }

    p -= 2 * bytes_per_row;
    hp -= 3 * bytes_per_row;
    lp -= 3 * bytes_per_row;

    printf("\033[0m\n");
  }