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