Skip to main content
GameDev.net gamedev.net
🔒 Locked

Lua Read Bitmap

Started by Matthew Shockley May 31, 2010 at 8:57 PM 9 replies 9.9k views
Original Post
Matthew Shockley
Matthew Shockley
This is for an experiment I am doing. I want to read a bitmap in Lua. I don't know anything about them, so this is what I was asking for help. Each loop, I want it to call a function, DrawPoint(x, y, color).

function DrawBitmap(bytecode)    for i = 1, length do    DrawPoint(x, y, color)    endend


This is not meant for efficiency, and I don't want to use any other pre-made libraries or anthing like that.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
Matthew Shockley
Matthew Shockley
Is it even possible?
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
dmail
dmail
You have not given a lot of details.
Are you passing a string of the byte data after parsing the bitmap file or are you trying to parse the bitmap file in Lua.

If you do not understand the bitmap format then you will want to see the links on this page hosted by gamedev to understand the format.

Quote:
Original post by Matthew Shockley
Is it even possible?

This ?
Quote:
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?

If so then no it is not possible.
Matthew Shockley
Matthew Shockley
Lets say I open a bitmap file in notepad, copy everything in it, put it in a string, and call that DrawBitmap function it.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
CableGuy
CableGuy
Where is color assigned a value?

Anyway, that wouldn't work. You need to open the file and parse it, there are lots of resources on the web on reading bmp files.

Hope this helps
Matthew Shockley
Matthew Shockley
How would I parse it in Lua? All the resources I look at show how to read it in C++ or they are libraries.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
CableGuy
CableGuy
Technically speaking you can open the file, read it's content into a string and use string.byte to get the value at a given index, then it is just a matter of understanding the BMP file format.
leeor_net
leeor_net
Quote:
Original post by Matthew Shockley
Lets say I open a bitmap file in notepad, copy everything in it, put it in a string, and call that DrawBitmap function it.


That's not very helpful and more than likely you're going to lose some byte data.

Without having tried it myself in Lua and not being certain about Lua's filesystem operations the basic method would be to open the file in binary mode, copy the file, byte for byte, into a string object of sorts (or an array of byte's) and then interpret that data based on the BMP format (linked to several times before). As you interpret that you'll want to store the interpreted data in an array that contains the RGB values for each pixel (BMP's don't contain alpha values so you can ignore that).

In Lua you can use Tables which can effectively be used as arrays. I've never tried it with complex data types (for instance, a table that stores other tables) but I'm sure it's possible.
Evil Steve
Evil Steve
Because I'm a kind and wonderful person:
function error(err)	-- Replace with your own error output method:	println(err);end-- Helper function: Parse a 16-bit WORD from the binary stringfunction ReadWORD(str, offset)	local loByte = str:byte(offset);	local hiByte = str:byte(offset+1);	return hiByte*256 + loByte;end-- Helper function: Parse a 32-bit DWORD from the binary stringfunction ReadDWORD(str, offset)	local loWord = ReadWORD(str, offset);	local hiWord = ReadWORD(str, offset+2);	return hiWord*65536 + loWord;end-- Process a bitmap file in a string, and call DrawPoint for each pixelfunction DrawBitmap(bytecode)	-------------------------	-- Parse BITMAPFILEHEADER	-------------------------	local offset = 1;	local bfType = ReadWORD(bytecode, offset);	if(bfType ~= 0x4D42) then		error("Not a bitmap file (Invalid BMP magic value)");		return;	end	local bfOffBits = ReadWORD(bytecode, offset+10);	-------------------------	-- Parse BITMAPINFOHEADER	-------------------------	offset = 15; -- BITMAPFILEHEADER is 14 bytes long	local biWidth = ReadDWORD(bytecode, offset+4);	local biHeight = ReadDWORD(bytecode, offset+8);	local biBitCount = ReadWORD(bytecode, offset+14);	local biCompression = ReadDWORD(bytecode, offset+16);	if(biBitCount ~= 24) then		error("Only 24-bit bitmaps supported (Is " .. biBitCount .. "bpp)");		return;	end	if(biCompression ~= 0) then		error("Only uncompressed bitmaps supported (Compression type is " .. biCompression .. ")");		return;	end	---------------------	-- Parse bitmap image	---------------------	for y = biHeight-1, 0, -1 do		offset = bfOffBits + (biWidth*biBitCount/8)*y + 1;		for x = 0, biWidth-1 do			local b = bytecode:byte(offset);			local g = bytecode:byte(offset+1);			local r = bytecode:byte(offset+2);			offset = offset + 3;			DrawPoint(x, biHeight-y-1, r, g, b);		end	endendfunction testBmp(args)	local f = assert(io.open(args, "rb"));	local bmp = f:read("*a");	f:close();	DrawBitmap(bmp);end

In my code, DrawPoint takes 3 seperate RGB values. If you want it to take a Win32 COLORREF instead, change the DrawPoint call to:
DrawPoint(x, biHeight-y-1, r + g*256 + b*65536);

That code assumes a 24-bit bitmap file, and can't handle compressed bitmaps (Although BMPs are generally uncompressed anyway). Post here if you want any of that code explained.
Matthew Shockley
Matthew Shockley
Well... One problem. I don't have access to the io library. How else can I get the bytecode into a string without losing any data. Like a program gets the data and makes it where I can copy and paste it into a string for my lua program.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
Evil Steve
Evil Steve
You can use lua_pushlstring from C/C++ code to pass the BMP file contents to Lua as a function argument.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.