From c00b0d50c6575fb41234edb687dd6a1b83d38dca Mon Sep 17 00:00:00 2001 From: Nils Dagsson Moskopp Date: Sat, 14 May 2022 19:56:02 +0200 Subject: [PATCH] Add usage examples --- README.md | 2 ++ examples.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 examples.lua diff --git a/README.md b/README.md index 9b3293dda..dc7f2ab69 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/examples.lua b/examples.lua new file mode 100644 index 000000000..b832dddaa --- /dev/null +++ b/examples.lua @@ -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")