1
0
Fork 0

Add usage examples

This commit is contained in:
Nils Dagsson Moskopp 2022-05-14 19:56:02 +02:00
parent adb8e45d67
commit c00b0d50c6
Signed by untrusted user who does not match committer: erlehmann
GPG Key ID: A3BC671C35191080
2 changed files with 51 additions and 0 deletions

View File

@ -2,3 +2,5 @@
A TGA Encoder written in Lua without the use of external Libraries.
May be used as a Minetest mod.
See `examples.lua` for example code and usage hints.

49
examples.lua Normal file
View File

@ -0,0 +1,49 @@
dofile("init.lua")
-- encode a bitmap
local _ = { 0, 0, 0 }
local R = { 255, 127, 127 }
local pixels = {
{ _, _, _, _, _, _, _ },
{ _, _, _, R, _, _, _ },
{ _, _, R, R, R, _, _ },
{ _, R, R, R, R, R, _ },
{ _, R, R, R, R, R, _ },
{ _, _, R, _, R, _, _ },
{ _, _, _, _, _, _, _ },
}
tga_encoder.image(pixels):save("bitmap_small.tga")
-- change a single pixel, then rescale the bitmap
local pixels_orig = pixels
pixels_orig[4][4] = { 255, 255, 255 }
local pixels = {}
for x = 1,56,1 do
local x_orig = math.ceil(x/8)
for z = 1,56,1 do
local z_orig = math.ceil(z/8)
local color = pixels_orig[z_orig][x_orig]
pixels[z] = pixels[z] or {}
pixels[z][x] = color
end
end
tga_encoder.image(pixels):save("bitmap_large.tga")
local pixels = {}
for x = 1,16,1 do -- left to right
for z = 1,16,1 do -- bottom to top
local r = math.min(x * 32 - 1, 255)
local g = math.min(z * 32 - 1, 255)
local b = 0
-- blue rectangle in top right corner
if x > 8 and z > 8 then
r = 0
g = 0
b = math.min(z * 16 - 1, 255)
end
local color = { r, g, b }
pixels[z] = pixels[z] or {}
pixels[z][x] = color
end
end
tga_encoder.image(pixels):save("gradients.tga")