Advertisement
Bualrond

Untitled

Dec 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.65 KB | None | 0 0
  1. def dc():
  2.     global w, h
  3.     def decode(inp):
  4.         if(ord("A") <= ord(inp) <= ord("Z")):
  5.             return(ord(inp) - ord("A"))
  6.         if(ord("a") <= ord(inp) <= ord("z")):
  7.             return(ord(inp) - ord("a") + 26)
  8.         if(ord("0") <= ord(inp) <= ord("9")):
  9.             return(ord(inp) - ord("0") + 52)
  10.         if(inp == "+"):
  11.             return(62)
  12.         if(inp == "/"):
  13.             return(63)
  14.     st = ''
  15.     while(len(st) < w * h * 4 // 3):
  16.         st += input()
  17.     st = list(map(decode, st))
  18.     ans = []
  19.     for i in range(0, len(st), 4):
  20.         cnt = 0
  21.         for j in st[i:i + 4]:
  22.             cnt = cnt << 6
  23.             cnt += j
  24.        
  25.         arr = []
  26.         for j in range(3):
  27.             a = cnt & (255 << (8 * j))
  28.             a = a >> (8 * j)
  29.             arr.insert(0, a)
  30.        
  31.         ans += arr
  32.    
  33.     out = []
  34.     while(ans):
  35.         out.append(ans[:w])
  36.         ans = ans[w:]
  37.        
  38.     return out
  39.  
  40. def bicubic(inp):
  41.     def wrk(p, y, k):
  42.         def sr(p, st, ln):
  43.             out = []
  44.             for i in p:
  45.                 out.append(i[st:st + ln])
  46.             return out
  47.        
  48.         def cubicInterpolate(p, x):
  49.             x *= 0.25
  50.             a = p[1] + 0.5 * x*(p[2] - p[0] + x*(2.0*p[0] - 5.0*p[1] + 4.0*p[2] - p[3] + x*(3.0*(p[1] - p[2]) + p[3] - p[0])))
  51.             return min(255, max(0, int(a)))
  52.        
  53.         def bicubicInterpolate(p, x, y):
  54.             arr = [0] * 4
  55.             arr[0] = cubicInterpolate(p[0], y)
  56.             arr[1] = cubicInterpolate(p[1], y)
  57.             arr[2] = cubicInterpolate(p[2], y)
  58.             arr[3] = cubicInterpolate(p[3], y)
  59.             return cubicInterpolate(arr, x)
  60.        
  61.         global w, h
  62.         out = [0] * w * 2
  63.         if(k):
  64.             out[0] = p[y][0]
  65.             out[1] = bicubicInterpolate(sr(p, 0, 4), 0.5, y)
  66.             out[2] = p[y][1]
  67.             for i in range(w - 3):
  68.                 out[i * 2 + 3] = bicubicInterpolate(sr(p, i, 4), 1.5, y)
  69.                 out[i * 2 + 4] = p[y][i + 2]
  70.             out[-3] = bicubicInterpolate(sr(p, w - 4, 4), 2.5, y)
  71.             out[-2] = out[-1] = p[y][-1]
  72.         else:
  73.             out[0] = bicubicInterpolate(sr(p, 0, 4), 0, y)
  74.             out[1] = bicubicInterpolate(sr(p, 0, 4), 0.5, y)
  75.             out[2] = bicubicInterpolate(sr(p, 0, 4), 1, y)
  76.             for i in range(w - 3):
  77.                 out[i * 2 + 3] = bicubicInterpolate(sr(p, i, 4), 1.5, y)
  78.                 out[i * 2 + 4] = bicubicInterpolate(sr(p, i, 4), 2, y)
  79.             out[-3] = bicubicInterpolate(sr(p, w - 4, 4), 2.5, y)
  80.             out[-2] = out[-1] = bicubicInterpolate(sr(p, w - 4, 4), 3, y)
  81.         return out
  82.    
  83.     arr = [0] * h * 2
  84.    
  85.     arr[0] = wrk(inp[:4], 0, 1)
  86.     arr[1] = wrk(inp[:4], 0.5, 0)
  87.     arr[2] = wrk(inp[:4], 1, 1)
  88.     for i in range(h - 3):
  89.         arr[i * 2 + 3] = wrk(inp[i:i + 4], 1.5, 0)
  90.         arr[i * 2 + 4] = wrk(inp[i:i + 4], 2, 1)
  91.     arr[-3] = wrk(inp[h - 4:h], 2.5, 0)
  92.     arr[-2] = arr[-1] = wrk(inp[h - 4:h], 3, 1)
  93.        
  94.     return arr
  95.  
  96. def middling(inp):
  97.     global w, h
  98.     out = [0] * h * 2
  99.    
  100.     for i in range(h):
  101.         arr = [0] * w * 2
  102.         for j in range(w - 1):
  103.             arr[j * 2] = inp[i][j]
  104.             arr[j * 2 + 1] = (inp[i][j] + inp[i][j + 1]) // 2
  105.         arr[-2] = arr[-1] = inp[i][-1]
  106.         out[i * 2] = arr
  107.    
  108.     for i in range(h - 1):
  109.         arr = [0] * w * 2
  110.         for j in range(w * 2):
  111.             arr[j] = (out[i * 2][j] + out[i * 2 + 2][j]) // 2
  112.         out[i * 2 + 1] = arr
  113.     out[-1] = out[-2]
  114.    
  115.     return out
  116.  
  117. def ic(inp):
  118.     def toChr(i):
  119.         dc = {
  120.              0:'A',
  121.              1:'B',
  122.              2:'C',
  123.              3:'D',
  124.              4:'E',
  125.              5:'F',
  126.              6:'G',
  127.              7:'H',
  128.              8:'I',
  129.              9:'J',
  130.             10:'K',
  131.             11:'L',
  132.             12:'M',
  133.             13:'N',
  134.             14:'O',
  135.             15:'P',
  136.             16:'Q',
  137.             17:'R',
  138.             18:'S',
  139.             19:'T',
  140.             20:'U',
  141.             21:'V',
  142.             22:'W',
  143.             23:'X',
  144.             24:'Y',
  145.             25:'Z',
  146.             26:'a',
  147.             27:'b',
  148.             28:'c',
  149.             29:'d',
  150.             30:'e',
  151.             31:'f',
  152.             32:'g',
  153.             33:'h',
  154.             34:'i',
  155.             35:'j',
  156.             36:'k',
  157.             37:'l',
  158.             38:'m',
  159.             39:'n',
  160.             40:'o',
  161.             41:'p',
  162.             42:'q',
  163.             43:'r',
  164.             44:'s',
  165.             45:'t',
  166.             46:'u',
  167.             47:'v',
  168.             48:'w',
  169.             49:'x',
  170.             50:'y',
  171.             51:'z',
  172.             52:'0',
  173.             53:'1',
  174.             54:'2',
  175.             55:'3',
  176.             56:'4',
  177.             57:'5',
  178.             58:'6',
  179.             59:'7',
  180.             60:'8',
  181.             61:'9',
  182.             62:'+',
  183.             63:'/'
  184.             }
  185.         return dc[i]
  186.    
  187.     for i in inp:
  188.         for j in range(0, len(i), 3):
  189.             cnt = 0
  190.             for k in i[j:j + 3]:
  191.                 cnt = cnt << 8
  192.                 cnt += k
  193.             p = ''
  194.             for k in range(4):
  195.                 a = cnt & 63
  196.                 cnt -= a
  197.                 cnt = cnt >> 6
  198.                 p = toChr(a) + p
  199.             print(p, end = '')
  200.    
  201. w, h = map(int, input().split())
  202. print(w * 2, h * 2)
  203. arr = dc()
  204. arr = bicubic(arr)
  205. ic(arr)
  206. #f = open("output.txt", "w")
  207. #f.write(arr)
  208. '''
  209. for i in arr:
  210.    for j in i:
  211.        f.write(str(j) + ' ' * (4 - len(str(j))))
  212.    f.write("\n")
  213. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement