Use ImageMagick to write PNG screenshot

This commit is contained in:
emersion 2017-10-07 19:12:47 +02:00
parent a87f016017
commit 63af97800f
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
1 changed files with 30 additions and 9 deletions

View File

@ -27,6 +27,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
@ -184,7 +185,7 @@ static void argb_to_rgba(uint32_t *data, size_t height, size_t stride) {
} }
static void static void
write_png(int width, int height) write_image(const char *filename, int width, int height)
{ {
int output_stride, buffer_stride, i; int output_stride, buffer_stride, i;
void *data, *d, *s; void *data, *d, *s;
@ -213,12 +214,33 @@ write_png(int width, int height)
argb_to_rgba(data, height, buffer_stride); argb_to_rgba(data, height, buffer_stride);
// TODO: call convert char size[10 + 1 + 10 + 2 + 1]; // int32_t are max 10 digits
FILE *f = fopen("wayland-screenshot", "w"); sprintf(size, "%dx%d+0", width, height);
fwrite(data, buffer_stride * height, 1, f);
fclose(f);
free(data); int fd[2];
pipe(fd);
pid_t child = fork();
if (child < 0) {
fprintf(stderr, "fork() failed\n");
exit(EXIT_FAILURE);
} else if (child != 0) {
close(fd[0]);
write(fd[1], data, buffer_stride * height);
close(fd[1]);
free(data);
waitpid(child, NULL, 0);
} else {
close(fd[1]);
if (dup2(fd[0], 0) != 0) {
fprintf(stderr, "cannot dup the pipe\n");
}
close(fd[0]);
execlp("convert", "convert", "-depth", "8", "-size", size, "rgba:-",
"-alpha", "opaque", filename, NULL);
fprintf(stderr, "cannot execute convert\n");
exit(EXIT_FAILURE);
}
} }
static int static int
@ -296,7 +318,6 @@ int main(int argc, char *argv[])
wl_display_roundtrip(display); wl_display_roundtrip(display);
} }
write_png(width, height); write_image("wayland-screenshot.png", width, height);
return EXIT_SUCCESS;
return 0;
} }