smain.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <grass/bitmap.h>
  4. static int dump_map(struct BM *map);
  5. int main(int argc, char *argv[])
  6. {
  7. int SIZE;
  8. struct BM *map, *map2;
  9. int i, x, y;
  10. int dump;
  11. FILE *fp;
  12. if (argc < 2)
  13. SIZE = 11;
  14. else
  15. SIZE = atoi(argv[1]);
  16. if (NULL == getenv("NODUMP"))
  17. dump = 1;
  18. else
  19. dump = 0;
  20. map = BM_create_sparse(SIZE, SIZE);
  21. /* turn on bits in X pattern */
  22. for (i = 0; i < SIZE; i++) {
  23. BM_set(map, i, i, 1);
  24. BM_set(map, (SIZE - 1) - i, i, 1);
  25. }
  26. if (dump)
  27. dump_map(map);
  28. fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
  29. /*
  30. BM_dump_map_sparse (map);
  31. */
  32. fprintf(stdout, "\n\n");
  33. /* now invert it */
  34. for (y = 0; y < SIZE; y++)
  35. for (x = 0; x < SIZE; x++) {
  36. BM_set(map, x, y, !BM_get(map, x, y));
  37. #ifdef FOO
  38. /*DEBUG*/ if (y == 0)
  39. /*DEBUG*/ BM_dump_map_row_sparse(map, y);
  40. #endif
  41. }
  42. if (dump)
  43. dump_map(map);
  44. fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
  45. /*
  46. fprintf (stdout, "\n\n");
  47. BM_dump_map_sparse (map);
  48. */
  49. {
  50. fp = fopen("dumpfile", "w");
  51. if (0 > BM_file_write(fp, map)) {
  52. fprintf(stderr, "File_write failed\n");
  53. goto nowrite;
  54. }
  55. fclose(fp);
  56. fp = fopen("dumpfile", "r");
  57. map2 = BM_file_read(fp);
  58. fclose(fp);
  59. dump_map(map2);
  60. }
  61. BM_destroy(map2);
  62. nowrite:
  63. BM_destroy(map);
  64. return 0;
  65. }
  66. static int dump_map(struct BM *map)
  67. {
  68. int x, y;
  69. for (y = 0; y < map->rows; y++) {
  70. for (x = 0; x < map->cols; x++) {
  71. fprintf(stdout, "%c", BM_get(map, x, y) ? '#' : '.');
  72. }
  73. fprintf(stdout, "\n");
  74. }
  75. }