~zethra/kitty-img

60dc31bae111e0325f08019bfea19691896d9734 — Sashanoraa 11 months ago
Initial commit
5 files changed, 258 insertions(+), 0 deletions(-)

A .gitignore
A Cargo.lock
A Cargo.toml
A LICENSE
A src/main.rs
A  => .gitignore +2 -0
@@ 1,2 @@
/target
.rpm

A  => Cargo.lock +157 -0
@@ 1,157 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"

[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"

[[package]]
name = "base64"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a"

[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"

[[package]]
name = "bytemuck"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17febce684fd15d89027105661fec94afb475cb995fbc59d2865198446ba2eea"

[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"

[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"

[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"

[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
 "cfg-if",
]

[[package]]
name = "flate2"
version = "1.0.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841"
dependencies = [
 "crc32fast",
 "miniz_oxide",
]

[[package]]
name = "image"
version = "0.24.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69b7ea949b537b0fd0af141fff8c77690f2ce96f4f41f042ccb6c69c6c965945"
dependencies = [
 "bytemuck",
 "byteorder",
 "color_quant",
 "jpeg-decoder",
 "num-rational",
 "num-traits",
 "png",
]

[[package]]
name = "jpeg-decoder"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e"

[[package]]
name = "kitty-img"
version = "0.1.0"
dependencies = [
 "base64",
 "image",
 "libc",
]

[[package]]
name = "libc"
version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"

[[package]]
name = "miniz_oxide"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa"
dependencies = [
 "adler",
]

[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
 "autocfg",
 "num-traits",
]

[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
 "autocfg",
 "num-integer",
 "num-traits",
]

[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
 "autocfg",
]

[[package]]
name = "png"
version = "0.17.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d708eaf860a19b19ce538740d2b4bdeeb8337fa53f7738455e706623ad5c638"
dependencies = [
 "bitflags",
 "crc32fast",
 "flate2",
 "miniz_oxide",
]

A  => Cargo.toml +26 -0
@@ 1,26 @@
[package]
name = "kitty-img"
description = "Print images inline in kitty"
version = "0.1.0"
edition = "2021"
license = "MIT"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
base64 = "0.21.0"
libc = "0.2.139"

[dependencies.image]
version = "0.24.5"
default_features = false
features = ["jpeg", "png", "webp"]

[package.metadata.rpm]
package = "kitty-img"

[package.metadata.rpm.cargo]
buildflags = ["--release"]

[package.metadata.rpm.targets]
kitty-img = { path = "/usr/bin/kitty-img" }

A  => LICENSE +20 -0
@@ 1,20 @@
Copyright (c) 2023 Sashanoraa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


A  => src/main.rs +53 -0
@@ 1,53 @@
use std::{path::Path, process::exit};

use base64::{prelude::BASE64_STANDARD, Engine};
use image::{imageops::resize, io::Reader as ImageReader};

fn get_winsize() -> std::io::Result<libc::winsize> {
    let w = libc::winsize {
        ws_row: 0,
        ws_col: 0,
        ws_xpixel: 0,
        ws_ypixel: 0,
    };
    match unsafe { libc::ioctl(libc::STDOUT_FILENO, libc::TIOCGWINSZ, &w) } {
        0 => Ok(w),
        e => Err(std::io::Error::from_raw_os_error(e)),
    }
}

fn main() {
    let Some(path) = std::env::args().nth(1) else {
        eprintln!("usage: kitty-img <path to image>");
        exit(1);
    };
    let Ok(win_size) = get_winsize() else {
        eprintln!("Error getting terminal size info");
        exit(1);
    };
    let Ok(img) = ImageReader::open(path) else {
        eprintln!("Error opening image file");
        exit(1);
    };
    let Ok(img) = img.decode() else {
        eprintln!("Error decoding image");
        exit(1);
    };
    let height = (win_size.ws_ypixel / win_size.ws_row) as u32;
    if height == 0 {
        eprintln!("Terminal returned bad size info");
        exit(1);
    }
    let width = (img.width() as f32 / (img.height() as f32 / height as f32)) as u32;
    let path = format!("/tmp/pfp-{width}x{height}.png");
    if !Path::new(&path).exists() {
        if let Err(_) = resize(&img, width, height, image::imageops::FilterType::Nearest)
            .save_with_format(&path, image::ImageFormat::Png)
        {
            eprintln!("Error resizing image");
            exit(1);
        }
    }
    let b64 = BASE64_STANDARD.encode(&path);
    print!("\x1b_Ga=T,q=2,f=100,t=f;{b64}\x1b\\");
}