memory.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "local_proto.h"
  2. int open_map(MAPS * rast)
  3. {
  4. int row, col;
  5. char *mapset;
  6. struct Cell_head cellhd;
  7. void *tmp_buf;
  8. mapset = (char *)G_find_raster2(rast->elevname, "");
  9. if (mapset == NULL)
  10. G_fatal_error(_("Raster map <%s> not found"), rast->elevname);
  11. rast->fd = Rast_open_old(rast->elevname, mapset);
  12. Rast_get_cellhd(rast->elevname, mapset, &cellhd);
  13. rast->raster_type = Rast_map_type(rast->elevname, mapset);
  14. if (window.ew_res < cellhd.ew_res || window.ns_res < cellhd.ns_res)
  15. G_warning(_("Region resolution shoudn't be lesser than map %s resolution. Run g.region raster=%s to set proper resolution"),
  16. rast->elevname, rast->elevname);
  17. tmp_buf = Rast_allocate_buf(rast->raster_type);
  18. rast->elev = (FCELL **) G_malloc((row_buffer_size + 1) * sizeof(FCELL *));
  19. for (row = 0; row < row_buffer_size + 1; ++row) {
  20. rast->elev[row] = Rast_allocate_buf(FCELL_TYPE);
  21. Rast_get_row(rast->fd, tmp_buf, row, rast->raster_type);
  22. for (col = 0; col < ncols; ++col)
  23. get_cell(col, rast->elev[row], tmp_buf, rast->raster_type);
  24. } /* end elev */
  25. G_free(tmp_buf);
  26. return 0;
  27. }
  28. int get_cell(int col, float *buf_row, void *buf, RASTER_MAP_TYPE raster_type)
  29. {
  30. switch (raster_type) {
  31. case CELL_TYPE:
  32. if (Rast_is_null_value(&((CELL *) buf)[col], CELL_TYPE))
  33. Rast_set_f_null_value(&buf_row[col], 1);
  34. else
  35. buf_row[col] = (FCELL) ((CELL *) buf)[col];
  36. break;
  37. case FCELL_TYPE:
  38. if (Rast_is_null_value(&((FCELL *) buf)[col], FCELL_TYPE))
  39. Rast_set_f_null_value(&buf_row[col], 1);
  40. else
  41. buf_row[col] = (FCELL) ((FCELL *) buf)[col];
  42. break;
  43. case DCELL_TYPE:
  44. if (Rast_is_null_value(&((DCELL *) buf)[col], DCELL_TYPE))
  45. Rast_set_f_null_value(&buf_row[col], 1);
  46. else
  47. buf_row[col] = (FCELL) ((DCELL *) buf)[col];
  48. break;
  49. }
  50. return 0;
  51. }
  52. int shift_buffers(int row)
  53. {
  54. int i;
  55. int col;
  56. void *tmp_buf;
  57. FCELL *tmp_elev_buf;
  58. tmp_buf = Rast_allocate_buf(elevation.raster_type);
  59. tmp_elev_buf = elevation.elev[0];
  60. for (i = 1; i < row_buffer_size + 1; ++i)
  61. elevation.elev[i - 1] = elevation.elev[i];
  62. elevation.elev[row_buffer_size] = tmp_elev_buf;
  63. Rast_get_row(elevation.fd, tmp_buf, row + row_radius_size + 1,
  64. elevation.raster_type);
  65. for (col = 0; col < ncols; ++col)
  66. get_cell(col, elevation.elev[row_buffer_size], tmp_buf,
  67. elevation.raster_type);
  68. G_free(tmp_buf);
  69. return 0;
  70. }
  71. int free_map(FCELL ** map, int n)
  72. {
  73. int i;
  74. for (i = 0; i < n; ++i)
  75. G_free(map[i]);
  76. G_free(map);
  77. return 0;
  78. }
  79. int write_form_cat_colors(char *raster, CATCOLORS * ccolors)
  80. {
  81. struct Colors colors;
  82. struct Categories cats;
  83. int i;
  84. Rast_init_colors(&colors);
  85. for (i = 1; i < CNT; ++i)
  86. Rast_add_color_rule(&ccolors[i].cat, ccolors[i].r, ccolors[i].g,
  87. ccolors[i].b, &ccolors[i].cat, ccolors[i].r,
  88. ccolors[i].g, ccolors[i].b, &colors, CELL_TYPE);
  89. Rast_write_colors(raster, G_mapset(), &colors);
  90. Rast_free_colors(&colors);
  91. Rast_init_cats("Forms", &cats);
  92. for (i = 1; i < CNT; ++i)
  93. Rast_set_cat(&ccolors[i].cat, &ccolors[i].cat, ccolors[i].label,
  94. &cats, CELL_TYPE);
  95. Rast_write_cats(raster, &cats);
  96. Rast_free_cats(&cats);
  97. return 0;
  98. }
  99. int write_contrast_colors(char *raster)
  100. {
  101. struct Colors colors;
  102. /* struct Categories cats; */
  103. FCOLORS fcolors[9] = { /* colors for positive openness */
  104. {-2500, 0, 0, 50, NULL},
  105. {-100, 0, 0, 56, NULL},
  106. {-15, 0, 56, 128, NULL},
  107. {-3, 0, 128, 255, NULL},
  108. {0, 255, 255, 255, NULL},
  109. {3, 255, 128, 0, NULL},
  110. {15, 128, 56, 0, NULL},
  111. {100, 56, 0, 0, NULL},
  112. {2500, 50, 0, 0, NULL}
  113. };
  114. int i;
  115. Rast_init_colors(&colors);
  116. for (i = 0; i < 8; ++i)
  117. Rast_add_d_color_rule(&fcolors[i].cat, fcolors[i].r, fcolors[i].g,
  118. fcolors[i].b, &fcolors[i + 1].cat,
  119. fcolors[i + 1].r, fcolors[i + 1].g,
  120. fcolors[i + 1].b, &colors);
  121. Rast_write_colors(raster, G_mapset(), &colors);
  122. Rast_free_colors(&colors);
  123. /*
  124. Rast_init_cats("Forms", &cats);
  125. for(i=0;i<8;++i)
  126. Rast_set_cat(&ccolors[i].cat, &ccolors[i].cat, ccolors[i].label, &cats, CELL_TYPE);
  127. Rast_write_cats(raster, &cats);
  128. Rast_free_cats(&cats);
  129. */
  130. return 0;
  131. }