get_row.c 26 KB

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