Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f3063b2

Browse files
Added option to write to stdout.
1 parent e2c5632 commit f3063b2

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

raspi2png

0 Bytes
Binary file not shown.

raspi2png.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ main(
5757
{
5858
int opt = 0;
5959

60+
bool writeToStdout = false;
6061
char *pngName = "snapshot.png";
6162
int32_t requestedWidth = 0;
6263
int32_t requestedHeight = 0;
@@ -71,7 +72,7 @@ main(
7172

7273
//-------------------------------------------------------------------
7374

74-
char *sopts = "d:Hh:p:w:";
75+
char *sopts = "d:Hh:p:w:s";
7576

7677
struct option lopts[] =
7778
{
@@ -80,6 +81,7 @@ main(
8081
{ "help", no_argument, NULL, 'H' },
8182
{ "pngname", required_argument, NULL, 'p' },
8283
{ "width", required_argument, NULL, 'w' },
84+
{ "stdout", no_argument, NULL, 's' },
8385
{ NULL, no_argument, NULL, 0 }
8486
};
8587

@@ -107,6 +109,11 @@ main(
107109
requestedWidth = atoi(optarg);
108110
break;
109111

112+
case 's':
113+
114+
writeToStdout = true;
115+
break;
116+
110117
case 'H':
111118
default:
112119

@@ -470,17 +477,26 @@ main(
470477
exit(EXIT_FAILURE);
471478
}
472479

473-
FILE *pngfp = fopen(pngName, "wb");
480+
FILE *pngfp = NULL;
474481

475-
if (pngfp == NULL)
482+
if (writeToStdout)
476483
{
477-
fprintf(stderr,
478-
"%s: unable to create %s - %s\n",
479-
program,
480-
pngName,
481-
strerror(errno));
484+
pngfp = stdout;
485+
}
486+
else
487+
{
488+
pngfp = fopen(pngName, "wb");
482489

483-
exit(EXIT_FAILURE);
490+
if (pngfp == NULL)
491+
{
492+
fprintf(stderr,
493+
"%s: unable to create %s - %s\n",
494+
program,
495+
pngName,
496+
strerror(errno));
497+
498+
exit(EXIT_FAILURE);
499+
}
484500
}
485501

486502
png_init_io(pngPtr, pngfp);
@@ -508,7 +524,11 @@ main(
508524

509525
png_write_end(pngPtr, NULL);
510526
png_destroy_write_struct(&pngPtr, &infoPtr);
511-
fclose(pngfp);
527+
528+
if (pngfp != stdout)
529+
{
530+
fclose(pngfp);
531+
}
512532

513533
//-------------------------------------------------------------------
514534

0 commit comments

Comments
 (0)