Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for use with triggerhappy #29

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
61 changes: 55 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# raspi2png

Utility to take a snapshot of the raspberry pi screen and save it as a PNG file
Utility to take a snapshot of the raspberry pi screen and save it as a PNG file. Works with all Raspberry Pi boards.

Usage: raspi2png [--pngname name] [--width <width>] [--height <height>] [--compression <level>] [--delay <delay>] [--display <number>] [--stdout] [--help]
Usage: raspi2png [--pngname name] [--width <width>] [--height <height>] [--compression <level>] [--delay <delay>] [--display <number>] [--stdout] [--version] [--help]

--pngname,-p - name of png file to create (default is snapshot.png)
--height,-h - image height (default is screen height)
--width,-w - image width (default is screen width)
--compression,-c - PNG compression level (0 - 9)
--delay,-d - delay in seconds (default 0)
--display,-D - Raspberry Pi display number (default 0)
--stdout,-s - write file to stdout
--stdout,-s - write file to stdout
--savepath,-a - save path
--version,-v - print version number
--help,-H - print this usage information

## Simple Install
Expand All @@ -21,9 +23,56 @@ curl -sL https://raw.githubusercontent.com/AndrewFromMelbourne/raspi2png/master/

## Manual Building

You will need to install libpng before you build the program. On Raspbian
You will need to install libpng before you build the program. On Raspbian / Raspberry Pi OS execute
``sudo apt-get install libpng-dev``
Then just type 'make' and 'make install' in the raspi2png directory you cloned from github.

sudo apt-get install libpng12-dev
## Execute with print key (triggerhappy)

Then just type 'make' in the raspi2png directory you cloned from github.
create configuration file '/etc/triggerhappy/triggers.d/printscr.conf':
```
KEY_SYSRQ 1 /usr/bin/raspi2png -a /home/pi/snapshot
```
execute following commands:
```
mkdir /home/pi/snapshot
chmod g+w /home/pi/snapshot
sudo chown nobody /home/pi/snapshot
sudo adduser nobody video
sudo service triggerhappy restart
```

### Hardware GPIO input for print key (BCM26 active low)

Command-line:
```
sudo dtoverlay gpio-key gpio=26 keycode=99 label="KEY_SYSRQ" gpio_pull=2
```
config.txt:
```
dtoverlay=gpio-key,gpio=26,keycode=99,label="KEY_SYSRQ",gpio_pull=2
```


## Benchmark

raspi2png 1.1, 720p screen, Raspberry Pi 3 B, RAM-disk

| compression | seconds | size (KiB) |
|:-:|:----:|-----:|
| 0 | 0,23 | 2705 |
| 1 | 0,42 | 676 |
| **2** | 0,42 | 654 |
| 3 | 0,53 | 624 |
| 4 | 0,57 | 606 |
| 5 | 0,71 | 593 |
| 6 | 1,10 | 575 |
| 7 | 1,46 | 569 |
| 8 | 3,04 | 564 |
| 9 | 5,30 | 560 |

optipng size: ~553 KiB


