]> 4ch.mooo.com Git - 16.git/blob - src/lib/modex16/16planar.c
OK IT COMPILES!
[16.git] / src / lib / modex16 / 16planar.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 #include <stdio.h>\r
24 #include <stdlib.h>\r
25 #include <malloc.h>\r
26 #include "src/lib/modex16/16planar.h"\r
27 \r
28 #ifndef PCXHEADER_H\r
29 #define PCXHEADER_H\r
30 static struct pcxHeader {\r
31         byte id;\r
32         byte version;\r
33         byte encoding;\r
34         byte bpp;\r
35         word xmin;\r
36         word ymin;\r
37         word xmax;\r
38         word ymax;\r
39         word hres;\r
40         word vres;\r
41         byte pal16[48];\r
42         byte res1;\r
43         word bpplane;\r
44         word palType;\r
45         word hScreenSize;\r
46         word vScreenSize;\r
47         byte padding[54];\r
48 } head;\r
49 #endif /*PCXHEADER_H*/\r
50 \r
51 static void loadPcxpbufStage1(FILE *file, planar_buf_t *result) {\r
52         int index;\r
53         byte count, val;\r
54         long int pos;\r
55 \r
56         /* read the header */\r
57         fread(&head, sizeof(char), sizeof(struct pcxHeader), file);\r
58 \r
59         /* get the width and height */\r
60         result->width = head.xmax - head.xmin + 1;\r
61         result->height = head.ymax - head.ymin + 1;\r
62 \r
63         /* make sure this  is 8bpp */\r
64         if(head.bpp != 8) {\r
65                 fprintf(stderr, "I only know how to handle 8bpp pcx files!\n");\r
66                 fclose(file);\r
67                 //exit(-2);\r
68         }\r
69 }\r
70 \r
71 static void loadPcxpbufPalette(FILE *file, planar_buf_t *result) {\r
72         byte val;\r
73         int index;\r
74 \r
75         /* handle the palette */\r
76         fseek(file, -769, SEEK_END);\r
77         val = fgetc(file);\r
78         result->palette = modexNewPal();\r
79         if(head.version == 5 && val == 12) {\r
80         /* use the vga palette */\r
81         for(index=0; !feof(file) && index < PAL_SIZE; index++) {\r
82                 val = fgetc(file);\r
83                 result->palette[index] = val >> 2;\r
84         }\r
85         } else {\r
86         /* use the 16 color palette */\r
87         for(index=0; index<48; index++) {\r
88                 result->palette[index]  = head.pal16[index];\r
89         }\r
90         }\r
91 }\r
92 \r
93 /*      sparky4's functions~    */\r
94 planar_buf_t planarLoadPcx(char *filename)\r
95 {\r
96         FILE *file;\r
97         planar_buf_t result;\r
98         dword bufSize;\r
99         int index, plane, x, y;\r
100         byte count, val;\r
101         word q;\r
102 \r
103         /* open the PCX file for reading */\r
104         file = fopen(filename, "rb");\r
105         if(!file) {\r
106                 fprintf(stderr, "Could not open %s for reading.\n", filename);\r
107                 //exit(-2);\r
108         }\r
109 \r
110         /* load the first part of the pcx file */\r
111         loadPcxpbufStage1(file, &result);\r
112 \r
113         /* allocate the buffer */\r
114         bufSize = (/*(dword)*/result.width * result.height);\r
115         //result = pbuf_alloc(result.width, result.height);\r
116         if(!result.plane[0]) {\r
117                 fprintf(stderr, "Could not allocate memory for bitmap data.");\r
118                 fclose(file);\r
119                 //exit(-1);\r
120         }\r
121 \r
122         /*  read the buffer in */\r
123         index = 0;\r
124         do {\r
125         /* get the run length and the value */\r
126         count = fgetc(file);\r
127         if(0xC0 ==  (count & 0xC0)) { /* this is the run count */\r
128                 count &= 0x3f;\r
129                 val = fgetc(file);\r
130         } else {\r
131                 val = count;\r
132                 count = 1;\r
133         }\r
134 \r
135         // start on the first plane\r
136         plane=0;\r
137         /* write the pixel the specified number of times */\r
138         for(; count && index < bufSize; count--,index++)  {\r
139                 switch (plane)\r
140                 {\r
141                         case 4:\r
142                                 plane=0;\r
143                         break;\r
144                 }\r
145                 // copy to each plane\r
146                 result.plane[plane++][index]=val;\r
147         }\r
148         } while(index < bufSize);\r
149 \r
150         //++++loadPcxpbufPalette(file, &result);\r
151         fclose(file);\r
152         return result;\r
153 \r
154 }\r
155 \r
156 //TODO: update!!\r
157 tileset_t\r
158 planarLoadPcxTiles(char *filename, word twidth, word theight) {\r
159         tileset_t ts;\r
160         FILE *file;\r
161         planar_buf_t result;\r
162         int i;\r
163 \r
164         /* open the PCX file for reading */\r
165         file = fopen(filename, "rb");\r
166         if(!file) {\r
167                 printf("Could not open %s for reading.\n", filename);\r
168                 exit(-2);\r
169         }\r
170 \r
171         /* load the first part of the pcx file */\r
172         loadPcxpbufStage1(file, &result);\r
173 \r
174         /* get the number of tiles and set up the result structure */\r
175         ts.twidth = twidth;\r
176         ts.theight = theight;\r
177         ts.ntiles = (result.width/twidth) * (result.height/theight);\r
178         ts.palette = result.palette;\r
179 \r
180         /* allocate the pixel storage for the tiles */\r
181         /*ts.data = malloc(sizeof(byte*) * ts.ntiles);\r
182         ts.data[0] = malloc(sizeof(byte) * ts.ntiles * twidth * theight);\r
183         for(i=1; i < ts.ntiles; i++) {\r
184                 ts.data[i] = ts.data[i-1] + twidth * theight;\r
185         }*/\r
186 \r
187         /* finish off the file */\r
188         //++++loadPcxPalette(file, &result);\r
189 \r
190         fclose(file);\r
191 \r
192         return ts;\r
193 }\r