diff options
Diffstat (limited to '')
| -rw-r--r-- | src/framebuffer.c | 103 |
1 files changed, 50 insertions, 53 deletions
diff --git a/src/framebuffer.c b/src/framebuffer.c index da0480e..c13d6ab 100644 --- a/src/framebuffer.c +++ b/src/framebuffer.c @@ -30,59 +30,56 @@ #include <stdlib.h> -int main() +int +main(void) { - auto struct fb_var_screeninfo var_info; - long width; - long height; - long y; - auto int fd; - register char* restrict mem; - register double grand_sum = 0; - double inv_width; - size_t ptr, len; - - if (fd = open("/dev/fb0", O_RDONLY), fd == -1) - return perror("get-luminosity::framebuffer: open: /dev/fb0"), close(fd), 1; - - if (ioctl(fd, (unsigned long)FBIOGET_VSCREENINFO, &var_info)) - return perror("get-luminosity::framebuffer: ioctl"), close(fd), 1; - - width = var_info.xres; - height = var_info.yres; - inv_width = 1 / (double)width; - len = (size_t)(width * height * 4); - - posix_fadvise(fd, 0, len, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE | POSIX_FADV_WILLNEED); - if (mem = malloc(len), mem == NULL) - return perror("get-luminosity::framebuffer: malloc"), close(fd), 1; - for (ptr = 0; ptr < len;) - { - register ssize_t got = read(fd, mem + ptr, len - ptr); - if (got <= 0) - return perror("get-luminosity::framebuffer: read"), close(fd), free(mem), 1; - ptr += (size_t)got; - } - close(fd); - - for (y = 0; y < height; y++) - { - register const int32_t* restrict linemem = (const int32_t*)(mem + y * width * 4); - register const int32_t* restrict end = linemem + width; - register double sum = 0; - while (linemem != end) - { - register int32_t value = *linemem++; - sum += linearise(value, 0) + linearise(value, 1) + linearise(value, 2); + auto struct fb_var_screeninfo var_info; + long width; + long height; + long y; + auto int fd; + register char *restrict mem; + register double grand_sum = 0; + double inv_width; + size_t ptr, len; + + if (fd = open("/dev/fb0", O_RDONLY), fd == -1) + return perror("get-luminosity::framebuffer: open: /dev/fb0"), 1; + + if (ioctl(fd, (unsigned long)FBIOGET_VSCREENINFO, &var_info)) + return perror("get-luminosity::framebuffer: ioctl"), close(fd), 1; + + width = var_info.xres; + height = var_info.yres; + inv_width = 1 / (double)width; + len = (size_t)(width * height * 4); + + posix_fadvise(fd, 0, len, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE | POSIX_FADV_WILLNEED); + if (mem = malloc(len), mem == NULL) + return perror("get-luminosity::framebuffer: malloc"), close(fd), 1; + for (ptr = 0; ptr < len;) { + register ssize_t got = read(fd, mem + ptr, len - ptr); + if (got <= 0) + return perror("get-luminosity::framebuffer: read"), close(fd), free(mem), 1; + ptr += (size_t)got; } - grand_sum += sum * inv_width; - } - grand_sum /= (double)(height * 3); - - fprintf(stderr, "%lf\n", (double)grand_sum); - fflush(stdout); - - free(mem); - return 0; -} + close(fd); + for (y = 0; y < height; y++) { + register const int32_t *restrict linemem = (const int32_t*)(mem + y * width * 4); + register const int32_t *restrict end = linemem + width; + register double sum = 0; + while (linemem != end) { + register int32_t value = *linemem++; + sum += linearise(value, 0) + linearise(value, 1) + linearise(value, 2); + } + grand_sum += sum * inv_width; + } + grand_sum /= (double)(height * 3); + + fprintf(stderr, "%lf\n", (double)grand_sum); + fflush(stdout); + + free(mem); + return 0; +} |
