FRIGN wrote:
> The writing-function is rather trivial.
> Now, what puzzles me is why no explanation is given on how the data
> itself should be stored. It says RGBA, so I suppose he meant
Thanks for the feedback. The header is strict to avoid complex text
handling. I have attached a script to convert any image to this format,
using ImageMagick.
Revised spec
------------
This is an easy-to-parse image format that works well in pipelines.
Inspired by Netpbm and the Plan 9 image format.
Bytes Description
9 ASCII string: "imagefile"
1 Any byte.
9 Right-justified, blank-padded ASCII string containing the width.
1 Any byte.
9 Right-justified, blank-padded ASCII string containing the height.
1 Any byte.
Then, (width*height) pixels, where each pixel is four bytes. Each byte
represents red, green, blue, and alpha respectively. The X axis runs
fastest.
This function reads an image:
char *
readimage(int fd, int *w, int *h)
{
char hdr[30];
char *data;
int len;
if (read(fd, hdr, 30) != 30)
return NULL;
hdr[9] = '\0';
hdr[19] = '\0';
hdr[29] = '\0';
if (strcmp(hdr, "imagefile") != 0)
return NULL;
*w = atoi(hdr+10);
*h = atoi(hdr+20);
len = *w * *h * 4;
data = malloc(len);
if (!data)
return NULL;
if (read(fd, data, len) != len) {
free(data);
return NULL;
}
return data;
}
This function writes an image:
int
writeimage(int fd, int w, int h, char *data)
{
char hdr[30];
if (snprintf(hdr, 30, "imagefile %9d %9d ", w, h) != 30)
return -1;
if (write(fd, hdr, 30) != 30)
return -1;
if (write(fd, data, w*h*4) != w*h*4)
return -1;
return 0;
}
Received on Wed Jul 16 2014 - 10:42:31 CEST