#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

# include "psphot.h"

// 2006.02.04 : no leaks
bool psphotMaskReadout (pmReadout *readout, psMetadata *recipe) {
    
    bool status;

    // mask the excluded outer pixels
    // these coordinates refer to the parent image
    // these bounds will saturate on the subimage
    // negative upper bounds will subtract from the *subimage*
    float XMIN  = psMetadataLookupF32 (&status, recipe, "XMIN");
    float XMAX  = psMetadataLookupF32 (&status, recipe, "XMAX");
    float YMIN  = psMetadataLookupF32 (&status, recipe, "YMIN");
    float YMAX  = psMetadataLookupF32 (&status, recipe, "YMAX");
    psRegion valid = psRegionSet (XMIN, XMAX, YMIN, YMAX);

    // psRegionForImage creates a region for the subimage, not the parent
    // XXX I propose that psRegionForImage treat the region as referring to the parent...
    psRegion keep = psRegionForImage (readout->image, valid);

    // psImageKeepRegion assumes the region refers to the parent coordinates
    psImageKeepRegion (readout->mask, keep, "OR", PM_MASK_BAD);

    return true;
}
