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)
121 G_fatal_error(
_(
"Error writing uncompressed FP data for row %d of <%s>: %s"),
122 row, fcb->
name, strerror(errno));
125 static void write_data_compressed(
int fd,
int row,
unsigned char *buf,
int n,
int compressor)
128 int nwrite = fcb->
nbytes * n;
131 G_fatal_error(
_(
"Error writing compressed FP data for row %d of <%s>: %s"),
132 row, fcb->
name, strerror(errno));
135 static void set_file_pointer(
int fd,
int row)
142 static void convert_float(
float *work_buf,
int size,
char *null_buf,
143 const FCELL *rast,
int row,
int n)
147 for (i = 0; i < n; i++) {
162 static void convert_double(
double *work_buf,
int size,
char *null_buf,
163 const DCELL *rast,
int row,
int n)
167 for (i = 0; i < n; i++) {
183 static void put_fp_data(
int fd,
char *null_buf,
const void *rast,
200 set_file_pointer(fd, row);
203 convert_float(work_buf, size, null_buf, rast, row, n);
205 convert_double(work_buf, size, null_buf, rast, row, n);
210 write_data(fd, row, work_buf, n);
215 static void convert_int(
unsigned char *wk,
char *null_buf,
const CELL * rast,
216 int n,
int len,
int zeros_r_nulls)
222 for (i = 0; i < n; i++) {
232 else if (zeros_r_nulls && !v)
244 for (k = len - 1; k >= 0; k--) {
257 static int count_bytes(
const unsigned char *wk,
int n,
int len)
261 for (i = 0; i < len - 1; i++)
262 for (j = 0; j < n; j++)
263 if (wk[j * len + i] != 0)
269 static void trim_bytes(
unsigned char *wk,
int n,
int slen,
int trim)
271 unsigned char *wk2 = wk;
274 for (i = 0; i < n; i++) {
275 for (j = 0; j < trim; j++)
277 for (; j < slen; j++)
282 static int same(
const unsigned char *
x,
const unsigned char *y,
int n)
284 return (memcmp(
x, y, n) == 0);
287 static int count_run(
const unsigned char *src,
int n,
int nbytes)
289 const unsigned char *cur = src +
nbytes;
292 for (i = 1; i < n; i++) {
293 if (i == 255 || !same(cur, src, nbytes))
302 static int rle_compress(
unsigned char *
dst,
unsigned char *src,
int n,
306 int total = nbytes * n;
311 nwrite += nbytes + 1;
315 count = count_run(src, n, nbytes);
318 memcpy(
dst, src, nbytes);
325 return (nwrite >= total) ? 0 : nwrite;
328 static void put_data(
int fd,
char *null_buf,
const CELL * cell,
329 int row,
int n,
int zeros_r_nulls)
333 int len = compressed ?
sizeof(
CELL) : fcb->
nbytes;
334 unsigned char *work_buf, *wk;
347 set_file_pointer(fd, row);
352 convert_int(wk, null_buf, cell, n, len, zeros_r_nulls);
355 int nbytes = count_bytes(wk, n, len);
356 unsigned char *compressed_buf;
364 trim_bytes(wk, n, len, len - nbytes);
372 compressed_buf =
G_malloc(cmax + 1);
374 compressed_buf[0] = work_buf[0] =
nbytes;
378 nwrite = rle_compress(compressed_buf + 1, work_buf + 1, n, nbytes);
380 nwrite =
G_compress(work_buf + 1, total, compressed_buf + 1, cmax,
390 if (write(fcb->
data_fd, compressed_buf, nwrite) != nwrite)
391 G_fatal_error(
_(
"Error writing compressed data for row %d of <%s>: %s"),
392 row, fcb->
name, strerror(errno));
395 nwrite = nbytes * n + 1;
396 if (write(fcb->
data_fd, work_buf, nwrite) != nwrite)
397 G_fatal_error(
_(
"Error writing compressed data for row %d of <%s>: %s"),
398 row, fcb->
name, strerror(errno));
406 if (write(fcb->
data_fd, work_buf, nwrite) != nwrite)
407 G_fatal_error(
_(
"Error writing uncompressed data for row %d of <%s>: %s"),
408 row, fcb->
name, strerror(errno));
414 static void put_data_gdal(
int fd,
const void *rast,
int row,
int n,
422 void *work_buf, *
dst;
423 GDALDataType datatype;
435 datatype = GDT_Unknown;
438 datatype = GDT_Int32;
441 datatype = GDT_Float32;
444 datatype = GDT_Float64;
451 for (i = 0; i < n; i++) {
453 (zeros_r_nulls && !*(
CELL *) src))
456 memcpy(dst, src, size);
462 work_buf, n, 1, datatype, 0, 0);
467 G_fatal_error(
_(
"Error writing data via GDAL for row %d of <%s>"),
472 static void put_raster_data(
int fd,
char *null_buf,
const void *rast,
479 put_data_gdal(fd, rast, row, n, zeros_r_nulls, map_type);
481 put_data(fd, null_buf, rast, row, n, zeros_r_nulls);
483 put_fp_data(fd, null_buf, rast, row, n, map_type);
486 static void put_null_value_row(
int fd,
const char *flags)
491 G_fatal_error(
_(
"GDAL output doesn't support writing null rows separately"));
502 static void write_null_bits_compressed(
const unsigned char *flags,
503 int row,
size_t size,
int fd)
506 unsigned char *compressed_buf;
517 nwrite =
G_compress((
unsigned char *)flags, size, compressed_buf, cmax, 3);
519 if (nwrite > 0 && nwrite < size) {
520 if (write(fcb->
null_fd, compressed_buf, nwrite) != nwrite)
521 G_fatal_error(
_(
"Error writing compressed null data for row %d of <%s>: %s"),
522 row, fcb->
name, strerror(errno));
525 if (write(fcb->
null_fd, flags, size) != size)
526 G_fatal_error(
_(
"Error writing compressed null data for row %d of <%s>: %s"),
527 row, fcb->
name, strerror(errno));
553 write_null_bits_compressed(flags, row, size, fd);
557 offset = (off_t) size * row;
559 if (lseek(fcb->
null_fd, offset, SEEK_SET) < 0)
563 if (write(fcb->
null_fd, flags, size) != size)
565 row, fcb->
name, strerror(errno));
568 static void convert_and_write_if(
int fd,
const void *vbuf)
570 const CELL *buf = vbuf;
579 p[i] = (
FCELL) buf[i];
584 static void convert_and_write_df(
int fd,
const void *vbuf)
586 const DCELL *buf = vbuf;
595 p[i] = (
FCELL) buf[i];
600 static void convert_and_write_id(
int fd,
const void *vbuf)
602 const CELL *buf = vbuf;
611 p[i] = (
DCELL) buf[i];
616 static void convert_and_write_fd(
int fd,
const void *vbuf)
618 const FCELL *buf = vbuf;
627 p[i] = (
DCELL) buf[i];
632 static void convert_and_write_fi(
int fd,
const void *vbuf)
634 const FCELL *buf = vbuf;
643 p[i] = (
CELL) buf[i];
648 static void convert_and_write_di(
int fd,
const void *vbuf)
650 const DCELL *buf = vbuf;
659 p[i] = (
CELL) buf[i];
677 static void put_raster_row(
int fd,
const void *buf,
RASTER_MAP_TYPE data_type,
680 static void (*convert_and_write_FtypeOtype[3][3])(int,
const void *) = {
681 {
NULL, convert_and_write_if, convert_and_write_id},
682 {convert_and_write_fi,
NULL, convert_and_write_fd},
683 {convert_and_write_di, convert_and_write_df,
NULL}
690 G_fatal_error(
_(
"put_raster_row: raster map <%s> not open for write - request ignored"),
697 G_fatal_error(
_(
"put_raster_row: unopened file descriptor - request ignored"));
702 convert_and_write_FtypeOtype[data_type][fcb->
map_type](fd, buf);
710 zeros_r_nulls, data_type);
727 put_null_value_row(fd, null_buf);
#define OPEN_NEW_COMPRESSED
void Rast__write_null_bits(int fd, const unsigned char *flags)
Write null data.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
struct compressor_list compressor[]
int G_write_compressed(int, unsigned char *, int, int)
void Rast_set_c_null_value(CELL *, int)
To set a number of CELL raster values to NULL.
void Rast_put_c_row(int fd, const CELL *buf)
Writes the next row for cell file (CELL version)
CPLErr Rast_gdal_raster_IO(GDALRasterBandH, GDALRWFlag, int, int, int, int, void *, int, int, GDALDataType, int, int)
#define Rast_is_d_null_value(dcellVal)
int G_compress_bound(int, int)
void G_free(void *)
Free allocated memory.
void Rast__row_update_range(const CELL *, int, struct Range *, int)
Update range structure based on raster row.
#define G_incr_void_ptr(ptr, size)
#define Rast_is_f_null_value(fcellVal)
unsigned char * null_bits
#define OPEN_NEW_UNCOMPRESSED
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
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.
int G_compress(unsigned char *, int, unsigned char *, int, int)
void G_xdr_put_double(void *, const double *)
void G_xdr_put_float(void *, const float *)
void Rast_set_f_null_value(FCELL *, int)
To set a number of FCELL raster values to NULL.
void Rast_put_f_row(int fd, const FCELL *buf)
Writes the next row for fcell file (FCELL version)
struct fileinfo * fileinfo
int Rast_update_cell_stats(const CELL *, int, struct Cell_stats *)
Add data to cell stats.
void Rast_put_d_row(int fd, const DCELL *buf)
Writes the next row for dcell file (DCELL version)
void Rast_set_d_value(void *, DCELL, RASTER_MAP_TYPE)
Places a DCELL raster value.
void Rast_row_update_fp_range(const void *, int, struct FPRange *, RASTER_MAP_TYPE)
Update range structure based on raster row (floating-point)
int cols
Number of columns for 2D data.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void Rast__convert_01_flags(const char *, unsigned char *, int)
?
void Rast_put_row(int fd, const void *buf, RASTER_MAP_TYPE data_type)
Writes the next row for cell/fcell/dcell file.
int rows
Number of rows for 2D data.
#define Rast_is_c_null_value(cellVal)
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
int Rast_is_null_value(const void *, RASTER_MAP_TYPE)
To check if a raster value is set to NULL.