]> 4ch.mooo.com Git - 16.git/blob - src/lib/modex16/16planar.c
i got it to work to a deree!
[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 /*\r
29  *============================================================================\r
30  */\r
31 \r
32 static void loadPcxpbufStage1(FILE *file, planar_buf_t *result) {\r
33         int index;\r
34         byte count, val;\r
35         long int pos;\r
36 \r
37 //word w=0;\r
38 //fprintf(stderr, "\nplanarLoadPcx: ");\r
39 //fprintf(stderr, "%u ", w++);\r
40         /* read the header */\r
41         fread(&head, sizeof(char), sizeof(struct pcxHeader), file);\r
42 //fprintf(stderr, "%u ", w++);\r
43         /* get the width and height */\r
44         result->width = head.xmax - head.xmin + 1;\r
45         result->height = head.ymax - head.ymin + 1;\r
46         result->pwidth = result->width / 4 + (result->width%4 ? 1 : 0);\r
47 //fprintf(stderr, "%u ", w++);\r
48         /* make sure this  is 8bpp */\r
49         if(head.bpp != 8) {\r
50                 fprintf(stderr, "I only know how to handle 8bpp pcx files!\n");\r
51                 fclose(file);\r
52                 exit(-2);\r
53         }\r
54 }\r
55 \r
56 static void loadPcxpbufPalette(FILE *file, planar_buf_t *result) {\r
57         byte val;\r
58         int index;\r
59 \r
60         /* handle the palette */\r
61         fseek(file, -769, SEEK_END);\r
62         val = fgetc(file);\r
63         result->palette = modexNewPal();\r
64         if(head.version == 5 && val == 12) {\r
65         /* use the vga palette */\r
66         for(index=0; !feof(file) && index < PAL_SIZE; index++) {\r
67                 val = fgetc(file);\r
68                 result->palette[index] = val >> 2;\r
69         }\r
70         } else {\r
71         /* use the 16 color palette */\r
72         for(index=0; index<48; index++) {\r
73                 result->palette[index]  = head.pal16[index];\r
74         }\r
75         }\r
76 }\r
77 \r
78 /* allocates a planar buffer with specified dimensions */\r
79 static planar_buf_t\r
80 pbuf_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         //p.pwidth = width / 4 + (width%4 ? 1 : 0);\r
90 \r
91         /* allocate the planes */\r
92         for(i=0; i<4; i++) {\r
93                 p.plane[i] = _fmalloc(p.height * p.pwidth);\r
94         }\r
95 \r
96         return p;\r
97 }\r
98 \r
99 /* allocates a planar buffer with specified dimensions */\r
100 static void\r
101 pbuf_alloc0(planar_buf_t *p, word width, word height) {\r
102         int i;\r
103 \r
104         /* allocate the structure and populate sizes */\r
105         p=_fmalloc(sizeof(planar_buf_t));\r
106         p->width  = width;\r
107         p->height = height;\r
108         p->pwidth = width / 4 + (width%4 ? 1 : 0);\r
109         //p.pwidth = width / 4 + (width%4 ? 1 : 0);\r
110 \r
111         /* allocate the planes */\r
112         for(i=0; i<4; i++) {\r
113                 p->plane[i] = _fmalloc(p->height * p->pwidth);\r
114         }\r
115 }\r
116 \r
117 /*      sparky4's functions~    */\r
118 planar_buf_t planarLoadPcx(char *filename)\r
119 {\r
120         FILE *file;\r
121         planar_buf_t result;\r
122         dword bufSize;\r
123         word index[4], plane;\r
124         byte count, val;\r
125 \r
126         word px,py,i,pla;\r
127 \r
128 /*word w=0;\r
129 fprintf(stderr, "\nplanarLoadPcx: ");\r
130 fprintf(stderr, "%u ", w++);*/\r
131         /* open the PCX file for reading */\r
132         file = fopen(filename, "rb");\r
133 //fprintf(stderr, "%u ", w++);\r
134         if(!file) {\r
135                 fprintf(stderr, "Could not open %s for reading.\n", filename);\r
136                 exit(-2);\r
137         }\r
138 //fprintf(stderr, "%u ", w++);\r
139         /* load the first part of the pcx file */\r
140         loadPcxpbufStage1(file, &result);\r
141 //fprintf(stderr, "%u ", w++);\r
142         /* allocate the buffer */\r
143         bufSize = ((dword)result.width * result.height);\r
144         result = pbuf_alloc(result.width, result.height);\r
145         //pbuf_alloc0(&result, result.width, result.height);\r
146 \r
147 //fprintf(stderr, "%u ", w++);\r
148 //      printf("&bufSize=%p\n", &bufSize);\r
149 //      printf("&result.data=%p\n", result.plane);\r
150 //      printf("Size of block is %zu bytes\n", _msize(result.plane));\r
151 //      printf("Size of bufSize is %zu bytes\n", bufSize);\r
152 //      printf("Size of result.width is %zu \n", result.width);\r
153 //      printf("Size of result.height is %zu \n", result.height);\r
154 //      printf("Dimensions of result is %lu\n", (dword)result.width*result.height);\r
155 //      //exit(0);\r
156         if(!result.plane) {\r
157                 fprintf(stderr, "Could not allocate memory for bitmap data.");\r
158                 fclose(file);\r
159                 exit(-1);\r
160         }\r
161 //fprintf(stderr, "read the buffer %u ", w++);\r
162         /*  read the buffer in */\r
163         index[0] = 0,index[1]=0,index[2]=0,index[3]=0;\r
164         /* start on the first plane */\r
165         plane=0;\r
166         do {\r
167         /* get the run length and the value */\r
168         count = fgetc(file);\r
169         if(0xC0 ==  (count & 0xC0)) { /* this is the run count */\r
170                 count &= 0x3f;\r
171                 val = fgetc(file);\r
172         } else {\r
173                 val = count;\r
174                 count = 1;\r
175         }\r
176 \r
177 // if(index[plane]==0 && plane==0) fprintf(stdout, "Val dump of %u[%u]  &&      count=%02X:\n", index[plane], plane, count);\r
178 //fprintf(stdout, "Val dump of %u[%u]   &&      count=%02X:\n", index[plane], plane, count);\r
179 // fprintf(stdout, "%02X ", val);\r
180 // if(index[plane]==result.pwidth-1) fprintf(stdout, "\n");\r
181 \r
182         /* write the pixel the specified number of times */\r
183 //fprintf(stderr, "\nputting in memory~ %u\n", w++);\r
184         for(; count && (index[0]+index[1]+index[2]+index[3]) < bufSize; count--,index[plane]++)  {\r
185                 switch (plane)\r
186                 {\r
187                         case 4:\r
188                                 plane=0;\r
189                         break;\r
190                         case 0:\r
191                         case 1:\r
192                         case 2:\r
193                                 plane++;\r
194                         break;\r
195                         default:\r
196                                 plane=0;\r
197                         break;\r
198                 }\r
199                 // copy to each plane\r
200                 result.plane[plane][index[plane]]=val;\r
201 //fprintf(stdout, "count=%02X   index=%u        plane=%u        ", count, index[plane], plane);\r
202 //fprintf(stdout, "index val=%02X       val=%02X\n", result.plane[plane][index[plane]], val);\r
203         }\r
204 // fprintf(stdout, "\nindex=%lu         bufsize=%lu\n\n", (dword)(index[0]+index[1]+index[2]+index[3]),  bufSize);\r
205         } while((index[0]+index[1]+index[2]+index[3]) < bufSize);\r
206         loadPcxpbufPalette(file, &result);\r
207         fclose(file);\r
208 \r
209 //      //dump value!!\r
210 //      for(pla=0; pla < 4; pla++) {\r
211 //              i=0;\r
212 //              printf("Plane %d\n", pla);\r
213 //              for(py=0; py < result.height; py++) {\r
214 //                      for(px=0; px < result.pwidth; px++) {\r
215 //                              printf("%02X ", (int) result.plane[pla][i++]);\r
216 //                      }\r
217 //                      printf("\n");\r
218 //              }\r
219 //      }\r
220 // fprintf(stderr, "\n\n%s      count=%d        index=%d        plane=%d\n", filename, count, (dword)(index[0]+index[1]+index[2]+index[3]), pla);\r
221 // exit(0);\r
222         return result;\r
223 \r
224 }\r
225 \r
226 //TODO: update!!\r
227 tileset_t\r
228 planarLoadPcxTiles(char *filename, word twidth, word theight) {\r
229         tileset_t ts;\r
230         FILE *file;\r
231         planar_buf_t result;\r
232         int i;\r
233 \r
234         /* open the PCX file for reading */\r
235         file = fopen(filename, "rb");\r
236         if(!file) {\r
237                 printf("Could not open %s for reading.\n", filename);\r
238                 exit(-2);\r
239         }\r
240 \r
241         /* load the first part of the pcx file */\r
242         loadPcxpbufStage1(file, &result);\r
243 \r
244         /* get the number of tiles and set up the result structure */\r
245         ts.twidth = twidth;\r
246         ts.theight = theight;\r
247         ts.ntiles = (result.width/twidth) * (result.height/theight);\r
248         ts.palette = result.palette;\r
249 \r
250         /* allocate the pixel storage for the tiles */\r
251         /*ts.data = malloc(sizeof(byte*) * ts.ntiles);\r
252         ts.data[0] = malloc(sizeof(byte) * ts.ntiles * twidth * theight);\r
253         for(i=1; i < ts.ntiles; i++) {\r
254                 ts.data[i] = ts.data[i-1] + twidth * theight;\r
255         }*/\r
256 \r
257         /* finish off the file */\r
258         //++++loadPcxPalette(file, &result);\r
259 \r
260         fclose(file);\r
261 \r
262         return ts;\r
263 }\r