mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-04-22 06:44:42 +00:00
Refactor some code
This commit is contained in:
parent
c2db3b703a
commit
1d4b452839
2 changed files with 1143 additions and 1158 deletions
2054
libc/str/demangle.c
2054
libc/str/demangle.c
File diff suppressed because it is too large
Load diff
|
@ -18,33 +18,39 @@
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "dsp/scale/scale.h"
|
#include "dsp/scale/scale.h"
|
||||||
#include "libc/calls/calls.h"
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/calls/struct/stat.h"
|
|
||||||
#include "libc/calls/struct/winsize.h"
|
|
||||||
#include "libc/calls/termios.h"
|
#include "libc/calls/termios.h"
|
||||||
#include "libc/log/log.h"
|
#include "libc/fmt/conv.h"
|
||||||
#include "libc/macros.internal.h"
|
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/str/locale.h"
|
#include "libc/stdio/stdio.h"
|
||||||
#include "libc/str/str.h"
|
#include "libc/stdio/sysparam.h"
|
||||||
#include "libc/sysv/consts/exit.h"
|
#include "third_party/getopt/getopt.internal.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 "third_party/stb/stb_image.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
|
||||||
#define ORDIE(X) \
|
|
||||||
do { \
|
|
||||||
if (!(X)) \
|
|
||||||
perror(#X), exit(1); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
int want24bit_;
|
#define USAGE \
|
||||||
int wantfullsize_;
|
" IMAGE...\n\
|
||||||
|
\n\
|
||||||
|
SYNOPSIS\n\
|
||||||
|
\n\
|
||||||
|
Prints Image\n\
|
||||||
|
\n\
|
||||||
|
FLAGS\n\
|
||||||
|
\n\
|
||||||
|
-? help\n\
|
||||||
|
-t true color mode\n\
|
||||||
|
-f print image at full size\n\
|
||||||
|
-w INT specify printed width in pixels (or cols)\n\
|
||||||
|
-h INT specify printed height in pixels (or rows*2)\n\
|
||||||
|
\n"
|
||||||
|
|
||||||
|
int FLAG_width;
|
||||||
|
int FLAG_height;
|
||||||
|
bool FLAG_true;
|
||||||
|
bool FLAG_full;
|
||||||
|
const char *prog;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Quantizes 24-bit RGB to xterm256 code range [16,256).
|
* Quantizes 24-bit RGB to xterm256 code range [16,256).
|
||||||
|
@ -58,12 +64,10 @@ int rgb2xterm256(int r, int g, int b) {
|
||||||
qg = cube[(ig = UNCUBE(g))];
|
qg = cube[(ig = UNCUBE(g))];
|
||||||
qb = cube[(ib = UNCUBE(b))];
|
qb = cube[(ib = UNCUBE(b))];
|
||||||
if (SQR(qr - r) + SQR(qg - g) + SQR(qb - b) <=
|
if (SQR(qr - r) + SQR(qg - g) + SQR(qb - b) <=
|
||||||
SQR(ql - r) + SQR(ql - g) + SQR(ql - b)) {
|
SQR(ql - r) + SQR(ql - g) + SQR(ql - b))
|
||||||
return ir * 36 + ig * 6 + ib + 020;
|
return ir * 36 + ig * 6 + ib + 020;
|
||||||
} else {
|
|
||||||
return il + 0350;
|
return il + 0350;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints raw packed 8-bit RGB data from memory.
|
* Prints raw packed 8-bit RGB data from memory.
|
||||||
|
@ -72,7 +76,7 @@ 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) {
|
||||||
for (x = 0; x < xn; ++x) {
|
for (x = 0; x < xn; ++x) {
|
||||||
if (want24bit_) {
|
if (FLAG_true) {
|
||||||
printf("\033[48;2;%hhu;%hhu;%hhu;38;2;%hhu;%hhu;%hhum▄",
|
printf("\033[48;2;%hhu;%hhu;%hhu;38;2;%hhu;%hhu;%hhum▄",
|
||||||
rgb[y + 0][x][0], rgb[y + 0][x][1], rgb[y + 0][x][2],
|
rgb[y + 0][x][0], rgb[y + 0][x][1], rgb[y + 0][x][2],
|
||||||
rgb[MIN(y + 1, yn - 1)][x][0], rgb[MIN(y + 1, yn - 1)][x][1],
|
rgb[MIN(y + 1, yn - 1)][x][0], rgb[MIN(y + 1, yn - 1)][x][1],
|
||||||
|
@ -90,102 +94,117 @@ void PrintImage(int yn, int xn, unsigned char rgb[yn][xn][3]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void Deblinterlace(int zn, int yn, int xn, unsigned char dst[zn][yn][xn],
|
||||||
* Determines dimensions of teletypewriter.
|
|
||||||
*/
|
|
||||||
void GetTermSize(int *out_rows, int *out_cols) {
|
|
||||||
struct winsize ws;
|
|
||||||
ws.ws_row = 20;
|
|
||||||
ws.ws_col = 80;
|
|
||||||
tcgetwinsize(STDOUT_FILENO, &ws);
|
|
||||||
tcgetwinsize(STDIN_FILENO, &ws);
|
|
||||||
*out_rows = ws.ws_row;
|
|
||||||
*out_cols = ws.ws_col;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ReadAll(int fd, char *p, size_t n) {
|
|
||||||
ssize_t rc;
|
|
||||||
size_t got;
|
|
||||||
do {
|
|
||||||
ORDIE((rc = read(fd, p, n)) != -1);
|
|
||||||
got = rc;
|
|
||||||
if (!got && n) {
|
|
||||||
fprintf(stderr, "error: expected eof\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
p += got;
|
|
||||||
n -= got;
|
|
||||||
} while (n);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Deblinterlace(long zn, long yn, long xn,
|
|
||||||
unsigned char dst[zn][yn][xn],
|
|
||||||
const unsigned char src[yn][xn][zn]) {
|
const unsigned char src[yn][xn][zn]) {
|
||||||
long y, x, z;
|
int y, x, z;
|
||||||
for (y = 0; y < yn; ++y) {
|
for (y = 0; y < yn; ++y)
|
||||||
for (x = 0; x < xn; ++x) {
|
for (x = 0; x < xn; ++x)
|
||||||
for (z = 0; z < zn; ++z) {
|
for (z = 0; z < zn; ++z)
|
||||||
dst[z][y][x] = src[y][x][z];
|
dst[z][y][x] = src[y][x][z];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Reblinterlace(long zn, long yn, long xn,
|
void Reblinterlace(int zn, int yn, int xn, unsigned char dst[yn][xn][zn],
|
||||||
unsigned char dst[yn][xn][zn],
|
|
||||||
const unsigned char src[zn][yn][xn]) {
|
const unsigned char src[zn][yn][xn]) {
|
||||||
long y, x, z;
|
int y, x, z;
|
||||||
for (y = 0; y < yn; ++y) {
|
for (y = 0; y < yn; ++y)
|
||||||
for (x = 0; x < xn; ++x) {
|
for (x = 0; x < xn; ++x)
|
||||||
for (z = 0; z < zn; ++z) {
|
for (z = 0; z < zn; ++z)
|
||||||
dst[y][x][z] = src[z][y][x];
|
dst[y][x][z] = src[z][y][x];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
wontreturn void PrintUsage(int rc, int fd) {
|
||||||
|
tinyprint(fd, "usage: ", prog, USAGE, NULL);
|
||||||
|
exit(rc);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
struct stat st;
|
|
||||||
void *map, *data, *data2;
|
// get program name
|
||||||
int i, fd, tyn, txn, yn, xn, cn;
|
prog = argv[0];
|
||||||
setlocale(LC_ALL, "C.UTF-8");
|
if (!prog)
|
||||||
for (i = 1; i < argc; ++i) {
|
prog = "basicidea";
|
||||||
if (strcmp(argv[i], "-t") == 0) {
|
|
||||||
want24bit_ = 1;
|
// get flags
|
||||||
} else if (strcmp(argv[i], "-f") == 0) {
|
int opt;
|
||||||
wantfullsize_ = 1;
|
while ((opt = getopt(argc, argv, "?tfw:h:")) != -1) {
|
||||||
} else {
|
switch (opt) {
|
||||||
fd = open(argv[i], O_RDONLY);
|
case 't':
|
||||||
if (fd == -1) {
|
FLAG_true = true;
|
||||||
perror(argv[i]);
|
break;
|
||||||
|
case 'f':
|
||||||
|
FLAG_full = true;
|
||||||
|
break;
|
||||||
|
case 'w':
|
||||||
|
FLAG_width = atoi(optarg);
|
||||||
|
break;
|
||||||
|
case 'h':
|
||||||
|
FLAG_height = atoi(optarg);
|
||||||
|
break;
|
||||||
|
case '?':
|
||||||
|
PrintUsage(0, 1);
|
||||||
|
default:
|
||||||
|
PrintUsage(1, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (argc == optind) {
|
||||||
|
tinyprint(2, prog, ": missing image path (pass -? for help)\n", NULL);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
fstat(fd, &st);
|
|
||||||
map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
// process arguments
|
||||||
data = stbi_load_from_memory(map, st.st_size, &xn, &yn, &cn, 3);
|
for (int i = optind; i < argc; ++i) {
|
||||||
munmap(map, st.st_size);
|
const char *path = argv[i];
|
||||||
close(fd);
|
|
||||||
if (!wantfullsize_) {
|
// open image
|
||||||
GetTermSize(&tyn, &txn);
|
void *data;
|
||||||
--tyn;
|
int cn, iyn, ixn;
|
||||||
tyn *= 2;
|
if (!(data = stbi_load(path, &ixn, &iyn, &cn, 3))) {
|
||||||
data2 = memalign(32, 3 * yn * xn);
|
perror(path);
|
||||||
Deblinterlace(3, yn, xn, data2, data);
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// determine size
|
||||||
|
int yn, xn;
|
||||||
|
struct winsize ws;
|
||||||
|
if (FLAG_full) {
|
||||||
|
yn = iyn;
|
||||||
|
xn = ixn;
|
||||||
|
} else if (FLAG_width > 0 || FLAG_height > 0) {
|
||||||
|
if (FLAG_width <= 0) {
|
||||||
|
yn = FLAG_height;
|
||||||
|
xn = (double)ixn / iyn * FLAG_height + .5;
|
||||||
|
} else if (FLAG_height <= 0) {
|
||||||
|
yn = (double)iyn / ixn * FLAG_width + .5;
|
||||||
|
xn = FLAG_width;
|
||||||
|
} else {
|
||||||
|
yn = FLAG_height;
|
||||||
|
xn = FLAG_width;
|
||||||
|
}
|
||||||
|
} else if (!tcgetwinsize(1, &ws)) {
|
||||||
|
yn = (ws.ws_row - 1) * 2;
|
||||||
|
xn = ws.ws_col;
|
||||||
|
} else {
|
||||||
|
yn = iyn;
|
||||||
|
xn = ixn;
|
||||||
|
}
|
||||||
|
|
||||||
|
// scale image
|
||||||
|
if (yn != iyn || xn != ixn) {
|
||||||
|
void *data2 = memalign(32, 3l * iyn * ixn);
|
||||||
|
Deblinterlace(3, iyn, ixn, data2, data);
|
||||||
free(data);
|
free(data);
|
||||||
data = memalign(32, 3 * tyn * txn);
|
data = memalign(32, 3l * yn * xn);
|
||||||
EzGyarados(3, tyn, txn, data, 3, yn, xn, data2, 0, 3, tyn, txn, yn, xn,
|
EzGyarados(3, yn, xn, data, 3, iyn, ixn, data2, 0, 3, yn, xn, iyn, ixn, 0,
|
||||||
0, 0, 0, 0);
|
0, 0, 0);
|
||||||
free(data2);
|
free(data2);
|
||||||
data2 = memalign(32, 3 * yn * xn);
|
data2 = memalign(32, 3l * yn * xn);
|
||||||
Reblinterlace(3, tyn, txn, data2, data);
|
Reblinterlace(3, yn, xn, data2, data);
|
||||||
free(data);
|
free(data);
|
||||||
data = data2;
|
data = data2;
|
||||||
yn = tyn;
|
|
||||||
xn = txn;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// print image
|
||||||
PrintImage(yn, xn, data);
|
PrintImage(yn, xn, data);
|
||||||
free(data);
|
free(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue