]> 4ch.mooo.com Git - 16.git/blob - src/lib/planar.c
8b9f004204fbee2bb74b2096706de631fea2ffd6
[16.git] / src / lib / planar.c
1 /*\r
2  * Implimentation of the planar buffer files.\r
3  */\r
4 #include <stdlib.h>\r
5 #include "planar.h"\r
6 \r
7 /* creates a planar buffer from the bitmap data.\r
8    The planar buffer is dynamically allocated, and should\r
9    be destroyed with the planar_buf_free function when no longer\r
10    needed.\r
11  */\r
12 planar_buf_t *\r
13 planar_buf_from_bitmap(bitmap_t *b) {\r
14     planar_buf_t *p;\r
15     int plane, bi, pi, x, y;\r
16 \r
17     /* allocate the buffer */\r
18     p = planar_buf_alloc(b->width, b->height);\r
19 \r
20     /* copy the bitmap data into the planar format */\r
21     bi=0;\r
22     pi=0;\r
23     for(y=0; y < b->height; y++) {\r
24         /* start on the first plane */\r
25         plane=0;\r
26         for(x=0; x < b->width; x++) {\r
27             /* copy to each plane */\r
28             p->plane[plane++][pi]=b->data[bi++];\r
29 \r
30             /* handle the completion of 4 planes. */\r
31             if(plane==4) {\r
32                 plane=0;\r
33                 pi++;\r
34             }\r
35         }\r
36 \r
37         /* correct for images not divisible by 4 */\r
38         if(plane) pi++;\r
39     }\r
40 \r
41     return p;\r
42 }\r
43 \r
44 \r
45 /* allocates a planar buffer with specified dimensions */\r
46 planar_buf_t *\r
47 planar_buf_alloc(word width, word height) {\r
48     planar_buf_t *p;\r
49     int i;\r
50 \r
51     /* allocate the structure and populate sizes */\r
52     p=malloc(sizeof(planar_buf_t));\r
53     p->width  = width;\r
54     p->height = height;\r
55     p->pwidth = width / 4 + (width%4 ? 1 : 0);\r
56 \r
57     /* allocate the planes */\r
58     for(i=0; i<4; i++) {\r
59         p->plane[i] = malloc(p->height * p->pwidth);\r
60     }\r
61 \r
62     return p;\r
63 }\r
64 \r
65 \r
66 /* deallocates a planar buffer */\r
67 void\r
68 planar_buf_free(planar_buf_t *p) {\r
69     int i;\r
70 \r
71     /* free the planes */\r
72     for(i=0; i<4; i++) {\r
73         free(p->plane[i]);\r
74     }\r
75 \r
76     /* free the structure */\r
77     free(p);\r
78 }\r