get_row.c 28 KB

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