User:Rosy.iso

From SRB2 Wiki
(Redirected from User:Felix44)
Jump to navigation Jump to search

Hello, I'm a 18 years old girl that likes to code random Lua things for everyone to enjoy.

I try to edit the wiki sometimes but I mostly fail to cause visual editor sucks lol

How to create a .bmp file with Lua

you first of all need some requirements:

-a general understanding of Lua (duh)

-a general understanding of I/O with lua

-the acknowledgment that most of this comes from https://en.wikipedia.org/wiki/BMP_file_format and that you should probably check that page too

-a 320x200 matrix (you can technically use different ones, but this "tutorial" will assume yours is 320x200)

-this function:

local function C_IntToString(val, size)
	local str = ""
	for i = 0, size-1 do
		str = str..string.char(val & 255)
		val = $ >> 8
	end
	return str
end

-and this massive table:

local ppalette = {
{ r = 255, g = 255, b = 255},
{ r = 246, g = 246, b = 246},
{ r = 237, g = 237, b = 237},
{ r = 228, g = 228, b = 228},
{ r = 218, g = 218, b = 218},
{ r = 209, g = 209, b = 209},
{ r = 200, g = 200, b = 200},
{ r = 191, g = 191, b = 191},
{ r = 183, g = 183, b = 183},
{ r = 175, g = 175, b = 175},
{ r = 167, g = 167, b = 167},
{ r = 159, g = 159, b = 159},
{ r = 151, g = 151, b = 151},
{ r = 143, g = 143, b = 143},
{ r = 135, g = 135, b = 135},
{ r = 127, g = 127, b = 127},
{ r = 119, g = 119, b = 119},
{ r = 111, g = 111, b = 111},
{ r = 103, g = 103, b = 103},
{ r = 95, g = 95, b = 95},
{ r = 87, g = 87, b = 87},
{ r = 79, g = 79, b = 79},
{ r = 71, g = 71, b = 71},
{ r = 63, g = 63, b = 63},
{ r = 55, g = 55, b = 55},
{ r = 47, g = 47, b = 47},
{ r = 39, g = 39, b = 39},
{ r = 31, g = 31, b = 31},
{ r = 23, g = 23, b = 23},
{ r = 15, g = 15, b = 15},
{ r = 7, g = 7, b = 7},
{ r = 0, g = 0, b = 0},
{ r = 255, g = 127, b = 127},
{ r = 255, g = 95, b = 95},
{ r = 255, g = 63, b = 63},
{ r = 255, g = 0, b = 0},
{ r = 239, g = 0, b = 0},
{ r = 223, g = 0, b = 0},
{ r = 207, g = 0, b = 0},
{ r = 191, g = 0, b = 0},
{ r = 175, g = 0, b = 0},
{ r = 159, g = 0, b = 0},
{ r = 143, g = 0, b = 0},
{ r = 127, g = 0, b = 0},
{ r = 111, g = 0, b = 0},
{ r = 95, g = 0, b = 0},
{ r = 71, g = 0, b = 0},
{ r = 55, g = 0, b = 0},
{ r = 255, g = 219, b = 192},
{ r = 255, g = 203, b = 151},
{ r = 255, g = 185, b = 117},
{ r = 255, g = 168, b = 85},
{ r = 255, g = 151, b = 54},
{ r = 255, g = 134, b = 25},
{ r = 255, g = 117, b = 0},
{ r = 236, g = 105, b = 0},
{ r = 221, g = 94, b = 0},
{ r = 208, g = 88, b = 0},
{ r = 196, g = 79, b = 0},
{ r = 181, g = 68, b = 0},
{ r = 159, g = 56, b = 0},
{ r = 138, g = 41, b = 0},
{ r = 129, g = 32, b = 0},
{ r = 121, g = 24, b = 0},
{ r = 235, g = 219, b = 87},
{ r = 215, g = 187, b = 67},
{ r = 195, g = 155, b = 47},
{ r = 175, g = 123, b = 31},
{ r = 155, g = 91, b = 19},
{ r = 135, g = 67, b = 7},
{ r = 117, g = 41, b = 0},
{ r = 85, g = 0, b = 0},
{ r = 255, g = 255, b = 79},
{ r = 255, g = 255, b = 0},
{ r = 227, g = 217, b = 15},
{ r = 201, g = 187, b = 14},
{ r = 170, g = 155, b = 11},
{ r = 136, g = 120, b = 9},
{ r = 112, g = 96, b = 7},
{ r = 90, g = 73, b = 5},
{ r = 255, g = 255, b = 207},
{ r = 255, g = 255, b = 175},
{ r = 255, g = 255, b = 143},
{ r = 255, g = 255, b = 115},
{ r = 235, g = 222, b = 129},
{ r = 208, g = 194, b = 128},
{ r = 183, g = 169, b = 119},
{ r = 150, g = 131, b = 93},
{ r = 222, g = 255, b = 168},
{ r = 199, g = 228, b = 148},
{ r = 173, g = 200, b = 128},
{ r = 149, g = 173, b = 107},
{ r = 124, g = 146, b = 88},
{ r = 100, g = 119, b = 68},
{ r = 74, g = 90, b = 48},
{ r = 50, g = 63, b = 29},
{ r = 119, g = 255, b = 79},
{ r = 112, g = 240, b = 75},
{ r = 105, g = 224, b = 70},
{ r = 97, g = 208, b = 65},
{ r = 90, g = 192, b = 60},
{ r = 82, g = 176, b = 55},
{ r = 75, g = 160, b = 50},
{ r = 67, g = 144, b = 45},
{ r = 60, g = 128, b = 40},
{ r = 53, g = 112, b = 35},
{ r = 45, g = 96, b = 30},
{ r = 38, g = 80, b = 25},
{ r = 30, g = 64, b = 20},
{ r = 23, g = 48, b = 15},
{ r = 15, g = 32, b = 10},
{ r = 7, g = 15, b = 4},
{ r = 0, g = 255, b = 0},
{ r = 0, g = 223, b = 0},
{ r = 0, g = 191, b = 0},
{ r = 0, g = 159, b = 0},
{ r = 0, g = 127, b = 0},
{ r = 0, g = 95, b = 0},
{ r = 0, g = 63, b = 0},
{ r = 0, g = 45, b = 0},
{ r = 183, g = 251, b = 231},
{ r = 102, g = 247, b = 203},
{ r = 21, g = 242, b = 176},
{ r = 11, g = 210, b = 151},
{ r = 3, g = 177, b = 128},
{ r = 2, g = 147, b = 107},
{ r = 2, g = 115, b = 84},
{ r = 1, g = 86, b = 63},
{ r = 206, g = 250, b = 255},
{ r = 166, g = 241, b = 255},
{ r = 117, g = 231, b = 255},
{ r = 87, g = 213, b = 255},
{ r = 79, g = 199, b = 255},
{ r = 71, g = 185, b = 255},
{ r = 55, g = 165, b = 255},
{ r = 32, g = 138, b = 225},
{ r = 24, g = 111, b = 182},
{ r = 21, g = 83, b = 134},
{ r = 14, g = 53, b = 86},
{ r = 7, g = 30, b = 48},
{ r = 116, g = 209, b = 201},
{ r = 66, g = 179, b = 179},
{ r = 23, g = 136, b = 136},
{ r = 0, g = 95, b = 95},
{ r = 231, g = 231, b = 255},
{ r = 198, g = 198, b = 255},
{ r = 173, g = 173, b = 255},
{ r = 140, g = 140, b = 255},
{ r = 115, g = 115, b = 255},
{ r = 82, g = 82, b = 255},
{ r = 49, g = 49, b = 255},
{ r = 24, g = 24, b = 255},
{ r = 0, g = 0, b = 255},
{ r = 0, g = 0, b = 223},
{ r = 0, g = 0, b = 196},
{ r = 0, g = 0, b = 172},
{ r = 0, g = 0, b = 149},
{ r = 0, g = 0, b = 128},
{ r = 0, g = 0, b = 102},
{ r = 0, g = 0, b = 82},
{ r = 216, g = 183, b = 255},
{ r = 199, g = 153, b = 255},
{ r = 173, g = 106, b = 255},
{ r = 152, g = 68, b = 255},
{ r = 127, g = 22, b = 255},
{ r = 107, g = 0, b = 238},
{ r = 91, g = 0, b = 201},
{ r = 72, g = 0, b = 159},
{ r = 51, g = 0, b = 113},
{ r = 36, g = 0, b = 81},
{ r = 151, g = 151, b = 213},
{ r = 119, g = 119, b = 187},
{ r = 84, g = 84, b = 167},
{ r = 65, g = 65, b = 131},
{ r = 46, g = 46, b = 92},
{ r = 33, g = 34, b = 78},
{ r = 255, g = 202, b = 255},
{ r = 255, g = 170, b = 255},
{ r = 255, g = 138, b = 255},
{ r = 255, g = 106, b = 255},
{ r = 255, g = 74, b = 255},
{ r = 255, g = 0, b = 255},
{ r = 221, g = 0, b = 221},
{ r = 191, g = 0, b = 191},
{ r = 162, g = 0, b = 162},
{ r = 121, g = 0, b = 121},
{ r = 85, g = 0, b = 85},
{ r = 53, g = 0, b = 53},
{ r = 197, g = 232, b = 0},
{ r = 167, g = 202, b = 4},
{ r = 140, g = 168, b = 11},
{ r = 108, g = 124, b = 18},
{ r = 207, g = 127, b = 207},
{ r = 183, g = 111, b = 183},
{ r = 159, g = 95, b = 159},
{ r = 135, g = 79, b = 135},
{ r = 111, g = 63, b = 111},
{ r = 87, g = 47, b = 87},
{ r = 64, g = 32, b = 64},
{ r = 43, g = 21, b = 43},
{ r = 255, g = 196, b = 224},
{ r = 255, g = 153, b = 192},
{ r = 245, g = 112, b = 165},
{ r = 221, g = 87, b = 140},
{ r = 199, g = 61, b = 116},
{ r = 177, g = 52, b = 102},
{ r = 157, g = 47, b = 91},
{ r = 133, g = 39, b = 77},
{ r = 255, g = 230, b = 219},
{ r = 255, g = 191, b = 191},
{ r = 255, g = 159, b = 159},
{ r = 225, g = 133, b = 133},
{ r = 204, g = 113, b = 113},
{ r = 194, g = 99, b = 99},
{ r = 181, g = 83, b = 83},
{ r = 167, g = 63, b = 63},
{ r = 255, g = 207, b = 179},
{ r = 255, g = 193, b = 158},
{ r = 255, g = 183, b = 139},
{ r = 247, g = 171, b = 123},
{ r = 239, g = 163, b = 115},
{ r = 227, g = 151, b = 103},
{ r = 215, g = 139, b = 91},
{ r = 207, g = 131, b = 83},
{ r = 191, g = 123, b = 75},
{ r = 179, g = 115, b = 71},
{ r = 171, g = 111, b = 67},
{ r = 163, g = 107, b = 63},
{ r = 155, g = 99, b = 59},
{ r = 143, g = 95, b = 55},
{ r = 135, g = 87, b = 51},
{ r = 127, g = 83, b = 47},
{ r = 119, g = 79, b = 43},
{ r = 107, g = 71, b = 39},
{ r = 95, g = 67, b = 35},
{ r = 83, g = 63, b = 31},
{ r = 75, g = 55, b = 27},
{ r = 63, g = 47, b = 23},
{ r = 51, g = 43, b = 19},
{ r = 43, g = 35, b = 15},
{ r = 191, g = 167, b = 143},
{ r = 175, g = 152, b = 128},
{ r = 159, g = 137, b = 113},
{ r = 146, g = 125, b = 101},
{ r = 134, g = 114, b = 90},
{ r = 126, g = 106, b = 82},
{ r = 117, g = 98, b = 74},
{ r = 109, g = 90, b = 66},
{ r = 101, g = 83, b = 59},
{ r = 93, g = 75, b = 51},
{ r = 87, g = 69, b = 45},
{ r = 75, g = 60, b = 35},
{ r = 255, g = 231, b = 246},
{ r = 0, g = 0, b = 63},
{ r = 0, g = 0, b = 32},
{ r = 0, g = 255, b = 255},
}

