Jump to content


Photo

readBMPFile


  • Please log in to reply
23 replies to this topic

#1 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 12 October 2010 - 03:59 AM

Source,
slayer_64's topic

Dont forget to comment out the draw_point in the loop

/* 
    readBMPFile
    Arguments: 
        0 ds grid 
        1 fname 
         
    Returns: 
        reads the 24 or 32 bit bmp image from fname and loads it's data in the ds grid 
*/ 

if(!file_exists(argument1))
{
    show_message("File does not exist#"+string(argument1)) 
    exit;
}
var file; file=file_bin_open(argument1,0) 

if (file<=0) 
{
    show_message("Error opening file#"+string(argument1)) 
    exit;
}
 
 
var magicNumber; magicNumber=chr(file_bin_read_byte(file))+chr(file_bin_read_byte(file)) 
 
if (magicNumber!="BM") 
{ 
    show_message("File is not a BMP#"+string(argument1)) 
    file_bin_close(file)
    exit 
} 

var fileSize; fileSize=fileBinReadWord(file,4) 
var unused0; unused0=fileBinReadWord(file,2) 
var unused1; unused1=fileBinReadWord(file,2) 
var dataOffset; dataOffset=fileBinReadWord(file,4) 
var headerBytes; headerBytes=fileBinReadWord(file,4) 
var width; width=fileBinReadWord(file,4) 
var height; height=fileBinReadWord(file,4) 
var colorPlanes; colorPlanes=fileBinReadWord(file,2) 
var bitsPerPixel; bitsPerPixel=fileBinReadWord(file,2) 
var compression; compression=fileBinReadWord(file,4) 
var dataSize; dataSize=fileBinReadWord(file,4) 
var resolutionx; resolutionx=fileBinReadWord(file,4) 
var resolutiony; resolutiony=fileBinReadWord(file,4) 
var colorsInPalette; colorsInPalette=fileBinReadWord(file,4) 
var importantColors; importantColors=fileBinReadWord(file,4) 
/*
magicNumber BM

fileSize 65590

unused0 0

unused1 0

dataOffset 54

headerBytes 40

width 128

height 128

colorPlanes 1

bitsPerPixel 32

compression 0

dataSize 65536

resolutionx 0

resolutiony 0

colorsInPalette 0

importantColors 0
*/

if(bitsPerPixel != 32 and bitsPerPixel != 24)
{ 
    show_message("File must be true color (24 or 32 bpp)#"+string(argument1)) 
    file_bin_close(file);
    exit;
} 

if(compression != 0)
{ 
    show_message("Compressed BMP is not supported#"+string(argument1)) 
    file_bin_close(file);
    exit;
}
/*
show_debug_message("magicNumber "+string(magicNumber)) 
show_debug_message("fileSize "+string(fileSize)) 
show_debug_message("unused0 "+string(unused0)) 
show_debug_message("unused1 "+string(unused1)) 
show_debug_message("dataOffset "+string(dataOffset)) 
show_debug_message("headerBytes "+string(headerBytes)) 
show_debug_message("width "+string(width)) 
show_debug_message("height "+string(height)) 
show_debug_message("colorPlanes "+string(colorPlanes)) 
show_debug_message("bitsPerPixel "+string(bitsPerPixel)) 
show_debug_message("compression "+string(compression)) 
show_debug_message("dataSize "+string(dataSize)) 
show_debug_message("resolutionx "+string(resolutionx)) 
show_debug_message("resolutiony "+string(resolutiony)) 
show_debug_message("colorsInPalette "+string(colorsInPalette)) 
show_debug_message("importantColors "+string(importantColors)) 
show_debug_message("AT:  "+string(file_bin_position(file))) 
*/

ds_grid_resize(argument0,width,height) 

file_bin_seek(file,dataOffset)
var xx, yy, forth, r,g,b; 
for(yy=height-1;yy>=0;yy-=1) 
{ 
    for(xx=0;xx<width;xx+=1) 
    { 
        b=file_bin_read_byte(file);
        g=file_bin_read_byte(file);
        r=file_bin_read_byte(file); 
        if(bitsPerPixel == 32) 
            forth = file_bin_read_byte(file);
         
        ds_grid_set(argument0,xx,yy,make_color_rgb(r,g,b))
        draw_point_color(xx,yy,make_color_rgb(r,g,b)) 
    } 
     
    //padding at the end of the scanline 
    repeat (width mod 4) file_bin_read_byte(file) 
} 
 
file_bin_close(file)

