]> 4ch.mooo.com Git - 16.git/blob - src/lib/planar.c
wwww
[16.git] / src / lib / planar.c
1 /* Project 16 Source Code~
2  * Copyright (C) 2012-2015 sparky4 & pngwen & andrius4669
3  *
4  * This file is part of Project 16.
5  *
6  * Project 16 is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Project 16 is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,
19  * Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22 /*\r
23  * Implimentation of the planar buffer files.\r
24  */\r
25 #include <stdlib.h>\r
26 #include "src/lib/planar.h"\r
27 \r
28 /* creates a planar buffer from the bitmap data.\r
29    The planar buffer is dynamically allocated, and should\r
30    be destroyed with the planar_buf_free function when no longer\r
31    needed.\r
32  */\r
33 planar_buf_t *\r
34 planar_buf_from_bitmap(bitmap_t *b) {\r
35     planar_buf_t *p;\r
36     int plane, bi, pi, x, y;\r
37 \r
38     /* allocate the buffer */\r
39     p = planar_buf_alloc(b->width, b->height);\r
40 \r
41     /* copy the bitmap data into the planar format */\r
42     bi=0;\r
43     pi=0;\r
44     for(y=0; y < b->height; y++) {\r
45         /* start on the first plane */\r
46         plane=0;\r
47         for(x=0; x < b->width; x++) {\r
48             /* copy to each plane */\r
49             p->plane[plane++][pi]=b->data[bi++];\r
50 \r
51             /* handle the completion of 4 planes. */\r
52             if(plane==4) {\r
53                 plane=0;\r
54                 pi++;\r
55             }\r
56         }\r
57 \r
58         /* correct for images not divisible by 4 */\r
59         if(plane) pi++;\r
60     }\r
61 \r
62     return p;\r
63 }\r
64 \r
65 \r
66 /* allocates a planar buffer with specified dimensions */\r
67 planar_buf_t *\r
68 planar_buf_alloc(word width, word height) {\r
69     planar_buf_t *p;\r
70     int i;\r
71 \r
72     /* allocate the structure and populate sizes */\r
73     p=malloc(sizeof(planar_buf_t));\r
74     p->width  = width;\r
75     p->height = height;\r
76     p->pwidth = width / 4 + (width%4 ? 1 : 0);\r
77 \r
78     /* allocate the planes */\r
79     for(i=0; i<4; i++) {\r
80         p->plane[i] = malloc(p->height * p->pwidth);\r
81     }\r
82 \r
83     return p;\r
84 }\r
85 \r
86 \r
87 /* deallocates a planar buffer */\r
88 void\r
89 planar_buf_free(planar_buf_t *p) {\r
90     int i;\r
91 \r
92     /* free the planes */\r
93     for(i=0; i<4; i++) {\r
94         free(p->plane[i]);\r
95     }\r
96 \r
97     /* free the structure */\r
98     free(p);\r
99 }\r