I am writing a program in python that converts raster files using gdal, and using VRT as an intermediate format. One of the things that i need to do is convert bands between different formats. I am pretty sure that you do this using Pixel Functions. Since python doesn't have this functionality, i have to use C++ for this part. I used some code from the VRT tutorial website, but it doesn't seem to work.

GDALAddDerivedBandPixelFunc("MyFirstFunction", TestFunction);

typedef CPLErr
(*GDALDerivedPixelFunc)(void **papoSources, int nSources, void *pData,
                        int nXSize, int nYSize,
                        GDALDataType eSrcType, GDALDataType eBufType,
                        int nPixelSpace, int nLineSpace);

I implement that function with the code from http://www.gdal.org/gdal_vrttut.html, but it doesn't seem to be working. I am calling gdal_translate to do the conversion process, but it seems like the pixel function does not stay. The addpixelfunc returns a result of 0. Is there some way to write a GDAL extension to have this function be permanent?


I have been able to correctly create a pixel function and get it to work if i manually convert the file in the same C++ program as the GDALAddDerivedBandPixelFunc resides, however i am still interested in how i could make this permanent. Although it may not be needed since i might want to let the user change some numbers in the function.

1 Answer 1



It is not possible to have user supplied functions outside the program in which they are registered. They really are rather special purpose.

You mention your original need is to convert bands between different formats. Perhaps you mean different pixel data types? Are you just wanting to convert the values directly or do you need to rescale?

If you want direct conversion (with default rules for clipping and floating point to integer conversion) there is nothing special you need to do. Just set the type appropriately for the virtual band in the VRT and refer back to the original band regardless of it's type.

If you want rescaling there are extended VRT parameters for this. I'd suggest you review the implementation of -scale in gdal_translate which also uses an intermediate VRT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.