IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 14, 2004, 12:39:58 PM (22 years ago)
Author:
desonia
Message:

Interim/pretest version.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/image/psImage.c

    r664 r692  
    77 *  @author Ross Harman, MHPCC
    88 *   
    9  *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2004-05-13 20:03:35 $
     9 *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2004-05-14 22:39:58 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1818#include "psMemory.h"
    1919#include "psError.h"
    20 #include "psVector.h"
    2120#include "psImage.h"
    2221
    23 /******************************************************************************/
    24 /*  DEFINE STATEMENTS                                                         */
    25 /******************************************************************************/
    26 
    27 // None
    28 
    29 /******************************************************************************/
    30 /*  TYPE DEFINITIONS                                                          */
    31 /******************************************************************************/
    32 
    33 // None
    34 
    35 /*****************************************************************************/
    36 /*  GLOBAL VARIABLES                                                         */
    37 /*****************************************************************************/
    38 
    39 // None
    40 
    41 /*****************************************************************************/
    42 /*  FILE STATIC VARIABLES                                                    */
    43 /*****************************************************************************/
    44 
    45 // None
    46 
    47 /*****************************************************************************/
    48 /*  FUNCTION IMPLEMENTATION - LOCAL                                          */
    49 /*****************************************************************************/
    50 
    51 // None
     22#include <string.h>
    5223
    5324/*****************************************************************************/
     
    5526/*****************************************************************************/
    5627
    57 psImage *psImageAlloc(unsigned int numCols, unsigned int numRows, psType type)
     28psImage *psImageAlloc(unsigned int numCols, unsigned int numRows, const psElemType type)
    5829{
    5930    int area = 0;
    60     psElemType imageType = 0;
    61     psDimen imageDim = 0;
    62     int elementSize = PSELEMTYPE_SIZEOF(type.type); // element size in bytes
     31    int elementSize = PSELEMTYPE_SIZEOF(type); // element size in bytes
    6332    int rowSize = numCols*elementSize;  // row size in bytes.
    6433
    65     imageType = type.type;
    66     imageDim =  type.dimen;
    6734    area = numCols*numRows;
    68 
    69     if(imageDim != PS_DIMEN_IMAGE) {
    70         psError(__func__, " : Image dimensionality not PS_DIMEN_IMAGE (3). Dimensionality: %d\n",
    71                 imageDim);
    72         return NULL;
    73     } else if(area <= 0) {
    74         psError(__func__, " : Invalid value for number of rows or columns. numRows: %d numCols: %d\n",
    75                 numRows, numCols);
    76         return NULL;
    77     }
    7835
    7936    psImage *image = (psImage *)psAlloc(sizeof(psImage));
    8037
    81     image->data.v[0] = psAlloc(area*elementSize);
     38    if(area < 1) {
     39        image->data.v[0] = NULL;
     40    } else {
     41        image->data.v[0] = psAlloc(area*elementSize);
    8242
    83     for(int i = 1; i < numRows; i++) {
    84         image->data.v[i] = (void*)((int8_t*)image->data.v[i-1]+rowSize);
     43        for(int i = 1; i < numRows; i++) {
     44            image->data.v[i] = (void*)((int8_t*)image->data.v[i-1]+rowSize);
     45        }
    8546    }
    8647
    87     image->col0 = 0;
    88     image->row0 = 0;
    89     image->numCols = numCols;
    90     image->numRows = numRows;
    91     image->type = type;
     48    *(int*)&image->col0 = 0;
     49    *(int*)&image->row0 = 0;
     50    *(unsigned int*)&image->numCols = numCols;
     51    *(unsigned int*)&image->numRows = numRows;
     52    *(psDimen*)&image->type.dimen = PS_DIMEN_IMAGE;
     53    *(psElemType*)&image->type.type = type;
    9254    image->parent = NULL;
    9355    image->nChildren = 0;
     
    10163    int i = 0;
    10264    int nChildren = 0;
    103     psElemType imageType = 0;
    104     psImage *children = NULL;
     65    psImage **children = NULL;
    10566
    10667    if(image != NULL) {
    107         imageType = image->type.type;
    10868        nChildren = image->nChildren;
    10969        children = image->children;
     70
     71        for(i=0; i<nChildren; i++) {
     72            psImageFree(children[i]);
     73        }
    11074
    11175        psFree(image->data.v[0]);
    11276        psFree(image->data.v);
    11377
    114         for(i=0; i<nChildren; i++) {
    115             psImageFree(&children[i]);
    116         }
    11778    }
    11879}
     80
     81psImage *psImageSubset(psImage *out,psImage *image,unsigned int numCols,
     82                       unsigned int numRows, unsigned int col0, unsigned int row0)
     83{
     84    psElemType type;
     85    unsigned int elementSize;           // size of image element in bytes
     86    unsigned int outputRowSize;         // output row size in bytes
     87    unsigned int inputColOffset;        // offset in bytes to first subset pixel in input row
     88
     89    if (image == NULL) {
     90        psError(__func__,"Can not subset image because input image is NULL.");
     91        return NULL;
     92    }
     93
     94    if (image->type.dimen != PS_DIMEN_IMAGE) {
     95        psError(__func__,"Can not subset image because input image is not an image.");
     96        return NULL;
     97    }
     98
     99    /* validate subimage size */
     100    if (col0+numCols >= image->numCols || row0+numRows >= image->numRows) {
     101        psError(__func__,"Can not subset image outside of image boundaries (size=%dx%d, "
     102                "subset=[%d:%d,%d:%d]).",image->numCols,image->numRows,image->col0,
     103                image->col0+numCols, image->row0, image->row0+numRows);
     104        return NULL;
     105    }
     106
     107    type = image->type.type;
     108    elementSize = PSELEMTYPE_SIZEOF(type);
     109
     110    if (out == NULL) {
     111        out = psImageAlloc(numCols,numRows,image->type.type);
     112    } else if (out->numCols != numCols || out->numRows != numRows || out->type.type != type) {
     113        // sizes/type different, can't reuse the given image buffer
     114        psImageFree(out);
     115        out = psImageAlloc(numCols,numRows,image->type.type);
     116    }
     117
     118    // set the parent information into the child output image
     119    *(int*)&out->row0 = row0;
     120    *(int*)&out->col0 = col0;
     121    *(psImage**)&out->parent = (psImage*)image;
     122
     123    // add output image as a child of the input image.
     124    image->nChildren++;
     125    image->children = (psImage **) psRealloc(image->children,image->nChildren*sizeof(psImage *));
     126    image->children[image->nChildren-1] = out;
     127
     128    inputColOffset = elementSize*col0;
     129    outputRowSize = elementSize*numCols;
     130
     131    for (int row = 0; row < numRows; row++) {
     132        memcpy(out->data.v[row],image->data.i8[row0+row] + inputColOffset,outputRowSize);
     133    }
     134
     135    return (out);
     136}
Note: See TracChangeset for help on using the changeset viewer.