raster.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. #include <grass/config.h>
  2. #include <errno.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <grass/gis.h>
  9. #include <grass/glocale.h>
  10. #include <grass/raster.h>
  11. #include "driver.h"
  12. extern const struct driver *PNG_Driver(void);
  13. extern const struct driver *PS_Driver(void);
  14. extern const struct driver *HTML_Driver(void);
  15. #ifdef USE_CAIRO
  16. extern const struct driver *Cairo_Driver(void);
  17. #endif
  18. static void init(void)
  19. {
  20. const char *fenc = getenv("GRASS_ENCODING");
  21. const char *font = getenv("GRASS_FONT");
  22. int t = R_screen_top();
  23. int b = R_screen_bot();
  24. int l = R_screen_left();
  25. int r = R_screen_rite();
  26. R_font(font ? font : "romans");
  27. if (fenc)
  28. R_charset(fenc);
  29. R_set_window(t, b, l, r);
  30. }
  31. int R_open_driver(void)
  32. {
  33. const char *p = getenv("GRASS_RENDER_IMMEDIATE");
  34. const struct driver *drv =
  35. (p && G_strcasecmp(p, "PS") == 0) ? PS_Driver() :
  36. (p && G_strcasecmp(p, "HTML") == 0) ? HTML_Driver() :
  37. #ifdef USE_CAIRO
  38. (p && G_strcasecmp(p, "cairo") == 0) ? Cairo_Driver() :
  39. #endif
  40. PNG_Driver();
  41. LIB_init(drv, 0, NULL);
  42. init();
  43. COM_Client_Open();
  44. return 0;
  45. }
  46. void R_flush(void)
  47. {
  48. COM_Respond();
  49. }
  50. void R_close_driver(void)
  51. {
  52. COM_Respond();
  53. COM_Client_Close();
  54. COM_Graph_close();
  55. }
  56. /*!
  57. * \brief screen left edge
  58. *
  59. * Returns the coordinate of the left edge of the screen.
  60. *
  61. * \param void
  62. * \return int
  63. */
  64. int R_screen_left(void)
  65. {
  66. int l;
  67. COM_Screen_left(&l);
  68. return l;
  69. }
  70. /*!
  71. * \brief screen right edge
  72. *
  73. * Returns the coordinate of the right edge of the screen.
  74. *
  75. * \param void
  76. * \return int
  77. */
  78. int R_screen_rite(void)
  79. {
  80. int r;
  81. COM_Screen_rite(&r);
  82. return r;
  83. }
  84. /*!
  85. * \brief bottom of screen
  86. *
  87. * Returns the coordinate of the bottom of the screen.
  88. *
  89. * \param void
  90. * \return int
  91. */
  92. int R_screen_bot(void)
  93. {
  94. int b;
  95. COM_Screen_bot(&b);
  96. return b;
  97. }
  98. /*!
  99. * \brief top of screen
  100. *
  101. * Returns the coordinate of the top of the screen.
  102. *
  103. * \param void
  104. * \return int
  105. */
  106. int R_screen_top(void)
  107. {
  108. int t;
  109. COM_Screen_top(&t);
  110. return t;
  111. }
  112. int R_get_num_colors(void)
  113. {
  114. int c;
  115. COM_Number_of_colors(&c);
  116. return c;
  117. }
  118. /*!
  119. * \brief select standard color
  120. *
  121. * Selects the
  122. * standard <b>color</b> to be used in subsequent draw commands. The
  123. * <b>color</b> value is best retrieved using <i>D_translate_color.</i>
  124. * See Display_Graphics_Library.
  125. *
  126. * \param index
  127. * \return int
  128. */
  129. void R_standard_color(int index)
  130. {
  131. COM_Standard_color(index);
  132. }
  133. /*!
  134. * \brief select color
  135. *
  136. * When in
  137. * float mode (see <i>R_color_table_float</i>), this call selects the color
  138. * most closely matched to the <b>red, grn</b>, and <b>blue</b> intensities
  139. * requested. These values must be in the range of 0-255.
  140. *
  141. * \param red
  142. * \param grn
  143. * \param blue
  144. * \return int
  145. */
  146. void R_RGB_color(unsigned char red, unsigned char grn, unsigned char blu)
  147. {
  148. COM_Color_RGB(red, grn, blu);
  149. }
  150. /*!
  151. * \brief change the width of line
  152. *
  153. * Changes the <b>width</b> of line to be used in subsequent draw commands.
  154. *
  155. * \param width
  156. * \return int
  157. */
  158. void R_line_width(int width)
  159. {
  160. COM_Line_width(width);
  161. }
  162. /*!
  163. * \brief erase screen
  164. *
  165. * Erases the entire screen to black.
  166. *
  167. * \param void
  168. * \return int
  169. */
  170. void R_erase(void)
  171. {
  172. COM_Erase();
  173. }
  174. /*!
  175. * \brief move current location
  176. *
  177. * Move the current location to the absolute screen coordinate <b>x,y.</b>
  178. * Nothing is drawn on the screen.
  179. *
  180. * \param x
  181. * \param y
  182. * \return int
  183. */
  184. void R_move_abs(int x, int y)
  185. {
  186. COM_Move_abs(x, y);
  187. }
  188. /*!
  189. * \brief move current location
  190. *
  191. * Shift the current screen location by the values in <b>dx</b> and <b>dy</b>:
  192. \code
  193. Newx = Oldx + dx;
  194. Newy = Oldy + dy;
  195. \endcode
  196. * Nothing is drawn on the screen.
  197. *
  198. * \param x dx
  199. * \param y dy
  200. * \return int
  201. */
  202. void R_move_rel(int x, int y)
  203. {
  204. COM_Move_rel(x, y);
  205. }
  206. /*!
  207. * \brief draw line
  208. *
  209. * Draw a line using the current color, selected via <i>R_color</i>, from the
  210. * current location to the location specified by <b>x,y.</b> The current location
  211. * is updated to <b>x,y.</b>
  212. *
  213. * \param x
  214. * \param y
  215. * \return int
  216. */
  217. void R_cont_abs(int x, int y)
  218. {
  219. COM_Cont_abs(x, y);
  220. }
  221. /*!
  222. * \brief draw line
  223. *
  224. * Draw a line using the
  225. * current color, selected via <i>R_color</i>, from the current location to
  226. * the relative location specified by <b>x</b> and <b>y.</b> The current
  227. * location is updated:
  228. \code
  229. Newx = Oldx + x;
  230. Newy = Oldy + y;
  231. \endcode
  232. *
  233. * \param x
  234. * \param y
  235. * \return int
  236. */
  237. void R_cont_rel(int x, int y)
  238. {
  239. COM_Cont_rel(x, y);
  240. }
  241. /*!
  242. * \brief draw a series of dots
  243. *
  244. * Pixels at the <b>num</b> absolute positions in the <b>x</b> and
  245. * <b>y</b> arrays are turned to the current color. The current location is
  246. * left updated to the position of the last dot.
  247. *
  248. * \param xarray x
  249. * \param yarray y
  250. * \param number
  251. * \return int
  252. */
  253. void R_polydots_abs(const int *xarray, const int *yarray, int number)
  254. {
  255. COM_Polydots_abs(xarray, yarray, number);
  256. }
  257. /*!
  258. * \brief draw a series of dots
  259. *
  260. * Pixels at the <b>number</b> relative positions in the <b>x</b> and
  261. * <b>y</b> arrays are turned to the current color. The first position is
  262. * relative to the starting current location; the succeeding positions are then
  263. * relative to the previous position. The current location is updated to the
  264. * position of the last dot.
  265. *
  266. * \param xarray x
  267. * \param yarray y
  268. * \param number
  269. * \return int
  270. */
  271. void R_polydots_rel(const int *xarray, const int *yarray, int number)
  272. {
  273. COM_Polydots_rel(xarray, yarray, number);
  274. }
  275. /*!
  276. * \brief draw an open polygon
  277. *
  278. * The <b>number</b> absolute positions in the <b>x</b> and <b>y</b>
  279. * arrays are used to generate a multisegment line (often curved). This line is
  280. * drawn with the current color. The current location is left updated to the
  281. * position of the last point.
  282. * <b>Note.</b> It is not assumed that the line is closed, i.e., no line is
  283. * drawn from the last point to the first point.
  284. *
  285. * \param xarray x
  286. * \param yarray y
  287. * \param number
  288. * \return int
  289. */
  290. void R_polyline_abs(const int *xarray, const int *yarray, int number)
  291. {
  292. COM_Polyline_abs(xarray, yarray, number);
  293. }
  294. /*!
  295. * \brief draw an open polygon
  296. *
  297. * The <b>number</b> relative positions in the <b>x</b> and <b>y</b>
  298. * arrays are used to generate a multisegment line (often curved). The first
  299. * position is relative to the starting current location; the succeeding
  300. * positions are then relative to the previous position. The current location is
  301. * updated to the position of the last point. This line is drawn with the current
  302. * color.
  303. * <b>Note.</b> No line is drawn between the last point and the first point.
  304. *
  305. * \param xarray x
  306. * \param yarray y
  307. * \param number
  308. * \return int
  309. */
  310. void R_polyline_rel(const int *xarray, const int *yarray, int number)
  311. {
  312. COM_Polyline_rel(xarray, yarray, number);
  313. }
  314. /*!
  315. * \brief draw a closed polygon
  316. *
  317. * The <b>number</b> absolute positions in the <b>x</b> and <b>y</b> arrays
  318. * outline a closed polygon which is filled with the current color. The current
  319. * location is undefined afterwards.
  320. *
  321. * \param xarray x
  322. * \param yarray y
  323. * \param number
  324. * \return int
  325. */
  326. void R_polygon_abs(const int *xarray, const int *yarray, int number)
  327. {
  328. COM_Polygon_abs(xarray, yarray, number);
  329. }
  330. /*!
  331. * \brief draw a closed polygon
  332. *
  333. * The <b>number</b> relative positions in the <b>x</b> and <b>y</b>
  334. * arrays outline a closed polygon which is filled with the current color. The
  335. * first position is relative to the starting current location; the succeeding
  336. * positions are then relative to the previous position. The current location is
  337. * undefined afterwards.
  338. *
  339. * \param xarray x
  340. * \param yarray y
  341. * \param number
  342. * \return int
  343. */
  344. void R_polygon_rel(const int *xarray, const int *yarray, int number)
  345. {
  346. COM_Polygon_rel(xarray, yarray, number);
  347. }
  348. /*!
  349. * \brief fill a box
  350. *
  351. * A box is drawn in the current color using the coordinates <b>x1,y1</b> and
  352. * <b>x2,y2</b> as opposite corners of the box. The current location is undefined
  353. * afterwards
  354. *
  355. * \param x1
  356. * \param y1
  357. * \param x2
  358. * \param y2
  359. * \return int
  360. */
  361. void R_box_abs(int x1, int y1, int x2, int y2)
  362. {
  363. COM_Box_abs(x1, y1, x2, y2);
  364. }
  365. /*!
  366. * \brief fill a box
  367. *
  368. * A box is drawn in the current color using the current location as one corner
  369. * and the current location plus <b>x</b> and <b>y</b> as the opposite corner
  370. * of the box. The current location is undefined afterwards.
  371. *
  372. * \param x
  373. * \param y
  374. * \return int
  375. */
  376. void R_box_rel(int x, int y)
  377. {
  378. COM_Box_rel(x, y);
  379. }
  380. /*!
  381. * \brief set text size
  382. *
  383. * Sets text pixel width and height to <b>width</b> and <b>height.</b>
  384. *
  385. * \param width
  386. * \param height
  387. * \return int
  388. */
  389. void R_text_size(int width, int height)
  390. {
  391. COM_Text_size(width, height);
  392. }
  393. void R_text_rotation(float rotation)
  394. {
  395. COM_Text_rotation(rotation);
  396. }
  397. /*!
  398. * \brief set text clipping frame
  399. *
  400. * Subsequent calls to <i>R_text</i> will have text strings
  401. * clipped to the screen frame defined by <b>top, bottom, left, right.</b>
  402. *
  403. * \param t top
  404. * \param b bottom
  405. * \param l left
  406. * \param r right
  407. * \return int
  408. */
  409. void R_set_window(int t, int b, int l, int r)
  410. {
  411. COM_Set_window(t, b, l, r);
  412. }
  413. /*!
  414. * \brief write text
  415. *
  416. * Writes <b>text</b> in the current color and font, at the current text
  417. * width and height, starting at the current screen location.
  418. *
  419. * \param sometext
  420. * \return int
  421. */
  422. void R_text(const char *text)
  423. {
  424. COM_Text(text);
  425. }
  426. /*!
  427. * \brief get text extents
  428. *
  429. * The extent of the area enclosing the <b>text</b>
  430. * is returned in the integer pointers <b>top, bottom, left</b>, and
  431. * <b>right.</b> No text is actually drawn. This is useful for capturing the
  432. * text extent so that the text location can be prepared with proper background
  433. * or border.
  434. *
  435. * \param sometext
  436. * \param t top
  437. * \param b bottom
  438. * \param l left
  439. * \param r right
  440. * \return int
  441. */
  442. void R_get_text_box(const char *text, int *t, int *b, int *l, int *r)
  443. {
  444. COM_Get_text_box(text, t, b, l, r);
  445. }
  446. /*!
  447. * \brief choose font
  448. *
  449. * Set current font to <b>font name</b>.
  450. *
  451. * \param name
  452. * \return int
  453. */
  454. void R_font(const char *name)
  455. {
  456. COM_Font_get(name);
  457. }
  458. void R_charset(const char *name)
  459. {
  460. COM_Font_init_charset(name);
  461. }
  462. void R_font_list(char ***list, int *count)
  463. {
  464. COM_Font_list(list, count);
  465. }
  466. void R_font_info(char ***list, int *count)
  467. {
  468. COM_Font_info(list, count);
  469. }
  470. void R_begin_scaled_raster(int mask, int src[2][2], int dst[2][2])
  471. {
  472. COM_begin_scaled_raster(mask, src, dst);
  473. }
  474. int R_scaled_raster(int n, int row,
  475. const unsigned char *red, const unsigned char *grn,
  476. const unsigned char *blu, const unsigned char *nul)
  477. {
  478. return COM_scaled_raster(n, row, red, grn, blu, nul);
  479. }
  480. void R_end_scaled_raster(void)
  481. {
  482. COM_end_scaled_raster();
  483. }
  484. void R_bitmap(int ncols, int nrows, int threshold, const unsigned char *buf)
  485. {
  486. COM_Bitmap(ncols, nrows, threshold, buf);
  487. }