get_row.c 33 KB

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