//fileBinReadWord(file,count)
//count specifies the number of bytes to form up the word
var ret; ret = 0;
var shift; shift = 0;
repeat(argument1)
{
    ret+=file_bin_read_byte(argument0) <<shift;
    shift+=8;
}
return ret;

This mod reads in regions of BMP
http://gmc.yoyogames...dpost&p=3629155
the 32bpp support may not be implemented, not a big deal.

Edited by icuurd12b42, 26 October 2012 - 08:51 PM.

  • 4

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#2 RTII

RTII

    RT

  • New Member
  • 402 posts

Posted 12 October 2010 - 04:37 AM

Thanks, a time ago I was wondering how to do that. Could be really useful.
  • 0
Posted Image Posted Image Posted Image
My Games | RT-dev | RTII forum | Fight My Brute | Grayscale Corporation Forum
Please read and follow the closed, or your account may be Signature Rules.

#3 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 12 October 2010 - 06:09 AM

Thanks, a time ago I was wondering how to do that. Could be really useful.

Height map for one thing
  • 1

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#4 slayer 64

slayer 64

    Slayer of gingers

  • GMC Member
  • 4301 posts
  • Version:GM:Studio

Posted 12 October 2010 - 11:20 PM

excellent,
now when people search google or the gmc they should find what they're looking for easily, unlike myself most of the time xD
  • 0

5y5rs3d.pngfg0UQNL.png


#5 LaLaLa

LaLaLa

    GMC Member

  • New Member
  • 511 posts
  • Version:GM8

Posted 20 October 2010 - 04:25 AM

Won't this be super ridiculously slow? Besides, if you wanted to read a heightmap you could just save a raw dump of the data instead of using the stupid BMP format which you then have to parse.

Edited by LaLaLa, 20 October 2010 - 04:27 AM.

  • 0

Posted ImagePosted Image
GM Ray Tracer (Multi-threaded version)| Batch Explorer (soon) | TI-Basic IDE (soon) | Mandelbrot Set Renderer (soon) |Image Set Viewer


#6 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 20 October 2010 - 06:25 AM

Won't this be super ridiculously slow? Besides, if you wanted to read a heightmap you could just save a raw dump of the data instead of using the stupid BMP format which you then have to parse.


Say what? None of your statements actualy made sense

>>Won't this be super ridiculously slow?
No, where is the basis of your claim?

>>you could just save a raw dump of the data instead of using the stupid BMP format
So is that "raw dump" format supported by paint or paint.net? oh wait, BMP IS RAW DUMP.

>>which you then have to parse.
parse? what parse? there is no parsing!! It's a straight sequential read of raw <BMP> information!


You can always use this to convert your heightmap images to another <faster> readeable format if you please... Or are you infering you want to make a height map editor in gm, implying you would also need to make a paint program. WHY WOULD YOU?
  • 2

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#7 LaLaLa

LaLaLa

    GMC Member

  • New Member
  • 511 posts
  • Version:GM8

Posted 22 October 2010 - 05:27 AM

Won't this be super ridiculously slow? Besides, if you wanted to read a heightmap you could just save a raw dump of the data instead of using the stupid BMP format which you then have to parse.


Say what? None of your statements actualy made sense

>>Won't this be super ridiculously slow?
No, where is the basis of your claim?

>>you could just save a raw dump of the data instead of using the stupid BMP format
So is that "raw dump" format supported by paint or paint.net? oh wait, BMP IS RAW DUMP.

>>which you then have to parse.
parse? what parse? there is no parsing!! It's a straight sequential read of raw <BMP> information!


You can always use this to convert your heightmap images to another <faster> readeable format if you please... Or are you infering you want to make a height map editor in gm, implying you would also need to make a paint program. WHY WOULD YOU?

AHHH!!! Lol, anyway...

Although this code seems to be optimzed, in my experience reading files (and therefore having to loop thousands of times) is very slow in GM. I'm just thinking that there would at least be a few seconds delay in reading like an 800x600px bitmap?

As far as BMP not being a "raw" dump, I was refering to the stupid variable-length header and 4-byte alignment that has to be accounted for. While it is true that you can't just get a 24-bit BBGGRR dump with Paint, I think it would be a good idea to do this yourself for resoures that will be packaged in a game.

And that extra overhead is what I was refering to when I said parsing.

Maybe I'm wrong, but I just don't see this working well :unsure:, no dis to your code.

Edited by LaLaLa, 22 October 2010 - 06:31 AM.

  • 0

Posted ImagePosted Image
GM Ray Tracer (Multi-threaded version)| Batch Explorer (soon) | TI-Basic IDE (soon) | Mandelbrot Set Renderer (soon) |Image Set Viewer


#8 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 22 October 2010 - 07:22 AM

Well, the only thing faster than binary (without having to convert byte to values like that extra script does, mind you I assume GM has a decent binary access implementation) is the file_text_read/write_real (one after the other, no crlf, which is broken in GM8; I know it's the fastest method for reals, that I tested... even ds_read/write is dead slow due to the shear size of the resulting write. In short I know the text_read_real is the fastest method in GM...

Lemmy test here how fast it is...

2048x2048 67 seconds, yep, it's slow, vs 15 seconds for a sequencial file_test_read_real.... Well, that is a shocker, you are right there, I apologize... Let me test with my GMBinFile dll, nope, it's worst.

GM must cache text file for this more complex file system to be faster than binary. I cant say for sure with my bin file dll, overhead of the api call and all. All I can say is that I'm unpleasantly surprized.

Edited by icuurd12b42, 22 October 2010 - 07:24 AM.

  • 1

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#9 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • GMC Elder
  • 9603 posts
  • Version:GM:Studio

Posted 22 October 2010 - 07:47 AM

Perhaps instead of reading it byte-by-byte, you could use my File Read DLL (check the mirrors PlasticineGuy posted here) and use string_char_at() and ord()?

Not sure if string_char_at is faster than file I/O, but I'd assume it is?

-IMP ;) :)
  • 0

:GM123: Are you an artist who likes creating original creature designs? Maybe you can help out with Elementa here! Give it a look; you might like the idea :)

:bunny: :excl: :bunny: :excl: :bunny: :excl: :bunny: :excl: :bunny: :excl: :bunny: :excl: :bunny: :excl: :bunny: :excl: :bunny: :excl: :bunny:


#10 TheMagicNumber

TheMagicNumber

    GMC Member

  • GMC Member
  • 5247 posts
  • Version:Unknown

Posted 22 October 2010 - 12:36 PM

Reading with the text functions and then using the string functions to get the individual bytes should be faster, the binary functions are horrible in GM.
  • 0

#11 slayer 64

slayer 64

    Slayer of gingers

  • GMC Member
  • 4301 posts
  • Version:GM:Studio

Posted 22 October 2010 - 10:03 PM

you guys are crazy.

readBMPfile() is great. the time it takes to read a bmp file is far faster than the time it takes to generate a terrain from the color values in it.

for me, this script has the fastest development time. pop it into a game and done. let's make heightmaps. if you're into processing speed, then go write code in c++ or something. lol.
  • 0

5y5rs3d.pngfg0UQNL.png


#12 TheSnidr

TheSnidr

    Heavy metal viking dentist

  • GMC Elder
  • 3345 posts
  • Version:GM:Studio

Posted 06 December 2011 - 04:40 PM

I can't believe this doesn't have more comments. It's incredibly much faster than the method I've been using until recently, the read color bullcrap. Thanks for the script!
  • 0

#13 sneaky666

sneaky666

    536e65616b79

  • GMC Member
  • 1047 posts
  • Version:GM:Studio

Posted 04 January 2012 - 03:36 AM

This is what I was working on here as well
http://gmc.yoyogames...1

But I create another file (hmp) off the heightmap file, the process is long and makes the hmp file much bigger than the heightmap file, because I store every information you could possible use for a heightmap (but I don't embed a texture in the file) in it, and when loading the heightmap, it loads really really fast because you just real the values from it, and no calculations are done. I store the normals (using cross product) for example, so the model made from it supports lighting. Plus the texture coordinates are set so that a pic gets stretched over the whole map. This is useful because you can do 3d destructible terrain really fast while recalculating all the normals for lighting.

Edited by sneaky666, 04 January 2012 - 03:37 AM.

  • 0

Mountain Adventure 3D Highscore: 44 [◊ TheUltimate ◊]
Shoot Them Up Challenge Edition Winner: [◊ TheUltimate ◊]
Cursor Catch Current Winners: mr magnus || ◊ TheUltimate ◊
Tron Challenge Current Winners: ◊ TheUltimate ◊ [Legendary] || thegame [Easy] || FoxInABox [Easy]

>>>>>Click Here To See My List Of Games<<<<<


#14 Guest_TamoNekiTipo_*

Guest_TamoNekiTipo_*
  • Guests

Posted 11 January 2012 - 09:36 PM

WOW, this is |great|!! :thumbsup:
Keep on the work!

#15 Coga19000

Coga19000

    GMC Member

  • GMC Member
  • 123 posts
  • Version:GM8

Posted 24 May 2012 - 08:41 AM

Okay, I feel I'm going to say something very stupid, but I had to ask. Is this ds grid having something to do with the Nintendo DS?
  • -1

#16 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 24 May 2012 - 04:20 PM

Okay, I feel I'm going to say something very stupid, but I had to ask. Is this ds grid having something to do with the Nintendo DS?

No. it's a data container like ds_list and ds_stack...
  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#17 Coga19000

Coga19000

    GMC Member

  • GMC Member
  • 123 posts
  • Version:GM8

Posted 24 May 2012 - 07:18 PM

Okay, I feel I'm going to say something very stupid, but I had to ask. Is this ds grid having something to do with the Nintendo DS?

No. it's a data container like ds_list and ds_stack...

Oh, okay. And something else, justas the subject was brought. Can a Game Maker game be converted into a Nintendo DS game?
  • -2

#18 Big J

Big J

    GMC Member

  • GMC Member
  • 2853 posts
  • Version:GM8.1

Posted 11 June 2012 - 12:23 PM

Game Maker has had data structures (ds) AT LEAST since version 5.3. This is before the Nintendo DS existed. :thumbsup:

How many Game Maker games have you seen converted to Nintendo DS games? Zero? Exactly.

Edited by Big J, 11 June 2012 - 12:24 PM.

  • 0

Get your GM 8.1 Anti-Aliasing here!

2712265.png

http://www.youtube.com/Sporkinator


#19 InCreator[EST]

InCreator[EST]

    GMC Member

  • GMC Member
  • 144 posts
  • Version:GM:Studio

Posted 23 September 2013 - 07:57 AM

This is absolutely awesome, I can make 1200x800 bmp file, and turn every pixel to a part of 16x16 tileset. Who needs level editors if you can simply open bmp, draw some roads and rivers and let GM turn them into actual game tiles? Also, that's moddabilty out of the box

What I fail to do is getting it to find bmp files anywhere else than working_directory + '\image.bmp'

I tried saving files to HD root and something like

 

readBMPFile(grid_mainmap,'c:\image.bmp')

 

and it still reports file not found. Help?


Edited by InCreator[EST], 23 September 2013 - 07:58 AM.

  • 0

#20 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 23 September 2013 - 09:02 AM

studio limits where you can read from. it's really silly on windows and win8


  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#21 crusaderdesignproject

crusaderdesignproject

    GMC Member

  • GMC Member
  • 291 posts
  • Version:GM8.1

Posted 03 November 2014 - 05:42 AM

I wanna comment on this. I apologize for the necro post BTW... but there is something that should be pointed out and since icuurd still has this in his list of guides I feel the need to point this out.

"the 32bpp support may not be implemented, not a big deal. "

Here is how to fix that, it's VERY easy... just make one small change.

    if (bitsPerPixel==24) {repeat (width mod 4) file_bin_read_byte(file);}

Adding this check for 24bpp is important, BECAUSE there is no such padding for 32bpp bitmaps. After this, it should work.

  • 0

twitter.com/RetroKoH

Game Maker user since 2002

PRIMARY PROJECTS (As of Spetember 2014):
Sonic Triad (Studio PR1 COMING SOON) - Sonic the Hedgehog Mega Drive Disassembly Editing Studio. HIATUS DUE TO MOVING


#22 slayer 64

slayer 64

    Slayer of gingers

  • GMC Member
  • 4301 posts
  • Version:GM:Studio

Posted 03 November 2014 - 07:43 PM

I wanna comment on this. I apologize for the necro post BTW... but there is something that should be pointed out and since icuurd still has this in his list of guides I feel the need to point this out.

"the 32bpp support may not be implemented, not a big deal." 

Here is how to fix that, it's VERY easy... just make one small change.
    if (bitsPerPixel==24) {repeat (width mod 4) file_bin_read_byte(file);}

Adding this check for 24bpp is important, BECAUSE there is no such padding for 32bpp bitmaps. After this, it should work.

what program saves as 32 bit?

Edited by slayer 64, 03 November 2014 - 07:45 PM.

  • 0

5y5rs3d.pngfg0UQNL.png


#23 Ultimate Omicron

Ultimate Omicron

  • GMC:Member
  • 949 posts
  • Version:GM:Studio

Posted 03 November 2014 - 08:57 PM

what program saves as 32 bit?

I am pretty sure Game Maker 8 saves surfaces as 32b bitmaps.

Edit: It does, because I use that mechanism to read images as binary data on a very old game.Skip to 54, loop of read three + dummy read.

Edited by Ultimate Omicron, 03 November 2014 - 09:06 PM.


#24 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 04 November 2014 - 01:48 AM

Yep, I had actually added the 32 bit support after I realised gm8 did 32 bits... not sure if it was added right though so thanks
  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button