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