17 #include <sys/types.h>
28 static int compute_window_row(
int fd,
int row,
int *cellRow)
36 G_fatal_error(
_(
"Reading raster map <%s@%s> request for row %d is "
42 f = row * fcb->
C1 + fcb->
C2;
55 static void do_reclass_int(
int fd,
void *cell,
int null_is_zero)
71 if (c[i] <
min || c[i] >
max) {
79 c[i] = reclass_table[c[i] -
min];
86 static void read_data_fp_compressed(
int fd,
int row,
unsigned char *data_buf,
91 off_t t2 = fcb->
row_ptr[row + 1];
92 size_t readamount = t2 - t1;
96 if (lseek(fcb->
data_fd, t1, SEEK_SET) < 0)
98 _(
"Error seeking fp raster data file for row %d of <%s>: %s"), row,
99 fcb->
name, strerror(errno));
106 G_fatal_error(
_(
"Error uncompressing fp raster data for row %d of "
107 "<%s>: error code %d"),
108 row, fcb->
name, ret);
111 static void rle_decompress(
unsigned char *
dst,
const unsigned char *
src,
114 int pairs = size / (
nbytes + 1);
117 for (i = 0; i < pairs; i++) {
121 for (j = 0; j < repeat; j++) {
130 static void read_data_compressed(
int fd,
int row,
unsigned char *data_buf,
135 off_t t2 = fcb->
row_ptr[row + 1];
136 ssize_t readamount = t2 - t1;
138 unsigned char *cmp, *cmp2;
141 if (lseek(fcb->
data_fd, t1, SEEK_SET) < 0)
143 _(
"Error seeking raster data file for row %d of <%s>: %s"), row,
144 fcb->
name, strerror(errno));
148 if (read(fcb->
data_fd, cmp, readamount) != readamount) {
150 G_fatal_error(
_(
"Error reading raster data for row %d of <%s>: %s"),
151 row, fcb->
name, strerror(errno));
170 rle_decompress(data_buf, cmp, n, readamount);
172 if ((n =
G_expand(cmp, readamount, data_buf, bufsize,
174 (
unsigned int)n != bufsize) {
176 _(
"Error uncompressing raster data for row %d of <%s>"),
182 memcpy(data_buf, cmp, readamount);
187 static void read_data_uncompressed(
int fd,
int row,
unsigned char *data_buf,
195 if (lseek(fcb->
data_fd, (off_t)row * bufsize, SEEK_SET) == -1)
196 G_fatal_error(
_(
"Error reading raster data for row %d of <%s>"), row,
199 if (read(fcb->
data_fd, data_buf, bufsize) != bufsize)
200 G_fatal_error(
_(
"Error reading raster data for row %d of <%s>"), row,
205 static void read_data_gdal(
int fd,
int row,
unsigned char *data_buf,
236 _(
"Error reading raster data via GDAL for row %d of <%s>"), row,
241 static void read_data(
int fd,
int row,
unsigned char *data_buf,
int *
nbytes)
247 read_data_gdal(fd, row, data_buf,
nbytes);
253 read_data_uncompressed(fd, row, data_buf,
nbytes);
255 read_data_compressed(fd, row, data_buf,
nbytes);
257 read_data_fp_compressed(fd, row, data_buf,
nbytes);
261 static void cell_values_int(
int fd
UNUSED,
const unsigned char *
data UNUSED,
270 for (i = 0; i < n; i++) {
271 const unsigned char *d;
281 if (cmap[i] == cmapold) {
288 if (big && (*d & 0x80)) {
297 for (j = 1; j <
nbytes; j++)
306 static void cell_values_float(
int fd,
const unsigned char *
data UNUSED,
311 const float *work_buf = (
const float *)fcb->
data;
315 for (i = 0; i < n; i++) {
325 static void cell_values_double(
int fd,
const unsigned char *
data UNUSED,
330 const double *work_buf = (
const double *)fcb->
data;
334 for (i = 0; i < n; i++) {
345 static void gdal_values_int(
int fd,
const unsigned char *
data,
351 const unsigned char *d;
355 for (i = 0; i < n; i++) {
361 if (cmap[i] == cmapold) {
373 #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 7, 0)
382 c[i] = *(GUInt16 *)d;
388 c[i] = *(GUInt32 *)d;
400 static void gdal_values_float(
int fd
UNUSED,
const unsigned char *
data,
405 const float *d = (
const float *)
data;
409 for (i = 0; i < n; i++) {
415 if (cmap[i] == cmapold) {
420 c[i] = d[cmap[i] - 1];
426 static void gdal_values_double(
int fd
UNUSED,
const unsigned char *
data,
431 const double *d = (
const double *)
data;
435 for (i = 0; i < n; i++) {
441 if (cmap[i] == cmapold) {
446 c[i] = d[cmap[i] - 1];
462 static void transfer_to_cell_XX(
int fd,
void *cell)
464 static void (*cell_values_type[3])(
466 int) = {cell_values_int, cell_values_float, cell_values_double};
468 static void (*gdal_values_type[3])(
470 int) = {gdal_values_int, gdal_values_float, gdal_values_double};
486 static void transfer_to_cell_fi(
int fd,
void *cell)
492 transfer_to_cell_XX(fd, work_buf);
503 static void transfer_to_cell_di(
int fd,
void *cell)
509 transfer_to_cell_XX(fd, work_buf);
520 static void transfer_to_cell_if(
int fd,
void *cell)
525 transfer_to_cell_XX(fd, work_buf);
528 ((
FCELL *)cell)[i] = work_buf[i];
533 static void transfer_to_cell_df(
int fd,
void *cell)
538 transfer_to_cell_XX(fd, work_buf);
541 ((
FCELL *)cell)[i] = work_buf[i];
546 static void transfer_to_cell_id(
int fd,
void *cell)
551 transfer_to_cell_XX(fd, work_buf);
554 ((
DCELL *)cell)[i] = work_buf[i];
559 static void transfer_to_cell_fd(
int fd,
void *cell)
564 transfer_to_cell_XX(fd, work_buf);
567 ((
DCELL *)cell)[i] = work_buf[i];
576 static int get_map_row_nomask(
int fd,
void *rast,
int row,
579 static void (*transfer_to_cell_FtypeOtype[3][3])(int,
void *) = {
580 {transfer_to_cell_XX, transfer_to_cell_if, transfer_to_cell_id},
581 {transfer_to_cell_fi, transfer_to_cell_XX, transfer_to_cell_fd},
582 {transfer_to_cell_di, transfer_to_cell_df, transfer_to_cell_XX}};
592 row_status = compute_window_row(fd, row, &
r);
606 (transfer_to_cell_FtypeOtype[fcb->
map_type][data_type])(fd, rast);
611 static void get_map_row_no_reclass(
int fd,
void *rast,
int row,
615 get_map_row_nomask(fd, rast, row, data_type);
616 embed_nulls(fd, rast, row, data_type, null_is_zero, with_mask);
619 static void get_map_row(
int fd,
void *rast,
int row,
RASTER_MAP_TYPE data_type,
620 int null_is_zero,
int with_mask)
639 get_map_row_no_reclass(fd, buf, row, type, null_is_zero, with_mask);
647 do_reclass_int(fd, buf, null_is_zero);
691 get_map_row(fd, buf, row, data_type, 0, 0);
783 get_map_row(fd, buf, row, data_type, 0, 1);
847 static int read_null_bits_compressed(
int null_fd,
unsigned char *flags,
int row,
853 size_t readamount = t2 - t1;
854 unsigned char *compressed_buf;
857 if (lseek(
null_fd, t1, SEEK_SET) < 0)
859 _(
"Error seeking compressed null data for row %d of <%s>"), row,
862 if (readamount == size) {
863 if ((res = read(
null_fd, flags, size)) < 0 ||
864 (
unsigned int)res != size) {
866 _(
"Error reading compressed null data for row %d of <%s>"), row,
872 compressed_buf =
G_malloc(readamount);
874 if ((res = read(
null_fd, compressed_buf, readamount)) < 0 ||
875 (
unsigned int)res != readamount) {
878 _(
"Error reading compressed null data for row %d of <%s>"), row,
883 if (
G_lz4_expand(compressed_buf, readamount, flags, size) < 1) {
884 G_fatal_error(
_(
"Error uncompressing null data for row %d of <%s>"),
902 if (compute_window_row(fd, row, &R) <= 0) {
913 return read_null_bits_compressed(
null_fd, flags, R, size, fd);
915 offset = (off_t)size * R;
917 if (lseek(
null_fd, offset, SEEK_SET) < 0)
920 if (read(
null_fd, flags, size) != size)
926 #define check_null_bit(flags, bit_num) \
927 ((flags)[(bit_num) >> 3] & ((unsigned char)0x80 >> ((bit_num) & 7)) ? 1 : 0)
929 static void get_null_value_row_nomask(
int fd,
char *flags,
int row)
935 G_warning(
_(
"Reading raster map <%s@%s> request for row %d is outside "
956 get_map_row_nomask(fd, mask_buf, row,
CELL_TYPE);
958 flags[j] = (mask_buf[j] == 0);
987 static void get_null_value_row_gdal(
int fd,
char *flags,
int row)
993 if (get_map_row_nomask(fd, tmp_buf, row,
DCELL_TYPE) <= 0) {
1002 tmp_buf[i] != tmp_buf[i];
1013 static void embed_mask(
char *flags,
int row)
1040 static void get_null_value_row(
int fd,
char *flags,
int row,
int with_mask)
1046 get_null_value_row_gdal(fd, flags, row);
1049 get_null_value_row_nomask(fd, flags, row);
1052 embed_mask(flags, row);
1056 int null_is_zero,
int with_mask)
1071 get_null_value_row(fd, null_buf, row, with_mask);
1109 get_null_value_row(fd, flags, row, 1);
CPLErr Rast_gdal_raster_IO(GDALRasterBandH, GDALRWFlag, int, int, int, int, void *, int, int, GDALDataType, int, int)
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int G_expand(unsigned char *, int, unsigned char *, int, int)
void G_xdr_get_float(float *, const void *)
int G_lz4_expand(unsigned char *src, int src_sz, unsigned char *dst, int dst_sz)
int G_read_compressed(int, int, unsigned char *, int, int)
void G_xdr_get_double(double *, const void *)
#define G_incr_void_ptr(ptr, size)
int Rast_is_null_value(const void *, RASTER_MAP_TYPE)
To check if a raster value is set to NULL.
DCELL * Rast_allocate_d_input_buf(void)
int Rast__null_bitstream_size(int)
Determines null bitstream size.
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 Rast_get_vrt_row(int, void *, int, RASTER_MAP_TYPE)
void Rast_zero_input_buf(void *, RASTER_MAP_TYPE)
void Rast_set_c_null_value(CELL *, int)
To set a number of CELL raster values to NULL.
size_t Rast_cell_size(RASTER_MAP_TYPE)
Returns size of a raster cell in bytes.
void Rast_set_c_value(void *, CELL, RASTER_MAP_TYPE)
Places a CELL raster value.
void Rast__set_null_value(void *, int, int, RASTER_MAP_TYPE)
To set one or more raster values to null.
void Rast__init_null_bits(unsigned char *, int)
?
#define Rast_is_c_null_value(cellVal)
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
void Rast_get_c_row_nomask(int fd, CELL *buf, int row)
Read raster row without masking (CELL type)
#define check_null_bit(flags, bit_num)
void Rast_get_null_value_row(int fd, char *flags, int row)
Read or simulate null value row.
void Rast_get_f_row_nomask(int fd, FCELL *buf, int row)
Read raster row without masking (FCELL type)
void Rast_get_row_nomask(int fd, void *buf, int row, RASTER_MAP_TYPE data_type)
Read raster row without masking.
void Rast_get_d_row_nomask(int fd, DCELL *buf, int row)
Read raster row without masking (DCELL type)
void Rast_get_d_row(int fd, DCELL *buf, int row)
Get raster row (DCELL type)
void Rast_get_c_row(int fd, CELL *buf, int row)
Get raster row (CELL type)
void Rast_get_f_row(int fd, FCELL *buf, int row)
Get raster row (FCELL type)
void Rast_get_row(int fd, void *buf, int row, RASTER_MAP_TYPE data_type)
Get raster row.
int Rast__read_null_bits(int fd, int row, unsigned char *flags)
int compressed
Compression mode (raster header only)
int rows
Number of rows for 2D data.
int cols
Number of columns for 2D data.
struct fileinfo * fileinfo
struct Cell_head rd_window
unsigned char * null_bits
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)