get_row.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /*!
  2. \file lib/raster/get_row.c
  3. \brief Raster library - Get raster row
  4. (C) 2003-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/types.h>
  12. #include <errno.h>
  13. #include <grass/config.h>
  14. #include <grass/raster.h>
  15. #include <grass/glocale.h>
  16. #include "R.h"
  17. static void embed_nulls(int, void *, int, RASTER_MAP_TYPE, int, int);
  18. static int compute_window_row(int fd, int row, int *cellRow)
  19. {
  20. struct fileinfo *fcb = &R__.fileinfo[fd];
  21. double f;
  22. int r;
  23. /* check for row in window */
  24. if (row < 0 || row >= R__.rd_window.rows) {
  25. G_fatal_error(_("Reading raster map <%s@%s> request for row %d is outside region"),
  26. fcb->name, fcb->mapset, row);
  27. }
  28. /* convert window row to cell file row */
  29. f = row * fcb->C1 + fcb->C2;
  30. r = (int)f;
  31. if (f < r) /* adjust for rounding up of negatives */
  32. r--;
  33. if (r < 0 || r >= fcb->cellhd.rows)
  34. return 0;
  35. *cellRow = r;
  36. return 1;
  37. }
  38. static void do_reclass_int(int fd, void *cell, int null_is_zero)
  39. {
  40. struct fileinfo *fcb = &R__.fileinfo[fd];
  41. CELL *c = cell;
  42. CELL *reclass_table = fcb->reclass.table;
  43. CELL min = fcb->reclass.min;
  44. CELL max = fcb->reclass.max;
  45. int i;
  46. for (i = 0; i < R__.rd_window.cols; i++) {
  47. if (Rast_is_c_null_value(&c[i])) {
  48. if (null_is_zero)
  49. c[i] = 0;
  50. continue;
  51. }
  52. if (c[i] < min || c[i] > max) {
  53. if (null_is_zero)
  54. c[i] = 0;
  55. else
  56. Rast_set_c_null_value(&c[i], 1);
  57. continue;
  58. }
  59. c[i] = reclass_table[c[i] - min];
  60. if (null_is_zero && Rast_is_c_null_value(&c[i]))
  61. c[i] = 0;
  62. }
  63. }
  64. static void read_data_fp_compressed(int fd, int row, unsigned char *data_buf,
  65. int *nbytes)
  66. {
  67. struct fileinfo *fcb = &R__.fileinfo[fd];
  68. off_t t1 = fcb->row_ptr[row];
  69. off_t t2 = fcb->row_ptr[row + 1];
  70. size_t readamount = t2 - t1;
  71. size_t bufsize = fcb->cellhd.cols * fcb->nbytes;
  72. int ret;
  73. if (lseek(fcb->data_fd, t1, SEEK_SET) < 0)
  74. G_fatal_error(_("Error seeking fp raster data file for row %d of <%s>: %s"),
  75. row, fcb->name, strerror(errno));
  76. *nbytes = fcb->nbytes;
  77. ret = G_read_compressed(fcb->data_fd, readamount, data_buf,
  78. bufsize, fcb->cellhd.compressed);
  79. if (ret <= 0)
  80. G_fatal_error(_("Error uncompressing fp raster data for row %d of <%s>: error code %d"),
  81. row, fcb->name, ret);
  82. }
  83. static void rle_decompress(unsigned char *dst, const unsigned char *src,
  84. int nbytes, int size)
  85. {
  86. int pairs = size / (nbytes + 1);
  87. int i;
  88. for (i = 0; i < pairs; i++) {
  89. int repeat = *src++;
  90. int j;
  91. for (j = 0; j < repeat; j++) {
  92. memcpy(dst, src, nbytes);
  93. dst += nbytes;
  94. }
  95. src += nbytes;
  96. }
  97. }
  98. static void read_data_compressed(int fd, int row, unsigned char *data_buf,
  99. int *nbytes)
  100. {
  101. struct fileinfo *fcb = &R__.fileinfo[fd];
  102. off_t t1 = fcb->row_ptr[row];
  103. off_t t2 = fcb->row_ptr[row + 1];
  104. ssize_t readamount = t2 - t1;
  105. size_t bufsize;
  106. unsigned char *cmp, *cmp2;
  107. int n;
  108. if (lseek(fcb->data_fd, t1, SEEK_SET) < 0)
  109. G_fatal_error(_("Error seeking raster data file for row %d of <%s>: %s"),
  110. row, fcb->name, strerror(errno));
  111. cmp = G_malloc(readamount);
  112. if (read(fcb->data_fd, cmp, readamount) != readamount) {
  113. G_free(cmp);
  114. G_fatal_error(_("Error reading raster data for row %d of <%s>: %s"),
  115. row, fcb->name, strerror(errno));
  116. }
  117. /* save cmp for free below */
  118. cmp2 = cmp;
  119. /* Now decompress the row */
  120. if (fcb->cellhd.compressed > 0) {
  121. /* one byte is nbyte count */
  122. n = *nbytes = *cmp++;
  123. readamount--;
  124. }
  125. else
  126. /* pre 3.0 compression */
  127. n = *nbytes = fcb->nbytes;
  128. bufsize = n * fcb->cellhd.cols;
  129. if (fcb->cellhd.compressed < 0 || readamount < bufsize) {
  130. if (fcb->cellhd.compressed == 1)
  131. rle_decompress(data_buf, cmp, n, readamount);
  132. else {
  133. if (G_expand(cmp, readamount, data_buf, bufsize,
  134. fcb->cellhd.compressed) != bufsize)
  135. G_fatal_error(_("Error uncompressing raster data for row %d of <%s>"),
  136. row, fcb->name);
  137. }
  138. }
  139. else
  140. memcpy(data_buf, cmp, readamount);
  141. G_free(cmp2);
  142. }
  143. static void read_data_uncompressed(int fd, int row, unsigned char *data_buf,
  144. int *nbytes)
  145. {
  146. struct fileinfo *fcb = &R__.fileinfo[fd];
  147. ssize_t bufsize = fcb->cellhd.cols * fcb->nbytes;
  148. *nbytes = fcb->nbytes;
  149. if (lseek(fcb->data_fd, (off_t) row * bufsize, SEEK_SET) == -1)
  150. G_fatal_error(_("Error reading raster data for row %d of <%s>"),
  151. row, fcb->name);
  152. if (read(fcb->data_fd, data_buf, bufsize) != bufsize)
  153. G_fatal_error(_("Error reading raster data for row %d of <%s>"),
  154. row, fcb->name);
  155. }
  156. #ifdef HAVE_GDAL
  157. static void read_data_gdal(int fd, int row, unsigned char *data_buf,
  158. int *nbytes)
  159. {
  160. struct fileinfo *fcb = &R__.fileinfo[fd];
  161. unsigned char *buf;
  162. CPLErr err;
  163. *nbytes = fcb->nbytes;
  164. if (fcb->gdal->vflip)
  165. row = fcb->cellhd.rows - 1 - row;
  166. buf = fcb->gdal->hflip ? G_malloc(fcb->cellhd.cols * fcb->cur_nbytes)
  167. : data_buf;
  168. err =
  169. Rast_gdal_raster_IO(fcb->gdal->band, GF_Read, 0, row,
  170. fcb->cellhd.cols, 1, buf, fcb->cellhd.cols, 1,
  171. fcb->gdal->type, 0, 0);
  172. if (fcb->gdal->hflip) {
  173. int i;
  174. for (i = 0; i < fcb->cellhd.cols; i++)
  175. memcpy(data_buf + i * fcb->cur_nbytes,
  176. buf + (fcb->cellhd.cols - 1 - i) * fcb->cur_nbytes,
  177. fcb->cur_nbytes);
  178. G_free(buf);
  179. }
  180. if (err != CE_None)
  181. G_fatal_error(_("Error reading raster data via GDAL for row %d of <%s>"),
  182. row, fcb->name);
  183. }
  184. #endif
  185. static void read_data(int fd, int row, unsigned char *data_buf, int *nbytes)
  186. {
  187. struct fileinfo *fcb = &R__.fileinfo[fd];
  188. #ifdef HAVE_GDAL
  189. if (fcb->gdal) {
  190. read_data_gdal(fd, row, data_buf, nbytes);
  191. return;
  192. }
  193. #endif
  194. if (!fcb->cellhd.compressed)
  195. read_data_uncompressed(fd, row, data_buf, nbytes);
  196. else if (fcb->map_type == CELL_TYPE)
  197. read_data_compressed(fd, row, data_buf, nbytes);
  198. else
  199. read_data_fp_compressed(fd, row, data_buf, nbytes);
  200. }
  201. /* copy cell file data to user buffer translated by window column mapping */
  202. static void cell_values_int(int fd, const unsigned char *data,
  203. const COLUMN_MAPPING * cmap, int nbytes,
  204. void *cell, int n)
  205. {
  206. CELL *c = cell;
  207. COLUMN_MAPPING cmapold = 0;
  208. int big = (size_t) nbytes >= sizeof(CELL);
  209. int i;
  210. for (i = 0; i < n; i++) {
  211. const unsigned char *d;
  212. int neg;
  213. CELL v;
  214. int j;
  215. if (!cmap[i]) {
  216. c[i] = 0;
  217. continue;
  218. }
  219. if (cmap[i] == cmapold) {
  220. c[i] = c[i - 1];
  221. continue;
  222. }
  223. d = data + (cmap[i] - 1) * nbytes;
  224. if (big && (*d & 0x80)) {
  225. neg = 1;
  226. v = *d++ & 0x7f;
  227. }
  228. else {
  229. neg = 0;
  230. v = *d++;
  231. }
  232. for (j = 1; j < nbytes; j++)
  233. v = (v << 8) + *d++;
  234. c[i] = neg ? -v : v;
  235. cmapold = cmap[i];
  236. }
  237. }
  238. static void cell_values_float(int fd, const unsigned char *data,
  239. const COLUMN_MAPPING * cmap, int nbytes,
  240. void *cell, int n)
  241. {
  242. struct fileinfo *fcb = &R__.fileinfo[fd];
  243. const float *work_buf = (const float *) fcb->data;
  244. FCELL *c = cell;
  245. int i;
  246. for (i = 0; i < n; i++) {
  247. if (!cmap[i]) {
  248. c[i] = 0;
  249. continue;
  250. }
  251. G_xdr_get_float(&c[i], &work_buf[cmap[i]-1]);
  252. }
  253. }
  254. static void cell_values_double(int fd, const unsigned char *data,
  255. const COLUMN_MAPPING * cmap, int nbytes,
  256. void *cell, int n)
  257. {
  258. struct fileinfo *fcb = &R__.fileinfo[fd];
  259. const double *work_buf = (const double *) fcb->data;
  260. DCELL *c = cell;
  261. int i;
  262. for (i = 0; i < n; i++) {
  263. if (!cmap[i]) {
  264. c[i] = 0;
  265. continue;
  266. }
  267. G_xdr_get_double(&c[i], &work_buf[cmap[i]-1]);
  268. }
  269. }
  270. #ifdef HAVE_GDAL
  271. static void gdal_values_int(int fd, const unsigned char *data,
  272. const COLUMN_MAPPING * cmap, int nbytes,
  273. CELL * cell, int n)
  274. {
  275. struct fileinfo *fcb = &R__.fileinfo[fd];
  276. const unsigned char *d;
  277. COLUMN_MAPPING cmapold = 0;
  278. int i;
  279. for (i = 0; i < n; i++) {
  280. if (!cmap[i]) {
  281. cell[i] = 0;
  282. continue;
  283. }
  284. if (cmap[i] == cmapold) {
  285. cell[i] = cell[i - 1];
  286. continue;
  287. }
  288. d = data + (cmap[i] - 1) * nbytes;
  289. switch (fcb->gdal->type) {
  290. case GDT_Byte:
  291. cell[i] = *(GByte *) d;
  292. break;
  293. case GDT_Int16:
  294. cell[i] = *(GInt16 *) d;
  295. break;
  296. case GDT_UInt16:
  297. cell[i] = *(GUInt16 *) d;
  298. break;
  299. case GDT_Int32:
  300. cell[i] = *(GInt32 *) d;
  301. break;
  302. case GDT_UInt32:
  303. cell[i] = *(GUInt32 *) d;
  304. break;
  305. default:
  306. /* shouldn't happen */
  307. Rast_set_c_null_value(&cell[i], 1);
  308. break;
  309. }
  310. cmapold = cmap[i];
  311. }
  312. }
  313. static void gdal_values_float(int fd, const float *data,
  314. const COLUMN_MAPPING * cmap, int nbytes,
  315. FCELL * cell, int n)
  316. {
  317. COLUMN_MAPPING cmapold = 0;
  318. int i;
  319. for (i = 0; i < n; i++) {
  320. if (!cmap[i]) {
  321. cell[i] = 0;
  322. continue;
  323. }
  324. if (cmap[i] == cmapold) {
  325. cell[i] = cell[i - 1];
  326. continue;
  327. }
  328. cell[i] = data[cmap[i] - 1];
  329. cmapold = cmap[i];
  330. }
  331. }
  332. static void gdal_values_double(int fd, const double *data,
  333. const COLUMN_MAPPING * cmap, int nbytes,
  334. DCELL * cell, int n)
  335. {
  336. COLUMN_MAPPING cmapold = 0;
  337. int i;
  338. for (i = 0; i < n; i++) {
  339. if (!cmap[i]) {
  340. cell[i] = 0;
  341. continue;
  342. }
  343. if (cmap[i] == cmapold) {
  344. cell[i] = cell[i - 1];
  345. continue;
  346. }
  347. cell[i] = data[cmap[i] - 1];
  348. cmapold = cmap[i];
  349. }
  350. }
  351. #endif
  352. /* transfer_to_cell_XY takes bytes from fcb->data, converts these bytes with
  353. the appropriate procedure (e.g. XDR or byte reordering) into type X
  354. values which are put into array work_buf.
  355. finally the values in work_buf are converted into
  356. type Y and put into 'cell'.
  357. if type X == type Y the intermediate step of storing the values in
  358. work_buf might be omitted. check the appropriate function for XY to
  359. determine the procedure of conversion.
  360. */
  361. static void transfer_to_cell_XX(int fd, void *cell)
  362. {
  363. static void (*cell_values_type[3]) () = {
  364. cell_values_int, cell_values_float, cell_values_double};
  365. #ifdef HAVE_GDAL
  366. static void (*gdal_values_type[3]) () = {
  367. gdal_values_int, gdal_values_float, gdal_values_double};
  368. #endif
  369. struct fileinfo *fcb = &R__.fileinfo[fd];
  370. #ifdef HAVE_GDAL
  371. if (fcb->gdal)
  372. (gdal_values_type[fcb->map_type]) (fd, fcb->data, fcb->col_map,
  373. fcb->cur_nbytes, cell,
  374. R__.rd_window.cols);
  375. else
  376. #endif
  377. (cell_values_type[fcb->map_type]) (fd, fcb->data, fcb->col_map,
  378. fcb->cur_nbytes, cell,
  379. R__.rd_window.cols);
  380. }
  381. static void transfer_to_cell_fi(int fd, void *cell)
  382. {
  383. struct fileinfo *fcb = &R__.fileinfo[fd];
  384. FCELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(FCELL));
  385. int i;
  386. transfer_to_cell_XX(fd, work_buf);
  387. for (i = 0; i < R__.rd_window.cols; i++)
  388. ((CELL *) cell)[i] = (fcb->col_map[i] == 0)
  389. ? 0 : Rast_quant_get_cell_value(&fcb->quant, work_buf[i]);
  390. G_free(work_buf);
  391. }
  392. static void transfer_to_cell_di(int fd, void *cell)
  393. {
  394. struct fileinfo *fcb = &R__.fileinfo[fd];
  395. DCELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(DCELL));
  396. int i;
  397. transfer_to_cell_XX(fd, work_buf);
  398. for (i = 0; i < R__.rd_window.cols; i++)
  399. ((CELL *) cell)[i] = (fcb->col_map[i] == 0)
  400. ? 0 : Rast_quant_get_cell_value(&fcb->quant, work_buf[i]);
  401. G_free(work_buf);
  402. }
  403. static void transfer_to_cell_if(int fd, void *cell)
  404. {
  405. CELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
  406. int i;
  407. transfer_to_cell_XX(fd, work_buf);
  408. for (i = 0; i < R__.rd_window.cols; i++)
  409. ((FCELL *) cell)[i] = work_buf[i];
  410. G_free(work_buf);
  411. }
  412. static void transfer_to_cell_df(int fd, void *cell)
  413. {
  414. DCELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(DCELL));
  415. int i;
  416. transfer_to_cell_XX(fd, work_buf);
  417. for (i = 0; i < R__.rd_window.cols; i++)
  418. ((FCELL *) cell)[i] = work_buf[i];
  419. G_free(work_buf);
  420. }
  421. static void transfer_to_cell_id(int fd, void *cell)
  422. {
  423. CELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
  424. int i;
  425. transfer_to_cell_XX(fd, work_buf);
  426. for (i = 0; i < R__.rd_window.cols; i++)
  427. ((DCELL *) cell)[i] = work_buf[i];
  428. G_free(work_buf);
  429. }
  430. static void transfer_to_cell_fd(int fd, void *cell)
  431. {
  432. FCELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(FCELL));
  433. int i;
  434. transfer_to_cell_XX(fd, work_buf);
  435. for (i = 0; i < R__.rd_window.cols; i++)
  436. ((DCELL *) cell)[i] = work_buf[i];
  437. G_free(work_buf);
  438. }
  439. /*
  440. * works for all map types and doesn't consider
  441. * null row corresponding to the requested row
  442. */
  443. static int get_map_row_nomask(int fd, void *rast, int row,
  444. RASTER_MAP_TYPE data_type)
  445. {
  446. static void (*transfer_to_cell_FtypeOtype[3][3]) () = {
  447. {
  448. transfer_to_cell_XX, transfer_to_cell_if, transfer_to_cell_id}, {
  449. transfer_to_cell_fi, transfer_to_cell_XX, transfer_to_cell_fd}, {
  450. transfer_to_cell_di, transfer_to_cell_df, transfer_to_cell_XX}
  451. };
  452. struct fileinfo *fcb = &R__.fileinfo[fd];
  453. int r;
  454. int row_status;
  455. /* is this the best place to read a vrt row, or
  456. * call Rast_get_vrt_row() earlier ? */
  457. if (fcb->vrt)
  458. return Rast_get_vrt_row(fd, rast, row, data_type);
  459. row_status = compute_window_row(fd, row, &r);
  460. if (!row_status) {
  461. fcb->cur_row = -1;
  462. Rast_zero_input_buf(rast, data_type);
  463. return 0;
  464. }
  465. /* read cell file row if not in memory */
  466. if (r != fcb->cur_row) {
  467. fcb->cur_row = r;
  468. read_data(fd, fcb->cur_row, fcb->data, &fcb->cur_nbytes);
  469. }
  470. (transfer_to_cell_FtypeOtype[fcb->map_type][data_type]) (fd, rast);
  471. return 1;
  472. }
  473. static void get_map_row_no_reclass(int fd, void *rast, int row,
  474. RASTER_MAP_TYPE data_type, int null_is_zero,
  475. int with_mask)
  476. {
  477. get_map_row_nomask(fd, rast, row, data_type);
  478. embed_nulls(fd, rast, row, data_type, null_is_zero, with_mask);
  479. }
  480. static void get_map_row(int fd, void *rast, int row, RASTER_MAP_TYPE data_type,
  481. int null_is_zero, int with_mask)
  482. {
  483. struct fileinfo *fcb = &R__.fileinfo[fd];
  484. int size = Rast_cell_size(data_type);
  485. CELL *temp_buf = NULL;
  486. void *buf;
  487. int type;
  488. int i;
  489. if (fcb->reclass_flag && data_type != CELL_TYPE) {
  490. temp_buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
  491. buf = temp_buf;
  492. type = CELL_TYPE;
  493. }
  494. else {
  495. buf = rast;
  496. type = data_type;
  497. }
  498. get_map_row_no_reclass(fd, buf, row, type, null_is_zero, with_mask);
  499. if (!fcb->reclass_flag)
  500. return;
  501. /* if the map is reclass table, get and
  502. reclass CELL row and copy results to needed type */
  503. do_reclass_int(fd, buf, null_is_zero);
  504. if (data_type == CELL_TYPE)
  505. return;
  506. for (i = 0; i < R__.rd_window.cols; i++) {
  507. Rast_set_c_value(rast, temp_buf[i], data_type);
  508. rast = G_incr_void_ptr(rast, size);
  509. }
  510. if (fcb->reclass_flag && data_type != CELL_TYPE) {
  511. G_free(temp_buf);
  512. }
  513. }
  514. /*!
  515. * \brief Read raster row without masking
  516. *
  517. * This routine reads the specified <em>row</em> from the raster map
  518. * open on file descriptor <em>fd</em> into the <em>buf</em> buffer
  519. * like Rast_get_c_row() does. The difference is that masking is
  520. * suppressed. If the user has a mask set, Rast_get_c_row() will apply
  521. * the mask but Rast_get_c_row_nomask() will ignore it. This routine
  522. * prints a diagnostic message and returns -1 if there is an error
  523. * reading the raster map. Otherwise a nonnegative value is returned.
  524. *
  525. * <b>Note.</b> Ignoring the mask is not generally acceptable. Users
  526. * expect the mask to be applied. However, in some cases ignoring the
  527. * mask is justified. For example, the GRASS modules
  528. * <i>r.describe</i>, which reads the raster map directly to report
  529. * all data values in a raster map, and <i>r.slope.aspect</i>, which
  530. * produces slope and aspect from elevation, ignore both the mask and
  531. * the region. However, the number of GRASS modules which do this
  532. * should be minimal. See Mask for more information about the mask.
  533. *
  534. * \param fd file descriptor for the opened raster map
  535. * \param buf buffer for the row to be placed into
  536. * \param row data row desired
  537. * \param data_type data type
  538. *
  539. * \return void
  540. */
  541. void Rast_get_row_nomask(int fd, void *buf, int row, RASTER_MAP_TYPE data_type)
  542. {
  543. get_map_row(fd, buf, row, data_type, 0, 0);
  544. }
  545. /*!
  546. * \brief Read raster row without masking (CELL type)
  547. *
  548. * Same as Rast_get_c_row() except no masking occurs.
  549. *
  550. * \param fd file descriptor for the opened raster map
  551. * \param buf buffer for the row to be placed into
  552. * \param row data row desired
  553. *
  554. * \return void
  555. */
  556. void Rast_get_c_row_nomask(int fd, CELL * buf, int row)
  557. {
  558. Rast_get_row_nomask(fd, buf, row, CELL_TYPE);
  559. }
  560. /*!
  561. * \brief Read raster row without masking (FCELL type)
  562. *
  563. * Same as Rast_get_f_row() except no masking occurs.
  564. *
  565. * \param fd file descriptor for the opened raster map
  566. * \param buf buffer for the row to be placed into
  567. * \param row data row desired
  568. *
  569. * \return void
  570. */
  571. void Rast_get_f_row_nomask(int fd, FCELL * buf, int row)
  572. {
  573. Rast_get_row_nomask(fd, buf, row, FCELL_TYPE);
  574. }
  575. /*!
  576. * \brief Read raster row without masking (DCELL type)
  577. *
  578. * Same as Rast_get_d_row() except no masking occurs.
  579. *
  580. * \param fd file descriptor for the opened raster map
  581. * \param buf buffer for the row to be placed into
  582. * \param row data row desired
  583. *
  584. * \return void
  585. */
  586. void Rast_get_d_row_nomask(int fd, DCELL * buf, int row)
  587. {
  588. Rast_get_row_nomask(fd, buf, row, DCELL_TYPE);
  589. }
  590. /*!
  591. * \brief Get raster row
  592. *
  593. * If <em>data_type</em> is
  594. * - CELL_TYPE, calls Rast_get_c_row()
  595. * - FCELL_TYPE, calls Rast_get_f_row()
  596. * - DCELL_TYPE, calls Rast_get_d_row()
  597. *
  598. * Reads appropriate information into the buffer <em>buf</em> associated
  599. * with the requested row <em>row</em>. <em>buf</em> is associated with the
  600. * current window.
  601. *
  602. * Note, that the type of the data in <em>buf</em> (say X) is independent of
  603. * the type of the data in the file described by <em>fd</em> (say Y).
  604. *
  605. * - Step 1: Read appropriate raw map data into a intermediate buffer.
  606. * - Step 2: Convert the data into a CPU readable format, and subsequently
  607. * resample the data. the data is stored in a second intermediate
  608. * buffer (the type of the data in this buffer is Y).
  609. * - Step 3: Convert this type Y data into type X data and store it in
  610. * buffer "buf". Conversion is performed in functions
  611. * "transfer_to_cell_XY". (For details of the conversion between
  612. * two particular types check the functions).
  613. * - Step 4: read or simmulate null value row and zero out cells corresponding
  614. * to null value cells. The masked out cells are set to null when the
  615. * mask exists. (the MASK is taken care of by null values
  616. * (if the null file doesn't exist for this map, then the null row
  617. * is simulated by assuming that all zero are nulls *** in case
  618. * of Rast_get_row() and assuming that all data is valid
  619. * in case of G_get_f/d_raster_row(). In case of deprecated function
  620. * Rast_get_c_row() all nulls are converted to zeros (so there are
  621. * no embedded nulls at all). Also all masked out cells become zeros.
  622. *
  623. * \param fd file descriptor for the opened raster map
  624. * \param buf buffer for the row to be placed into
  625. * \param row data row desired
  626. * \param data_type data type
  627. *
  628. * \return void
  629. */
  630. void Rast_get_row(int fd, void *buf, int row, RASTER_MAP_TYPE data_type)
  631. {
  632. get_map_row(fd, buf, row, data_type, 0, 1);
  633. }
  634. /*!
  635. * \brief Get raster row (CELL type)
  636. *
  637. * Reads a row of raster data and leaves the NULL values intact. (As
  638. * opposed to the deprecated function Rast_get_c_row() which
  639. * converts NULL values to zero.)
  640. *
  641. * <b>NOTE.</b> When the raster map is old and null file doesn't
  642. * exist, it is assumed that all 0-cells are no-data. When map is
  643. * floating point, uses quant rules set explicitly by
  644. * Rast_set_quant_rules() or stored in map's quant file to convert floats
  645. * to integers.
  646. *
  647. * \param fd file descriptor for the opened raster map
  648. * \param buf buffer for the row to be placed into
  649. * \param row data row desired
  650. *
  651. * \return void
  652. */
  653. void Rast_get_c_row(int fd, CELL * buf, int row)
  654. {
  655. Rast_get_row(fd, buf, row, CELL_TYPE);
  656. }
  657. /*!
  658. * \brief Get raster row (FCELL type)
  659. *
  660. * Read a row from the raster map open on <em>fd</em> into the
  661. * <tt>float</tt> array <em>fcell</em> performing type conversions as
  662. * necessary based on the actual storage type of the map. Masking,
  663. * resampling into the current region. NULL-values are always
  664. * embedded in <tt>fcell</tt> (<em>never converted to a value</em>).
  665. *
  666. * \param fd file descriptor for the opened raster map
  667. * \param buf buffer for the row to be placed into
  668. * \param row data row desired
  669. *
  670. * \return void
  671. */
  672. void Rast_get_f_row(int fd, FCELL * buf, int row)
  673. {
  674. Rast_get_row(fd, buf, row, FCELL_TYPE);
  675. }
  676. /*!
  677. * \brief Get raster row (DCELL type)
  678. *
  679. * Same as Rast_get_f_row() except that the array <em>dcell</em>
  680. * is <tt>double</tt>.
  681. *
  682. * \param fd file descriptor for the opened raster map
  683. * \param buf buffer for the row to be placed into
  684. * \param row data row desired
  685. *
  686. * \return void
  687. */
  688. void Rast_get_d_row(int fd, DCELL * buf, int row)
  689. {
  690. Rast_get_row(fd, buf, row, DCELL_TYPE);
  691. }
  692. static int read_null_bits_compressed(int null_fd, unsigned char *flags,
  693. int row, size_t size, int fd)
  694. {
  695. struct fileinfo *fcb = &R__.fileinfo[fd];
  696. off_t t1 = fcb->null_row_ptr[row];
  697. off_t t2 = fcb->null_row_ptr[row + 1];
  698. size_t readamount = t2 - t1;
  699. unsigned char *compressed_buf;
  700. if (lseek(null_fd, t1, SEEK_SET) < 0)
  701. G_fatal_error(_("Error seeking compressed null data for row %d of <%s>"),
  702. row, fcb->name);
  703. if (readamount == size) {
  704. if (read(null_fd, flags, size) != size) {
  705. G_fatal_error(_("Error reading compressed null data for row %d of <%s>"),
  706. row, fcb->name);
  707. }
  708. return 1;
  709. }
  710. compressed_buf = G_malloc(readamount);
  711. if (read(null_fd, compressed_buf, readamount) != readamount) {
  712. G_free(compressed_buf);
  713. G_fatal_error(_("Error reading compressed null data for row %d of <%s>"),
  714. row, fcb->name);
  715. }
  716. /* null bits file compressed with LZ4, see lib/gis/compress.h */
  717. if (G_lz4_expand(compressed_buf, readamount, flags, size) < 1) {
  718. G_fatal_error(_("Error uncompressing null data for row %d of <%s>"),
  719. row, fcb->name);
  720. }
  721. G_free(compressed_buf);
  722. return 1;
  723. }
  724. int Rast__read_null_bits(int fd, int row, unsigned char *flags)
  725. {
  726. struct fileinfo *fcb = &R__.fileinfo[fd];
  727. int null_fd = fcb->null_fd;
  728. int cols = fcb->cellhd.cols;
  729. off_t offset;
  730. ssize_t size;
  731. int R;
  732. if (compute_window_row(fd, row, &R) <= 0) {
  733. Rast__init_null_bits(flags, cols);
  734. return 1;
  735. }
  736. if (null_fd < 0)
  737. return 0;
  738. size = Rast__null_bitstream_size(cols);
  739. if (fcb->null_row_ptr)
  740. return read_null_bits_compressed(null_fd, flags, R, size, fd);
  741. offset = (off_t) size * R;
  742. if (lseek(null_fd, offset, SEEK_SET) < 0)
  743. G_fatal_error(_("Error seeking null row %d for <%s>"), R, fcb->name);
  744. if (read(null_fd, flags, size) != size)
  745. G_fatal_error(_("Error reading null row %d for <%s>"), R, fcb->name);
  746. return 1;
  747. }
  748. #define check_null_bit(flags, bit_num) ((flags)[(bit_num)>>3] & ((unsigned char)0x80>>((bit_num)&7)) ? 1 : 0)
  749. static void get_null_value_row_nomask(int fd, char *flags, int row)
  750. {
  751. struct fileinfo *fcb = &R__.fileinfo[fd];
  752. int j;
  753. if (row > R__.rd_window.rows || row < 0) {
  754. G_warning(_("Reading raster map <%s@%s> request for row %d is outside region"),
  755. fcb->name, fcb->mapset, row);
  756. for (j = 0; j < R__.rd_window.cols; j++)
  757. flags[j] = 1;
  758. return;
  759. }
  760. if (fcb->vrt) {
  761. /* vrt: already done when reading the real maps, no extra NULL values */
  762. for (j = 0; j < R__.rd_window.cols; j++)
  763. flags[j] = 0;
  764. return;
  765. }
  766. if (row != fcb->null_cur_row) {
  767. if (!Rast__read_null_bits(fd, row, fcb->null_bits)) {
  768. fcb->null_cur_row = -1;
  769. if (fcb->map_type == CELL_TYPE) {
  770. /* If can't read null row, assume that all map 0's are nulls */
  771. CELL *mask_buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
  772. get_map_row_nomask(fd, mask_buf, row, CELL_TYPE);
  773. for (j = 0; j < R__.rd_window.cols; j++)
  774. flags[j] = (mask_buf[j] == 0);
  775. G_free(mask_buf);
  776. }
  777. else { /* fp map */
  778. /* if can't read null row, assume that all data is valid */
  779. G_zero(flags, sizeof(char) * R__.rd_window.cols);
  780. /* the flags row is ready now */
  781. }
  782. return;
  783. } /*if no null file */
  784. else
  785. fcb->null_cur_row = row;
  786. }
  787. /* copy null row to flags row translated by window column mapping */
  788. for (j = 0; j < R__.rd_window.cols; j++) {
  789. if (!fcb->col_map[j])
  790. flags[j] = 1;
  791. else
  792. flags[j] = check_null_bit(fcb->null_bits, fcb->col_map[j] - 1);
  793. }
  794. }
  795. /*--------------------------------------------------------------------------*/
  796. #ifdef HAVE_GDAL
  797. static void get_null_value_row_gdal(int fd, char *flags, int row)
  798. {
  799. struct fileinfo *fcb = &R__.fileinfo[fd];
  800. DCELL *tmp_buf = Rast_allocate_d_input_buf();
  801. int i;
  802. if (get_map_row_nomask(fd, tmp_buf, row, DCELL_TYPE) <= 0) {
  803. memset(flags, 1, R__.rd_window.cols);
  804. G_free(tmp_buf);
  805. return;
  806. }
  807. for (i = 0; i < R__.rd_window.cols; i++)
  808. /* note: using == won't work if the null value is NaN */
  809. flags[i] = !fcb->col_map[i] ||
  810. tmp_buf[i] == fcb->gdal->null_val ||
  811. tmp_buf[i] != tmp_buf[i];
  812. G_free(tmp_buf);
  813. }
  814. #endif
  815. /*--------------------------------------------------------------------------*/
  816. /*--------------------------------------------------------------------------*/
  817. static void embed_mask(char *flags, int row)
  818. {
  819. CELL *mask_buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
  820. int i;
  821. if (R__.auto_mask <= 0) {
  822. G_free(mask_buf);
  823. return;
  824. }
  825. if (get_map_row_nomask(R__.mask_fd, mask_buf, row, CELL_TYPE) < 0) {
  826. G_free(mask_buf);
  827. return;
  828. }
  829. if (R__.fileinfo[R__.mask_fd].reclass_flag) {
  830. embed_nulls(R__.mask_fd, mask_buf, row, CELL_TYPE, 0, 0);
  831. do_reclass_int(R__.mask_fd, mask_buf, 1);
  832. }
  833. for (i = 0; i < R__.rd_window.cols; i++)
  834. if (mask_buf[i] == 0 || Rast_is_c_null_value(&mask_buf[i]))
  835. flags[i] = 1;
  836. G_free(mask_buf);
  837. }
  838. static void get_null_value_row(int fd, char *flags, int row, int with_mask)
  839. {
  840. #ifdef HAVE_GDAL
  841. struct fileinfo *fcb = &R__.fileinfo[fd];
  842. if (fcb->gdal)
  843. get_null_value_row_gdal(fd, flags, row);
  844. else
  845. #endif
  846. get_null_value_row_nomask(fd, flags, row);
  847. if (with_mask)
  848. embed_mask(flags, row);
  849. }
  850. static void embed_nulls(int fd, void *buf, int row, RASTER_MAP_TYPE map_type,
  851. int null_is_zero, int with_mask)
  852. {
  853. struct fileinfo *fcb = &R__.fileinfo[fd];
  854. size_t size = Rast_cell_size(map_type);
  855. char *null_buf;
  856. int i;
  857. /* this is because without null file the nulls can be only due to 0's
  858. in data row or mask */
  859. if (null_is_zero && !fcb->null_file_exists
  860. && (R__.auto_mask <= 0 || !with_mask))
  861. return;
  862. null_buf = G_malloc(R__.rd_window.cols);
  863. get_null_value_row(fd, null_buf, row, with_mask);
  864. for (i = 0; i < R__.rd_window.cols; i++) {
  865. /* also check for nulls which might be already embedded by quant
  866. rules in case of fp map. */
  867. if (null_buf[i] || Rast_is_null_value(buf, map_type)) {
  868. /* G__set_[f/d]_null_value() sets it to 0 is the embedded mode
  869. is not set and calls G_set_[f/d]_null_value() otherwise */
  870. Rast__set_null_value(buf, 1, null_is_zero, map_type);
  871. }
  872. buf = G_incr_void_ptr(buf, size);
  873. }
  874. G_free(null_buf);
  875. }
  876. /*!
  877. \brief Read or simulate null value row
  878. Read or simulate null value row and set the cells corresponding
  879. to null value to 1. The masked out cells are set to null when the
  880. mask exists. (the MASK is taken care of by null values
  881. (if the null file doesn't exist for this map, then the null row
  882. is simulated by assuming that all zeros in raster map are nulls.
  883. Also all masked out cells become nulls.
  884. \param fd file descriptor for the opened map
  885. \param buf buffer for the row to be placed into
  886. \param flags
  887. \param row data row desired
  888. \return void
  889. */
  890. void Rast_get_null_value_row(int fd, char *flags, int row)
  891. {
  892. struct fileinfo *fcb = &R__.fileinfo[fd];
  893. if (!fcb->reclass_flag)
  894. get_null_value_row(fd, flags, row, 1);
  895. else {
  896. CELL *buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
  897. int i;
  898. Rast_get_c_row(fd, buf, row);
  899. for (i = 0; i < R__.rd_window.cols; i++)
  900. flags[i] = Rast_is_c_null_value(&buf[i]) ? 1 : 0;
  901. G_free(buf);
  902. }
  903. }