1 /* Project 16 Source Code~
2 * Copyright (C) 2012-2015 sparky4 & pngwen & andrius4669
4 * This file is part of Project 16.
6 * Project 16 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Project 16 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
19 * Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "src/lib/bitmap.h"
\r
27 #include "src/lib/modex16.h"
\r
29 static struct pcxHeader {
\r
50 static void loadPcxStage1(FILE *file, bitmap_t *result) {
\r
56 /* read the header */
\r
57 fread(&head, sizeof(char), sizeof(struct pcxHeader), file);
\r
59 /* get the width and height */
\r
60 result->width = head.xmax - head.xmin + 1;
\r
61 result->height = head.ymax - head.ymin + 1;
\r
63 /* make sure this is 8bpp */
\r
65 printf("I only know how to handle 8bpp pcx files!\n");
\r
72 static void loadPcxPalette(FILE *file, bitmap_t *result) {
\r
76 /* handle the palette */
\r
77 fseek(file, -769, SEEK_END);
\r
79 result->palette = modexNewPal();
\r
80 if(head.version == 5 && val == 12) {
\r
81 /* use the vga palette */
\r
82 for(index=0; !feof(file) && index < PAL_SIZE; index++) {
\r
84 result->palette[index] = val >> 2;
\r
87 /* use the 16 color palette */
\r
88 for(index=0; index<48; index++) {
\r
89 result->palette[index] = head.pal16[index];
\r
96 bitmapLoadPcx(char *filename) {
\r
103 /* open the PCX file for reading */
\r
104 file = fopen(filename, "rb");
\r
106 printf("Could not open %s for reading.\n", filename);
\r
110 /* load the first part of the pcx file */
\r
111 loadPcxStage1(file, &result);
113 /* allocate the buffer */
114 //printf("%zu\n", _memmax());
\r
115 bufSize = (/*(dword)*/result.width * result.height);
116 result.data = malloc(bufSize);
117 // result.data = (byte far *)_fmalloc(bufSize);
118 // result.data = (byte __huge *)halloc(bufSize, sizeof(byte));
119 /*printf("&bufSize=%p\n", &bufSize);
120 printf("&result.data=%p\n", result.data);
121 printf("Size of block is %zu bytes\n", _msize(result.data));
122 printf("Size of bufSize is %zu bytes\n", bufSize);
123 printf("Size of result.width is %zu \n", result.width);
124 printf("Size of result.height is %zu \n", result.height);
125 printf("Dimensions of result is %lu\n", (dword)result.width*result.height);*/
128 fprintf(stderr, "Could not allocate memory for bitmap data.");
\r
133 /* read the buffer in */
\r
136 /* get the run length and the value */
\r
137 count = fgetc(file);
\r
138 if(0xC0 == (count & 0xC0)) { /* this is the run count */
\r
146 /* write the pixel the specified number of times */
\r
147 for(; count && index < bufSize; count--,index++) {
\r
148 result.data[index] = val;
\r
150 } while(index < bufSize);
\r
152 loadPcxPalette(file, &result);
\r
161 bitmapLoadPcxTiles(char *filename, word twidth, word theight) {
\r
167 /* open the PCX file for reading */
\r
168 file = fopen(filename, "rb");
\r
170 printf("Could not open %s for reading.\n", filename);
\r
174 /* load the first part of the pcx file */
\r
175 loadPcxStage1(file, &result);
\r
177 /* get the number of tiles and set up the result structure */
\r
178 ts.twidth = twidth;
\r
179 ts.theight = theight;
\r
180 ts.ntiles = (result.width/twidth) * (result.height/theight);
\r
181 ts.palette = result.palette;
\r
183 /* allocate the pixel storage for the tiles */
\r
184 ts.data = malloc(sizeof(byte*) * ts.ntiles);
\r
185 ts.data[0] = malloc(sizeof(byte) * ts.ntiles * twidth * theight);
\r
186 for(i=1; i < ts.ntiles; i++) {
\r
187 ts.data[i] = ts.data[i-1] + twidth * theight;
\r
190 /* finish off the file */
\r
191 loadPcxPalette(file, &result);
\r