You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

anaglyph.py 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # -*- mode: Python -*-
  4. '''
  5. anaglyph
  6. v0.1.0
  7. Attempts to create a valid 3D-glasses structure
  8. LICENCE : CC
  9. by cocoa
  10. '''
  11. from __future__ import print_function
  12. import argparse
  13. import ast
  14. import math
  15. import os
  16. import random
  17. import sys
  18. import time
  19. name = "filters::cycle"
  20. maxDist = 300
  21. argsparser = argparse.ArgumentParser(description="Redis exporter LJ")
  22. argsparser.add_argument("-x","--centerX",help="geometrical center X position",default=400,type=int)
  23. argsparser.add_argument("-y","--centerY",help="geometrical center Y position",default=400,type=int)
  24. argsparser.add_argument("-m","--min",help="Minimal displacement (default:2) ",default=1,type=int)
  25. argsparser.add_argument("-M","--max",help="Maximal displacement (default:20) ",default=5,type=int)
  26. argsparser.add_argument("-f","--fps",help="Frame Per Second",default=30,type=int)
  27. argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose")
  28. args = argsparser.parse_args()
  29. fps = args.fps
  30. minVal = args.min
  31. maxVal = args.max
  32. centerX = args.centerX
  33. centerY = args.centerY
  34. verbose = args.verbose
  35. optimal_looptime = 1 / fps
  36. name = "filters::anaglyph"
  37. def debug(*args, **kwargs):
  38. if( verbose == False ):
  39. return
  40. print(*args, file=sys.stderr, **kwargs)
  41. def rgb2int(rgb):
  42. #debug(name,"::rgb2int rbg:{}".format(rgb))
  43. return int('0x%02x%02x%02x' % tuple(rgb),0)
  44. def isValidColor( color, intensityColThreshold ):
  45. if color[0] + color[1] + color[2] > intensityColThreshold:
  46. return True
  47. return False
  48. # These are paper colors
  49. red = (41,24,24)
  50. white = (95,95,95)
  51. blue = (0,41,64)
  52. red = (127,0,0)
  53. blue = (0,128,128)
  54. white = (128,128,128)
  55. def anaglyph( pl ):
  56. debug(name,'--------------- new loop ------------------')
  57. # We will send one list after the other to optimize color change
  58. blueList = list()
  59. redList = list()
  60. whiteList = list()
  61. out = []
  62. out1 = []
  63. out2 = []
  64. out3 = []
  65. # The anaglyphic effect will be optained by :
  66. # * having close objects appear as white
  67. # * having distant objects appear as blue + red
  68. # * having in between objects appear as distanceDecreased(white) + blue + red
  69. for i, point in enumerate(pl):
  70. ref_x = point[0]-centerX
  71. ref_y = point[1]-centerY
  72. ref_color = point[2]
  73. angle = math.atan2( ref_x , ref_y )
  74. dist = ref_y / math.cos(angle)
  75. white_rvb = (0,0,0)
  76. blue_rvb = (0,0,0)
  77. red_rvb = (0,0,0)
  78. # Calculate the point's spread factor (0.0 to 1.0)
  79. # The spread is high if the point is close to center
  80. """
  81. dist = 0 : spread = 1.0
  82. dist = maxDist spread = 0.0
  83. """
  84. if dist == 0:
  85. spread = 1.0
  86. else :
  87. spread =( maxDist - dist ) / maxDist
  88. if spread < 0.0:
  89. spread = 0.0
  90. #debug(name,"dist:{} spread:{}".format(dist,spread))
  91. # White color is high if spread is low, i.e. point away from center
  92. """
  93. spread = 1.0 : white_c = 0.0
  94. spread = 0.0 : whice_c = 1.0
  95. """
  96. if point[2] == 0:
  97. white_color = 0
  98. else:
  99. white_factor = 1.0 - math.pow(spread,0.5)
  100. white_rvb = tuple(map( lambda a: int(white_factor* a), white))
  101. white_color = rgb2int( white_rvb)
  102. #debug(name,"spread:{}\t white_rvb:{}\t white_color:{}".format(spread, white_rvb, white_color))
  103. # Blue and Red colors are high if spread is high, i.e. close to center
  104. """
  105. spread = 1.0 : red_c = 1.0
  106. spread = 0.0 : red_c = 0.0
  107. """
  108. color_factor = math.pow(spread,1)
  109. if point[2] == 0:
  110. blue_color = 0
  111. red_color = 0
  112. else:
  113. blue_rvb = tuple(map( lambda a: int(color_factor * a), blue))
  114. blue_color = rgb2int( blue_rvb)
  115. red_rvb = tuple(map( lambda a: int(color_factor * a), red))
  116. red_color = rgb2int( red_rvb)
  117. #debug(name,"color_factor:{}\t\t blue_color:{}\t\t red_color:{}".format(color_factor,blue_color,red_color))
  118. # Blue-to-Red spatial spread is high when spread is high, i.e. point close to center
  119. """
  120. spread = 1.0 : spatial_spread = maxVal
  121. spread = 0.0 : spatial_spread = minVal
  122. """
  123. spatial_spread = minVal + spread * (maxVal - minVal)
  124. #debug(name,"spatial_spread:{}".format(spatial_spread))
  125. red_x = int(point[0] + spatial_spread)
  126. blue_x = int(point[0] - spatial_spread )
  127. red_y = int(point[1] )
  128. blue_y = int(point[1])
  129. white_point = [point[0], point[1], white_color]
  130. blue_point = [blue_x,blue_y,blue_color]
  131. red_point = [red_x,red_y,red_color]
  132. #debug(name,"white[x,y,c]:{}".format(white_point))
  133. #debug(name,"blue[x,y,c]:{}".format(blue_point))
  134. #debug(name,"red[x,y,c]:{}".format(red_point))
  135. # Do not append "black lines" i.e. a color where each composent is below X
  136. # if isValidColor(white_rvb, 150):
  137. # out1.append(white_point)
  138. # if isValidColor(blue_rvb, 50):
  139. # out2.append(blue_point)
  140. # if isValidColor(red_rvb, 30):
  141. # out3.append(red_point)
  142. out1.append(white_point)
  143. out2.append(blue_point)
  144. out3.append(red_point)
  145. #debug(name,"source pl:{}".format(pl))
  146. debug(name,"whiteList:{}".format(out1))
  147. debug(name,"blueList:{}".format(out2))
  148. debug(name,"redList:{}".format(out3))
  149. return out1 + out3 + out2
  150. #return out1 + out2 + out3
  151. try:
  152. while True:
  153. start = time.time()
  154. line = sys.stdin.readline()
  155. if line == "":
  156. time.sleep(0.01)
  157. line = line.rstrip('\n')
  158. pointsList = ast.literal_eval(line)
  159. # Do the filter
  160. result = anaglyph( pointsList )
  161. print( result, flush=True )
  162. looptime = time.time() - start
  163. # debug(name+" looptime:"+str(looptime))
  164. if( looptime < optimal_looptime ):
  165. time.sleep( optimal_looptime - looptime)
  166. # debug(name+" micro sleep:"+str( optimal_looptime - looptime))
  167. except EOFError:
  168. debug(name+" break")# no more information