![Screenshot](https://github.com/GrazerComputerClub/raspi2png/raw/master/screenshot.png)

Binary file modified raspi2png
Binary file not shown.
59 changes: 52 additions & 7 deletions raspi2png.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
#include <string.h>
#include <unistd.h>
#include <zlib.h>

#include <time.h>
#include <sys/stat.h>
#include "bcm_host.h"

//-----------------------------------------------------------------------
Expand All @@ -47,9 +48,14 @@

//-----------------------------------------------------------------------

#define VERSION "1.1"
#define DEFAULT_DELAY 0
#define DEFAULT_DISPLAY_NUMBER 0
#define DEFAULT_NAME "snapshot.png"
#define DEFAULT_PNG_COMPRESSION 2
#define DEFAULT_NAME_START "snapshot"
#define DEFAULT_NAME DEFAULT_NAME_START ".png"
#define MAX_NAME_SIZE 50


//-----------------------------------------------------------------------

Expand All @@ -60,11 +66,15 @@ const char* program = NULL;
void
usage(void)
{
fprintf(stderr, "%s (%s)\n", program, VERSION);
fprintf(stderr, "Utility to take a snapshot of the raspberry pi screen and save it as a PNG file\n\n");

fprintf(stderr, "Usage: %s [--pngname name]", program);
fprintf(stderr, " [--width <width>] [--height <height>]");
fprintf(stderr, " [--compression <level>]");
fprintf(stderr, " [--delay <delay>] [--display <number>]");
fprintf(stderr, " [--stdout] [--help]\n");
fprintf(stderr, " [--stdout] [--savepath path]");
fprintf(stderr, " [--version] [--help]\n");

fprintf(stderr, "\n");

Expand All @@ -88,12 +98,19 @@ usage(void)

fprintf(stderr, " --stdout,-s - write file to stdout\n");

fprintf(stderr, " --savepath,-a - save path\n");

fprintf(stderr, " --version,-v - print version number\n");

fprintf(stderr, " --help,-H - print this usage information\n");

fprintf(stderr, "\n");
}


bool file_exists(char *filename) {
struct stat FileStat;
return (stat(filename, &FileStat) == 0);
}
//-----------------------------------------------------------------------

int
Expand All @@ -104,11 +121,13 @@ main(
int opt = 0;

bool writeToStdout = false;
char pngNameWithTimestamp[MAX_NAME_SIZE+1];
char *pngName = DEFAULT_NAME;
bool replace = false;
int32_t requestedWidth = 0;
int32_t requestedHeight = 0;
uint32_t displayNumber = DEFAULT_DISPLAY_NUMBER;
int compression = Z_DEFAULT_COMPRESSION;
int compression = DEFAULT_PNG_COMPRESSION;
int delay = DEFAULT_DELAY;

VC_IMAGE_TYPE_T imageType = VC_IMAGE_RGBA32;
Expand All @@ -120,7 +139,7 @@ main(

//-------------------------------------------------------------------

char *sopts = "c:d:D:Hh:p:w:s";
char *sopts = "c:d:D:Hh:p:w:s:a:v";

struct option lopts[] =
{
Expand All @@ -129,16 +148,23 @@ main(
{ "display", required_argument, NULL, 'D' },
{ "height", required_argument, NULL, 'h' },
{ "help", no_argument, NULL, 'H' },
{ "version", no_argument, NULL, 'v' },
{ "pngname", required_argument, NULL, 'p' },
{ "width", required_argument, NULL, 'w' },
{ "stdout", no_argument, NULL, 's' },
{ "savepath", required_argument, NULL, 'a' },
{ NULL, no_argument, NULL, 0 }
};

while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1)
{
switch (opt)
{
case 'a':

chdir(optarg);
break;

case 'c':

compression = atoi(optarg);
Expand Down Expand Up @@ -168,6 +194,7 @@ main(
case 'p':

pngName = optarg;
replace = true;
break;

case 'w':
Expand All @@ -180,6 +207,11 @@ main(
writeToStdout = true;
break;

case 'v':

fprintf(stderr, "%s (%s)\n", program, VERSION);
exit(EXIT_SUCCESS);

case 'H':
default:

Expand Down Expand Up @@ -522,8 +554,21 @@ main(
}
else
{
pngfp = fopen(pngName, "wb");
if (!replace && file_exists(pngName))
{
time_t rawtime;
struct tm *ltime;
time(&rawtime);
ltime = localtime(&rawtime);
int nLen = strlen(DEFAULT_NAME_START);

strcpy(pngNameWithTimestamp, DEFAULT_NAME_START);
strftime(&pngNameWithTimestamp[nLen], MAX_NAME_SIZE-nLen,
"_%Y%m%d-%H%M%S.png",ltime);
pngName = pngNameWithTimestamp;
}

pngfp = fopen(pngName, "wb");
if (pngfp == NULL)
{
fprintf(stderr,
Expand Down
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.