20 #include <sys/types.h>
67 put_raster_row(fd, buf, data_type, 0);
115 static void write_data(
int fd,
int row,
unsigned char *buf,
int n)
118 ssize_t nwrite = fcb->
nbytes * n;
120 if (write(fcb->
data_fd, buf, nwrite) != nwrite)
122 _(
"Error writing uncompressed FP data for row %d of <%s>: %s"), row,
123 fcb->
name, strerror(errno));
126 static void write_data_compressed(
int fd,
int row,
unsigned char *buf,
int n,
130 int nwrite = fcb->
nbytes * n;
134 _(
"Error writing compressed FP data for row %d of <%s>: %s"), row,
135 fcb->
name, strerror(errno));
138 static void set_file_pointer(
int fd,
int row)
145 static void convert_float(
float *work_buf,
char *null_buf,
const FCELL *rast,
150 for (i = 0; i < n; i++) {
165 static void convert_double(
double *work_buf,
char *null_buf,
const DCELL *rast,
170 for (i = 0; i < n; i++) {
186 static void put_fp_data(
int fd,
char *null_buf,
const void *rast,
int row,
203 set_file_pointer(fd, row);
206 convert_float(work_buf, null_buf, rast, n);
208 convert_double(work_buf, null_buf, rast, n);
213 write_data(fd, row, work_buf, n);
218 static void convert_int(
unsigned char *wk,
char *null_buf,
const CELL *rast,
219 int n,
int len,
int zeros_r_nulls)
225 for (i = 0; i < n; i++) {
235 else if (zeros_r_nulls && !v)
247 for (k = len - 1; k >= 0; k--) {
260 static int count_bytes(
const unsigned char *wk,
int n,
int len)
264 for (i = 0; i < len - 1; i++)
265 for (j = 0; j < n; j++)
266 if (wk[j * len + i] != 0)
272 static void trim_bytes(
unsigned char *wk,
int n,
int slen,
int trim)
274 unsigned char *wk2 = wk;
277 for (i = 0; i < n; i++) {
278 for (j = 0; j < trim; j++)
280 for (; j < slen; j++)
285 static int same(
const unsigned char *
x,
const unsigned char *y,
int n)
287 return (memcmp(
x, y, n) == 0);
290 static int count_run(
const unsigned char *
src,
int n,
int nbytes)
295 for (i = 1; i < n; i++) {
305 static int rle_compress(
unsigned char *
dst,
unsigned char *
src,
int n,
328 return (nwrite >= total) ? 0 : nwrite;
331 static void put_data(
int fd,
char *null_buf,
const CELL *cell,
int row,
int n,
336 int len = compressed ? (int)
sizeof(
CELL) : fcb->
nbytes;
337 unsigned char *work_buf, *wk;
350 set_file_pointer(fd, row);
355 convert_int(wk, null_buf, cell, n, len, zeros_r_nulls);
358 int nbytes = count_bytes(wk, n, len);
359 unsigned char *compressed_buf;
367 trim_bytes(wk, n, len, len -
nbytes);
375 compressed_buf =
G_malloc(cmax + 1);
377 compressed_buf[0] = work_buf[0] =
nbytes;
381 nwrite = rle_compress(compressed_buf + 1, work_buf + 1, n,
nbytes);
383 nwrite =
G_compress(work_buf + 1, total, compressed_buf + 1, cmax,
393 if (write(fcb->
data_fd, compressed_buf, nwrite) != nwrite)
395 _(
"Error writing compressed data for row %d of <%s>: %s"),
396 row, fcb->
name, strerror(errno));
400 if (write(fcb->
data_fd, work_buf, nwrite) != nwrite)
402 _(
"Error writing compressed data for row %d of <%s>: %s"),
403 row, fcb->
name, strerror(errno));
411 if (write(fcb->
data_fd, work_buf, nwrite) != nwrite)
413 _(
"Error writing uncompressed data for row %d of <%s>: %s"),
414 row, fcb->
name, strerror(errno));
420 static void put_data_gdal(
int fd,
const void *rast,
int row,
int n,
428 void *work_buf, *
dst;
429 GDALDataType datatype;
441 datatype = GDT_Unknown;
444 datatype = GDT_Int32;
447 datatype = GDT_Float32;
450 datatype = GDT_Float64;
457 for (i = 0; i < n; i++) {
459 (zeros_r_nulls && !*(
CELL *)
src))
468 n, 1, datatype, 0, 0);
473 G_fatal_error(
_(
"Error writing data via GDAL for row %d of <%s>"), row,
478 static void put_raster_data(
int fd,
char *null_buf,
const void *rast,
int row,
484 put_data_gdal(fd, rast, row, n, zeros_r_nulls,
map_type);
486 put_data(fd, null_buf, rast, row, n, zeros_r_nulls);
488 put_fp_data(fd, null_buf, rast, row, n,
map_type);
491 static void put_null_value_row(
int fd,
const char *flags)
497 _(
"GDAL output doesn't support writing null rows separately"));
507 static void write_null_bits_compressed(
const unsigned char *flags,
int row,
511 unsigned char *compressed_buf;
523 nwrite =
G_compress((
unsigned char *)flags, size, compressed_buf, cmax, 3);
525 if (nwrite > 0 && (
size_t)nwrite < size) {
526 if ((res = write(fcb->
null_fd, compressed_buf, nwrite)) < 0 ||
527 (
unsigned int)res != nwrite)
529 _(
"Error writing compressed null data for row %d of <%s>: %s"),
530 row, fcb->
name, strerror(errno));
533 if ((res = write(fcb->
null_fd, flags, size)) < 0 ||
534 (
unsigned int)res != size)
536 _(
"Error writing compressed null data for row %d of <%s>: %s"),
537 row, fcb->
name, strerror(errno));
564 write_null_bits_compressed(flags, row, size, fd);
568 offset = (off_t)size * row;
570 if (lseek(fcb->
null_fd, offset, SEEK_SET) < 0)
573 if ((res = write(fcb->
null_fd, flags, size)) < 0 ||
574 (
unsigned int)res != size)
576 fcb->
name, strerror(errno));
579 static void convert_and_write_if(
int fd,
const void *vbuf)
581 const CELL *buf = vbuf;
590 p[i] = (
FCELL)buf[i];
595 static void convert_and_write_df(
int fd,
const void *vbuf)
597 const DCELL *buf = vbuf;
606 p[i] = (
FCELL)buf[i];
611 static void convert_and_write_id(
int fd,
const void *vbuf)
613 const CELL *buf = vbuf;
622 p[i] = (
DCELL)buf[i];
627 static void convert_and_write_fd(
int fd,
const void *vbuf)
629 const FCELL *buf = vbuf;
638 p[i] = (
DCELL)buf[i];
643 static void convert_and_write_fi(
int fd,
const void *vbuf)
645 const FCELL *buf = vbuf;
659 static void convert_and_write_di(
int fd,
const void *vbuf)
661 const DCELL *buf = vbuf;
688 static void put_raster_row(
int fd,
const void *buf,
RASTER_MAP_TYPE data_type,
691 static void (*convert_and_write_FtypeOtype[3][3])(int,
const void *) = {
692 {
NULL, convert_and_write_if, convert_and_write_id},
693 {convert_and_write_fi,
NULL, convert_and_write_fd},
694 {convert_and_write_di, convert_and_write_df,
NULL}};
700 G_fatal_error(
_(
"put_raster_row: raster map <%s> not open for write - "
709 _(
"put_raster_row: unopened file descriptor - request ignored"));
714 convert_and_write_FtypeOtype[data_type][fcb->
map_type](fd, buf);
722 zeros_r_nulls, data_type);
739 put_null_value_row(fd, null_buf);
#define OPEN_NEW_COMPRESSED
#define OPEN_NEW_UNCOMPRESSED
CPLErr Rast_gdal_raster_IO(GDALRasterBandH, GDALRWFlag, int, int, int, int, void *, int, int, GDALDataType, int, int)
struct compressor_list compressor[]
void G_xdr_put_float(void *, const float *)
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
int G_compress(unsigned char *, int, unsigned char *, int, int)
void G_xdr_put_double(void *, const double *)
int G_write_compressed(int, unsigned char *, int, int)
#define G_incr_void_ptr(ptr, size)
int G_compress_bound(int, int)
int Rast_is_null_value(const void *, RASTER_MAP_TYPE)
To check if a raster value is set to NULL.
void Rast__row_update_range(const CELL *, int, struct Range *, int)
Update range structure based on raster row.
int Rast__null_bitstream_size(int)
Determines null bitstream size.
#define Rast_is_f_null_value(fcellVal)
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
void Rast_set_f_null_value(FCELL *, int)
To set a number of FCELL raster values to NULL.
void Rast__convert_01_flags(const char *, unsigned char *, int)
?
void Rast_set_d_value(void *, DCELL, RASTER_MAP_TYPE)
Places a DCELL raster value.
void Rast_set_c_null_value(CELL *, int)
To set a number of CELL raster values to NULL.
int Rast_update_cell_stats(const CELL *, int, struct Cell_stats *)
Add data to cell stats.
size_t Rast_cell_size(RASTER_MAP_TYPE)
Returns size of a raster cell in bytes.
#define Rast_is_d_null_value(dcellVal)
#define Rast_is_c_null_value(cellVal)
void Rast_row_update_fp_range(const void *, int, struct FPRange *, RASTER_MAP_TYPE)
Update range structure based on raster row (floating-point)
void Rast_put_d_row(int fd, const DCELL *buf)
Writes the next row for dcell file (DCELL version)
void Rast_put_row(int fd, const void *buf, RASTER_MAP_TYPE data_type)
Writes the next row for cell/fcell/dcell file.
void Rast__write_null_bits(int fd, const unsigned char *flags)
Write null data.
void Rast_put_f_row(int fd, const FCELL *buf)
Writes the next row for fcell file (FCELL version)
void Rast_put_c_row(int fd, const CELL *buf)
Writes the next row for cell file (CELL version)
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
unsigned char * null_bits
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)