├── .github └── workflows │ └── c-cpp.yml ├── .gitignore ├── .travis.yml ├── AUTHORS ├── COPYING ├── COPYING.LESSER ├── HACKING ├── Makefile.am ├── NEWS ├── README ├── README.md ├── SECURITY.md ├── TODO ├── acinclude.m4 ├── autogen.sh ├── chafa.pc.in ├── chafa ├── .gitignore ├── Makefile.am ├── chafa-canvas-config.c ├── chafa-canvas-config.h ├── chafa-canvas.c ├── chafa-canvas.h ├── chafa-common.h ├── chafa-features.c ├── chafa-features.h ├── chafa-frame.c ├── chafa-frame.h ├── chafa-image.c ├── chafa-image.h ├── chafa-placement.c ├── chafa-placement.h ├── chafa-symbol-map.c ├── chafa-symbol-map.h ├── chafa-term-db.c ├── chafa-term-db.h ├── chafa-term-info.c ├── chafa-term-info.h ├── chafa-term-seq-def.h ├── chafa-term-seq-doc-in.h ├── chafa-util.c ├── chafa-util.h ├── chafa-version-macros.h ├── chafa.h ├── chafaconfig.h.in └── internal │ ├── Makefile.am │ ├── chafa-avx2.c │ ├── chafa-base64.c │ ├── chafa-base64.h │ ├── chafa-batch.c │ ├── chafa-batch.h │ ├── chafa-bitfield.h │ ├── chafa-canvas-internal.h │ ├── chafa-canvas-printer.c │ ├── chafa-canvas-printer.h │ ├── chafa-color-hash.c │ ├── chafa-color-hash.h │ ├── chafa-color-table.c │ ├── chafa-color-table.h │ ├── chafa-color.c │ ├── chafa-color.h │ ├── chafa-dither.c │ ├── chafa-dither.h │ ├── chafa-indexed-image.c │ ├── chafa-indexed-image.h │ ├── chafa-iterm2-canvas.c │ ├── chafa-iterm2-canvas.h │ ├── chafa-kitty-canvas.c │ ├── chafa-kitty-canvas.h │ ├── chafa-math-util.c │ ├── chafa-math-util.h │ ├── chafa-mmx.c │ ├── chafa-noise.c │ ├── chafa-noise.h │ ├── chafa-palette.c │ ├── chafa-palette.h │ ├── chafa-passthrough-encoder.c │ ├── chafa-passthrough-encoder.h │ ├── chafa-pca.c │ ├── chafa-pca.h │ ├── chafa-pixops.c │ ├── chafa-pixops.h │ ├── chafa-popcnt.c │ ├── chafa-private.h │ ├── chafa-sixel-canvas.c │ ├── chafa-sixel-canvas.h │ ├── chafa-sse41.c │ ├── chafa-string-util.c │ ├── chafa-string-util.h │ ├── chafa-symbols-ascii-ibm.h │ ├── chafa-symbols-ascii.h │ ├── chafa-symbols-block.h │ ├── chafa-symbols-kana.h │ ├── chafa-symbols-latin.h │ ├── chafa-symbols-misc-narrow.h │ ├── chafa-symbols.c │ ├── chafa-vector.h │ ├── chafa-work-cell.c │ ├── chafa-work-cell.h │ └── smolscale │ ├── COPYING │ ├── COPYING.BSD-4 │ ├── COPYING.LGPLv3 │ ├── Makefile.am │ ├── README │ ├── smolscale-avx2.c │ ├── smolscale-generic.c │ ├── smolscale-private.h │ ├── smolscale.c │ └── smolscale.h ├── configure.ac ├── docs ├── .gitignore ├── Makefile.am ├── building.xml ├── chafa-docs.xml ├── chafa-logo.gif ├── chafa-sections.txt ├── chafa.xml ├── manpage.css ├── style.css ├── using.xml └── version.xml.in ├── examples ├── Makefile.am ├── adaptive.c ├── example.c └── ncurses.c ├── libnsgif ├── COPYING ├── Makefile.am ├── README ├── README-chafa ├── libnsgif.c ├── libnsgif.h ├── log.h ├── lzw.c └── lzw.h ├── lodepng ├── LICENSE ├── Makefile.am ├── README.md ├── lodepng.c └── lodepng.h ├── tests ├── .gitignore ├── Makefile.am ├── canvas-test.c ├── chafa-tool-bad-test.sh ├── chafa-tool-cmode-test.sh ├── chafa-tool-format-test.sh ├── chafa-tool-gallery.sh ├── chafa-tool-grid-test.sh ├── chafa-tool-loader-test.sh ├── chafa-tool-options-test.sh ├── chafa-tool-pipe-test.sh ├── chafa-tool-test-common.sh ├── data │ ├── Makefile.am │ ├── bad │ │ ├── Makefile.am │ │ ├── libavif-inf-frame-duration.avif │ │ ├── libnsgif-lzw-oob.gif │ │ ├── libnsgif-uninitialized-frame-fields.gif │ │ ├── lodepng-adam7-mystery-over-read.png │ │ ├── lodepng-zero-length-literal.png │ │ ├── lodepng-zlib-big-alloc.png │ │ ├── no-frame-data.gif │ │ ├── pixops-normalize-signed-overflow.gif │ │ ├── smolscale-unaligned-1.xwd │ │ ├── smolscale-unaligned-2.xwd │ │ ├── smolscale-unaligned-3.xwd │ │ └── smolscale-unaligned-4.xwd │ └── good │ │ ├── Makefile.am │ │ ├── anim-local-cmaps.gif │ │ ├── anim.gif │ │ ├── card-32c-alpha.png │ │ ├── card-32c-noalpha.png │ │ ├── card-full-alpha.png │ │ ├── card-full-noalpha.png │ │ ├── noise-32x32-indexed.png │ │ ├── noise-32x32.gif │ │ ├── noise-32x32.png │ │ ├── noise-32x32.xwd │ │ ├── pixel-cmyk.jpeg │ │ ├── pixel.avif │ │ ├── pixel.gif │ │ ├── pixel.jpeg │ │ ├── pixel.jxl │ │ ├── pixel.png │ │ ├── pixel.qoi │ │ ├── pixel.svg │ │ ├── pixel.tiff │ │ ├── pixel.webp │ │ ├── pixel.xwd │ │ └── taxic.jpg ├── postinstall.sh ├── sixel-compare.sh ├── term-info-test.c └── test-driver.sh └── tools ├── Makefile.am ├── chafa ├── .gitignore ├── LICENSE.qoi ├── Makefile.am ├── avif-loader.c ├── avif-loader.h ├── c64-platform.c ├── chafa-byte-fifo.c ├── chafa-byte-fifo.h ├── chafa-parser.c ├── chafa-parser.h ├── chafa-term.c ├── chafa-term.h ├── chafa-wakeup.c ├── chafa-wakeup.h ├── chafa.c ├── conhost.c ├── conhost.h ├── file-mapping.c ├── file-mapping.h ├── font-loader.c ├── font-loader.h ├── gif-loader.c ├── gif-loader.h ├── grid-layout.c ├── grid-layout.h ├── ibm-platform.c ├── jpeg-loader.c ├── jpeg-loader.h ├── jxl-loader.c ├── jxl-loader.h ├── manifest.rc ├── media-loader.c ├── media-loader.h ├── ms-utf8.xml ├── named-colors.c ├── named-colors.h ├── placement-counter.c ├── placement-counter.h ├── png-loader.c ├── png-loader.h ├── qoi-loader.c ├── qoi-loader.h ├── qoi.h ├── svg-loader.c ├── svg-loader.h ├── tiff-loader.c ├── tiff-loader.h ├── util.c ├── util.h ├── webp-loader.c ├── webp-loader.h ├── xwd-loader.c └── xwd-loader.h ├── completions ├── Makefile.am ├── fish-completion.fish └── zsh-completion.zsh └── fontgen ├── Makefile.am ├── README.md ├── chafa8x8.py └── compare.py /.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ "master" ] 6 | pull_request: 7 | branches: [ "master" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: Running prerequisites 17 | run: > 18 | sudo apt update -y 19 | 20 | sudo apt install -y 21 | automake 22 | libtool 23 | libglib2.0-dev 24 | libavif-dev 25 | libjpeg-dev 26 | librsvg2-dev 27 | libtiff-dev 28 | libwebp-dev 29 | gtk-doc-tools 30 | docbook-xml 31 | libxml2-utils 32 | 33 | export LD_LIBRARY_PATH=$(if [[ $CC == "clang" ]]; then echo -n '/usr/local/clang/lib'; fi) 34 | 35 | mkdir build 36 | 37 | - name: Building 38 | working-directory: ./build 39 | run: > 40 | CFLAGS=' 41 | -g 42 | -O2 43 | -fsanitize=address,undefined 44 | -fsanitize-undefined-trap-on-error 45 | -fstack-protector-all 46 | -Werror -Wno-error=unused 47 | -Wno-error=unused-function 48 | -Wno-error=unused-macros 49 | -Wno-error=unused-parameter 50 | -Wno-error=unused-variable 51 | -Wno-error=unused-const-variable 52 | -Wno-error=unused-value 53 | -Wno-error=comment 54 | -Wno-error=missing-braces' 55 | ../autogen.sh --prefix=/usr --enable-gtk-doc --enable-man 56 | 57 | make -j4 58 | 59 | - name: Running tests 60 | working-directory: ./build 61 | run: make check || { RET=$$?; find . -name "test-suite.log" -exec cat {} + ; exit $RET; } 62 | 63 | - name: Installing chafa 64 | working-directory: ./build 65 | run: > 66 | sudo make install 67 | 68 | chafa --version 69 | 70 | - name: Run post install 71 | working-directory: ./tests 72 | run: ./postinstall.sh 73 | 74 | - name: After failure 75 | if: failure() 76 | run: > 77 | touch tests/test-suite.log 78 | 79 | cat tests/test-suite.log 80 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | \#*\# 3 | *.la 4 | *.lo 5 | *.o 6 | *.stamp 7 | *-stamp 8 | .deps 9 | .libs 10 | Makefile 11 | Makefile.in 12 | 13 | /aclocal.m4 14 | /ar-lib 15 | /autom4te.cache 16 | /chafa.pc 17 | /compile 18 | /config.guess 19 | /config.h 20 | /config.h.in 21 | /config.log 22 | /config.status 23 | /config.sub 24 | /configure 25 | /depcomp 26 | /gtk-doc.m4 27 | /gtk-doc.make 28 | /install-sh 29 | /libtool 30 | /ltmain.sh 31 | /m4 32 | /missing 33 | /stamp-h1 34 | /test-driver 35 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | 3 | dist: bionic 4 | 5 | compiler: 6 | - clang 7 | - gcc 8 | 9 | before_install: 10 | - sudo apt-get install -qq -y automake libtool libglib2.0-dev libmagickwand-dev libjpeg-dev librsvg2-dev libtiff-dev libwebp-dev gtk-doc-tools docbook-xml libxml2-utils 11 | # Needed for ImageMagick/clang runtime not finding libomp.so 12 | - export LD_LIBRARY_PATH=$(if [[ $CC == "clang" ]]; then echo -n '/usr/local/clang/lib'; fi) 13 | 14 | script: 15 | - mkdir build && cd build && CFLAGS='-g -O2 -fsanitize=address,undefined -fsanitize-undefined-trap-on-error -Werror -Wno-error=unused -Wno-error=unused-function -Wno-error=unused-parameter -Wno-error=unused-variable -Wno-error=unused-const-variable -Wno-error=unused-value -Wno-error=comment -Wno-error=missing-braces' ../autogen.sh --prefix=/usr --enable-gtk-doc --enable-man --without-imagemagick && make -j4 && make check && rm -Rf ../build/* && ../autogen.sh --prefix=/usr --enable-gtk-doc --enable-man && make -j4 && sudo make install && chafa --version && cd ../tests && ./postinstall.sh 16 | 17 | after_failure: 18 | - touch tests/test-suite.log && cat tests/test-suite.log 19 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Chafa is principally written and maintained by Hans Petter Jansson 2 | . Feel free to contact him with any questions and 3 | comments. 4 | 5 | There is a growing list of contributors; these can be found in the 6 | git log. For a complete list, you could try: 7 | 8 | git log --pretty="format:%an <%ae>" | sort -f | uniq 9 | 10 | Per 2025-05-18, this yields the following (sans duplicates): 11 | 12 | alkahest 13 | AnonymouX47 14 | begasus 15 | Biswapriyo Nath 16 | Daniel Eklöf 17 | data-man 18 | Delgan 19 | Eli Schwartz 20 | Emanuel Haupt 21 | Erica 22 | Felix Yan 23 | Hans Petter Jansson 24 | Hoang Nguyen 25 | Johan Mattsson <39247600+mjunix@users.noreply.github.com> 26 | korei999 27 | Launch Lee <80872691+LaunchLee@users.noreply.github.com> 28 | Michael Vetter 29 | Mikel Olasagasti Uranga 30 | Mo Zhou 31 | oshaboy <35503208+oshaboy@users.noreply.github.com> 32 | oupson 33 | Ricardo Arguello 34 | Robert-André Mauchin 35 | Roman Wagner 36 | Samuel Thibault 37 | Sebastian Stark 38 | Simon Law 39 | sitiom 40 | Sotiris Papatheodorou 41 | Sudhakar Verma 42 | Tim Gates 43 | Wu Zhenyu 44 | Øyvind Kolås 45 | -------------------------------------------------------------------------------- /HACKING: -------------------------------------------------------------------------------- 1 | How to hack on Chafa 2 | ==================== 3 | 4 | Code formatting and structure 5 | ----------------------------- 6 | 7 | The code is mostly C99 with limited use of extensions. It should compile with 8 | most standards-compliant C compilers released in the last couple of years. 9 | 10 | GLib is our primary support library, and the code is in general very GLib-y. 11 | We use only the base library, no GObject or GIO. 12 | 13 | Formatting is done with spaces (no tabs) and four-space indenting stops. 14 | 15 | The directory layout is as follows: 16 | 17 | * Top level ............ Build scripts, README, etc. 18 | |- chafa ............. The Chafa library. All exported APIs are here. 19 | | `- internal ....... Chafa library internals. Internal APIs. 20 | | `- smolscale ... Private copy of a pixmap scaling library. 21 | |- docs .............. Built documentation (API and man pages). 22 | |- libnsgif .......... Private copy of a GIF library, used by tools. 23 | |- lodepng ........... Private copy of a PNG library, used by tools. 24 | |- tests ............. Tests for library and tools. 25 | `- tools ............. Command-line tools. 26 | |- chafa .......... The Chafa command-line graphics viewer. 27 | `- fontgen ........ Experimental font generator. 28 | 29 | Making source releases 30 | ---------------------- 31 | 32 | Releases are made as compressed, signed tar archives ("tarballs"). We use 33 | semantic versioning. 34 | 35 | The following can be done multiple times and at any time during development, 36 | always on the master branch: 37 | 38 | 1) Write/edit NEWS section with a (TBA) placeholder for release date. 39 | 40 | Then right before the release, still on the master branch: 41 | 42 | 2) Update the soversion in chafa/Makefile.am (-version-info c:r:a): 43 | - If the library source code has changed at all since the last update, 44 | then increment revision (‘c:r:a’ becomes ‘c:r+1:a’). 45 | - If any interfaces have been added, removed, or changed since the last 46 | update, increment current, and set revision to 0. 47 | - If any interfaces have been added since the last public release, then 48 | increment age. 49 | - If any interfaces have been removed or changed since the last public 50 | release, then set age to 0. 51 | 52 | 3) If this is a minor (x.y.0) release, bump package to the next even version 53 | in configure.ac. 54 | 55 | 4) Make sure 'make distcheck' passes. Correct any issues. 56 | 57 | 5) Commit and push above changes. Wait for green CI and correct any issues. 58 | 59 | 6) Edit NEWS and replace (TBA) with today's date. 60 | 61 | 7) If this is a minor (x.y.0) release, edit README.md and update the CI links. 62 | They should reference master and the latest stable branch. 63 | 64 | 8) If this is a micro (x.y.z) release, switch to that release's maintenance 65 | branch (x.y) and cherry-pick all changes from the previous steps into it, 66 | then increment the micro version in configure.ac. 67 | 68 | 9) Commit above changes. 69 | 70 | 10) Tag and sign the release: 'git tag -s x.y.z'. Annotate with the appropriate 71 | NEWS item, without the --- underline for the heading. 72 | 73 | 11) If this is a minor (x.y.0) release, make a maintenance branch for it, 74 | rooted at the tag: 'git branch x.y'. But keep working on master. 75 | 76 | 12) Build tarball: 'make distcheck'. 77 | 78 | 13) Sign tarball: 'gpg --sign --detach --armor chafa-x.y.z.tar.xz'. 79 | 80 | 14) If this was a minor (x.y.0) release, bump package to the next odd version. 81 | 82 | 15) Commit the post-release version bump. 83 | 84 | 16) Push changes. Make sure to push tags and branches too. 85 | 86 | 17) Upload the tarball and signature to GitHub, and copy the NEWS item there. 87 | Add markdown formatting. 88 | 89 | That should do it. 90 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = chafa docs examples libnsgif lodepng tools tests 2 | 3 | EXTRA_DIST = \ 4 | HACKING \ 5 | README.md \ 6 | SECURITY.md \ 7 | autogen.sh \ 8 | chafa.pc.in 9 | 10 | pkgconfigdir = $(libdir)/pkgconfig 11 | pkgconfig_DATA = chafa.pc 12 | 13 | all-local: 14 | @echo 15 | @echo --- 16 | @echo --- Success! You can now run tools/chafa/chafa, or install everything 17 | @echo --- using "make install" or "sudo make install". 18 | @echo --- 19 | @echo -e '--- \e[0;91mNOTE:\e[0m You may have to run \e[0;93msudo ldconfig\e[0m after installing.' 20 | @echo --- 21 | @echo 22 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Chafa 2 | ===== 3 | 4 | Chafa is a command-line utility that converts image data, including 5 | animated GIFs, into graphics formats or ANSI/Unicode character art suitable 6 | for display in a terminal. It has broad feature support, allowing it to be 7 | used on devices ranging from historical teleprinters to modern terminal 8 | emulators and everything in between. 9 | 10 | The core functionality is provided by a C library with a public, 11 | well-documented API. 12 | 13 | Both library and frontend tools are covered by the Lesser GPL license, 14 | version 3 or later (LGPLv3+). 15 | 16 | For the most up-to-date information, please see https://hpjansson.org/chafa/ 17 | 18 | Installing with package manager 19 | ------------------------------- 20 | 21 | Chafa is available as packages for many software distributions. A few are 22 | listed below, along with their command-line installation instructions: 23 | 24 | Arch Linux .... pacman -S chafa 25 | Brew .......... brew install chafa 26 | Debian ........ apt install chafa 27 | Fedora ........ dnf install chafa 28 | FreeBSD ....... pkg install chafa 29 | Gentoo ........ emerge media-gfx/chafa 30 | Guix .......... guix install chafa 31 | Kali Linux .... apt install chafa 32 | MacPorts ...... port install chafa 33 | OpenBSD ....... pkg_add chafa 34 | openSUSE ...... zypper in chafa 35 | Ubuntu ........ apt install chafa 36 | 37 | On Windows, Chafa can be installed via Scoop and Winget: 38 | 39 | Scoop ....... scoop install chafa 40 | Winget ....... winget install hpjansson.Chafa 41 | 42 | See https://hpjansson.org/chafa/download/ for more. 43 | 44 | Installing from tarball 45 | ----------------------- 46 | 47 | You will need GCC, make and the GLib development package installed to 48 | compile Chafa from a release tarball. If you want to build the 49 | command-line tool `chafa` and not just the library, you will 50 | additionally need the ImageMagick development packages. 51 | 52 | Prebuilt documentation is included in the release tarball, and you do not 53 | need gtk-doc unless you want to make changes/rebuild it. 54 | 55 | After unpacking, cd to the toplevel directory and issue the following 56 | shell commands: 57 | 58 | $ ./configure 59 | $ make 60 | $ sudo make install 61 | 62 | Installing from git repository 63 | ------------------------------ 64 | 65 | You will need GCC, make, Autoconf, Automake, Libtool and the GLib 66 | development package installed to compile Chafa from its git repository. If 67 | you want to build the command-line tool `chafa` and not just the library, 68 | you will additionally need development packages for: 69 | 70 | * FreeType2 71 | * libjpeg (optional) 72 | * librsvg (optional) 73 | * libtiff (optional) 74 | * libwebp (optional) 75 | * ImageMagick (optional, deprecated) 76 | 77 | If you want to build documentation, you will also need gtk-doc. 78 | 79 | Start by cloning the repository: 80 | 81 | $ git clone https://github.com/hpjansson/chafa.git 82 | 83 | Then cd to the toplevel directory and issue the following shell commands: 84 | 85 | $ ./autogen.sh 86 | $ make 87 | $ sudo make install 88 | 89 | Python bindings 90 | --------------- 91 | 92 | Erica Ferrua Edwardsdóttir maintains excellent Python bindings for Chafa. If 93 | Python's your thing, check them out. They are easy to use and come with a 94 | detailed tutorial: 95 | 96 | https://chafapy.mage.black/ 97 | 98 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 5 | 6 |

7 |

Master Build StatusLatest ReleaseFriendly Chat

8 |

AboutGalleryPackagesDevelopment

9 | 10 | ## About 11 | 12 | Chafa is a command-line utility that converts image data, including 13 | animated GIFs, into graphics formats or ANSI/Unicode character art suitable 14 | for display in a terminal. It has broad feature support, allowing it to be 15 | used on devices ranging from historical teleprinters to modern terminal 16 | emulators and everything in between. 17 | 18 | The core functionality is provided by a C library with a public, 19 | well-documented API. 20 | 21 | Both library and frontend tools are covered by the Lesser GPL license, 22 | version 3 or later (LGPLv3+). 23 | 24 | The [official web pages](https://hpjansson.org/chafa/) and [C API 25 | documentation](https://hpjansson.org/chafa/ref/) can be found online. Check 26 | out the [gallery](https://hpjansson.org/chafa/gallery/) for screenshots. 27 | 28 | ## Installing 29 | 30 | Chafa is most likely packaged for your distribution, so if you're not 31 | going to hack on it, you're better off using 32 | [official packages](https://hpjansson.org/chafa/download/). If you want to 33 | build the latest and greatest yourself, read on. 34 | 35 | You will need GCC, make, Autoconf, Automake, Libtool and the GLib 36 | development package installed to compile Chafa from its git repository. If 37 | you want to build the command-line tool `chafa` and not just the library, 38 | you will additionally need development packages for: 39 | 40 | * FreeType2. Often packaged as `libfreetype6-dev` or `freetype2-devel`. 41 | * libjpeg (optional). Look for `libjpeg-dev`, `libjpeg62-devel` or `libjpeg8-devel`. 42 | * librsvg (optional). Look for `librsvg2-dev` or `librsvg-devel`. 43 | * libtiff (optional). Look for `libtiff5-dev` or `libtiff-devel`. 44 | * libwebp (optional). Look for `libwebp-dev` or `libwebp-devel`. 45 | 46 | If you want to build documentation, you will also need gtk-doc. 47 | 48 | Start by cloning the repository: 49 | 50 | ```sh 51 | $ git clone https://github.com/hpjansson/chafa.git 52 | ``` 53 | 54 | Then cd to the toplevel directory and issue the following shell commands: 55 | 56 | ```sh 57 | $ ./autogen.sh 58 | $ make 59 | $ sudo make install 60 | ``` 61 | 62 | That should do it! 63 | 64 | ## Python bindings 65 | 66 | [Erica Ferrua Edwardsdóttir](https://mage.black/) maintains 67 | [excellent Python bindings](https://chafapy.mage.black/) for Chafa. If 68 | Python's your thing, check them out. They are easy to use and come with a 69 | [detailed tutorial](https://chafapy.mage.black/usage/tutorial.html). 70 | 71 | ## JavaScript bindings 72 | 73 | [Héctor Molinero Fernández](https://hector.molinero.dev/) maintains 74 | [wonderful JavaScript bindings](https://github.com/hectorm/chafa-wasm) 75 | built around his WebAssembly port. These are available from NPM and can 76 | be used in Node.js, web browsers, and more. 77 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policies and Procedures 2 | 3 | This document outlines security procedures and general policies for Chafa. 4 | 5 | ## Reporting a Bug 6 | 7 | We are grateful for the testing and analysis carried out by the community. All 8 | bug reports are taken seriously. 9 | 10 | Normally, bugs can be filed directly in the public GitHub issue tracker, but if 11 | you believe there is a security impact, please contact the lead maintainer at 12 | his e-mail address instead. 13 | 14 | We will most likely respond within 48 hours, but since Chafa is a volunteer 15 | project, please allow up to a week for those rare times we're away from the 16 | keyboard or general connectivity. 17 | 18 | When a fix is published, you will receive credit under your real name or bug 19 | tracker handle in the NEWS document and possibly elsewhere (GitHub, blog post, 20 | etc). If you prefer to remain anonymous or pseudonymous, you should mention 21 | this in your e-mail. 22 | 23 | ## Disclosure Policy 24 | 25 | The maintainer will coordinate the fix and release process, involving the 26 | following steps: 27 | 28 | * Confirm the problem and determine the affected versions. 29 | * Audit code to find any potential similar problems. 30 | * Prepare fixes for all releases still under maintenance. These fixes will be 31 | released as fast as possible. 32 | 33 | You may be asked to provide further information in pursuit of a fix. 34 | 35 | ## Comments on this Policy 36 | 37 | If you have suggestions on how this process could be improved, please submit an 38 | issue or pull request. 39 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | TODO 2 | ==== 3 | 4 | If you're particularly interested in any of this, send patches/pull requests 5 | or maybe just prod me a little. 6 | 7 | Minor Features/UX 8 | ----------------- 9 | 10 | - Accept -o, --output to write to file. 11 | - Verbose output. Show file names. Call it --annotate. 12 | - Add a --test option to print a test page. 13 | - Add a --show-symbols op to print matching symbols. 14 | - If FG color is transparent, see if we can use an inverted symbol and swap 15 | with BG. 16 | - Avoid using transparent foreground due to XFCE Terminal (other terminals?) 17 | weird handling with background picture set? 18 | - Except in FGBG modes. 19 | - Test with more terminals. 20 | - PuTTY on Windows 8, 10? 21 | - Windows 7 fonts support half, solid, some borders. 22 | - Terminology. 23 | - Emulate tycat. 24 | - Emacs ansi-term. 25 | - Emacs shell (TERM=dumb). 26 | - Come up with some kind of support matrix. 27 | - More symbols/symbol aliases: 28 | - CP437. 29 | - Output to retro art formats: 30 | - RexPaint. 31 | - lvllvl.com. 32 | - CharPad/CharPad Pro. 33 | - Marq's PETSCII Editor (http://www.kameli.net/marq/?page_id=2717). 34 | - PabloDraw. 35 | - ANSILove (direct to .ANS, other formats?). 36 | - Others? 37 | - More image loaders: 38 | - BMP. The GIMP has a fairly complete decoder. 39 | - XPM. https://en.wikipedia.org/wiki/X_PixMap 40 | - PBM, PGM, PPM. https://en.wikipedia.org/wiki/Netpbm 41 | - Run image decoders (and Chafa backend?) in sandboxed subprocess. 42 | - "Auto" modes for ChafaCanvasMode and ChafaPixelMode. 43 | 44 | Major features 45 | -------------- 46 | 47 | - Selection from multiple internal named font bitmap sets (IBM, C64, etc). 48 | - Custom palettes. 49 | - External (e.g. in GIMP format, or extract from image) or named internal. 50 | - Using different palettes for BG and FG allows for retro modes like the 51 | C64's Extended Color Character Mode (ECM). 52 | - Pixmap export (raw image buffer from backend, PNG or TIFF export in frontend). 53 | - Lossy/lossless intra-frame compression. Data rate regulated: 54 | - By desired output size. 55 | - By maximum desired per-cell error. 56 | - By total error? 57 | - Slide window over row, calculate mean colors, calc error? 58 | - Lossy/lossless delta compression. 59 | - Double-buffer with a checkpoint call to swap. 60 | - Dirty map not enough in case each frame is composited in multiple steps. 61 | - Emit difference between checkpoint state and current. 62 | - Optimization: Keep a rect or region of changed area. 63 | - Multiply previous symbol's new error with weight to increase or decrease 64 | stability (prevent flicker)? 65 | - Drawing context with clip rect/region, etc. 66 | - Potentially a context stack. 67 | - Getting into NCurses territory... 68 | - Video playback. 69 | - Interactive UI (may need to be in separate tool). 70 | 71 | Optimization 72 | ------------ 73 | 74 | - Preload next image/frame in delay phase. 75 | - Don't calculate error if we're only using a single symbol (e.g. vhalf). 76 | 77 | The Fine Material 78 | ----------------- 79 | 80 | - Tips. 81 | - For scrolling, use e.g. chafa input.jpg -s 200 | less -S 82 | - Rate-controlled playback with e.g. cat input.txt | pv -qL 100k 83 | - Playback with awk + proper inter-frame delay. 84 | - X11 applications in terminal 85 | $ Xvfb :99 -ac -fbdir t -screen 0 1440x900x24 & 86 | $ DISPLAY=:99 lament -root & 87 | $ chafa --watch t/Xvfb_screen0 88 | - gnome-shell in terminal 89 | $ XDG_SESSION_TYPE=x11 DISPLAY=:99 gnome-shell 90 | - Run as different user. 91 | - Using (unreleased) ffmpeg driver: 92 | $ ./ffmpeg -i movie.mkv -pix_fmt rgba -f chafa -color 16 -symbols vhalf,space -fill ascii - 93 | - Compact listing of icons: 94 | $ montage -tile 6x -background none -geometry 64x64+1+1 /usr/share/icons/Mint-X/*/16/*.png - \ 95 | | chafa -s 80 96 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Run this to generate all the initial makefiles, etc. 3 | 4 | srcdir=`dirname $0` 5 | test -z "$srcdir" && srcdir=. 6 | 7 | ORIGDIR=`pwd` 8 | cd $srcdir 9 | PROJECT=chafa 10 | TEST_TYPE=-f 11 | FILE=chafa/Makefile.am 12 | DIE=0 13 | 14 | MISSING_TOOLS= 15 | 16 | MY_ECHO=$(which echo) 17 | [ x$MY_ECHO = x ] && MY_ECHO=echo 18 | 19 | (autoconf --version) < /dev/null > /dev/null 2>&1 || { 20 | MISSING_TOOLS="${MISSING_TOOLS}autoconf " 21 | DIE=1 22 | } 23 | 24 | (automake --version) < /dev/null > /dev/null 2>&1 || { 25 | MISSING_TOOLS="${MISSING_TOOLS}automake " 26 | DIE=1 27 | } 28 | 29 | (libtoolize --version) < /dev/null > /dev/null 2>&1 || { 30 | MISSING_TOOLS="${MISSING_TOOLS}libtool " 31 | DIE=1 32 | } 33 | 34 | (pkg-config --version) < /dev/null > /dev/null 2>&1 || { 35 | MISSING_TOOLS="${MISSING_TOOLS}pkg-config " 36 | DIE=1 37 | } 38 | 39 | if test "$DIE" -eq 1; then 40 | ${MY_ECHO} 41 | ${MY_ECHO} -e "Missing mandatory tools:\e[1;31m $MISSING_TOOLS" 42 | ${MY_ECHO} -e "\e[0m" 43 | ${MY_ECHO} "These are required for building Chafa from its git repository." 44 | ${MY_ECHO} "You should be able to install them using your operating system's" 45 | ${MY_ECHO} "package manager (apt-get, yum, zypper or similar). Alternately" 46 | ${MY_ECHO} "they can be obtained directly from GNU: https://ftp.gnu.org/gnu/" 47 | ${MY_ECHO} 48 | ${MY_ECHO} "If you can't provide these tools, you may still be able to" 49 | ${MY_ECHO} "build Chafa from a tarball release: https://hpjansson.org/chafa/releases/" 50 | ${MY_ECHO} 51 | fi 52 | 53 | if test "$DIE" -eq 1; then 54 | exit 1 55 | fi 56 | 57 | test $TEST_TYPE $FILE || { 58 | ${MY_ECHO} 59 | ${MY_ECHO} "You must run this script in the top-level $PROJECT directory." 60 | ${MY_ECHO} 61 | exit 1 62 | } 63 | 64 | if test x$NOCONFIGURE = x && test -z "$*"; then 65 | ${MY_ECHO} 66 | ${MY_ECHO} "I am going to run ./configure with no arguments - if you wish " 67 | ${MY_ECHO} "to pass any to it, please specify them on the $0 command line." 68 | ${MY_ECHO} 69 | fi 70 | 71 | am_opt="--include-deps --add-missing" 72 | 73 | ${MY_ECHO} "Running libtoolize..." 74 | libtoolize --force --copy 75 | 76 | GTKDOCIZE=$(which gtkdocize 2>/dev/null) 77 | 78 | if test -z $GTKDOCIZE; then 79 | ${MY_ECHO} -e "Missing optional tool:\e[1;33m gtk-doc" 80 | ${MY_ECHO} -e "\e[0m" 81 | ${MY_ECHO} "Without this, no developer documentation will be generated." 82 | ${MY_ECHO} 83 | rm -f gtk-doc.make 84 | cat > gtk-doc.make < /dev/null 2>&1 && autoheader 97 | 98 | ${MY_ECHO} "Running automake..." 99 | automake -a $am_opt 100 | 101 | ${MY_ECHO} "Running autoconf..." 102 | autoconf 103 | 104 | cd $ORIGDIR 105 | 106 | conf_flags="--enable-maintainer-mode" 107 | 108 | if test x$NOCONFIGURE = x; then 109 | ${MY_ECHO} Running $srcdir/configure $conf_flags "$@" ... 110 | $srcdir/configure $conf_flags "$@" || exit 1 111 | else 112 | ${MY_ECHO} Skipping configure process. 113 | fi 114 | -------------------------------------------------------------------------------- /chafa.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: Chafa 7 | Description: Image to character art facsimile 8 | Requires: glib-2.0 9 | Version: @VERSION@ 10 | Libs: -L${libdir} -lchafa 11 | Libs.private: -lm 12 | Cflags: -I${includedir}/chafa -I${libdir}/chafa/include 13 | -------------------------------------------------------------------------------- /chafa/.gitignore: -------------------------------------------------------------------------------- 1 | /chafa-term-seq-doc.h 2 | /chafaconfig.h 3 | -------------------------------------------------------------------------------- /chafa/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = internal 2 | DISTCLEANFILES = 3 | BUILT_SOURCES = 4 | 5 | ## --- Library --- 6 | 7 | lib_LTLIBRARIES = libchafa.la 8 | noinst_LTLIBRARIES = 9 | noinst_HEADERS = 10 | 11 | libchafa_la_CFLAGS = $(LIBCHAFA_CFLAGS) $(GLIB_CFLAGS) -DCHAFA_COMPILATION 12 | libchafa_la_LDFLAGS = $(LIBCHAFA_LDFLAGS) -no-undefined -version-info 10:1:10 13 | libchafa_la_LIBADD = $(GLIB_LIBS) internal/libchafa-internal.la -lm 14 | 15 | libchafa_la_SOURCES = \ 16 | chafa-canvas.c \ 17 | chafa-canvas-config.c \ 18 | chafa-features.c \ 19 | chafa-frame.c \ 20 | chafa-image.c \ 21 | chafa-placement.c \ 22 | chafa-symbol-map.c \ 23 | chafa-term-db.c \ 24 | chafa-term-info.c \ 25 | chafa-util.c 26 | 27 | chafaincludedir=$(includedir)/chafa 28 | chafainclude_HEADERS = \ 29 | chafa.h \ 30 | chafa-canvas.h \ 31 | chafa-canvas-config.h \ 32 | chafa-common.h \ 33 | chafa-features.h \ 34 | chafa-frame.h \ 35 | chafa-image.h \ 36 | chafa-placement.h \ 37 | chafa-symbol-map.h \ 38 | chafa-term-db.h \ 39 | chafa-term-info.h \ 40 | chafa-term-seq-def.h \ 41 | chafa-util.h \ 42 | chafa-version-macros.h 43 | 44 | # Generate header prototypes with docstrings (-CC to pass through comments) 45 | # for terminal sequence accessors. These will be processed by gtk-doc to 46 | # produce documentation. 47 | 48 | noinst_HEADERS += chafa-term-seq-doc.h chafa-term-seq-doc-in.h 49 | DISTCLEANFILES += chafa-term-seq-doc.h 50 | BUILT_SOURCES += chafa-term-seq-doc.h 51 | 52 | chafa-term-seq-doc.h: $(srcdir)/chafa-term-seq-doc-in.h 53 | $(CPP) $(CPPFLAGS) -CC $< -o $@ 54 | 55 | # Generate chafaconfig.h 56 | # 57 | # The timestamp of the stamp file is used to indicate if chafaconfig.h is 58 | # up to date with respect to config.status. In the usual case, changes 59 | # to config.status will not result in changes to chafaconfig.h, so we 60 | # avoid touching its timestamp (so we don't rebuild the whole tree). 61 | 62 | DISTCLEANFILES += chafaconfig-stamp chafaconfig.h 63 | BUILT_SOURCES += chafaconfig-stamp 64 | configexecincludedir = $(libdir)/chafa/include 65 | nodist_configexecinclude_HEADERS = chafaconfig.h 66 | chafaconfig-stamp: ../config.status 67 | $(AM_V_GEN) cd $(top_builddir) && \ 68 | $(SHELL) ./config.status chafa/chafaconfig.h 69 | @touch chafaconfig-stamp 70 | 71 | ## --- General --- 72 | 73 | AM_CPPFLAGS = \ 74 | -I$(top_srcdir) 75 | -------------------------------------------------------------------------------- /chafa/chafa-canvas.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_CANVAS_H__ 21 | #define __CHAFA_CANVAS_H__ 22 | 23 | #if !defined (__CHAFA_H_INSIDE__) && !defined (CHAFA_COMPILATION) 24 | # error "Only can be included directly." 25 | #endif 26 | 27 | #include 28 | 29 | G_BEGIN_DECLS 30 | 31 | typedef struct ChafaCanvas ChafaCanvas; 32 | 33 | CHAFA_AVAILABLE_IN_ALL 34 | ChafaCanvas *chafa_canvas_new (const ChafaCanvasConfig *config); 35 | CHAFA_AVAILABLE_IN_ALL 36 | ChafaCanvas *chafa_canvas_new_similar (ChafaCanvas *orig); 37 | CHAFA_AVAILABLE_IN_ALL 38 | void chafa_canvas_ref (ChafaCanvas *canvas); 39 | CHAFA_AVAILABLE_IN_ALL 40 | void chafa_canvas_unref (ChafaCanvas *canvas); 41 | 42 | CHAFA_AVAILABLE_IN_ALL 43 | const ChafaCanvasConfig *chafa_canvas_peek_config (ChafaCanvas *canvas); 44 | 45 | CHAFA_AVAILABLE_IN_1_14 46 | void chafa_canvas_set_placement (ChafaCanvas *canvas, ChafaPlacement *placement); 47 | 48 | CHAFA_AVAILABLE_IN_1_2 49 | void chafa_canvas_draw_all_pixels (ChafaCanvas *canvas, ChafaPixelType src_pixel_type, 50 | const guint8 *src_pixels, 51 | gint src_width, gint src_height, gint src_rowstride); 52 | CHAFA_AVAILABLE_IN_1_6 53 | GString *chafa_canvas_print (ChafaCanvas *canvas, ChafaTermInfo *term_info); 54 | CHAFA_AVAILABLE_IN_1_14 55 | void chafa_canvas_print_rows (ChafaCanvas *canvas, ChafaTermInfo *term_info, 56 | GString ***array_out, gint *array_len_out); 57 | CHAFA_AVAILABLE_IN_1_14 58 | gchar **chafa_canvas_print_rows_strv (ChafaCanvas *canvas, ChafaTermInfo *term_info); 59 | 60 | CHAFA_AVAILABLE_IN_1_8 61 | gunichar chafa_canvas_get_char_at (ChafaCanvas *canvas, gint x, gint y); 62 | CHAFA_AVAILABLE_IN_1_8 63 | gint chafa_canvas_set_char_at (ChafaCanvas *canvas, gint x, gint y, gunichar c); 64 | 65 | CHAFA_AVAILABLE_IN_1_8 66 | void chafa_canvas_get_colors_at (ChafaCanvas *canvas, gint x, gint y, 67 | gint *fg_out, gint *bg_out); 68 | CHAFA_AVAILABLE_IN_1_8 69 | void chafa_canvas_set_colors_at (ChafaCanvas *canvas, gint x, gint y, 70 | gint fg, gint bg); 71 | 72 | CHAFA_AVAILABLE_IN_1_8 73 | void chafa_canvas_get_raw_colors_at (ChafaCanvas *canvas, gint x, gint y, 74 | gint *fg_out, gint *bg_out); 75 | CHAFA_AVAILABLE_IN_1_8 76 | void chafa_canvas_set_raw_colors_at (ChafaCanvas *canvas, gint x, gint y, 77 | gint fg, gint bg); 78 | 79 | CHAFA_DEPRECATED_IN_1_2 80 | void chafa_canvas_set_contents_rgba8 (ChafaCanvas *canvas, const guint8 *src_pixels, 81 | gint src_width, gint src_height, gint src_rowstride); 82 | CHAFA_DEPRECATED_IN_1_6 83 | GString *chafa_canvas_build_ansi (ChafaCanvas *canvas); 84 | 85 | G_END_DECLS 86 | 87 | #endif /* __CHAFA_CANVAS_H__ */ 88 | -------------------------------------------------------------------------------- /chafa/chafa-features.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_FEATURES_H__ 21 | #define __CHAFA_FEATURES_H__ 22 | 23 | #if !defined (__CHAFA_H_INSIDE__) && !defined (CHAFA_COMPILATION) 24 | # error "Only can be included directly." 25 | #endif 26 | 27 | G_BEGIN_DECLS 28 | 29 | /* Features */ 30 | 31 | typedef enum 32 | { 33 | CHAFA_FEATURE_MMX = (1 << 0), 34 | CHAFA_FEATURE_SSE41 = (1 << 1), 35 | CHAFA_FEATURE_POPCNT = (1 << 2), 36 | CHAFA_FEATURE_AVX2 = (1 << 3) 37 | } 38 | ChafaFeatures; 39 | 40 | CHAFA_AVAILABLE_IN_ALL 41 | ChafaFeatures chafa_get_builtin_features (void); 42 | CHAFA_AVAILABLE_IN_ALL 43 | ChafaFeatures chafa_get_supported_features (void); 44 | CHAFA_AVAILABLE_IN_ALL 45 | gchar *chafa_describe_features (ChafaFeatures features); 46 | 47 | CHAFA_AVAILABLE_IN_1_10 48 | gint chafa_get_n_threads (void); 49 | CHAFA_AVAILABLE_IN_1_10 50 | void chafa_set_n_threads (gint n); 51 | 52 | CHAFA_AVAILABLE_IN_1_10 53 | gint chafa_get_n_actual_threads (void); 54 | 55 | G_END_DECLS 56 | 57 | #endif /* __CHAFA_FEATURES_H__ */ 58 | -------------------------------------------------------------------------------- /chafa/chafa-frame.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_FRAME_H__ 21 | #define __CHAFA_FRAME_H__ 22 | 23 | #if !defined (__CHAFA_H_INSIDE__) && !defined (CHAFA_COMPILATION) 24 | # error "Only can be included directly." 25 | #endif 26 | 27 | G_BEGIN_DECLS 28 | 29 | typedef struct ChafaFrame ChafaFrame; 30 | 31 | CHAFA_AVAILABLE_IN_1_14 32 | ChafaFrame *chafa_frame_new (gconstpointer data, 33 | ChafaPixelType pixel_type, 34 | gint width, gint height, gint rowstride); 35 | CHAFA_AVAILABLE_IN_1_14 36 | ChafaFrame *chafa_frame_new_steal (gpointer data, 37 | ChafaPixelType pixel_type, 38 | gint width, gint height, gint rowstride); 39 | CHAFA_AVAILABLE_IN_1_14 40 | ChafaFrame *chafa_frame_new_borrow (gpointer data, 41 | ChafaPixelType pixel_type, 42 | gint width, gint height, gint rowstride); 43 | 44 | CHAFA_AVAILABLE_IN_1_14 45 | void chafa_frame_ref (ChafaFrame *frame); 46 | CHAFA_AVAILABLE_IN_1_14 47 | void chafa_frame_unref (ChafaFrame *frame); 48 | 49 | G_END_DECLS 50 | 51 | #endif /* __CHAFA_FRAME_H__ */ 52 | -------------------------------------------------------------------------------- /chafa/chafa-image.c: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #include "config.h" 21 | #include "chafa.h" 22 | #include "internal/chafa-private.h" 23 | 24 | /** 25 | * SECTION:chafa-image 26 | * @title: ChafaImage 27 | * @short_description: An image to be placed on a #ChafaCanvas 28 | * 29 | * A #ChafaImage represents a raster image for placement on a #ChafaCanvas. It 30 | * can currently hold a single #ChafaFrame. 31 | * 32 | * To place an image on a canvas, it must first be assigned to a #ChafaPlacement. 33 | **/ 34 | 35 | /** 36 | * chafa_image_new: 37 | * 38 | * Creates a new #ChafaImage. The image is initially transparent and dimensionless. 39 | * 40 | * Returns: The new image 41 | * 42 | * Since: 1.14 43 | **/ 44 | ChafaImage * 45 | chafa_image_new (void) 46 | { 47 | ChafaImage *image; 48 | 49 | image = g_new0 (ChafaImage, 1); 50 | image->refs = 1; 51 | 52 | return image; 53 | } 54 | 55 | /** 56 | * chafa_image_ref: 57 | * @image: Image to add a reference to 58 | * 59 | * Adds a reference to @image. 60 | * 61 | * Since: 1.14 62 | **/ 63 | void 64 | chafa_image_ref (ChafaImage *image) 65 | { 66 | gint refs; 67 | 68 | g_return_if_fail (image != NULL); 69 | refs = g_atomic_int_get (&image->refs); 70 | g_return_if_fail (refs > 0); 71 | 72 | g_atomic_int_inc (&image->refs); 73 | } 74 | 75 | /** 76 | * chafa_image_unref: 77 | * @image: Image to remove a reference from 78 | * 79 | * Removes a reference from @image. When the reference count drops to zero, 80 | * the image is freed and can no longer be used. 81 | * 82 | * Since: 1.14 83 | **/ 84 | void 85 | chafa_image_unref (ChafaImage *image) 86 | { 87 | gint refs; 88 | 89 | g_return_if_fail (image != NULL); 90 | refs = g_atomic_int_get (&image->refs); 91 | g_return_if_fail (refs > 0); 92 | 93 | if (g_atomic_int_dec_and_test (&image->refs)) 94 | { 95 | if (image->frame) 96 | chafa_frame_unref (image->frame); 97 | g_free (image); 98 | } 99 | } 100 | 101 | /** 102 | * chafa_image_set_frame: 103 | * @image: Image to assign frame to 104 | * @frame: Frame to be assigned to image 105 | * 106 | * Assigns @frame as the content for @image. The image will keep its own 107 | * reference to the frame. 108 | * 109 | * Since: 1.14 110 | **/ 111 | void 112 | chafa_image_set_frame (ChafaImage *image, ChafaFrame *frame) 113 | { 114 | g_return_if_fail (image != NULL); 115 | 116 | if (frame) 117 | chafa_frame_ref (frame); 118 | if (image->frame) 119 | chafa_frame_unref (image->frame); 120 | 121 | image->frame = frame; 122 | } 123 | -------------------------------------------------------------------------------- /chafa/chafa-image.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_IMAGE_H__ 21 | #define __CHAFA_IMAGE_H__ 22 | 23 | #if !defined (__CHAFA_H_INSIDE__) && !defined (CHAFA_COMPILATION) 24 | # error "Only can be included directly." 25 | #endif 26 | 27 | G_BEGIN_DECLS 28 | 29 | typedef struct ChafaImage ChafaImage; 30 | 31 | CHAFA_AVAILABLE_IN_1_14 32 | ChafaImage *chafa_image_new (void); 33 | CHAFA_AVAILABLE_IN_1_14 34 | void chafa_image_ref (ChafaImage *image); 35 | CHAFA_AVAILABLE_IN_1_14 36 | void chafa_image_unref (ChafaImage *image); 37 | 38 | CHAFA_AVAILABLE_IN_1_14 39 | void chafa_image_set_frame (ChafaImage *image, ChafaFrame *frame); 40 | 41 | G_END_DECLS 42 | 43 | #endif /* __CHAFA_IMAGE_H__ */ 44 | -------------------------------------------------------------------------------- /chafa/chafa-placement.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_PLACEMENT_H__ 21 | #define __CHAFA_PLACEMENT_H__ 22 | 23 | #if !defined (__CHAFA_H_INSIDE__) && !defined (CHAFA_COMPILATION) 24 | # error "Only can be included directly." 25 | #endif 26 | 27 | G_BEGIN_DECLS 28 | 29 | typedef struct ChafaPlacement ChafaPlacement; 30 | 31 | CHAFA_AVAILABLE_IN_1_14 32 | ChafaPlacement *chafa_placement_new (ChafaImage *image, gint id); 33 | CHAFA_AVAILABLE_IN_1_14 34 | void chafa_placement_ref (ChafaPlacement *placement); 35 | CHAFA_AVAILABLE_IN_1_14 36 | void chafa_placement_unref (ChafaPlacement *placement); 37 | 38 | CHAFA_AVAILABLE_IN_1_14 39 | ChafaTuck chafa_placement_get_tuck (ChafaPlacement *placement); 40 | CHAFA_AVAILABLE_IN_1_14 41 | void chafa_placement_set_tuck (ChafaPlacement *placement, ChafaTuck tuck); 42 | 43 | CHAFA_AVAILABLE_IN_1_14 44 | ChafaAlign chafa_placement_get_halign (ChafaPlacement *placement); 45 | CHAFA_AVAILABLE_IN_1_14 46 | void chafa_placement_set_halign (ChafaPlacement *placement, ChafaAlign align); 47 | 48 | CHAFA_AVAILABLE_IN_1_14 49 | ChafaAlign chafa_placement_get_valign (ChafaPlacement *placement); 50 | CHAFA_AVAILABLE_IN_1_14 51 | void chafa_placement_set_valign (ChafaPlacement *placement, ChafaAlign align); 52 | 53 | G_END_DECLS 54 | 55 | #endif /* __CHAFA_PLACEMENT_H__ */ 56 | -------------------------------------------------------------------------------- /chafa/chafa-term-db.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2020-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_TERM_DB_H__ 21 | #define __CHAFA_TERM_DB_H__ 22 | 23 | #if !defined (__CHAFA_H_INSIDE__) && !defined (CHAFA_COMPILATION) 24 | # error "Only can be included directly." 25 | #endif 26 | 27 | #include 28 | 29 | G_BEGIN_DECLS 30 | 31 | typedef struct ChafaTermDb ChafaTermDb; 32 | 33 | CHAFA_AVAILABLE_IN_1_6 34 | ChafaTermDb *chafa_term_db_new (void); 35 | CHAFA_AVAILABLE_IN_1_6 36 | ChafaTermDb *chafa_term_db_copy (const ChafaTermDb *term_db); 37 | CHAFA_AVAILABLE_IN_1_6 38 | void chafa_term_db_ref (ChafaTermDb *term_db); 39 | CHAFA_AVAILABLE_IN_1_6 40 | void chafa_term_db_unref (ChafaTermDb *term_db); 41 | 42 | CHAFA_AVAILABLE_IN_1_6 43 | ChafaTermDb *chafa_term_db_get_default (void); 44 | 45 | CHAFA_AVAILABLE_IN_1_6 46 | ChafaTermInfo *chafa_term_db_detect (ChafaTermDb *term_db, gchar **envp); 47 | CHAFA_AVAILABLE_IN_1_6 48 | ChafaTermInfo *chafa_term_db_get_fallback_info (ChafaTermDb *term_db); 49 | 50 | G_END_DECLS 51 | 52 | #endif /* __CHAFA_TERM_DB_H__ */ 53 | -------------------------------------------------------------------------------- /chafa/chafa-term-seq-doc-in.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2020-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #define CHAFA_TERM_SEQ_DEF(name, NAME, n_args, arg_proc, arg_type, ...) \ 21 | gchar *chafa_term_info_emit_##name (const ChafaTermInfo *term_info, gchar *dest __VA_ARGS__); 22 | #define CHAFA_TERM_SEQ_DEF_VARARGS(name, NAME, arg_type) \ 23 | gchar * chafa_term_info_emit_##name(const ChafaTermInfo *term_info, gchar *dest, arg_type *args, gint n_args); 24 | #include "chafa-term-seq-def.h" 25 | #undef CHAFA_TERM_SEQ_DEF 26 | #undef CHAFA_TERM_SEQ_DEF_VARARGS 27 | 28 | typedef enum 29 | { 30 | #define CHAFA_TERM_SEQ_DEF(name, NAME, n_args, arg_proc, arg_type, ...) CHAFA_TERM_SEQ_##NAME, 31 | #define CHAFA_TERM_SEQ_DEF_VARARGS(name, NAME, arg_type) CHAFA_TERM_SEQ_##NAME, 32 | #include "chafa-term-seq-def.h" 33 | #undef CHAFA_TERM_SEQ_DEF 34 | #undef CHAFA_TERM_SEQ_DEF_VARARGS 35 | 36 | CHAFA_TERM_SEQ_MAX 37 | } 38 | ChafaTermSeq; 39 | -------------------------------------------------------------------------------- /chafa/chafa-util.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_UTIL_H__ 21 | #define __CHAFA_UTIL_H__ 22 | 23 | #if !defined (__CHAFA_H_INSIDE__) && !defined (CHAFA_COMPILATION) 24 | # error "Only can be included directly." 25 | #endif 26 | 27 | G_BEGIN_DECLS 28 | 29 | /* Miscellaneous functions */ 30 | 31 | CHAFA_AVAILABLE_IN_ALL 32 | void chafa_calc_canvas_geometry (gint src_width, 33 | gint src_height, 34 | gint *dest_width_inout, 35 | gint *dest_height_inout, 36 | gfloat font_ratio, 37 | gboolean zoom, 38 | gboolean stretch); 39 | 40 | CHAFA_AVAILABLE_IN_1_14 41 | void chafa_free_gstring_array (GString **gsa); 42 | 43 | G_END_DECLS 44 | 45 | #endif /* __CHAFA_UTIL_H__ */ 46 | -------------------------------------------------------------------------------- /chafa/chafa.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_H__ 21 | #define __CHAFA_H__ 22 | #define __CHAFA_H_INSIDE__ 23 | 24 | #include 25 | 26 | G_BEGIN_DECLS 27 | 28 | /* Version macros go before everything else */ 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | 43 | G_END_DECLS 44 | 45 | #undef __CHAFA_H_INSIDE__ 46 | #endif /* __CHAFA_H__ */ 47 | -------------------------------------------------------------------------------- /chafa/chafaconfig.h.in: -------------------------------------------------------------------------------- 1 | /* chafaconfig.h 2 | * 3 | * This is a generated file. Please modify 'chafaconfig.h.in'. */ 4 | 5 | #ifndef __CHAFACONFIG_H__ 6 | #define __CHAFACONFIG_H__ 7 | 8 | G_BEGIN_DECLS 9 | 10 | #define CHAFA_MAJOR_VERSION @CHAFA_MAJOR_VERSION@ 11 | #define CHAFA_MINOR_VERSION @CHAFA_MINOR_VERSION@ 12 | #define CHAFA_MICRO_VERSION @CHAFA_MICRO_VERSION@ 13 | 14 | G_END_DECLS 15 | 16 | #endif /* __CHAFACONFIG_H__ */ 17 | -------------------------------------------------------------------------------- /chafa/internal/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = smolscale 2 | 3 | ## --- Library --- 4 | 5 | noinst_LTLIBRARIES = libchafa-internal.la 6 | 7 | libchafa_internal_la_CFLAGS = $(LIBCHAFA_CFLAGS) $(GLIB_CFLAGS) -DCHAFA_COMPILATION 8 | libchafa_internal_la_LDFLAGS = $(LIBCHAFA_LDFLAGS) 9 | libchafa_internal_la_LIBADD = $(GLIB_LIBS) smolscale/libsmolscale.la -lm 10 | 11 | libchafa_internal_la_SOURCES = \ 12 | chafa-base64.c \ 13 | chafa-base64.h \ 14 | chafa-batch.c \ 15 | chafa-batch.h \ 16 | chafa-bitfield.h \ 17 | chafa-canvas-internal.h \ 18 | chafa-canvas-printer.c \ 19 | chafa-canvas-printer.h \ 20 | chafa-color.c \ 21 | chafa-color.h \ 22 | chafa-color-hash.c \ 23 | chafa-color-hash.h \ 24 | chafa-color-table.c \ 25 | chafa-color-table.h \ 26 | chafa-dither.c \ 27 | chafa-dither.h \ 28 | chafa-indexed-image.c \ 29 | chafa-indexed-image.h \ 30 | chafa-iterm2-canvas.c \ 31 | chafa-iterm2-canvas.h \ 32 | chafa-kitty-canvas.c \ 33 | chafa-kitty-canvas.h \ 34 | chafa-math-util.c \ 35 | chafa-math-util.h \ 36 | chafa-noise.c \ 37 | chafa-noise.h \ 38 | chafa-palette.c \ 39 | chafa-palette.h \ 40 | chafa-passthrough-encoder.c \ 41 | chafa-passthrough-encoder.h \ 42 | chafa-pca.c \ 43 | chafa-pca.h \ 44 | chafa-pixops.c \ 45 | chafa-pixops.h \ 46 | chafa-private.h \ 47 | chafa-sixel-canvas.c \ 48 | chafa-sixel-canvas.h \ 49 | chafa-string-util.c \ 50 | chafa-string-util.h \ 51 | chafa-symbols.c \ 52 | chafa-symbols-ascii.h \ 53 | chafa-symbols-block.h \ 54 | chafa-symbols-kana.h \ 55 | chafa-symbols-latin.h \ 56 | chafa-symbols-misc-narrow.h \ 57 | chafa-vector.h \ 58 | chafa-work-cell.c \ 59 | chafa-work-cell.h 60 | 61 | if HAVE_MMX_INTRINSICS 62 | noinst_LTLIBRARIES += libchafa-mmx.la 63 | libchafa_internal_la_LIBADD += libchafa-mmx.la 64 | libchafa_mmx_la_SOURCES = chafa-mmx.c 65 | libchafa_mmx_la_CFLAGS = $(LIBCHAFA_CFLAGS) $(GLIB_CFLAGS) -mmmx -DCHAFA_COMPILATION 66 | libchafa_mmx_la_LDFLAGS = $(LIBCHAFA_LDFLAGS) 67 | endif 68 | 69 | if HAVE_SSE41_INTRINSICS 70 | noinst_LTLIBRARIES += libchafa-sse41.la 71 | libchafa_internal_la_LIBADD += libchafa-sse41.la 72 | libchafa_sse41_la_SOURCES = chafa-sse41.c 73 | libchafa_sse41_la_CFLAGS = $(LIBCHAFA_CFLAGS) $(GLIB_CFLAGS) -msse4.1 -DCHAFA_COMPILATION 74 | libchafa_sse41_la_LDFLAGS = $(LIBCHAFA_LDFLAGS) 75 | endif 76 | 77 | if HAVE_POPCNT_INTRINSICS 78 | noinst_LTLIBRARIES += libchafa-popcnt.la 79 | libchafa_internal_la_LIBADD += libchafa-popcnt.la 80 | libchafa_popcnt_la_SOURCES = chafa-popcnt.c 81 | libchafa_popcnt_la_CFLAGS = $(LIBCHAFA_CFLAGS) $(GLIB_CFLAGS) -mpopcnt -DCHAFA_COMPILATION 82 | libchafa_popcnt_la_LDFLAGS = $(LIBCHAFA_LDFLAGS) 83 | endif 84 | 85 | if HAVE_AVX2_INTRINSICS 86 | noinst_LTLIBRARIES += libchafa-avx2.la 87 | libchafa_internal_la_LIBADD += libchafa-avx2.la 88 | libchafa_avx2_la_SOURCES = chafa-avx2.c 89 | libchafa_avx2_la_CFLAGS = $(LIBCHAFA_CFLAGS) $(GLIB_CFLAGS) -mavx2 -DCHAFA_COMPILATION 90 | libchafa_avx2_la_LDFLAGS = $(LIBCHAFA_LDFLAGS) 91 | endif 92 | 93 | ## --- General --- 94 | 95 | ## Include $(top_builddir)/chafa to get generated chafaconfig.h. 96 | 97 | AM_CPPFLAGS = \ 98 | -I$(top_srcdir)/chafa \ 99 | -I$(top_builddir)/chafa 100 | -------------------------------------------------------------------------------- /chafa/internal/chafa-base64.c: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2021-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #include "config.h" 21 | 22 | #include 23 | #include 24 | 25 | #include "chafa.h" 26 | #include "internal/chafa-base64.h" 27 | 28 | static const gchar base64_dict [] = 29 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 30 | "abcdefghijklmnopqrstuvwxyz" 31 | "0123456789+/"; 32 | 33 | void 34 | chafa_base64_init (ChafaBase64 *base64) 35 | { 36 | memset (base64, 0, sizeof (*base64)); 37 | } 38 | 39 | void 40 | chafa_base64_deinit (ChafaBase64 *base64) 41 | { 42 | memset (base64, 0, sizeof (*base64)); 43 | base64->buf_len = -1; 44 | } 45 | 46 | static void 47 | encode_3_bytes (GString *gs_out, guint32 bytes) 48 | { 49 | g_string_append_c (gs_out, base64_dict [(bytes >> (3 * 6)) & 0x3f]); 50 | g_string_append_c (gs_out, base64_dict [(bytes >> (2 * 6)) & 0x3f]); 51 | g_string_append_c (gs_out, base64_dict [(bytes >> (1 * 6)) & 0x3f]); 52 | g_string_append_c (gs_out, base64_dict [bytes & 0x3f]); 53 | } 54 | 55 | void 56 | chafa_base64_encode (ChafaBase64 *base64, GString *gs_out, gconstpointer in, gint in_len) 57 | { 58 | const guint8 *in_u8 = in; 59 | const guint8 *end_u8 = in_u8 + in_len; 60 | guint32 r; 61 | 62 | if (base64->buf_len + in_len < 3) 63 | { 64 | memcpy (base64->buf + base64->buf_len, in_u8, in_len); 65 | base64->buf_len += in_len; 66 | return; 67 | } 68 | 69 | if (base64->buf_len == 1) 70 | { 71 | r = (base64->buf [0] << 16) | (in_u8 [0] << 8) | in_u8 [1]; 72 | in_u8 += 2; 73 | encode_3_bytes (gs_out, r); 74 | } 75 | else if (base64->buf_len == 2) 76 | { 77 | r = (base64->buf [0] << 16) | (base64->buf [1] << 8) | in_u8 [0]; 78 | in_u8++; 79 | encode_3_bytes (gs_out, r); 80 | } 81 | 82 | base64->buf_len = 0; 83 | 84 | while (end_u8 - in_u8 >= 3) 85 | { 86 | r = (in_u8 [0] << 16) | (in_u8 [1] << 8) | in_u8 [2]; 87 | encode_3_bytes (gs_out, r); 88 | in_u8 += 3; 89 | } 90 | 91 | while (end_u8 - in_u8 > 0) 92 | { 93 | base64->buf [base64->buf_len++] = *(in_u8++); 94 | } 95 | } 96 | 97 | void 98 | chafa_base64_encode_end (ChafaBase64 *base64, GString *gs_out) 99 | { 100 | if (base64->buf_len == 1) 101 | { 102 | g_string_append_c (gs_out, base64_dict [base64->buf [0] >> 2]); 103 | g_string_append_c (gs_out, base64_dict [(base64->buf [0] << 4) & 0x30]); 104 | g_string_append (gs_out, "=="); 105 | } 106 | else if (base64->buf_len == 2) 107 | { 108 | g_string_append_c (gs_out, base64_dict [base64->buf [0] >> 2]); 109 | g_string_append_c (gs_out, base64_dict [((base64->buf [0] << 4) | (base64->buf [1] >> 4)) & 0x3f]); 110 | g_string_append_c (gs_out, base64_dict [(base64->buf [1] << 2) & 0x3c]); 111 | g_string_append_c (gs_out, '='); 112 | } 113 | 114 | base64->buf_len = 0; 115 | } 116 | -------------------------------------------------------------------------------- /chafa/internal/chafa-base64.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2021-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_BASE64_H__ 21 | #define __CHAFA_BASE64_H__ 22 | 23 | #include 24 | #include "chafa.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct 29 | { 30 | /* We turn 3-byte groups into 4-character base64 groups, so we 31 | * may need to buffer up to 2 bytes between batches */ 32 | guint8 buf [2]; 33 | gint buf_len; 34 | } 35 | ChafaBase64; 36 | 37 | void chafa_base64_init (ChafaBase64 *base64); 38 | void chafa_base64_deinit (ChafaBase64 *base64); 39 | 40 | void chafa_base64_encode (ChafaBase64 *base64, GString *gs_out, gconstpointer in, gint in_len); 41 | void chafa_base64_encode_end (ChafaBase64 *base64, GString *gs_out); 42 | 43 | G_END_DECLS 44 | 45 | #endif /* __CHAFA_BASE64_H__ */ 46 | -------------------------------------------------------------------------------- /chafa/internal/chafa-batch.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_BATCH_H__ 21 | #define __CHAFA_BATCH_H__ 22 | 23 | #include 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef struct 28 | { 29 | gint first_row; 30 | gint n_rows; 31 | 32 | gpointer ret_p; 33 | gint ret_n; 34 | } 35 | ChafaBatchInfo; 36 | 37 | void chafa_process_batches (gpointer ctx, GFunc batch_func, GFunc post_func, 38 | gint n_rows, gint n_batches, gint batch_unit); 39 | 40 | G_END_DECLS 41 | 42 | #endif /* __CHAFA_BATCH_H__ */ 43 | -------------------------------------------------------------------------------- /chafa/internal/chafa-bitfield.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_BITFIELD_H__ 21 | #define __CHAFA_BITFIELD_H__ 22 | 23 | #include 24 | #include 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct 29 | { 30 | guint32 *bits; 31 | guint n_bits; 32 | } 33 | ChafaBitfield; 34 | 35 | static inline void 36 | chafa_bitfield_init (ChafaBitfield *bitfield, guint n_bits) 37 | { 38 | bitfield->n_bits = n_bits; 39 | bitfield->bits = g_malloc0 ((n_bits + 31) / 8); 40 | } 41 | 42 | static inline void 43 | chafa_bitfield_deinit (ChafaBitfield *bitfield) 44 | { 45 | g_free (bitfield->bits); 46 | bitfield->n_bits = 0; 47 | bitfield->bits = NULL; 48 | } 49 | 50 | static inline void 51 | chafa_bitfield_clear (ChafaBitfield *bitfield) 52 | { 53 | memset (bitfield->bits, 0, (bitfield->n_bits + 31) / 8); 54 | } 55 | 56 | static inline gboolean 57 | chafa_bitfield_get_bit (const ChafaBitfield *bitfield, guint nth) 58 | { 59 | gint index; 60 | gint shift; 61 | 62 | g_return_val_if_fail (nth < bitfield->n_bits, FALSE); 63 | 64 | index = (nth / 32); 65 | shift = (nth % 32); 66 | 67 | return (bitfield->bits [index] >> shift) & 1U; 68 | } 69 | 70 | static inline void 71 | chafa_bitfield_set_bit (ChafaBitfield *bitfield, guint nth, gboolean value) 72 | { 73 | gint index; 74 | gint shift; 75 | guint32 v32; 76 | 77 | g_return_if_fail (nth < bitfield->n_bits); 78 | 79 | index = (nth / 32); 80 | shift = (nth % 32); 81 | v32 = (guint32) !!value; 82 | 83 | bitfield->bits [index] = (bitfield->bits [index] & ~(1UL << shift)) | (v32 << shift); 84 | } 85 | 86 | G_END_DECLS 87 | 88 | #endif /* __CHAFA_BITFIELD_H__ */ 89 | -------------------------------------------------------------------------------- /chafa/internal/chafa-canvas-internal.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_CANVAS_INTERNAL_H__ 21 | #define __CHAFA_CANVAS_INTERNAL_H__ 22 | 23 | #include 24 | #include "chafa.h" 25 | #include "internal/chafa-private.h" 26 | #include "internal/chafa-pixops.h" 27 | 28 | G_BEGIN_DECLS 29 | 30 | struct ChafaCanvasCell 31 | { 32 | gunichar c; 33 | 34 | /* Colors can be either packed RGBA or index */ 35 | guint32 fg_color; 36 | guint32 bg_color; 37 | }; 38 | 39 | struct ChafaCanvas 40 | { 41 | gint refs; 42 | 43 | gint width_pixels, height_pixels; 44 | ChafaPixel *pixels; 45 | ChafaCanvasCell *cells; 46 | guint have_alpha : 1; 47 | guint needs_clear : 1; 48 | 49 | /* Whether to consider inverted symbols; FALSE if using FG only */ 50 | guint consider_inverted : 1; 51 | 52 | /* Whether to extract symbol colors; FALSE if using default colors */ 53 | guint extract_colors : 1; 54 | 55 | /* Whether to quantize colors before calculating error (slower, but 56 | * yields better results in palettized modes, especially 16/8) */ 57 | guint use_quantized_error : 1; 58 | 59 | ChafaColorPair default_colors; 60 | guint work_factor_int; 61 | 62 | /* Character to use in cells where fg color == bg color. Typically 63 | * space, but could be something else depending on the symbol map. */ 64 | gunichar blank_char; 65 | 66 | /* Character to use in cells where fg color == bg color and the color 67 | * is only legal in FG. Typically 0x2588 (solid block), but could be 68 | * something else depending on the symbol map. Can be zero if there is 69 | * no good candidate! */ 70 | gunichar solid_char; 71 | 72 | ChafaCanvasConfig config; 73 | 74 | /* Used when setting pixel data */ 75 | ChafaDither dither; 76 | 77 | /* This is NULL in CHAFA_PIXEL_MODE_SYMBOLS, otherwise one of: 78 | * (ChafaSixelCanvas *), (ChafaKittyCanvas *), (ChafaIterm2Canvas *) */ 79 | gpointer pixel_canvas; 80 | 81 | /* It's possible to have a single placement that covers the entire 82 | * canvas. In this case, it is stored here. */ 83 | ChafaPlacement *placement; 84 | 85 | /* Our palettes. Kind of a big structure, so they go last. */ 86 | ChafaPalette fg_palette; 87 | ChafaPalette bg_palette; 88 | }; 89 | 90 | G_END_DECLS 91 | 92 | #endif /* __CHAFA_CANVAS_INTERNAL_H__ */ 93 | -------------------------------------------------------------------------------- /chafa/internal/chafa-canvas-printer.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2020-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_CANVAS_PRINTER_H__ 21 | #define __CHAFA_CANVAS_PRINTER_H__ 22 | 23 | #include 24 | #include "chafa.h" 25 | #include "internal/chafa-canvas-internal.h" 26 | #include "internal/chafa-private.h" 27 | #include "internal/chafa-pixops.h" 28 | 29 | G_BEGIN_DECLS 30 | 31 | GString *chafa_canvas_print_symbols (ChafaCanvas *canvas, ChafaTermInfo *ti); 32 | void chafa_canvas_print_symbol_rows (ChafaCanvas *canvas, ChafaTermInfo *ti, 33 | GString ***array_out, gint *array_len_out); 34 | 35 | G_END_DECLS 36 | 37 | #endif /* __CHAFA_CANVAS_PRINTER_H__ */ 38 | -------------------------------------------------------------------------------- /chafa/internal/chafa-color-hash.c: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #include "config.h" 21 | 22 | #include "chafa.h" 23 | #include "internal/chafa-color-hash.h" 24 | 25 | void 26 | chafa_color_hash_init (ChafaColorHash *color_hash) 27 | { 28 | guint i; 29 | guint32 j; 30 | 31 | /* Initialize with invalid entries */ 32 | 33 | for (i = 0, j = 0; i < CHAFA_COLOR_HASH_N_ENTRIES; i++) 34 | { 35 | while (_chafa_color_hash_calc_hash (j) == i) 36 | { 37 | j++; 38 | j %= 0x01000000; 39 | } 40 | 41 | color_hash->map [i] = j << 8; 42 | } 43 | } 44 | 45 | void 46 | chafa_color_hash_deinit (G_GNUC_UNUSED ChafaColorHash *color_hash) 47 | { 48 | } 49 | -------------------------------------------------------------------------------- /chafa/internal/chafa-color-hash.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_COLOR_HASH_H__ 21 | #define __CHAFA_COLOR_HASH_H__ 22 | 23 | G_BEGIN_DECLS 24 | 25 | #define CHAFA_COLOR_HASH_N_ENTRIES 16384 26 | 27 | typedef struct 28 | { 29 | guint32 map [CHAFA_COLOR_HASH_N_ENTRIES]; 30 | } 31 | ChafaColorHash; 32 | 33 | void chafa_color_hash_init (ChafaColorHash *color_hash); 34 | void chafa_color_hash_deinit (ChafaColorHash *color_hash); 35 | 36 | static inline guint 37 | _chafa_color_hash_calc_hash (guint32 color) 38 | { 39 | color &= 0x00ffffff; 40 | 41 | return (color ^ (color >> 7) ^ (color >> 14)) % CHAFA_COLOR_HASH_N_ENTRIES; 42 | } 43 | 44 | static inline void 45 | chafa_color_hash_replace (ChafaColorHash *color_hash, guint32 color, guint8 pen) 46 | { 47 | guint index = _chafa_color_hash_calc_hash (color); 48 | guint32 entry = (color << 8) | pen; 49 | 50 | color_hash->map [index] = entry; 51 | } 52 | 53 | static inline gint 54 | chafa_color_hash_lookup (const ChafaColorHash *color_hash, guint32 color) 55 | { 56 | guint index = _chafa_color_hash_calc_hash (color); 57 | guint32 entry = color_hash->map [index]; 58 | 59 | if ((entry & 0xffffff00) == (color << 8)) 60 | return entry & 0xff; 61 | 62 | return -1; 63 | } 64 | 65 | G_END_DECLS 66 | 67 | #endif /* __CHAFA_COLOR_HASH_H__ */ 68 | -------------------------------------------------------------------------------- /chafa/internal/chafa-color-table.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_COLOR_TABLE_H__ 21 | #define __CHAFA_COLOR_TABLE_H__ 22 | 23 | #include "internal/chafa-pca.h" 24 | 25 | G_BEGIN_DECLS 26 | 27 | #define CHAFA_COLOR_TABLE_MAX_ENTRIES 256 28 | 29 | typedef struct 30 | { 31 | gint v [2]; 32 | gint pen; 33 | } 34 | ChafaColorTableEntry; 35 | 36 | typedef struct 37 | { 38 | ChafaColorTableEntry entries [CHAFA_COLOR_TABLE_MAX_ENTRIES]; 39 | 40 | /* Each pen is 24 bits (B8G8R8) of color information */ 41 | guint32 pens [CHAFA_COLOR_TABLE_MAX_ENTRIES]; 42 | 43 | gint n_entries; 44 | guint is_sorted : 1; 45 | 46 | ChafaVec3i32 eigenvectors [2]; 47 | ChafaVec3i32 average; 48 | 49 | guint eigen_mul [2]; 50 | } 51 | ChafaColorTable; 52 | 53 | void chafa_color_table_init (ChafaColorTable *color_table); 54 | void chafa_color_table_deinit (ChafaColorTable *color_table); 55 | 56 | guint32 chafa_color_table_get_pen_color (const ChafaColorTable *color_table, gint pen); 57 | void chafa_color_table_set_pen_color (ChafaColorTable *color_table, gint pen, guint32 color); 58 | 59 | void chafa_color_table_sort (ChafaColorTable *color_table); 60 | gint chafa_color_table_find_nearest_pen (const ChafaColorTable *color_table, guint32 color); 61 | 62 | G_END_DECLS 63 | 64 | #endif /* __CHAFA_COLOR_TABLE_H__ */ 65 | -------------------------------------------------------------------------------- /chafa/internal/chafa-dither.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_DITHER_H__ 21 | #define __CHAFA_DITHER_H__ 22 | 23 | #include "internal/chafa-palette.h" 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef struct 28 | { 29 | ChafaDitherMode mode; 30 | gdouble intensity; 31 | gint grain_width_shift; 32 | gint grain_height_shift; 33 | 34 | gint texture_size_shift; 35 | guint texture_size_mask; 36 | gint *texture_data; 37 | } 38 | ChafaDither; 39 | 40 | void chafa_dither_init (ChafaDither *dither, ChafaDitherMode mode, 41 | gdouble intensity, 42 | gint grain_width, gint grain_height); 43 | void chafa_dither_deinit (ChafaDither *dither); 44 | void chafa_dither_copy (const ChafaDither *src, ChafaDither *dest); 45 | 46 | ChafaColor chafa_dither_color (const ChafaDither *dither, ChafaColor color, gint x, gint y); 47 | 48 | G_END_DECLS 49 | 50 | #endif /* __CHAFA_DITHER_H__ */ 51 | -------------------------------------------------------------------------------- /chafa/internal/chafa-indexed-image.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_INDEXED_IMAGE_H__ 21 | #define __CHAFA_INDEXED_IMAGE_H__ 22 | 23 | #include "internal/chafa-palette.h" 24 | #include "internal/chafa-dither.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct 29 | { 30 | gint width, height; 31 | ChafaPalette palette; 32 | ChafaDither dither; 33 | guint8 *pixels; 34 | } 35 | ChafaIndexedImage; 36 | 37 | ChafaIndexedImage *chafa_indexed_image_new (gint width, gint height, 38 | const ChafaPalette *palette, 39 | const ChafaDither *dither); 40 | void chafa_indexed_image_destroy (ChafaIndexedImage *indexed_image); 41 | 42 | void chafa_indexed_image_draw_pixels (ChafaIndexedImage *indexed_image, 43 | ChafaColorSpace color_space, 44 | ChafaPixelType src_pixel_type, 45 | gconstpointer src_pixels, 46 | gint src_width, gint src_height, gint src_rowstride, 47 | gint dest_width, gint dest_height, 48 | ChafaAlign halign, ChafaAlign valign, 49 | ChafaTuck tuck, 50 | gfloat quality); 51 | 52 | G_END_DECLS 53 | 54 | #endif /* __CHAFA_INDEXED_IMAGE_H__ */ 55 | -------------------------------------------------------------------------------- /chafa/internal/chafa-iterm2-canvas.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2021-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_ITERM2_CANVAS_H__ 21 | #define __CHAFA_ITERM2_CANVAS_H__ 22 | 23 | #include "chafa.h" 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef struct 28 | { 29 | gint width, height; 30 | gpointer rgba_image; 31 | } 32 | ChafaIterm2Canvas; 33 | 34 | ChafaIterm2Canvas *chafa_iterm2_canvas_new (gint width, gint height); 35 | void chafa_iterm2_canvas_destroy (ChafaIterm2Canvas *iterm2_canvas); 36 | 37 | void chafa_iterm2_canvas_draw_all_pixels (ChafaIterm2Canvas *iterm2_canvas, ChafaPixelType src_pixel_type, 38 | gconstpointer src_pixels, 39 | gint src_width, gint src_height, gint src_rowstride, 40 | ChafaColor bg_color, 41 | ChafaAlign halign, ChafaAlign valign, 42 | ChafaTuck tuck); 43 | void chafa_iterm2_canvas_build_ansi (ChafaIterm2Canvas *iterm2_canvas, ChafaTermInfo *term_info, GString *out_str, 44 | gint width_cells, gint height_cells); 45 | 46 | G_END_DECLS 47 | 48 | #endif /* __CHAFA_ITERM2_CANVAS_H__ */ 49 | -------------------------------------------------------------------------------- /chafa/internal/chafa-kitty-canvas.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_KITTY_CANVAS_H__ 21 | #define __CHAFA_KITTY_CANVAS_H__ 22 | 23 | #include "chafa.h" 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef struct 28 | { 29 | gint width, height; 30 | gpointer rgba_image; 31 | } 32 | ChafaKittyCanvas; 33 | 34 | ChafaKittyCanvas *chafa_kitty_canvas_new (gint width, gint height); 35 | void chafa_kitty_canvas_destroy (ChafaKittyCanvas *kitty_canvas); 36 | 37 | void chafa_kitty_canvas_draw_all_pixels (ChafaKittyCanvas *kitty_canvas, 38 | ChafaPixelType src_pixel_type, 39 | gconstpointer src_pixels, 40 | gint src_width, gint src_height, gint src_rowstride, 41 | ChafaColor bg_color, 42 | ChafaAlign halign, ChafaAlign valign, 43 | ChafaTuck tuck); 44 | void chafa_kitty_canvas_build_ansi (ChafaKittyCanvas *kitty_canvas, ChafaTermInfo *term_info, GString *out_str, 45 | gint width_cells, gint height_cells, 46 | gint placement_id, 47 | ChafaPassthrough passthrough); 48 | 49 | G_END_DECLS 50 | 51 | #endif /* __CHAFA_KITTY_CANVAS_H__ */ 52 | -------------------------------------------------------------------------------- /chafa/internal/chafa-math-util.c: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2021-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #include "config.h" 21 | 22 | #include 23 | #include "internal/chafa-private.h" 24 | #include "internal/chafa-math-util.h" 25 | 26 | static gint 27 | align_dim (ChafaAlign align, gint src_size, gint dest_size) 28 | { 29 | gint ofs; 30 | 31 | g_return_val_if_fail (src_size <= dest_size, 0); 32 | 33 | switch (align) 34 | { 35 | case CHAFA_ALIGN_START: 36 | ofs = 0; 37 | break; 38 | 39 | case CHAFA_ALIGN_CENTER: 40 | ofs = (dest_size - src_size) / 2; 41 | break; 42 | 43 | case CHAFA_ALIGN_END: 44 | ofs = dest_size - src_size; 45 | break; 46 | 47 | default: 48 | g_assert_not_reached (); 49 | } 50 | 51 | return ofs; 52 | } 53 | 54 | void 55 | chafa_tuck_and_align (gint src_width, gint src_height, 56 | gint dest_width, gint dest_height, 57 | ChafaAlign halign, ChafaAlign valign, 58 | ChafaTuck tuck, 59 | gint *ofs_x_out, gint *ofs_y_out, 60 | gint *width_out, gint *height_out) 61 | { 62 | gfloat ratio [2]; 63 | 64 | switch (tuck) 65 | { 66 | case CHAFA_TUCK_STRETCH: 67 | *ofs_x_out = 0; 68 | *ofs_y_out = 0; 69 | *width_out = dest_width; 70 | *height_out = dest_height; 71 | break; 72 | 73 | case CHAFA_TUCK_SHRINK_TO_FIT: 74 | if (src_width <= dest_width && src_height <= dest_height) 75 | { 76 | /* Image fits entirely in dest. Do alignment only, no scaling. */ 77 | *width_out = src_width; 78 | *height_out = src_height; 79 | break; 80 | } 81 | 82 | /* Fall through */ 83 | 84 | case CHAFA_TUCK_FIT: 85 | ratio [0] = (gfloat) dest_width / (gfloat) src_width; 86 | ratio [1] = (gfloat) dest_height / (gfloat) src_height; 87 | 88 | *width_out = ceilf (src_width * MIN (ratio [0], ratio [1])); 89 | *height_out = ceilf (src_height * MIN (ratio [0], ratio [1])); 90 | break; 91 | 92 | default: 93 | g_assert_not_reached (); 94 | } 95 | 96 | /* Never exceed the dest size */ 97 | *width_out = MIN (*width_out, dest_width); 98 | *height_out = MIN (*height_out, dest_height); 99 | 100 | *ofs_x_out = align_dim (halign, *width_out, dest_width); 101 | *ofs_y_out = align_dim (valign, *height_out, dest_height); 102 | 103 | #if 0 104 | g_printerr ("src=(%dx%d) dest=(%dx%d) out=(%dx%d) @ (%d,%d)\n", 105 | src_width, src_height, 106 | dest_width, dest_height, 107 | *width_out, *height_out, 108 | *ofs_x_out, *ofs_y_out); 109 | #endif 110 | } 111 | 112 | gint 113 | chafa_round_up_to_multiple_of (gint value, gint m) 114 | { 115 | value = value + m - 1; 116 | return value - (value % m); 117 | } 118 | -------------------------------------------------------------------------------- /chafa/internal/chafa-math-util.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2021-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_MATH_UTIL_H__ 21 | #define __CHAFA_MATH_UTIL_H__ 22 | 23 | #include 24 | 25 | G_BEGIN_DECLS 26 | 27 | #define CHAFA_SQUARE(n) ((n) * (n)) 28 | 29 | void chafa_tuck_and_align (gint src_width, gint src_height, 30 | gint dest_width, gint dest_height, 31 | ChafaAlign halign, ChafaAlign valign, 32 | ChafaTuck tuck, 33 | gint *ofs_x_out, gint *ofs_y_out, 34 | gint *width_out, gint *height_out); 35 | 36 | gint chafa_round_up_to_multiple_of (gint value, gint m); 37 | 38 | G_END_DECLS 39 | 40 | #endif /* __CHAFA_MATH_UTIL_H__ */ 41 | -------------------------------------------------------------------------------- /chafa/internal/chafa-mmx.c: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #include "config.h" 21 | 22 | #include 23 | #include "chafa.h" 24 | #include "internal/chafa-private.h" 25 | 26 | void 27 | calc_colors_mmx (const ChafaPixel *pixels, ChafaColorAccum *accums_out, const guint8 *cov) 28 | { 29 | __m64 accum [2] = { 0 }; 30 | const guint32 *u32p0 = (const guint32 *) pixels; 31 | __m64 m64b; 32 | gint i; 33 | 34 | m64b = _mm_setzero_si64 (); 35 | 36 | for (i = 0; i < CHAFA_SYMBOL_N_PIXELS; i++) 37 | { 38 | __m64 *m64p1; 39 | __m64 m64a; 40 | 41 | m64p1 = &accum [cov [i]]; 42 | m64a = _mm_cvtsi32_si64 (u32p0 [i]); 43 | m64a = _mm_unpacklo_pi8 (m64a, m64b); 44 | *m64p1 = _mm_adds_pi16 (*m64p1, m64a); 45 | } 46 | 47 | ((__m64 *) accums_out) [0] = accum [0]; 48 | ((__m64 *) accums_out) [1] = accum [1]; 49 | 50 | #if 0 51 | /* Called after outer loop is done */ 52 | _mm_empty (); 53 | #endif 54 | } 55 | 56 | void 57 | chafa_leave_mmx (void) 58 | { 59 | #ifdef HAVE_MMX_INTRINSICS 60 | if (chafa_have_mmx ()) 61 | _mm_empty (); 62 | #endif 63 | } 64 | -------------------------------------------------------------------------------- /chafa/internal/chafa-noise.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_NOISE_H__ 21 | #define __CHAFA_NOISE_H__ 22 | 23 | G_BEGIN_DECLS 24 | 25 | #define CHAFA_NOISE_TEXTURE_DIM 64 26 | #define CHAFA_NOISE_TEXTURE_N_CHANNELS 3 27 | #define CHAFA_NOISE_TEXTURE_N_PIXELS (CHAFA_NOISE_TEXTURE_DIM * CHAFA_NOISE_TEXTURE_DIM) 28 | #define CHAFA_NOISE_TEXTURE_ARRAY_LEN (CHAFA_NOISE_TEXTURE_N_PIXELS * CHAFA_NOISE_TEXTURE_N_CHANNELS) 29 | 30 | gint *chafa_gen_noise_matrix (gdouble magnitude); 31 | 32 | G_END_DECLS 33 | 34 | #endif /* __CHAFA_NOISE_H__ */ 35 | -------------------------------------------------------------------------------- /chafa/internal/chafa-palette.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_PALETTE_H__ 21 | #define __CHAFA_PALETTE_H__ 22 | 23 | #include 24 | #include "internal/chafa-color.h" 25 | #include "internal/chafa-color-table.h" 26 | 27 | G_BEGIN_DECLS 28 | 29 | typedef enum 30 | { 31 | CHAFA_PALETTE_TYPE_DYNAMIC_256, 32 | CHAFA_PALETTE_TYPE_FIXED_256, 33 | CHAFA_PALETTE_TYPE_FIXED_240, 34 | CHAFA_PALETTE_TYPE_FIXED_16, 35 | CHAFA_PALETTE_TYPE_FIXED_8, 36 | CHAFA_PALETTE_TYPE_FIXED_FGBG 37 | } 38 | ChafaPaletteType; 39 | 40 | typedef struct 41 | { 42 | ChafaPaletteType type; 43 | ChafaPaletteColor colors [CHAFA_PALETTE_INDEX_MAX]; 44 | ChafaColorTable table [CHAFA_COLOR_SPACE_MAX]; 45 | gint first_color; 46 | gint n_colors; 47 | gint alpha_threshold; 48 | gint transparent_index; 49 | } 50 | ChafaPalette; 51 | 52 | /* Global init */ 53 | void chafa_init_palette (void); 54 | 55 | void chafa_palette_init (ChafaPalette *palette_out, ChafaPaletteType type); 56 | void chafa_palette_deinit (ChafaPalette *palette); 57 | 58 | void chafa_palette_copy (const ChafaPalette *src, ChafaPalette *dest); 59 | void chafa_palette_generate (ChafaPalette *palette_out, gconstpointer pixels, gint n_pixels, 60 | ChafaColorSpace color_space, gfloat quality); 61 | 62 | ChafaPaletteType chafa_palette_get_type (const ChafaPalette *palette); 63 | 64 | gint chafa_palette_get_first_color (const ChafaPalette *palette); 65 | gint chafa_palette_get_n_colors (const ChafaPalette *palette); 66 | 67 | gint chafa_palette_lookup_nearest (const ChafaPalette *palette, ChafaColorSpace color_space, 68 | const ChafaColor *color, ChafaColorCandidates *candidates); 69 | 70 | gint chafa_palette_lookup_with_error (const ChafaPalette *palette, ChafaColorSpace color_space, 71 | ChafaColor color, ChafaColorAccum *error_inout); 72 | 73 | const ChafaColor *chafa_palette_get_color (const ChafaPalette *palette, ChafaColorSpace color_space, 74 | gint index); 75 | void chafa_palette_set_color (ChafaPalette *palette, gint index, const ChafaColor *color); 76 | 77 | gint chafa_palette_get_alpha_threshold (const ChafaPalette *palette); 78 | void chafa_palette_set_alpha_threshold (ChafaPalette *palette, gint alpha_threshold); 79 | 80 | gint chafa_palette_get_transparent_index (const ChafaPalette *palette); 81 | void chafa_palette_set_transparent_index (ChafaPalette *palette, gint index); 82 | 83 | G_END_DECLS 84 | 85 | #endif /* __CHAFA_PALETTE_H__ */ 86 | -------------------------------------------------------------------------------- /chafa/internal/chafa-passthrough-encoder.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_PASSTHROUGH_ENCODER_H__ 21 | #define __CHAFA_PASSTHROUGH_ENCODER_H__ 22 | 23 | #include "chafa.h" 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef struct 28 | { 29 | ChafaPassthrough mode; 30 | ChafaTermInfo *term_info; 31 | GString *out; 32 | gint packet_size; 33 | } 34 | ChafaPassthroughEncoder; 35 | 36 | 37 | void chafa_passthrough_encoder_begin (ChafaPassthroughEncoder *ptenc, 38 | ChafaPassthrough passthrough, 39 | ChafaTermInfo *term_info, 40 | GString *out_str); 41 | void chafa_passthrough_encoder_end (ChafaPassthroughEncoder *ptenc); 42 | 43 | void chafa_passthrough_encoder_append (ChafaPassthroughEncoder *ptenc, 44 | const gchar *in); 45 | void chafa_passthrough_encoder_append_len (ChafaPassthroughEncoder *ptenc, 46 | const gchar *in, 47 | gint len); 48 | void chafa_passthrough_encoder_flush (ChafaPassthroughEncoder *ptenc); 49 | void chafa_passthrough_encoder_reset (ChafaPassthroughEncoder *ptenc); 50 | 51 | G_END_DECLS 52 | 53 | #endif /* __CHAFA_PASSTHROUGH_ENCODER_H__ */ 54 | -------------------------------------------------------------------------------- /chafa/internal/chafa-pca.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_PCA_H__ 21 | #define __CHAFA_PCA_H__ 22 | 23 | #include 24 | #include "internal/chafa-vector.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | void chafa_vec3f32_array_compute_pca (const ChafaVec3f32 *vecs_in, gint n_vecs, 29 | gint n_components, 30 | ChafaVec3f32 *eigenvectors_out, 31 | gfloat *eigenvalues_out, 32 | ChafaVec3f32 *average_out); 33 | 34 | G_END_DECLS 35 | 36 | #endif /* __CHAFA_PCA_H__ */ 37 | -------------------------------------------------------------------------------- /chafa/internal/chafa-pixops.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2020-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_PIXOPS_H__ 21 | #define __CHAFA_PIXOPS_H__ 22 | 23 | #include 24 | #include "internal/chafa-private.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | void chafa_prepare_pixel_data_for_symbols (const ChafaPalette *palette, 29 | const ChafaDither *dither, 30 | ChafaColorSpace color_space, 31 | gboolean preprocessing_enabled, 32 | gint work_factor, 33 | ChafaPixelType src_pixel_type, 34 | gconstpointer src_pixels, 35 | gint src_width, 36 | gint src_height, 37 | gint src_rowstride, 38 | ChafaPixel *dest_pixels, 39 | gint dest_width, 40 | gint dest_height, 41 | gint cell_width, 42 | gint cell_height, 43 | ChafaAlign halign, 44 | ChafaAlign valign, 45 | ChafaTuck tuck); 46 | 47 | void chafa_sort_pixel_index_by_channel (guint8 *index, 48 | const ChafaPixel *pixels, gint n_pixels, 49 | gint ch); 50 | 51 | G_END_DECLS 52 | 53 | #endif /* __CHAFA_PIXOPS_H__ */ 54 | -------------------------------------------------------------------------------- /chafa/internal/chafa-popcnt.c: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #include "config.h" 21 | 22 | #include 23 | #include "chafa.h" 24 | #include "internal/chafa-private.h" 25 | 26 | gint 27 | chafa_pop_count_u64_builtin (guint64 v) 28 | { 29 | #if defined(HAVE_POPCNT64_INTRINSICS) 30 | return (gint) _mm_popcnt_u64 (v); 31 | #else /* HAVE_POPCNT32_INTRINSICS */ 32 | const guint32 *w = (const guint32 *) &v; 33 | return (gint) _mm_popcnt_u32(w[0]) + _mm_popcnt_u32(w[1]); 34 | #endif 35 | } 36 | 37 | void 38 | chafa_pop_count_vu64_builtin (const guint64 *vv, gint *vc, gint n) 39 | { 40 | while (n--) 41 | { 42 | #if defined(HAVE_POPCNT64_INTRINSICS) 43 | *(vc++) = _mm_popcnt_u64 (*(vv++)); 44 | #else /* HAVE_POPCNT32_INTRINSICS */ 45 | const guint32 *w = (const guint32 *)vv; 46 | *(vc++) = _mm_popcnt_u32(w[0]) + _mm_popcnt_u32(w[1]); 47 | vv++; 48 | #endif 49 | } 50 | } 51 | 52 | void 53 | chafa_hamming_distance_vu64_builtin (guint64 a, const guint64 *vb, gint *vc, gint n) 54 | { 55 | #if defined(HAVE_POPCNT64_INTRINSICS) 56 | while (n >= 4) 57 | { 58 | n -= 4; 59 | *(vc++) = _mm_popcnt_u64 (a ^ *(vb++)); 60 | *(vc++) = _mm_popcnt_u64 (a ^ *(vb++)); 61 | *(vc++) = _mm_popcnt_u64 (a ^ *(vb++)); 62 | *(vc++) = _mm_popcnt_u64 (a ^ *(vb++)); 63 | } 64 | 65 | while (n--) 66 | { 67 | *(vc++) = _mm_popcnt_u64 (a ^ *(vb++)); 68 | } 69 | #else /* HAVE_POPCNT32_INTRINSICS */ 70 | const guint32 *aa = (const guint32 *) &a; 71 | const guint32 *wb = (const guint32 *) vb; 72 | 73 | while (n--) 74 | { 75 | *(vc++) = _mm_popcnt_u32 (aa [0] ^ wb [0]) + _mm_popcnt_u32 (aa [1] ^ wb [1]); 76 | wb += 2; 77 | } 78 | #endif 79 | } 80 | 81 | /* Two bitmaps per item (a points to a pair, vb points to array of pairs) */ 82 | void 83 | chafa_hamming_distance_2_vu64_builtin (const guint64 *a, const guint64 *vb, gint *vc, gint n) 84 | { 85 | #if defined(HAVE_POPCNT64_INTRINSICS) 86 | while (n >= 4) 87 | { 88 | n -= 4; 89 | *vc = _mm_popcnt_u64 (a [0] ^ *(vb++)); 90 | *(vc++) += _mm_popcnt_u64 (a [1] ^ *(vb++)); 91 | *vc = _mm_popcnt_u64 (a [0] ^ *(vb++)); 92 | *(vc++) += _mm_popcnt_u64 (a [1] ^ *(vb++)); 93 | *vc = _mm_popcnt_u64 (a [0] ^ *(vb++)); 94 | *(vc++) += _mm_popcnt_u64 (a [1] ^ *(vb++)); 95 | *vc = _mm_popcnt_u64 (a [0] ^ *(vb++)); 96 | *(vc++) += _mm_popcnt_u64 (a [1] ^ *(vb++)); 97 | } 98 | 99 | while (n--) 100 | { 101 | *vc = _mm_popcnt_u64 (a [0] ^ *(vb++)); 102 | *(vc++) += _mm_popcnt_u64 (a [1] ^ *(vb++)); 103 | } 104 | #else /* HAVE_POPCNT32_INTRINSICS */ 105 | const guint32 *aa = (const guint32 *) a; 106 | const guint32 *wb = (const guint32 *) vb; 107 | 108 | while (n--) 109 | { 110 | *(vc++) = _mm_popcnt_u32 (aa [0] ^ wb [0]) + _mm_popcnt_u32 (aa [1] ^ wb [1]) 111 | + _mm_popcnt_u32 (aa [2] ^ wb [2]) + _mm_popcnt_u32 (aa [3] ^ wb [3]); 112 | wb += 4; 113 | } 114 | #endif 115 | } 116 | -------------------------------------------------------------------------------- /chafa/internal/chafa-sixel-canvas.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_SIXEL_CANVAS_H__ 21 | #define __CHAFA_SIXEL_CANVAS_H__ 22 | 23 | #include "chafa.h" 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef struct 28 | { 29 | gint width, height; 30 | ChafaColorSpace color_space; 31 | ChafaIndexedImage *image; 32 | } 33 | ChafaSixelCanvas; 34 | 35 | ChafaSixelCanvas *chafa_sixel_canvas_new (gint width, gint height, 36 | ChafaColorSpace color_space, 37 | const ChafaPalette *palette, 38 | const ChafaDither *dither); 39 | void chafa_sixel_canvas_destroy (ChafaSixelCanvas *sixel_canvas); 40 | 41 | void chafa_sixel_canvas_draw_all_pixels (ChafaSixelCanvas *sixel_canvas, ChafaPixelType src_pixel_type, 42 | gconstpointer src_pixels, 43 | gint src_width, gint src_height, gint src_rowstride, 44 | ChafaAlign halign, ChafaAlign valign, 45 | ChafaTuck tuck, 46 | gfloat quality); 47 | void chafa_sixel_canvas_build_ansi (ChafaSixelCanvas *sixel_canvas, ChafaTermInfo *term_info, 48 | GString *out_str, ChafaPassthrough passthrough); 49 | 50 | G_END_DECLS 51 | 52 | #endif /* __CHAFA_SIXEL_CANVAS_H__ */ 53 | -------------------------------------------------------------------------------- /chafa/internal/chafa-sse41.c: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #include "config.h" 21 | 22 | #include 23 | #include "chafa.h" 24 | #include "internal/chafa-private.h" 25 | 26 | gint 27 | calc_error_sse41 (const ChafaPixel *pixels, const ChafaColorPair *color_pair, const guint8 *cov) 28 | { 29 | const guint32 *u32p0 = (const guint32 *) pixels; 30 | guint32 cpair_u32 [2]; 31 | __m128i err = { 0 }; 32 | gint i; 33 | 34 | memcpy (&cpair_u32 [0], color_pair->colors [0].ch, sizeof (guint32)); 35 | memcpy (&cpair_u32 [1], color_pair->colors [1].ch, sizeof (guint32)); 36 | 37 | for (i = 0; i < CHAFA_SYMBOL_N_PIXELS; i++) 38 | { 39 | __m128i t0, t1, t; 40 | 41 | t0 = _mm_cvtepu8_epi32 (_mm_cvtsi32_si128 (u32p0 [i])); 42 | t1 = _mm_cvtepu8_epi32 (_mm_cvtsi32_si128 (cpair_u32 [cov [i]])); 43 | 44 | t = t0 - t1; 45 | t = _mm_mullo_epi32 (t, t); 46 | err += t; 47 | } 48 | 49 | /* FIXME: Perhaps not ideal */ 50 | return _mm_extract_epi32 (err, 0) + _mm_extract_epi32 (err, 1) 51 | + _mm_extract_epi32 (err, 2) + _mm_extract_epi32 (err, 3); 52 | } 53 | -------------------------------------------------------------------------------- /chafa/internal/chafa-string-util.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2021-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_STRING_UTIL_H__ 21 | #define __CHAFA_STRING_UTIL_H__ 22 | 23 | #include 24 | #include 25 | 26 | G_BEGIN_DECLS 27 | 28 | extern const char chafa_ascii_dec_u8 [256] [4]; 29 | 30 | /* Will overwrite 4 bytes starting at dest. Returns a pointer to the first 31 | * byte after the formatted ASCII decimal number (dest + 1..3). */ 32 | static inline gchar * 33 | chafa_format_dec_u8 (gchar *dest, guint8 n) 34 | { 35 | memcpy (dest, &chafa_ascii_dec_u8 [n] [0], 4); 36 | return dest + chafa_ascii_dec_u8 [n] [3]; 37 | } 38 | 39 | /* Will overwrite 4 bytes starting at dest. Returns a pointer to the first 40 | * byte after the formatted ASCII decimal number (dest + 1..4). */ 41 | gchar *chafa_format_dec_uint_0_to_9999 (char *dest, guint arg); 42 | 43 | /* Will overwrite 4 bytes starting at dest. Returns a pointer to the first 44 | * byte after the formatted ASCII hexadecimal number (dest + 4). */ 45 | gchar *chafa_format_dec_u16_hex (char *dest, guint16 arg); 46 | 47 | G_END_DECLS 48 | 49 | #endif /* __CHAFA_STRING_UTIL_H__ */ 50 | -------------------------------------------------------------------------------- /chafa/internal/chafa-work-cell.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_WORK_CELL_H__ 21 | #define __CHAFA_WORK_CELL_H__ 22 | 23 | G_BEGIN_DECLS 24 | 25 | typedef struct ChafaWorkCell ChafaWorkCell; 26 | 27 | struct ChafaWorkCell 28 | { 29 | ChafaPixel pixels [CHAFA_SYMBOL_N_PIXELS]; 30 | guint8 pixels_sorted_index [4] [CHAFA_SYMBOL_N_PIXELS]; 31 | guint8 have_pixels_sorted_by_channel [4]; 32 | gint dominant_channel; 33 | }; 34 | 35 | /* Currently unused */ 36 | typedef enum 37 | { 38 | CHAFA_PICK_CONSIDER_INVERTED = (1 << 0), 39 | CHAFA_PICK_HAVE_ALPHA = (1 << 1) 40 | } 41 | ChafaPickFlags; 42 | 43 | void chafa_work_cell_init (ChafaWorkCell *wcell, const ChafaPixel *src_image, 44 | gint src_width, gint cx, gint cy); 45 | 46 | void chafa_work_cell_get_mean_colors_for_symbol (const ChafaWorkCell *wcell, const ChafaSymbol *sym, 47 | ChafaColorPair *color_pair_out); 48 | void chafa_work_cell_get_median_colors_for_symbol (ChafaWorkCell *wcell, const ChafaSymbol *sym, 49 | ChafaColorPair *color_pair_out); 50 | void chafa_work_cell_get_contrasting_color_pair (ChafaWorkCell *wcell, ChafaColorPair *color_pair_out); 51 | void chafa_work_cell_calc_mean_color (const ChafaWorkCell *wcell, ChafaColor *color_out); 52 | guint64 chafa_work_cell_to_bitmap (const ChafaWorkCell *wcell, const ChafaColorPair *color_pair); 53 | 54 | G_END_DECLS 55 | 56 | #endif /* __CHAFA_WORK_CELL_H__ */ 57 | -------------------------------------------------------------------------------- /chafa/internal/smolscale/COPYING: -------------------------------------------------------------------------------- 1 | This software is Copyright © 2019 Hans Petter Jansson. All rights reserved. 2 | 3 | You may use and redistribute this software under the terms and conditions laid 4 | out in either the BSD 4-clause license (see COPYING.BSD-4) or the LGPL 5 | version 3 (see COPYING.LGPLv3). 6 | 7 | The practical intent is that you should be able to use the software in 8 | an LGPLv3-compatible project without giving credit (although it would be nice 9 | if you did so). However, if you're using it in a closed-source project, you 10 | must either publish it separately in an LGPLv3-licensed library and link 11 | with that, or credit the contributors in your advertising materials. 12 | 13 | If you want to use it under a different license, feel free to contact me by 14 | e-mail at . 15 | -------------------------------------------------------------------------------- /chafa/internal/smolscale/COPYING.BSD-4: -------------------------------------------------------------------------------- 1 | Copyright © 2019 Hans Petter Jansson. 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 | 7 | 1. Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in 12 | the documentation and/or other materials provided with the 13 | distribution. 14 | 15 | 3. Neither the name of the copyright holder nor the names of its 16 | contributors may be used to endorse or promote products derived 17 | from this software without specific prior written permission. 18 | 19 | 4. All advertising materials mentioning features or use of this 20 | software must display the following acknowledgement: 'This 21 | product includes software developed by Hans Petter Jansson 22 | (https://hpjansson.org/).' 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS 25 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 27 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 28 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 30 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 31 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 32 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 33 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 34 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | -------------------------------------------------------------------------------- /chafa/internal/smolscale/Makefile.am: -------------------------------------------------------------------------------- 1 | noinst_LTLIBRARIES = libsmolscale.la 2 | 3 | SMOLSCALE_CFLAGS = $(LIBCHAFA_CFLAGS) 4 | SMOLSCALE_LDFLAGS = $(LIBCHAFA_LDFLAGS) 5 | 6 | if HAVE_AVX2_INTRINSICS 7 | SMOLSCALE_CFLAGS += -DSMOL_WITH_AVX2 8 | endif 9 | 10 | libsmolscale_la_CFLAGS = $(SMOLSCALE_CFLAGS) 11 | libsmolscale_la_LDFLAGS = $(SMOLSCALE_LDFLAGS) 12 | libsmolscale_la_LIBADD = 13 | 14 | libsmolscale_la_SOURCES = \ 15 | smolscale.c \ 16 | smolscale.h \ 17 | smolscale-generic.c \ 18 | smolscale-private.h 19 | 20 | if HAVE_AVX2_INTRINSICS 21 | noinst_LTLIBRARIES += libsmolscale-avx2.la 22 | libsmolscale_la_LIBADD += libsmolscale-avx2.la 23 | libsmolscale_avx2_la_SOURCES = smolscale-avx2.c 24 | libsmolscale_avx2_la_CFLAGS = $(SMOLSCALE_CFLAGS) -mavx2 25 | libsmolscale_avx2_la_LDFLAGS = $(SMOLSCALE_LDFLAGS) 26 | endif 27 | -------------------------------------------------------------------------------- /chafa/internal/smolscale/README: -------------------------------------------------------------------------------- 1 | Smolscale 2 | ========= 3 | 4 | Smolscale is a smol piece of C code for quickly scaling images to a reasonable 5 | level of quality using CPU resources only (no GPU). It operates on 4-channel 6 | data with 32 bits per pixel, i.e. packed RGBA, ARGB, BGRA, etc. It supports 7 | both premultiplied and unassociated alpha and can convert between the two. It 8 | is host byte ordering agnostic. 9 | 10 | The design goals are: 11 | 12 | * High throughput: Optimized code, within reason. Easily parallelizable. 13 | 14 | * Decent quality: No "jaggies" as produced by nearest-neighbor scaling. 15 | 16 | * Low memory overhead: Mostly on the stack. 17 | 18 | * Simplicity: A modern C toolchain as the only dependency. 19 | 20 | * Ease of use: One-shot and row-batch APIs. 21 | 22 | Usage 23 | ----- 24 | 25 | First, read COPYING. If your project meets the requirements, you should be 26 | able to copy the following files into it and add it to your build with 27 | minimal fuss: 28 | 29 | smolscale.c 30 | smolscale.h 31 | smolscale-private.h 32 | 33 | If you want AVX2 SIMD support, optionally copy this additional file and 34 | compile everything with -DSMOL_WITH_AVX2: 35 | 36 | smolscale-avx2.c 37 | 38 | Keep in mind that this file is mostly just a straight copy of the generic 39 | code for the time being. Still, you will get a performance boost by building 40 | it with -mavx2 and letting Smolscale pick the implementation at runtime. 41 | 42 | The API documentation lives in smolscale.h along with the public declarations. 43 | -------------------------------------------------------------------------------- /docs/.gitignore: -------------------------------------------------------------------------------- 1 | /chafa-decl-list.txt 2 | /chafa-decl.txt 3 | /chafa-overrides.txt 4 | /chafa-undeclared.txt 5 | /chafa-undocumented.txt 6 | /chafa-unused.txt 7 | /chafa.1 8 | /chafa.actions 9 | /chafa.args 10 | /chafa.hierarchy 11 | /chafa.html 12 | /chafa.interfaces 13 | /chafa.prerequisites 14 | /chafa.proc 15 | /chafa.signals 16 | /docbook.xsl 17 | /html 18 | /refentry.xsl 19 | /version.xml 20 | /xml 21 | -------------------------------------------------------------------------------- /docs/Makefile.am: -------------------------------------------------------------------------------- 1 | ## Process this file with automake to produce Makefile.in 2 | 3 | AUTOMAKE_OPTIONS = 1.6 4 | 5 | # The name of the module. 6 | DOC_MODULE=chafa 7 | 8 | # The top-level SGML file. 9 | DOC_MAIN_SGML_FILE=chafa-docs.xml 10 | 11 | content_files = \ 12 | building.xml \ 13 | using.xml 14 | 15 | # The directory containing the source code. Relative to $(srcdir) 16 | DOC_SOURCE_DIR = $(top_srcdir)/chafa 17 | 18 | # Extra options to supply to gtkdoc-scan 19 | SCAN_OPTIONS=--deprecated-guards="G_DISABLE_DEPRECATED" --ignore-decorators="GLIB_VAR|G_GNUC_WARN_UNUSED_RESULT" 20 | 21 | # Used for dependencies 22 | HFILE_GLOB = $(top_srcdir)/chafa/chafa-*.h 23 | CFILE_GLOB = $(top_srcdir)/chafa/chafa-*.c 24 | 25 | # Ignore some private headers 26 | IGNORE_HFILES = \ 27 | chafa.h \ 28 | chafa-private.h \ 29 | chafa-term-seq-doc-in.h \ 30 | named-colors.h 31 | 32 | # Extra options to supply to gtkdoc-mkdb 33 | MKDB_OPTIONS=--output-format=xml --name-space=chafa 34 | 35 | FIXXREF_OPTIONS=--extra-dir=$(GLIB_PREFIX)/share/gtk-doc/html/glib 36 | 37 | # Images to copy into HTML directory 38 | HTML_IMAGES = 39 | 40 | # include common portion ... 41 | include $(top_srcdir)/gtk-doc.make 42 | 43 | # Other files to distribute 44 | EXTRA_DIST += \ 45 | version.xml.in \ 46 | style.css 47 | 48 | ######################################################################## 49 | 50 | man_MANS = 51 | manhtml = 52 | 53 | if ENABLE_MAN 54 | 55 | man_MANS += \ 56 | chafa.1 57 | 58 | manhtml += chafa.html 59 | 60 | all-local: $(manhtml) 61 | 62 | XSLTPROC_FLAGS = \ 63 | --nonet \ 64 | --stringparam man.output.quietly 1 \ 65 | --stringparam funcsynopsis.style ansi \ 66 | --stringparam man.th.extra1.suppress 1 \ 67 | --stringparam man.authors.section.enabled 0 \ 68 | --stringparam man.copyright.section.enabled 0 \ 69 | --stringparam root.filename chafa \ 70 | --stringparam html.stylesheet manpage.css 71 | 72 | .xml.1: 73 | $(AM_V_GEN) $(XSLTPROC) $(XSLTPROC_FLAGS) http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $< 74 | 75 | chafa.html: $(srcdir)/chafa.xml 76 | $(AM_V_GEN) $(XSLTPROC) $(XSLTPROC_FLAGS) http://docbook.sourceforge.net/release/xsl/current/html/onechunk.xsl $< 77 | 78 | dist-local-check-mans-enabled: 79 | if grep "Man generation disabled" $(man_MANS) >/dev/null; then $(RM) $(man_MANS); fi 80 | 81 | else 82 | 83 | $(man_MANS) chafa.html: 84 | echo Man generation disabled. Creating dummy $@. Configure with --enable-man to enable it. 85 | echo Man generation disabled. Remove this file, configure with --enable-man, and rebuild > $@ 86 | 87 | 88 | dist-local-check-mans-enabled: 89 | echo "*** --enable-man must be used in order to make dist" 90 | false 91 | 92 | endif 93 | 94 | CLEANFILES ?= 95 | CLEANFILES += $(man_MANS) $(manhtml) 96 | 97 | EXTRA_DIST += chafa.xml $(man_MANS) $(manhtml) manpage.css 98 | 99 | dist-hook-local: dist-local-check-mans-enabled all-local 100 | 101 | chafa-docs-clean: clean 102 | cd $(srcdir) && rm -rf xml html 103 | 104 | -------------------------------------------------------------------------------- /docs/chafa-docs.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | %gtkdocentities; 9 | ]> 10 | 11 | 12 | Chafa Reference Manual 13 | 14 | For Chafa version &version;. 15 | The latest version of this documentation can be found online at 16 | https://hpjansson.org/&package_name;/ref/. 17 | 18 | 19 | 20 | 21 | Overview 22 | 23 | 24 | 25 | 26 | 27 | Chafa C API 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | API Index 42 | 43 | 44 | 45 | 46 | Index of new API in version 1.2 47 | 48 | 49 | 50 | 51 | Index of new API in version 1.4 52 | 53 | 54 | 55 | 56 | Index of new API in version 1.6 57 | 58 | 59 | 60 | 61 | Index of new API in version 1.8 62 | 63 | 64 | 65 | 66 | Index of new API in version 1.10 67 | 68 | 69 | 70 | 71 | Index of new API in version 1.12 72 | 73 | 74 | 75 | 76 | Index of new API in version 1.14 77 | 78 | 79 | 80 | 81 | Index of new API in version 1.16 82 | 83 | 84 | 85 | 86 | Index of deprecated API 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /docs/chafa-logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/docs/chafa-logo.gif -------------------------------------------------------------------------------- /docs/version.xml.in: -------------------------------------------------------------------------------- 1 | @CHAFA_VERSION@ 2 | -------------------------------------------------------------------------------- /examples/Makefile.am: -------------------------------------------------------------------------------- 1 | EXTRA_DIST = \ 2 | adaptive.c \ 3 | example.c \ 4 | ncurses.c 5 | -------------------------------------------------------------------------------- /examples/example.c: -------------------------------------------------------------------------------- 1 | /* This file is in the public domain, and you are free to use it as you see fit. 2 | * 3 | * Compile with: 4 | * 5 | * cc example.c -o example $(pkg-config --libs --cflags chafa) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | #define PIX_WIDTH 3 12 | #define PIX_HEIGHT 3 13 | #define N_CHANNELS 4 14 | 15 | int 16 | main (int argc, char *argv []) 17 | { 18 | const guint8 pixels [PIX_WIDTH * PIX_HEIGHT * N_CHANNELS] = 19 | { 20 | 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 21 | 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 22 | 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff 23 | }; 24 | ChafaSymbolMap *symbol_map; 25 | ChafaCanvasConfig *config; 26 | ChafaCanvas *canvas; 27 | GString *gs; 28 | 29 | /* Specify the symbols we want */ 30 | symbol_map = chafa_symbol_map_new (); 31 | chafa_symbol_map_add_by_tags (symbol_map, CHAFA_SYMBOL_TAG_ALL); 32 | 33 | /* Set up a configuration with the symbols and the canvas size in characters */ 34 | config = chafa_canvas_config_new (); 35 | chafa_canvas_config_set_geometry (config, 40, 20); 36 | chafa_canvas_config_set_symbol_map (config, symbol_map); 37 | 38 | /* Create canvas */ 39 | canvas = chafa_canvas_new (config); 40 | 41 | /* Draw pixels to canvas */ 42 | chafa_canvas_draw_all_pixels (canvas, 43 | CHAFA_PIXEL_RGBA8_UNASSOCIATED, 44 | pixels, 45 | PIX_WIDTH, 46 | PIX_HEIGHT, 47 | PIX_WIDTH * N_CHANNELS); 48 | 49 | /* Generate a string that will show the canvas contents on a terminal */ 50 | gs = chafa_canvas_print (canvas, NULL); 51 | 52 | /* Print the string */ 53 | fwrite (gs->str, sizeof (char), gs->len, stdout); 54 | fputc ('\n', stdout); 55 | 56 | /* Free resources */ 57 | g_string_free (gs, TRUE); 58 | chafa_canvas_unref (canvas); 59 | chafa_canvas_config_unref (config); 60 | chafa_symbol_map_unref (symbol_map); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /libnsgif/COPYING: -------------------------------------------------------------------------------- 1 | Copyright (C) 2004 Richard Wilson 2 | Copyright (C) 2008 Sean Fox 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | * The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /libnsgif/Makefile.am: -------------------------------------------------------------------------------- 1 | noinst_LTLIBRARIES = libnsgif.la 2 | 3 | libnsgif_la_SOURCES = libnsgif.c libnsgif.h lzw.c lzw.h log.h 4 | libnsgif_la_CFLAGS = ${LIBNSGIF_CFLAGS} -I${top_srcdir} 5 | libnsgif_la_LDFLAGS = ${LIBNSGIF_LDFLAGS} 6 | 7 | EXTRA_DIST = COPYING README README-chafa 8 | -------------------------------------------------------------------------------- /libnsgif/README: -------------------------------------------------------------------------------- 1 | libnsgif - Decoding GIF files 2 | ============================= 3 | 4 | The functions provided by this library allow for efficient progressive 5 | GIF decoding. Whilst the initialisation does not ensure that there is 6 | sufficient image data to complete the entire frame, it does ensure 7 | that the information provided is valid. Any subsequent attempts to 8 | decode an initialised GIF are guaranteed to succeed, and any bytes of 9 | the image not present are assumed to be totally transparent. 10 | 11 | To begin decoding a GIF, the 'gif' structure must be initialised with 12 | the 'gif_data' and 'buffer_size' set to their initial values. The 13 | 'buffer_position' should initially be 0, and will be internally 14 | updated as the decoding commences. The caller should then repeatedly 15 | call gif_initialise() with the structure until the function returns 1, 16 | or no more data is avaliable. 17 | 18 | Once the initialisation has begun, the decoder completes the variables 19 | 'frame_count' and 'frame_count_partial'. The former being the total 20 | number of frames that have been successfully initialised, and the 21 | latter being the number of frames that a partial amount of data is 22 | available for. This assists the caller in managing the animation 23 | whilst decoding is continuing. 24 | 25 | To decode a frame, the caller must use gif_decode_frame() which 26 | updates the current 'frame_image' to reflect the desired frame. The 27 | required 'disposal_method' is also updated to reflect how the frame 28 | should be plotted. The caller must not assume that the current 29 | 'frame_image' will be valid between calls if initialisation is still 30 | occuring, and should either always request that the frame is decoded 31 | (no processing will occur if the 'decoded_frame' has not been 32 | invalidated by initialisation) or perform the check itself. 33 | 34 | It should be noted that gif_finalise() should always be called, even 35 | if no frames were initialised. Additionally, it is the responsibility 36 | of the caller to free 'gif_data'. 37 | -------------------------------------------------------------------------------- /libnsgif/README-chafa: -------------------------------------------------------------------------------- 1 | Notes on libnsgif as bundled with Chafa 2 | ======================================= 3 | 4 | The libnsgif bundled with Chafa is currently based on version 0.2.1. The 5 | original source is here: 6 | 7 | http://www.netsurf-browser.org/projects/libnsgif/ 8 | 9 | It has been slightly adapted from the original to make it fit into the Chafa 10 | build. It may have further modifications to make it more convenient to use 11 | in this context. 12 | 13 | It is my intention to submit any generally useful enhancements made for 14 | potential inclusion upstream. 15 | -------------------------------------------------------------------------------- /libnsgif/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2003 James Bursa 3 | * Copyright 2004 John Tytgat 4 | * 5 | * This file is part of NetSurf, http://www.netsurf-browser.org/ 6 | * Licenced under the MIT License, 7 | * http://www.opensource.org/licenses/mit-license.php 8 | */ 9 | 10 | #include 11 | 12 | #ifndef _LIBNSGIF_LOG_H_ 13 | #define _LIBNSGIF_LOG_H_ 14 | 15 | #ifdef NDEBUG 16 | # define LOG(x) ((void) 0) 17 | #else 18 | # define LOG(x) do { fprintf(stderr, x), fputc('\n', stderr); } while (0) 19 | #endif /* NDEBUG */ 20 | 21 | #endif /* _LIBNSGIF_LOG_H_ */ 22 | -------------------------------------------------------------------------------- /libnsgif/lzw.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of NetSurf's LibNSGIF, http://www.netsurf-browser.org/ 3 | * Licensed under the MIT License, 4 | * http://www.opensource.org/licenses/mit-license.php 5 | * 6 | * Copyright 2017 Michael Drake 7 | */ 8 | 9 | #ifndef LZW_H_ 10 | #define LZW_H_ 11 | 12 | /** 13 | * \file 14 | * \brief LZW decompression (interface) 15 | * 16 | * Decoder for GIF LZW data. 17 | */ 18 | 19 | 20 | /** Maximum LZW code size in bits */ 21 | #define LZW_CODE_MAX 12 22 | 23 | 24 | /* Declare lzw internal context structure */ 25 | struct lzw_ctx; 26 | 27 | 28 | /** LZW decoding response codes */ 29 | typedef enum lzw_result { 30 | LZW_OK, /**< Success */ 31 | LZW_OK_EOD, /**< Success; reached zero-length sub-block */ 32 | LZW_NO_MEM, /**< Error: Out of memory */ 33 | LZW_NO_DATA, /**< Error: Out of data */ 34 | LZW_EOI_CODE, /**< Error: End of Information code */ 35 | LZW_BAD_ICODE, /**< Error: Bad initial LZW code */ 36 | LZW_BAD_CODE, /**< Error: Bad LZW code */ 37 | } lzw_result; 38 | 39 | 40 | /** 41 | * Create an LZW decompression context. 42 | * 43 | * \param[out] ctx Returns an LZW decompression context. Caller owned, 44 | * free with lzw_context_destroy(). 45 | * \return LZW_OK on success, or appropriate error code otherwise. 46 | */ 47 | lzw_result lzw_context_create( 48 | struct lzw_ctx **ctx); 49 | 50 | /** 51 | * Destroy an LZW decompression context. 52 | * 53 | * \param[in] ctx The LZW decompression context to destroy. 54 | */ 55 | void lzw_context_destroy( 56 | struct lzw_ctx *ctx); 57 | 58 | /** 59 | * Initialise an LZW decompression context for decoding. 60 | * 61 | * Caller owns neither `stack_base_out` or `stack_pos_out`. 62 | * 63 | * \param[in] ctx The LZW decompression context to initialise. 64 | * \param[in] compressed_data The compressed data. 65 | * \param[in] compressed_data_len Byte length of compressed data. 66 | * \param[in] compressed_data_pos Start position in data. Must be position 67 | * of a size byte at sub-block start. 68 | * \param[in] code_size The initial LZW code size to use. 69 | * \param[out] stack_base_out Returns base of decompressed data stack. 70 | * \param[out] stack_pos_out Returns current stack position. 71 | * There are `stack_pos_out - stack_base_out` 72 | * current stack entries. 73 | * \return LZW_OK on success, or appropriate error code otherwise. 74 | */ 75 | lzw_result lzw_decode_init( 76 | struct lzw_ctx *ctx, 77 | const uint8_t *compressed_data, 78 | uint64_t compressed_data_len, 79 | uint64_t compressed_data_pos, 80 | uint8_t code_size, 81 | const uint8_t ** const stack_base_out, 82 | const uint8_t ** const stack_pos_out); 83 | 84 | /** 85 | * Fill the LZW stack with decompressed data 86 | * 87 | * Ensure anything on the stack is used before calling this, as anything 88 | * on the stack before this call will be trampled. 89 | * 90 | * Caller does not own `stack_pos_out`. 91 | * 92 | * \param[in] ctx LZW reading context, updated. 93 | * \param[out] stack_pos_out Returns current stack position. 94 | * Use with `stack_base_out` value from previous 95 | * lzw_decode_init() call. 96 | * There are `stack_pos_out - stack_base_out` 97 | * current stack entries. 98 | * \return LZW_OK on success, or appropriate error code otherwise. 99 | */ 100 | lzw_result lzw_decode( 101 | struct lzw_ctx *ctx, 102 | const uint8_t ** const stack_pos_out); 103 | 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /lodepng/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2005-2018 Lode Vandevenne 2 | 3 | This software is provided 'as-is', without any express or implied 4 | warranty. In no event will the authors be held liable for any damages 5 | arising from the use of this software. 6 | 7 | Permission is granted to anyone to use this software for any purpose, 8 | including commercial applications, and to alter it and redistribute it 9 | freely, subject to the following restrictions: 10 | 11 | 1. The origin of this software must not be misrepresented; you must not 12 | claim that you wrote the original software. If you use this software 13 | in a product, an acknowledgment in the product documentation would be 14 | appreciated but is not required. 15 | 16 | 2. Altered source versions must be plainly marked as such, and must not be 17 | misrepresented as being the original software. 18 | 19 | 3. This notice may not be removed or altered from any source 20 | distribution. 21 | 22 | -------------------------------------------------------------------------------- /lodepng/Makefile.am: -------------------------------------------------------------------------------- 1 | noinst_LTLIBRARIES = liblodepng.la 2 | 3 | liblodepng_la_SOURCES = lodepng.c lodepng.h 4 | liblodepng_la_CFLAGS = ${LODEPNG_CFLAGS} -I${top_srcdir} 5 | liblodepng_la_LDFLAGS = ${LODEPNG_LDFLAGS} 6 | 7 | EXTRA_DIST = LICENSE README.md 8 | -------------------------------------------------------------------------------- /lodepng/README.md: -------------------------------------------------------------------------------- 1 | LodePNG 2 | ------- 3 | 4 | PNG encoder and decoder in C and C++, without dependencies 5 | 6 | Home page: http://lodev.org/lodepng/ 7 | 8 | ### Documentation 9 | 10 | Detailed documentation is included in a large comment in the second half of the 11 | header file `lodepng.h`. 12 | 13 | Source code examples using LodePNG can be found in the examples directory. 14 | 15 | An FAQ can be found on http://lodev.org/lodepng/ 16 | 17 | ### Building 18 | 19 | Only two files are needed to encode and decode PNGs: 20 | 21 | * `lodepng.cpp` (or renamed to `lodepng.c`) 22 | * `lodepng.h` 23 | 24 | All other files are just source code examples, tests, misc utilities, etc..., 25 | which are normally not needed in projects using this. 26 | 27 | You can include the files directly in your project's source tree and its 28 | makefile, IDE project file, or other build system. No library is necessary. 29 | 30 | In addition to C++, LodePNG also supports ANSI C (C89), with all the same 31 | functionality: C++ only adds extra convenience API. 32 | 33 | For C, rename `lodepng.cpp` to `lodepng.c`. 34 | 35 | Consider using git submodules to include LodePNG in your project. 36 | 37 | ### Compiling in C++ 38 | 39 | If you have a hypothetical `your_program.cpp` that #includes and uses `lodepng.h`, 40 | you can build as follows: 41 | 42 | `g++ your_program.cpp lodepng.cpp -Wall -Wextra -pedantic -ansi -O3` 43 | 44 | or: 45 | 46 | `clang++ your_program.cpp lodepng.cpp -Wall -Wextra -pedantic -ansi -O3` 47 | 48 | This shows compiler flags it was designed for, but normally one would use the 49 | compiler or build system of their project instead of those commands, and other 50 | C++ compilers are supported. 51 | 52 | ### Compiling in C 53 | 54 | Rename `lodepng.cpp` to `lodepng.c` for this. 55 | 56 | If you have a hypothetical your_program.c that #includes and uses lodepng.h, 57 | you can build as follows: 58 | 59 | `gcc your_program.c lodepng.c -ansi -pedantic -Wall -Wextra -O3` 60 | 61 | or 62 | 63 | `clang your_program.c lodepng.c -ansi -pedantic -Wall -Wextra -O3` 64 | 65 | This shows compiler flags it was designed for, but normally one would use the 66 | compiler or build system of their project instead of those commands, and other 67 | C compilers are supported. 68 | 69 | ### Makefile 70 | 71 | There is a Makefile, but this is not intended for using LodePNG itself since the 72 | way to use that one is to include its source files in your program. The Makefile 73 | only builds development and testing utilities. It can be used as follows: 74 | 75 | `make -j` 76 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | /*.log 2 | /*.trs 3 | /canvas-test 4 | /term-info-test 5 | -------------------------------------------------------------------------------- /tests/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = data 2 | 3 | AM_CFLAGS = $(CHAFA_CFLAGS) $(GLIB_CFLAGS) 4 | LDADD = $(GLIB_LIBS) $(top_builddir)/chafa/libchafa.la 5 | 6 | ## --- Test driver --- 7 | 8 | # Our custom test driver is based on Automake's, with the addition that 9 | # it will print logs of failed tests to the console. This simplifies log 10 | # perusal in build systems. Requires Automake 1.12+. 11 | 12 | LOG_DRIVER = $(top_srcdir)/tests/test-driver.sh 13 | 14 | ## --- Backend tests --- 15 | 16 | noinst_PROGRAMS = \ 17 | canvas-test \ 18 | term-info-test 19 | 20 | canvas_test_SOURCES = \ 21 | canvas-test.c 22 | 23 | term_info_test_SOURCES = \ 24 | term-info-test.c 25 | 26 | ## --- Frontend tests --- 27 | 28 | if WANT_TOOLS 29 | TOOL_CHECKS = \ 30 | chafa-tool-bad-test.sh \ 31 | chafa-tool-cmode-test.sh \ 32 | chafa-tool-format-test.sh \ 33 | chafa-tool-grid-test.sh \ 34 | chafa-tool-loader-test.sh \ 35 | chafa-tool-options-test.sh \ 36 | chafa-tool-pipe-test.sh 37 | else 38 | TOOL_CHECKS = 39 | endif 40 | 41 | TESTS = \ 42 | canvas-test \ 43 | term-info-test \ 44 | $(TOOL_CHECKS) 45 | 46 | AM_TESTS_ENVIRONMENT = \ 47 | export top_builddir=$(top_builddir) \ 48 | export top_srcdir=$(top_srcdir) \ 49 | ; 50 | 51 | ## --- General --- 52 | 53 | ## Include $(top_builddir)/chafa to get generated chafaconfig.h. 54 | 55 | AM_CPPFLAGS = \ 56 | -I$(top_srcdir)/chafa \ 57 | -I$(top_builddir)/chafa 58 | 59 | EXTRA_DIST = \ 60 | $(TOOL_CHECKS) \ 61 | chafa-tool-gallery.sh \ 62 | chafa-tool-test-common.sh \ 63 | test-driver.sh 64 | -------------------------------------------------------------------------------- /tests/canvas-test.c: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | 3 | #include 4 | #include 5 | 6 | static void 7 | symbols_fgbg_test_canvas (ChafaCanvas *canvas) 8 | { 9 | const guint8 black_pixel [4] = 10 | { 11 | 0x00, 0x00, 0x00, 0xff 12 | }; 13 | const guint8 white_pixel [4] = 14 | { 15 | 0xff, 0xff, 0xff, 0xff 16 | }; 17 | gint i, j; 18 | 19 | chafa_canvas_draw_all_pixels (canvas, 20 | CHAFA_PIXEL_RGBA8_UNASSOCIATED, 21 | black_pixel, 22 | 1, 1, 4); 23 | for (i = 0; i < 100; i++) 24 | { 25 | for (j = 0; j < 100; j++) 26 | { 27 | gunichar c = chafa_canvas_get_char_at (canvas, j, i); 28 | g_assert (c == ' '); 29 | } 30 | } 31 | 32 | chafa_canvas_draw_all_pixels (canvas, 33 | CHAFA_PIXEL_RGBA8_UNASSOCIATED, 34 | white_pixel, 35 | 1, 1, 4); 36 | for (i = 0; i < 100; i++) 37 | { 38 | for (j = 0; j < 100; j++) 39 | { 40 | gunichar c = chafa_canvas_get_char_at (canvas, j, i); 41 | g_assert (c == 'a'); 42 | } 43 | } 44 | } 45 | 46 | static void 47 | symbols_fgbg_test (void) 48 | { 49 | ChafaSymbolMap *symbol_map; 50 | ChafaCanvasConfig *config; 51 | ChafaCanvas *canvas, *canvas2; 52 | 53 | symbol_map = chafa_symbol_map_new (); 54 | chafa_symbol_map_apply_selectors (symbol_map, "[ a]", NULL); 55 | 56 | config = chafa_canvas_config_new (); 57 | chafa_canvas_config_set_canvas_mode (config, CHAFA_CANVAS_MODE_FGBG_BGFG); 58 | chafa_canvas_config_set_symbol_map (config, symbol_map); 59 | chafa_canvas_config_set_geometry (config, 100, 100); 60 | chafa_canvas_config_set_fg_only_enabled (config, TRUE); 61 | 62 | canvas = chafa_canvas_new (config); 63 | symbols_fgbg_test_canvas (canvas); 64 | 65 | canvas2 = chafa_canvas_new_similar (canvas); 66 | chafa_canvas_unref (canvas); 67 | symbols_fgbg_test_canvas (canvas2); 68 | chafa_canvas_unref (canvas2); 69 | 70 | chafa_canvas_config_unref (config); 71 | chafa_symbol_map_unref (symbol_map); 72 | } 73 | 74 | int 75 | main (int argc, char *argv []) 76 | { 77 | g_test_init (&argc, &argv, NULL); 78 | 79 | g_test_add_func ("/canvas/symbols/fgbg", symbols_fgbg_test); 80 | 81 | return g_test_run (); 82 | } 83 | -------------------------------------------------------------------------------- /tests/chafa-tool-bad-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | [ "x${srcdir}" = "x" ] && srcdir="." 4 | . "${srcdir}/chafa-tool-test-common.sh" 5 | 6 | for format in $formats; do 7 | for size in $sizes; do 8 | run_cmd_all_bad_files "$tool -f $format -c 16 --dither fs -s $size --threads 12 -d 0 --speed max" || exit $? 9 | done 10 | done 11 | -------------------------------------------------------------------------------- /tests/chafa-tool-cmode-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | [ "x${srcdir}" = "x" ] && srcdir="." 4 | . "${srcdir}/chafa-tool-test-common.sh" 5 | 6 | for cmode in $cmodes; do 7 | for size in $sizes; do 8 | for n_threads in $thread_counts; do 9 | run_cmd "$tool -f symbol -c $cmode -s $size --threads $n_threads --animate no" || exit $? 10 | done 11 | done 12 | done 13 | -------------------------------------------------------------------------------- /tests/chafa-tool-format-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | [ "x${srcdir}" = "x" ] && srcdir="." 4 | . "${srcdir}/chafa-tool-test-common.sh" 5 | 6 | for format in $formats; do 7 | for size in $sizes; do 8 | for n_threads in $thread_counts; do 9 | run_cmd_all_safe_files "$tool -f $format -c full -s $size --threads $n_threads -d 0 --speed max" || exit $? 10 | done 11 | done 12 | done 13 | -------------------------------------------------------------------------------- /tests/chafa-tool-gallery.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | [ "x${srcdir}" = "x" ] && srcdir="." 4 | . "${srcdir}/chafa-tool-test-common.sh" 5 | 6 | tempdir="${top_builddir}/tests/.temp" 7 | 8 | xvfb=$(which Xvfb) 9 | [ "x${xvfb}" = "x" ] && echo "Missing Xvfb" && exit 1 10 | 11 | gterm_name=xfce4-terminal 12 | gterm=$(which ${gterm_name}) 13 | [ "x${gterm}" = "x" ] && echo "Missing ${gterm_name}" && exit 1 14 | 15 | montage=$(which montage) 16 | [ "x${montage}" = "x" ] && echo "Missing montage" && exit 1 17 | 18 | # Terminal parameters 19 | font='xos4 Terminus 8' 20 | geometry=60x40 21 | screen_size=1280x1024 22 | 23 | # Chafa parameter lists 24 | color_modes="tc 240 16 8 2 none" 25 | color_spaces="rgb din99d" 26 | color_extractors="average median" 27 | dither_types="none ordered diffusion" 28 | symbol_selectors="+0 all wide" 29 | work_factors="1 5 9" 30 | 31 | # Input filename. Must be in data/good/. 32 | file="taxic.jpg" 33 | 34 | function gen_screenshot 35 | { 36 | FNAME="$1" 37 | 38 | echo -- Running -e "bash -c \"${tool} -s ${geometry} -w ${5} -c ${2} --symbols ${3} --color-space ${4} \ 39 | ${top_srcdir}/tests/data/good/${FNAME} ; sleep 0.5 ; import -window chafa out/out-${2}-${3}-${5}-${4}.png -trim\"" 40 | 41 | HOME="${tempdir}" XDG_CONFIG_HOME="${HOME}/.config" DISPLAY=:99 ${gterm} \ 42 | -T chafa \ 43 | --disable-server \ 44 | --geometry ${geometry}+0+0 \ 45 | --font "$font" \ 46 | --hide-menubar \ 47 | --hide-toolbar \ 48 | --hide-borders \ 49 | --hide-scrollbar \ 50 | -e "bash -c \"${tool} -s ${geometry} -w ${5} -c ${2} --symbols ${3} --color-space ${4} \ 51 | ${top_srcdir}/tests/data/good/${FNAME}; sleep 0.5 ; import -window chafa ${tempdir}/out/${2}-${3}-${5}-${4}.png -trim >/dev/null 2>&1\"" >/dev/null 2>&1 52 | } 53 | 54 | trap "" EXIT SIGQUIT SIGSTOP SIGTERM 55 | 56 | # Make sure we don't blow away any old .temp dir 57 | rm -Rf "${tempdir}/.config" 58 | rm -Rf "${tempdir}/out" 59 | rmdir "${tempdir}" 60 | 61 | mkdir -p "${tempdir}/out" 62 | 63 | mkdir -p ${tempdir}/.config/xfce4/terminal 64 | cat >${tempdir}/.config/xfce4/terminal/terminalrc </dev/null 2>&1 & 100 | XVFB_PID=$! 101 | 102 | sleep 2 103 | 104 | for cmode in ${color_modes}; do 105 | for symbols in ${symbol_selectors}; do 106 | for cspace in ${color_spaces}; do 107 | for work in ${work_factors}; do 108 | gen_screenshot "$file" "$cmode" "$symbols" "$cspace" "$work" 109 | done 110 | done 111 | done 112 | done 113 | 114 | kill ${XVFB_PID} 115 | 116 | rm -f "${top_builddir}/tests/chafa-tool-gallery.png" 117 | ${montage} -stroke white -background grey20 -geometry +4+4 -tile 6 -label '%f' \ 118 | "${tempdir}/out/*.png" \ 119 | "${top_builddir}/tests/chafa-tool-gallery.png" 120 | -------------------------------------------------------------------------------- /tests/chafa-tool-grid-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | [ "x${srcdir}" = "x" ] && srcdir="." 4 | . "${srcdir}/chafa-tool-test-common.sh" 5 | 6 | grid_sizes="1 4 x3 5x" 7 | 8 | for format in $formats; do 9 | for size in $sizes; do 10 | for grid_size in $grid_sizes; do 11 | run_cmd_all_safe_files "$tool -f $format -c full -s $size --grid $grid_size --threads 12" || exit $? 12 | done 13 | done 14 | done 15 | -------------------------------------------------------------------------------- /tests/chafa-tool-loader-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | [ "x${srcdir}" = "x" ] && srcdir="." 4 | . "${srcdir}/chafa-tool-test-common.sh" 5 | 6 | extensions="$(get_supported_loaders)" 7 | 8 | for ext in $extensions; do 9 | [ "x${ext}" = "ximagemagick" ] && continue 10 | run_cmd_single_file "$tool -f sixel --threads 12 --animate no" "good/pixel.$ext" || exit $? 11 | done 12 | -------------------------------------------------------------------------------- /tests/chafa-tool-options-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | [ "x${srcdir}" = "x" ] && srcdir="." 4 | . "${srcdir}/chafa-tool-test-common.sh" 5 | 6 | color_spaces="rgb din99d" 7 | color_extractors="average median" 8 | dither_types="none ordered diffusion" 9 | symbol_selectors="none all wide 0..1ffff" 10 | 11 | for format in $formats; do 12 | for color_space in $color_spaces; do 13 | for color_extractor in $color_extractors; do 14 | for dither_type in $dither_types; do 15 | for main_symbols in $symbol_selectors; do 16 | for fill_symbols in $symbol_selectors; do 17 | run_cmd "$tool -f $format -c 240 -s 53 --threads 12 --animate no --color-space $color_space \ 18 | --color-extractor $color_extractor --dither $dither_type --symbols $main_symbols --fill $fill_symbols" || exit $? 19 | done 20 | done 21 | done 22 | done 23 | done 24 | done 25 | -------------------------------------------------------------------------------- /tests/chafa-tool-pipe-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | [ "x${srcdir}" = "x" ] && srcdir="." 4 | . "${srcdir}/chafa-tool-test-common.sh" 5 | 6 | run_cmd "$tool -f symbol -c full -s 63 --threads 12 --animate no < " || exit $? 7 | -------------------------------------------------------------------------------- /tests/chafa-tool-test-common.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | run_cmd () { 4 | cmd="$1 ${top_srcdir}/tests/data/good/card-32c-noalpha.png >/dev/null" 5 | echo "$cmd" >&2 6 | sh -c "$cmd" || exit $? 7 | } 8 | 9 | run_cmd_single_file () { 10 | file="$2" 11 | cmd="$1 ${top_srcdir}/tests/data/$file >/dev/null" 12 | echo "$cmd" >&2 13 | sh -c "$cmd" || exit $? 14 | } 15 | 16 | run_cmd_all_safe_files () { 17 | # Only run on files for which we're guaranteed to have loaders. 18 | # '$dir/*.{gif,png,xwd}' is a Bash-ism, so we can't use it. 19 | dir="${top_srcdir}/tests/data/good" 20 | cmd="$1 $dir/*.gif $dir/*.png $dir/*.xwd >/dev/null" 21 | echo "$cmd" >&2 22 | sh -c "$cmd" || exit $? 23 | } 24 | 25 | run_cmd_all_bad_files () { 26 | # Since this only fails on crashes, it's fine to run it on absolutely 27 | # everything (including unsupported formats, build files etc). 28 | cmd="$1 ${top_srcdir}/tests/data/bad/* >/dev/null" 29 | echo "$cmd" >&2 30 | sh -c "$cmd" 31 | result=$? 32 | 33 | # For corrupt files, an exit value of 1 or 2 is a fine result, 34 | # but other values are trouble (e.g. terminated by signal). 35 | if test $result -gt 2; then exit $result; fi 36 | } 37 | 38 | get_supported_loaders () { 39 | sh -c "$tool --version" \ 40 | | grep '^Loaders: ' \ 41 | | sed 's/[^:]*: *\(.*\)/\1/' \ 42 | | tr [:upper:] [:lower:] 43 | } 44 | 45 | [ "x${top_srcdir}" = "x" ] && top_srcdir="${srcdir}/.." 46 | [ "x${top_builddir}" = "x" ] && top_builddir=".." 47 | [ "x$1" = "xlong" ] && long="yes" 48 | 49 | tool="${top_builddir}/tools/chafa/chafa" 50 | 51 | if [ "x$long" = "xyes" ]; then 52 | formats="symbol sixel kitty iterm" 53 | cmodes="none 2 8 16/8 16 240 256 full" 54 | sizes="$(seq 1 100)" 55 | thread_counts="$(seq 1 32) 61" 56 | else 57 | # The 13x13 size is specifically required to trigger the normalization 58 | # overflow (see commit 60d7718f9d8fa591d3d69079fe583913c58c19d9). 59 | 60 | formats="symbol sixel kitty iterm" 61 | cmodes="none 2 16/8 16 256 full" 62 | sizes="1 3 13x13 133" 63 | thread_counts="1 2 3 12 32 61" 64 | fi 65 | -------------------------------------------------------------------------------- /tests/data/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = bad good 2 | -------------------------------------------------------------------------------- /tests/data/bad/Makefile.am: -------------------------------------------------------------------------------- 1 | EXTRA_DIST = \ 2 | libnsgif-lzw-oob.gif \ 3 | libnsgif-uninitialized-frame-fields.gif \ 4 | lodepng-adam7-mystery-over-read.png \ 5 | lodepng-zero-length-literal.png \ 6 | lodepng-zlib-big-alloc.png \ 7 | no-frame-data.gif \ 8 | pixops-normalize-signed-overflow.gif \ 9 | smolscale-unaligned-1.xwd \ 10 | smolscale-unaligned-2.xwd \ 11 | smolscale-unaligned-3.xwd \ 12 | smolscale-unaligned-4.xwd 13 | -------------------------------------------------------------------------------- /tests/data/bad/libavif-inf-frame-duration.avif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/libavif-inf-frame-duration.avif -------------------------------------------------------------------------------- /tests/data/bad/libnsgif-lzw-oob.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/libnsgif-lzw-oob.gif -------------------------------------------------------------------------------- /tests/data/bad/libnsgif-uninitialized-frame-fields.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/libnsgif-uninitialized-frame-fields.gif -------------------------------------------------------------------------------- /tests/data/bad/lodepng-adam7-mystery-over-read.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/lodepng-adam7-mystery-over-read.png -------------------------------------------------------------------------------- /tests/data/bad/lodepng-zero-length-literal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/lodepng-zero-length-literal.png -------------------------------------------------------------------------------- /tests/data/bad/lodepng-zlib-big-alloc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/lodepng-zlib-big-alloc.png -------------------------------------------------------------------------------- /tests/data/bad/no-frame-data.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/no-frame-data.gif -------------------------------------------------------------------------------- /tests/data/bad/pixops-normalize-signed-overflow.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/pixops-normalize-signed-overflow.gif -------------------------------------------------------------------------------- /tests/data/bad/smolscale-unaligned-1.xwd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/smolscale-unaligned-1.xwd -------------------------------------------------------------------------------- /tests/data/bad/smolscale-unaligned-2.xwd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/smolscale-unaligned-2.xwd -------------------------------------------------------------------------------- /tests/data/bad/smolscale-unaligned-3.xwd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/smolscale-unaligned-3.xwd -------------------------------------------------------------------------------- /tests/data/bad/smolscale-unaligned-4.xwd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/bad/smolscale-unaligned-4.xwd -------------------------------------------------------------------------------- /tests/data/good/Makefile.am: -------------------------------------------------------------------------------- 1 | EXTRA_DIST = \ 2 | anim-local-cmaps.gif \ 3 | anim.gif \ 4 | card-32c-alpha.png \ 5 | card-32c-noalpha.png \ 6 | card-full-alpha.png \ 7 | card-full-noalpha.png \ 8 | noise-32x32-indexed.png \ 9 | noise-32x32.gif \ 10 | noise-32x32.png \ 11 | noise-32x32.xwd \ 12 | pixel.avif \ 13 | pixel.gif \ 14 | pixel.jpeg \ 15 | pixel.jxl \ 16 | pixel.png \ 17 | pixel.qoi \ 18 | pixel.svg \ 19 | pixel.tiff \ 20 | pixel.webp \ 21 | pixel.xwd \ 22 | taxic.jpg 23 | -------------------------------------------------------------------------------- /tests/data/good/anim-local-cmaps.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/anim-local-cmaps.gif -------------------------------------------------------------------------------- /tests/data/good/anim.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/anim.gif -------------------------------------------------------------------------------- /tests/data/good/card-32c-alpha.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/card-32c-alpha.png -------------------------------------------------------------------------------- /tests/data/good/card-32c-noalpha.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/card-32c-noalpha.png -------------------------------------------------------------------------------- /tests/data/good/card-full-alpha.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/card-full-alpha.png -------------------------------------------------------------------------------- /tests/data/good/card-full-noalpha.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/card-full-noalpha.png -------------------------------------------------------------------------------- /tests/data/good/noise-32x32-indexed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/noise-32x32-indexed.png -------------------------------------------------------------------------------- /tests/data/good/noise-32x32.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/noise-32x32.gif -------------------------------------------------------------------------------- /tests/data/good/noise-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/noise-32x32.png -------------------------------------------------------------------------------- /tests/data/good/noise-32x32.xwd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/noise-32x32.xwd -------------------------------------------------------------------------------- /tests/data/good/pixel-cmyk.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/pixel-cmyk.jpeg -------------------------------------------------------------------------------- /tests/data/good/pixel.avif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/pixel.avif -------------------------------------------------------------------------------- /tests/data/good/pixel.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/pixel.gif -------------------------------------------------------------------------------- /tests/data/good/pixel.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/pixel.jpeg -------------------------------------------------------------------------------- /tests/data/good/pixel.jxl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/pixel.jxl -------------------------------------------------------------------------------- /tests/data/good/pixel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/pixel.png -------------------------------------------------------------------------------- /tests/data/good/pixel.qoi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/pixel.qoi -------------------------------------------------------------------------------- /tests/data/good/pixel.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /tests/data/good/pixel.tiff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/pixel.tiff -------------------------------------------------------------------------------- /tests/data/good/pixel.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/pixel.webp -------------------------------------------------------------------------------- /tests/data/good/pixel.xwd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/pixel.xwd -------------------------------------------------------------------------------- /tests/data/good/taxic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpjansson/chafa/c4acf780b80bde97ae897a83322da937ab9280b0/tests/data/good/taxic.jpg -------------------------------------------------------------------------------- /tests/postinstall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -evx 4 | 5 | cc -g \ 6 | -fsanitize=address,undefined \ 7 | -fsanitize-undefined-trap-on-error \ 8 | $(pkg-config --libs --cflags chafa) \ 9 | ../examples/example.c \ 10 | -o example 11 | 12 | ./example 13 | -------------------------------------------------------------------------------- /tools/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = chafa fontgen completions 2 | -------------------------------------------------------------------------------- /tools/chafa/.gitignore: -------------------------------------------------------------------------------- 1 | /chafa 2 | -------------------------------------------------------------------------------- /tools/chafa/LICENSE.qoi: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Dominic Szablewski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /tools/chafa/Makefile.am: -------------------------------------------------------------------------------- 1 | if WANT_TOOLS 2 | 3 | bin_PROGRAMS = \ 4 | chafa 5 | 6 | chafa_SOURCES = \ 7 | chafa.c \ 8 | chafa-byte-fifo.c \ 9 | chafa-byte-fifo.h \ 10 | chafa-parser.c \ 11 | chafa-parser.h \ 12 | chafa-term.c \ 13 | chafa-term.h \ 14 | chafa-wakeup.c \ 15 | chafa-wakeup.h \ 16 | file-mapping.c \ 17 | file-mapping.h \ 18 | font-loader.c \ 19 | font-loader.h \ 20 | gif-loader.c \ 21 | gif-loader.h \ 22 | grid-layout.c \ 23 | grid-layout.h \ 24 | media-loader.c \ 25 | media-loader.h \ 26 | placement-counter.c \ 27 | placement-counter.h \ 28 | png-loader.c \ 29 | png-loader.h \ 30 | named-colors.c \ 31 | named-colors.h \ 32 | qoi.h \ 33 | qoi-loader.c \ 34 | qoi-loader.h \ 35 | util.c \ 36 | util.h \ 37 | xwd-loader.c \ 38 | xwd-loader.h 39 | 40 | if HAVE_AVIF 41 | chafa_SOURCES += \ 42 | avif-loader.c \ 43 | avif-loader.h 44 | endif 45 | 46 | if HAVE_JPEG 47 | chafa_SOURCES += \ 48 | jpeg-loader.c \ 49 | jpeg-loader.h 50 | endif 51 | 52 | if HAVE_TIFF 53 | chafa_SOURCES += \ 54 | tiff-loader.c \ 55 | tiff-loader.h 56 | endif 57 | 58 | if HAVE_SVG 59 | chafa_SOURCES += \ 60 | svg-loader.c \ 61 | svg-loader.h 62 | endif 63 | 64 | if HAVE_WEBP 65 | chafa_SOURCES += \ 66 | webp-loader.c \ 67 | webp-loader.h 68 | endif 69 | 70 | if HAVE_JXL 71 | chafa_SOURCES += \ 72 | jxl-loader.c \ 73 | jxl-loader.h 74 | endif 75 | 76 | # We can pass -rpath so the binary knows where to find libchafa.so when 77 | # installed outside /usr (e.g. the default /usr/local). This affects Ubuntu. 78 | # Resolved by running ldconfig. See Github issue #32. 79 | # 80 | # This is disabled by default. 81 | 82 | chafa_CFLAGS = $(CHAFA_CFLAGS) $(GLIB_CFLAGS) $(JPEG_CFLAGS) $(SVG_CFLAGS) $(TIFF_CFLAGS) $(WEBP_CFLAGS) $(JXL_CFLAGS) $(AVIF_CFLAGS) $(FREETYPE_CFLAGS) 83 | if ENABLE_RPATH 84 | chafa_LDFLAGS = $(CHAFA_LDFLAGS) -rpath $(libdir) 85 | endif 86 | chafa_LDADD = $(GLIB_LIBS) $(JPEG_LIBS) $(SVG_LIBS) $(TIFF_LIBS) $(WEBP_LIBS) $(JXL_LIBS) $(AVIF_LIBS) $(FREETYPE_LIBS) $(top_builddir)/chafa/libchafa.la $(top_builddir)/libnsgif/libnsgif.la $(top_builddir)/lodepng/liblodepng.la -lm $(WIN32_LDADD) 87 | 88 | # On Microsoft Windows, we compile a resource file with windres and link it in. 89 | # This enables UTF-8 support in filenames, environment variables, etc. 90 | 91 | if IS_WIN32_BUILD 92 | WIN32_LDADD = manifest.o 93 | chafa_SOURCES += \ 94 | conhost.c \ 95 | conhost.h 96 | manifest.o: $(srcdir)/manifest.rc 97 | $(WINDRES) -o $@ $(srcdir)/manifest.rc 98 | else 99 | WIN32_LDADD = 100 | endif 101 | 102 | endif 103 | 104 | ## --- General --- 105 | 106 | ## Include $(top_builddir)/chafa to get generated chafaconfig.h. 107 | 108 | AM_CPPFLAGS = \ 109 | -I$(top_srcdir)/chafa \ 110 | -I$(top_builddir)/chafa \ 111 | -I$(top_srcdir)/libnsgif \ 112 | -I$(top_srcdir)/lodepng 113 | 114 | EXTRA_DIST = manifest.rc ms-utf8.xml 115 | -------------------------------------------------------------------------------- /tools/chafa/avif-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __AVIF_LOADER_H__ 21 | #define __AVIF_LOADER_H__ 22 | 23 | #include 24 | #include "file-mapping.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct AvifLoader AvifLoader; 29 | 30 | AvifLoader *avif_loader_new_from_mapping (FileMapping *mapping); 31 | void avif_loader_destroy (AvifLoader *loader); 32 | 33 | gboolean avif_loader_get_is_animation (AvifLoader *loader); 34 | 35 | gconstpointer avif_loader_get_frame_data (AvifLoader *loader, ChafaPixelType *pixel_type_out, 36 | gint *width_out, gint *height_out, gint *rowstride_out); 37 | gint avif_loader_get_frame_delay (AvifLoader *loader); 38 | 39 | void avif_loader_goto_first_frame (AvifLoader *loader); 40 | gboolean avif_loader_goto_next_frame (AvifLoader *loader); 41 | 42 | G_END_DECLS 43 | 44 | #endif /* __AVIF_LOADER_H__ */ 45 | -------------------------------------------------------------------------------- /tools/chafa/chafa-byte-fifo.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2024-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_BYTE_FIFO_H__ 21 | #define __CHAFA_BYTE_FIFO_H__ 22 | 23 | #include 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef struct ChafaByteFifo ChafaByteFifo; 28 | 29 | ChafaByteFifo *chafa_byte_fifo_new (void); 30 | void chafa_byte_fifo_destroy (ChafaByteFifo *byte_fifo); 31 | 32 | gint chafa_byte_fifo_get_len (ChafaByteFifo *byte_fifo); 33 | void chafa_byte_fifo_push (ChafaByteFifo *byte_fifo, gconstpointer src, gint src_len); 34 | gint chafa_byte_fifo_pop (ChafaByteFifo *byte_fifo, gpointer dest, gint dest_len); 35 | gconstpointer chafa_byte_fifo_peek (ChafaByteFifo *byte_fifo, gint *len); 36 | gint chafa_byte_fifo_drop (ChafaByteFifo *byte_fifo, gint len); 37 | 38 | G_END_DECLS 39 | 40 | #endif /* __CHAFA_BYTE_FIFO_H__ */ 41 | -------------------------------------------------------------------------------- /tools/chafa/chafa-parser.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2024-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_PARSER_H__ 21 | #define __CHAFA_PARSER_H__ 22 | 23 | #include 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef enum 28 | { 29 | CHAFA_EOF_EVENT, 30 | CHAFA_UNICHAR_EVENT, 31 | CHAFA_SEQ_EVENT, 32 | } 33 | ChafaEventType; 34 | 35 | typedef struct ChafaEvent ChafaEvent; 36 | typedef struct ChafaParser ChafaParser; 37 | 38 | ChafaEventType chafa_event_get_type (ChafaEvent *event); 39 | gunichar chafa_event_get_unichar (ChafaEvent *event); 40 | ChafaTermSeq chafa_event_get_seq (ChafaEvent *event); 41 | gint chafa_event_get_seq_arg (ChafaEvent *event, gint n); 42 | gint chafa_event_get_n_seq_args (ChafaEvent *event); 43 | 44 | ChafaParser *chafa_parser_new (ChafaTermInfo *term_info); 45 | void chafa_parser_destroy (ChafaParser *parser); 46 | void chafa_parser_init (ChafaParser *parser_out, ChafaTermInfo *term_info); 47 | void chafa_parser_deinit (ChafaParser *parser); 48 | 49 | void chafa_parser_push_data (ChafaParser *parser, gconstpointer data, gint data_len); 50 | void chafa_parser_push_eof (ChafaParser *parser); 51 | ChafaEvent *chafa_parser_pop_event (ChafaParser *parser); 52 | 53 | G_END_DECLS 54 | 55 | #endif /* __CHAFA_PARSER_H__ */ 56 | -------------------------------------------------------------------------------- /tools/chafa/chafa-term.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2024-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __CHAFA_TERM_H__ 21 | #define __CHAFA_TERM_H__ 22 | 23 | #include 24 | #include "chafa-parser.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct ChafaTerm ChafaTerm; 29 | 30 | ChafaTerm *chafa_term_get_default (void); 31 | void chafa_term_destroy (ChafaTerm *term); 32 | 33 | gint chafa_term_get_buffer_max (ChafaTerm *term); 34 | void chafa_term_set_buffer_max (ChafaTerm *term, gint max); 35 | 36 | ChafaTermInfo *chafa_term_get_term_info (ChafaTerm *term); 37 | 38 | ChafaEvent *chafa_term_read_event (ChafaTerm *term, guint timeout_ms); 39 | 40 | void chafa_term_write (ChafaTerm *term, gconstpointer data, gint len); 41 | gint chafa_term_print (ChafaTerm *term, const gchar *format, ...) G_GNUC_PRINTF (2, 3); 42 | void chafa_term_print_seq (ChafaTerm *term, ChafaTermSeq seq, ...); 43 | gboolean chafa_term_flush (ChafaTerm *term); 44 | 45 | void chafa_term_write_err (ChafaTerm *term, gconstpointer data, gint len); 46 | gint chafa_term_print_err (ChafaTerm *term, const gchar *format, ...) G_GNUC_PRINTF (2, 3); 47 | 48 | void chafa_term_get_size_px (ChafaTerm *term, gint *width_px_out, gint *height_px_out); 49 | void chafa_term_get_size_cells (ChafaTerm *term, gint *width_cells_out, gint *height_cells_out); 50 | 51 | gboolean chafa_term_sync_probe (ChafaTerm *term, gint timeout_ms); 52 | 53 | void chafa_term_notify_size_changed (ChafaTerm *term); 54 | 55 | gint32 chafa_term_get_default_fg_color (ChafaTerm *term); 56 | gint32 chafa_term_get_default_bg_color (ChafaTerm *term); 57 | 58 | G_END_DECLS 59 | 60 | #endif /* __CHAFA_TERM_H__ */ 61 | -------------------------------------------------------------------------------- /tools/chafa/chafa-wakeup.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2011 Canonical Limited 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public 15 | * License along with this library; if not, see . 16 | * 17 | * Author: Ryan Lortie 18 | */ 19 | 20 | #ifndef __CHAFA_WAKEUP_H__ 21 | #define __CHAFA_WAKEUP_H__ 22 | 23 | #include 24 | 25 | typedef struct _ChafaWakeup ChafaWakeup; 26 | 27 | ChafaWakeup * chafa_wakeup_new (void); 28 | void chafa_wakeup_free (ChafaWakeup *wakeup); 29 | 30 | void chafa_wakeup_get_pollfd (ChafaWakeup *wakeup, 31 | GPollFD *poll_fd); 32 | void chafa_wakeup_signal (ChafaWakeup *wakeup); 33 | void chafa_wakeup_acknowledge (ChafaWakeup *wakeup); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /tools/chafa/conhost.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2024-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | /* Authors: oshaboy@github */ 21 | 22 | #ifndef CHARACTER_CANVAS_TO_CONHOST_H 23 | #define CHARACTER_CANVAS_TO_CONHOST_H 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | typedef WORD ConhostAttribute; 30 | typedef struct 31 | { 32 | gunichar2 *str; 33 | ConhostAttribute *attributes; 34 | size_t length; 35 | size_t utf16_string_length; 36 | } 37 | ConhostRow; 38 | 39 | /* We must determine if stdout is redirected to a file, and if so, use a 40 | * different set of I/O functions. */ 41 | extern gboolean win32_stdout_is_file; 42 | 43 | gssize canvas_to_conhost (ChafaCanvas *canvas, ConhostRow **lines); 44 | void write_image_conhost (const ConhostRow *lines, gsize s); 45 | void destroy_lines (ConhostRow *lines, gsize s); 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /tools/chafa/file-mapping.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __FILE_MAPPING_H__ 21 | #define __FILE_MAPPING_H__ 22 | 23 | #include 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef struct FileMapping FileMapping; 28 | 29 | FileMapping *file_mapping_new (const gchar *path); 30 | void file_mapping_destroy (FileMapping *file_mapping); 31 | 32 | gboolean file_mapping_open_now (FileMapping *file_mapping, GError **error); 33 | 34 | const gchar *file_mapping_get_path (FileMapping *file_mapping); 35 | 36 | gboolean file_mapping_taste (FileMapping *file_mapping, gpointer out, goffset ofs, gsize length); 37 | gssize file_mapping_read (FileMapping *file_mapping, gpointer out, goffset ofs, gsize length); 38 | gconstpointer file_mapping_get_data (FileMapping *file_mapping, gsize *length_out); 39 | 40 | gboolean file_mapping_has_magic (FileMapping *file_mapping, goffset ofs, 41 | gconstpointer data, gsize length); 42 | 43 | G_END_DECLS 44 | 45 | #endif /* __FILE_MAPPING_H__ */ 46 | -------------------------------------------------------------------------------- /tools/chafa/font-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2019-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __FONT_LOADER_H__ 21 | #define __FONT_LOADER_H__ 22 | 23 | #include 24 | #include "file-mapping.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct FontLoader FontLoader; 29 | 30 | FontLoader *font_loader_new_from_mapping (FileMapping *mapping); 31 | void font_loader_destroy (FontLoader *loader); 32 | 33 | gboolean font_loader_get_next_glyph (FontLoader *loader, gunichar *char_out, 34 | gpointer *glyph_out, gint *width_out, gint *height_out); 35 | 36 | G_END_DECLS 37 | 38 | #endif /* __FONT_LOADER_H__ */ 39 | -------------------------------------------------------------------------------- /tools/chafa/gif-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __GIF_LOADER_H__ 21 | #define __GIF_LOADER_H__ 22 | 23 | #include 24 | #include "file-mapping.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct GifLoader GifLoader; 29 | 30 | GifLoader *gif_loader_new_from_mapping (FileMapping *mapping); 31 | void gif_loader_destroy (GifLoader *loader); 32 | 33 | gboolean gif_loader_get_is_animation (GifLoader *loader); 34 | 35 | gconstpointer gif_loader_get_frame_data (GifLoader *loader, ChafaPixelType *pixel_type_out, 36 | gint *width_out, gint *height_out, gint *rowstride_out); 37 | gint gif_loader_get_frame_delay (GifLoader *loader); 38 | 39 | void gif_loader_goto_first_frame (GifLoader *loader); 40 | gboolean gif_loader_goto_next_frame (GifLoader *loader); 41 | 42 | G_END_DECLS 43 | 44 | #endif /* __GIF_LOADER_H__ */ 45 | -------------------------------------------------------------------------------- /tools/chafa/grid-layout.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2024-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __GRID_LAYOUT_H__ 21 | #define __GRID_LAYOUT_H__ 22 | 23 | #include 24 | #include "chafa-term.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct GridLayout GridLayout; 29 | 30 | GridLayout *grid_layout_new (void); 31 | void grid_layout_destroy (GridLayout *grid); 32 | 33 | void grid_layout_set_canvas_config (GridLayout *grid, ChafaCanvasConfig *canvas_config); 34 | void grid_layout_set_term_info (GridLayout *grid, ChafaTermInfo *term_info); 35 | void grid_layout_set_view_size (GridLayout *grid, gint width, gint height); 36 | void grid_layout_set_grid_size (GridLayout *grid, gint n_cols, gint n_rows); 37 | void grid_layout_set_align (GridLayout *grid, ChafaAlign halign, ChafaAlign valign); 38 | void grid_layout_set_tuck (GridLayout *grid, ChafaTuck tuck); 39 | void grid_layout_set_print_labels (GridLayout *grid, gboolean print_labels); 40 | void grid_layout_set_use_unicode (GridLayout *grid, gboolean use_unicode); 41 | 42 | void grid_layout_push_path (GridLayout *grid, const gchar *path); 43 | 44 | gboolean grid_layout_print_chunk (GridLayout *grid, ChafaTerm *term); 45 | 46 | G_END_DECLS 47 | 48 | #endif /* __GRID_LAYOUT_H__ */ 49 | -------------------------------------------------------------------------------- /tools/chafa/jpeg-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __JPEG_LOADER_H__ 21 | #define __JPEG_LOADER_H__ 22 | 23 | #include 24 | #include "file-mapping.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct JpegLoader JpegLoader; 29 | 30 | JpegLoader *jpeg_loader_new_from_mapping (FileMapping *mapping); 31 | void jpeg_loader_destroy (JpegLoader *loader); 32 | 33 | gboolean jpeg_loader_get_is_animation (JpegLoader *loader); 34 | 35 | gconstpointer jpeg_loader_get_frame_data (JpegLoader *loader, ChafaPixelType *pixel_type_out, 36 | gint *width_out, gint *height_out, gint *rowstride_out); 37 | gint jpeg_loader_get_frame_delay (JpegLoader *loader); 38 | 39 | void jpeg_loader_goto_first_frame (JpegLoader *loader); 40 | gboolean jpeg_loader_goto_next_frame (JpegLoader *loader); 41 | 42 | G_END_DECLS 43 | 44 | #endif /* __JPEG_LOADER_H__ */ 45 | -------------------------------------------------------------------------------- /tools/chafa/jxl-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2024-2025 oupson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __JXL_LOADER_H__ 21 | #define __JXL_LOADER_H__ 22 | 23 | #include "file-mapping.h" 24 | #include 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct JxlLoader JxlLoader; 29 | 30 | JxlLoader *jxl_loader_new_from_mapping (FileMapping *mapping); 31 | void jxl_loader_destroy (JxlLoader *loader); 32 | 33 | gboolean jxl_loader_get_is_animation (JxlLoader *loader); 34 | 35 | gconstpointer jxl_loader_get_frame_data (JxlLoader *loader, 36 | ChafaPixelType *pixel_type_out, gint *width_out, 37 | gint *height_out, gint *rowstride_out); 38 | gint jxl_loader_get_frame_delay (JxlLoader *loader); 39 | 40 | void jxl_loader_goto_first_frame (JxlLoader *loader); 41 | gboolean jxl_loader_goto_next_frame (JxlLoader *loader); 42 | 43 | G_END_DECLS 44 | 45 | #endif /* __JXL_LOADER_H__ */ 46 | -------------------------------------------------------------------------------- /tools/chafa/manifest.rc: -------------------------------------------------------------------------------- 1 | #include 2 | CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "ms-utf8.xml" 3 | -------------------------------------------------------------------------------- /tools/chafa/media-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2024-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __MEDIA_LOADER_H__ 21 | #define __MEDIA_LOADER_H__ 22 | 23 | #include 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef struct MediaLoader MediaLoader; 28 | 29 | MediaLoader *media_loader_new (const gchar *path, gint target_width, gint target_height, GError **error); 30 | void media_loader_destroy (MediaLoader *loader); 31 | 32 | gboolean media_loader_get_is_animation (MediaLoader *loader); 33 | 34 | void media_loader_goto_first_frame (MediaLoader *loader); 35 | gboolean media_loader_goto_next_frame (MediaLoader *loader); 36 | 37 | gconstpointer media_loader_get_frame_data (MediaLoader *loader, ChafaPixelType *pixel_type_out, 38 | gint *width_out, gint *height_out, gint *rowstride_out); 39 | gint media_loader_get_frame_delay (MediaLoader *loader); 40 | 41 | gchar **get_loader_names (void); 42 | 43 | G_END_DECLS 44 | 45 | #endif /* __MEDIA_LOADER_H__ */ 46 | -------------------------------------------------------------------------------- /tools/chafa/ms-utf8.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | UTF-8 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /tools/chafa/named-colors.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __NAMED_COLORS_H__ 21 | #define __NAMED_COLORS_H__ 22 | 23 | #include 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef struct 28 | { 29 | guint8 color [3]; 30 | const gchar *name; 31 | } 32 | NamedColor; 33 | 34 | const NamedColor *find_color_by_name (const gchar *name); 35 | 36 | G_END_DECLS 37 | 38 | #endif /* __NAMED_COLORS_H__ */ 39 | -------------------------------------------------------------------------------- /tools/chafa/placement-counter.c: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #include "config.h" 21 | #include 22 | #include 23 | 24 | #include 25 | #include "placement-counter.h" 26 | 27 | #define BUF_SIZE 256 28 | 29 | struct PlacementCounter 30 | { 31 | guint id; 32 | }; 33 | 34 | static void 35 | save_id (PlacementCounter *counter) 36 | { 37 | gchar *path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_cache_dir (), "chafa", "placement-id", NULL); 38 | gchar buf [BUF_SIZE]; 39 | 40 | snprintf (buf, BUF_SIZE, "%u\n", counter->id); 41 | g_file_set_contents (path, buf, -1, NULL); 42 | 43 | g_free (path); 44 | } 45 | 46 | static void 47 | restore_id (PlacementCounter *counter) 48 | { 49 | gchar *path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_cache_dir (), "chafa", "placement-id", NULL); 50 | gchar *buf = NULL; 51 | gsize length; 52 | 53 | g_file_get_contents (path, &buf, &length, NULL); 54 | 55 | if (buf && length > 0) 56 | { 57 | counter->id = strtoul (buf, NULL, 10); 58 | if (counter->id < 1) 59 | counter->id = 1; 60 | } 61 | 62 | g_free (buf); 63 | g_free (path); 64 | } 65 | 66 | static void 67 | ensure_id_storage (void) 68 | { 69 | gchar *path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_cache_dir (), "chafa", NULL); 70 | g_mkdir_with_parents (path, 0750); 71 | g_free (path); 72 | } 73 | 74 | PlacementCounter * 75 | placement_counter_new (void) 76 | { 77 | PlacementCounter *counter; 78 | 79 | counter = g_new0 (PlacementCounter, 1); 80 | counter->id = 0; 81 | 82 | ensure_id_storage (); 83 | restore_id (counter); 84 | 85 | return counter; 86 | } 87 | 88 | void 89 | placement_counter_destroy (PlacementCounter *counter) 90 | { 91 | save_id (counter); 92 | g_free (counter); 93 | } 94 | 95 | guint 96 | placement_counter_get_next_id (PlacementCounter *counter) 97 | { 98 | counter->id = 1 + (counter->id % 65535); 99 | return counter->id; 100 | } 101 | -------------------------------------------------------------------------------- /tools/chafa/placement-counter.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __PLACEMENT_COUNTER_H__ 21 | #define __PLACEMENT_COUNTER_H__ 22 | 23 | #include 24 | 25 | G_BEGIN_DECLS 26 | 27 | typedef struct PlacementCounter PlacementCounter; 28 | 29 | PlacementCounter *placement_counter_new (void); 30 | void placement_counter_destroy (PlacementCounter *counter); 31 | 32 | guint placement_counter_get_next_id (PlacementCounter *counter); 33 | 34 | G_END_DECLS 35 | 36 | #endif /* __PLACEMENT_COUNTER_H__ */ 37 | -------------------------------------------------------------------------------- /tools/chafa/png-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __PNG_LOADER_H__ 21 | #define __PNG_LOADER_H__ 22 | 23 | #include 24 | #include "file-mapping.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct PngLoader PngLoader; 29 | 30 | PngLoader *png_loader_new_from_mapping (FileMapping *mapping); 31 | void png_loader_destroy (PngLoader *loader); 32 | 33 | gboolean png_loader_get_is_animation (PngLoader *loader); 34 | 35 | gconstpointer png_loader_get_frame_data (PngLoader *loader, ChafaPixelType *pixel_type_out, 36 | gint *width_out, gint *height_out, gint *rowstride_out); 37 | gint png_loader_get_frame_delay (PngLoader *loader); 38 | 39 | void png_loader_goto_first_frame (PngLoader *loader); 40 | gboolean png_loader_goto_next_frame (PngLoader *loader); 41 | 42 | G_END_DECLS 43 | 44 | #endif /* __PNG_LOADER_H__ */ 45 | -------------------------------------------------------------------------------- /tools/chafa/qoi-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __QOI_LOADER_H__ 21 | #define __QOI_LOADER_H__ 22 | 23 | #include 24 | #include "file-mapping.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct QoiLoader QoiLoader; 29 | 30 | QoiLoader *qoi_loader_new_from_mapping (FileMapping *mapping); 31 | void qoi_loader_destroy (QoiLoader *loader); 32 | 33 | gboolean qoi_loader_get_is_animation (QoiLoader *loader); 34 | 35 | gconstpointer qoi_loader_get_frame_data (QoiLoader *loader, ChafaPixelType *pixel_type_out, 36 | gint *width_out, gint *height_out, gint *rowstride_out); 37 | gint qoi_loader_get_frame_delay (QoiLoader *loader); 38 | 39 | void qoi_loader_goto_first_frame (QoiLoader *loader); 40 | gboolean qoi_loader_goto_next_frame (QoiLoader *loader); 41 | 42 | G_END_DECLS 43 | 44 | #endif /* __QOI_LOADER_H__ */ 45 | -------------------------------------------------------------------------------- /tools/chafa/svg-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __SVG_LOADER_H__ 21 | #define __SVG_LOADER_H__ 22 | 23 | #include 24 | #include "file-mapping.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct SvgLoader SvgLoader; 29 | 30 | SvgLoader *svg_loader_new_from_mapping (FileMapping *mapping, gint target_width, gint target_height); 31 | void svg_loader_destroy (SvgLoader *loader); 32 | 33 | gboolean svg_loader_get_is_animation (SvgLoader *loader); 34 | 35 | gconstpointer svg_loader_get_frame_data (SvgLoader *loader, ChafaPixelType *pixel_type_out, 36 | gint *width_out, gint *height_out, gint *rowstride_out); 37 | gint svg_loader_get_frame_delay (SvgLoader *loader); 38 | 39 | void svg_loader_goto_first_frame (SvgLoader *loader); 40 | gboolean svg_loader_goto_next_frame (SvgLoader *loader); 41 | 42 | G_END_DECLS 43 | 44 | #endif /* __SVG_LOADER_H__ */ 45 | -------------------------------------------------------------------------------- /tools/chafa/tiff-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __TIFF_LOADER_H__ 21 | #define __TIFF_LOADER_H__ 22 | 23 | #include 24 | #include "file-mapping.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct TiffLoader TiffLoader; 29 | 30 | TiffLoader *tiff_loader_new_from_mapping (FileMapping *mapping); 31 | void tiff_loader_destroy (TiffLoader *loader); 32 | 33 | gboolean tiff_loader_get_is_animation (TiffLoader *loader); 34 | 35 | gconstpointer tiff_loader_get_frame_data (TiffLoader *loader, ChafaPixelType *pixel_type_out, 36 | gint *width_out, gint *height_out, gint *rowstride_out); 37 | gint tiff_loader_get_frame_delay (TiffLoader *loader); 38 | 39 | void tiff_loader_goto_first_frame (TiffLoader *loader); 40 | gboolean tiff_loader_goto_next_frame (TiffLoader *loader); 41 | 42 | G_END_DECLS 43 | 44 | #endif /* __TIFF_LOADER_H__ */ 45 | -------------------------------------------------------------------------------- /tools/chafa/util.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __UTIL_H__ 21 | #define __UTIL_H__ 22 | 23 | #include 24 | #include "chafa-term.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef enum 29 | { 30 | ROTATION_NONE = 0, 31 | ROTATION_0 = 1, 32 | ROTATION_0_MIRROR = 2, 33 | ROTATION_180 = 3, 34 | ROTATION_180_MIRROR = 4, 35 | ROTATION_270_MIRROR = 5, 36 | ROTATION_270 = 6, 37 | ROTATION_90_MIRROR = 7, 38 | ROTATION_90 = 8, 39 | ROTATION_UNDEFINED = 9, 40 | 41 | ROTATION_MAX 42 | } 43 | RotationType; 44 | 45 | RotationType invert_rotation (RotationType rot); 46 | void rotate_image (gpointer *src, guint *width, guint *height, guint *rowstride, 47 | guint n_channels, RotationType rot); 48 | 49 | gchar *ellipsize_string (const gchar *str, gint len_max, 50 | gboolean use_unicode); 51 | gchar *path_get_ellipsized_basename (const gchar *path, gint len_max, 52 | gboolean use_unicode); 53 | void print_rep_char (ChafaTerm *term, gchar c, gint n); 54 | void path_print_label (ChafaTerm *term, const gchar *path, ChafaAlign halign, 55 | gint field_width, gboolean use_unicode); 56 | 57 | G_END_DECLS 58 | 59 | #endif /* __UTIL_H__ */ 60 | -------------------------------------------------------------------------------- /tools/chafa/webp-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __WEBP_LOADER_H__ 21 | #define __WEBP_LOADER_H__ 22 | 23 | #include 24 | #include "file-mapping.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct WebpLoader WebpLoader; 29 | 30 | WebpLoader *webp_loader_new_from_mapping (FileMapping *mapping); 31 | void webp_loader_destroy (WebpLoader *loader); 32 | 33 | gboolean webp_loader_get_is_animation (WebpLoader *loader); 34 | 35 | gconstpointer webp_loader_get_frame_data (WebpLoader *loader, ChafaPixelType *pixel_type_out, 36 | gint *width_out, gint *height_out, gint *rowstride_out); 37 | gint webp_loader_get_frame_delay (WebpLoader *loader); 38 | 39 | void webp_loader_goto_first_frame (WebpLoader *loader); 40 | gboolean webp_loader_goto_next_frame (WebpLoader *loader); 41 | 42 | G_END_DECLS 43 | 44 | #endif /* __WEBP_LOADER_H__ */ 45 | -------------------------------------------------------------------------------- /tools/chafa/xwd-loader.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 | 3 | /* Copyright (C) 2018-2025 Hans Petter Jansson 4 | * 5 | * This file is part of Chafa, a program that shows pictures on text terminals. 6 | * 7 | * Chafa is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published 9 | * by the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Chafa is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Chafa. If not, see . */ 19 | 20 | #ifndef __XWD_LOADER_H__ 21 | #define __XWD_LOADER_H__ 22 | 23 | #include 24 | #include "file-mapping.h" 25 | 26 | G_BEGIN_DECLS 27 | 28 | typedef struct XwdLoader XwdLoader; 29 | 30 | XwdLoader *xwd_loader_new_from_mapping (FileMapping *mapping); 31 | void xwd_loader_destroy (XwdLoader *loader); 32 | 33 | gboolean xwd_loader_get_is_animation (XwdLoader *loader); 34 | 35 | gconstpointer xwd_loader_get_frame_data (XwdLoader *loader, ChafaPixelType *pixel_type_out, 36 | gint *width_out, gint *height_out, gint *rowstride_out); 37 | gint xwd_loader_get_frame_delay (XwdLoader *loader); 38 | 39 | void xwd_loader_goto_first_frame (XwdLoader *loader); 40 | gboolean xwd_loader_goto_next_frame (XwdLoader *loader); 41 | 42 | G_END_DECLS 43 | 44 | #endif /* __XWD_LOADER_H__ */ 45 | -------------------------------------------------------------------------------- /tools/completions/Makefile.am: -------------------------------------------------------------------------------- 1 | PREFIX=/usr/share 2 | EXTRA_DIST = \ 3 | fish-completion.fish \ 4 | zsh-completion.zsh 5 | 6 | .PHONY: install-zsh-completion 7 | install-zsh-completion: 8 | install -Dm644 zsh-completion.zsh $(PREFIX)/zsh/site-functions/_chafa 9 | 10 | .PHONY: install-fish-completion 11 | install-fish-completion: 12 | install -Dm644 fish-completion.fish $(PREFIX)/fish/vendor_completions.d/chafa.fish 13 | -------------------------------------------------------------------------------- /tools/fontgen/Makefile.am: -------------------------------------------------------------------------------- 1 | EXTRA_DIST = \ 2 | README.md \ 3 | chafa8x8.py \ 4 | compare.py 5 | -------------------------------------------------------------------------------- /tools/fontgen/README.md: -------------------------------------------------------------------------------- 1 | Chafa8x8 Custom Block Font 2 | === 3 | 4 | ## Advantages of this custom font 5 | 6 | 1. One can read large fonts from images more easily. 7 | 8 | 2. Very good at dealing with some natural image textures. 9 | 10 | 3. Higher definition compared to block glyphs in standard Unicode. 11 | 12 | ## How to build this font 13 | 14 | 1. Prepare many images, and put them to a folder, e.g. `~/coco/*.jpg`. 15 | 16 | 2. Create dataset from those images. `./chafa8x8.py CreateDataset --glob "$HOME/coco/*.jpg"`. Note, don't miss the quotes because the wildcard should be expanded by python's `glob` instead of the shell. Although the default parameters should be fine, the optional arguments are: (1) `-Mc ` specifies the number of vectors to sample from each image file. The default is `16`. Namely, in total 16 random crops (converted to vectors) are sampled from each image; (2) `-N ` specifies the vector length. The default is 64 for 8x8 blocks. After this, a fine named `chafa8x8.npz` will be created for the following clustering step. 17 | 18 | 3. Run K-Means algorithm to find cluster center vectors. `./chafa8x8.py Clustering`. This will create the optimal vectors. The optional parameter `-C ` specifies how many vectors you want (i.e., how many optimal 8x8 blocks you want). The actual usable vectors (glyphs) will be less than this number after post processing (such as deduplication). The results will be stored in `chafa8x8.raw.json` by default. Note, this step is slow. For GPU accelerated computing, see the misc notes in the next section. 19 | 20 | 4. Post-process. Generate C code, SVG images and TTF font. `./chafa8x8.py GenA`. This is actually a composed command. It executes the following four steps: (1) It invokes `./chafa8x8.py Postproc` to deduplicate the optimal glyphs. The results will be written in `chafa8x8.json`; (2) Then it invokes `./chafa8x8.py GenC` to generate C header from json as `chafa8x8.h`; (3) Next, it invokes `./chafa8x8.py GenSVG` to convert the optimal glyphs from json into SVG files under the `chafa8x8_svg` directory; (4) Lastly, it invokes `./chafa8x8.py GenFont` to create the font file `chafa8x8.ttf` from the json result. 21 | 22 | ## Misc Notes 23 | 24 | 1. Python dependencies: `pip3 install numpy scipy scikit-learn tqdm`. 25 | 26 | 2. Note, in order to generate a usable font, the `python3-fontforge` (for Debian bullseye and newer) package has to be installed as well. It will be used in the `./chafa8x8.py GenA` step. It will automatically invoke `chafa8x8.py GenFont` subcommand for creating the font. 27 | 28 | 3. Note, the clustering step is the most time-consuming procedure. If the created dataset is very large (e.g., more than 1 million vectors inside), it won't be surprising to run for several hours or even longer. Although the result can be better with a large dataset, the resulting fonts should be good to use as long as, say, the dataset size is more than 10 times larger than the number of optimal glyphs we want. 29 | 30 | 4. You may take a quick glance at the glyphs from the json files, e.g., for the finalized glyphs: `./chafa8x8.py Dump --json chafa8x8.json | less`, and for the preliminary glyphs before deduplication: `./chafa8x8.py Dump --json chafa8x8.raw.json | less`. 31 | 32 | 5. The clustering step can be accelerated using Nvidia GPU (through CUDA). You need to install faiss first: `pip3 install faiss-gpu`. To use it, just specify the backend in the command line: `./chafa8x8.py Clustering --backend faiss`. 33 | 34 | ## Todos 35 | 36 | - [ ] update `compare.py` with the latest usage. 37 | 38 | ## Acknowledgement 39 | 40 | The core algorithm used to generate the glyphs is 41 | [K-Means](https://en.wikipedia.org/wiki/K-means_clustering), one of the 42 | clustering algorithms in the machine learning literature. 43 | 44 | You can generate a font by using some image dataset e.g. 45 | [MSCOCO](http://cocodataset.org) dataset (research-oriented). 46 | 47 | ## License 48 | 49 | Copyright (C) 2018 Mo Zhou, LGPLv3+ 50 | -------------------------------------------------------------------------------- /tools/fontgen/compare.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Just a simple tool to compare the results using different symbol sets 3 | # (C) 2018 Mo Zhou 4 | # LGPLv3+ 5 | import glob 6 | import argparse 7 | import random 8 | from subprocess import call 9 | 10 | if __name__ == '__main__': 11 | ag = argparse.ArgumentParser() 12 | ag.add_argument('--glob', type=str, required=True) 13 | ag.add_argument('-n', type=int, default=32) 14 | ag = ag.parse_args() 15 | 16 | images = glob.glob(ag.glob, recursive=True) 17 | print(f'=> found {len(images)} images') 18 | 19 | for i in range(ag.n): 20 | im = random.choice(images) 21 | print('<<<<<< chafa: block') 22 | # FIXME: the command is oudated 23 | call(['chafa', '-s', '80x24', '--symbols', 'block', im]) 24 | print('------') 25 | call(['chafa', '-s', '80x24', '--symbols', 'custom', im]) 26 | print('>>>>>> chafa: symbols=custom') 27 | --------------------------------------------------------------------------------