rotate
問題
rotate.zip
問題(和訳)
メモ
解法
rotate.pyは
- 2byteごとにsigned charとしてunpackして(x, y)とする。
- (第二引数)度だけ座標を回転する。
- 回転後の(x', y')をfloatとしてpackして出力する。
元のファイルがJPEGなのでFF D8で始まっているはずである。 よって最初の回転後の座標のペアになるような回転角を探索し,以降の座標を逆回転して元の値を計算する。
行番号表示/非表示切替
1 import sys
2 import math
3 import struct
4
5 p = lambda x: struct.pack('b', int(round(x)))
6 u = lambda x: struct.unpack('f', x)[0]
7 pf = lambda x: struct.pack('f', x)
8
9 if len(sys.argv) != 2:
10 sys.exit(1)
11
12 filename = sys.argv[1]
13
14 bs = open(filename, 'rb').read()
15 x,y = struct.unpack('bb', "\xff\xd8")
16 for i in range(360):
17 key = math.radians(i)
18 if bs[0:8] == pf(x * math.cos(key) - y * math.sin(key)) + pf(x * math.sin(key) + y * math.cos(key)):
19 key = -key
20 break
21
22 dec = open(filename + '.dec', 'wb')
23
24 for i in range(0, len(bs), 8):
25 x, y = u(bs[i:i+4]), u(bs[i+4:i+8])
26 # print [(x * math.cos(key) - y * math.sin(key)) , (x * math.sin(key) + y * math.cos(key))]
27 dec.write(p(x * math.cos(key) - y * math.sin(key)) + p(x * math.sin(key) + y * math.cos(key)))