gina-alaska / dans-gdal-scripts

A number of utilities for use in conjunction with GDAL.
http://www.gina.alaska.edu/projects/gina-tools/
Other
159 stars 42 forks source link

calculate alpha channel directly in gdal_trace_outline #13

Open xjock opened 6 years ago

xjock commented 6 years ago

Add this function to polygon-rasterizer.cc and we could get the alpah channel tiff file directly . Then we can use gdal_merge_simple to generate the rgba image.

There is no need to generate the temporary pbm and png files .

void mask_alpha_from_mpoly(const Mpoly &mpoly, size_t w, size_t h, const std::string &fn) { printf("mask draw: begin\n"); printf("%d,%d\n",w,h); std::vector rows = get_row_crossings(mpoly, 0, h); GDALAllRegister(); GDALDatasetH hDstDS; GDALDriverH hDriver = GDALGetDriverByName("GTiff"); char *papszOptions = NULL; hDstDS = GDALCreate( hDriver, fn.c_str(), w, h, 1, GDT_Byte, papszOptions ); if( hDstDS == NULL ) { printf("cannot open mask output\n"); } GDALRasterBandH hBand = GDALGetRasterBand( hDstDS, 1 ); uint8_t line=(uint8_t )CPLMalloc(sizeof(uint8_t) w);

    printf("mask draw: write\n");
    for(size_t y=0; y<h; y++) {
            uint8_t bitp = 128;
            const row_crossings_t &r = rows[y];
            for(size_t i=0; i<w; i++) {
                    uint8_t v = 1;
                    // not the fastest way...
                    for(size_t j=0; j<r.size(); j++) {
                            if(double(i) >= r[j]) v = !v;
                    }
                    if(v) line[i] = 0;
                    else line[i] = 255;
            }
            GDALRasterIO(hBand, GF_Write, 0, y, w, 1, line, w, 1, GDT_Byte, 0, 0 );
    }
    if(line != NULL)
    {
            CPLFree(line);
    }
    GDALClose( hDstDS );

    printf("mask draw: done\n");

}

dstahlke commented 6 years ago

Thanks. If you send it as a pull request, I'll add it to the repository.