main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /****************************************************************************
  2. *
  3. * MODULE: r.bitpattern
  4. * AUTHOR(S): Radim Blazek
  5. * PURPOSE: bit pattern comparison
  6. * Functionality:
  7. * 1. define position: set bit(s) to 1 you want to match
  8. * then convert this position pattern to integer, set pattern=
  9. * parameter with that integer value
  10. * 2. define pattern *value* which should be in that position:
  11. * first bit pattern of value, convert to integer, set
  12. * patval= parameter
  13. *
  14. * 128 64 32 16 8 4 2 1
  15. * Example:
  16. * 1. define position
  17. * xx xx 1x xx
  18. * binary: 1000 -> integer: 8 -> pattern=8
  19. * 2. define value
  20. * Ex.: we want to check for 0 in that position
  21. * xx xx 0x xx
  22. * binary: 0000 -> integer: 0 -> patval=0
  23. * if value can be arbitray (0/1), then assume 0 value
  24. *
  25. * COPYRIGHT: (C) 2002 by the GRASS Development Team
  26. *
  27. * This program is free software under the GNU General Public
  28. * License (>=v2). Read the file COPYING that comes with GRASS
  29. * for details.
  30. *
  31. *****************************************************************************/
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <grass/gis.h>
  36. #include <grass/raster.h>
  37. #include <grass/glocale.h>
  38. extern CELL f_c(CELL);
  39. int main(int argc, char *argv[])
  40. {
  41. struct Cell_head cellhd;
  42. char *name, *result;
  43. void *inrast;
  44. unsigned char *outrast;
  45. int nrows, ncols;
  46. int row, col;
  47. int infd, outfd;
  48. int verbose;
  49. RASTER_MAP_TYPE data_type;
  50. int pat, patv;
  51. struct GModule *module;
  52. struct Option *input, *output, *pattern, *patval;
  53. struct Flag *flag1;
  54. G_gisinit(argv[0]);
  55. module = G_define_module();
  56. G_add_keyword(_("raster"));
  57. G_add_keyword(_("algebra"));
  58. module->description = _("Compares bit patterns with a raster map.");
  59. /* Define the different options */
  60. input = G_define_standard_option(G_OPT_R_INPUT);
  61. output = G_define_standard_option(G_OPT_R_OUTPUT);
  62. pattern = G_define_option();
  63. pattern->key = "pattern";
  64. pattern->type = TYPE_INTEGER;
  65. pattern->required = YES;
  66. pattern->description = _("Bit pattern position(s)");
  67. patval = G_define_option();
  68. patval->key = "patval";
  69. patval->type = TYPE_INTEGER;
  70. patval->required = YES;
  71. patval->description = _("Bit pattern value");
  72. /* Define the different flags */
  73. flag1 = G_define_flag();
  74. flag1->key = 'q';
  75. flag1->description = _("Quiet");
  76. if (G_parser(argc, argv))
  77. exit(EXIT_FAILURE);
  78. name = input->answer;
  79. result = output->answer;
  80. verbose = (!flag1->answer);
  81. pat = atoi(pattern->answer);
  82. patv = atoi(patval->answer);
  83. infd = Rast_open_old(name, "");
  84. /* determine the inputmap type (CELL/FCELL/DCELL) */
  85. data_type = Rast_get_map_type(infd);
  86. Rast_get_cellhd(name, "", &cellhd);
  87. /* Allocate input buffer */
  88. inrast = Rast_allocate_buf(data_type);
  89. /* Allocate output buffer, use input map data_type */
  90. nrows = Rast_window_rows();
  91. ncols = Rast_window_cols();
  92. outrast = Rast_allocate_buf(data_type);
  93. outfd = Rast_open_new(result, data_type);
  94. for (row = 0; row < nrows; row++) {
  95. CELL c;
  96. if (verbose)
  97. G_percent(row, nrows, 2);
  98. /* read input map */
  99. Rast_get_row(infd, inrast, row, data_type);
  100. /*process the data */
  101. for (col = 0; col < ncols; col++) {
  102. c = ((CELL *) inrast)[col];
  103. /*((CELL *) outrast)[col] = c; */
  104. if ((c & pat) == patv)
  105. ((CELL *) outrast)[col] = 1;
  106. else
  107. ((CELL *) outrast)[col] = 0;
  108. }
  109. Rast_put_row(outfd, outrast, data_type);
  110. }
  111. G_free(inrast);
  112. G_free(outrast);
  113. Rast_close(infd);
  114. Rast_close(outfd);
  115. return (EXIT_SUCCESS);
  116. }