now you are all set

first of all get your file with io.openlocal

example: local file = io.openlocal("path/to/your/image.bmp", "w+b"), the b at at the end of the opening method is needed for binary mode

now you need to write a bunch of stuff, with the C_IntToString function

file:write("BM") --so that the file is recognized as a bmp
file:write(C_IntToString(4*320*200+54,4)) --size of the file
file:write(C_IntToString(0,2)) --apparently this needs to be 0
file:write(C_IntToString(0,2)) --same goes here
file:write(C_IntToString(54,4)) --the offset/start of the pixel array
file:write(C_IntToString(40,4)) --size of the header, this should be BITMAPINFOHEADER
file:write(C_IntToString(320,4)) --bitmap width in pixels
file:write(C_IntToString(200,4)) --bitmap length in pixels
file:write(C_IntToString(1,2)) --number of color planes, apparently should be 1 so
file:write(C_IntToString(32,2)) --the number of bits per pixel, which is the color depth of the image
file:write(C_IntToString(0,4)) --compression method, too advanced for me so none
file:write(C_IntToString(4*320*200,4)) --the image size
file:write(C_IntToString(283500,4)) --the horizontal resolution
file:write(C_IntToString(283500,4)) --the vertical resolution
file:write(C_IntToString(0,4)) --the colors in the palette
file:write(C_IntToString(0,4)) --the important colors, all colors have equal rights so

now we can start putting the pixels into your image (this method writes the pixel in BGRA btw)

for y = 199,0,-1
	for x = 0,319
		local color = yourmatrix[x][y]
		file:write(string.char(ppalette[color+1].b)) --writes the blue value
		file:write(string.char(ppalette[color+1].g)) --writes the green value 
		file:write(string.char(ppalette[color+1].r)) --writes the red value
		file:write("\0") --writes the translucency or something, we don't need that so 0 is good
	end
end

dont forget to close your file with file:close() and you are pretty much done, unless I did some mistake