]> 4ch.mooo.com Git - 16.git/blobdiff - src/lib/bitmap.c
huge memory model fucks up everything! wwww
[16.git] / src / lib / bitmap.c
index a5a439444fed36ba8b01b800f22416b84495e41d..3777c3d125b54ead3bb82498682b399db3715c2a 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>\r
-#include <stdlib.h>\r
+#include <stdlib.h>
+#include <malloc.h>
 #include "src/lib/bitmap.h"\r
 #include "src/lib/modex16.h"\r
 \r
@@ -39,9 +40,9 @@ static void loadPcxStage1(FILE *file, bitmap_t *result) {
 \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
+               printf("I only know how to handle 8bpp pcx files!\n");\r
+               fclose(file);\r
+               exit(-2);\r
     }\r
 }\r
 \r
@@ -73,28 +74,34 @@ bitmap_t
 bitmapLoadPcx(char *filename) {\r
     FILE *file;\r
     bitmap_t result;\r
-    long bufSize;\r
+    dword 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
+               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
+       bufSize = ((dword)result.width * result.height);
+       result.data = malloc((bufSize));
+       /*0000printf("Size of block is %u bytes\n", _msize(result.data));
+       printf("Size of bufSize is %lu bytes\n", bufSize);
+       printf("Size of result.width is %lu \n", result.width);
+       printf("Size of result.height is %lu \n", result.height);
+       printf("Dimensions of result is %lu\n", result.width*result.height);*/
+       //exit(0);
+       if(!result.data) {
+               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
@@ -102,16 +109,16 @@ bitmapLoadPcx(char *filename) {
        /* 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
 \r
@@ -122,7 +129,7 @@ bitmapLoadPcx(char *filename) {
     return result;\r
 }\r
 \r
-\r
+//update!!\r
 tileset_t\r
 bitmapLoadPcxTiles(char *filename, word twidth, word theight) {\r
     tileset_t ts;\r
@@ -132,10 +139,10 @@ bitmapLoadPcxTiles(char *filename, word twidth, word theight) {
 \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
+       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
@@ -146,12 +153,12 @@ bitmapLoadPcxTiles(char *filename, word twidth, word theight) {
     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
+       /* 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