Skip to content

Instantly share code, notes, and snippets.

@zguangyu
Last active August 5, 2019 07:01
Show Gist options
  • Save zguangyu/2ebe7fe9961367f3a6721f967c0c6ec8 to your computer and use it in GitHub Desktop.
Save zguangyu/2ebe7fe9961367f3a6721f967c0c6ec8 to your computer and use it in GitHub Desktop.
拼图
#include <fstream>
#include <iostream>
#include <sys/time.h>
//#include <tbb/tbb.h>
using namespace std;
//using namespace tbb;
#define ROWS 4096
#define COLS 4096
#define CHANROWS 2048
#define CHANCOLS 1024
#define CHANS 8
void makeOneLineLDR(uint8_t line[], uint8_t image_buffer[])
{
const int channel_pixel_num = COLS / CHANS;
for (int i = 0; i < CHANS * 3 / 2; i += 4)
{
swap(line[i], line[i + 3]);
swap(line[i + 1], line[i + 2]);
}
for (int i = 0; i < channel_pixel_num; i++)
{
uint64_t low = 0, high = 0;
for (int i = 0; i < 6; i++)
{
low |= (uint64_t)(line[i]) << (5 - i) * 8;
high |= (uint64_t)(line[i + 6]) << (5 - i) * 8;
}
for (int j = CHANS - 1; j >= 0; j--)
{
unsigned int low6 = 0, high6 = 0;
low6 = (low >> (j * 6)) & 0x3f;
high6 = (high >> (j * 6)) & 0x3f;
int pixel = ((int)(high6) << 6) | low6;
int pos = channel_pixel_num * j + i;
// printf("%d 0x%02X 0x%02X %d\n", pos, low6, high6, pixel);
image_buffer[pos << 1] = 0xff & pixel;
image_buffer[(pos << 1) + 1] =
0xff & (pixel >> 8); // Caution: order should obey cfitsio.
}
line += CHANS * 3 / 2; // += 12
// printf("\n");
}
}
void makeOneLineLDR_new(uint8_t line[], uint16_t image_buffer[])
{
const int channel_pixel_num = COLS / CHANS;
for (int i = 0; i < CHANS * 3 / 2; i += 4)
{
swap(line[i], line[i + 3]);
swap(line[i + 1], line[i + 2]);
}
for (int i = 0; i < channel_pixel_num; i++)
{
uint16_t pix[8];
// 0-3 1-2 4-7 5-6 8-11 9-10
pix[0] = (line[5] & 0x3f) << 6 | (line[3] & 0x3f);
pix[1] = (line[4] & 0x0f) << 8 | (line[5] & 0xc0) << 2 | (line[2] & 0x0f) << 2 | (line[3] & 0xc0) >> 4;
pix[2] = (line[11]& 0x03) << 10| (line[4] & 0xf0) << 2 | (line[1] & 0x03) << 4 | (line[2] & 0xf0) >> 4;
pix[3] = (line[11]& 0xfc) << 4 | (line[1] & 0xfc) >> 2;
pix[4] = (line[10]& 0x3f) << 6 | (line[0] & 0x3f);
pix[5] = (line[9] & 0x0f) << 8 | (line[10]& 0xc0) << 2 | (line[7] & 0x0f) << 2 | (line[0] & 0xc0) >> 4;
pix[6] = (line[8] & 0x03) << 10| (line[9] & 0xf0) << 2 | (line[6] & 0x03) << 4 | (line[7] & 0xf0) >> 4;
pix[7] = (line[8] & 0xfc) << 4 | (line[6] & 0xfc) >> 2;
for (int j = 0; j < CHANS; j++)
{
int pos = channel_pixel_num * j + i;
image_buffer[pos] = pix[j];
}
line += CHANS * 3 / 2; // += 12
// printf("\n");
}
}
int main()
{
uint8_t *raw = new uint8_t[ROWS*COLS*3/2];
uint8_t *image = new uint8_t[ROWS*COLS*2];
ifstream myfile("12bit_pack.bin", ios::in | ios::binary);
myfile.read((char*)raw, ROWS*COLS*3/2);
myfile.close();
struct timeval tv;
gettimeofday(&tv, NULL);
cout << tv.tv_sec << ' ' << tv.tv_usec << endl;
for (int i = 0; i < ROWS; i++) {
//makeOneLineLDR(raw + i * COLS * 3 / 2, image + i * COLS * 2);
makeOneLineLDR_new(raw + i * COLS * 3 / 2, (uint16_t*)image + i * COLS);
}
/* parallel_for(blocked_range<size_t>(0, ROWS),
[=] (const blocked_range<size_t> r) {
for (size_t i = r.begin(); i != r.end(); ++i) {
makeOneLineLDR(raw + i * COLS * 3 / 2, image + i * COLS * 2);
//makeOneLineLDR_new(raw + i * COLS * 3 / 2, (uint16_t*)image + i * COLS);
}
}); */
gettimeofday(&tv, NULL);
cout << tv.tv_sec << ' ' << tv.tv_usec << endl;
ofstream myfile2("16bit_output1.bin", ios::out | ios::binary);
myfile2.write((char*)image, ROWS*COLS*2);
myfile2.close();
return 0;
}
import bitstring
import numpy as np
from tqdm import trange
ROWS = 4096
COLS = 512
FILENAME = "12bit_pack.bin"
chan_data = np.tile(np.arange(0, COLS, dtype=np.int16), (ROWS, 1))
chan_data_seq = np.reshape(chan_data, -1)
f = open(FILENAME, 'wb')
print(chan_data_seq.shape[0])
for i in trange(chan_data_seq.shape[0]):
low6 = chan_data_seq[i] & 0x3f
high6 = (chan_data_seq[i] >> 6) & 0x3f
data = bitstring.pack('uint:6', low6) * 8
data += bitstring.pack('uint:6', high6) * 8
data.byteswap(4)
data.tofile(f)
# coding: utf-8
import numpy as np
import matplotlib.pyplot as plt
data = np.fromfile("16bit_output.bin", dtype=np.uint16)
data = data.reshape(4096, 4096)
plt.imshow(data, cmap="gray")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment