mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 03:38:31 +00:00
Minor fixes for image printing programs
This commit is contained in:
parent
9d2c8798c1
commit
b149a9bcc4
3 changed files with 101 additions and 96 deletions
|
@ -1,41 +1,22 @@
|
||||||
/*bin/echo ' -*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;coding:utf-8 -*-┤
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
│ To the extent possible under law, Justine Tunney has waived │
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||||
│ all copyright and related or neighboring rights to this file, │
|
│ │
|
||||||
│ as it is written in the following disclaimers: │
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
│ • http://unlicense.org/ │
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
╚────────────────────────────────────────────────────────────────────'>/dev/null
|
│ │
|
||||||
if ! [ "${0%.*}.exe" -nt "$0" ]; then
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
cc -O -o "${0%.*}.exe" "$0" || exit
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
fi
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
exec "${0%.*}.exe" "$@"
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
exit
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
OVERVIEW
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
Simple Terminal Image Printer
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "dsp/scale/scale.h"
|
||||||
AUTHOR
|
|
||||||
|
|
||||||
Justine Tunney <jtunney@gmail.com>
|
|
||||||
|
|
||||||
DESCRIPTION
|
|
||||||
|
|
||||||
This program demonstrates a straightforward technique for displaying
|
|
||||||
PNG/GIF/JPG/etc. files from the command line. This program supports
|
|
||||||
xterm256 and 24-bit color w/ UNICODE half blocks. For example:
|
|
||||||
|
|
||||||
apt install build-essential imagemagick
|
|
||||||
./printimage.c lemur.png
|
|
||||||
|
|
||||||
NOTES
|
|
||||||
|
|
||||||
More advanced techniques exist for (1) gaining finer detail, as well
|
|
||||||
as (2) reducing the number of bytes emitted; this program is here to
|
|
||||||
get you started. */
|
|
||||||
|
|
||||||
#include "libc/calls/calls.h"
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/calls/ioctl.h"
|
#include "libc/calls/ioctl.h"
|
||||||
#include "libc/fmt/fmt.h"
|
#include "libc/fmt/fmt.h"
|
||||||
|
@ -43,8 +24,12 @@ NOTES
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/sysv/consts/exit.h"
|
#include "libc/sysv/consts/exit.h"
|
||||||
#include "libc/sysv/consts/fileno.h"
|
#include "libc/sysv/consts/fileno.h"
|
||||||
|
#include "libc/sysv/consts/map.h"
|
||||||
|
#include "libc/sysv/consts/o.h"
|
||||||
|
#include "libc/sysv/consts/prot.h"
|
||||||
#include "libc/sysv/consts/termios.h"
|
#include "libc/sysv/consts/termios.h"
|
||||||
#include "libc/unicode/locale.h"
|
#include "libc/unicode/locale.h"
|
||||||
|
#include "third_party/stb/stb_image.h"
|
||||||
|
|
||||||
#define SQR(X) ((X) * (X))
|
#define SQR(X) ((X) * (X))
|
||||||
#define UNCUBE(x) x < 48 ? 0 : x < 115 ? 1 : (x - 35) / 40
|
#define UNCUBE(x) x < 48 ? 0 : x < 115 ? 1 : (x - 35) / 40
|
||||||
|
@ -53,7 +38,8 @@ NOTES
|
||||||
if (!(X)) perror(#X), exit(1); \
|
if (!(X)) perror(#X), exit(1); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
static int want24bit_;
|
int want24bit_;
|
||||||
|
int wantfullsize_;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Quantizes 24-bit RGB to xterm256 code range [16,256).
|
* Quantizes 24-bit RGB to xterm256 code range [16,256).
|
||||||
|
@ -80,7 +66,6 @@ int rgb2xterm256(int r, int g, int b) {
|
||||||
void PrintImage(int yn, int xn, unsigned char rgb[yn][xn][3]) {
|
void PrintImage(int yn, int xn, unsigned char rgb[yn][xn][3]) {
|
||||||
unsigned y, x;
|
unsigned y, x;
|
||||||
for (y = 0; y < yn; y += 2) {
|
for (y = 0; y < yn; y += 2) {
|
||||||
if (y) printf("\r\n");
|
|
||||||
for (x = 0; x < xn; ++x) {
|
for (x = 0; x < xn; ++x) {
|
||||||
if (want24bit_) {
|
if (want24bit_) {
|
||||||
printf("\033[48;2;%hhu;%hhu;%hhu;38;2;%hhu;%hhu;%hhum▄",
|
printf("\033[48;2;%hhu;%hhu;%hhu;38;2;%hhu;%hhu;%hhum▄",
|
||||||
|
@ -93,8 +78,8 @@ void PrintImage(int yn, int xn, unsigned char rgb[yn][xn][3]) {
|
||||||
rgb2xterm256(rgb[y + 1][x][0], rgb[y + 1][x][1], rgb[y + 1][x][2]));
|
rgb2xterm256(rgb[y + 1][x][0], rgb[y + 1][x][1], rgb[y + 1][x][2]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
printf("\e[0m\n");
|
||||||
}
|
}
|
||||||
printf("\033[0m\r");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -125,43 +110,73 @@ void ReadAll(int fd, char *p, size_t n) {
|
||||||
} while (n);
|
} while (n);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *LoadImageOrDie(char *path, int yn, int xn) {
|
static void Deblinterlace(long zn, long yn, long xn,
|
||||||
size_t size;
|
unsigned char dst[zn][yn][xn],
|
||||||
void *rgb;
|
const unsigned char src[yn][xn][zn]) {
|
||||||
char dim[10 + 1 + 10 + 1 + 1];
|
long y, x, z;
|
||||||
int pid, wstatus, readwrite[2];
|
for (y = 0; y < yn; ++y) {
|
||||||
sprintf(dim, "%ux%u" /* jfc */ "!", xn, yn);
|
for (x = 0; x < xn; ++x) {
|
||||||
pipe(readwrite);
|
for (z = 0; z < zn; ++z) {
|
||||||
if (!(pid = fork())) {
|
dst[z][y][x] = src[y][x][z];
|
||||||
close(readwrite[0]);
|
}
|
||||||
dup2(readwrite[1], STDOUT_FILENO);
|
}
|
||||||
execvp("convert", /* apt install imagemagick */
|
}
|
||||||
(char *const[]){"convert", path, "-resize", dim, "-colorspace",
|
}
|
||||||
"RGB", "-depth", "8", "rgb:-", NULL});
|
|
||||||
_Exit(EXIT_FAILURE);
|
static void Reblinterlace(long zn, long yn, long xn,
|
||||||
|
unsigned char dst[yn][xn][zn],
|
||||||
|
const unsigned char src[zn][yn][xn]) {
|
||||||
|
long y, x, z;
|
||||||
|
for (y = 0; y < yn; ++y) {
|
||||||
|
for (x = 0; x < xn; ++x) {
|
||||||
|
for (z = 0; z < zn; ++z) {
|
||||||
|
dst[y][x][z] = src[z][y][x];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
close(readwrite[1]);
|
|
||||||
ORDIE((rgb = malloc((size = yn * xn * 3))));
|
|
||||||
ReadAll(readwrite[0], rgb, size);
|
|
||||||
ORDIE(close(readwrite[0]) != -1);
|
|
||||||
ORDIE(waitpid(pid, &wstatus, 0) != -1);
|
|
||||||
ORDIE(WEXITSTATUS(wstatus) == 0);
|
|
||||||
return rgb;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
void *rgb;
|
struct stat st;
|
||||||
int i, yn, xn;
|
void *map, *data, *data2;
|
||||||
|
int i, fd, tyn, txn, yn, xn, cn;
|
||||||
setlocale(LC_ALL, "C.UTF-8");
|
setlocale(LC_ALL, "C.UTF-8");
|
||||||
GetTermSize(&yn, &xn);
|
|
||||||
yn *= 2;
|
|
||||||
for (i = 1; i < argc; ++i) {
|
for (i = 1; i < argc; ++i) {
|
||||||
if (strcmp(argv[i], "-t") == 0) {
|
if (strcmp(argv[i], "-t") == 0) {
|
||||||
want24bit_ = 1;
|
want24bit_ = 1;
|
||||||
|
} else if (strcmp(argv[i], "-f") == 0) {
|
||||||
|
wantfullsize_ = 1;
|
||||||
} else {
|
} else {
|
||||||
rgb = LoadImageOrDie(argv[i], yn, xn);
|
fd = open(argv[i], O_RDONLY);
|
||||||
PrintImage(yn, xn, rgb);
|
if (fd == -1) {
|
||||||
free(rgb);
|
perror(argv[i]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
fstat(fd, &st);
|
||||||
|
map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
|
data = stbi_load_from_memory(map, st.st_size, &xn, &yn, &cn, 3);
|
||||||
|
munmap(map, st.st_size);
|
||||||
|
close(fd);
|
||||||
|
if (!wantfullsize_) {
|
||||||
|
GetTermSize(&tyn, &txn);
|
||||||
|
--tyn;
|
||||||
|
tyn *= 2;
|
||||||
|
data2 = memalign(32, 3 * yn * xn);
|
||||||
|
Deblinterlace(3, yn, xn, data2, data);
|
||||||
|
free(data);
|
||||||
|
data = memalign(32, 3 * tyn * txn);
|
||||||
|
EzGyarados(3, tyn, txn, data, 3, yn, xn, data2, 0, 3, tyn, txn, yn, xn,
|
||||||
|
0, 0, 0, 0);
|
||||||
|
free(data2);
|
||||||
|
data2 = memalign(32, 3 * yn * xn);
|
||||||
|
Reblinterlace(3, tyn, txn, data2, data);
|
||||||
|
free(data);
|
||||||
|
data = data2;
|
||||||
|
yn = tyn;
|
||||||
|
xn = txn;
|
||||||
|
}
|
||||||
|
PrintImage(yn, xn, data);
|
||||||
|
free(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -390,22 +390,6 @@ void WithImageFile(const char *path,
|
||||||
sxn = xn;
|
sxn = xn;
|
||||||
dyn = g_flags.height;
|
dyn = g_flags.height;
|
||||||
dxn = g_flags.width;
|
dxn = g_flags.width;
|
||||||
#if 0
|
|
||||||
while (HALF(syn) > dyn || HALF(sxn) > dxn) {
|
|
||||||
if (HALF(sxn) > dxn) {
|
|
||||||
Magikarp2xX(yn, xn, data, syn, sxn);
|
|
||||||
Magikarp2xX(yn, xn, (char *)data + yn * xn, syn, sxn);
|
|
||||||
Magikarp2xX(yn, xn, (char *)data + yn * xn * 2, syn, sxn);
|
|
||||||
sxn = HALF(sxn);
|
|
||||||
}
|
|
||||||
if (HALF(syn) > dyn) {
|
|
||||||
Magikarp2xY(yn, xn, data, syn, sxn);
|
|
||||||
Magikarp2xY(yn, xn, (char *)data + yn * xn, syn, sxn);
|
|
||||||
Magikarp2xY(yn, xn, (char *)data + yn * xn * 2, syn, sxn);
|
|
||||||
syn = HALF(syn);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
data = EzGyarados(3, dyn, dxn, gc(memalign(32, dyn * dxn * 3)), cn, yn, xn,
|
data = EzGyarados(3, dyn, dxn, gc(memalign(32, dyn * dxn * 3)), cn, yn, xn,
|
||||||
data, 0, cn, dyn, dxn, syn, sxn, 0, 0, 0, 0);
|
data, 0, cn, dyn, dxn, syn, sxn, 0, 0, 0, 0);
|
||||||
yn = dyn;
|
yn = dyn;
|
||||||
|
|
|
@ -347,7 +347,6 @@ static bool CloseSpeaker(void) {
|
||||||
int rc, wstatus;
|
int rc, wstatus;
|
||||||
rc = 0;
|
rc = 0;
|
||||||
sched_yield();
|
sched_yield();
|
||||||
INFOF("CloseSpeaker");
|
|
||||||
if (playfd_) {
|
if (playfd_) {
|
||||||
rc |= close(playfd_);
|
rc |= close(playfd_);
|
||||||
playfd_ = -1;
|
playfd_ = -1;
|
||||||
|
@ -1473,8 +1472,10 @@ static void TryToOpenFrameBuffer(void) {
|
||||||
INFOF("ioctl(%s) → %d", "FBIOGET_VSCREENINFO", rc);
|
INFOF("ioctl(%s) → %d", "FBIOGET_VSCREENINFO", rc);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "xres", fb0_.vscreen.xres);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "xres", fb0_.vscreen.xres);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "yres", fb0_.vscreen.yres);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "yres", fb0_.vscreen.yres);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "xres_virtual", fb0_.vscreen.xres_virtual);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "xres_virtual",
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "yres_virtual", fb0_.vscreen.yres_virtual);
|
fb0_.vscreen.xres_virtual);
|
||||||
|
INFOF("%s.%s=%u", "fb0_.vscreen", "yres_virtual",
|
||||||
|
fb0_.vscreen.yres_virtual);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "xoffset", fb0_.vscreen.xoffset);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "xoffset", fb0_.vscreen.xoffset);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "yoffset", fb0_.vscreen.yoffset);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "yoffset", fb0_.vscreen.yoffset);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "bits_per_pixel",
|
INFOF("%s.%s=%u", "fb0_.vscreen", "bits_per_pixel",
|
||||||
|
@ -1484,8 +1485,10 @@ static void TryToOpenFrameBuffer(void) {
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen.red", "length", fb0_.vscreen.red.length);
|
INFOF("%s.%s=%u", "fb0_.vscreen.red", "length", fb0_.vscreen.red.length);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen.red", "msb_right",
|
INFOF("%s.%s=%u", "fb0_.vscreen.red", "msb_right",
|
||||||
fb0_.vscreen.red.msb_right);
|
fb0_.vscreen.red.msb_right);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen.green", "offset", fb0_.vscreen.green.offset);
|
INFOF("%s.%s=%u", "fb0_.vscreen.green", "offset",
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen.green", "length", fb0_.vscreen.green.length);
|
fb0_.vscreen.green.offset);
|
||||||
|
INFOF("%s.%s=%u", "fb0_.vscreen.green", "length",
|
||||||
|
fb0_.vscreen.green.length);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen.green", "msb_right",
|
INFOF("%s.%s=%u", "fb0_.vscreen.green", "msb_right",
|
||||||
fb0_.vscreen.green.msb_right);
|
fb0_.vscreen.green.msb_right);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen.blue", "offset", fb0_.vscreen.blue.offset);
|
INFOF("%s.%s=%u", "fb0_.vscreen.blue", "offset", fb0_.vscreen.blue.offset);
|
||||||
|
@ -1505,9 +1508,12 @@ static void TryToOpenFrameBuffer(void) {
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "accel_flags", fb0_.vscreen.accel_flags);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "accel_flags", fb0_.vscreen.accel_flags);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "pixclock", fb0_.vscreen.pixclock);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "pixclock", fb0_.vscreen.pixclock);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "left_margin", fb0_.vscreen.left_margin);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "left_margin", fb0_.vscreen.left_margin);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "right_margin", fb0_.vscreen.right_margin);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "right_margin",
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "upper_margin", fb0_.vscreen.upper_margin);
|
fb0_.vscreen.right_margin);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "lower_margin", fb0_.vscreen.lower_margin);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "upper_margin",
|
||||||
|
fb0_.vscreen.upper_margin);
|
||||||
|
INFOF("%s.%s=%u", "fb0_.vscreen", "lower_margin",
|
||||||
|
fb0_.vscreen.lower_margin);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "hsync_len", fb0_.vscreen.hsync_len);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "hsync_len", fb0_.vscreen.hsync_len);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "vsync_len", fb0_.vscreen.vsync_len);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "vsync_len", fb0_.vscreen.vsync_len);
|
||||||
INFOF("%s.%s=%u", "fb0_.vscreen", "sync", fb0_.vscreen.sync);
|
INFOF("%s.%s=%u", "fb0_.vscreen", "sync", fb0_.vscreen.sync);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue