Skip to content

Instantly share code, notes, and snippets.

@alaingalvan
Last active July 11, 2022 22:11
Show Gist options
  • Save alaingalvan/2a37bc4619b2e67415638522f03ac42f to your computer and use it in GitHub Desktop.
Save alaingalvan/2a37bc4619b2e67415638522f03ac42f to your computer and use it in GitHub Desktop.
A way of reading a bitmap as a 2D array of colors in Game Maker Studio. Works with Game Maker 8.1.
///read_bmp(filename)
// Reads 24 or 32 bit bitmap files
//returns a ds_grid of colors encoded as rgb hex
var handle;
handle = file_bin_open(argument0, 0);
var offset, width, height, bits, output;
offset = 29;
width = 0;
height = 0;
bits = 0;
var i;
for (i = 0; i < offset; i += 1) {
if (i == 10)
offset = file_bin_read_byte(handle);
else
if (i == 11)
offset += file_bin_read_byte(handle) << 8;
else
if (i == 12)
offset += file_bin_read_byte(handle) << 16;
else
if (i == 13)
offset += file_bin_read_byte(handle) << 24;
else
if (i == 18)
width = file_bin_read_byte(handle);
else
if (i == 19)
width += file_bin_read_byte(handle) << 8;
else
if (i == 20)
width += file_bin_read_byte(handle) << 16;
else
if (i == 21)
width += file_bin_read_byte(handle) << 24;
else
if (i == 22)
height = file_bin_read_byte(handle);
else
if (i == 19)
height += file_bin_read_byte(handle) << 8;
else
if (i == 20)
height += file_bin_read_byte(handle) << 16;
else
if (i == 21)
height += file_bin_read_byte(handle) << 24;
else
if (i == 28)
bits = file_bin_read_byte(handle);
else
file_bin_read_byte(handle); // Skip
}
var output;
output = ds_grid_create(width, height);
var j;
for (j = 0; j < height; j += 1) {
for (i = 0; i < width; i += 1) {
var r, g, b, value;
r = file_bin_read_byte(handle);
g = file_bin_read_byte(handle) << 8;
b = file_bin_read_byte(handle) << 16;
if (bits == 32)
file_bin_read_byte(handle);
value = r + g + b;
ds_grid_set(output, i, (height-1)-j, value);
}
if (bits == 24) {
// Padding
for (h = 0; h < (width * 3) mod 4; h += 1) {
file_bin_read_byte(handle);
}
}
}
file_bin_close(handle);
return output;
@MarcusAugustusCaesar
Copy link

Hi! Thx for making this, haven't tested it out yet. But line 61
for (i = 0 i < width; i += 1) {
There has to be a ; after the 0.

Regards

@alaingalvan
Copy link
Author

Thanks @MarcusAugustusCaesar, I've corrected this now!

@elliot-treasure
Copy link

elliot-treasure commented Jul 11, 2022

Hello,
Was reading through this and noticed there was some duplicate if statements in there (19, 20, 21). Not that this will functionally change anything but I made a more condensed version of that section if you'd like to use it. Anyways, thank you for sharing this, it was super helpful!

for (i = 0; i < offset; i += 1) {
switch (i) {
case 10: offset = file_bin_read_byte(handle); break;
case 11: offset += file_bin_read_byte(handle) << 8; break;
case 12: offset += file_bin_read_byte(handle) << 16; break;
case 13: offset += file_bin_read_byte(handle) << 24; break;
case 18: width = file_bin_read_byte(handle); break;
case 19: width += file_bin_read_byte(handle) << 8; break;
case 20: width += file_bin_read_byte(handle) << 16; break;
case 21: width += file_bin_read_byte(handle) << 24; break;
case 22: height = file_bin_read_byte(handle); break;
case 28: bits = file_bin_read_byte(handle); break;
default: file_bin_read_byte(handle); break; // Skip
}
}

(not sure why it's not adding in the spaces/tabs :/)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment