main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /****************************************************************************
  2. *
  3. * MODULE: xganim
  4. * AUTHOR(S): Bill Brown <brown gis.uiuc.edu> CERL (original contributor),
  5. * Markus Neteler <neteler itc.it>,
  6. * Roberto Flor <flor itc.it>,
  7. * Bernhard Reiter <bernhard intevation.de>,
  8. * Brad Douglas <rez touchofmadness.com>,
  9. * Glynn Clements <glynn gclements.plus.com>
  10. * PURPOSE: a tool for animating a series of GRASS raster files
  11. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <limits.h>
  23. #include <unistd.h>
  24. #include <wx/image.h>
  25. #include <wx/bitmap.h>
  26. #include <wx/event.h>
  27. extern "C" {
  28. #include <grass/gis.h>
  29. #include <grass/raster.h>
  30. #include <grass/spawn.h>
  31. #include <grass/glocale.h>
  32. }
  33. #include "gui.h"
  34. #define DEF_MAX 900
  35. #define DEF_MIN 600
  36. #define BORDER_W 2
  37. static char **gee_wildfiles(const char *wildarg, const char *element, int *num);
  38. static void parse_command(
  39. struct Option **viewopts,
  40. char *vfiles[MAXVIEWS][MAXIMAGES],
  41. int *numviews, int *numframes);
  42. struct Option *viewopts[MAXVIEWS];
  43. unsigned int nrows, ncols;
  44. char *vfiles[MAXVIEWS][MAXIMAGES];
  45. int numviews;
  46. int frames;
  47. int Top = 0, Left = 0;
  48. char frame[MAXIMAGES][4];
  49. int LabelPos[MAXVIEWS][2];
  50. float vscale, scale; /* resampling scale factors */
  51. int irows, icols, vrows, vcols;
  52. BEGIN_EVENT_TABLE(MyApp, wxApp)
  53. EVT_IDLE(MyApp::do_run)
  54. END_EVENT_TABLE()
  55. int main(int argc, char **argv)
  56. {
  57. int i;
  58. G_gisinit(argv[0]);
  59. for (i = 0; i < MAXVIEWS; i++) {
  60. char buf[BUFSIZ];
  61. viewopts[i] = G_define_option();
  62. sprintf(buf, "view%d", i + 1);
  63. viewopts[i]->key = G_store(buf);
  64. viewopts[i]->type = TYPE_STRING;
  65. viewopts[i]->required = (i ? NO : YES);
  66. viewopts[i]->multiple = YES;
  67. viewopts[i]->gisprompt = "old,cell,Raster";;
  68. sprintf(buf, _("Raster file(s) for View%d"), i + 1);
  69. viewopts[i]->description = G_store(buf);
  70. }
  71. if (G_parser(argc, argv))
  72. exit(EXIT_FAILURE);
  73. parse_command(viewopts, vfiles, &numviews, &frames);
  74. return wxEntry(argc, argv);
  75. }
  76. bool MyApp::OnInit()
  77. {
  78. int i, j;
  79. unsigned int *sdimp;
  80. int longdim;
  81. /* debug */
  82. if (G_verbose() > G_verbose_std()) {
  83. for (i = 0; i < numviews; i++) {
  84. fprintf(stderr, "\nVIEW %d: ", i + 1);
  85. for (j = 0; j < frames; j++) {
  86. fprintf(stderr, "%s ", vfiles[i][j]);
  87. }
  88. }
  89. }
  90. fprintf(stderr, "\n");
  91. vrows = Rast_window_rows();
  92. vcols = Rast_window_cols();
  93. nrows = vrows;
  94. ncols = vcols;
  95. /* short dimension */
  96. sdimp = nrows > ncols ? &ncols : &nrows;
  97. /* these proportions should work fine for 1 or 4 views, but for
  98. 2 views, want to double the narrow dim & for 3 views triple it */
  99. if (numviews == 2)
  100. *sdimp *= 2;
  101. else if (numviews == 3)
  102. *sdimp *= 3;
  103. longdim = nrows > ncols ? nrows : ncols;
  104. scale = 1.0;
  105. { /* find animation image size */
  106. int max, min;
  107. char *p;
  108. max = DEF_MAX;
  109. min = DEF_MIN;
  110. if ((p = getenv("XGANIM_SIZE")))
  111. max = min = atoi(p);
  112. if (longdim > max) /* scale down */
  113. scale = (float)max / longdim;
  114. else if (longdim < min) /* scale up */
  115. scale = (float)min / longdim;
  116. }
  117. vscale = scale;
  118. if (numviews == 4)
  119. vscale = scale / 2.;
  120. nrows = (unsigned int) (nrows * scale);
  121. ncols = (unsigned int) (ncols * scale);
  122. /* now nrows & ncols are the size of the combined - views image */
  123. vrows = (int) (vrows * vscale);
  124. vcols = (int) (vcols * vscale);
  125. /* now vrows & vcols are the size for each sub-image */
  126. /* add to nrows & ncols for borders */
  127. /* irows, icols used for vert/horizontal determination in loop below */
  128. irows = nrows;
  129. icols = ncols;
  130. nrows += (1 + (nrows / vrows)) * BORDER_W;
  131. ncols += (1 + (ncols / vcols)) * BORDER_W;
  132. gd.speed = 100;
  133. gd.direction = 1;
  134. gd.shownames = 1;
  135. mainwin = new MyFrame(wxString("GRASS Animate", wxConvISO8859_1), ncols, nrows, &gd);
  136. mainwin->Show();
  137. SetTopWindow(mainwin);
  138. for (j = 0; j < MAXIMAGES; j++)
  139. sprintf(frame[j], "%2d", j + 1);
  140. return true;
  141. }
  142. int MyApp::load_files(void)
  143. {
  144. DCELL *dcell;
  145. unsigned char *tr, *tg, *tb, *tset;
  146. int tsiz, coff;
  147. int rowoff, row, col, vxoff, vyoff;
  148. int cnt, ret, fd;
  149. int vnum;
  150. const char *name;
  151. struct Colors colors;
  152. dcell = Rast_allocate_d_buf();
  153. tsiz = Rast_window_cols();
  154. /* allocate memory */
  155. tr = (unsigned char *) G_malloc(tsiz * sizeof(char));
  156. tg = (unsigned char *) G_malloc(tsiz * sizeof(char));
  157. tb = (unsigned char *) G_malloc(tsiz * sizeof(char));
  158. tset = (unsigned char *) G_malloc(tsiz * sizeof(char));
  159. wxImage img(ncols, nrows);
  160. for (cnt = 0; cnt < frames; cnt++) {
  161. if (cnt > MAXIMAGES) {
  162. cnt--;
  163. break;
  164. }
  165. for (vnum = 0; vnum < numviews; vnum++) {
  166. if (icols == vcols) {
  167. vxoff = BORDER_W;
  168. vyoff = (irows == vrows) ? BORDER_W :
  169. BORDER_W + vnum * (BORDER_W + vrows);
  170. }
  171. else if (irows == vrows) {
  172. vxoff = (icols == vcols) ? BORDER_W :
  173. BORDER_W + vnum * (BORDER_W + vcols);
  174. vyoff = BORDER_W;
  175. }
  176. else { /* 4 views */
  177. /* assumes we want :
  178. view1 view2
  179. view3 view4
  180. */
  181. vxoff = vnum % 2 ? BORDER_W : vcols + 2 * BORDER_W;
  182. vyoff = vnum > 1 ? vrows + 2 * BORDER_W : BORDER_W;
  183. }
  184. if (!cnt) {
  185. LabelPos[vnum][0] = vxoff;
  186. LabelPos[vnum][1] = vyoff + vrows - 1;
  187. }
  188. name = vfiles[vnum][cnt];
  189. G_message(_("Reading file [%s]..."), name);
  190. fd = Rast_open_old(name, "");
  191. if (fd < 0)
  192. G_fatal_error(_("Unable to open raster map <%s>"), name);
  193. /*
  194. strcpy(title[cnt],G_get_cell_title(name, mapset));
  195. */
  196. ret = Rast_read_colors(name, "", &colors);
  197. if (ret < 0)
  198. G_fatal_error(_("Unable to read color file"));
  199. for (row = 0; row < vrows; row++) {
  200. Rast_get_d_row(fd, dcell, (int)(row / vscale));
  201. rowoff = (vyoff + row) * ncols;
  202. Rast_lookup_d_colors(dcell, tr, tg, tb, tset, tsiz, &colors);
  203. for (col = 0; col < vcols; col++) {
  204. coff = (int)(col / vscale);
  205. if (!tset[coff])
  206. img.SetRGB(vxoff + col, vyoff + row, 255, 255, 255);
  207. else
  208. img.SetRGB(vxoff + col, vyoff + row, tr[coff], tg[coff], tb[coff]);
  209. }
  210. }
  211. Rast_close(fd);
  212. }
  213. wxBitmap *bmp = new wxBitmap(img);
  214. pic_array[cnt] = bmp;
  215. mainwin->canvas->draw_image(bmp);
  216. mainwin->change_label(frame[cnt]);
  217. }
  218. G_free(dcell);
  219. G_free(tr);
  220. G_free(tg);
  221. G_free(tb);
  222. G_free(tset);
  223. return cnt;
  224. }
  225. /* ###################################################### */
  226. void MyApp::do_run(wxIdleEvent &ev)
  227. {
  228. static int first = 1;
  229. struct gui_data *cd = &gd;
  230. int i, cnt;
  231. if (first) {
  232. first = 0;
  233. cnt = load_files();
  234. cd->curframe = cd->direction > 0 ? 0 : cnt - 1;
  235. cd->prevframe = cd->curframe;
  236. cd->step = cd->stop = 0;
  237. cd->loop = cd->swing = 0;
  238. cd->nframes = cnt;
  239. }
  240. if (cd->rewind) {
  241. cd->rewind = 0;
  242. cd->curframe = 0;
  243. cd->direction = 1;
  244. cd->step = 1;
  245. }
  246. if (cd->swing) {
  247. if (cd->curframe == cd->nframes || cd->curframe < 0) {
  248. cd->direction = -cd->direction;
  249. cd->curframe += cd->direction;
  250. }
  251. }
  252. else if (cd->loop) {
  253. if (cd->curframe == cd->nframes)
  254. cd->curframe = 0;
  255. else if (cd->curframe < 0)
  256. cd->curframe = cd->nframes - 1;
  257. }
  258. else if (cd->curframe == cd->nframes || cd->curframe < 0)
  259. cd->stop = 1;
  260. if (cd->stop && !cd->step)
  261. return;
  262. if (cd->curframe < cd->nframes && cd->curframe >= 0) {
  263. /* little pause */
  264. {
  265. float tf;
  266. for (tf = 0.0; tf < cd->speed; tf += .01) ;
  267. }
  268. mainwin->canvas->draw_image(pic_array[cd->curframe]);
  269. /* draw labels */
  270. for (i = 0; i < numviews; i++) {
  271. mainwin->canvas->draw_text(
  272. cd->shownames,
  273. LabelPos[i][0] + 5, LabelPos[i][1] - 5,
  274. vfiles[i][cd->curframe]);
  275. }
  276. mainwin->change_label(frame[cd->curframe]);
  277. cd->prevframe = cd->curframe;
  278. }
  279. cd->curframe += cd->direction;
  280. if (cd->step) {
  281. cd->step = 0;
  282. cd->stop = 1;
  283. }
  284. }
  285. /* ###################################################### */
  286. static void mlist(const char *element, const char *wildarg, const char *outfile)
  287. {
  288. int n;
  289. const char *mapset;
  290. for (n = 0; (mapset = G__mapset_name(n)); n++) {
  291. char type_arg[GNAME_MAX];
  292. char pattern_arg[GNAME_MAX];
  293. char mapset_arg[GMAPSET_MAX];
  294. if (strcmp(mapset, ".") == 0)
  295. mapset = G_mapset();
  296. sprintf(type_arg, "type=%s", element);
  297. sprintf(pattern_arg, "pattern=%s", wildarg);
  298. sprintf(mapset_arg, "mapset=%s", mapset);
  299. G_spawn_ex("g.mlist", "g.mlist",
  300. type_arg, pattern_arg, mapset_arg,
  301. SF_REDIRECT_FILE, SF_STDOUT, SF_MODE_APPEND, outfile,
  302. NULL);
  303. }
  304. }
  305. static char **parse(const char *filename, int *num)
  306. {
  307. char buf[GNAME_MAX];
  308. char **files = NULL;
  309. int max_files = 0;
  310. int num_files = 0;
  311. FILE *fp;
  312. fp = fopen(filename, "r");
  313. if (!fp)
  314. G_fatal_error(_("Error reading wildcard"));
  315. while (fgets(buf, sizeof(buf), fp)) {
  316. char *p = strchr(buf, '\n');
  317. if (p)
  318. *p = '\0';
  319. if (!*buf)
  320. continue;
  321. if (num_files >= max_files) {
  322. max_files += 50;
  323. files = (char **) G_realloc((void *) files,
  324. max_files * sizeof(char *));
  325. }
  326. files[num_files++] = G_store(buf);
  327. }
  328. fclose(fp);
  329. *num = num_files;
  330. return files;
  331. }
  332. static char **gee_wildfiles(const char *wildarg, const char *element, int *num)
  333. {
  334. char *tfile;
  335. char **files;
  336. tfile = G_tempfile();
  337. mlist(element, wildarg, tfile);
  338. files = parse(tfile, num);
  339. remove(tfile);
  340. G_free(tfile);
  341. return files;
  342. }
  343. static void parse_command(struct Option **viewopts,
  344. char *vfiles[MAXVIEWS][MAXIMAGES],
  345. int *numviews, int *numframes)
  346. {
  347. int i, j, k;
  348. *numviews = *numframes = 0;
  349. for (i = 0; i < MAXVIEWS; i++) {
  350. if (viewopts[i]->answers) {
  351. int numi, wildnum;
  352. (*numviews)++;
  353. for (j = 0, numi = 0; viewopts[i]->answers[j]; j++) {
  354. if ((NULL != strchr(viewopts[i]->answers[j], '*')) ||
  355. (NULL != strchr(viewopts[i]->answers[j], '?')) ||
  356. (NULL != strchr(viewopts[i]->answers[j], '['))) {
  357. char **wildfiles = gee_wildfiles(viewopts[i]->answers[j],
  358. "rast", &wildnum);
  359. for (k = 0; k < wildnum; k++)
  360. vfiles[i][numi++] = wildfiles[k];
  361. }
  362. else
  363. vfiles[i][numi++] = G_store(viewopts[i]->answers[j]);
  364. }
  365. /* keep track of smallest number of frames */
  366. *numframes =
  367. *numframes ? *numframes > numi ? numi : *numframes : numi;
  368. }
  369. }
  370. }
  371. /********************************************************************/
  372. IMPLEMENT_APP_NO_MAIN(MyApp)