On Mon, 14 Jul 2014 14:54:42 -0400
Charlie Murphy <cmsmur...@gmail.com> wrote:

> I'm looking for something like this[1] but with conversion tools for
> other formats.

Now, let's take a look at the proposition:

Bytes   Description
9       ASCII string: "imagefile"
1       Blank
9       Right-justified, blank-padded ASCII string containing width.
1       Blank
9       Right-justified, blank-padded ASCII string containing height.
1       Blank

The given example-implementation leaves a lot to ask for. Here's my
approach:

char *
readimage(int fd, int *w, int *h)
{
        size_t total;
        uint8_t *data;

        data = malloc(30);
        if (!data || read(fd, buf, 30) != 30 || strcmp(buf, "imagefile") != 0) {
                free(data);
                return NULL;
        }

        buf[9] = buf[19] = buf[29] = '\0';
        *w = atoi(buf+10);
        *h = atoi(buf+20);
        total = (*w) * (*h) * 4;

        data = realloc(data, total); 
        if (!data || read(fd, data, total) != total) {
                free(data);
                return NULL;
        }
        return data;
}

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

| 8 bit | 8 bit | 8 bit | 8 bit |
|  red  | green |  blue | alpha |

for each pixel. This is rather trivial then, given an unsigned
8-bit-integer can be expressed with his according ASCII-char, basically
turning this image file-format into a string:

imagefile 2 1 5%4&4"31

Let me know what you think. I suppose anything more complex than that will
limit our possibilities and the flexiblity and simplicity of this format.

Cheers

FRIGN

-- 
FRIGN <d...@frign.de>

Reply via email to