<commit_before>#include "df_bitmap.h"

#include <memory.h>
#include <stdio.h>


#define BMP_RGB				0
#define OS2INFOHEADERSIZE  12
#define WININFOHEADERSIZE  40


struct BitmapFileHeader
{
	unsigned short bfType;
	unsigned long  bfSize;
	unsigned short bfReserved1;
	unsigned short bfReserved2;
	unsigned long  bfOffBits;
};


struct BitmapInfoHeader
{
	unsigned long  biWidth;
	unsigned long  biHeight;
	unsigned short biBitCount;
	unsigned long  biCompression;
};


struct WinBmpInfoHeader
{
	unsigned long  biWidth;
	unsigned long  biHeight;
	unsigned short biPlanes;
	unsigned short biBitCount;
	unsigned long  biCompression;
	unsigned long  biSizeImage;
	unsigned long  biXPelsPerMeter;
	unsigned long  biYPelsPerMeter;
	unsigned long  biClrUsed;
	unsigned long  biClrImportant;
};


// ****************************************************************************
// Private Functions
// ****************************************************************************

static unsigned char ReadU8(FILE *file)
{
    return fgetc(file);
}


static short ReadS16(FILE *file)
{
    int b1 = fgetc(file);
    int b2 = fgetc(file);
    return ((b2 << 8) | b1);
}


static int ReadS32(FILE *file)
{
    int b1 = fgetc(file);
    int b2 = fgetc(file);
    int b3 = fgetc(file);
    int b4 = fgetc(file);
    return ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
}


// Reads a BMP file header and check that it has the BMP magic number.
static void ReadBmpFileHeader(FILE *f, BitmapFileHeader *fileheader)
{
	fileheader->bfType = ReadS16(f);
	fileheader->bfSize = ReadS32(f);
	fileheader->bfReserved1 = ReadS16(f);
	fileheader->bfReserved2 = ReadS16(f);
	fileheader->bfOffBits = ReadS32(f);

	ReleaseAssert(fileheader->bfType == 19778, "BMP file seems corrupt");
}


// Reads information from a BMP file header.
void ReadWinBmpInfoHeader(FILE *f, BitmapInfoHeader *infoheader)
{
	WinBmpInfoHeader win_infoheader;

	win_infoheader.biWidth = ReadS32(f);
	win_infoheader.biHeight = ReadS32(f);
	win_infoheader.biPlanes = ReadS16(f);
	win_infoheader.biBitCount = ReadS16(f);
	win_infoheader.biCompression = ReadS32(f);
	win_infoheader.biSizeImage = ReadS32(f);
	win_infoheader.biXPelsPerMeter = ReadS32(f);
	win_infoheader.biYPelsPerMeter = ReadS32(f);
	win_infoheader.biClrUsed = ReadS32(f);
	win_infoheader.biClrImportant = ReadS32(f);

	infoheader->biWidth = win_infoheader.biWidth;
	infoheader->biHeight = win_infoheader.biHeight;
	infoheader->biBitCount = win_infoheader.biBitCount;
	infoheader->biCompression = win_infoheader.biCompression;
}


void ReadBmpPalette(int ncols, DfColour pal[256], FILE *f)
{
	for (int i = 0; i < ncols; i++) 
	{
	    pal[i].b = ReadU8(f);
	    pal[i].g = ReadU8(f);
	    pal[i].r = ReadU8(f);
		pal[i].a = 255;
        ReadU8(f);
	}
}


// Support function for reading the 4 bit bitmap file format.
void Read4BitLine(DfBitmap *bitmap, int length, FILE *f, DfColour pal[256], int y)
{
	for (int x = 0; x < length; x += 2) 
	{
		unsigned char i = ReadU8(f);
		unsigned idx1 = i & 15;
		unsigned idx2 = (i >> 4) & 15;
		PutPix(bitmap, x+1, y, pal[idx1]);
		PutPix(bitmap, x, y, pal[idx2]);
	}
}


// Support function for reading the 8 bit bitmap file format.
void Read8BitLine(DfBitmap *bitmap, int length, FILE *f, DfColour pal[256], int y)
{
	for (int x = 0; x < length; ++x) 
	{
		unsigned char i = ReadU8(f);
		PutPix(bitmap, x, y, pal[i]);
	}
    ReadS16(f);
    ReadU8(f);
}


