16 #include <sys/types.h> 27 static int compute_window_row(
int fd,
int row,
int *cellRow)
35 G_fatal_error(
_(
"Reading raster map <%s@%s> request for row %d is outside region"),
40 f = row * fcb->
C1 + fcb->
C2;
53 static void do_reclass_int(
int fd,
void *cell,
int null_is_zero)
69 if (c[i] < min || c[i] > max) {
77 c[i] = reclass_table[c[i] -
min];
84 static void read_data_fp_compressed(
int fd,
int row,
unsigned char *data_buf,
89 off_t t2 = fcb->
row_ptr[row + 1];
90 size_t readamount = t2 - t1;
94 if (lseek(fcb->
data_fd, t1, SEEK_SET) < 0)
95 G_fatal_error(
_(
"Error seeking fp raster data file for row %d of <%s>: %s"),
96 row, fcb->
name, strerror(errno));
103 G_fatal_error(
_(
"Error uncompressing fp raster data for row %d of <%s>: error code %d"),
104 row, fcb->
name, ret);
107 static void rle_decompress(
unsigned char *
dst,
const unsigned char *src,
108 int nbytes,
int size)
110 int pairs = size / (nbytes + 1);
113 for (i = 0; i < pairs; i++) {
117 for (j = 0; j < repeat; j++) {
118 memcpy(dst, src, nbytes);
126 static void read_data_compressed(
int fd,
int row,
unsigned char *data_buf,
131 off_t t2 = fcb->
row_ptr[row + 1];
132 ssize_t readamount = t2 - t1;
134 unsigned char *cmp, *cmp2;
137 if (lseek(fcb->
data_fd, t1, SEEK_SET) < 0)
138 G_fatal_error(
_(
"Error seeking raster data file for row %d of <%s>: %s"),
139 row, fcb->
name, strerror(errno));
143 if (read(fcb->
data_fd, cmp, readamount) != readamount) {
145 G_fatal_error(
_(
"Error reading raster data for row %d of <%s>: %s"),
146 row, fcb->
name, strerror(errno));
155 n = *nbytes = *cmp++;
160 n = *nbytes = fcb->
nbytes;
165 rle_decompress(data_buf, cmp, n, readamount);
167 if (
G_expand(cmp, readamount, data_buf, bufsize,
169 G_fatal_error(
_(
"Error uncompressing raster data for row %d of <%s>"),
174 memcpy(data_buf, cmp, readamount);
179 static void read_data_uncompressed(
int fd,
int row,
unsigned char *data_buf,
187 if (lseek(fcb->
data_fd, (off_t) row * bufsize, SEEK_SET) == -1)
191 if (read(fcb->
data_fd, data_buf, bufsize) != bufsize)
197 static void read_data_gdal(
int fd,
int row,
unsigned char *data_buf,
228 G_fatal_error(
_(
"Error reading raster data via GDAL for row %d of <%s>"),
233 static void read_data(
int fd,
int row,
unsigned char *data_buf,
int *nbytes)
239 read_data_gdal(fd, row, data_buf, nbytes);
245 read_data_uncompressed(fd, row, data_buf, nbytes);
247 read_data_compressed(fd, row, data_buf, nbytes);
249 read_data_fp_compressed(fd, row, data_buf, nbytes);
253 static void cell_values_int(
int fd,
const unsigned char *
data,
259 int big = (size_t) nbytes >=
sizeof(
CELL);
262 for (i = 0; i < n; i++) {
263 const unsigned char *d;
273 if (cmap[i] == cmapold) {
278 d = data + (cmap[i] - 1) * nbytes;
280 if (big && (*d & 0x80)) {
289 for (j = 1; j <
nbytes; j++)
298 static void cell_values_float(
int fd,
const unsigned char *data,
303 const float *work_buf = (
const float *) fcb->
data;
307 for (i = 0; i < n; i++) {
317 static void cell_values_double(
int fd,
const unsigned char *data,
322 const double *work_buf = (
const double *) fcb->
data;
326 for (i = 0; i < n; i++) {
337 static void gdal_values_int(
int fd,
const unsigned char *data,
342 const unsigned char *d;
346 for (i = 0; i < n; i++) {
352 if (cmap[i] == cmapold) {
353 cell[i] = cell[i - 1];
357 d = data + (cmap[i] - 1) * nbytes;
361 cell[i] = *(GByte *) d;
364 cell[i] = *(GInt16 *) d;
367 cell[i] = *(GUInt16 *) d;
370 cell[i] = *(GInt32 *) d;
373 cell[i] = *(GUInt32 *) d;
385 static void gdal_values_float(
int fd,
const float *data,
392 for (i = 0; i < n; i++) {
398 if (cmap[i] == cmapold) {
399 cell[i] = cell[i - 1];
403 cell[i] = data[cmap[i] - 1];
409 static void gdal_values_double(
int fd,
const double *data,
416 for (i = 0; i < n; i++) {
422 if (cmap[i] == cmapold) {
423 cell[i] = cell[i - 1];
427 cell[i] = data[cmap[i] - 1];
443 static void transfer_to_cell_XX(
int fd,
void *cell)
445 static void (*cell_values_type[3]) () = {
446 cell_values_int, cell_values_float, cell_values_double};
448 static void (*gdal_values_type[3]) () = {
449 gdal_values_int, gdal_values_float, gdal_values_double};
465 static void transfer_to_cell_fi(
int fd,
void *cell)
471 transfer_to_cell_XX(fd, work_buf);
480 static void transfer_to_cell_di(
int fd,
void *cell)
486 transfer_to_cell_XX(fd, work_buf);
495 static void transfer_to_cell_if(
int fd,
void *cell)
500 transfer_to_cell_XX(fd, work_buf);
503 ((
FCELL *) cell)[i] = work_buf[i];
508 static void transfer_to_cell_df(
int fd,
void *cell)
513 transfer_to_cell_XX(fd, work_buf);
516 ((
FCELL *) cell)[i] = work_buf[i];
521 static void transfer_to_cell_id(
int fd,
void *cell)
526 transfer_to_cell_XX(fd, work_buf);
529 ((
DCELL *) cell)[i] = work_buf[i];
534 static void transfer_to_cell_fd(
int fd,
void *cell)
539 transfer_to_cell_XX(fd, work_buf);
542 ((
DCELL *) cell)[i] = work_buf[i];
551 static int get_map_row_nomask(
int fd,
void *rast,
int row,
554 static void (*transfer_to_cell_FtypeOtype[3][3]) () = {
556 transfer_to_cell_XX, transfer_to_cell_if, transfer_to_cell_id}, {
557 transfer_to_cell_fi, transfer_to_cell_XX, transfer_to_cell_fd}, {
558 transfer_to_cell_di, transfer_to_cell_df, transfer_to_cell_XX}
569 row_status = compute_window_row(fd, row, &r);
583 (transfer_to_cell_FtypeOtype[fcb->
map_type][data_type]) (fd, rast);
588 static void get_map_row_no_reclass(
int fd,
void *rast,
int row,
592 get_map_row_nomask(fd, rast, row, data_type);
593 embed_nulls(fd, rast, row, data_type, null_is_zero, with_mask);
596 static void get_map_row(
int fd,
void *rast,
int row,
RASTER_MAP_TYPE data_type,
597 int null_is_zero,
int with_mask)
616 get_map_row_no_reclass(fd, buf, row, type, null_is_zero, with_mask);
624 do_reclass_int(fd, buf, null_is_zero);
668 get_map_row(fd, buf, row, data_type, 0, 0);
761 get_map_row(fd, buf, row, data_type, 0, 1);
825 static int read_null_bits_compressed(
int null_fd,
unsigned char *flags,
826 int row,
size_t size,
int fd)
831 size_t readamount = t2 - t1;
832 unsigned char *compressed_buf;
834 if (lseek(null_fd, t1, SEEK_SET) < 0)
835 G_fatal_error(
_(
"Error seeking compressed null data for row %d of <%s>"),
838 if (readamount == size) {
839 if (read(null_fd, flags, size) != size) {
840 G_fatal_error(
_(
"Error reading compressed null data for row %d of <%s>"),
846 compressed_buf =
G_malloc(readamount);
848 if (read(null_fd, compressed_buf, readamount) != readamount) {
850 G_fatal_error(
_(
"Error reading compressed null data for row %d of <%s>"),
855 if (
G_lz4_expand(compressed_buf, readamount, flags, size) < 1) {
856 G_fatal_error(
_(
"Error uncompressing null data for row %d of <%s>"),
874 if (compute_window_row(fd, row, &R) <= 0) {
885 return read_null_bits_compressed(null_fd, flags, R, size, fd);
887 offset = (off_t) size * R;
889 if (lseek(null_fd, offset, SEEK_SET) < 0)
892 if (read(null_fd, flags, size) != size)
898 #define check_null_bit(flags, bit_num) ((flags)[(bit_num)>>3] & ((unsigned char)0x80>>((bit_num)&7)) ? 1 : 0) 900 static void get_null_value_row_nomask(
int fd,
char *flags,
int row)
906 G_warning(
_(
"Reading raster map <%s@%s> request for row %d is outside region"),
926 get_map_row_nomask(fd, mask_buf, row,
CELL_TYPE);
928 flags[j] = (mask_buf[j] == 0);
957 static void get_null_value_row_gdal(
int fd,
char *flags,
int row)
963 if (get_map_row_nomask(fd, tmp_buf, row,
DCELL_TYPE) <= 0) {
973 tmp_buf[i] != tmp_buf[i];
984 static void embed_mask(
char *flags,
int row)
1011 static void get_null_value_row(
int fd,
char *flags,
int row,
int with_mask)
1017 get_null_value_row_gdal(fd, flags, row);
1020 get_null_value_row_nomask(fd, flags, row);
1023 embed_mask(flags, row);
1027 int null_is_zero,
int with_mask)
1042 get_null_value_row(fd, null_buf, row, with_mask);
1080 get_null_value_row(fd, flags, row, 1);
struct Cell_head rd_window
void Rast_get_row_nomask(int fd, void *buf, int row, RASTER_MAP_TYPE data_type)
Read raster row without masking.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void Rast_get_f_row(int fd, FCELL *buf, int row)
Get raster row (FCELL type)
void Rast_set_c_null_value(CELL *, int)
To set a number of CELL raster values to NULL.
void Rast_get_null_value_row(int fd, char *flags, int row)
Read or simulate null value row.
CPLErr Rast_gdal_raster_IO(GDALRasterBandH, GDALRWFlag, int, int, int, int, void *, int, int, GDALDataType, int, int)
void Rast_get_c_row(int fd, CELL *buf, int row)
Get raster row (CELL type)
void G_free(void *)
Free allocated memory.
int G_lz4_expand(unsigned char *src, int src_sz, unsigned char *dst, int dst_sz)
void Rast_get_row(int fd, void *buf, int row, RASTER_MAP_TYPE data_type)
Get raster row.
#define G_incr_void_ptr(ptr, size)
unsigned char * null_bits
int Rast_get_vrt_row(int, void *, int, RASTER_MAP_TYPE)
void Rast_get_f_row_nomask(int fd, FCELL *buf, int row)
Read raster row without masking (FCELL type)
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
void Rast__set_null_value(void *, int, int, RASTER_MAP_TYPE)
To set one or more raster values to null.
CELL Rast_quant_get_cell_value(struct Quant *, DCELL)
Returns a CELL category for the floating-point value based on the quantization rules in q...
int compressed
Compression mode (raster header only)
int Rast__null_bitstream_size(int)
Determines null bitstream size.
size_t Rast_cell_size(RASTER_MAP_TYPE)
Returns size of a raster cell in bytes.
void Rast_get_d_row(int fd, DCELL *buf, int row)
Get raster row (DCELL type)
void Rast_get_c_row_nomask(int fd, CELL *buf, int row)
Read raster row without masking (CELL type)
void G_xdr_get_float(float *, const void *)
int Rast__read_null_bits(int fd, int row, unsigned char *flags)
#define check_null_bit(flags, bit_num)
struct fileinfo * fileinfo
void Rast_get_d_row_nomask(int fd, DCELL *buf, int row)
Read raster row without masking (DCELL type)
DCELL * Rast_allocate_d_input_buf(void)
void Rast_set_c_value(void *, CELL, RASTER_MAP_TYPE)
Places a CELL raster value.
void Rast__init_null_bits(unsigned char *, int)
?
int cols
Number of columns for 2D data.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_warning(const char *,...) __attribute__((format(printf
int rows
Number of rows for 2D data.
#define Rast_is_c_null_value(cellVal)
int G_read_compressed(int, int, unsigned char *, int, int)
void Rast_zero_input_buf(void *, RASTER_MAP_TYPE)
int G_expand(unsigned char *, int, unsigned char *, int, int)
int Rast_is_null_value(const void *, RASTER_MAP_TYPE)
To check if a raster value is set to NULL.
void G_xdr_get_double(double *, const void *)