+/* Project 16 Source Code~\r
+ * Copyright (C) 2012-2015 sparky4 & pngwen & andrius4669\r
+ *\r
+ * This file is part of Project 16.\r
+ *\r
+ * Project 16 is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Project 16 is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program. If not, see <http://www.gnu.org/licenses/>, or\r
+ * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
+ * Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ */\r
+\r
#include <stdio.h>\r
#include <stdlib.h>\r
-#include "src\lib\bitmap.h"\r
-#include "src\lib\modex16.h"\r
+#include <malloc.h>\r
+#include "src/lib/bitmap.h"\r
+#include "src/lib/modex16.h"\r
\r
+#ifndef PCXHEADER_H\r
+#define PCXHEADER_H\r
static struct pcxHeader {\r
- byte id;\r
- byte version;\r
- byte encoding;\r
- byte bpp;\r
- word xmin;\r
- word ymin;\r
- word xmax;\r
- word ymax;\r
- word hres;\r
- word vres;\r
- byte pal16[48];\r
- byte res1;\r
- word bpplane;\r
- word palType;\r
- word hScreenSize;\r
- word vScreenSize;\r
- byte padding[54];\r
+ byte id;\r
+ byte version;\r
+ byte encoding;\r
+ byte bpp;\r
+ word xmin;\r
+ word ymin;\r
+ word xmax;\r
+ word ymax;\r
+ word hres;\r
+ word vres;\r
+ byte pal16[48];\r
+ byte res1;\r
+ word bpplane;\r
+ word palType;\r
+ word hScreenSize;\r
+ word vScreenSize;\r
+ byte padding[54];\r
} head;\r
+#endif /*PCXHEADER_H*/\r
\r
\r
static void loadPcxStage1(FILE *file, bitmap_t *result) {\r
- long bufSize;\r
- int index;\r
- byte count, val;\r
- long int pos;\r
-\r
- /* read the header */\r
- fread(&head, sizeof(char), sizeof(struct pcxHeader), file);\r
-\r
- /* get the width and height */\r
- result->width = head.xmax - head.xmin + 1;\r
- result->height = head.ymax - head.ymin + 1;\r
-\r
- /* make sure this is 8bpp */\r
- if(head.bpp != 8) {\r
- printf("I only know how to handle 8bpp pcx files!\n");\r
- fclose(file);\r
- exit(-2);\r
- }\r
+ long bufSize;\r
+ int index;\r
+ byte count, val;\r
+ long int pos;\r
+\r
+ /* read the header */\r
+ fread(&head, sizeof(char), sizeof(struct pcxHeader), file);\r
+\r
+ /* get the width and height */\r
+ result->width = head.xmax - head.xmin + 1;\r
+ result->height = head.ymax - head.ymin + 1;\r
+\r
+ /* make sure this is 8bpp */\r
+ if(head.bpp != 8) {\r
+ printf("I only know how to handle 8bpp pcx files!\n");\r
+ fclose(file);\r
+ exit(-2);\r
+ }\r
}\r
\r
\r
static void loadPcxPalette(FILE *file, bitmap_t *result) {\r
- byte val;\r
- int index;\r
-\r
- /* handle the palette */\r
- fseek(file, -769, SEEK_END);\r
- val = fgetc(file);\r
- result->palette = modexNewPal();\r
- if(head.version == 5 && val == 12) {\r
+ byte val;\r
+ int index;\r
+\r
+ /* handle the palette */\r
+ fseek(file, -769, SEEK_END);\r
+ val = fgetc(file);\r
+ result->palette = modexNewPal();\r
+ if(head.version == 5 && val == 12) {\r
/* use the vga palette */\r
for(index=0; !feof(file) && index < PAL_SIZE; index++) {\r
- val = fgetc(file);\r
- result->palette[index] = val >> 2;\r
+ val = fgetc(file);\r
+ result->palette[index] = val >> 2;\r
}\r
- } else {\r
+ } else {\r
/* use the 16 color palette */\r
for(index=0; index<48; index++) {\r
- result->palette[index] = head.pal16[index];\r
+ result->palette[index] = head.pal16[index];\r
+ }\r
}\r
- }\r
}\r
\r
\r
bitmap_t\r
bitmapLoadPcx(char *filename) {\r
- FILE *file;\r
- bitmap_t result;\r
- long bufSize;\r
- int index;\r
- byte count, val;
-\r
- /* open the PCX file for reading */\r
- file = fopen(filename, "rb");\r
- if(!file) {\r
- printf("Could not open %s for reading.\n", filename);\r
- exit(-2);\r
- }\r
-\r
- /* load the first part of the pcx file */\r
- loadPcxStage1(file, &result);
-\r
- /* allocate the buffer */\r
- bufSize = result.width * result.height;
- result.data = malloc(bufSize); //it breaks right here~
- if(!result.data) {\r
- printf("Could not allocate memory for bitmap data.");\r
- fclose(file);\r
- exit(-1);\r
- }\r
+ FILE *file;\r
+ bitmap_t result;\r
+ dword bufSize;\r
+ int index;\r
+ byte count, val;\r
+\r
+ /* open the PCX file for reading */\r
+ file = fopen(filename, "rb");\r
+ if(!file) {\r
+ printf("Could not open %s for reading.\n", filename);\r
+ exit(-2);\r
+ }\r
+\r
+ /* load the first part of the pcx file */\r
+ loadPcxStage1(file, &result);\r
+\r
+ /* allocate the buffer */\r
+ //printf("%zu\n", _memmax());\r
+ bufSize = (/*(dword)*/result.width * result.height);\r
+ result.data = malloc(bufSize);\r
+// result.data = (byte far *)_fmalloc(bufSize);\r
+// result.data = (byte __huge *)halloc(bufSize, sizeof(byte));\r
+ /*printf("&bufSize=%p\n", &bufSize);\r
+ printf("&result.data=%p\n", result.data);\r
+ printf("Size of block is %zu bytes\n", _msize(result.data));\r
+ printf("Size of bufSize is %zu bytes\n", bufSize);\r
+ printf("Size of result.width is %zu \n", result.width);\r
+ printf("Size of result.height is %zu \n", result.height);\r
+ printf("Dimensions of result is %lu\n", (dword)result.width*result.height);*/\r
+ //exit(0);\r
+ if(!result.data) {\r
+ fprintf(stderr, "Could not allocate memory for bitmap data.");\r
+ fclose(file);\r
+ exit(-1);\r
+ }\r
\r
- /* read the buffer in */\r
- index = 0;\r
- do {\r
+ /* read the buffer in */\r
+ index = 0;\r
+ do {\r
/* get the run length and the value */\r
count = fgetc(file);\r
if(0xC0 == (count & 0xC0)) { /* this is the run count */\r
- count &= 0x3f;\r
- val = fgetc(file);\r
+ count &= 0x3f;\r
+ val = fgetc(file);\r
} else {\r
- val = count;\r
- count = 1;\r
+ val = count;\r
+ count = 1;\r
}\r
\r
/* write the pixel the specified number of times */\r
for(; count && index < bufSize; count--,index++) {\r
- result.data[index] = val;\r
+ result.data[index] = val;\r
}\r
- } while(index < bufSize);\r
+ } while(index < bufSize);\r
\r
- loadPcxPalette(file, &result);\r
+ loadPcxPalette(file, &result);\r
\r
- fclose(file);\r
+ fclose(file);\r
\r
- return result;\r
+ return result;\r
}\r
\r
-\r
+//TODO: update!!\r
tileset_t\r
bitmapLoadPcxTiles(char *filename, word twidth, word theight) {\r
- tileset_t ts;\r
- FILE *file;\r
- bitmap_t result;\r
- int i;\r
-\r
- /* open the PCX file for reading */\r
- file = fopen(filename, "rb");\r
- if(!file) {\r
- printf("Could not open %s for reading.\n", filename);\r
- exit(-2);\r
- }\r
-\r
- /* load the first part of the pcx file */\r
- loadPcxStage1(file, &result);\r
-\r
- /* get the number of tiles and set up the result structure */\r
- ts.twidth = twidth;\r
- ts.theight = theight;\r
- ts.ntiles = (result.width/twidth) * (result.height/theight);\r
- ts.palette = result.palette;\r
-\r
- /* allocate the pixel storage for the tiles */\r
- ts.data = malloc(sizeof(byte*) * ts.ntiles);\r
- ts.data[0] = malloc(sizeof(byte) * ts.ntiles * twidth * theight);\r
- for(i=1; i < ts.ntiles; i++) {\r
- ts.data[i] = ts.data[i-1] + twidth * theight;\r
- }\r
- \r
- /* finish off the file */\r
- loadPcxPalette(file, &result);\r
-\r
- fclose(file);\r
-\r
- return ts;\r
+ tileset_t ts;\r
+ FILE *file;\r
+ bitmap_t result;\r
+ int i;\r
+\r
+ /* open the PCX file for reading */\r
+ file = fopen(filename, "rb");\r
+ if(!file) {\r
+ printf("Could not open %s for reading.\n", filename);\r
+ exit(-2);\r
+ }\r
+\r
+ /* load the first part of the pcx file */\r
+ loadPcxStage1(file, &result);\r
+\r
+ /* get the number of tiles and set up the result structure */\r
+ ts.twidth = twidth;\r
+ ts.theight = theight;\r
+ ts.ntiles = (result.width/twidth) * (result.height/theight);\r
+ ts.palette = result.palette;\r
+\r
+ /* allocate the pixel storage for the tiles */\r
+ ts.data = malloc(sizeof(byte*) * ts.ntiles);\r
+ ts.data[0] = malloc(sizeof(byte) * ts.ntiles * twidth * theight);\r
+ for(i=1; i < ts.ntiles; i++) {\r
+ ts.data[i] = ts.data[i-1] + twidth * theight;\r
+ }\r
+\r
+ /* finish off the file */\r
+ loadPcxPalette(file, &result);\r
+\r
+ fclose(file);\r
+\r
+ return ts;\r
}\r