commit c775040

shrub  ·  2026-02-22 22:40:50 +0000 UTC
parent eb98a26
what
1 files changed,  +21, -3
+21, -3
 1@@ -15,31 +15,49 @@
 2 #include <wayland-server.h>
 3 #include <wld/wld.h>
 4 
 5+static unsigned char *ppm_rgb_buffer;
 6+static size_t ppm_rgb_buffer_size;
 7+
 8 static void
 9 ppm(int fd, const uint8_t *pixels, uint32_t width, uint32_t height,
10     uint32_t pitch)
11 {
12 	FILE *f = fdopen(fd, "wb");
13+	size_t rgb_size = (size_t)width * height * 3;
14+
15 	if (!f) {
16 		close(fd);
17 		return;
18 	}
19 
20+	if (rgb_size > ppm_rgb_buffer_size) {
21+		unsigned char *buffer = realloc(ppm_rgb_buffer, rgb_size);
22+		if (!buffer) {
23+			fclose(f);
24+			return;
25+		}
26+
27+		ppm_rgb_buffer = buffer;
28+		ppm_rgb_buffer_size = rgb_size;
29+	}
30+
31 	/* ppm  header */
32 	fprintf(f, "P6\n%u %u\n255\n", width, height);
33 
34 	/* pixel data convert argb8888 to rgb) */
35+	unsigned char *dst = ppm_rgb_buffer;
36 	for (uint32_t y = 0; y < height; y++) {
37 		const uint32_t *row = (const uint32_t *)(pixels + ((size_t)y * pitch));
38 
39 		for (uint32_t x = 0; x < width; x++) {
40 			uint32_t pixel = row[x];
41-			unsigned char rgb[3] = {(pixel >> 16) & 0xFF, (pixel >> 8) & 0xFF,
42-			                        pixel & 0xFF};
43-			fwrite(rgb, 1, 3, f);
44+			*dst++ = (pixel >> 16) & 0xFF;
45+			*dst++ = (pixel >> 8) & 0xFF;
46+			*dst++ = pixel & 0xFF;
47 		}
48 	}
49 
50+	fwrite(ppm_rgb_buffer, 1, rgb_size, f);
51 	fclose(f);
52 }
53