-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCSI.py
More file actions
159 lines (141 loc) · 6.24 KB
/
Copy pathPCSI.py
File metadata and controls
159 lines (141 loc) · 6.24 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#import re
import os
from ctypes.wintypes import PINT
import numpy as np
import imageio
from pcsi.pcsiolw import PCSIolw
from PIL import ImageTk, Image
import cv2
#import serial
#from email.mime import base
import binascii
import bitstring
import argparse
from pcsi.pcsitximage_mmt import PCSItxImage
from pcsi.pcsidecoder_mmt import PCSIDecoder
def encode_callsign(callsign):
base40_code = 0
for i in range(len(callsign)-1,-1,-1):
base40_code *= 40
if(ord(callsign[i]) >= ord('A') and ord(callsign[i]) <= ord('Z')):
base40_code += ord(callsign[i]) - ord('A') + 14
elif(ord(callsign[i]) >= ord('a') and ord(callsign[i]) <= ord('z')):
base40_code += ord(callsign[i]) - ord('a') + 14
elif(ord(callsign[i]) >= ord('0') and ord(callsign[i]) <= ord('9')):
base40_code += ord(callsign[i]) - ord('0') + 1
return base40_code
def decode_callsign(base40_code):
callsign = ""
i = 0
while(base40_code>0):
i -= 1
s = base40_code % 40
if(s==0):
callsign += '-'
elif(s<11):
callsign += chr(ord('0') + s - 1)
elif(s<14):
callsign += '-'
else: callsign += chr(ord('A') + s - 14)
base40_code = base40_code // 40
return callsign
def encode_PCSI(inputfile,outputfile,imageID,callsign,bitDepth,chromaCompression):
header = bitstring.pack(
'uint:8, uint:8, uint:32',
85,
118,
encode_callsign(callsign)).tobytes()
txImage = PCSItxImage(inputfile,
imageID,
bitDepth,
chromaCompression,
infoBytes=218,
APRSprefixBytes=False, # if we change this, we have to change the decode too
base91=False)
for i in range(0,txImage.largestFullPacketNum+1):
outputfile.write(header+txImage.genPayload(i))
print("Wrote",txImage.largestFullPacketNum+1,"packets")
def decode_PCSI(imageSelected, X, nynx, pixelsY, pixelsCbCr):
Z = np.zeros(X.shape, dtype='uint8')
ny = nynx[0]
nx = nynx[1]
XY = X[:,:,0]
XCb = X[:,:,1]
XCr = X[:,:,2]
riY = pixelsY
riCbCr = pixelsCbCr
bY = XY.T.flat[riY].astype(float)
bCb = XCb.T.flat[riCbCr].astype(float)
bCr = XCr.T.flat[riCbCr].astype(float)
# print([bY.shape, len(riY)])
if not os.path.exists(imageSelected+"_Y.npy"):
np.save(imageSelected+"_Y.npy",np.zeros(nx*ny))
Xat2_Y = np.load(imageSelected+"_Y.npy")
pcsiSolverY = PCSIolw(nx, ny, bY, riY, Xat2_Y)
Z[:,:,0],Xat2_Y = pcsiSolverY.go()
np.save(imageSelected+"_Y.npy",Xat2_Y)
Z[:,:,0] = Z[:,:,0].astype('uint8')# choosenImage.get()
if not os.path.exists(imageSelected+"_Cb.npy"):
np.save(imageSelected+"_Cb.npy",np.zeros(nx*ny))
Xat2_Cb = np.load(imageSelected+"_Cb.npy")
pcsiSolverCb = PCSIolw(nx, ny, bCb, riCbCr, Xat2_Cb)
Z[:,:,1],Xat2_Cb = pcsiSolverCb.go()
np.save(imageSelected+"_Cb.npy",Xat2_Cb)
Z[:,:,1] = Z[:,:,1].astype('uint8')# choosenImage.get()
if not os.path.exists(imageSelected+"_Cr.npy"):
np.save(imageSelected+"_Cr.npy",np.zeros(nx*ny))
Xat2_Cr = np.load(imageSelected+"_Cr.npy")
pcsiSolverCr = PCSIolw(nx, ny, bCr, riCbCr, Xat2_Cr)
Z[:,:,2],Xat2_Cr = pcsiSolverCr.go()
np.save(imageSelected+"_Cr.npy",Xat2_Cr)
Z[:,:,2] = Z[:,:,2].astype('uint8')# choosenImage.get()
Z=cv2.cvtColor(Z, cv2.COLOR_YCrCb2BGR) # open CV switches order of channels, so this works
imageio.imwrite(imageSelected, Z)
if __name__=="__main__":
parser = argparse.ArgumentParser(description="Command line tool to PCSI",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-e", "--encode", action = 'store_true', default=False,
help = 'Encode image to PCSI packets.')
parser.add_argument("-d", "--decode", action = 'store_true', default=False,
help = 'Decode PCSI packets to image.')
parser.add_argument("inputfile", type=str,
help="Input file name")
parser.add_argument("-c", "--callsign", type=str, default='BJ1TG',
help="Set the callign. Accepts A-Z 0-9 and space, up to 6 characters.")
parser.add_argument("-i", "--imageid", type=int, default=0,
help="Set the image ID (0-255).")
parser.add_argument("-b", "--bitdepth", type=int, default=12,
help="Bit depth transmit (e.g., 24 for 24-bit color)")
parser.add_argument("-C", "--chromacomp", type=int, default=20,
help="Chroma Compression ratio")
parser.add_argument("outputfile", type=str,
help="Output file name")
args = parser.parse_args()
if(args.encode and not args.decode):
with open(str(args.outputfile), 'wb') as f:
encode_PCSI(args.inputfile,f,args.imageid,args.callsign,args.bitdepth,args.chromacomp)
f.close()
if(not args.encode and args.decode):
decoder = PCSIDecoder()
with open(str(args.inputfile), 'rb') as f:
newdata = f.read()
#print(newdata)
if newdata:
decoder.processSerial(newdata)
# for imageSelected in decoder.Z:
# imageio.imwrite("pixel_raw_"+args.outputfile, decoder.Z[imageSelected])
for imageSelected in decoder.Z:
decode_PCSI(args.outputfile,
decoder.Z[imageSelected][:],
decoder.nynx[imageSelected][:],
list(decoder.pixelsY[imageSelected]),
list(decoder.pixelsCbCr[imageSelected]))
ny = decoder.nynx[imageSelected][0]
nx = decoder.nynx[imageSelected][1]
pixelsY = len(decoder.pixelsY[imageSelected])
pixelsPerPacket = decoder.pixelsPerPacket[imageSelected]
print("Callsign:", decode_callsign(decoder.callsign_Base40))
print("Image ID:", '{:02X}'.format(decoder.imageID))
print("Resolution:",str(nx)+"x"+str(ny))
print("Total Packets", ((ny*nx)//pixelsPerPacket))
print("Received Packets", (pixelsY//pixelsPerPacket))