Esempio n. 1
0
void CaptureImageState::LoadXTCorrection()
{
    std::string h5_name = h5file + ":/XTalk_corr/";

    if (H5File::DatasetExist<std::string> (h5_name+"xt_vectors")) {
        printf("[CaptureImageState] Loading electrical XeTalk correction from %s\n",h5file.c_str());

        //vector number and length
        std::vector<int> H5num_and_len;
        H5File::ReadVector (h5_name+"num_and_length", H5num_and_len);
        int nvects = H5num_and_len[0];
        int vector_len = H5num_and_len[1];

        //column offset
        std::vector<int> H5col_offset;
        H5File::ReadVector (h5_name+"vector_indicies",H5col_offset);

        //vectors
        arma::Mat<float> H5vectors;
        H5Arma::ReadMatrix (h5_name+"xt_vectors", H5vectors);

        //Create image transformer objects
        ChannelXTCorrection *xtptr = new ChannelXTCorrection();
        float *pvects = xtptr->AllocateVectorStorage(nvects,vector_len);
        float **vect_ptrs = xtptr->AllocateVectorPointerStorage(nvects);
        xtptr->SetVectorIndicies(&H5col_offset[0],vector_len);

        for ( int vndx=0; vndx < nvects; vndx++ ) {
            vect_ptrs[vndx] = pvects+vector_len*vndx;
            for ( int vn=0; vn < vector_len; vn++ )
                vect_ptrs[vndx][vn] = H5vectors.at (vndx, vn);
        }

        ImageTransformer::custom_correction_data = xtptr;
        ImageTransformer::selected_chip_xt_vectors = ImageTransformer::custom_correction_data->GetCorrectionDescriptor();
    }
    else {
        printf("[CaptureImageState] No electrical XeTalk correction found\n");
    }
}
// generate an electrical cross-talk correction from the lsrow image
// if the file pointed to by lsimg_path does not exist, the method returns NULL
// If lsimg_path does exist, then a correction is generated and a pointer to
// a ChannelXTCorrection object with all the relevant information is returned
ChannelXTCorrection *LSRowImageProcessor::GenerateCorrection(const char *lsimg_path)
{
    uint32_t magic_value;
    uint32_t file_version;
    uint32_t rows = 0;
    uint32_t cols = 0;
    int nread;

    // the lsrowimage file contains:
    // uint32 magic value (0xFF115E3A)
    // uint32 file version number (0+)
    // uint32 rows
    // uint32 columns
    // uint16[rows*columns] high-speed and low-speed reference data, in row major order
    //  ..with the first row in the image being a high-speed collected row
    //    and the next row in the image being the low-speed reference for the previous high-speed row data

    FILE *lsrowfile;

    lsrowfile = fopen(lsimg_path,"rb");

    // if we have trouble opening the file, just return NULL
    if(lsrowfile == NULL)
        return NULL;

    nread = fread(&magic_value,sizeof(int32_t),1,lsrowfile);
    if(nread != 1)
    {
        printf("Ivalid lsrowfile detected\n");
        fclose(lsrowfile);
        return NULL;
    }
    magic_value = BYTE_SWAP_4(magic_value);

    if(magic_value != LSROWIMAGE_MAGIC_VALUE)
    {
        printf("Ivalid lsrowfile detected\n");
        fclose(lsrowfile);
        return NULL;
    }

    nread = fread(&file_version,sizeof(int32_t),1,lsrowfile);
    if(nread != 1)
    {
        printf("Ivalid lsrowfile detected\n");
        fclose(lsrowfile);
        return NULL;
    }
    file_version = BYTE_SWAP_4(file_version);

    if(file_version != 0)
    {
        printf("Unsupported lsrowimage file version\n");
        fclose(lsrowfile);
        return NULL;
    }

    nread = fread(&rows,sizeof(int32_t),1,lsrowfile);
    if(nread != 1)
    {
        printf("Ivalid lsrowfile detected\n");
        fclose(lsrowfile);
        return NULL;
    }
    rows = BYTE_SWAP_4(rows);

    nread = fread(&cols,sizeof(int32_t),1,lsrowfile);
    if(nread != 1)
    {
        printf("Ivalid lsrowfile detected\n");
        fclose(lsrowfile);
        return NULL;
    }
    cols = BYTE_SWAP_4(cols);

    int tot_pts = rows*cols;

    printf("reading lsrowfile with %d rows and %d columns\n",rows,cols);
    uint16_t *img = new uint16_t[tot_pts];

    nread = fread(img,sizeof(uint16_t),tot_pts,lsrowfile);
    if(nread != tot_pts)
    {
        printf("Ivalid lsrowfile detected\n");
        fclose(lsrowfile);
        return NULL;
    }

    // byte swap the image
    for(int i=0;i < tot_pts;i++)
        img[i] = BYTE_SWAP_2(img[i]);

    // this version of the code generates a correction for columns-modulo eight. (it generates a correction
    // for columns that belong to one of eight different groups, where membership is determined from 
    // (column % 8))
    ChannelXTCorrection *xtptr = new ChannelXTCorrection();
    float *pvects = xtptr->AllocateVectorStorage(N_CORRECTION_GROUPS,nLen);
    float **vect_ptrs = xtptr->AllocateVectorPointerStorage(N_CORRECTION_GROUPS);
    xtptr->SetVectorIndicies(indicies,nLen);

    bool correction_valid = true;

    for(int i=0;i < N_CORRECTION_GROUPS;i++)
    {
        vect_ptrs[i] = pvects+nLen*i;
        if (GenerateGroupCorrection(i,vect_ptrs[i],rows,cols,img) == false)
        {
            correction_valid = false;
            break;
        }
    }

    delete [] img;
    fclose(lsrowfile);

    if(!correction_valid)
    {
        printf("Unable to compute valid correction\n");
        delete xtptr;
        return NULL;
    }

    return xtptr;
}