Skip to content

Instantly share code, notes, and snippets.

@Gimanua
Last active August 6, 2023 13:43
Show Gist options
  • Save Gimanua/bfc7435267e5f8ab6e3e524ad3c70fce to your computer and use it in GitHub Desktop.
Save Gimanua/bfc7435267e5f8ab6e3e524ad3c70fce to your computer and use it in GitHub Desktop.
Computercraft Teletext
--[[
base64 -- v1.5.3 public domain Lua base64 encoder/decoder
no warranty implied; use at your own risk
Needs bit32.extract function. If not present it's implemented using BitOp
or Lua 5.3 native bit operators. For Lua 5.1 fallbacks to pure Lua
implementation inspired by Rici Lake's post:
http://ricilake.blogspot.co.uk/2007/10/iterating-bits-in-lua.html
author: Ilya Kolbin (iskolbin@gmail.com)
url: github.com/iskolbin/lbase64
COMPATIBILITY
Lua 5.1+, LuaJIT
LICENSE
See end of file for license information.
--]]
local base64 = {}
local extract = _G.bit32 and _G.bit32.extract -- Lua 5.2/Lua 5.3 in compatibility mode
if not extract then
if _G.bit then -- LuaJIT
local shl, shr, band = _G.bit.lshift, _G.bit.rshift, _G.bit.band
extract = function( v, from, width )
return band( shr( v, from ), shl( 1, width ) - 1 )
end
elseif _G._VERSION == "Lua 5.1" then
extract = function( v, from, width )
local w = 0
local flag = 2^from
for i = 0, width-1 do
local flag2 = flag + flag
if v % flag2 >= flag then
w = w + 2^i
end
flag = flag2
end
return w
end
else -- Lua 5.3+
extract = load[[return function( v, from, width )
return ( v >> from ) & ((1 << width) - 1)
end]]()
end
end
function base64.makeencoder( s62, s63, spad )
local encoder = {}
for b64code, char in pairs{[0]='A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y',
'Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2',
'3','4','5','6','7','8','9',s62 or '+',s63 or'/',spad or'='} do
encoder[b64code] = char:byte()
end
return encoder
end
function base64.makedecoder( s62, s63, spad )
local decoder = {}
for b64code, charcode in pairs( base64.makeencoder( s62, s63, spad )) do
decoder[charcode] = b64code
end
return decoder
end
local DEFAULT_ENCODER = base64.makeencoder()
local DEFAULT_DECODER = base64.makedecoder()
local char, concat = string.char, table.concat
function base64.encode( str, encoder, usecaching )
encoder = encoder or DEFAULT_ENCODER
local t, k, n = {}, 1, #str
local lastn = n % 3
local cache = {}
for i = 1, n-lastn, 3 do
local a, b, c = str:byte( i, i+2 )
local v = a*0x10000 + b*0x100 + c
local s
if usecaching then
s = cache[v]
if not s then
s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)])
cache[v] = s
end
else
s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)])
end
t[k] = s
k = k + 1
end
if lastn == 2 then
local a, b = str:byte( n-1, n )
local v = a*0x10000 + b*0x100
t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[64])
elseif lastn == 1 then
local v = str:byte( n )*0x10000
t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[64], encoder[64])
end
return concat( t )
end
function base64.decode( b64, decoder, usecaching )
decoder = decoder or DEFAULT_DECODER
local pattern = '[^%w%+%/%=]'
if decoder then
local s62, s63
for charcode, b64code in pairs( decoder ) do
if b64code == 62 then s62 = charcode
elseif b64code == 63 then s63 = charcode
end
end
pattern = ('[^%%w%%%s%%%s%%=]'):format( char(s62), char(s63) )
end
b64 = b64:gsub( pattern, '' )
local cache = usecaching and {}
local t, k = {}, 1
local n = #b64
local padding = b64:sub(-2) == '==' and 2 or b64:sub(-1) == '=' and 1 or 0
for i = 1, padding > 0 and n-4 or n, 4 do
local a, b, c, d = b64:byte( i, i+3 )
local s
if usecaching then
local v0 = a*0x1000000 + b*0x10000 + c*0x100 + d
s = cache[v0]
if not s then
local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40 + decoder[d]
s = char( extract(v,16,8), extract(v,8,8), extract(v,0,8))
cache[v0] = s
end
else
local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40 + decoder[d]
s = char( extract(v,16,8), extract(v,8,8), extract(v,0,8))
end
t[k] = s
k = k + 1
end
if padding == 1 then
local a, b, c = b64:byte( n-3, n-1 )
local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40
t[k] = char( extract(v,16,8), extract(v,8,8))
elseif padding == 2 then
local a, b = b64:byte( n-3, n-2 )
local v = decoder[a]*0x40000 + decoder[b]*0x1000
t[k] = char( extract(v,16,8))
end
return concat( t )
end
return base64
--[[
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2018 Ilya Kolbin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
--]]
local bitutils = {}
---Parses a number of bits at an index of the bit to start at from a series of bytes, and returns the number formed by the bits
---@param bytes string Bytes to read from
---@param bits integer Number of bits to read
---@param bitIndex integer Starts at index 0
function bitutils.extractBits (bytes, bits, bitIndex)
local startByteIndex = 1 + math.floor(bitIndex / 8)
local endByteIndex = 1 + math.floor((bitIndex + bits - 1) / 8)
local neededBytes = { bytes:byte(startByteIndex, endByteIndex) }
local combinedNumber = 0
for i, byte in ipairs(neededBytes) do
combinedNumber = combinedNumber + bit32.lshift(byte, (i - 1) * 8)
end
return bit32.extract(combinedNumber, bitIndex % 8, bits)
end
return bitutils
local fontMasks = {}
fontMasks.doubleHeightChars = {
['00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'] = ' ',
['00000000019201920192019201920192019201920192019201920192019201920192019200000000000000000192019201920192000000000000000000000000'] = '!',
['00000000081608160816081608160816081608160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'] = '"',
['00000000081608160816081608160816409240924092409208160816081608164092409240924092081608160816081608160816000000000000000000000000'] = '#',
['00000000179217923974397434703470399639961848184801120112022402240448044809240924185418543638363831343134002800280000000000000000'] = '%',
['00000000044804480992099219041904158415841584158420322032099209921588158831003100309630964092409209980998000000000000000000000000'] = '&',
['00000000038403840384038403840384076807680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'] = "'",
['00000000000000000048004801120112009600960192019201920192019201920192019201920192019201920096009601120112004800480000000000000000'] = '(',
['00000000000000000384038404480448019201920096009600960096009600960096009600960096009600960192019204480448038403840000000000000000'] = ')',
['00000000079207920792079204320432043204320224022420442044204420440224022404320432043204320792079207920792000000000000000000000000'] = '*',
['00000000000000000000000000960096009600960096009600960096204620462046204600960096009600960096009600960096000000000000000000000000'] = '+',
['00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000192019201920192019201920384038400000000'] = ',',
['00000000000000000000000000000000000000000000000000000000000000001016101610161016000000000000000000000000000000000000000000000000'] = '-',
['00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000192019201920192000000000000000000000000'] = '.',
['00000000000000000006000600140014002800280056005601120112022402240448044808960896179217923584358430723072000000000000000000000000'] = '/',
['00000000022402240496049609520952182018201548154815481548154815481548154818201820095209520496049602240224000000000000000000000000'] = '0',
['00000000009600960224022404800480009600960096009600960096009600960096009600960096009600960240024005040504000000000000000000000000'] = '1',
['00000000050405041020102007800780001200120028002801200120048004800384038407680768076807681020102010201020000000000000000000000000'] = '2',
['00000000102010201020102000240024004800480120012002520252001200120012001200120012078007801020102005040504000000000000000000000000'] = '3',
['00000000012001200120012002160216021602160408040804080408079207921020102010201020002400240024002400240024000000000000000000000000'] = '4',
['00000000102010201020102007680768076807681016101610201020001200120012001200120012078007801020102005040504000000000000000000000000'] = '5',
['00000000024802480504050403840384076807680768076810161016102010200780078007800780078007801020102005040504000000000000000000000000'] = '6',
['00000000102010201020102000240024002400240048004800480048009600960096009601920192019201920384038403840384000000000000000000000000'] = '7',
['00000000050405041020102007800780078007800780078005040504050405040780078007800780078007801020102005040504000000000000000000000000'] = '8',
['00000000050405041020102007800780078007800780078010201020050805080012001200120012002400240504050404960496000000000000000000000000'] = '9',
['00000000000000000000000000000000000000000000000000000000019201920192019200000000000000000192019201920192000000000000000000000000'] = ':',
['00000000000000000000000000000000000000000000000000000000019201920192019200000000000000000192019201920192019201920384038400000000'] = ';',
['00000000002400240056005601120112022402240448044808960896089608960448044802240224011201120056005600240024000000000000000000000000'] = '<',
['00000000000000000000000000000000000000002044204420442044000000000000000020442044204420440000000000000000000000000000000000000000'] = '=',
['00000000076807680896089604480448022402240112011200560056005600560112011202240224044804480896089607680768000000000000000000000000'] = '>',
['00000000049604961016101607920792079207920048004800960096019201920192019201920192000000000192019201920192000000000000000000000000'] = '?',
['00000000022402240496049609520952182018201548154815481548204420442044204415481548154815481548154815481548000000000000000000000000'] = 'A',
['00000000204020402044204415481548154815481548154820402040204020401548154815481548154815482044204420402040000000000000000000000000'] = 'B',
['00000000050405041020102007800780153615361536153615361536153615361536153615361536078007801020102005040504000000000000000000000000'] = 'C',
['00000000203220322040204015601560154815481548154815481548154815481548154815481548156015602040204020322032000000000000000000000000'] = 'D',
['00000000102010201020102007680768076807680768076810081008100810080768076807680768076807681020102010201020000000000000000000000000'] = 'E',
['00000000102010201020102007680768076807680768076810081008100810080768076807680768076807680768076807680768000000000000000000000000'] = 'F',
['00000000050405041020102007800780153615361536153615361536156415641564156415481548078007801020102005080508000000000000000000000000'] = 'G',
['00000000154815481548154815481548154815481548154820442044204420441548154815481548154815481548154815481548000000000000000000000000'] = 'H',
['00000000100810081008100801920192019201920192019201920192019201920192019201920192019201921008100810081008000000000000000000000000'] = 'I',
['00000000002400240024002400240024002400240024002400240024002400240024002407920792079207921016101604960496000000000000000000000000'] = 'J',
['00000000078007800780078007920792079207920816081610081008100810080816081607920792079207920780078007800780000000000000000000000000'] = 'K',
['00000000076807680768076807680768076807680768076807680768076807680768076807680768076807681020102010201020000000000000000000000000'] = 'L',
['00000000154815481820182019801980204420441772177216121612154815481548154815481548154815481548154815481548000000000000000000000000'] = 'M',
['00000000180418041804180419321932193219321740174017401740164416441644164415961596159615961564156415641564000000000000000000000000'] = 'N',
['00000000049604961016101607920792154815481548154815481548154815481548154815481548079207921016101604960496000000000000000000000000'] = 'O',
['00000000101610161020102007800780078007800780078007800780102010201016101607680768076807680768076807680768000000000000000000000000'] = 'P',
['00000000049604961016101607920792154815481548154815481548154815481548154815481548079207921016101604960496006000600028002800000000'] = 'Q',
['00000000101610161020102007800780078007800780078007800780101610161008100808240824079207920780078007800780000000000000000000000000'] = 'R',
['00000000101610162044204415481548153615361536153620402040102010200012001200120012154815482044204410161016000000000000000000000000'] = 'S',
['00000000409240924092409201920192019201920192019201920192019201920192019201920192019201920192019201920192000000000000000000000000'] = 'T',
['00000000078007800780078007800780078007800780078007800780078007800780078007800780078007801020102005040504000000000000000000000000'] = 'U',
['00000000078007800780078007800780078007800780078007800780092409240408040805040504024002400240024000960096000000000000000000000000'] = 'V',
['00000000307830783078307830783078307830783078307831423142330233023574357440304030387038701548154815481548000000000000000000000000'] = 'W',
['00000000154815481548154807920792079207920432043202240224022402240432043207920792079207921548154815481548000000000000000000000000'] = 'X',
['00000000308430843084308415601560156015600816081610081008048004800192019201920192019201920192019201920192000000000000000000000000'] = 'Y',
['00000000101610161016101600480048004800480096009600960096019201920192019203840384038403841016101610161016000000000000000000000000'] = 'Z',
['00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000204420442044204400000000'] = '_',
['00000000000000000000000000000000000000000504050405080508001200120508050810201020078007801020102005080508000000000000000000000000'] = 'a',
['00000000076807680768076807680768076807681016101610201020078007800780078007800780078007801020102010161016000000000000000000000000'] = 'b',
['00000000000000000000000000000000000000000508050810201020076807680768076807680768076807681020102005080508000000000000000000000000'] = 'c',
['00000000001200120012001200120012001200120508050810201020078007800780078007800780078007801020102005080508000000000000000000000000'] = 'd',
['00000000000000000000000000000000000000000504050410201020078007801020102010161016076807681016101605040504000000000000000000000000'] = 'e',
['00000000011201120240024001920192019201920192019210081008100810080192019201920192019201920192019201920192000000000000000000000000'] = 'f',
['00000000000000000000000000000000000000000508050810201020078007800780078007800780078007801020102005080508001200121020102010161016'] = 'g',
['00000000076807680768076807680768076807681016101610201020078007800780078007800780078007800780078007800780000000000000000000000000'] = 'h',
['00000000009600960096009600000000000000000224022402240224009600960096009600960096009600960504050405040504000000000000000000000000'] = 'i',
['00000000009600960096009600000000000000000096009600960096009600960096009600960096009600960096009600960096009600960480048004480448'] = 'j',
['00000000076807680768076807680768076807680780078008280828100810080992099208160816079207920780078007800780000000000000000000000000'] = 'k',
['00000000022402240224022400960096009600960096009600960096009600960096009600960096009600960504050405040504000000000000000000000000'] = 'l',
['00000000000000000000000000000000000000002012201220462046163816381638163816381638163816381638163816381638000000000000000000000000'] = 'm',
['00000000000000000000000000000000000000002040204020442044154815481548154815481548154815481548154815481548000000000000000000000000'] = 'n',
['00000000000000000000000000000000000000001016101620442044154815481548154815481548154815482044204410161016000000000000000000000000'] = 'o',
['00000000000000000000000000000000000000002040204020442044154815481548154815481548154815482044204420402040153615361536153615361536'] = 'p',
['00000000000000000000000000000000000000001020102020442044154815481548154815481548154815482044204410201020001200120012001200120012'] = 'q',
['00000000000000000000000000000000000000000892089210201020089608960768076807680768076807680768076807680768000000000000000000000000'] = 'r',
['00000000000000000000000000000000000000000508050810201020076807681016101605080508001200121020102010161016000000000000000000000000'] = 's',
['00000000019201920192019201920192019201921008100810081008019201920192019201920192019201920240024001120112000000000000000000000000'] = 't',
['00000000000000000000000000000000000000000780078007800780078007800780078007800780078007801020102005080508000000000000000000000000'] = 'u',
['00000000000000000000000000000000000000000780078007800780078007800780078004080408040804080240024000960096000000000000000000000000'] = 'v',
['00000000000000000000000000000000000000001542154215421542163816381638163816381638163816382046204609240924000000000000000000000000'] = 'w',
['00000000000000000000000000000000000000001548154818201820095209520496049604960496095209521820182015481548000000000000000000000000'] = 'x',
['00000000000000000000000000000000000000000780078007800780078007800780078007800780078007801020102005080508001200121020102010161016'] = 'y',
['00000000000000000000000000000000000000001020102010201020002400240120012004800480038403841020102010201020000000000000000000000000'] = 'z',
['80648064806480648064806480648064806480640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'] = string.char(129),
['01270127012701270127012701270127012701270000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'] = string.char(130),
['81918191819181918191819181918191819181910000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'] = string.char(131),
['00000000000000000000000000000000000000008064806480648064806480648064806480648064806480640000000000000000000000000000000000000000'] = string.char(132),
['80648064806480648064806480648064806480648064806480648064806480648064806480648064806480640000000000000000000000000000000000000000'] = string.char(133),
['01270127012701270127012701270127012701278064806480648064806480648064806480648064806480640000000000000000000000000000000000000000'] = string.char(134),
['81918191819181918191819181918191819181918064806480648064806480648064806480648064806480640000000000000000000000000000000000000000'] = string.char(135),
['00000000000000000000000000000000000000000127012701270127012701270127012701270127012701270000000000000000000000000000000000000000'] = string.char(136),
['80648064806480648064806480648064806480640127012701270127012701270127012701270127012701270000000000000000000000000000000000000000'] = string.char(137),
['01270127012701270127012701270127012701270127012701270127012701270127012701270127012701270000000000000000000000000000000000000000'] = string.char(138),
['81918191819181918191819181918191819181910127012701270127012701270127012701270127012701270000000000000000000000000000000000000000'] = string.char(139),
['00000000000000000000000000000000000000008191819181918191819181918191819181918191819181910000000000000000000000000000000000000000'] = string.char(140),
['80648064806480648064806480648064806480648191819181918191819181918191819181918191819181910000000000000000000000000000000000000000'] = string.char(141),
['01270127012701270127012701270127012701278191819181918191819181918191819181918191819181910000000000000000000000000000000000000000'] = string.char(142),
['81918191819181918191819181918191819181918191819181918191819181918191819181918191819181910000000000000000000000000000000000000000'] = string.char(143),
['00000000000000000000000000000000000000000000000000000000000000000000000000000000000000008064806480648064806480648064806480648064'] = string.char(144),
['80648064806480648064806480648064806480640000000000000000000000000000000000000000000000008064806480648064806480648064806480648064'] = string.char(145),
['01270127012701270127012701270127012701270000000000000000000000000000000000000000000000008064806480648064806480648064806480648064'] = string.char(146),
['81918191819181918191819181918191819181910000000000000000000000000000000000000000000000008064806480648064806480648064806480648064'] = string.char(147),
['00000000000000000000000000000000000000008064806480648064806480648064806480648064806480648064806480648064806480648064806480648064'] = string.char(148),
['80648064806480648064806480648064806480648064806480648064806480648064806480648064806480648064806480648064806480648064806480648064'] = string.char(149),
['01270127012701270127012701270127012701278064806480648064806480648064806480648064806480648064806480648064806480648064806480648064'] = string.char(150),
['81918191819181918191819181918191819181918064806480648064806480648064806480648064806480648064806480648064806480648064806480648064'] = string.char(151),
['00000000000000000000000000000000000000000127012701270127012701270127012701270127012701278064806480648064806480648064806480648064'] = string.char(152),
['80648064806480648064806480648064806480640127012701270127012701270127012701270127012701278064806480648064806480648064806480648064'] = string.char(153),
['01270127012701270127012701270127012701270127012701270127012701270127012701270127012701278064806480648064806480648064806480648064'] = string.char(154),
['81918191819181918191819181918191819181910127012701270127012701270127012701270127012701278064806480648064806480648064806480648064'] = string.char(155),
['00000000000000000000000000000000000000008191819181918191819181918191819181918191819181918064806480648064806480648064806480648064'] = string.char(156),
['80648064806480648064806480648064806480648191819181918191819181918191819181918191819181918064806480648064806480648064806480648064'] = string.char(157),
['01270127012701270127012701270127012701278191819181918191819181918191819181918191819181918064806480648064806480648064806480648064'] = string.char(158),
['81918191819181918191819181918191819181918191819181918191819181918191819181918191819181918064806480648064806480648064806480648064'] = string.char(159),
['00000000307830781548154810161016204420441548154815481548154815481548154820442044101610161548154830783078000000000000000000000000'] = '¤',
['07920792079207920000000002240224049604960952095218201820154815482044204420442044154815481548154815481548000000000000000000000000'] = 'Ä',
['02240224043204320224022402240224049604960952095218201820154815482044204420442044154815481548154815481548000000000000000000000000'] = 'Å',
['00240024004800480096009610201020102010200768076807680768100810081008100807680768076807681020102010201020000000000000000000000000'] = 'É',
['07920792079207920000000004960496101610160792079215481548154815481548154815481548079207921016101604960496000000000000000000000000'] = 'Ö',
['03960396039603960000000007800780078007800780078007800780078007800780078007800780078007801020102005040504000000000000000000000000'] = 'Ü',
['00000000039603960396039600000000000000000504050405080508001200120508050810201020078007801020102005080508000000000000000000000000'] = 'ä',
['00000000011201120216021601120112000000000504050405080508001200120508050810201020078007801020102005080508000000000000000000000000'] = 'å',
['00000000002400240048004800960096000000000504050410201020078007801020102010161016076807681016101605040504000000000000000000000000'] = 'é',
['00000000079207920792079200000000000000001016101620442044154815481548154815481548154815482044204410161016000000000000000000000000'] = 'ö',
['00000000039603960396039600000000000000000780078007800780078007800780078007800780078007801020102005080508000000000000000000000000'] = 'ü'
}
fontMasks.normalHeightChars = {
['0000000000000000000000000000000000000000000000000000000000000000'] = ' ',
['0000019201920192019201920192019201920000000001920192000000000000'] = '!',
['0000081608160816081600000000000000000000000000000000000000000000'] = '"',
['0000081608160816409240920816081640924092081608160816000000000000'] = '#',
['0000179239743470399618480112022404480924185436383134002800000000'] = '%',
['0000044809921904158415842032099215883100309640920998000000000000'] = '&',
['0000038403840384076800000000000000000000000000000000000000000000'] = "'",
['0000000000480112009601920192019201920192019200960112004800000000'] = '(',
['0000000003840448019200960096009600960096009601920448038400000000'] = ')',
['0000079207920432043202242044204402240432043207920792000000000000'] = '*',
['0000000000000096009600960096204620460096009600960096000000000000'] = '+',
['0000000000000000000000000000000000000000000001920192019203840000'] = ',',
['0000000000000000000000000000000010161016000000000000000000000000'] = '-',
['0000000000000000000000000000000000000000000001920192000000000000'] = '.',
['0000000000060014002800560112022404480896179235843072000000000000'] = '/',
['0000022404960952182015481548154815481820095204960224000000000000'] = '0',
['0000009602240480009600960096009600960096009602400504000000000000'] = '1',
['0000050410200780001200280120048003840768076810201020000000000000'] = '2',
['0000102010200024004801200252001200120012078010200504000000000000'] = '3',
['0000012001200216021604080408079210201020002400240024000000000000'] = '4',
['0000102010200768076810161020001200120012078010200504000000000000'] = '5',
['0000024805040384076807681016102007800780078010200504000000000000'] = '6',
['0000102010200024002400480048009600960192019203840384000000000000'] = '7',
['0000050410200780078007800504050407800780078010200504000000000000'] = '8',
['0000050410200780078007801020050800120012002405040496000000000000'] = '9',
['0000000000000000000000000000019201920000000001920192000000000000'] = ':',
['0000000000000000000000000000019201920000000001920192019203840000'] = ';',
['0000002400560112022404480896089604480224011200560024000000000000'] = '<',
['0000000000000000000020442044000000002044204400000000000000000000'] = '=',
['0000076808960448022401120056005601120224044808960768000000000000'] = '>',
['0000049610160792079200480096019201920192000001920192000000000000'] = '?',
['0000022404960952182015481548204420441548154815481548000000000000'] = 'A',
['0000204020441548154815482040204015481548154820442040000000000000'] = 'B',
['0000050410200780153615361536153615361536078010200504000000000000'] = 'C',
['0000203220401560154815481548154815481548156020402032000000000000'] = 'D',
['0000102010200768076807681008100807680768076810201020000000000000'] = 'E',
['0000102010200768076807681008100807680768076807680768000000000000'] = 'F',
['0000050410200780153615361536156415641548078010200508000000000000'] = 'G',
['0000154815481548154815482044204415481548154815481548000000000000'] = 'H',
['0000100810080192019201920192019201920192019210081008000000000000'] = 'I',
['0000002400240024002400240024002400240792079210160496000000000000'] = 'J',
['0000078007800792079208161008100808160792079207800780000000000000'] = 'K',
['0000076807680768076807680768076807680768076810201020000000000000'] = 'L',
['0000154818201980204417721612154815481548154815481548000000000000'] = 'M',
['0000180418041932193217401740164416441596159615641564000000000000'] = 'N',
['0000049610160792154815481548154815481548079210160496000000000000'] = 'O',
['0000101610200780078007800780102010160768076807680768000000000000'] = 'P',
['0000049610160792154815481548154815481548079210160496006000280000'] = 'Q',
['0000101610200780078007800780101610080824079207800780000000000000'] = 'R',
['0000101620441548153615362040102000120012154820441016000000000000'] = 'S',
['0000409240920192019201920192019201920192019201920192000000000000'] = 'T',
['0000078007800780078007800780078007800780078010200504000000000000'] = 'U',
['0000078007800780078007800780092404080504024002400096000000000000'] = 'V',
['0000307830783078307830783142330235744030387015481548000000000000'] = 'W',
['0000154815480792079204320224022404320792079215481548000000000000'] = 'X',
['0000308430841560156008161008048001920192019201920192000000000000'] = 'Y',
['0000101610160048004800960096019201920384038410161016000000000000'] = 'Z',
['0000000000000000000000000000000000000000000000000000204420440000'] = '_',
['0000000000000000000005040508001205081020078010200508000000000000'] = 'a',
['0000076807680768076810161020078007800780078010201016000000000000'] = 'b',
['0000000000000000000005081020076807680768076810200508000000000000'] = 'c',
['0000001200120012001205081020078007800780078010200508000000000000'] = 'd',
['0000000000000000000005041020078010201016076810160504000000000000'] = 'e',
['0000011202400192019201921008100801920192019201920192000000000000'] = 'f',
['0000000000000000000005081020078007800780078010200508001210201016'] = 'g',
['0000076807680768076810161020078007800780078007800780000000000000'] = 'h',
['0000009600960000000002240224009600960096009605040504000000000000'] = 'i',
['0000009600960000000000960096009600960096009600960096009604800448'] = 'j',
['0000076807680768076807800828100809920816079207800780000000000000'] = 'k',
['0000022402240096009600960096009600960096009605040504000000000000'] = 'l',
['0000000000000000000020122046163816381638163816381638000000000000'] = 'm',
['0000000000000000000020402044154815481548154815481548000000000000'] = 'n',
['0000000000000000000010162044154815481548154820441016000000000000'] = 'o',
['0000000000000000000020402044154815481548154820442040153615361536'] = 'p',
['0000000000000000000010202044154815481548154820441020001200120012'] = 'q',
['0000000000000000000008921020089607680768076807680768000000000000'] = 'r',
['0000000000000000000005081020076810160508001210201016000000000000'] = 's',
['0000019201920192019210081008019201920192019202400112000000000000'] = 't',
['0000000000000000000007800780078007800780078010200508000000000000'] = 'u',
['0000000000000000000007800780078007800408040802400096000000000000'] = 'v',
['0000000000000000000015421542163816381638163820460924000000000000'] = 'w',
['0000000000000000000015481820095204960496095218201548000000000000'] = 'x',
['0000000000000000000007800780078007800780078010200508001210201016'] = 'y',
['0000000000000000000010201020002401200480038410201020000000000000'] = 'z',
['8064806480648064806400000000000000000000000000000000000000000000'] = string.char(129),
['0127012701270127012700000000000000000000000000000000000000000000'] = string.char(130),
['8191819181918191819100000000000000000000000000000000000000000000'] = string.char(131),
['0000000000000000000080648064806480648064806400000000000000000000'] = string.char(132),
['8064806480648064806480648064806480648064806400000000000000000000'] = string.char(133),
['0127012701270127012780648064806480648064806400000000000000000000'] = string.char(134),
['8191819181918191819180648064806480648064806400000000000000000000'] = string.char(135),
['0000000000000000000001270127012701270127012700000000000000000000'] = string.char(136),
['8064806480648064806401270127012701270127012700000000000000000000'] = string.char(137),
['0127012701270127012701270127012701270127012700000000000000000000'] = string.char(138),
['8191819181918191819101270127012701270127012700000000000000000000'] = string.char(139),
['0000000000000000000081918191819181918191819100000000000000000000'] = string.char(140),
['8064806480648064806481918191819181918191819100000000000000000000'] = string.char(141),
['0127012701270127012781918191819181918191819100000000000000000000'] = string.char(142),
['8191819181918191819181918191819181918191819100000000000000000000'] = string.char(143),
['0000000000000000000000000000000000000000000080648064806480648064'] = string.char(144),
['8064806480648064806400000000000000000000000080648064806480648064'] = string.char(145),
['0127012701270127012700000000000000000000000080648064806480648064'] = string.char(146),
['8191819181918191819100000000000000000000000080648064806480648064'] = string.char(147),
['0000000000000000000080648064806480648064806480648064806480648064'] = string.char(148),
['8064806480648064806480648064806480648064806480648064806480648064'] = string.char(149),
['0127012701270127012780648064806480648064806480648064806480648064'] = string.char(150),
['8191819181918191819180648064806480648064806480648064806480648064'] = string.char(151),
['0000000000000000000001270127012701270127012780648064806480648064'] = string.char(152),
['8064806480648064806401270127012701270127012780648064806480648064'] = string.char(153),
['0127012701270127012701270127012701270127012780648064806480648064'] = string.char(154),
['8191819181918191819101270127012701270127012780648064806480648064'] = string.char(155),
['0000000000000000000081918191819181918191819180648064806480648064'] = string.char(156),
['8064806480648064806481918191819181918191819180648064806480648064'] = string.char(157),
['0127012701270127012781918191819181918191819180648064806480648064'] = string.char(158),
['8191819181918191819181918191819181918191819180648064806480648064'] = string.char(159),
['0000307815481016204415481548154815482044101615483078000000000000'] = '¤',
['0792079200000224049609521820154820442044154815481548000000000000'] = 'Ä',
['0224043202240224049609521820154820442044154815481548000000000000'] = 'Å',
['0024004800961020102007680768100810080768076810201020000000000000'] = 'É',
['0792079200000496101607921548154815481548079210160496000000000000'] = 'Ö',
['0396039600000780078007800780078007800780078010200504000000000000'] = 'Ü',
['0000039603960000000005040508001205081020078010200508000000000000'] = 'ä',
['0000011202160112000005040508001205081020078010200508000000000000'] = 'å',
['0000002400480096000005041020078010201016076810160504000000000000'] = 'é',
['0000079207920000000010162044154815481548154820441016000000000000'] = 'ö',
['0000039603960000000007800780078007800780078010200508000000000000'] = 'ü'
}
return fontMasks
---Module which handles decoding of the image format "GIF" specified by https://www.w3.org/Graphics/GIF/spec-gif89a.txt
local gif = {}
local bitutils = require('bitutils')
local fixedValues = {
imageSeparator = 0x2C,
extensionIntroducer = 0x21,
commentLabel = 0xFE,
graphicControlLabel = 0xF9,
plainTextLabel = 0x01,
applicationExtensionLabel = 0xFF,
trailer = 0x3B
}
---Extracts the Header as specified in section 17 by the GIF89a specification
---@param gifData string
local function extractHeader (gifData)
local headerBlock = gifData:sub(1, 6)
return {
signature = headerBlock:sub(1, 3),
version = headerBlock:sub(4, 6)
}
end
---Extracts the Logical Screen Descriptor as specified in section 18 by the GIF89a specification
---@param gifData string
local function extractLogicalScreenDescriptor (gifData)
local logicalScreenDescriptorBlock = gifData:sub(7, 7 + 7 - 1)
local packedFields = logicalScreenDescriptorBlock:byte(5)
return {
logicalScreenWidth = bit32.lshift(logicalScreenDescriptorBlock:byte(2), 8) + logicalScreenDescriptorBlock:byte(1),
logicalScreenHeight = bit32.lshift(logicalScreenDescriptorBlock:byte(4), 8) + logicalScreenDescriptorBlock:byte(3),
globalColorTableFlag = bit32.extract(packedFields, 7),
colorResolution = bit32.extract(packedFields, 4, 3),
sortFlag = bit32.extract(packedFields, 3),
sizeOfGlobalColorTable = bit32.extract(packedFields, 0, 3),
backgroundColorIndex = logicalScreenDescriptorBlock:byte(6),
pixelAspectRatio = logicalScreenDescriptorBlock:byte(7)
}
end
---Extracts the <Logical Screen> as specified in Appendix B GIF Grammar by the GIF89a specification
---@param gifData string
---@return table logicalScreen, integer logicalScreenEndByte
local function extractLogicalScreen (gifData)
local logicalScreenDescriptor = extractLogicalScreenDescriptor(gifData)
if logicalScreenDescriptor.globalColorTableFlag == 1 then
error('Global Color Table not implemented')
end
local logicalScreen = {
logicalScreenDescriptor = logicalScreenDescriptor,
globalColorTable = nil
}
return logicalScreen, 14 -- We don't implement Global Color Table so we always know that datas (can) begin at byte 14
end
---Extracts an Image Descriptor as specified in section 20 by the GIF89a specification, also returns the index of the byte after
---@param gifData string
---@param index integer The n:th byte to start parsing an Image Descriptor in the gifData
local function extractImageDescriptor (gifData, index)
local imageDescriptorBlock = gifData:sub(index, index + 9)
local packedFields = imageDescriptorBlock:byte(10)
local imageDescriptor = {
imageSeparator = imageDescriptorBlock:byte(1),
imageLeftPosition = bit32.lshift(imageDescriptorBlock:byte(3), 8) + imageDescriptorBlock:byte(2),
imageTopPosition = bit32.lshift(imageDescriptorBlock:byte(5), 8) + imageDescriptorBlock:byte(4),
imageWidth = bit32.lshift(imageDescriptorBlock:byte(7), 8) + imageDescriptorBlock:byte(6),
imageHeight = bit32.lshift(imageDescriptorBlock:byte(9), 8) + imageDescriptorBlock:byte(8),
localColorTableFlag = bit32.extract(packedFields, 7),
interlaceFlag = bit32.extract(packedFields, 6),
sortFlag = bit32.extract(packedFields, 5),
reserved = bit32.extract(packedFields, 3, 2),
sizeOfLocalColorTable = bit32.extract(packedFields, 0, 3)
}
return imageDescriptor, index + 10
end
---Extracts a Local Color Table as specified in section 21 by the GIF89a specification, also returns the index of the byte after
---@param gifData string
---@param index integer The n:th byte to start parsing a Local Color Table in the gifData
---@param sizeOfLocalColorTable integer The size of the Local Color Table from related Image Descriptor
local function extractLocalColorTable (gifData, index, sizeOfLocalColorTable)
local localColorTableBytes = 3 * math.pow(2, sizeOfLocalColorTable + 1)
local localColorTable = {}
for i = index, index + localColorTableBytes - 1,3 do
table.insert(localColorTable, {
red = gifData:byte(i),
green = gifData:byte(i + 1),
blue = gifData:byte(i + 2)
})
end
return localColorTable, index + localColorTableBytes
end
---Extracts Data Sub-Blocks as specified in section 15 by the GIF89a specification, also returns the index of the byte after
---@param gifData string
---@param index integer The n:th byte to start parsing Data Sub-Blocks in the gifData
local function extractDataSubBlocks (gifData, index)
local subBlocks = {}
while true do
local subBlock = {
blockSize = gifData:byte(index),
dataValues = {}
}
for i = 1, subBlock.blockSize do
table.insert(subBlock.dataValues, gifData:byte(index + i))
end
index = index + subBlock.blockSize + 1
table.insert(subBlocks, subBlock)
if subBlock.blockSize == 0 then
break
end
end
return subBlocks, index
end
---LZW Decoding stuff
---@param imageDataBytes string
---@param lzwMinimumCodeSize integer
local function lzwDecode (imageDataBytes, lzwMinimumCodeSize)
local dictionary = {}
local function generateDictionary ()
local newDictionary = {}
-- + 1 to reserve the clearCode and endOfInformationCode
for i = 0, math.pow(2, lzwMinimumCodeSize) + 1 do
newDictionary[i] = { i }
end
return newDictionary
end
local function flattenToTable (...)
local newTable = {}
for _, v in ipairs(arg) do
if type(v) == 'table' then
for _,v2 in ipairs(v) do table.insert(newTable, v2) end
else
table.insert(newTable, v)
end
end
return newTable
end
local bitIndex = 0
local clearCode = math.pow(2, lzwMinimumCodeSize)
local endOfInformationCode = clearCode + 1
local currentCodeSize = lzwMinimumCodeSize + 1
local decoded = {}
local previousCode = nil
while true do
local currentCode = bitutils.extractBits(imageDataBytes, currentCodeSize, bitIndex)
bitIndex = bitIndex + currentCodeSize
if currentCode == endOfInformationCode then
break
elseif currentCode == clearCode then
-- Hacky way to make sure ComputerCraft doesn't terminate this program due to it taking too long without yielding
os.queueEvent('fakeEvent')
os.pullEvent()
dictionary = generateDictionary()
currentCodeSize = lzwMinimumCodeSize + 1
elseif previousCode == clearCode then
table.insert(decoded, currentCode)
elseif dictionary[currentCode] == nil then
local K = dictionary[previousCode][1]
local combined = flattenToTable(dictionary[previousCode], K)
for _,v in ipairs(combined) do table.insert(decoded, v) end
table.insert(dictionary, combined)
else
for _,v in ipairs(dictionary[currentCode]) do table.insert(decoded, v) end
local K = dictionary[currentCode][1]
local combined = flattenToTable(dictionary[previousCode], K)
table.insert(dictionary, combined)
end
if #dictionary == math.pow(2, currentCodeSize) - 1 and currentCodeSize < 12 then
currentCodeSize = currentCodeSize + 1
end
previousCode = currentCode
end
return decoded
end
---Extracts Table Based Image Data as specified in section 22 and Appendix F by the GIF89a specification, also returns the index of the byte after
---@param gifData string
---@param index integer The n:th byte to start parsing Table Based Image Data in the gifData
local function extractTableBasedImageData (gifData, index)
local tableBasedImageData = {}
tableBasedImageData.lzwMinimumCodeSize = gifData:byte(index)
index = index + 1
tableBasedImageData.imageData, index = extractDataSubBlocks(gifData, index)
local imageDataByteChars = {}
for _, subBlock in ipairs(tableBasedImageData.imageData) do
for _, dataValue in ipairs(subBlock.dataValues) do
table.insert(imageDataByteChars, string.char(dataValue))
end
end
local imageDataBytes = table.concat(imageDataByteChars)
tableBasedImageData.pixelsColorIndexes = lzwDecode(imageDataBytes, tableBasedImageData.lzwMinimumCodeSize)
return tableBasedImageData, index
end
---Extracts a <Table-Based Image> as specified in Appendix B GIF Grammar by the GIF89a specification
---@param gifData string
---@param index integer The n:th byte to start parsing <Table-Based Image> in the gifData
local function extractTableBasedImage (gifData, index)
local tableBasedImage = {}
tableBasedImage.imageDescriptor, index = extractImageDescriptor(gifData, index)
if tableBasedImage.imageDescriptor.localColorTableFlag == 1 then
tableBasedImage.localColorTable, index = extractLocalColorTable(gifData, index, tableBasedImage.imageDescriptor.sizeOfLocalColorTable)
end
tableBasedImage.imageData = extractTableBasedImageData(gifData, index)
return tableBasedImage
end
---Extracts the <Data>s as specified in Appendix B GIF Grammar by the GIF89a specification
---@param gifData string
---@param index integer The n:th byte to start searching for <Data> in the gifData
local function extractDatas (gifData, index)
local datas = {}
while true do
local nextByte = gifData:byte(index)
if nextByte == fixedValues.trailer then
break
elseif nextByte == fixedValues.extensionIntroducer then
error('No extensions are implemented')
elseif nextByte == fixedValues.imageSeparator then
local graphicBlock = {
graphicControlExtension = nil,
graphicRenderingBlock = extractTableBasedImage(gifData, index)
}
table.insert(datas, graphicBlock)
else
error(string.format('Unknown data type beginning with %d at index %d', nextByte, index))
end
break -- TODO Fix this loop
end
return datas
end
---Decodes GIF data
---@param gifData string GIF bytes. Don't be fooled by the string type, string.byte is used to check the byte value of each char in the string
function gif.decode (gifData)
local logicalScreen, logicalScreenEndByte = extractLogicalScreen(gifData)
return {
header = extractHeader(gifData),
logicalScreen = logicalScreen,
datas = extractDatas(gifData, logicalScreenEndByte)
}
end
return gif
local base64 = require('base64')
local gif = require('gif')
local fontMasks = require('fontMasks')
local teletextColorPalette = require('teletextColorPalette')
local args = { ... }
local page = tonumber(args[1])
local request = http.get(string.format('https://www.svt.se/text-tv/%d', page))
local htmlPage = request.readAll()
request.close()
local base64GIFSubPages = {}
for base64GIFSubPage in string.gmatch(htmlPage, '<img class="Content_pageImage__MwTIV" src="data:image/gif;base64,(.-)"') do
table.insert(base64GIFSubPages, base64GIFSubPage)
end
local rows = 25
local columns = 40
local pageWidth = 520
local pageHeight = 400
local cellWidth = pageWidth / columns
local cellHeight = pageHeight / rows
-- Used for unknown characters
local upsideDownQuestionMark = string.char(191)
-- Used for the bottom cell of a double height character
local doubleHeightBottomMark = string.char(175)
local function isGraphicsChar (char)
local byte = string.byte(char)
return byte >= 129 and byte <= 159
end
local function charMaskToChar (charMask)
return fontMasks.normalHeightChars[charMask] or fontMasks.doubleHeightChars[charMask]
end
---Extracts information about a cell
---@param pixelColorIndexes table
---@param row number
---@param column number
---@param doubleHeight boolean
local function extractCell(pixelColorIndexes, row, column, doubleHeight)
if doubleHeight and row == rows - 1 then
return nil, nil
end
local cellOffset = row * cellHeight * pageWidth + column * cellWidth
-- Redefine cellHeight to optionally double it
local cellHeight = cellHeight * (doubleHeight and 2 or 1)
-- Check bottom right corner for background color index, because it's easy and aligns well with how the graphic chars work
local backgroundColorIndex = pixelColorIndexes[1 + cellOffset + (cellHeight - 1) * pageWidth + cellWidth - 1]
local foregroundColorIndex = backgroundColorIndex
local rowMasks = {}
for y = 0, cellHeight - 1 do
local yOffset = y * pageWidth
local rowMask = 0
for x = 0, cellWidth - 1 do
local xOffset = x
local pixelColorIndex = pixelColorIndexes[1 + cellOffset + yOffset + xOffset]
if pixelColorIndex ~= backgroundColorIndex then
rowMask = rowMask + bit32.lshift(1, cellWidth - 1 - x)
if foregroundColorIndex == backgroundColorIndex then foregroundColorIndex = pixelColorIndex end
end
end
table.insert(rowMasks, string.format('%04d', rowMask))
end
local charMask = table.concat(rowMasks)
local char = charMaskToChar(charMask)
if char == nil then
return { char = upsideDownQuestionMark, foregroundColorIndex = foregroundColorIndex, backgroundColorIndex = backgroundColorIndex }
elseif doubleHeight and char ~= ' ' then
-- If char is a space character we cannot actually be sure if it's intended to be a double height char or not,
-- but it's best to assume it's a normal height char as to not mark the empty cell below as a bottom double height cell
-- because it could actually be the top of a '-' double height char for example.
return
{ char = char, foregroundColorIndex = foregroundColorIndex, backgroundColorIndex = backgroundColorIndex },
-- Only use doubleHeightBottomMark for non-graphical double height chars
{ char = isGraphicsChar(char) and char or doubleHeightBottomMark, foregroundColorIndex = foregroundColorIndex, backgroundColorIndex = backgroundColorIndex }
else
return { char = char, foregroundColorIndex = foregroundColorIndex, backgroundColorIndex = backgroundColorIndex }
end
end
local function extractCells(pixels)
local cellRows = {}
for _ = 1, rows do table.insert(cellRows, {}) end
for row = 0, rows - 1 do
for column = 0, columns - 1 do
if cellRows[row + 1][column + 1] == nil then -- Only check cells which aren't already resolved
local cell, cellBelow = extractCell(pixels, row, column, true) -- Try to parse as double height
if cellBelow == nil then cell = extractCell(pixels, row, column, false) end -- If unable then parse as normal height
cellRows[row + 1][column + 1] = cell
if cellBelow ~= nil then cellRows[row + 2][column + 1] = cellBelow end
end
end
end
return cellRows
end
local monitor = peripheral.find('monitor')
teletextColorPalette.load(monitor)
monitor.clear()
monitor.setTextScale(0.5)
local subPageIndex = 1
local centeredXOffset = (function ()
local monitorWidth = monitor.getSize()
return 1 + math.floor((monitorWidth - columns) / 2)
end)()
while true do
local gifBytes = base64.decode(base64GIFSubPages[subPageIndex])
local decodedGIF = gif.decode(gifBytes)
local cells = extractCells(decodedGIF.datas[1].graphicRenderingBlock.imageData.pixelsColorIndexes)
for cellRowIndex, cellRow in ipairs(cells) do
monitor.setCursorPos(centeredXOffset, cellRowIndex)
local chars, foregroundBlits, backgroundBlits = {}, {}, {}
for _, cell in ipairs(cellRow) do
local foregroundColor = decodedGIF.datas[1].graphicRenderingBlock.localColorTable[1 + cell.foregroundColorIndex]
local backgroundColor = decodedGIF.datas[1].graphicRenderingBlock.localColorTable[1 + cell.backgroundColorIndex]
local foregroundBlit = teletextColorPalette.getBlitChar(foregroundColor.red, foregroundColor.green,
foregroundColor.blue)
local backgroundBlit = teletextColorPalette.getBlitChar(backgroundColor.red, backgroundColor.green,
backgroundColor.blue)
table.insert(chars, cell.char)
table.insert(foregroundBlits, foregroundBlit:rep(cell.char:len()))
table.insert(backgroundBlits, backgroundBlit:rep(cell.char:len()))
end
monitor.blit(table.concat(chars), table.concat(foregroundBlits), table.concat(backgroundBlits))
end
if #base64GIFSubPages == 1 then
break
end
sleep(6)
subPageIndex = subPageIndex + 1
if subPageIndex > #base64GIFSubPages then
subPageIndex = 1
end
end
local teletextColorPalette = {}
local blitMap = {
[0x000000] = { index = colors.black, blit = 'f' },
[0xFF0000] = { index = colors.red, blit = 'e' },
[0x00FF00] = { index = colors.green, blit = 'd' },
[0xFFFF00] = { index = colors.yellow, blit = '4' },
[0x0000FF] = { index = colors.blue, blit = 'b' },
[0xFF00FF] = { index = colors.magenta, blit = '2' },
[0x00FFFF] = { index = colors.cyan, blit = '9' },
[0xFFFFFF] = { index = colors.white, blit = '0' }
}
function teletextColorPalette.load (term)
for rgb, info in pairs(blitMap) do
term.setPaletteColor(info.index, rgb)
end
end
function teletextColorPalette.getBlitChar (r, g, b)
local combined = bit32.lshift(r, 16) + bit32.lshift(g, 8) + b
return blitMap[combined].blit
end
return teletextColorPalette
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment