|
| 1 | +/* |
| 2 | +
|
| 3 | +pidvbip - tvheadend client for the Raspberry Pi |
| 4 | +
|
| 5 | +(C) Dave Chapman 2012-2013 |
| 6 | +
|
| 7 | +This program is free software; you can redistribute it and/or modify |
| 8 | +it under the terms of the GNU General Public License as published by |
| 9 | +the Free Software Foundation; either version 2 of the License, or |
| 10 | +(at your option) any later version. |
| 11 | +
|
| 12 | +This program is distributed in the hope that it will be useful, |
| 13 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +GNU General Public License for more details. |
| 16 | +
|
| 17 | +You should have received a copy of the GNU General Public License |
| 18 | +along with this program; if not, write to the Free Software |
| 19 | +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | +
|
| 21 | +*/ |
| 22 | + |
| 23 | +#include <stdio.h> |
| 24 | +#include <stdlib.h> |
| 25 | +#include <stdarg.h> |
| 26 | +#include <assert.h> |
| 27 | +#include <unistd.h> |
| 28 | +#include <sys/time.h> |
| 29 | + |
| 30 | +#include "bcm_host.h" |
| 31 | + |
| 32 | +/* |
| 33 | + Based on sample code posted here by "hanzelpeter":: |
| 34 | +
|
| 35 | + http://www.raspberrypi.org/phpBB3/viewtopic.php?p=376546#p376546 |
| 36 | +*/ |
| 37 | + |
| 38 | +void save_snapshot(void) |
| 39 | +{ |
| 40 | + DISPMANX_DISPLAY_HANDLE_T display; |
| 41 | + DISPMANX_MODEINFO_T info; |
| 42 | + DISPMANX_RESOURCE_HANDLE_T resource; |
| 43 | + VC_IMAGE_TYPE_T type = VC_IMAGE_RGB888; |
| 44 | + VC_IMAGE_TRANSFORM_T transform = 0; |
| 45 | + VC_RECT_T rect; |
| 46 | + void *image; |
| 47 | + uint32_t vc_image_ptr; |
| 48 | + int ret; |
| 49 | + uint32_t screen = 0; |
| 50 | + |
| 51 | + fprintf(stderr,"\nWriting snapshot...\n"); |
| 52 | + //fprintf(stderr,"Open display[%i]...\n", screen ); |
| 53 | + display = vc_dispmanx_display_open( screen ); |
| 54 | + |
| 55 | + ret = vc_dispmanx_display_get_info(display, &info); |
| 56 | + assert(ret == 0); |
| 57 | + //fprintf(stderr,"Display is %d x %d\n", info.width, info.height ); |
| 58 | + |
| 59 | + image = calloc( 1, info.width * 3 * info.height ); |
| 60 | + |
| 61 | + assert(image); |
| 62 | + |
| 63 | + resource = vc_dispmanx_resource_create(type, info.width, info.height,&vc_image_ptr); |
| 64 | + |
| 65 | + vc_dispmanx_snapshot(display, resource, transform); |
| 66 | + |
| 67 | + vc_dispmanx_rect_set(&rect, 0, 0, info.width, info.height); |
| 68 | + vc_dispmanx_resource_read_data(resource, &rect, image, info.width*3); |
| 69 | + |
| 70 | + char* home = getenv("HOME"); |
| 71 | + char filename[1024]; |
| 72 | + if (!home) |
| 73 | + home = "/tmp"; |
| 74 | + |
| 75 | + snprintf(filename,sizeof(filename),"%s/pidvbip-%u.ppm",home,(unsigned int)time(NULL)); |
| 76 | + FILE *fp = fopen(filename, "wb"); |
| 77 | + fprintf(fp, "P6\n%d %d\n255\n", info.width, info.height); |
| 78 | + fwrite(image, info.width*3*info.height, 1, fp); |
| 79 | + fclose(fp); |
| 80 | + |
| 81 | + fprintf(stderr,"\nSnapshot written to %s\n",filename); |
| 82 | + |
| 83 | + ret = vc_dispmanx_resource_delete( resource ); |
| 84 | + assert( ret == 0 ); |
| 85 | + ret = vc_dispmanx_display_close(display ); |
| 86 | + assert( ret == 0 ); |
| 87 | + |
| 88 | + free(image); |
| 89 | +} |
0 commit comments