/*

 reinhard@finalmedia.de
 Mi 29. Jul 19:40:19 CEST 2026
 Public Domain


 rgba2fdraw liest rgba frames auf stdin und gibt zugehörige fdraw "p" befehle auf stdout aus.

 Dient als minimalistischer konverter in der fdraw toolchain.
 Es können beim Programmaufruf als Parameter weitere Argumente übergeben werden.
 Diese werden dann nach jedem vollen frame als line eingestreut. Beispielweise kann man so
 nach jedem Frame auch einen "s 33333" Pause befehl ausgeben.
 Wenn man keine Pause möchte, einfach nur rgba2fdraw aufrufen.


 Beispielaufruf, um nach jedem frame auch ein "s 33333" auszugeben.

  export SCREEN_WIDTH=640
  export SCREEN_HEIGHT=180
  rgba2fdraw s 33333


 Kompilieren mittels:

  musl-gcc -Q -march=native -O3 -static-pie -fPIE -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wl,-z,relro -Wl,-z,now rgba2fdraw.c -o rgba2fdraw


*/

#include <unistd.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    char *w = getenv("SCREEN_WIDTH"), *h = getenv("SCREEN_HEIGHT");
    int width, height, x = 0, y = 0, off = 0;
    unsigned char rgba[4];
    char out_buf[4096];
    int out_idx = 0;

    if (!w || !h || (width = atoi(w)) <= 0 || (height = atoi(h)) <= 0) return 111;

    int r;
    while ((r = read(0, &rgba[off], 4 - off)) > 0) {
        off += r;
        if (off < 4) continue;
        off = 0;

        if (out_idx > 4050) {
            write(1, out_buf, out_idx);
            out_idx = 0;
        }

        out_buf[out_idx++] = 'p';
        out_buf[out_idx++] = ' ';

        int vars[5] = {x, y, rgba[0], rgba[1], rgba[2]};
        for (int i = 0; i < 5; i++) {
            char b[10];
            int j = 10, n = vars[i];
            if (!n) b[--j] = '0';
            while (n > 0) { b[--j] = (n % 10) + '0'; n /= 10; }
            while (j < 10) out_buf[out_idx++] = b[j++];
            out_buf[out_idx++] = (i < 4) ? ' ' : '\n';
        }

        if (++x >= width) { 
            x = 0; 
            if (++y >= height) {
                y = 0;

                for (int i = 1; i < argc; i++) {
                    char *arg = argv[i];
                    while (*arg) {
                        if (out_idx >= 4096) {
                            write(1, out_buf, out_idx);
                            out_idx = 0;
                        }
                        out_buf[out_idx++] = *arg++;
                    }
                    if (i < argc - 1) {
                        if (out_idx >= 4096) {
                            write(1, out_buf, out_idx);
                            out_idx = 0;
                        }
                        out_buf[out_idx++] = ' ';
                    }
                }
                if (argc > 1) {
                    if (out_idx >= 4096) {
                        write(1, out_buf, out_idx);
                        out_idx = 0;
                    }
                    out_buf[out_idx++] = '\n';
                }
            }
        }
    }
    
    if (out_idx > 0) write(1, out_buf, out_idx);
    return 0;
}

