-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathostro_to8.lua
More file actions
executable file
·86 lines (74 loc) · 2.26 KB
/
ostro_to8.lua
File metadata and controls
executable file
·86 lines (74 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
-- ostro_to8.lua : convert a color image to a BM16
-- (160x200x16) thomson image using the Ostromoukhov's
-- error diffusion algorithm.
--
-- Version: 02-jan-2017
--
-- Copyright 2016-2017 by Samuel Devulder
--
-- This program is free software; you can redistribute
-- it and/or modify it under the terms of the GNU
-- General Public License as published by the Free
-- Software Foundation; version 2 of the License.
-- See <http://www.gnu.org/licenses/>
pcall(function() require('lib/cmdline') end)
run('lib/thomson.lua')
run('lib/ostromoukhov.lua')
run('lib/color_reduction.lua')
-- run('lib/zzz.lua')
-- get screen size
local screen_w, screen_h = getpicturesize()
-- Converts thomson coordinates (0-159,0-199) into screen coordinates
local CENTERED = true
local function thom2screen(x,y)
local i,j;
if screen_w/screen_h < 1.6 then
local o = CENTERED and (screen_w-screen_h*1.6)/4 or 0
i = x*screen_h/200+o
j = y*screen_h/200
else
local o = CENTERED and (screen_h-screen_w/1.6)/2 or 0
i = x*screen_w/320
j = y*screen_w/320+o
end
return math.floor(i*2), math.floor(j)
end
-- return the Color @(x,y) in normalized linear space (0-1)
-- corresonding to the thomson screen (x in 0-319, y in 0-199)
local function getLinearPixel(x,y)
local x1,y1 = thom2screen(x,y)
local x2,y2 = thom2screen(x+1,y+1)
if x2==x1 then x2=x1+1 end
if y2==y1 then y2=y1+1 end
local p = Color:new(0,0,0);
for j=y1,y2-1 do
for i=x1,x2-1 do
p:add(getLinearPictureColor(i,j))
end
end
p:div((y2-y1)*(x2-x1)) --:floor()
return p
end
local red = ColorReducer:new():analyzeWithDither(160,200,
getLinearPixel,
function(y)
thomson.info("Collecting stats...",math.floor(y*100),"%")
end)
-- BM16 mode
thomson.setBM16()
-- define palette
local palette = red:boostBorderColors():buildPalette(16)
thomson.palette(0, palette)
-- convert picture
OstroDither:new(palette)
:dither(thomson.h,thomson.w,
function(y,x) return getLinearPixel(x,y) end,
function(y,x,c) thomson.pset(x,y,c) end,
true,
function(x) thomson.info("Converting...",math.floor(x*100),"%") end)
-- refresh screen
setpicturesize(320,200)
thomson.updatescreen()
finalizepicture()
-- save picture
thomson.savep()