inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
tiv_lib.h
Go to the documentation of this file.
1
2/*
3 * Copyright (c) 2017-2023, Stefan Haustein, Aaron Liu
4 *
5 * This file is free software: you may copy, redistribute and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation, either version 3 of the License, or (at your
8 * option) any later version.
9 *
10 * This file is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * Alternatively, you may copy, redistribute and/or modify this file under
19 * the terms of the Apache License, version 2.0:
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License");
22 * you may not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * https://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS,
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
32 */
33
34#ifndef TIV_LIB_H
35#define TIV_LIB_H
36
37
38#include <array>
39#include <functional>
40#include <iostream>
41#include "CImg.h"
42
43namespace tiv
44{
45
46struct size {
47 size(unsigned int in_width, unsigned int in_height)
48 : width(in_width), height(in_height) {}
49 explicit size(cimg_library::CImg<unsigned int> img)
50 : width(img.width()), height(img.height()) {}
51 unsigned int width;
52 unsigned int height;
53 size scaled(double scale) { return size(width * scale, height * scale); }
54 size fitted_within(size container) {
55 double scale = std::min(container.width / static_cast<double>(width),
56 container.height / static_cast<double>(height));
57 return scaled(scale);
58 }
59};
60
61// Implementation of flag representation for flags in the main() method
62constexpr int FLAG_FG = 1;
63constexpr int FLAG_BG = 2;
64constexpr int FLAG_MODE_256 = 4; // Limit colors to 256-color mode
65constexpr int FLAG_24BIT = 8; // 24-bit color mode
66constexpr int FLAG_NOOPT = 16; // Only use the same half-block character
67constexpr int FLAG_TELETEXT = 32; // Use teletext characters
68
69
70// Color saturation value steps from 0 to 255
71constexpr int COLOR_STEP_COUNT = 6;
72constexpr int COLOR_STEPS[COLOR_STEP_COUNT] = {0, 0x5f, 0x87, 0xaf, 0xd7, 0xff};
73
74// Grayscale saturation value steps from 0 to 255
75constexpr int GRAYSCALE_STEP_COUNT = 24;
77 0x08, 0x12, 0x1c, 0x26, 0x30, 0x3a, 0x44, 0x4e, 0x58, 0x62, 0x6c, 0x76,
78 0x80, 0x8a, 0x94, 0x9e, 0xa8, 0xb2, 0xbc, 0xc6, 0xd0, 0xda, 0xe4, 0xee};
79
80
81typedef std::function<unsigned long(int, int)> GetPixelFunction;
82
83int clamp_byte(int value);
84
85int best_index(int value, const int STEPS[], int count);
86
87double sqr(double n);
88
95struct CharData {
96 std::array<int, 3> fgColor = std::array<int, 3>{0, 0, 0};
97 std::array<int, 3> bgColor = std::array<int, 3>{0, 0, 0};
99};
100
101// Return a CharData struct with the given code point and corresponding averag
102// fg and bg colors.
103CharData createCharData(GetPixelFunction get_pixel, int x0, int y0,
104 int codepoint, int pattern);
105
116CharData findCharData(GetPixelFunction get_pixel, int x0, int y0,
117 const int &flags);
118
119cimg_library::CImg<unsigned char> load_rgb_CImg(const char *const &filename);
120
121void printTermColor(std::ostream& os, const int &flags, int r, int g, int b);
122
123void printCodepoint(std::ostream& os, int codepoint);
124
125void printImage(const cimg_library::CImg<unsigned char> &image, const int &flags);
126std::pair<int, int> get_windows_size();
127#endif
128
129} // namespace tiv
Definition tiv_lib.cpp:61
constexpr int FLAG_TELETEXT
Definition tiv_lib.h:67
void printTermColor(std::ostream &os, const int &flags, int r, int g, int b)
Definition tiv_lib.cpp:397
CharData findCharData(GetPixelFunction get_pixel, int x0, int y0, const int &flags)
Find the best character and colors for a 4x8 part of the image at the given position.
Definition tiv_lib.cpp:271
constexpr int FLAG_BG
Definition tiv_lib.h:63
constexpr int COLOR_STEP_COUNT
Definition tiv_lib.h:71
CharData createCharData(GetPixelFunction get_pixel, int x0, int y0, int codepoint, int pattern)
Definition tiv_lib.cpp:233
std::function< unsigned long(int, int)> GetPixelFunction
Definition tiv_lib.h:81
constexpr int COLOR_STEPS[COLOR_STEP_COUNT]
Definition tiv_lib.h:72
void printCodepoint(std::ostream &os, int codepoint)
Definition tiv_lib.cpp:467
int clamp_byte(int value)
Definition tiv_lib.cpp:434
int best_index(int value, const int STEPS[], int count)
Definition tiv_lib.cpp:440
double sqr(double n)
Definition tiv_lib.cpp:438
constexpr int FLAG_24BIT
Definition tiv_lib.h:65
void printImage(const cimg_library::CImg< unsigned char > &image, const int &flags)
Definition tiv_lib.cpp:487
constexpr int FLAG_MODE_256
Definition tiv_lib.h:64
cimg_library::CImg< unsigned char > load_rgb_CImg(const char *const &filename)
Definition tiv_lib.cpp:453
std::pair< int, int > get_windows_size()
Definition tiv_lib.cpp:516
constexpr int GRAYSCALE_STEPS[GRAYSCALE_STEP_COUNT]
Definition tiv_lib.h:76
constexpr int GRAYSCALE_STEP_COUNT
Definition tiv_lib.h:75
constexpr int FLAG_NOOPT
Definition tiv_lib.h:66
constexpr int FLAG_FG
Definition tiv_lib.h:62
Struct to represent a character to be drawn.
Definition tiv_lib.h:95
int codePoint
Definition tiv_lib.h:98
std::array< int, 3 > fgColor
Definition tiv_lib.h:96
std::array< int, 3 > bgColor
Definition tiv_lib.h:97
size scaled(double scale)
Definition tiv_lib.h:53
size(unsigned int in_width, unsigned int in_height)
Definition tiv_lib.h:47
size fitted_within(size container)
Definition tiv_lib.h:54
unsigned int height
Definition tiv_lib.h:52
unsigned int width
Definition tiv_lib.h:51
size(cimg_library::CImg< unsigned int > img)
Definition tiv_lib.h:49