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
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include "pamdump.h"
void dump_pam(const uint32_t *ptr, unsigned pitch, bool alpha,
unsigned x1, unsigned y1, unsigned x2, unsigned y2,
const char *fmt, ...)
{
unsigned char buf[16*1024 + 16384];
unsigned x, y, i;
char fn[160];
va_list ap;
int fd, len;
va_start(ap, fmt);
len = vsnprintf(fn, sizeof(fn), fmt, ap);
va_end(ap);
if (len < 0 || len >= sizeof(fn))
return;
fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
return;
i = sprintf((char *)buf, "P7\n"
"WIDTH %u\n"
"HEIGHT %u\n"
"DEPTH %u\n"
"MAXVAL 255\n"
"TUPLTYPE RGB%s\n"
"ENDHDR\n",
x2 - x1, y2 - y1, 3 + alpha, alpha ? "_ALPHA" : "");
for (y = y1; y < y2; y++) {
const uint32_t *p = (const uint32_t *)((const char *)ptr + (y * pitch));
for (x = x1; x < x2; x++) {
buf[i++] = p[x] >> 16; // R
buf[i++] = p[x] >> 8; // G
buf[i++] = p[x]; // B
if (alpha)
buf[i++] = p[x] >> 24; // A
}
if (i >= 16*1024) {
write(fd, buf, i);
i = 0;
}
}
if (i)
write(fd, buf, i);
close(fd);
}
|