IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 14, 2006, 2:40:02 PM (20 years ago)
Author:
Paul Price
Message:

Adding functions to write masks. For this to work, had to add mask handling to pmHDUGenerate, and figured I may as well add weights as well.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/camera/pmHDUGenerate.c

    r9730 r9983  
    218218
    219219        pmReadout *readout = cell->readouts->data[0]; // The first readout, as representative
    220         psImage *image = readout->image;// The proper image
     220        // The proper image, used to get the size
     221        psImage *image = readout->image ? readout->image : (readout->mask ? readout->mask : readout->weight);
    221222        if (!image) {
    222223            continue;
     224        }
     225        if (readout->mask &&
     226                (readout->mask->numCols != image->numCols || readout->mask->numRows != image->numRows)) {
     227            psLogMsg(__func__, PS_LOG_WARN, "Image and mask have different sizes (%dx%d vs %dx%d)!\n",
     228                     image->numCols, image->numRows, readout->mask->numCols, readout->mask->numRows);
     229        }
     230        if (readout->weight &&
     231                (readout->weight->numCols != image->numCols || readout->weight->numRows != image->numRows)) {
     232            psLogMsg(__func__, PS_LOG_WARN, "Image and weight have different sizes (%dx%d vs %dx%d)!\n",
     233                     image->numCols, image->numRows, readout->weight->numCols, readout->weight->numRows);
    223234        }
    224235        // New reference
     
    239250    return (position > 0);
    240251}
     252
     253// Check the type for a current image against a previous type
     254static psElemType checkTypes(psElemType previous, // Previously defined type, or 0
     255                             psElemType current // Current type
     256                            )
     257{
     258    if (previous == 0) {
     259        return current;
     260    }
     261
     262    if (previous != current) {
     263        psLogMsg(__func__, PS_LOG_WARN, "Images within the HDU are of different types "
     264                 "(%x vs %x) --- promoting\n", previous, current);
     265        return PS_MAX(previous, current);
     266    }
     267
     268    return previous;
     269}
     270
     271
     272// Paste the source image into the target, according to the provided region.  The source is then updated to
     273// reference the region within the target.
     274static void pasteImage(psImage *target, // Target image, into which the paste is made
     275                       psImage **sourcePtr,// Source image, from which the paste is made, and then changed
     276                       psRegion *region // Image section into which to paste
     277                      )
     278{
     279    psImage *source = *sourcePtr;       // Dereference pointer, for convenience
     280    if (source->numCols != region->x1 - region->x0 || source->numRows != region->y1 - region->y0) {
     281        psString regionString = psRegionToString(*region);
     282        psLogMsg(__func__, PS_LOG_WARN, "Image size (%dx%d) does not match region (%s).\n",
     283                 source->numCols, source->numRows, regionString);
     284        psFree(regionString);
     285    }
     286    psImageOverlaySection(target, source, region->x0, region->y0, "=");
     287
     288    // Reference the HDU version, so that subsequent changes will touch the HDU
     289    psFree(source);
     290    *sourcePtr = psImageSubset(target, *region);
     291
     292    return;
     293}
     294
    241295
    242296// Generate the HDU, given a list of cells below that HDU.  This is the main engine function, that does all
     
    248302    // Check the number of readouts is consistent within the HDU
    249303    int numReadouts = -1;               // Number of readouts
    250     psElemType type = 0;                // Type of readout images
     304    psElemType imageType = 0;           // Type of readout images
     305    psElemType maskType = 0;   // Type of readout masks
     306    psElemType weightType = 0;          // Type of readout weights
    251307    {
    252308        psListIterator *iter = psListIteratorAlloc(cells, PS_LIST_HEAD, false); // Iterator for cells
     
    264320            for (int i = 0; i < numReadouts; i++) {
    265321                pmReadout *readout = readouts->data[i]; // The readout
    266                 if (!readout || !readout->image) {
     322                if (!readout) {
    267323                    continue;
    268324                }
    269                 psElemType imageType = readout->image->type.type; // Type for this image
    270                 if (type == 0) {
    271                     type = imageType;
    272                 } else if (type != imageType) {
    273                     psLogMsg(__func__, PS_LOG_WARN, "Images within the HDU are of different types "
    274                              "(%x vs %x) --- promoting\n", type, imageType);
    275                     type = PS_MAX(type, imageType);
     325
     326                if (readout->image) {
     327                    imageType = checkTypes(imageType, readout->image->type.type);
     328                }
     329                if (readout->mask) {
     330                    maskType = checkTypes(maskType, readout->mask->type.type);
     331                }
     332                if (readout->weight) {
     333                    weightType = checkTypes(weightType, readout->weight->type.type);
    276334                }
    277335            }
     
    279337        psFree(iter);
    280338    }
    281     if (numReadouts == 0 || type == 0) {
     339    if (numReadouts == 0 || (imageType == 0 && maskType == 0 && weightType == 0)) {
    282340        // Nothing from which to create an HDU
    283341        psFree(cells);
     
    293351
    294352    // Generate the HDU
    295     hdu->images = psArrayAlloc(numReadouts);
    296     for (int i = 0; i < numReadouts; i++) {
    297         psImage *image = psImageAlloc(xSize, ySize, type);
    298         psImageInit(image, 0.0);
    299         hdu->images->data[i] = image;
     353    if (imageType) {
     354        hdu->images = psArrayAlloc(numReadouts);
     355        for (int i = 0; i < numReadouts; i++) {
     356            psImage *image = psImageAlloc(xSize, ySize, imageType);
     357            psImageInit(image, 0.0);
     358            hdu->images->data[i] = image;
     359        }
     360    }
     361    if (maskType) {
     362        hdu->masks = psArrayAlloc(numReadouts);
     363        for (int i = 0; i < numReadouts; i++) {
     364            psImage *mask = psImageAlloc(xSize, ySize, maskType);
     365            psImageInit(mask, 0);
     366            hdu->masks->data[i] = mask;
     367        }
     368    }
     369    if (weightType) {
     370        hdu->weights = psArrayAlloc(numReadouts);
     371        for (int i = 0; i < numReadouts; i++) {
     372            psImage *weight = psImageAlloc(xSize, ySize, weightType);
     373            psImageInit(weight, 0.0);
     374            hdu->weights->data[i] = weight;
     375        }
    300376    }
    301377
     
    319395            psArray *readouts = cell->readouts; // Array of readouts
    320396            psArray *hduImages = hdu->images; // Array of images in the HDU
     397            psArray *hduMasks = hdu->masks; // Array of masks in the HDU
     398            psArray *hduWeights = hdu->weights; // Array of weights in the HDU
    321399            for (int i = 0; i < readouts->n; i++) {
    322400                pmReadout *readout = readouts->data[i]; // The readout of interest
     
    324402                    continue;
    325403                }
    326                 psImage *image = readout->image; // The image pixels
    327                 psImage *hduImage = hduImages->data[i]; // The HDU image of interest
    328 
    329                 if (image->numCols != trimsec->x1 - trimsec->x0 ||
    330                         image->numRows != trimsec->y1 - trimsec->y0) {
    331                     psString trimsecString = psRegionToString(*trimsec);
    332                     psLogMsg(__func__, PS_LOG_WARN, "Image size (%dx%d) does not match CELL.TRIMSEC (%s).\n",
    333                              image->numCols, image->numRows, trimsecString);
    334                     psFree(trimsecString);
    335                 }
    336                 psImageOverlaySection(hduImage, image, trimsec->x0, trimsec->y0, "=");
    337 
    338                 // Reference the HDU version, so that subsequent changes will touch the HDU
    339                 psFree(image);
    340                 readout->image = psImageSubset(hduImage, *trimsec);
     404
     405                if (readout->image) {
     406                    pasteImage(hduImages->data[i], &readout->image, trimsec);
     407                }
     408                if (readout->mask) {
     409                    pasteImage(hduMasks->data[i], &readout->mask, trimsec);
     410                }
     411                if (readout->weight) {
     412                    pasteImage(hduWeights->data[i], &readout->weight, trimsec);
     413                }
    341414
    342415                if (biassecs->n != readout->bias->n) {
     
    351424                while ((bias = psListGetAndIncrement(biasIter)) &&
    352425                        (biassec = psListGetAndIncrement(biassecsIter))) {
    353                     if (bias->numCols != biassec->x1 - biassec->x0 ||
    354                             bias->numRows != biassec->y1 - biassec->y0) {
    355                         psString biassecString = psRegionToString(*biassec);
    356                         psLogMsg(__func__, PS_LOG_WARN, "Bias size (%dx%d) does not match CELL.BIASSEC (%s)."
    357                                  "\n", bias->numCols, bias->numRows, biassecString);
    358                         psFree(biassecString);
    359                     }
    360 
    361                     psImageOverlaySection(hduImage, bias, biassec->x0, biassec->y0, "=");
    362 
    363                     // Reference the HDU version, so that subsequent changes will touch the HDU
    364                     bias = psImageSubset(hduImage, *biassec);
     426                    pasteImage(hduImages->data[i], &bias, biassec);
    365427                    psListAdd(newBias, PS_LIST_TAIL, bias);
    366428                }
Note: See TracChangeset for help on using the changeset viewer.