void Downsampled_calculation(unsigned char *src, unsigned char *dst) {
const uint32_t RawOut_Width = 3840;
const uint32_t RawOut_Height = 2160;
const uint32_t RawOut_Size = RawOut_Width * RawOut_Height * 2;
const uint32_t Downfactor = 15;
uint32_t new_height = RawOut_Height / Downfactor;
uint32_t new_width = RawOut_Width / Downfactor;
uint32_t new_Size = new_height * new_width * 2;
// unsigned char *src_temp = src;
// unsigned char *dst_temp = dst;
for (int y = 0; y < new_height; y++) {
for (int x = 0; x < new_width; x++) {
memcpy(dst,src,2);
dst += 2;
src +=2 * Downfactor;
//printf("%ld\n",*dst);
}
src += 2 * (Downfactor - 1) * RawOut_Width;
}
}
downsample need input *src and *dst which represent full resolution and resize resolution.
out this function,the code are as follow:
int width = 3840;
int height = 2160;
hi_s32 ret;
hi_char path[PATH_MAX] = { 0 };
size_t file_size;
//if (realpath("./atc/aaa_rgb.bin", path) == HI_NULL) { // rgb
if (realpath("./data/image/1.raw", path) == HI_NULL) { //yuv
sample_svp_trace_err("Invalid file!.\n");
return HI_FAILURE;
}
u_int16_t *raw_in = (u_int16_t *)malloc(3840*2160*2);
if(raw_in == NULL){
printf("内存分配失败");
return 1;
}
FILE *fp = fopen(path, "rb");
sample_svp_check_exps_return(fp == HI_NULL, HI_FAILURE, SAMPLE_SVP_ERR_LEVEL_ERROR,
"open image file failed!\n");
if (fp == NULL) {
fprintf(stderr, "无法打开文件\n");
free(raw_in);
return 1;
}
size_t element_size = fread(raw_in,2,3840*2160,fp);
if (element_size != 3840*2160) {
fprintf(stderr, "读取数据失败\n");
free(raw_in);
fclose(fp);
return 1;
}
//ret = fread(dev_buf, 144*256*2, 1, low_raw_1d);
u_int16_t *low_raw_1d = (u_int16_t*)malloc(144*256*2);
Downsampled_calculation(raw_in,low_raw_1d);
free(raw_in);
// file_size = (file_size > buf_size) ? buf_size : file_size;
// dev_buf = low_raw_1d;
memcpy(dev_buf,low_raw_1d,144*256*2);
for(int i = 0 ;i<100;i++){
printf("%d\n",*(low_raw_1d+i));
}
FILE *file_save = fopen("./raw_resize_.bin","wb");
size_t num_write = fwrite(dev_buf,2,144*256,file_save);
fclose(file_save);
after this operation , the low resolution image will be cp to dev_buf, free the low_raw_1d.
the code how to visualize the bin or raw use python are as follow:
import numpy as np
import matplotlib.pyplot as plt
filename = 'raw_resize_.bin'
width = 256
height = 144
data = np.fromfile(filename, dtype=np.uint16)
print(data.max())
data = data.reshape((height, width))
data = data**(1/2.2)
plt.imshow(data, cmap='gray')
plt.colorbar()
plt.title('16-bit RAW Image')
plt.show()