'Loading 16 Bits GrayScale Image by Malloc C++ Fail

How can I load RAW 16-bit grayscale image with Fread?

I have unsigned char* buffer with raw data. I know its dimensions in pixels which are INPUT_DIM_X / INPUT_DIM_Y and Num_Proj means numbers of input image. Here is my code.

I'm trying to load it with

    camera_mem = (unsigned short*)malloc(Num_Projs * INPUT_DIM_X * INPUT_DIM_Y * sizeof(unsigned short)); 
camera_data = (unsigned short***)malloc(Num_Projs * sizeof(unsigned short**));
for(int i = 0; i < Num_Projs; i++){
    camera_data[i] = (unsigned short**)malloc(INPUT_DIM_Y * sizeof(unsigned short *));
}   
for(int i = 0; i < Num_Projs; i++){
    for(int j = 0; j < INPUT_DIM_Y; j++){
        camera_data[i][j] = &camera_mem[i * INPUT_DIM_X  * INPUT_DIM_Y + j * INPUT_DIM_X];
    }
}
for(img_num = 1; img_num <= Num_Projs; img_num++){
    strcpy(currentfile, filepath);
    sprintf(scannumbuf, "%d", img_num);
    strcat(currentfile, scannumbuf);
    strcat(currentfile,".raw");
    in_file = fopen(currentfile, "rb");
    
    if(in_file==nullptr){
        perror("No Files/File Error");
    }

    base = img_num - start;
    size_t result = fread(camera_data[base][0], sizeof(unsigned short), INPUT_DIM_X * INPUT_DIM_Y, in_file);

But I had it like enter image description here

Could someone advice me which step of malloc would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source