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