summaryrefslogtreecommitdiff
path: root/src/canvas/kitty
diff options
context:
space:
mode:
authorpants <niklasjo2004@gmail.com>2026-09-21 14:18:06 -0700
committerpants <niklasjo2004@gmail.com>2026-09-21 14:18:06 -0700
commit83d5ba42f8a98fc0df177efef4333f74a0481f61 (patch)
tree6f1fcff64ec9adb2fc51a7cfcc451eb22a103fb3 /src/canvas/kitty
Diffstat (limited to 'src/canvas/kitty')
-rw-r--r--src/canvas/kitty/chunk.cpp52
-rw-r--r--src/canvas/kitty/chunk.hpp42
-rw-r--r--src/canvas/kitty/kitty.cpp106
-rw-r--r--src/canvas/kitty/kitty.hpp48
4 files changed, 248 insertions, 0 deletions
diff --git a/src/canvas/kitty/chunk.cpp b/src/canvas/kitty/chunk.cpp
new file mode 100644
index 0000000..a54dc94
--- /dev/null
+++ b/src/canvas/kitty/chunk.cpp
@@ -0,0 +1,52 @@
+// Display images inside a terminal
+// Copyright (C) 2023 JustKidding
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+#include "chunk.hpp"
+#include "util.hpp"
+
+KittyChunk::KittyChunk(const unsigned char* ptr, uint64_t size):
+ptr(ptr),
+size(size)
+{
+ uint64_t bufsize = 4*((size+2)/3);
+ result.resize(bufsize + 1, 0);
+}
+
+void KittyChunk::process_chunk(KittyChunk& chunk)
+{
+ util::base64_encode_v2(chunk.get_ptr(), chunk.get_size(),
+ reinterpret_cast<unsigned char*>(chunk.get_result()));
+}
+
+void KittyChunk::operator()(KittyChunk& chunk) const
+{
+ process_chunk(chunk);
+}
+
+auto KittyChunk::get_result() -> char*
+{
+ return result.data();
+}
+
+auto KittyChunk::get_ptr() const -> const unsigned char*
+{
+ return ptr;
+}
+
+auto KittyChunk::get_size() const -> uint64_t
+{
+ return size;
+}
diff --git a/src/canvas/kitty/chunk.hpp b/src/canvas/kitty/chunk.hpp
new file mode 100644
index 0000000..ac611e8
--- /dev/null
+++ b/src/canvas/kitty/chunk.hpp
@@ -0,0 +1,42 @@
+// Display images inside a terminal
+// Copyright (C) 2023 JustKidding
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+#ifndef KITTY_CHUNK_H
+#define KITTY_CHUNK_H
+
+#include <vector>
+#include <cstdint>
+
+class KittyChunk
+{
+public:
+ KittyChunk() = default;
+ KittyChunk(const unsigned char* ptr, uint64_t size);
+
+ void operator()(KittyChunk& chunk) const;
+
+ auto get_result() -> char*;
+ [[nodiscard]] auto get_ptr() const -> const unsigned char*;
+ [[nodiscard]] auto get_size() const -> uint64_t;
+
+ static void process_chunk(KittyChunk& chunk);
+private:
+ const unsigned char* ptr;
+ uint64_t size;
+ std::vector<char> result;
+};
+
+#endif
diff --git a/src/canvas/kitty/kitty.cpp b/src/canvas/kitty/kitty.cpp
new file mode 100644
index 0000000..ed89948
--- /dev/null
+++ b/src/canvas/kitty/kitty.cpp
@@ -0,0 +1,106 @@
+// Display images inside a terminal
+// Copyright (C) 2023 JustKidding
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+#include "kitty.hpp"
+#include "dimensions.hpp"
+#include "util.hpp"
+
+#include <fmt/format.h>
+
+#include <iostream>
+
+#ifdef HAVE_STD_EXECUTION_H
+# include <execution>
+#else
+# include <oneapi/tbb.h>
+#endif
+
+Kitty::Kitty(std::unique_ptr<Image> new_image, std::mutex *stdout_mutex)
+ : image(std::move(new_image)),
+ stdout_mutex(stdout_mutex),
+ id(util::generate_random_number<uint32_t>(1))
+{
+ const auto dims = image->dimensions();
+ x = dims.x + 1;
+ y = dims.y + 1;
+}
+
+Kitty::~Kitty()
+{
+ const std::scoped_lock lock{*stdout_mutex};
+ std::cout << fmt::format("\033_Ga=d,d=i,i={}\033\\", id) << std::flush;
+}
+
+void Kitty::draw()
+{
+ generate_frame();
+}
+
+void Kitty::generate_frame()
+{
+ const int bits_per_channel = 8;
+ auto chunks = process_chunks();
+ str.append(fmt::format("\033_Ga=T,m=1,i={},q=2,f={},s={},v={};{}\033\\", id, image->channels() * bits_per_channel,
+ image->width(), image->height(), chunks.front().get_result()));
+
+ for (auto chunk = std::next(std::begin(chunks)); chunk != std::prev(std::end(chunks)); std::advance(chunk, 1)) {
+ str.append("\033_Gm=1,q=2;");
+ str.append(chunk->get_result());
+ str.append("\033\\");
+ }
+
+ str.append("\033_Gm=0,q=2;");
+ str.append(chunks.back().get_result());
+ str.append("\033\\");
+
+ const std::scoped_lock lock{*stdout_mutex};
+ util::save_cursor_position();
+ util::move_cursor(y, x);
+ std::cout << str << std::flush;
+ util::restore_cursor_position();
+ str.clear();
+}
+
+auto Kitty::process_chunks() -> std::vector<KittyChunk>
+{
+ const uint64_t chunk_size = 3068;
+ uint64_t num_chunks = image->size() / chunk_size;
+ uint64_t last_chunk_size = image->size() % chunk_size;
+ if (last_chunk_size == 0) {
+ last_chunk_size = chunk_size;
+ num_chunks--;
+ }
+ const uint64_t bytes_per_chunk = 4 * ((chunk_size + 2) / 3) + 100;
+ str.reserve((num_chunks + 2) * bytes_per_chunk);
+
+ std::vector<KittyChunk> chunks;
+ chunks.reserve(num_chunks + 2);
+ const auto *ptr = image->data();
+
+ uint64_t idx = 0;
+ for (; idx < num_chunks; idx++) {
+ chunks.emplace_back(ptr + idx * chunk_size, chunk_size);
+ }
+ chunks.emplace_back(ptr + idx * chunk_size, last_chunk_size);
+
+#ifdef HAVE_STD_EXECUTION_H
+ std::for_each(std::execution::par_unseq, std::begin(chunks), std::end(chunks), KittyChunk::process_chunk);
+#else
+ oneapi::tbb::parallel_for_each(std::begin(chunks), std::end(chunks), KittyChunk());
+#endif
+
+ return chunks;
+}
diff --git a/src/canvas/kitty/kitty.hpp b/src/canvas/kitty/kitty.hpp
new file mode 100644
index 0000000..9889434
--- /dev/null
+++ b/src/canvas/kitty/kitty.hpp
@@ -0,0 +1,48 @@
+// Display images inside a terminal
+// Copyright (C) 2023 JustKidding
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+#ifndef KITTY_WINDOW_H
+#define KITTY_WINDOW_H
+
+#include "chunk.hpp"
+#include "image.hpp"
+#include "window.hpp"
+
+#include <memory>
+#include <mutex>
+#include <vector>
+
+class Kitty : public Window
+{
+ public:
+ Kitty(std::unique_ptr<Image> new_image, std::mutex *stdout_mutex);
+ ~Kitty() override;
+
+ void draw() override;
+ void generate_frame() override;
+
+ private:
+ std::string str;
+ std::unique_ptr<Image> image;
+ std::mutex *stdout_mutex;
+ uint32_t id;
+ int x;
+ int y;
+
+ auto process_chunks() -> std::vector<KittyChunk>;
+};
+
+#endif