Skip to content

Commit

Permalink
Add the MakeCompexPixelFunc that make a complex out of a real and ima…
Browse files Browse the repository at this point in the history
…g bands
  • Loading branch information
alexamici committed Jun 23, 2015
1 parent 4038d40 commit d7eaf5f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ List of pixel functions
:"imag":
extract imaginary part from a single raster band (0 for
non-complex)
:"makecomplex":
make a complex band merging two bands used as real and imag values
:"mod":
extract module from a single raster band (real or complex)
:"phase":
Expand Down
19 changes: 19 additions & 0 deletions autotest/gcore/data/pixfun_makecomplex.vrt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<VRTDataset rasterXSize="20" rasterYSize="20">
<VRTRasterBand dataType="CFloat32" band="1" subClass="VRTDerivedRasterBand">
<Description>Make complex</Description>
<PixelFunctionType>makecomplex</PixelFunctionType>
<SourceTransferType>CFloat32</SourceTransferType>
<SimpleSource>
<SourceFilename relativeToVRT="1">int32.tif</SourceFilename>
<SourceBand>1</SourceBand>
<SrcRect xOff="0" yOff="0" xSize="20" ySize="20"/>
<DstRect xOff="0" yOff="0" xSize="20" ySize="20"/>
</SimpleSource>
<SimpleSource>
<SourceFilename relativeToVRT="1">int32.tif</SourceFilename>
<SourceBand>1</SourceBand>
<SrcRect xOff="0" yOff="0" xSize="20" ySize="20"/>
<DstRect xOff="0" yOff="0" xSize="20" ySize="20"/>
</SimpleSource>
</VRTRasterBand>
</VRTDataset>
26 changes: 26 additions & 0 deletions autotest/gcore/pixfun.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,31 @@ def pixfun_imag_r():
return 'success'


###############################################################################
# Verify imaginary part extraction from a real dataset.

def pixfun_makecomplex():

filename = 'data/pixfun_makecomplex.vrt'
ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
if ds is None:
gdaltest.post_reason('Unable to open "%s" dataset.' % filename)
return 'fail'
data = ds.GetRasterBand(1).ReadAsArray()

reffilename = 'data/int32.tif'
refds = gdal.Open(reffilename)
if refds is None:
gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename)
return 'fail'
refdata = refds.GetRasterBand(1).ReadAsArray()

if not numpy.allclose(data, refdata + 1j * refdata):
return 'fail'

return 'success'


###############################################################################
# Verify modulus extraction from a complex (float) dataset.

Expand Down Expand Up @@ -731,6 +756,7 @@ def pixfun_dB2pow():
pixfun_real_r,
pixfun_imag_c,
pixfun_imag_r,
pixfun_makecomplex,
pixfun_mod_c,
pixfun_mod_r,
pixfun_phase_c,
Expand Down
34 changes: 34 additions & 0 deletions pixelfunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,39 @@ CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData,
} /* ImagPixelFunc */


CPLErr MakeComplexPixelFunc(void **papoSources, int nSources, void *pData,
int nXSize, int nYSize,
GDALDataType eSrcType, GDALDataType eBufType,
int nPixelSpace, int nLineSpace)
{
int ii, iLine, iCol;
double adfPixVal[2];

void *pReal = papoSources[0];
void *pImag = papoSources[1];

/* ---- Init ---- */
if (nSources != 2) return CE_Failure;

/* ---- Set pixels ---- */
for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) {
for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) {

/* Source raster pixels may be obtained with SRCVAL macro */
adfPixVal[0] = SRCVAL(pReal, eSrcType, ii); /* re */
adfPixVal[1] = SRCVAL(pImag, eSrcType, ii); /* im */

GDALCopyWords(adfPixVal, GDT_CFloat64, 0,
((GByte *)pData) + nLineSpace * iLine +
iCol * nPixelSpace, eBufType, nPixelSpace, 1);
}
}

/* ---- Return success ---- */
return CE_None;
} /* MakeComplexPixelFunc */


CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData,
int nXSize, int nYSize,
GDALDataType eSrcType, GDALDataType eBufType,
Expand Down Expand Up @@ -800,6 +833,7 @@ CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc()
{
GDALAddDerivedBandPixelFunc("real", RealPixelFunc);
GDALAddDerivedBandPixelFunc("imag", ImagPixelFunc);
GDALAddDerivedBandPixelFunc("makecomplex", MakeComplexPixelFunc);
GDALAddDerivedBandPixelFunc("mod", ModulePixelFunc);
GDALAddDerivedBandPixelFunc("phase", PhasePixelFunc);
GDALAddDerivedBandPixelFunc("conj", ConjPixelFunc);
Expand Down

0 comments on commit d7eaf5f

Please sign in to comment.