-
Notifications
You must be signed in to change notification settings - Fork 1
/
pixelflut.c
127 lines (113 loc) · 2.55 KB
/
pixelflut.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
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <signal.h>
#include <getopt.h>
#include "pf_png.h"
#include "socket.h"
static const struct option options_long[] = {
{ "host", required_argument, NULL, 'h' },
{ "port", required_argument, NULL, 'p' },
{ "image", required_argument, NULL, 'i' },
{ "xpos", required_argument, NULL, 'x' },
{ "ypos", required_argument, NULL, 'y' },
{ NULL, 0, NULL, 0 }
};
static const char *optstring = "h:p:i:x:y:";
void usage(const char *name)
{
fprintf(stderr,
"Usage: %s [options]\n"
"\n"
"-h|--host <host> (required)\n"
"-p|--port <port> (required)\n"
"-i|--image <file.png> (required)\n"
"-x|--xpos <num>\n"
"-y|--ypos <num>\n",
name);
}
int main(int argc, char *argv[])
{
int c;
char *host = NULL;
char *service = NULL;
char *image = NULL;
int xs = 0, ys = 0;
while ((c = getopt_long(argc, argv, optstring, options_long, NULL)) !=
-1) {
switch (c) {
case 'h':
host = optarg;
break;
case 'p':
service = optarg;
break;
case 'i':
image = optarg;
break;
case 'x':
xs = atoi(optarg);
break;
case 'y':
ys = atoi(optarg);
break;
default:
usage(argv[0]);
exit(1);
}
}
struct addrinfo hints = { 0, AF_UNSPEC, SOCK_STREAM, 0,
0, NULL, NULL, NULL };
struct addrinfo *res = NULL;
int err;
if ((err = getaddrinfo(host, service, &hints, &res))) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
exit(1);
}
if (pf_socket_setup(res, 1) < 0) {
fprintf(stderr, "Error in pf_socket_setup()\n");
exit(1);
}
signal(SIGPIPE, SIG_IGN);
if (pf_png_open(image)) {
fprintf(stderr, "Failed to open file\n");
exit(1);
}
if (pf_png_read()) {
fprintf(stderr, "Failed to read file\n");
exit(1);
}
char *buffer =
calloc(1 + pf_png_height() * pf_png_width() *
snprintf(NULL, 0, "PX %d %d 000000\n",
xs + pf_png_width(),
ys + pf_png_height()),
sizeof(char));
if (buffer == NULL) {
fprintf(stderr, "Could not allocate memory\n");
exit(1);
}
char *p = buffer;
for (unsigned int x = 0; x < pf_png_width(); x++) {
for (unsigned int y = 0; y < pf_png_height(); y++) {
int s = sprintf(p, "PX %d %d %06x\n", x + xs, y + ys,
pf_png_get_rgb(x, y));
p += s;
}
}
if (pf_png_close()) {
fprintf(stderr, "Could not close file\n");
exit(1);
}
size_t s = strlen(buffer);
pf_socket_loop_write(buffer, s);
pf_socket_destroy();
return 0;
}