dos-vga-arena-shooter-game/bmpload.c

86 lines
2.0 KiB
C
Raw Normal View History

2024-02-15 01:15:55 +00:00
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <dos.h>
2024-02-15 13:34:50 +00:00
#include "bmpload.h"
2024-02-20 13:00:13 +00:00
#include "system/pc_stuff.h"
#include "system/vga.h"
2024-02-15 01:15:55 +00:00
int readBMPHeader(FILE *fh, struct BMPImage *info) {
int sizeOfHeader;
2024-02-15 13:34:50 +00:00
char value;
2024-02-15 01:15:55 +00:00
unsigned int numberOfUsedColors;
if ((fgetc(fh) != 'B') || (fgetc(fh) != 'M')) {
printf("Not a BMP file\n");
return 1;
}
fseek(fh, 12, SEEK_CUR);
fread(&sizeOfHeader, sizeof(int), 1, fh);
fread(&info->width, sizeof(int), 1, fh);
fread(&info->height, sizeof(int), 1, fh);
fseek(fh, 2, SEEK_CUR);
fread(&info->bpp, sizeof(word), 1, fh);
// for gimp, you need to add colors to the color map until it hits
// 16 colors, then the image will be a 256 color, 8 bpp image
//
// https://www.gimp-forum.net/Thread-indexing-into-8-bit?pid=13233#pid13233
if (info->bpp != 8) return 1;
fseek(fh, 16, SEEK_CUR);
fread(&numberOfUsedColors, sizeof(unsigned int), 1, fh);
if (numberOfUsedColors > 256) return 1;
// get down to color data
fseek(fh, 14 + sizeOfHeader, SEEK_SET);
fread(info->colors, sizeof(struct BMPColor), numberOfUsedColors, fh);
return 0;
}
2024-02-15 13:34:50 +00:00
int readBMPIntoNewMemory(FILE *fh, struct BMPImage *info) {
2024-02-15 01:15:55 +00:00
int result;
result = readBMPHeader(fh, info);
if (result) return result;
2024-02-15 13:34:50 +00:00
info->memoryStart = malloc(info->width * info->height);
2024-02-15 01:15:55 +00:00
2024-02-15 13:34:50 +00:00
return readBMPIntoMemory(fh, info);
2024-02-15 01:15:55 +00:00
}
2024-02-15 13:34:50 +00:00
void freeBMP(struct BMPImage *info) {
free(info->memoryStart);
2024-02-15 01:15:55 +00:00
}
2024-02-15 13:34:50 +00:00
int readBMPIntoMemory(FILE *fh, struct BMPImage *info) {
2024-02-15 01:15:55 +00:00
int x, y, plane;
int result;
2024-02-15 13:34:50 +00:00
byte *currentPointer;
2024-02-15 01:15:55 +00:00
2024-02-15 13:34:50 +00:00
currentPointer = info->memoryStart;
2024-02-15 01:15:55 +00:00
for (y = 0; y < info->height; ++y) {
for (x = 0; x < info->width; ++x) {
2024-02-15 13:34:50 +00:00
currentPointer[(info->height - 1 - y) * info->width + x] = fgetc(fh);
2024-02-15 01:15:55 +00:00
}
}
return 0;
}
void bmp256ColorPaletteToVGAColorPalette(struct BMPImage* bmpImage, struct VGAColor colors[]) {
int i;
for (i = 0; i < 256; ++i) {
colors[i].red = bmpImage->colors[i].red >> 2;
colors[i].green = bmpImage->colors[i].green >> 2;
colors[i].blue = bmpImage->colors[i].blue >> 2;
}
}