]> 4ch.mooo.com Git - 16.git/blob - src/lib/planar.c
pbuf????
[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 <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 huge *\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         word q;\r
38 \r
39         /* allocate the buffer */\r
40         p = planar_buf_alloc(b->width, b->height);\r
41 \r
42         /* copy the bitmap data into the planar format */\r
43         bi=0;\r
44         pi=0;\r
45         for(y=0; y < b->height; y++) {\r
46         /* start on the first plane */\r
47         plane=0;\r
48         for(x=0; x < b->width; x++) {\r
49                 /* copy to each plane */\r
50                 p->plane[plane++][pi]=b->data[bi++];\r
51 \r
52                 /* handle the completion of 4 planes. */\r
53                 if(plane==4) {\r
54                         plane=0;\r
55                         pi++;\r
56                 }\r
57         }\r
58 \r
59         /* correct for images not divisible by 4 */\r
60         if(plane) pi++;\r
61         }\r
62 \r
63         p->palette = modexNewPal();\r
64         for(;q<PALSIZE;q++)\r
65         {\r
66                 p->palette[q]=b->palette[q];\r
67         }\r
68 \r
69         return p;\r
70 }\r
71 \r
72 \r
73 /* allocates a planar buffer with specified dimensions */\r
74 planar_buf_t huge *\r
75 planar_buf_alloc(word width, word height) {\r
76         planar_buf_t *p;\r
77         int i;\r
78 \r
79         /* allocate the structure and populate sizes */\r
80         p=malloc(sizeof(planar_buf_t));\r
81         p->width  = width;\r
82         p->height = height;\r
83         p->pwidth = width / 4 + (width%4 ? 1 : 0);\r
84 \r
85         /* allocate the planes */\r
86         for(i=0; i<4; i++) {\r
87         p->plane[i] = malloc(p->height * p->pwidth);\r
88         }\r
89 \r
90         return p;\r
91 }\r
92 \r
93 \r
94 /* deallocates a planar buffer */\r
95 void\r
96 planar_buf_free(planar_buf_t *p) {\r
97         int i;\r
98 \r
99         /* free the planes */\r
100         for(i=0; i<4; i++) {\r
101         free(p->plane[i]);\r
102         }\r
103 \r
104         /* free the structure */\r
105         free(p);\r
106 }\r
107 \r
108 /*      non pointer version     */\r
109 planar_buf_t planar_buf_from_bitmap0(bitmap_t *b) {\r
110         planar_buf_t p;\r
111         int plane, bi, pi, x, y;\r
112         word q=0;\r
113 \r
114         /* allocate the buffer */\r
115         p = *planar_buf_alloc(b->width, b->height);\r
116 \r
117         /* copy the bitmap data into the planar format */\r
118         bi=0;\r
119         pi=0;\r
120         for(y=0; y < b->height; y++) {\r
121         /* start on the first plane */\r
122         plane=0;\r
123         for(x=0; x < b->width; x++) {\r
124                 /* copy to each plane */\r
125                 p.plane[plane++][pi]=b->data[bi++];\r
126 \r
127                 /* handle the completion of 4 planes. */\r
128                 if(plane==4) {\r
129                         plane=0;\r
130                         pi++;\r
131                 }\r
132         }\r
133 \r
134         /* correct for images not divisible by 4 */\r
135         if(plane) pi++;\r
136         }\r
137         p.palette = modexNewPal();\r
138         for(;q<PALSIZE;q++)\r
139         {\r
140                 p.palette[q]=b->palette[q];\r
141         }\r
142 \r
143         return p;\r
144 }\r