stefanhaustein / TerminalImageViewer

Small C++ program to display images in a (modern) terminal using RGB ANSI codes and unicode block graphics characters
Other
1.56k stars 112 forks source link

STDIN? #19

Open MTNTheCompWiz opened 6 years ago

MTNTheCompWiz commented 6 years ago

How much of a pain would it be to take input from stdin?

stefanhaustein commented 6 years ago

Shouldn't be hard but quite busy at the moment... Happy to accept a pull request O:)

stefanhaustein commented 6 years ago

p.s. did you try /dev/stdin as file name?

HybridDog commented 6 years ago

did you try /dev/stdin as file name?

I have tried it, it does not work because somehow it does not recognize the file format:

convert-im6.q16: no decode delegate for this image format `' @ error/constitute.c/ReadImage/504.
convert-im6.q16: no images defined `pnm:-' @ error/convert.c/ConvertImageCommand/3258.
sh: 1: gm: not found

[CImg] *** CImgIOException *** [instance(0,0,0,0,(nil),non-shared)] CImg<unsigned char>::load(): Failed to recognize format of file 'stdin'.
File format is not recognized for 'stdin'

For PNG images I can do ln -s /dev/stdin stdin.png and use stdin.png instead of /dev/stdin, this works for me. If I remove the .png extension from a png file and open it with tiv, it still recognizes the file format whereas with /dev/stdin it does not.

ln -s /dev/stdin /tmp/stdin.png
cat wmiz.png | tiv /tmp/stdin.png
hadisfr commented 6 years ago

There's a not so clean approach, to save input in a temporary file, run program on it, and then, delete it. I think process image data directly needs some changes in CImg library. Creating an issue in CImg's repo for this may be good. And there should be a method for distinguishing between file address and image data.

stefanhaustein commented 6 years ago

The reason why the extension works is probably that it saves cimg from consuming part of the file to figure out the type.

Maybe we could let the makefile create stdin aliases named stdin.png, stdin.jpg etc. and then use some special parameter to access them? Seems hacky (but still better than a tmp copy) though. Or we just document this approach on the main page?

mnpenner commented 6 years ago

Why not just add --format=jpg or --format=png options? This would bypass parsing the file to figure out the type for both stdin and incorrectly named files?

stefanhaustein commented 6 years ago

The problem is that CImg (the library that is used to load the images) doesn't seem to support providing the file type by other means than the file extension.

The best fix would probably be a corresponding feature in CImg...

user202729 commented 5 years ago

Actually CImg does support specifying the type, using the load_xxx member functions (for example load_bmp)

stefanhaustein commented 5 years ago

I'll have another look when the snap and macos things are resolved (and nobody has stepped in in the meantime) O:)

boretom commented 5 years ago

I was wondering what the problem may be and had a look at it (TL;DR: not successfully):

When I get time I'll gotta put a question on CImg.h github.

ghost commented 3 years ago

did you try /dev/stdin as file name?

I have tried it, it does not work because somehow it does not recognize the file format

Perhaps it fails because stdin is not seekable? I wonder if there's a simpler alternative to CImg that plays better with streams? For the needs of tiv, CImg seems a bit overkill.

I'd really like to have this as well, but I'm not willing to dig through the 64kloc of CImg.h to figure out what changes are needed…