-
Notifications
You must be signed in to change notification settings - Fork 0
/
frames2native.py
executable file
·143 lines (116 loc) · 4.66 KB
/
frames2native.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
#
# Take a set of separate view images (i.e. the tiles that normally
# make up a quilt) and convert them to a "native" image that can be
# displayed directly on a specific LG.
#
# The formulas derive from the concept of using a linear quilt,
# where all view images are placed side-by-side, i.e. with only 1
# tile vertically. This simplifies the formulas quite a bit and is
# conceptually slightly easier as a 1D tile index becomes the same
# as the view index.
#
# Partly based on https://github.com/lonetech/LookingGlass/blob/master/quiltshader.glsl
# (for which no license seems to be specified)
#
# XXX This currently doesn't output exactly matching native images
# compared to linquilt2native.py and quilt2native.py (seems to be an
# offset in views used). Haven't figured out why exactly yet, may be
# some subtly different rounding as the formulas are not exactly the same
# (although they're derived from linquilt2native.py)
#
#
# Copyright (c) 2019, SURFsara BV
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of SURFsara BV nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL SURFSARA BV BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys, json
from math import floor
from PIL import Image
from calibration import Calibration
def usage():
print('usage: %s <visual.json> <frame-pattern> <first> <last> <native-image>' % sys.argv[0])
print()
sys.exit(-1)
if len(sys.argv) != 6:
usage()
calibration = Calibration(sys.argv[1])
screenW = calibration.screenW
screenH = calibration.screenH
tilt = calibration.tilt
pitch = calibration.pitch
center = calibration.center
subp = calibration.subp
frame_file_pattern = sys.argv[2]
frame_file_first = int(sys.argv[3])
frame_file_last = int(sys.argv[4])
native_image_file = sys.argv[5]
NUM_FRAMES = frame_file_last - frame_file_first + 1
INV_NUM_FRAMES = 1.0 / NUM_FRAMES
# Load tiles
tile_images = [] # For holding references to image objects
tile_pixels = [] # For PixelAccess objects
FRAME_WIDTH = FRAME_HEIGHT = None
for i in range(NUM_FRAMES):
tile_file = frame_file_pattern % (frame_file_first + i)
#print(tile_file)
img = Image.open(tile_file)
tile_images.append(img)
tile_pixels.append(img.load())
if FRAME_WIDTH is None:
FRAME_WIDTH, FRAME_HEIGHT = img.size
print('Tile size %d x %d' % (FRAME_WIDTH, FRAME_HEIGHT))
def determine_view(a):
res = NUM_FRAMES - 1
a = a%1 * NUM_FRAMES
res -= floor(a)
return res
def pixel_color(u, v):
# XXX simplified to use the same i value for each subpixel. Seems to
# work, as the subpixels can still get different views, but not sure
# this is fully equivalent to what the Lenticular shader does :)
i = int(u * FRAME_WIDTH)
j = int(v * FRAME_HEIGHT)
a = (u + (1.0 - v)*tilt)*pitch - center
# Red
view = determine_view(a)
img = tile_pixels[view]
r = img[i,j][0]
# Green
view = determine_view(a+subp)
img = tile_pixels[view]
g = img[i,j][1]
# Blue
view = determine_view(a+2*subp)
img = tile_pixels[view]
b = img[i,j][2]
return (r, g, b)
outimg = Image.new('RGB', (screenW, screenH))
opx = outimg.load()
for j in range(screenH):
v = (j+0.5) / screenH
for i in range(screenW):
u = (i+0.5) / screenW
opx[i,j] = pixel_color(u, v)
del opx
outimg.save(native_image_file)