forked from AndrewFromMelbourne/raspi2png
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspi2png.c
596 lines (450 loc) · 15.1 KB
/
raspi2png.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//-------------------------------------------------------------------------
//
// The MIT License (MIT)
//
// Copyright (c) 2014 Andrew Duncan
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-------------------------------------------------------------------------
#define _GNU_SOURCE
#include <getopt.h>
#include <math.h>
#include <png.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <zlib.h>
#include "bcm_host.h"
//-----------------------------------------------------------------------
#ifndef ALIGN_TO_16
#define ALIGN_TO_16(x) ((x + 15) & ~15)
#endif
//-----------------------------------------------------------------------
#define DEFAULT_DELAY 0
#define DEFAULT_DISPLAY_NUMBER 0
#define DEFAULT_NAME "snapshot.png"
//-----------------------------------------------------------------------
const char* program = NULL;
//-----------------------------------------------------------------------
void
usage(void)
{
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, "\n");
fprintf(stderr, " --pngname,-p - name of png file to create ");
fprintf(stderr, "(default is %s)\n", DEFAULT_NAME);
fprintf(stderr, " --height,-h - image height ");
fprintf(stderr, "(default is screen height)\n");
fprintf(stderr, " --width,-w - image width ");
fprintf(stderr, "(default is screen width)\n");
fprintf(stderr, " --compression,-c - PNG compression level ");
fprintf(stderr, "(0 - 9)\n");
fprintf(stderr, " --delay,-d - delay in seconds ");
fprintf(stderr, "(default %d)\n", DEFAULT_DELAY);
fprintf(stderr, " --display,-D - Raspberry Pi display number ");
fprintf(stderr, "(default %d)\n", DEFAULT_DISPLAY_NUMBER);
fprintf(stderr, " --stdout,-s - write file to stdout\n");
fprintf(stderr, " --help,-H - print this usage information\n");
fprintf(stderr, "\n");
}
//-----------------------------------------------------------------------
int
main(
int argc,
char *argv[])
{
int opt = 0;
bool writeToStdout = false;
char *pngName = DEFAULT_NAME;
int32_t requestedWidth = 0;
int32_t requestedHeight = 0;
uint32_t displayNumber = DEFAULT_DISPLAY_NUMBER;
int compression = Z_DEFAULT_COMPRESSION;
int delay = DEFAULT_DELAY;
VC_IMAGE_TYPE_T imageType = VC_IMAGE_RGBA32;
int8_t dmxBytesPerPixel = 4;
int result = 0;
program = basename(argv[0]);
//-------------------------------------------------------------------
char *sopts = "c:d:D:Hh:p:w:s";
struct option lopts[] =
{
{ "compression", required_argument, NULL, 'c' },
{ "delay", required_argument, NULL, 'd' },
{ "display", required_argument, NULL, 'D' },
{ "height", required_argument, NULL, 'h' },
{ "help", no_argument, NULL, 'H' },
{ "pngname", required_argument, NULL, 'p' },
{ "width", required_argument, NULL, 'w' },
{ "stdout", no_argument, NULL, 's' },
{ NULL, no_argument, NULL, 0 }
};
while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1)
{
switch (opt)
{
case 'c':
compression = atoi(optarg);
if ((compression < 0) || (compression > 9))
{
compression = Z_DEFAULT_COMPRESSION;
}
break;
case 'd':
delay = atoi(optarg);
break;
case 'D':
displayNumber = atoi(optarg);
break;
case 'h':
requestedHeight = atoi(optarg);
break;
case 'p':
pngName = optarg;
break;
case 'w':
requestedWidth = atoi(optarg);
break;
case 's':
writeToStdout = true;
break;
case 'H':
default:
usage();
if (opt == 'H')
{
exit(EXIT_SUCCESS);
}
else
{
exit(EXIT_FAILURE);
}
break;
}
}
//-------------------------------------------------------------------
bcm_host_init();
//-------------------------------------------------------------------
//
// When the display is rotate (either 90 or 270 degrees) we need to
// swap the width and height of the snapshot
//
char response[1024];
int displayRotated = 0;
if (vc_gencmd(response, sizeof(response), "get_config int") == 0)
{
vc_gencmd_number_property(response,
"display_rotate",
&displayRotated);
}
//-------------------------------------------------------------------
if (delay)
{
sleep(delay);
}
//-------------------------------------------------------------------
DISPMANX_DISPLAY_HANDLE_T displayHandle
= vc_dispmanx_display_open(displayNumber);
if (displayHandle == 0)
{
fprintf(stderr,
"%s: unable to open display %d\n",
program,
displayNumber);
exit(EXIT_FAILURE);
}
DISPMANX_MODEINFO_T modeInfo;
result = vc_dispmanx_display_get_info(displayHandle, &modeInfo);
if (result != 0)
{
fprintf(stderr, "%s: unable to get display information\n", program);
exit(EXIT_FAILURE);
}
int32_t pngWidth = modeInfo.width;
int32_t pngHeight = modeInfo.height;
if (requestedWidth > 0)
{
pngWidth = requestedWidth;
if (requestedHeight == 0)
{
double numerator = modeInfo.height * requestedWidth;
double denominator = modeInfo.width;
pngHeight = (int32_t)ceil(numerator / denominator);
}
}
if (requestedHeight > 0)
{
pngHeight = requestedHeight;
if (requestedWidth == 0)
{
double numerator = modeInfo.width * requestedHeight;
double denominator = modeInfo.height;
pngWidth = (int32_t)ceil(numerator / denominator);
}
}
//-------------------------------------------------------------------
// only need to check low bit of displayRotated (value of 1 or 3).
// If the display is rotated either 90 or 270 degrees (value 1 or 3)
// the width and height need to be transposed.
int32_t dmxWidth = pngWidth;
int32_t dmxHeight = pngHeight;
if (displayRotated & 1)
{
dmxWidth = pngHeight;
dmxHeight = pngWidth;
}
int32_t dmxPitch = dmxBytesPerPixel * ALIGN_TO_16(dmxWidth);
void *dmxImagePtr = malloc(dmxPitch * dmxHeight);
if (dmxImagePtr == NULL)
{
fprintf(stderr, "%s: unable to allocated image buffer\n", program);
exit(EXIT_FAILURE);
}
//-------------------------------------------------------------------
uint32_t vcImagePtr = 0;
DISPMANX_RESOURCE_HANDLE_T resourceHandle;
resourceHandle = vc_dispmanx_resource_create(imageType,
dmxWidth,
dmxHeight,
&vcImagePtr);
result = vc_dispmanx_snapshot(displayHandle,
resourceHandle,
DISPMANX_NO_ROTATE);
if (result != 0)
{
vc_dispmanx_resource_delete(resourceHandle);
vc_dispmanx_display_close(displayHandle);
fprintf(stderr, "%s: vc_dispmanx_snapshot() failed\n", program);
exit(EXIT_FAILURE);
}
VC_RECT_T rect;
result = vc_dispmanx_rect_set(&rect, 0, 0, dmxWidth, dmxHeight);
if (result != 0)
{
vc_dispmanx_resource_delete(resourceHandle);
vc_dispmanx_display_close(displayHandle);
fprintf(stderr, "%s: vc_dispmanx_rect_set() failed\n", program);
exit(EXIT_FAILURE);
}
result = vc_dispmanx_resource_read_data(resourceHandle,
&rect,
dmxImagePtr,
dmxPitch);
if (result != 0)
{
vc_dispmanx_resource_delete(resourceHandle);
vc_dispmanx_display_close(displayHandle);
fprintf(stderr,
"%s: vc_dispmanx_resource_read_data() failed\n",
program);
exit(EXIT_FAILURE);
}
vc_dispmanx_resource_delete(resourceHandle);
vc_dispmanx_display_close(displayHandle);
//-------------------------------------------------------------------
// Convert from RGBA (32 bit) to RGB (24 bit)
int8_t pngBytesPerPixel = 3;
int32_t pngPitch = pngBytesPerPixel * pngWidth;
void *pngImagePtr = malloc(pngPitch * pngHeight);
int32_t j = 0;
for (j = 0 ; j < pngHeight ; j++)
{
int32_t dmxXoffset = 0;
int32_t dmxYoffset = 0;
switch (displayRotated & 3)
{
case 0: // 0 degrees
if (displayRotated & 0x20000) // flip vertical
{
dmxYoffset = (dmxHeight - j - 1) * dmxPitch;
}
else
{
dmxYoffset = j * dmxPitch;
}
break;
case 1: // 90 degrees
if (displayRotated & 0x20000) // flip vertical
{
dmxXoffset = j * dmxBytesPerPixel;
}
else
{
dmxXoffset = (dmxWidth - j - 1) * dmxBytesPerPixel;
}
break;
case 2: // 180 degrees
if (displayRotated & 0x20000) // flip vertical
{
dmxYoffset = j * dmxPitch;
}
else
{
dmxYoffset = (dmxHeight - j - 1) * dmxPitch;
}
break;
case 3: // 270 degrees
if (displayRotated & 0x20000) // flip vertical
{
dmxXoffset = (dmxWidth - j - 1) * dmxBytesPerPixel;
}
else
{
dmxXoffset = j * dmxBytesPerPixel;
}
break;
}
int32_t i = 0;
for (i = 0 ; i < pngWidth ; i++)
{
uint8_t *pngPixelPtr = pngImagePtr
+ (i * pngBytesPerPixel)
+ (j * pngPitch);
switch (displayRotated & 3)
{
case 0: // 0 degrees
if (displayRotated & 0x10000) // flip horizontal
{
dmxXoffset = (dmxWidth - i - 1) * dmxBytesPerPixel;
}
else
{
dmxXoffset = i * dmxBytesPerPixel;
}
break;
case 1: // 90 degrees
if (displayRotated & 0x10000) // flip horizontal
{
dmxYoffset = (dmxHeight - i - 1) * dmxPitch;
}
else
{
dmxYoffset = i * dmxPitch;
}
break;
case 2: // 180 degrees
if (displayRotated & 0x10000) // flip horizontal
{
dmxXoffset = i * dmxBytesPerPixel;
}
else
{
dmxXoffset = (dmxWidth - i - 1) * dmxBytesPerPixel;
}
break;
case 3: // 270 degrees
if (displayRotated & 0x10000) // flip horizontal
{
dmxYoffset = i * dmxPitch;
}
else
{
dmxYoffset = (dmxHeight - i - 1) * dmxPitch;
}
break;
}
uint8_t *dmxPixelPtr = dmxImagePtr + dmxXoffset + dmxYoffset;
memcpy(pngPixelPtr, dmxPixelPtr, 3);
}
}
free(dmxImagePtr);
dmxImagePtr = NULL;
//-------------------------------------------------------------------
png_structp pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
if (pngPtr == NULL)
{
fprintf(stderr,
"%s: unable to allocated PNG write structure\n",
program);
exit(EXIT_FAILURE);
}
png_infop infoPtr = png_create_info_struct(pngPtr);
if (infoPtr == NULL)
{
fprintf(stderr,
"%s: unable to allocated PNG info structure\n",
program);
exit(EXIT_FAILURE);
}
if (setjmp(png_jmpbuf(pngPtr)))
{
fprintf(stderr, "%s: unable to create PNG\n", program);
exit(EXIT_FAILURE);
}
FILE *pngfp = NULL;
if (writeToStdout)
{
pngfp = stdout;
}
else
{
pngfp = fopen(pngName, "wb");
if (pngfp == NULL)
{
fprintf(stderr,
"%s: unable to create %s - %s\n",
program,
pngName,
strerror(errno));
exit(EXIT_FAILURE);
}
}
png_init_io(pngPtr, pngfp);
png_set_IHDR(
pngPtr,
infoPtr,
pngWidth,
pngHeight,
8,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
if (compression != Z_DEFAULT_COMPRESSION)
{
png_set_compression_level(pngPtr, compression);
}
png_write_info(pngPtr, infoPtr);
int y = 0;
for (y = 0; y < pngHeight; y++)
{
png_write_row(pngPtr, pngImagePtr + (pngPitch * y));
}
png_write_end(pngPtr, NULL);
png_destroy_write_struct(&pngPtr, &infoPtr);
if (pngfp != stdout)
{
fclose(pngfp);
}
//-------------------------------------------------------------------
free(pngImagePtr);
pngImagePtr = NULL;
return 0;
}