0

I try to do something like this in C but I don't know how:( :

$: xwd -silent -root | convert xwd:- -crop 1920x1080+0+0 test.png

with execl.

Now I can do only something like that:

      #include <unistd.h>
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <fcntl.h>
      #include <stdio.h>
      
      int main(int argc, char *argv)
      {
          char *binaryPath = "/usr/bin/xwd";
          char *arg1 = "-root";
          char *arg2 = "-silent";
      
          int fd; /*file descriptor to the file we will redirect ls's output*/
      
          if((fd = open("xwd1.xwd", O_RDWR | O_CREAT , 0666))==-1){ /*open the file */
              perror("open");
              return 1;
          }
      
          dup2(fd,STDOUT_FILENO); 
          dup2(fd,STDERR_FILENO); 
          close(fd); 
          execl( binaryPath , binaryPath ,arg1,arg2, (char *) 0 );
      
          return 0;
      }

It writes screen dump to file xwd1.xwd but I don't know how make pipe with convert. Thanks for all the information and help.

  • Have you considered using `system("your | command")` instead to let a shell handle the piping? – that other guy Oct 20 '20 at 18:09
  • It actually writes a screendump to `"test.png"`, by the way. – Mark Setchell Oct 20 '20 at 18:55
  • You may find you can avoid the pipe altogether and get **ImageMagick** to write a cropped PNG for you with `import -window root -crop 1920x1080+0+0 test.png` – Mark Setchell Oct 20 '20 at 19:06
  • @MarkSetchell - you are right but when I try to put it to C program it didn't work:(. I think that import wasn't albe to determine file format. – Kris_Holder Oct 20 '20 at 19:18
  • Did you try `which import` first in your shell so you can get the full path to `import` to use in your C program. – Mark Setchell Oct 20 '20 at 19:48
  • Or you could maybe get `xwd` to write to a file and then, in a second step, get **ImageMagick** to convert the file. `xwd -silent -root -out /tmp/root.xwd` and then `convert /tmp/root.xwd -crop ... test.png` – Mark Setchell Oct 20 '20 at 20:18

1 Answers1

0

I've resolved my problem:). I've made something like that:

 system("xwd -silent -root | convert xwd:- -crop 1920x1080+0+0 test.tiff");

thank you for all comments Kris