// Support function for reading the 24 bit bitmap file format
void Read24BitLine(DfBitmap *bitmap, int length, FILE *f, int y)
{
	DfColour c;
	int nbytes = 0;
	c.a = 255;

	for (int i=0; i<length; i++) 
	{
		c.b = ReadU8(f);<commit_msg>Fixed the calculation of the amount of padding to skip when loading 8-bit BMPs.<commit_after>#include "df_bitmap.h"

#include <memory.h>
#include <stdio.h>


#define BMP_RGB				0
#define OS2INFOHEADERSIZE  12
#define WININFOHEADERSIZE  40


struct BitmapFileHeader
{
	unsigned short bfType;
	unsigned long  bfSize;
	unsigned short bfReserved1;
	unsigned short bfReserved2;
	unsigned long  bfOffBits;
};


struct BitmapInfoHeader
{
	unsigned long  biWidth;
	unsigned long  biHeight;
	unsigned short biBitCount;
	unsigned long  biCompression;
};


struct WinBmpInfoHeader
{
	unsigned long  biWidth;
	unsigned long  biHeight;
	unsigned short biPlanes;
	unsigned short biBitCount;
	unsigned long  biCompression;
	unsigned long  biSizeImage;
	unsigned long  biXPelsPerMeter;
	unsigned long  biYPelsPerMeter;
	unsigned long  biClrUsed;
	unsigned long  biClrImportant;
};


// ****************************************************************************
// Private Functions
// ****************************************************************************

static unsigned char ReadU8(FILE *file)
{
    return fgetc(file);
}


static short ReadS16(FILE *file)
{
    int b1 = fgetc(file);
    int b2 = fgetc(file);
    return ((b2 << 8) | b1);
}


static int ReadS32(FILE *file)
{
    int b1 = fgetc(file);
    int b2 = fgetc(file);
    int b3 = fgetc(file);
    int b4 = fgetc(file);
    return ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
}


// Reads a BMP file header and check that it has the BMP magic number.
static void ReadBmpFileHeader(FILE *f, BitmapFileHeader *fileheader)
{
	fileheader->bfType = ReadS16(f);
	fileheader->bfSize = ReadS32(f);
	fileheader->bfReserved1 = ReadS16(f);
	fileheader->bfReserved2 = ReadS16(f);
	fileheader->bfOffBits = ReadS32(f);

	ReleaseAssert(fileheader->bfType == 19778, "BMP file seems corrupt");
}


// Reads information from a BMP file header.
void ReadWinBmpInfoHeader(FILE *f, BitmapInfoHeader *infoheader)
{
	WinBmpInfoHeader win_infoheader;

	win_infoheader.biWidth = ReadS32(f);
	win_infoheader.biHeight = ReadS32(f);
	win_infoheader.biPlanes = ReadS16(f);
	win_infoheader.biBitCount = ReadS16(f);
	win_infoheader.biCompression = ReadS32(f);
	win_infoheader.biSizeImage = ReadS32(f);
	win_infoheader.biXPelsPerMeter = ReadS32(f);
	win_infoheader.biYPelsPerMeter = ReadS32(f);
	win_infoheader.biClrUsed = ReadS32(f);
	win_infoheader.biClrImportant = ReadS32(f);

	infoheader->biWidth = win_infoheader.biWidth;
	infoheader->biHeight = win_infoheader.biHeight;
	infoheader->biBitCount = win_infoheader.biBitCount;
	infoheader->biCompression = win_infoheader.biCompression;
}


void ReadBmpPalette(int ncols, DfColour pal[256], FILE *f)
{
	for (int i = 0; i < ncols; i++) 
	{
	    pal[i].b = ReadU8(f);
	    pal[i].g = ReadU8(f);
	    pal[i].r = ReadU8(f);
		pal[i].a = 255;
        ReadU8(f);
	}
}


// Support function for reading the 4 bit bitmap file format.
void Read4BitLine(DfBitmap *bitmap, int length, FILE *f, DfColour pal[256], int y)
{
	for (int x = 0; x < length; x += 2) 
	{
		unsigned char i = ReadU8(f);
		unsigned idx1 = i & 15;
		unsigned idx2 = (i >> 4) & 15;
		PutPix(bitmap, x+1, y, pal[idx1]);
		PutPix(bitmap, x, y, pal[idx2]);
	}
}


// Support function for reading the 8 bit bitmap file format.
void Read8BitLine(DfBitmap *bitmap, int length, FILE *f, DfColour pal[256], int y)
{
	for (int x = 0; x < length; ++x) 
	{
		unsigned char i = ReadU8(f);
		PutPix(bitmap, x, y, pal[i]);
	}

    // Skip any padding until we've read a multiple of 4 bytes.
    while (length & 3)
    {
        ReadU8(f);
        length++;
    }
}


// Support function for reading the 24 bit bitmap file format
void Read24BitLine(DfBitmap *bitmap, int length, FILE *f, int y)
{
	DfColour c;
	int nbytes = 0;
	c.a = 255;

	for (int i=0; i<length; i++) 
	{
		c.b = ReadU8(f);
{'fraction_non_alphanumeric': 0.15713902308105207, 'fraction_numerical': 0.03153515834675255, 'mean_word_length': 4.190111420612813, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3165675', 'n_tokens_mistral': 2966, 'n_tokens_neox': 2740, 'n_words': 720}