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 "
41 f = row * fcb->
C1 + fcb->
C2;
54 static void do_reclass_int(
int fd,
void *cell,
int null_is_zero)
70 if (c[i] <
min || c[i] >
max) {
78 c[i] = reclass_table[c[i] -
min];
85 static void read_data_fp_compressed(
int fd,
int row,
unsigned char *data_buf,
90 off_t t2 = fcb->
row_ptr[row + 1];
91 size_t readamount = t2 - t1;
95 if (lseek(fcb->
data_fd, t1, SEEK_SET) < 0)
97 _(
"Error seeking fp raster data file for row %d of <%s>: %s"), row,
98 fcb->
name, strerror(errno));
105 G_fatal_error(
_(
"Error uncompressing fp raster data for row %d of "
106 "<%s>: error code %d"),
107 row, fcb->
name, ret);
110 static void rle_decompress(
unsigned char *dst,
const unsigned char *src,
113 int pairs = size / (
nbytes + 1);
116 for (i = 0; i < pairs; i++) {
120 for (j = 0; j < repeat; j++) {
129 static void read_data_compressed(
int fd,
int row,
unsigned char *data_buf,
134 off_t t2 = fcb->
row_ptr[row + 1];
135 ssize_t readamount = t2 - t1;
137 unsigned char *cmp, *cmp2;
140 if (lseek(fcb->
data_fd, t1, SEEK_SET) < 0)
142 _(
"Error seeking raster data file for row %d of <%s>: %s"), row,
143 fcb->
name, strerror(errno));
147 if (read(fcb->
data_fd, cmp, readamount) != readamount) {
149 G_fatal_error(
_(
"Error reading raster data for row %d of <%s>: %s"),
150 row, fcb->
name, strerror(errno));
169 rle_decompress(data_buf, cmp, n, readamount);
171 if ((n =
G_expand(cmp, readamount, data_buf, bufsize,
173 (
unsigned int)n != bufsize) {
175 _(
"Error uncompressing raster data for row %d of <%s>"),
181 memcpy(data_buf, cmp, readamount);
186 static void read_data_uncompressed(
int fd,
int row,
unsigned char *data_buf,
194 if (lseek(fcb->
data_fd, (off_t)row * bufsize, SEEK_SET) == -1)
195 G_fatal_error(
_(
"Error reading raster data for row %d of <%s>"), row,
198 if (read(fcb->
data_fd, data_buf, bufsize) != bufsize)
199 G_fatal_error(
_(
"Error reading raster data for row %d of <%s>"), row,
204 static void read_data_gdal(
int fd,
int row,
unsigned char *data_buf,
235 _(
"Error reading raster data via GDAL for row %d of <%s>"), row,
240 static void read_data(
int fd,
int row,
unsigned char *data_buf,
int *
nbytes)
246 read_data_gdal(fd, row, data_buf,
nbytes);
252 read_data_uncompressed(fd, row, data_buf,
nbytes);
254 read_data_compressed(fd, row, data_buf,
nbytes);
256 read_data_fp_compressed(fd, row, data_buf,
nbytes);
260 static void cell_values_int(
int fd
UNUSED,
const unsigned char *
data UNUSED,
269 for (i = 0; i < n; i++) {
270 const unsigned char *d;
280 if (cmap[i] == cmapold) {
287 if (big && (*d & 0x80)) {
296 for (j = 1; j <
nbytes; j++)
305 static void cell_values_float(
int fd,
const unsigned char *
data UNUSED,
310 const float *work_buf = (
const float *)fcb->
data;
314 for (i = 0; i < n; i++) {
324 static void cell_values_double(
int fd,
const unsigned char *
data UNUSED,
329 const double *work_buf = (
const double *)fcb->
data;
333 for (i = 0; i < n; i++) {
344 static void gdal_values_int(
int fd,
const unsigned char *
data,
350 const unsigned char *d;
354 for (i = 0; i < n; i++) {
360 if (cmap[i] == cmapold) {
375 c[i] = *(GUInt16 *)d;
381 c[i] = *(GUInt32 *)d;
393 static void gdal_values_float(
int fd
UNUSED,
const unsigned char *
data,
398 const float *d = (
const float *)
data;
402 for (i = 0; i < n; i++) {
408 if (cmap[i] == cmapold) {
413 c[i] = d[cmap[i] - 1];
419 static void gdal_values_double(
int fd
UNUSED,
const unsigned char *
data,
424 const double *d = (
const double *)
data;
428 for (i = 0; i < n; i++) {
434 if (cmap[i] == cmapold) {
439 c[i] = d[cmap[i] - 1];
455 static void transfer_to_cell_XX(
int fd,
void *cell)
457 static void (*cell_values_type[3])(
459 int) = {cell_values_int, cell_values_float, cell_values_double};
461 static void (*gdal_values_type[3])(
463 int) = {gdal_values_int, gdal_values_float, gdal_values_double};
479 static void transfer_to_cell_fi(
int fd,
void *cell)
485 transfer_to_cell_XX(fd, work_buf);
496 static void transfer_to_cell_di(
int fd,
void *cell)
502 transfer_to_cell_XX(fd, work_buf);
513 static void transfer_to_cell_if(
int fd,
void *cell)
518 transfer_to_cell_XX(fd, work_buf);
521 ((
FCELL *)cell)[i] = work_buf[i];
526 static void transfer_to_cell_df(
int fd,
void *cell)
531 transfer_to_cell_XX(fd, work_buf);
534 ((
FCELL *)cell)[i] = work_buf[i];
539 static void transfer_to_cell_id(
int fd,
void *cell)
544 transfer_to_cell_XX(fd, work_buf);
547 ((
DCELL *)cell)[i] = work_buf[i];
552 static void transfer_to_cell_fd(
int fd,
void *cell)
557 transfer_to_cell_XX(fd, work_buf);
560 ((
DCELL *)cell)[i] = work_buf[i];
569 static int get_map_row_nomask(
int fd,
void *rast,
int row,
572 static void (*transfer_to_cell_FtypeOtype[3][3])(int,
void *) = {
573 {transfer_to_cell_XX, transfer_to_cell_if, transfer_to_cell_id},
574 {transfer_to_cell_fi, transfer_to_cell_XX, transfer_to_cell_fd},
575 {transfer_to_cell_di, transfer_to_cell_df, transfer_to_cell_XX}};
585 row_status = compute_window_row(fd, row, &
r);
599 (transfer_to_cell_FtypeOtype[fcb->
map_type][data_type])(fd, rast);
604 static void get_map_row_no_reclass(
int fd,
void *rast,
int row,
608 get_map_row_nomask(fd, rast, row, data_type);
609 embed_nulls(fd, rast, row, data_type, null_is_zero, with_mask);
612 static void get_map_row(
int fd,
void *rast,
int row,
RASTER_MAP_TYPE data_type,
613 int null_is_zero,
int with_mask)
632 get_map_row_no_reclass(fd, buf, row, type, null_is_zero, with_mask);
640 do_reclass_int(fd, buf, null_is_zero);
684 get_map_row(fd, buf, row, data_type, 0, 0);
776 get_map_row(fd, buf, row, data_type, 0, 1);
840 static int read_null_bits_compressed(
int null_fd,
unsigned char *flags,
int row,
846 size_t readamount = t2 - t1;
847 unsigned char *compressed_buf;
850 if (lseek(
null_fd, t1, SEEK_SET) < 0)
852 _(
"Error seeking compressed null data for row %d of <%s>"), row,
855 if (readamount == size) {
856 if ((res = read(
null_fd, flags, size)) < 0 ||
857 (
unsigned int)res != size) {
859 _(
"Error reading compressed null data for row %d of <%s>"), row,
865 compressed_buf =
G_malloc(readamount);
867 if ((res = read(
null_fd, compressed_buf, readamount)) < 0 ||
868 (
unsigned int)res != readamount) {
871 _(
"Error reading compressed null data for row %d of <%s>"), row,
876 if (
G_lz4_expand(compressed_buf, readamount, flags, size) < 1) {
877 G_fatal_error(
_(
"Error uncompressing null data for row %d of <%s>"),
895 if (compute_window_row(fd, row, &R) <= 0) {
906 return read_null_bits_compressed(
null_fd, flags, R, size, fd);
908 offset = (off_t)size * R;
910 if (lseek(
null_fd, offset, SEEK_SET) < 0)
913 if (read(
null_fd, flags, size) != size)
919 #define check_null_bit(flags, bit_num) \
920 ((flags)[(bit_num) >> 3] & ((unsigned char)0x80 >> ((bit_num) & 7)) ? 1 : 0)
922 static void get_null_value_row_nomask(
int fd,
char *flags,
int row)
928 G_warning(
_(
"Reading raster map <%s@%s> request for row %d is outside "
949 get_map_row_nomask(fd, mask_buf, row,
CELL_TYPE);
951 flags[j] = (mask_buf[j] == 0);
980 static void get_null_value_row_gdal(
int fd,
char *flags,
int row)
986 if (get_map_row_nomask(fd, tmp_buf, row,
DCELL_TYPE) <= 0) {
995 tmp_buf[i] != tmp_buf[i];
1006 static void embed_mask(
char *flags,
int row)
1033 static void get_null_value_row(
int fd,
char *flags,
int row,
int with_mask)
1039 get_null_value_row_gdal(fd, flags, row);
1042 get_null_value_row_nomask(fd, flags, row);
1045 embed_mask(flags, row);
1049 int null_is_zero,
int with_mask)
1064 get_null_value_row(fd, null_buf, row, with_mask);
1102 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)