Skip to content

Instantly share code, notes, and snippets.

@x-zvf
Created December 15, 2025 10:37
Show Gist options
  • Select an option

  • Save x-zvf/f0553b2356d75fd3ea708169144b10ec to your computer and use it in GitHub Desktop.

Select an option

Save x-zvf/f0553b2356d75fd3ea708169144b10ec to your computer and use it in GitHub Desktop.
Pixelflut client Scaffold in C using libim2
/*
* PixelFlut client scaffold using Imlib2
* Install:
* debian/ubuntu: apt install libimlib2
* arch: pacman -S imlib2
* fedora: dnf install imlib2
* Compile: `gcc -o pc file.c -Wall -Wextra -lImlib2`
* Usage: ./pc <hostip> <port> <file_path> <x> <y> <repeat>
* hostip: IP address of the server
* port: port of the server (should be 1337)
* file_path: path to the image file
* x, y: coordinates of the image on the server
* repeat: how many times to send the image (0 for infinite)
* (c) 2023-2025 Péter Bohner
* License: MIT
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <Imlib2.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/types.h>
uint32_t *get_pixelbuf(char *file_path, int *width, int *height) {
Imlib_Image image = imlib_load_image(file_path);
if (!image) {
printf("Error: cannot load image\n");
exit(1);
}
imlib_context_set_image(image);
*width = imlib_image_get_width();
*height = imlib_image_get_height();
uint32_t *argbbuf = imlib_image_get_data_for_reading_only();
if (!argbbuf) {
printf("Error: cannot get image data\n");
exit(1);
}
return argbbuf;
}
int connect_to_server(char *host, int port) {
// TODO: connect to server, return fd
(void)host;
(void)port;
return 0;
}
void flood(int fd, uint32_t const *argbbuf, int w, int h, int x, int y) {
// TODO: send pixels to server
// The (w * h) image should appear at (x, y) on the server
// argbbuf contains the loaded image in ARGB format (layed out
// row-by-row). For example, pixel (x=0, y=1) is at argbbuf[width].
// If it were pure green, argbbuf[width] would be 0x__00ff00,
// If it were violet, argbbuf[width] would be 0x__ff00ff,
// where __ is the alpha channel, which can be ignored.
(void)fd;
(void)argbbuf;
(void)w;
(void)h;
(void)x;
(void)y;
}
int main(int argc, char *argv[]) {
if (argc != 7) {
printf("Usage: %s <hostip> <port> <image_file_path> <origin-x> <origin-y> "
"<repeat count>\n",
argv[0]);
return 1;
}
char *host = argv[1];
int port = atoi(argv[2]);
char *file_path = argv[3];
int x = atoi(argv[4]);
int y = atoi(argv[5]);
int repeat = atoi(argv[6]);
int width, height;
uint32_t *argbbuf = get_pixelbuf(file_path, &width, &height);
int fd = connect_to_server(host, port);
if (fd < 0) {
return 1;
}
for (int i = 0; i < repeat || repeat == 0; i++) {
flood(fd, argbbuf, width, height, x, y);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment