[Gdal-dev] GDALWarp Gif Problems

Chris Portka cportka at newwireless.com
Tue Jan 23 20:10:19 EST 2007


Frank Warmerdam wrote:
> Chris Portka wrote:
>> Thanks for your feedback.  I think I may have narrowed down the 
>> problem, but I'm confused on one point that I can't find 
>> documentation for.  In the warp tutorial there is the following line 
>> for getting the suggested output:
>>
>> eErr = GDALSuggestedWarpOutput 
>> <http://gdal.org/gdal__alg_8h.html#816819e7495bfce06dbd110f7c57af65>( 
>> hSrcDS,                                    GDALGenImgProjTransform, 
>> hTransformArg,                                    adfDstGeoTransform, 
>> &nPixels, &nLines );
>>
>> I understand what all these arguments are except 
>> GDALGenImgProjTransform, which I believe to be the transformer 
>> function's signature, but I'm unclear.  Where is this defined?  How 
>> does this interact or differ from hTransformArg?  I'm pretty sure it 
>> is something to do with this interaction that is acting funny.  Are 
>> there alternatives to this such as a GDALReprojectionTransform?  Is 
>> there a list of all possible alternatives or a way to construct one 
>> myself?  
>
> Chris,
>
> The transformer is an actual function while the transformarg is the
> private data used by the transformer to keep track of what it is
> supposed to do.  The GDALTransformerFunc concept is documented at:
>
>   http://gdal.org/gdal__alg_8h.html#9ad4227ec5fd5b70637eeb6996172318
>
> You can also get there by following the GDALTransformerFunc link from the
> GDALSuggestedWarpOutput() documentation you referenced.
>
> You can implement your own transformation function though that should
> rarely be needed.  The GDALTransformFunc docs list some of the common
> implementations.  But for your case you will presumably want to use
> GDALGenImgProjTransform.
>
> Best regards,
Thanks for all your help so far.  I can now "successfully" warp without 
an exception occurring.  However, now when I try to actually read the 
data it is corrupted, or entirely blank (every pixel of the output file 
is black).  I think I'm having a problem getting  all the bands copied 
out of the raster file.  I want every band from the source raster to be 
copied and transformed to the destination, is there an easy way to 
specify this?  I'm also unclear on the type for the buffer I should be 
passing to WarpRegionToBuffer - I'm assuming an nPixels * nLines * 
nBands unsigned char array (the source raster is a 24-bit gif or jpg, so 
nBands gives me 3).  Here's my code (I've labeled where the band code 
starts):

    GDALDriverH hDriver;
    GDALDataType eDT;
    GDALDatasetH hDstDS;
    GDALDatasetH hSrcDS;

    GDALAllRegister();

    hSrcDS = GDALOpen(fileName.toAscii(), GA_ReadOnly);
    if (hSrcDS == NULL) {
        qDebug() << "Problem opening dataset";
    }

    eDT = GDALGetRasterDataType(GDALGetRasterBand(hSrcDS,1));

    hDriver = GDALGetDriverByName("MEM");
    if (hDriver == NULL) {
        qDebug() << "Problem creating driver";
    }

    char *pszSrcWKT = NULL;
    char *pszDstWKT = NULL;

    OGRSpatialReference srcSR;
    srcSR.SetWellKnownGeogCS("WGS84");
    srcSR.SetUTM(18, TRUE);
    srcSR.exportToWkt(&pszSrcWKT);
    if (pszSrcWKT == NULL || strlen(pszSrcWKT) <= 0) {
        qDebug() << "Problem creating pszSrcWKT";
    }

    OGRSpatialReference dstSR;
    dstSR.SetWellKnownGeogCS("WGS84");
    dstSR.SetUTM(17, TRUE);
    dstSR.exportToWkt(&pszDstWKT);
    if (pszDstWKT == NULL || strlen(pszDstWKT) <= 0) {
        qDebug() << "Problem creating pszDstWKT";
    }

    void *hTransformArg = NULL;
    hTransformArg = GDALCreateReprojectionTransformer(pszSrcWKT, 
pszDstWKT);
    if (hTransformArg == NULL) {
        qDebug() << "Problems creating the transformer";
    }

    double adfDstGeoTransform[6];
    int nPixels = 0;
    int nLines = 0;
    CPLErr eErr;
    eErr = GDALSuggestedWarpOutput(hSrcDS, GDALReprojectionTransform,
        hTransformArg, adfDstGeoTransform, &nPixels, &nLines);
    if (eErr != CE_None) {
        qDebug() << "Problem getting suggested warp output";
    }

    GDALDestroyReprojectionTransformer(hTransformArg);

    hDstDS = GDALCreate(hDriver, outputFileName.toAscii(), nPixels, nLines,
        GDALGetRasterCount(hSrcDS), eDT, NULL);
    if (hDstDS == NULL) {
        qDebug() << "Problem creating destination";
    }

    GDALSetProjection(hDstDS, pszDstWKT);
    GDALSetGeoTransform(hDstDS, adfDstGeoTransform);

    GDALColorTableH hCT;
    hCT = GDALGetRasterColorTable(GDALGetRasterBand(hSrcDS, 1));
    if(hCT != NULL) {
        GDALSetRasterColorTable(GDALGetRasterBand(hDstDS, 1), hCT);
    }

    GDALWarpOptions *warpOptions = GDALCreateWarpOptions();
    warpOptions->hSrcDS = hSrcDS;
    warpOptions->hDstDS = hDstDS;


    //HERES WHERE THE BAND CODE STARTS

    warpOptions->nBandCount = GDALGetRasterCount(hSrcDS);
    warpOptions->panSrcBands = (int *) CPLMalloc(sizeof(int) * 
warpOptions->nBandCount);
    for (int x = 0; x < warpOptions->nBandCount; x++) {
        warpOptions->panSrcBands[x] = x+1;
    }

    warpOptions->panDstBands = (int *) CPLMalloc(sizeof(int) * 
warpOptions->nBandCount);
    for (int x = 0; x < warpOptions->nBandCount; x++) {
        warpOptions->panDstBands[x] = x+1;
    }

    //HERES WHERE THE BAND CODE ENDS

    warpOptions->pfnProgress = GDALTermProgress;

    warpOptions->pTransformerArg = 
GDALCreateReprojectionTransformer(pszSrcWKT, pszDstWKT);
    if (warpOptions->pTransformerArg == NULL) {
        qDebug() << "Problem creating transformer for warp options";
    }
    warpOptions->pfnTransformer = GDALReprojectionTransform;


    GDALWarpOperation operation;
    unsigned char *destination = (unsigned char *) new char[nPixels * 
nLines * warpOptions->nBandCount];

    operation.Initialize(warpOptions);
    operation.WarpRegionToBuffer(0, 0, nPixels, nLines,    destination, 
GDT_Unknown);

    GDALDestroyReprojectionTransformer(warpOptions->pTransformerArg);
    GDALDestroyWarpOptions(warpOptions);
    GDALClose(hSrcDS);
    GDALClose(hDstDS);

    image.loadFromData(destination, nPixels * nLines * 
warpOptions->nBandCount);
    ...displayimage...

Thanks,
Chris



More information about the Gdal-dev mailing list