Skip to content

Instantly share code, notes, and snippets.

@thejefflarson
Created July 1, 2013 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejefflarson/5903830 to your computer and use it in GitHub Desktop.
Save thejefflarson/5903830 to your computer and use it in GitHub Desktop.
#include <gdal_priv.h>
#include <cpl_conv.h>
#include <stdio.h>
uint32_t htonf(float f){
uint32_t p;
uint32_t sign = 0;
if (f < 0) { f = 0; } // floor
p = ((((uint32_t)f)&0x7fff)<<16) | (sign<<31); // whole part and sign
p |= (uint32_t)(((f - (int)f) * 65536.0f))&0xffff; // fraction
return p;
}
int main(int argc, char **argv){
if(argc != 3) {
puts("Usage: packmap <gray_valued_raster> <out>");
exit(1);
}
GDALAllRegister();
GDALDataset *dataset = (GDALDataset *) GDALOpen(argv[1], GA_ReadOnly);
if(dataset == NULL) { printf("couldn't open %s", argv[1]); exit(1); }
GDALRasterBand *band = dataset->GetRasterBand(1);
int xSize = band->GetXSize(), ySize = band->GetYSize();
float *scanline = (float *) CPLMalloc(sizeof(float)*xSize);
GDALDriver *driver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset *outDS = driver->Create(argv[2], xSize, ySize, 4, GDT_Byte, NULL);
GDALRasterBand *aband = outDS->GetRasterBand(4);
for(int y = 0; y < ySize; y++) {
band->RasterIO(GF_Read, 0, y, xSize, 1, scanline, xSize, 1, band->GetRasterDataType(), 0, 0);
for(int x = 0; x < xSize; x++) {
uint32_t i1 = htonf(scanline[x]);
for(int i = 1; i <= 4; i++){
GDALRasterBand *band = outDS->GetRasterBand(i);
// HACCCCCKKKZZZZZZ
char* it = ((char*)&i1);
band->RasterIO(GF_Write, x, y, 1, 1, &(it[i - 1]), 1, 1, GDT_Byte, 0, 0);
}
}
}
GDALClose((GDALDatasetH) dataset);
GDALClose((GDALDatasetH) outDS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment