From 1b742175f50295ad6ed7ede71fd966598f9bc67b Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 22 Feb 2026 13:49:44 +0100 Subject: m fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- TODO | 1 - src/blueshift-demomode-image.c | 426 ++++++++++++++++++++-------------------- src/blueshift-demomode-image.py | 3 - src/blueshift_demomode.py | 3 - 4 files changed, 212 insertions(+), 221 deletions(-) diff --git a/TODO b/TODO index b680e22..80dabc4 100644 --- a/TODO +++ b/TODO @@ -1,2 +1 @@ add video support - diff --git a/src/blueshift-demomode-image.c b/src/blueshift-demomode-image.c index 2b597ff..a5f805d 100644 --- a/src/blueshift-demomode-image.c +++ b/src/blueshift-demomode-image.c @@ -26,7 +26,7 @@ /** * Value for `tupletype` when the image is black and white - * The bytes are: whitenames (note that it is not blackness as with P1 and P4) + * The bytes are: whiteness (note that it is not blackness as with P1 and P4) * (note that it is bytes and not bits as with P4) */ #define BLACKANDWHITE 0 @@ -76,7 +76,7 @@ static size_t height = 0; static int have_height = 0; /** - * The number of channals, 1 for black and white or greyscale, 3 for colour, plus 1 for alpha + * The number of channels, 1 for black and white or greyscale, 3 for colour, plus 1 for alpha */ static int depth = 0; static int have_depth = 0; @@ -95,255 +95,253 @@ static int have_tupltype = 0; static size_t red_n, green_n, blue_n; -static uint32_t* max = NULL; -static uint32_t* red = NULL; -static uint32_t* green = NULL; -static uint32_t* blue = NULL; +static uint32_t *max = NULL; +static uint32_t *red = NULL; +static uint32_t *green = NULL; +static uint32_t *blue = NULL; -static char* execname; +static char *execname; -static void strip(char* buffer) +static void +strip(char *buffer) { - size_t offset = 0; - size_t end = strlen(buffer); - while (buffer[offset] && ((buffer[offset] == ' ') || (buffer[offset] == '\t'))) - offset++; - while (end && ((buffer[end - 1] == ' ') || (buffer[end - 1] == '\t'))) - end--; - buffer[end] = '\0'; - memmove(buffer, buffer + offset, end + 1 - offset); + size_t offset = 0; + size_t end = strlen(buffer); + while (buffer[offset] && ((buffer[offset] == ' ') || (buffer[offset] == '\t'))) + offset++; + while (end && ((buffer[end - 1] == ' ') || (buffer[end - 1] == '\t'))) + end--; + buffer[end] = '\0'; + memmove(buffer, buffer + offset, end + 1 - offset); } -static long long conv(char* buffer) +static long long int +conv(char *buffer) { - strip(buffer); - return atoll(buffer); + strip(buffer); + return atoll(buffer); } -static uint32_t* int_split(char* buffer, size_t* out_n) +static uint32_t * +int_split(char *buffer, size_t *out_n) { - size_t n = 1; - char* buf = buffer; - uint32_t* rc = NULL; - uint32_t v = 0; + size_t n = 1; + char *buf = buffer; + uint32_t *rc = NULL; + uint32_t v = 0; - while (*buf) - if (*buf++ == ' ') - n++; + while (*buf) + if (*buf++ == ' ') + n++; - if (out_n) - *out_n = n; - rc = malloc(n * sizeof(uint32_t)); - if (rc == NULL) - return NULL; + if (out_n) + *out_n = n; + rc = malloc(n * sizeof(uint32_t)); + if (!rc) + return NULL; - for (n = 0; *buf; buf++) - if (*buf == ' ') - rc[n++] = v, v = 0; - else - v = (v * 10) + (*buf & 15); - rc[n] = v; + for (n = 0; *buf; buf++) { + if (*buf == ' ') + rc[n++] = v, v = 0; + else + v = (v * 10) + (*buf & 15); + } + rc[n] = v; - return rc; + return rc; } /* TODO precalculate */ -static inline void conv1(unsigned char* in, unsigned char* out, uint32_t* lut, size_t lutsize, uint32_t lutmax) +static inline void +conv1(unsigned char *in, unsigned char *out, uint32_t *lut, size_t lutsize, uint32_t lutmax) { - size_t n = ((uint32_t)*in * lutsize + maxval / 2) / maxval; - if (n > lutsize) - n = lutsize; - *out = (unsigned char)(lut[n] * maxval / lutmax); + size_t n = ((uint32_t)*in * lutsize + maxval / 2) / maxval; + if (n > lutsize) + n = lutsize; + *out = (unsigned char)(lut[n] * maxval / lutmax); } -static inline void conv2(unsigned char* in, unsigned char* out, uint32_t* lut, size_t lutsize, uint32_t lutmax) +static inline void +conv2(unsigned char *in, unsigned char *out, uint32_t *lut, size_t lutsize, uint32_t lutmax) { - size_t n = (((uint32_t)(in[0])) << 8) | ((uint32_t)(in[1])); - n = (n * lutsize + maxval / 2) / maxval; - if (n > lutsize) - n = lutsize; - n = lut[n] * maxval / lutmax; - out[0] = (n >> 8) & 255; - out[1] = (n >> 0) & 255; + size_t n = (((uint32_t)in[0]) << 8) | ((uint32_t)in[1]); + n = (n * lutsize + maxval / 2) / maxval; + if (n > lutsize) + n = lutsize; + n = lut[n] * maxval / lutmax; + out[0] = (n >> 8) & 255; + out[1] = (n >> 0) & 255; } -#define FILTER(FOR, SIZE, RGB, WPTR) \ - for FOR \ - { \ - conv##SIZE(data + ptr + RGB * SIZE * 0, data + WPTR + SIZE * 0, red, red_n, max[0]); \ - conv##SIZE(data + ptr + RGB * SIZE * 1, data + WPTR + SIZE * 1, green, green_n, max[1]); \ - conv##SIZE(data + ptr + RGB * SIZE * 2, data + WPTR + SIZE * 2, blue, blue_n, max[2]); \ - } +#define FILTER(FOR, SIZE, RGB, WPTR)\ + for FOR {\ + conv##SIZE(data + ptr + RGB * SIZE * 0, data + WPTR + SIZE * 0, red, red_n, max[0]);\ + conv##SIZE(data + ptr + RGB * SIZE * 1, data + WPTR + SIZE * 1, green, green_n, max[1]);\ + conv##SIZE(data + ptr + RGB * SIZE * 2, data + WPTR + SIZE * 2, blue, blue_n, max[2]);\ + } -static int filter(size_t size, int rgb, int alpha, int dual) +static int +filter(size_t size, int rgb, int alpha, int dual) { - char* data = malloc(size * (rgb ? 1 : 3)); - size_t ptr = 0; - ssize_t got; - if (data == NULL) - goto fail; - - while (size) - { - got = read(0, data + ptr, size); - if (got < 0) - goto fail; - ptr += (size_t)got; - size -= (size_t)got; - } - - size = ptr; - - if (rgb && alpha && dual) FILTER((ptr = 0; ptr < size; ptr += 8), 2, 1, ptr) - else if (rgb && alpha) FILTER((ptr = 0; ptr < size; ptr += 4), 1, 1, ptr) - else if (rgb && dual) FILTER((ptr = 0; ptr < size; ptr += 6), 2, 1, ptr) - else if (rgb) FILTER((ptr = 0; ptr < size; ptr += 3), 1, 1, ptr) - else if (alpha && dual) FILTER((; ptr; ptr -= 4, size -= 8), 2, 0, size - 8) - else if (alpha) FILTER((; ptr; ptr -= 2, size -= 4), 1, 0, size - 4) - else if (dual) FILTER((; ptr; ptr -= 2, size -= 6), 2, 0, size - 6) - else FILTER((; ptr; ptr -= 1, size -= 3), 1, 0, size - 3) - - return 0; - fail: - perror(execname); - free(data); - return -1; + char *data = malloc(size * (rgb ? 1 : 3)); + size_t ptr = 0; + ssize_t got; + + if (!data) + goto fail; + + while (size) { + got = read(0, data + ptr, size); + if (got < 0) + goto fail; + ptr += (size_t)got; + size -= (size_t)got; + } + + size = ptr; + + if (rgb && alpha && dual) FILTER((ptr = 0; ptr < size; ptr += 8), 2, 1, ptr) + else if (rgb && alpha) FILTER((ptr = 0; ptr < size; ptr += 4), 1, 1, ptr) + else if (rgb && dual) FILTER((ptr = 0; ptr < size; ptr += 6), 2, 1, ptr) + else if (rgb) FILTER((ptr = 0; ptr < size; ptr += 3), 1, 1, ptr) + else if (alpha && dual) FILTER((; ptr; ptr -= 4, size -= 8), 2, 0, size - 8) + else if (alpha) FILTER((; ptr; ptr -= 2, size -= 4), 1, 0, size - 4) + else if (dual) FILTER((; ptr; ptr -= 2, size -= 6), 2, 0, size - 6) + else FILTER((; ptr; ptr -= 1, size -= 3), 1, 0, size - 3) + + return 0; + +fail: + perror(execname); + free(data); + return -1; } -int main(int argc, char** argv) +int +main(int argc, char** argv) { - int ok = 0; - int stage = 0; - char buf[1024]; - size_t ptr = 0, size; - int alpha; - - execname = *argv; - - - for (;;) - { - int c = getchar(); - if (c < 0) - goto bad; - if (c == '\n') - { - buf[ptr] = '\0'; - ptr = 0; - strip(buf); - if (!strcmp(buf, "P7")) - { - if (stage++) - break; - } - else if (!strcmp(buf, "ENDHDR")) - { - ok = 1; - break; - } - else if (strstr(buf, "WIDTH") == buf) - { - if (have_width) break; - have_width = 1, width = (size_t)conv(buf + 5); - } - else if (strstr(buf, "HEIGHT") == buf) - { - if (have_height) break; - have_height = 1, height = (size_t)conv(buf + 6); - } - else if (strstr(buf, "DEPTH") == buf) - { - if (have_depth) break; - have_depth = 1, depth = (int)conv(buf + 5); - } - else if (strstr(buf, "MAXVAL") == buf) - { - if (have_maxval) break; - have_maxval = 1, maxval = (uint32_t)conv(buf + 6); - } - else if (strstr(buf, "TUPLTYPE") == buf) - { - if (have_tupltype) break; - strip(buf + 8); - have_tupltype = 1; - if (!strcmp(buf + 8, "BLACKANDWHITE")) tupltype = BLACKANDWHITE; - else if (!strcmp(buf + 8, "GRAYSCALE")) tupltype = GRAYSCALE; - else if (!strcmp(buf + 8, "RGB")) tupltype = RGB; - else if (!strcmp(buf + 8, "BLACKANDWHITE_ALPHA")) tupltype = BLACKANDWHITE_ALPHA; - else if (!strcmp(buf + 8, "GRAYSCALE_ALPHA")) tupltype = GRAYSCALE_ALPHA; - else if (!strcmp(buf + 8, "RGB_ALPHA")) tupltype = RGB_ALPHA; - else - goto bad; - } + int ok = 0; + int stage = 0; + char buf[1024]; + size_t ptr = 0, size; + int alpha, c; + + execname = *argv; + + for (;;) { + c = getchar(); + if (c < 0) + goto bad; + if (c == '\n') { + buf[ptr] = '\0'; + ptr = 0; + strip(buf); + if (!strcmp(buf, "P7")) { + if (stage++) + break; + } else if (!strcmp(buf, "ENDHDR")) { + ok = 1; + break; + } else if (strstr(buf, "WIDTH") == buf) { + if (have_width) + break; + have_width = 1; + width = (size_t)conv(&buf[5]); + } else if (strstr(buf, "HEIGHT") == buf) { + if (have_height) + break; + have_height = 1; + height = (size_t)conv(&buf[6]); + } else if (strstr(buf, "DEPTH") == buf) { + if (have_depth) + break; + have_depth = 1; + depth = (int)conv(&buf[5]); + } else if (strstr(buf, "MAXVAL") == buf) { + if (have_maxval) + break; + have_maxval = 1; + maxval = (uint32_t)conv(&buf[6]); + } else if (strstr(buf, "TUPLTYPE") == buf) { + if (have_tupltype) + break; + strip(&buf[8]); + have_tupltype = 1; + if (!strcmp(buf + 8, "BLACKANDWHITE")) tupltype = BLACKANDWHITE; + else if (!strcmp(buf + 8, "GRAYSCALE")) tupltype = GRAYSCALE; + else if (!strcmp(buf + 8, "RGB")) tupltype = RGB; + else if (!strcmp(buf + 8, "BLACKANDWHITE_ALPHA")) tupltype = BLACKANDWHITE_ALPHA; + else if (!strcmp(buf + 8, "GRAYSCALE_ALPHA")) tupltype = GRAYSCALE_ALPHA; + else if (!strcmp(buf + 8, "RGB_ALPHA")) tupltype = RGB_ALPHA; + else + goto bad; + } + } else if (ptr + 1 == sizeof(buf) / sizeof(*buf)) { + goto bad; + } else { + buf[ptr++] = (char)c; + } } - else - if (ptr + 1 == sizeof(buf) / sizeof(*buf)) - goto bad; - else - buf[ptr++] = (char)c; - } - - if (!ok || !width || !height || !depth || !maxval || !tupltype) - ok = 0; - else if ((width < 1) || (height < 0)) ok = 0; - else if (tupltype == BLACKANDWHITE) ok = (depth == 1) && (1 <= maxval) && (maxval <= 1); - else if (tupltype == GRAYSCALE) ok = (depth == 1) && (1 <= maxval) && (maxval <= 65535); - else if (tupltype == RGB) ok = (depth == 3) && (1 <= maxval) && (maxval <= 65535); - else if (tupltype == BLACKANDWHITE_ALPHA) ok = (depth == 2) && (1 <= maxval) && (maxval <= 1); - else if (tupltype == GRAYSCALE_ALPHA) ok = (depth == 2) && (1 <= maxval) && (maxval <= 65535); - else if (tupltype == RGB_ALPHA) ok = (depth == 4) && (1 <= maxval) && (maxval <= 65535); - if (!ok) - goto bad; - - if ((max = int_split(argv[1], NULL)) == NULL) goto fail; - if ((red = int_split(argv[2], &red_n)) == NULL) goto fail; - if ((green = int_split(argv[3], &green_n)) == NULL) goto fail; - if ((blue = int_split(argv[4], &blue_n)) == NULL) goto fail; - alpha = tupltype >= BLACKANDWHITE_ALPHA; - - - printf("P7\n"); - printf("WIDTH %zu\n", width); - printf("HEIGHT %zu\n", height); - printf("DEPTH %i\n", alpha ? 4 : 3); - printf("MAXVAL %zu\n", (size_t)maxval); - printf("TUPLTYPE %s\n", alpha ? "RGB_ALPHA" : "RGB"); - printf("ENDHDR\n"); - - - red_n--; - green_n--; - blue_n--; - size = width * height * depth * (maxval > 255 ? 2 : 1); - if (filter(size, tupltype == (alpha ? RGB_ALPHA : RGB), alpha, maxval > 255)) - goto fail; - - - free(max); - free(red); - free(green); - free(blue); - return 0; - bad: - fprintf(stderr, "%s: %s\n", execname, "Failed to load image"); - return 1; - fail: - perror(execname); - free(max); - free(red); - free(green); - free(blue); - return 1; -} + if (!ok || !width || !height || !depth || !maxval || !tupltype) + ok = 0; + else if ((width < 1) || (height < 1)) ok = 0; + else if (tupltype == BLACKANDWHITE) ok = (depth == 1) && (1 <= maxval) && (maxval <= 1); + else if (tupltype == GRAYSCALE) ok = (depth == 1) && (1 <= maxval) && (maxval <= 65535); + else if (tupltype == RGB) ok = (depth == 3) && (1 <= maxval) && (maxval <= 65535); + else if (tupltype == BLACKANDWHITE_ALPHA) ok = (depth == 2) && (1 <= maxval) && (maxval <= 1); + else if (tupltype == GRAYSCALE_ALPHA) ok = (depth == 2) && (1 <= maxval) && (maxval <= 65535); + else if (tupltype == RGB_ALPHA) ok = (depth == 4) && (1 <= maxval) && (maxval <= 65535); + if (!ok) + goto bad; + + if (!(max = int_split(argv[1], NULL))) goto fail; + if (!(red = int_split(argv[2], &red_n))) goto fail; + if (!(green = int_split(argv[3], &green_n))) goto fail; + if (!(blue = int_split(argv[4], &blue_n))) goto fail; + alpha = tupltype >= BLACKANDWHITE_ALPHA; + + printf("P7\n"); + printf("WIDTH %zu\n", width); + printf("HEIGHT %zu\n", height); + printf("DEPTH %i\n", alpha ? 4 : 3); + printf("MAXVAL %zu\n", (size_t)maxval); + printf("TUPLTYPE %s\n", alpha ? "RGB_ALPHA" : "RGB"); + printf("ENDHDR\n"); + + red_n--; + green_n--; + blue_n--; + size = width * height * depth * (maxval > 255 ? 2 : 1); + if (filter(size, tupltype == (alpha ? RGB_ALPHA : RGB), alpha, maxval > 255)) + goto fail; + + free(max); + free(red); + free(green); + free(blue); + return 0; + +bad: + fprintf(stderr, "%s: %s\n", execname, "Failed to load image"); + return 1; + +fail: + perror(execname); + free(max); + free(red); + free(green); + free(blue); + return 1; +} diff --git a/src/blueshift-demomode-image.py b/src/blueshift-demomode-image.py index ea0b334..08a5148 100755 --- a/src/blueshift-demomode-image.py +++ b/src/blueshift-demomode-image.py @@ -16,8 +16,6 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library. If not, see . ''' - - import os, sys from argparser import * @@ -114,4 +112,3 @@ else: os.dup2(read_end, 0) os.close(read_end) os.execvp(command[0], command) - diff --git a/src/blueshift_demomode.py b/src/blueshift_demomode.py index c32c7d9..53e34ba 100644 --- a/src/blueshift_demomode.py +++ b/src/blueshift_demomode.py @@ -16,8 +16,6 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library. If not, see . ''' - - import time import monitor @@ -43,4 +41,3 @@ def set_gamma_(*crtcs, screen = 0, **kwargs): print() old_set_gamma = monitor.set_gamma monitor.set_gamma = set_gamma_ - -- cgit v1.2.3-70-g09d2