Skip to content

Commit

Permalink
i.edge: add -n flag for empty input/output (#460)
Browse files Browse the repository at this point in the history
* add -n flag to create empty output if input is empty
* do not compare signed and unsigned integers
  • Loading branch information
metzm committed Mar 1, 2021
1 parent fc7131b commit 070abd5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion grass7/imagery/i.edge/canny.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void performHysteresis(CELL * edges, CELL * magnitude, int low, int high,

void thresholdEdges(CELL * edges, int rows, int cols)
{
int i;
size_t i;
size_t max = (size_t)rows * cols;

for (i = 0; i < max; i++) {
Expand Down
66 changes: 54 additions & 12 deletions grass7/imagery/i.edge/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
#include "canny.h"
#include "gauss.h"


/** Loads map into memory.
\param[out] mat map in a matrix (row order), field have to be allocated
*/
static void readMap(const char *name, const char *mapset, int nrows,
static int readMap(const char *name, const char *mapset, int nrows,
int ncols, DCELL * mat)
{

int r, c;
int map_fd;
int check_reading;
Expand All @@ -46,7 +46,6 @@ static void readMap(const char *name, const char *mapset, int nrows,
row_buffer = Rast_allocate_d_input_buf();
check_reading = 0;


/* load map */
map_fd = Rast_open_old(name, mapset);
if (map_fd < 0) {
Expand Down Expand Up @@ -79,24 +78,23 @@ static void readMap(const char *name, const char *mapset, int nrows,
}
G_free(row_buffer);

if (!check_reading)
G_fatal_error(_("Input map contains no data"));

Rast_close(map_fd);

return check_reading;
}


/** Writes map from memory into the file.
\param[in] map map in a matrix (row order)
*/
static void writeMap(const char *name, int nrows, int ncols, CELL * map)
{
unsigned char *outrast; /* output buffer */

int outfd;
int r, c;

outfd = Rast_open_new(name, CELL_TYPE); /* FIXME: using both open old and open new */
int r, c;

outrast = Rast_allocate_buf(CELL_TYPE);
for (r = 0; r < nrows; r++) {
Expand Down Expand Up @@ -136,15 +134,17 @@ int main(int argc, char *argv[])
static const int MAGNITUDE_LIMIT = 1000;

int lowThreshold, highThreshold, low, high;
int nrows, ncols;
int nrows, ncols, i;
size_t dim_2;
DCELL *mat1;

struct History history; /* holds meta-data (title, comments,..) */
struct GModule *module; /* GRASS module for parsing arguments */

/* options */
struct Option *input, *output, *angleOutput,
*lowThresholdOption, *highThresholdOption, *sigmaOption;
struct Flag *nullflag;

size_t r;

Expand Down Expand Up @@ -195,6 +195,11 @@ int main(int argc, char *argv[])
sigmaOption->description = _("Kernel radius");
sigmaOption->answer = "2";

nullflag = G_define_flag();
nullflag->key = 'n';
nullflag->label = _("Create empty output if input map is empty");
nullflag->description = _("Default: no output and ERROR");

/* options and flags parser */
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -240,8 +245,6 @@ int main(int argc, char *argv[])
ncols = Rast_window_cols();
dim_2 = (size_t)nrows *ncols;

DCELL *mat1;

/* Memory allocation for map_1: */
mat1 = (DCELL *) G_calloc((dim_2), sizeof(DCELL));

Expand All @@ -256,7 +259,46 @@ int main(int argc, char *argv[])
G_fatal_error(_("Error getting first raster map type"));
*/

readMap(name, mapset, nrows, ncols, mat1);
if (!readMap(name, mapset, nrows, ncols, mat1)) {
if (!nullflag->answer)
G_fatal_error(_("Input map contains no data"));
else {
int outfd;
CELL *outrast; /* output buffer */

outfd = Rast_open_new(result, CELL_TYPE);

outrast = Rast_allocate_buf(CELL_TYPE);
Rast_set_c_null_value(outrast, ncols);
for (i = 0; i < nrows; i++) {
Rast_put_row(outfd, outrast, CELL_TYPE);
}

Rast_close(outfd);
/* add command line incantation to history file */
Rast_short_history(result, "raster", &history);
Rast_command_history(&history);
Rast_write_history(result, &history);

if (anglesMapName) {
outfd = Rast_open_new(anglesMapName, CELL_TYPE);

for (i = 0; i < nrows; i++) {
Rast_put_row(outfd, outrast, CELL_TYPE);
}

Rast_close(outfd);
/* add command line incantation to history file */
Rast_short_history(anglesMapName, "raster", &history);
Rast_command_history(&history);
Rast_write_history(anglesMapName, &history);
}

G_free(outrast);

exit(EXIT_SUCCESS);
}
}

/* **** */

Expand Down

0 comments on commit 070abd5

Please sign in to comment.