void p_cubic_f(struct cache *ibuffer, /* input buffer */ void *obufptr, /* ptr in output buffer */ int cell_type, /* raster map type of obufptr */ double *col_idx, /* column index */ double *row_idx, /* row index */ struct Cell_head *cellhd /* cell header of input layer */ ) { /* start nearest neighbor to do some basic tests */ int row, col; /* row/col of nearest neighbor */ FCELL *cellp, cell; /* cut indices to integer */ row = (int)floor(*row_idx); col = (int)floor(*col_idx); /* check for out of bounds - if out of bounds set NULL value */ if (row < 0 || row >= cellhd->rows || col < 0 || col >= cellhd->cols) { Rast_set_null_value(obufptr, 1, cell_type); return; } cellp = CPTR(ibuffer, row, col); /* if nearest is null, all the other interps will be null */ if (Rast_is_f_null_value(cellp)) { Rast_set_null_value(obufptr, 1, cell_type); return; } cell = *cellp; p_cubic(ibuffer, obufptr, cell_type, col_idx, row_idx, cellhd); /* fallback to bilinear if cubic is null */ if (Rast_is_f_null_value(obufptr)) { p_bilinear(ibuffer, obufptr, cell_type, col_idx, row_idx, cellhd); /* fallback to nearest if bilinear is null */ if (Rast_is_f_null_value(obufptr)) Rast_set_f_value(obufptr, cell, cell_type); } }
int main( int argc, char **argv ) { char *name = nullptr; struct Option *map; struct Cell_head window; G_gisinit( argv[0] ); G_define_module(); map = G_define_standard_option( G_OPT_R_OUTPUT ); if ( G_parser( argc, argv ) ) exit( EXIT_FAILURE ); name = map->answer; #ifdef Q_OS_WIN _setmode( _fileno( stdin ), _O_BINARY ); _setmode( _fileno( stdout ), _O_BINARY ); //setvbuf( stdin, NULL, _IONBF, BUFSIZ ); // setting _IONBF on stdout works on windows correctly, data written immediately even without fflush(stdout) //setvbuf( stdout, NULL, _IONBF, BUFSIZ ); #endif QgsGrassDataFile stdinFile; stdinFile.open( stdin ); QDataStream stdinStream( &stdinFile ); QFile stdoutFile; stdoutFile.open( stdout, QIODevice::WriteOnly | QIODevice::Unbuffered ); QDataStream stdoutStream( &stdoutFile ); qint32 proj, zone; stdinStream >> proj >> zone; QgsRectangle extent; qint32 rows, cols; stdinStream >> extent >> cols >> rows; checkStream( stdinStream ); QString err = QgsGrass::setRegion( &window, extent, rows, cols ); if ( !err.isEmpty() ) { G_fatal_error( "Cannot set region: %s", err.toUtf8().constData() ); } window.proj = ( int ) proj; window.zone = ( int ) zone; G_set_window( &window ); Qgis::DataType qgis_type; qint32 type; stdinStream >> type; checkStream( stdinStream ); qgis_type = ( Qgis::DataType )type; RASTER_MAP_TYPE grass_type; switch ( qgis_type ) { case Qgis::Int32: grass_type = CELL_TYPE; break; case Qgis::Float32: grass_type = FCELL_TYPE; break; case Qgis::Float64: grass_type = DCELL_TYPE; break; default: G_fatal_error( "QGIS data type %d not supported", qgis_type ); return 1; } cf = Rast_open_new( name, grass_type ); if ( cf < 0 ) { G_fatal_error( "Unable to create raster map <%s>", name ); return 1; } void *buf = Rast_allocate_buf( grass_type ); int expectedSize = cols * QgsRasterBlock::typeSize( qgis_type ); bool isCanceled = false; QByteArray byteArray; for ( int row = 0; row < rows; row++ ) { stdinStream >> isCanceled; checkStream( stdinStream ); if ( isCanceled ) { break; } double noDataValue; stdinStream >> noDataValue; stdinStream >> byteArray; checkStream( stdinStream ); if ( byteArray.size() != expectedSize ) { G_fatal_error( "Wrong byte array size, expected %d bytes, got %d, row %d / %d", expectedSize, byteArray.size(), row, rows ); return 1; } qint32 *cell = nullptr; float *fcell = nullptr; double *dcell = nullptr; if ( grass_type == CELL_TYPE ) cell = ( qint32 * ) byteArray.data(); else if ( grass_type == FCELL_TYPE ) fcell = ( float * ) byteArray.data(); else if ( grass_type == DCELL_TYPE ) dcell = ( double * ) byteArray.data(); void *ptr = buf; for ( int col = 0; col < cols; col++ ) { if ( grass_type == CELL_TYPE ) { if ( ( CELL )cell[col] == ( CELL )noDataValue ) { Rast_set_c_null_value( ( CELL * )ptr, 1 ); } else { Rast_set_c_value( ptr, ( CELL )( cell[col] ), grass_type ); } } else if ( grass_type == FCELL_TYPE ) { if ( ( FCELL )fcell[col] == ( FCELL )noDataValue ) { Rast_set_f_null_value( ( FCELL * )ptr, 1 ); } else { Rast_set_f_value( ptr, ( FCELL )( fcell[col] ), grass_type ); } } else if ( grass_type == DCELL_TYPE ) { if ( ( DCELL )dcell[col] == ( DCELL )noDataValue ) { Rast_set_d_null_value( ( DCELL * )ptr, 1 ); } else { Rast_set_d_value( ptr, ( DCELL )dcell[col], grass_type ); } } ptr = G_incr_void_ptr( ptr, Rast_cell_size( grass_type ) ); } Rast_put_row( cf, buf, grass_type ); #ifndef Q_OS_WIN // Because stdin is somewhere buffered on Windows (not clear if in QProcess or by Windows) // we cannot in QgsGrassImport wait for this because it hangs. Setting _IONBF on stdin does not help // and there is no flush() on QProcess. // OTOH, smaller stdin buffer is probably blocking QgsGrassImport so that the import can be canceled immediately. stdoutStream << ( bool )true; // row written stdoutFile.flush(); #endif } if ( isCanceled ) { Rast_unopen( cf ); } else { Rast_close( cf ); struct History history; Rast_short_history( name, "raster", &history ); Rast_command_history( &history ); Rast_write_history( name, &history ); } exit( EXIT_SUCCESS ); }