forked from M-Labs/artiq-zynq
149 lines
8.4 KiB
Python
149 lines
8.4 KiB
Python
from migen import *
|
|
from misoc.interconnect import stream
|
|
|
|
from src.gateware.cxp_pipeline import *
|
|
from src.gateware.cxp_frame_pipeline import *
|
|
|
|
from PIL import Image
|
|
import numpy as np
|
|
|
|
|
|
def get_image_header(
|
|
stream_id, source_tag, xsize, xoffset, ysize, yoffset, dsize, pixelF, tag_geo, flag
|
|
):
|
|
stream_id = C(stream_id, char_width)
|
|
source_tag = C(source_tag, 2 * char_width)
|
|
xsize = C(xsize, 3 * char_width)
|
|
xoffset = C(xoffset, 3 * char_width)
|
|
ysize = C(ysize, 3 * char_width)
|
|
yoffset = C(yoffset, 3 * char_width)
|
|
dsize = C(dsize, 3 * char_width)
|
|
pixelF = C(pixelF, 2 * char_width)
|
|
tag_geo = C(tag_geo, 2 * char_width)
|
|
flag = C(flag, char_width)
|
|
|
|
assert len(stream_id) == len(flag) == char_width
|
|
assert len(source_tag) == len(pixelF) == len(tag_geo) == 2 * char_width
|
|
assert len(xsize) == len(xoffset) == len(ysize) == len(yoffset) == 3 * char_width
|
|
return [
|
|
{"data": Replicate(KCode["stream_marker"], 4), "k": Replicate(1, 4)},
|
|
{"data": Replicate(C(0x01, char_width), 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(stream_id, 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(source_tag[8:], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(source_tag[:8], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(xsize[16:], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(xsize[8:16], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(xsize[:8], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(xoffset[16:], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(xoffset[8:16], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(xoffset[:8], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(ysize[16:], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(ysize[8:16], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(ysize[:8], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(yoffset[16:], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(yoffset[8:16], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(yoffset[:8], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(dsize[16:], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(dsize[8:16], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(dsize[:8], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(pixelF[8:], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(pixelF[:8], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(tag_geo[8:], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(tag_geo[:8], 4), "k": Replicate(0, 4)},
|
|
{"data": Replicate(flag, 4), "k": Replicate(0, 4)},
|
|
]
|
|
|
|
|
|
def get_line_marker():
|
|
return [
|
|
{"data": Replicate(KCode["stream_marker"], 4), "k": Replicate(1, 4)},
|
|
{"data": Replicate(C(0x02, char_width), 4), "k": Replicate(0, 4)},
|
|
]
|
|
|
|
|
|
def get_frame_packet(stream_id, pixel_format="mono16"):
|
|
assert pixel_format in ["mono16"]
|
|
|
|
arr = [
|
|
[204, 200, 203, 205, 190, 187, 189, 205, 214, 197, 188, 185, 181, 178, 193, 209, 211, 207, 211, 192, 168, 168, 171, 199, 210, 212, 203, 196],
|
|
[218, 205, 199, 190, 192, 197, 196, 195, 184, 178, 182, 173, 166, 132, 122, 114, 154, 184, 187, 188, 171, 168, 170, 180, 192, 196, 202, 198],
|
|
[223, 222, 222, 224, 216, 199, 199, 207, 205, 189, 183, 182, 144, 66, 61, 66, 80, 148, 181, 175, 169, 170, 174, 177, 196, 206, 223, 218],
|
|
[221, 226, 225, 222, 211, 200, 202, 208, 215, 201, 187, 180, 133, 116, 113, 118, 96, 111, 206, 193, 170, 169, 186, 211, 218, 224, 231, 223],
|
|
[219, 216, 206, 197, 210, 201, 206, 203, 191, 190, 185, 145, 134, 140, 159, 170, 150, 116, 180, 173, 179, 170, 172, 185, 201, 218, 227, 227],
|
|
[203, 198, 194, 208, 227, 201, 201, 201, 215, 221, 209, 170, 136, 113, 141, 139, 141, 145, 188, 170, 180, 169, 184, 173, 174, 192, 215, 230],
|
|
[206, 224, 213, 213, 233, 207, 204, 226, 233, 227, 214, 166, 145, 123, 145, 155, 147, 186, 213, 187, 171, 169, 193, 193, 171, 178, 186, 207],
|
|
[212, 228, 216, 205, 214, 205, 204, 230, 235, 225, 219, 187, 143, 122, 146, 163, 158, 195, 209, 203, 174, 168, 190, 185, 187, 202, 180, 174],
|
|
[197, 206, 201, 223, 213, 201, 203, 231, 234, 225, 218, 206, 147, 125, 149, 155, 190, 208, 206, 203, 175, 168, 171, 179, 184, 206, 189, 176],
|
|
[213, 202, 209, 235, 223, 200, 202, 202, 227, 227, 202, 176, 138, 122, 144, 153, 190, 209, 207, 191, 172, 167, 179, 204, 190, 191, 180, 193],
|
|
[225, 225, 207, 231, 219, 197, 215, 200, 194, 199, 181, 172, 131, 129, 147, 159, 113, 175, 196, 179, 184, 169, 181, 210, 202, 204, 200, 177],
|
|
[208, 222, 204, 223, 210, 191, 195, 198, 203, 167, 171, 168, 135, 129, 149, 175, 66, 57, 90, 121, 147, 165, 181, 205, 195, 217, 209, 173],
|
|
[188, 216, 201, 206, 199, 180, 185, 180, 129, 75, 139, 166, 124, 146, 189, 135, 51, 41, 38, 40, 45, 63, 131, 201, 189, 215, 193, 170],
|
|
[188, 194, 195, 192, 182, 180, 134, 68, 45, 41, 96, 130, 116, 156, 163, 64, 46, 41, 43, 41, 42, 42, 74, 181, 177, 198, 175, 193],
|
|
[179, 179, 209, 224, 198, 182, 99, 42, 44, 41, 44, 100, 116, 125, 100, 46, 45, 42, 42, 37, 44, 43, 49, 150, 183, 170, 172, 198],
|
|
[175, 177, 208, 223, 197, 180, 94, 40, 42, 40, 41, 99, 134, 117, 80, 43, 46, 43, 37, 37, 44, 42, 35, 129, 195, 170, 170, 180],
|
|
[179, 181, 187, 217, 193, 175, 91, 38, 41, 41, 42, 106, 151, 107, 62, 43, 45, 41, 33, 38, 42, 34, 33, 77, 188, 175, 173, 208],
|
|
[190, 191, 180, 213, 194, 175, 78, 38, 40, 40, 40, 98, 134, 97, 51, 44, 59, 50, 37, 40, 36, 26, 36, 44, 100, 178, 192, 206],
|
|
[199, 191, 184, 204, 196, 176, 78, 33, 38, 38, 39, 80, 102, 83, 43, 44, 112, 130, 122, 63, 33, 24, 29, 34, 33, 74, 162, 195],
|
|
[191, 170, 196, 193, 186, 177, 88, 27, 34, 37, 36, 74, 101, 70, 36, 37, 81, 127, 137, 113, 40, 28, 30, 32, 36, 29, 69, 173],
|
|
[164, 189, 190, 180, 176, 172, 83, 26, 28, 33, 32, 68, 97, 62, 32, 30, 44, 97, 123, 136, 58, 42, 44, 43, 43, 40, 58, 162],
|
|
[177, 202, 205, 181, 174, 163, 78, 38, 35, 47, 54, 67, 92, 51, 28, 29, 26, 21, 39, 85, 47, 46, 52, 47, 46, 45, 48, 141],
|
|
[181, 193, 199, 192, 171, 163, 91, 67, 121, 123, 91, 63, 89, 45, 25, 25, 23, 20, 15, 13, 20, 48, 54, 35, 34, 34, 68, 146],
|
|
[175, 192, 195, 179, 165, 163, 100, 64, 99, 94, 82, 58, 83, 37, 23, 22, 22, 27, 21, 15, 14, 44, 98, 83, 94, 118, 164, 157],
|
|
[153, 184, 171, 163, 161, 157, 140, 70, 58, 89, 61, 53, 76, 30, 20, 20, 20, 31, 24, 19, 16, 47, 159, 163, 160, 171, 160, 142],
|
|
[142, 150, 161, 168, 154, 154, 164, 138, 76, 55, 26, 37, 62, 24, 19, 19, 20, 21, 23, 27, 31, 46, 142, 156, 151, 153, 147, 145],
|
|
[153, 147, 174, 171, 151, 150, 169, 158, 142, 92, 28, 60, 59, 20, 20, 18, 20, 26, 27, 29, 33, 38, 125, 153, 150, 147, 147, 148],
|
|
[138, 141, 166, 164, 146, 144, 164, 149, 132, 72, 34, 88, 72, 24, 19, 18, 18, 23, 25, 28, 31, 30, 98, 150, 146, 144, 146, 144]
|
|
]
|
|
source_tag = 0
|
|
xsize, ysize = len(arr[0]), len(arr)
|
|
|
|
xoffset, yoffset = 0, 0
|
|
if pixel_format == "mono16":
|
|
dsize = xsize // 2
|
|
pixelF = 0x0105
|
|
tag_geo = 0
|
|
flag = 0
|
|
|
|
packet = []
|
|
# Image header
|
|
packet += get_image_header(
|
|
stream_id,
|
|
source_tag,
|
|
xsize,
|
|
xoffset,
|
|
ysize,
|
|
yoffset,
|
|
dsize,
|
|
pixelF,
|
|
tag_geo,
|
|
flag,
|
|
)
|
|
|
|
for line in arr:
|
|
packet += get_line_marker()
|
|
if pixel_format == "mono16":
|
|
for i in range(len(line)):
|
|
if (i % 2) == 0:
|
|
if i == len(line) - 1:
|
|
# print(C(line[i]))
|
|
packet += [
|
|
{
|
|
"data": C(line[i], 4 * char_width),
|
|
"k": Replicate(0, 4),
|
|
},
|
|
]
|
|
else:
|
|
# print(C(line[i], 2 * char_width), C(line[i + 1]))
|
|
# CXP use MSB
|
|
packet += [
|
|
{
|
|
"data": Cat(
|
|
C(line[i], 2 * char_width),
|
|
C(line[i + 1], 2 * char_width),
|
|
),
|
|
"k": Replicate(0, 4),
|
|
},
|
|
]
|
|
|
|
return packet
|