#include #include #include #include #include #include typedef uint32_t u32; void bgraify(char *rgb_buf, char *bgra_buf, size_t n_pixels) { for (size_t i = 0; i != n_pixels; i++) { /* Naïve approach for a baseline */ char *p = &rgb_buf[i*3], *q = &bgra_buf[i*4]; q[0] = p[2]; q[1] = p[1]; q[2] = p[0]; } } /* Not currently tested. Renders an 8-bit PseudoColor image into 32-bit TrueColor. */ void depalettize(u32 *palette, unsigned char *img, u32 *out, size_t n_pixels) { for (size_t i = 0; i < n_pixels; i++) out[i] = palette[img[i]]; } void debitmapize(u32 *palette, unsigned char *img, u32 *out, size_t n_pixels) { for (size_t i = 0; i < n_pixels/8; i++) { unsigned char c = img[i]; for (size_t j = 8; j; j--) { out[i*8 + j] = palette[1 & c]; c >>= 1; } } } int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "Usage: %s largefile 53\n" "Converts the RGB data in largefile to a BGRA memory buffer\n" "53 times to measure the speed of byteswapping.\n", argv[0]); return 1; } /* XXX copied and pasted from memcpycost.c */ int f = open(argv[1], O_RDONLY); int n = atoi(argv[2]); if (f < 0) { perror(argv[1]); return 1; } int size = lseek(f, 0, SEEK_END); char *m = mmap(0, size, PROT_READ, MAP_SHARED, f, 0); if (MAP_FAILED == m) { perror("mmap"); return 1; } int out_size = size/3*4; char *p = malloc(out_size); if (!p) { perror("malloc"); return 1; } printf("BGRAifying %d input bytes into %d output bytes\n", size, out_size); for (int i = 0; i != n; i++) { bgraify(m, p, size/3); } printf("Byte 53 is %d\n", p[53]); return 0; }