Write simple farbfeld-writing module
This commit is contained in:
84
src/ff.c
Normal file
84
src/ff.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "ff.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define MAGIC "farbfeld"
|
||||
#define MAGICLEN 8
|
||||
|
||||
#define BUFSIZE 4096
|
||||
|
||||
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
||||
|
||||
static uint8_t buf[BUFSIZE];
|
||||
|
||||
static uint8_t *loadpix(uint8_t *p, const pix_t *pix, unsigned n)
|
||||
{
|
||||
for (unsigned i = 0; i < n; ++i) {
|
||||
*((uint16_t *)p) = htons(pix[i].r);
|
||||
p += sizeof(uint16_t);
|
||||
*((uint16_t *)p) = htons(pix[i].g);
|
||||
p += sizeof(uint16_t);
|
||||
*((uint16_t *)p) = htons(pix[i].b);
|
||||
p += sizeof(uint16_t);
|
||||
*((uint16_t *)p) = htons(pix[i].a);
|
||||
p += sizeof(uint16_t);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static void writebuf(int fd, size_t size)
|
||||
{
|
||||
while (1) {
|
||||
errno = 0;
|
||||
const ssize_t res = write(fd, buf, size);
|
||||
|
||||
if (res == (ssize_t)size) {
|
||||
return;
|
||||
} else if (res > 0) {
|
||||
fputs(
|
||||
"Error writing image: Couldn't write entire buffer"
|
||||
" (storage may be full)\n",
|
||||
stderr);
|
||||
exit(1);
|
||||
} else {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
} else {
|
||||
perror("Error writing image");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ff_write(int fd, img_t img)
|
||||
{
|
||||
uint8_t *p = buf;
|
||||
|
||||
memcpy(p, MAGIC, MAGICLEN);
|
||||
p += MAGICLEN;
|
||||
*((uint32_t *)p) = htonl(img.w);
|
||||
p += sizeof(uint32_t);
|
||||
*((uint32_t *)p) = htonl(img.h);
|
||||
p += sizeof(uint32_t);
|
||||
|
||||
const pix_t *pix = img.pix;
|
||||
unsigned rem = img.w * img.h;
|
||||
while (rem > 0) {
|
||||
const unsigned cap = (BUFSIZE - (p - buf)) / sizeof(pix_t);
|
||||
const unsigned n = MIN(rem, cap);
|
||||
|
||||
p = loadpix(p, pix, n);
|
||||
pix += n;
|
||||
rem -= n;
|
||||
|
||||
writebuf(fd, p - buf);
|
||||
p = buf;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user