Adapt to tree-sitter style
This commit is contained in:
@@ -1,54 +1,57 @@
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include "pixels.hpp"
|
||||
#include <libscerror.hpp>
|
||||
#include <libscterm.hpp>
|
||||
#include "pixels.hpp"
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static std::string resize_str() {
|
||||
sc::term t {STDOUT_FILENO};
|
||||
int cols = t.cols() - 1;
|
||||
int rows = t.rows() * 2 - 4;
|
||||
std::ostringstream oss;
|
||||
oss << cols << "x" << rows;
|
||||
return oss.str();
|
||||
sc::term t{STDOUT_FILENO};
|
||||
int cols = t.cols() - 1;
|
||||
int rows = t.rows() * 2 - 4;
|
||||
std::ostringstream oss;
|
||||
oss << cols << "x" << rows;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void convert(const std::string& path) { // try to convert to ppm using ImageMagick
|
||||
int pfd[2];
|
||||
throw_if_min1_msg(pipe(pfd), "could not create pipe");
|
||||
void convert(
|
||||
const std::string &path) { // try to convert to ppm using ImageMagick
|
||||
int pfd[2];
|
||||
throw_if_min1_msg(pipe(pfd), "could not create pipe");
|
||||
|
||||
switch (fork()) {
|
||||
case -1:
|
||||
throw std::runtime_error {"could not fork"};
|
||||
switch (fork()) {
|
||||
case -1:
|
||||
throw std::runtime_error{"could not fork"};
|
||||
|
||||
case 0: { // child
|
||||
std::string resize {resize_str()};
|
||||
case 0: { // child
|
||||
std::string resize{resize_str()};
|
||||
|
||||
throw_if_min1(dup2(pfd[1], STDOUT_FILENO));
|
||||
throw_if_min1(close(pfd[0]));
|
||||
throw_if_min1(close(pfd[1]));
|
||||
throw_if_min1(dup2(pfd[1], STDOUT_FILENO));
|
||||
throw_if_min1(close(pfd[0]));
|
||||
throw_if_min1(close(pfd[1]));
|
||||
|
||||
int res = execlp("magick", "magick", path.c_str(), "-resize", resize.c_str(), "ppm:-", nullptr);
|
||||
if (res == -1) { // probably "magick" not found, try "convert"
|
||||
res = execlp("convert", "convert", path.c_str(), "-resize", resize.c_str(), "ppm:-", nullptr);
|
||||
if (res == -1) { // no ImageMagick, abort
|
||||
throw std::runtime_error {"no ImageMagick installed, abort"};
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: // parent
|
||||
throw_if_min1(dup2(pfd[0], STDIN_FILENO));
|
||||
throw_if_min1(close(pfd[0]));
|
||||
throw_if_min1(close(pfd[1]));
|
||||
|
||||
write_image(std::cout, stdin);
|
||||
|
||||
throw_if_min1(wait(nullptr));
|
||||
break;
|
||||
int res = execlp("magick", "magick", path.c_str(), "-resize",
|
||||
resize.c_str(), "ppm:-", nullptr);
|
||||
if (res == -1) { // probably "magick" not found, try "convert"
|
||||
res = execlp("convert", "convert", path.c_str(), "-resize",
|
||||
resize.c_str(), "ppm:-", nullptr);
|
||||
if (res == -1) { // no ImageMagick, abort
|
||||
throw std::runtime_error{"no ImageMagick installed, abort"};
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: // parent
|
||||
throw_if_min1(dup2(pfd[0], STDIN_FILENO));
|
||||
throw_if_min1(close(pfd[0]));
|
||||
throw_if_min1(close(pfd[1]));
|
||||
|
||||
write_image(std::cout, stdin);
|
||||
|
||||
throw_if_min1(wait(nullptr));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
119
src/main.cpp
119
src/main.cpp
@@ -1,68 +1,67 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <getopt.h>
|
||||
#include <netpbm/pam.h>
|
||||
#include <libscstring.hpp>
|
||||
#include "version.hpp"
|
||||
#include "pixels.hpp"
|
||||
#include "conversion.hpp"
|
||||
#include "pixels.hpp"
|
||||
#include "version.hpp"
|
||||
#include <cstdlib>
|
||||
#include <getopt.h>
|
||||
#include <iostream>
|
||||
#include <libscstring.hpp>
|
||||
#include <netpbm/pam.h>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
static void print_help() {
|
||||
std::cout << "usage: termage [-h|--version] <imagefile>\n";
|
||||
std::cout << " -h, --help show this help text and exit\n";
|
||||
std::cout << " --version show version number and exit\n";
|
||||
std::cout << "usage: termage [-h|--version] <imagefile>\n";
|
||||
std::cout << " -h, --help show this help text and exit\n";
|
||||
std::cout << " --version show version number and exit\n";
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
try {
|
||||
pm_init("termage", 0);
|
||||
int opt_char, opt_val;
|
||||
struct option long_options[] = {
|
||||
{"help", no_argument, nullptr, 'h'},
|
||||
{"version", no_argument, &opt_val, 1},
|
||||
{nullptr, 0, nullptr, 0}
|
||||
};
|
||||
while ((opt_char = getopt_long(argc, argv, "h", long_options, nullptr)) != -1) {
|
||||
std::string arg {optarg ? optarg : ""};
|
||||
switch (opt_char) {
|
||||
case 0: {
|
||||
// handle long-only options here
|
||||
switch (opt_val) {
|
||||
case 1:
|
||||
std::cout << termage_version() << '\n';
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'h':
|
||||
print_help();
|
||||
return EXIT_SUCCESS;
|
||||
case '?':
|
||||
throw std::runtime_error("unrecognized option");
|
||||
}
|
||||
int main(int argc, char *argv[]) {
|
||||
try {
|
||||
pm_init("termage", 0);
|
||||
int opt_char, opt_val;
|
||||
struct option long_options[] = {{"help", no_argument, nullptr, 'h'},
|
||||
{"version", no_argument, &opt_val, 1},
|
||||
{nullptr, 0, nullptr, 0}};
|
||||
while ((opt_char = getopt_long(argc, argv, "h", long_options, nullptr)) !=
|
||||
-1) {
|
||||
std::string arg{optarg ? optarg : ""};
|
||||
switch (opt_char) {
|
||||
case 0: {
|
||||
// handle long-only options here
|
||||
switch (opt_val) {
|
||||
case 1:
|
||||
std::cout << termage_version() << '\n';
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
if (optind == argc) {
|
||||
// no file arg => read from stdin
|
||||
write_image(std::cout);
|
||||
}
|
||||
for (int i = optind; i < argc; ++i) {
|
||||
try {
|
||||
std::string path {argv[i]};
|
||||
std::string ext {sc::filename_extension(path)};
|
||||
if (ext != "ppm") {
|
||||
convert(path);
|
||||
} else {
|
||||
write_image(std::cout, path);
|
||||
}
|
||||
} catch (const std::runtime_error& ex) {
|
||||
std::cerr << "termage: " << ex.what() << '\n';
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& ex) {
|
||||
std::cerr << "termage: " << ex.what() << '\n';
|
||||
return EXIT_FAILURE;
|
||||
break;
|
||||
}
|
||||
case 'h':
|
||||
print_help();
|
||||
return EXIT_SUCCESS;
|
||||
case '?':
|
||||
throw std::runtime_error("unrecognized option");
|
||||
}
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
if (optind == argc) {
|
||||
// no file arg => read from stdin
|
||||
write_image(std::cout);
|
||||
}
|
||||
for (int i = optind; i < argc; ++i) {
|
||||
try {
|
||||
std::string path{argv[i]};
|
||||
std::string ext{sc::filename_extension(path)};
|
||||
if (ext != "ppm") {
|
||||
convert(path);
|
||||
} else {
|
||||
write_image(std::cout, path);
|
||||
}
|
||||
} catch (const std::runtime_error &ex) {
|
||||
std::cerr << "termage: " << ex.what() << '\n';
|
||||
}
|
||||
}
|
||||
} catch (const std::exception &ex) {
|
||||
std::cerr << "termage: " << ex.what() << '\n';
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1,40 +1,41 @@
|
||||
#include <memory>
|
||||
#include <libscterm.hpp>
|
||||
#include <netpbm/pam.h>
|
||||
#include "pixels.hpp"
|
||||
#include <libscterm.hpp>
|
||||
#include <memory>
|
||||
#include <netpbm/pam.h>
|
||||
|
||||
void write_image(std::ostream& os, FILE* in) {
|
||||
struct pam info;
|
||||
pnm_readpaminit(in, &info, PAM_STRUCT_SIZE(tuple_type));
|
||||
std::unique_ptr<tuple, void(*)(void*)> row1 {pnm_allocpamrow(&info), pm_freerow};
|
||||
std::unique_ptr<tuple, void(*)(void*)> row2 {pnm_allocpamrow(&info), pm_freerow};
|
||||
for (int y = 0; y < info.height; y += 2) {
|
||||
tuple* t1 {row1.get()};
|
||||
tuple* t2 {row2.get()};
|
||||
pnm_readpamrow(&info, row1.get());
|
||||
if (y < info.height - 1) {
|
||||
pnm_readpamrow(&info, row2.get());
|
||||
}
|
||||
for (int x = 0; x < info.width; ++x) {
|
||||
if (info.depth == 3) { // assume RGB, 1 byte per sample
|
||||
os << sc::io::truecolorf(t1[x][0], t1[x][1], t1[x][2]);
|
||||
if (y < info.height - 1) {
|
||||
os << sc::io::truecolorb(t2[x][0], t2[x][1], t2[x][2]);
|
||||
} else {
|
||||
os << sc::io::defaultb;
|
||||
}
|
||||
os << u8"\u2580"; // unicode upper half block
|
||||
}
|
||||
}
|
||||
os << sc::io::reset << '\n';
|
||||
void write_image(std::ostream &os, FILE *in) {
|
||||
struct pam info;
|
||||
pnm_readpaminit(in, &info, PAM_STRUCT_SIZE(tuple_type));
|
||||
std::unique_ptr<tuple, void (*)(void *)> row1{pnm_allocpamrow(&info),
|
||||
pm_freerow};
|
||||
std::unique_ptr<tuple, void (*)(void *)> row2{pnm_allocpamrow(&info),
|
||||
pm_freerow};
|
||||
for (int y = 0; y < info.height; y += 2) {
|
||||
tuple *t1{row1.get()};
|
||||
tuple *t2{row2.get()};
|
||||
pnm_readpamrow(&info, row1.get());
|
||||
if (y < info.height - 1) {
|
||||
pnm_readpamrow(&info, row2.get());
|
||||
}
|
||||
for (int x = 0; x < info.width; ++x) {
|
||||
if (info.depth == 3) { // assume RGB, 1 byte per sample
|
||||
os << sc::io::truecolorf(t1[x][0], t1[x][1], t1[x][2]);
|
||||
if (y < info.height - 1) {
|
||||
os << sc::io::truecolorb(t2[x][0], t2[x][1], t2[x][2]);
|
||||
} else {
|
||||
os << sc::io::defaultb;
|
||||
}
|
||||
os << u8"\u2580"; // unicode upper half block
|
||||
}
|
||||
}
|
||||
os << sc::io::reset << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
void write_image(std::ostream& os) {
|
||||
write_image(os, stdin);
|
||||
}
|
||||
void write_image(std::ostream &os) { write_image(os, stdin); }
|
||||
|
||||
void write_image(std::ostream& os, const std::string& path) {
|
||||
std::unique_ptr<FILE, void(*)(FILE*)> infile {pm_openr(path.c_str()), pm_close};
|
||||
write_image(os, infile.get());
|
||||
void write_image(std::ostream &os, const std::string &path) {
|
||||
std::unique_ptr<FILE, void (*)(FILE *)> infile{pm_openr(path.c_str()),
|
||||
pm_close};
|
||||
write_image(os, infile.get());
|
||||
}
|
||||
|
||||
@@ -2,18 +2,18 @@
|
||||
#include <sstream>
|
||||
|
||||
std::string termage_version() {
|
||||
#include "version.inc"
|
||||
#include "commit.inc"
|
||||
std::ostringstream oss;
|
||||
oss << "termage version " << version;
|
||||
#include "version.inc"
|
||||
std::ostringstream oss;
|
||||
oss << "termage version " << version;
|
||||
#ifdef DEBUG
|
||||
oss << " DEBUG";
|
||||
oss << " DEBUG";
|
||||
#endif
|
||||
oss << '\n';
|
||||
if (commit[0] != '\0') {
|
||||
oss << "build " << commit << ", ";
|
||||
}
|
||||
oss << __DATE__ << ", " << __TIME__ << '\n';
|
||||
oss << "(c) Bob Polis, all rights reserved";
|
||||
return oss.str();
|
||||
oss << '\n';
|
||||
if (commit[0] != '\0') {
|
||||
oss << "build " << commit << ", ";
|
||||
}
|
||||
oss << __DATE__ << ", " << __TIME__ << '\n';
|
||||
oss << "(c) Bob Polis, all rights reserved";
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user