Support scaling images in cat-img to fit line height
This commit is contained in:
parent
8c93afb03e
commit
cf86e1732d
@ -4,6 +4,7 @@
|
||||
* Copyright (C) 2016 Kevin Lange
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <getopt.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <termios.h>
|
||||
@ -32,59 +33,107 @@ void unraw_output(void) {
|
||||
tcsetattr(fileno(stdin), TCSAFLUSH, &new);
|
||||
}
|
||||
|
||||
int usage(char * argv[]) {
|
||||
printf(
|
||||
"usage: %s [-?ns] [path]\n"
|
||||
"\n"
|
||||
" -n \033[3mdon't print a new line after image\033[0m\n"
|
||||
" -s \033[3mscale to cell height (up or down)\033[0m\n"
|
||||
" -? \033[3mshow this help text\033[0m\n"
|
||||
"\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main (int argc, char * argv[]) {
|
||||
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO)) {
|
||||
fprintf(stderr, "Can't cat-img to a non-terminal.\n");
|
||||
exit(1);
|
||||
}
|
||||
int i = 1;
|
||||
|
||||
int opt;
|
||||
int no_newline = 0;
|
||||
if (!strcmp(argv[1],"-n")) {
|
||||
i++;
|
||||
no_newline = 1;
|
||||
int scale_to_cell_height = 0;
|
||||
|
||||
while ((opt = getopt(argc, argv, "?ns")) != -1) {
|
||||
switch (opt) {
|
||||
case '?':
|
||||
return usage(argv);
|
||||
case 'n':
|
||||
no_newline = 1;
|
||||
break;
|
||||
case 's':
|
||||
scale_to_cell_height = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (optind >= argc ) {
|
||||
return usage(argv);
|
||||
}
|
||||
|
||||
int w, h;
|
||||
get_cell_sizes(&w, &h);
|
||||
sprite_t image;
|
||||
load_sprite_png(&image, argv[i]);
|
||||
|
||||
int width_in_cells = image.width / w;
|
||||
if (image.width % w) width_in_cells++;
|
||||
while (optind < argc) {
|
||||
sprite_t * image = calloc(sizeof(sprite_t),1);
|
||||
if (load_sprite_png(image, argv[optind])) {
|
||||
free(image);
|
||||
optind++;
|
||||
continue;
|
||||
}
|
||||
|
||||
int height_in_cells = image.height / h;
|
||||
if (image.height % h) height_in_cells++;
|
||||
sprite_t * source = image;
|
||||
|
||||
raw_output();
|
||||
printf("\033[?25l");
|
||||
if (scale_to_cell_height) {
|
||||
int new_width = (h * image->width) / image->height;
|
||||
source = create_sprite(new_width,h,1);
|
||||
gfx_context_t * g = init_graphics_sprite(source);
|
||||
draw_fill(g, 0x00000000);
|
||||
draw_sprite_scaled(g, image, 0, 0, new_width, h);
|
||||
sprite_free(image);
|
||||
}
|
||||
|
||||
for (int y = 0; y < height_in_cells; y++) {
|
||||
for (int x = 0; x < width_in_cells; x++) {
|
||||
printf("\033Ts");
|
||||
uint32_t * tmp = malloc(sizeof(uint32_t) * w * h);
|
||||
for (int yy = 0; yy < h; yy++) {
|
||||
for (int xx = 0; xx < w; xx++) {
|
||||
if (x*w + xx >= image.width || y*h + yy >= image.height) {
|
||||
tmp[yy * w + xx] = rgba(0,0,0,TERM_DEFAULT_OPAC);
|
||||
} else {
|
||||
uint32_t data = alpha_blend_rgba(
|
||||
rgba(0,0,0,TERM_DEFAULT_OPAC),
|
||||
premultiply(image.bitmap[(x*w+xx)+(y*h+yy)*image.width]));
|
||||
tmp[yy * w + xx] = data;
|
||||
int width_in_cells = source->width / w;
|
||||
if (source->width % w) width_in_cells++;
|
||||
|
||||
int height_in_cells = source->height / h;
|
||||
if (source->height % h) height_in_cells++;
|
||||
|
||||
raw_output();
|
||||
printf("\033[?25l");
|
||||
|
||||
for (int y = 0; y < height_in_cells; y++) {
|
||||
for (int x = 0; x < width_in_cells; x++) {
|
||||
printf("\033Ts");
|
||||
uint32_t * tmp = malloc(sizeof(uint32_t) * w * h);
|
||||
for (int yy = 0; yy < h; yy++) {
|
||||
for (int xx = 0; xx < w; xx++) {
|
||||
if (x*w + xx >= source->width || y*h + yy >= source->height) {
|
||||
tmp[yy * w + xx] = rgba(0,0,0,TERM_DEFAULT_OPAC);
|
||||
} else {
|
||||
uint32_t data = alpha_blend_rgba(
|
||||
rgba(0,0,0,TERM_DEFAULT_OPAC),
|
||||
premultiply(source->bitmap[(x*w+xx)+(y*h+yy)*source->width]));
|
||||
tmp[yy * w + xx] = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
fwrite(tmp, sizeof(uint32_t) * w * h, 1, stdout);
|
||||
free(tmp);
|
||||
fflush(stdout);
|
||||
}
|
||||
if (y != height_in_cells - 1 || !no_newline) {
|
||||
printf("\r\n");
|
||||
}
|
||||
fwrite(tmp, sizeof(uint32_t) * w * h, 1, stdout);
|
||||
free(tmp);
|
||||
fflush(stdout);
|
||||
}
|
||||
if (y != height_in_cells - 1 || !no_newline) {
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
printf("\033[?25h");
|
||||
unraw_output();
|
||||
fflush(stdout);
|
||||
sprite_free(source);
|
||||
|
||||
printf("\033[?25h");
|
||||
unraw_output();
|
||||
fflush(stdout);
|
||||
optind++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user