#include <stdio.h>
#include "pslib.h"

#include "pmFPA.h"

// Get the bias images for a readout, using the CELL.BIASSEC
psList *pmReadoutGetBias(pmReadout *readout // Readout for which to get the bias sections
                        )
{
    pmCell *cell = readout->parent;     // The parent cell
    psList *sections = (psList*)psMetadataLookupPtr(NULL, cell->concepts, "CELL.BIASSEC"); // CELL.BIASSEC

    psImage *image = readout->image;    // The image that contains both the biassec and the trimsec

    psList *images = psListAlloc(NULL); // List of images from bias sections
    psListIterator *sectionsIter = psListIteratorAlloc(sections, PS_LIST_HEAD, true); // Iterator
    psRegion *region = NULL;            // Bias region from list
    while ((region = psListGetAndIncrement(sectionsIter))) {
        psImage *bias = psMemIncrRefCounter(psImageSubset(image, *region)); // Image from bias section
        psListAdd(images, PS_LIST_TAIL, bias);
        psFree(bias);
    }
    psFree(sectionsIter);

    return images;
}
