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