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