]> 4ch.mooo.com Git - 16.git/blob - src/lib/planar.c
cleaned up the repo from debugging watcom2 ^^
[16.git] / src / lib / planar.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123\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 /* creates a planar buffer from the bitmap data.\r
32    The planar buffer is dynamically allocated, and should\r
33    be destroyed with the planar_buf_free function when no longer\r
34    needed.\r
35  */\r
36 planar_buf_t huge *\r
37 planar_buf_from_bitmap(bitmap_t *b) {\r
38         planar_buf_t *p;\r
39         int plane, bi, pi, x, y;\r
40         word q;\r
41 \r
42         /* allocate the buffer */\r
43         p = planar_buf_alloc(b->width, b->height);\r
44 \r
45         /* copy the bitmap data into the planar format */\r
46         bi=0;\r
47         pi=0;\r
48         for(y=0; y < b->height; y++) {\r
49         /* start on the first plane */\r
50         plane=0;\r
51         for(x=0; x < b->width; x++) {\r
52                 /* copy to each plane */\r
53                 p->plane[plane++][pi]=b->data[bi++];\r
54 \r
55                 /* handle the completion of 4 planes. */\r
56                 if(plane==4) {\r
57                         plane=0;\r
58                         pi++;\r
59                 }\r
60         }\r
61 \r
62         /* correct for images not divisible by 4 */\r
63         if(plane) pi++;\r
64         }\r
65 \r
66         p->palette = modexNewPal();\r
67         for(;q<PALSIZE;q++)\r
68         {\r
69                 p->palette[q]=b->palette[q];\r
70         }\r
71 \r
72         return p;\r
73 }\r
74 \r
75 \r
76 /* allocates a planar buffer with specified dimensions */\r
77 planar_buf_t huge *\r
78 planar_buf_alloc(word width, word height) {\r
79         planar_buf_t *p;\r
80         int i;\r
81 \r
82         /* allocate the structure and populate sizes */\r
83         p=malloc(sizeof(planar_buf_t));\r
84         p->width  = width;\r
85         p->height = height;\r
86         p->pwidth = width / 4 + (width%4 ? 1 : 0);\r
87 \r
88         /* allocate the planes */\r
89         for(i=0; i<4; i++) {\r
90                 p->plane[i] = malloc(p->height * p->pwidth);\r
91         }\r
92 \r
93         return p;\r
94 }\r
95 \r
96 \r
97 /* deallocates a planar buffer */\r
98 void\r
99 planar_buf_free(planar_buf_t *p) {\r
100         int i;\r
101 \r
102         /* free the planes */\r
103         for(i=0; i<4; i++) {\r
104         free(p->plane[i]);\r
105         }\r
106 \r
107         /* free the structure */\r
108         free(p);\r
109 }\r