---- Bytes Description 9 ASCII string: "imagefile" 2 Width of the image stored in network byte order (big-endian) 2 Height of the image stored in network byte order (big-endian) Then, (width*height) pixels arranged in height scanlines, where each pixel is four bytes. Each byte represents red, green, blue, and alpha respectively. This function reads an image: char * readimage(int fd, uint16_t *w, uint16_t *h) { char hdr[13]; char *data; int len; if (read(fd, hdr, 13) != 13 || strcmp(hdr, "imagefile")) return NULL; *w = ntohs(hdr[9]); *h = ntohs(hdr[11]); len = (*w) * (*h) * 4; if (!(data = malloc(len)) || read(fd, data, len) != len) { free(data); return NULL; } return data; } This function writes an image: int writeimage(int fd, uint16_t w, uint16_t h, char *data) { uint16_t nw = htons(w); uint16_t nh = htons(h); return (write(fd, "imagefile", 9) == 9 && write(fd, &nw, 2) == 2 && write(fd, &nh, 2) == 2 && write(fd, data, w*h*4) == w*h*4); } We also avoid *printf and stdio.h. --MarkusReceived on Wed Jul 16 2014 - 15:28:44 CEST
This archive was generated by hypermail 2.3.0 : Wed Jul 16 2014 - 15:36:07 CEST