PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ rt_raster_generate_new_band()

int rt_raster_generate_new_band ( rt_raster  raster,
rt_pixtype  pixtype,
double  initialvalue,
uint32_t  hasnodata,
double  nodatavalue,
int  index 
)

Generate a new inline band and add it to a raster.

Parameters
raster: the raster to add a band to
pixtype: the pixel type for the new band
initialvalue: initial value for pixels
hasnodata: indicates if the band has a nodata value
nodatavalue: nodata value for the new band
index: position to add the new band in the raster
Returns
identifier (position) for the just-added raster, or -1 on error

Memory is allocated in this function for band data.

Parameters
raster: the raster to add a band to
pixtype: the pixel type for the new band
initialvalue: initial value for pixels
hasnodata: indicates if the band has a nodata value
nodatavalue: nodata value for the new band
index: position to add the new band in the raster
Returns
identifier (position) for the just-added raster, or -1 on error

Definition at line 485 of file rt_raster.c.

489 {
490 rt_band band = NULL;
491 int width = 0;
492 int height = 0;
493 int numval = 0;
494 int datasize = 0;
495 int oldnumbands = 0;
496 int numbands = 0;
497 void * mem = NULL;
498 int32_t checkvalint = 0;
499 uint32_t checkvaluint = 0;
500 double checkvaldouble = 0;
501 float checkvalfloat = 0;
502 int i;
503
504
505 assert(NULL != raster);
506
507 /* Make sure index is in a valid range */
508 oldnumbands = rt_raster_get_num_bands(raster);
509 if (index < 0)
510 index = 0;
511 else if (index > oldnumbands + 1)
512 index = oldnumbands + 1;
513
514 /* Determine size of memory block to allocate and allocate it */
515 width = rt_raster_get_width(raster);
516 height = rt_raster_get_height(raster);
517 numval = width * height;
518 datasize = rt_pixtype_size(pixtype) * numval;
519
520 mem = (int *)rtalloc(datasize);
521 if (!mem) {
522 rterror("rt_raster_generate_new_band: Could not allocate memory for band");
523 return -1;
524 }
525
526 if (FLT_EQ(initialvalue, 0.0))
527 memset(mem, 0, datasize);
528 else {
529 switch (pixtype)
530 {
531 case PT_1BB:
532 {
533 uint8_t *ptr = mem;
534 uint8_t clamped_initval = rt_util_clamp_to_1BB(initialvalue);
535 for (i = 0; i < numval; i++)
536 ptr[i] = clamped_initval;
537 checkvalint = ptr[0];
538 break;
539 }
540 case PT_2BUI:
541 {
542 uint8_t *ptr = mem;
543 uint8_t clamped_initval = rt_util_clamp_to_2BUI(initialvalue);
544 for (i = 0; i < numval; i++)
545 ptr[i] = clamped_initval;
546 checkvalint = ptr[0];
547 break;
548 }
549 case PT_4BUI:
550 {
551 uint8_t *ptr = mem;
552 uint8_t clamped_initval = rt_util_clamp_to_4BUI(initialvalue);
553 for (i = 0; i < numval; i++)
554 ptr[i] = clamped_initval;
555 checkvalint = ptr[0];
556 break;
557 }
558 case PT_8BSI:
559 {
560 int8_t *ptr = mem;
561 int8_t clamped_initval = rt_util_clamp_to_8BSI(initialvalue);
562 for (i = 0; i < numval; i++)
563 ptr[i] = clamped_initval;
564 checkvalint = ptr[0];
565 break;
566 }
567 case PT_8BUI:
568 {
569 uint8_t *ptr = mem;
570 uint8_t clamped_initval = rt_util_clamp_to_8BUI(initialvalue);
571 for (i = 0; i < numval; i++)
572 ptr[i] = clamped_initval;
573 checkvalint = ptr[0];
574 break;
575 }
576 case PT_16BSI:
577 {
578 int16_t *ptr = mem;
579 int16_t clamped_initval = rt_util_clamp_to_16BSI(initialvalue);
580 for (i = 0; i < numval; i++)
581 ptr[i] = clamped_initval;
582 checkvalint = ptr[0];
583 break;
584 }
585 case PT_16BUI:
586 {
587 uint16_t *ptr = mem;
588 uint16_t clamped_initval = rt_util_clamp_to_16BUI(initialvalue);
589 for (i = 0; i < numval; i++)
590 ptr[i] = clamped_initval;
591 checkvalint = ptr[0];
592 break;
593 }
594 case PT_32BSI:
595 {
596 int32_t *ptr = mem;
597 int32_t clamped_initval = rt_util_clamp_to_32BSI(initialvalue);
598 for (i = 0; i < numval; i++)
599 ptr[i] = clamped_initval;
600 checkvalint = ptr[0];
601 break;
602 }
603 case PT_32BUI:
604 {
605 uint32_t *ptr = mem;
606 uint32_t clamped_initval = rt_util_clamp_to_32BUI(initialvalue);
607 for (i = 0; i < numval; i++)
608 ptr[i] = clamped_initval;
609 checkvaluint = ptr[0];
610 break;
611 }
612 case PT_32BF:
613 {
614 float *ptr = mem;
615 float clamped_initval = rt_util_clamp_to_32F(initialvalue);
616 for (i = 0; i < numval; i++)
617 ptr[i] = clamped_initval;
618 checkvalfloat = ptr[0];
619 break;
620 }
621 case PT_64BF:
622 {
623 double *ptr = mem;
624 for (i = 0; i < numval; i++)
625 ptr[i] = initialvalue;
626 checkvaldouble = ptr[0];
627 break;
628 }
629 default:
630 {
631 rterror("rt_raster_generate_new_band: Unknown pixeltype %d", pixtype);
632 rtdealloc(mem);
633 return -1;
634 }
635 }
636 }
637
638 /* Overflow checking */
640 initialvalue,
641 checkvalint, checkvaluint,
642 checkvalfloat, checkvaldouble,
643 pixtype
644 );
645
646 band = rt_band_new_inline(width, height, pixtype, hasnodata, nodatavalue, mem);
647 if (! band) {
648 rterror("rt_raster_generate_new_band: Could not add band to raster. Aborting");
649 rtdealloc(mem);
650 return -1;
651 }
652 rt_band_set_ownsdata_flag(band, 1); /* we DO own this data!!! */
653 index = rt_raster_add_band(raster, band, index);
654 numbands = rt_raster_get_num_bands(raster);
655 if (numbands == oldnumbands || index == -1) {
656 rterror("rt_raster_generate_new_band: Could not add band to raster. Aborting");
657 rt_band_destroy(band);
658 }
659
660 /* set isnodata if hasnodata = TRUE and initial value = nodatavalue */
661 if (hasnodata && FLT_EQ(initialvalue, nodatavalue))
663
664 return index;
665}
rt_band rt_band_new_inline(uint16_t width, uint16_t height, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, uint8_t *data)
Create an in-db rt_band with no data.
Definition rt_band.c:63
void rt_band_set_ownsdata_flag(rt_band band, int flag)
Definition rt_band.c:667
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition rt_context.c:199
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition rt_context.c:171
rt_errorstate rt_band_set_isnodata_flag(rt_band band, int flag)
Set isnodata flag value.
Definition rt_band.c:695
int8_t rt_util_clamp_to_8BSI(double value)
Definition rt_util.c:49
uint8_t rt_util_clamp_to_1BB(double value)
Definition rt_util.c:34
int32_t rt_util_clamp_to_32BSI(double value)
Definition rt_util.c:69
@ PT_32BUI
Definition librtcore.h:194
@ PT_2BUI
Definition librtcore.h:187
@ PT_32BSI
Definition librtcore.h:193
@ PT_4BUI
Definition librtcore.h:188
@ PT_32BF
Definition librtcore.h:195
@ PT_1BB
Definition librtcore.h:186
@ PT_16BUI
Definition librtcore.h:192
@ PT_8BSI
Definition librtcore.h:189
@ PT_16BSI
Definition librtcore.h:191
@ PT_64BF
Definition librtcore.h:196
@ PT_8BUI
Definition librtcore.h:190
int rt_util_dbl_trunc_warning(double initialvalue, int32_t checkvalint, uint32_t checkvaluint, float checkvalfloat, double checkvaldouble, rt_pixtype pixtype)
Definition rt_util.c:629
#define FLT_EQ(x, y)
Definition librtcore.h:2235
uint8_t rt_util_clamp_to_2BUI(double value)
Definition rt_util.c:39
uint8_t rt_util_clamp_to_8BUI(double value)
Definition rt_util.c:54
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition rt_band.c:340
int16_t rt_util_clamp_to_16BSI(double value)
Definition rt_util.c:59
uint8_t rt_util_clamp_to_4BUI(double value)
Definition rt_util.c:44
void rtdealloc(void *mem)
Definition rt_context.c:186
uint16_t rt_util_clamp_to_16BUI(double value)
Definition rt_util.c:64
uint32_t rt_util_clamp_to_32BUI(double value)
Definition rt_util.c:74
float rt_util_clamp_to_32F(double value)
Definition rt_util.c:79
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition rt_pixel.c:39
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
Definition rt_raster.c:405
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition rt_raster.c:372
uint16_t rt_raster_get_height(rt_raster raster)
Definition rt_raster.c:129
uint16_t rt_raster_get_width(rt_raster raster)
Definition rt_raster.c:121

References FLT_EQ, PT_16BSI, PT_16BUI, PT_1BB, PT_2BUI, PT_32BF, PT_32BSI, PT_32BUI, PT_4BUI, PT_64BF, PT_8BSI, PT_8BUI, rt_band_destroy(), rt_band_new_inline(), rt_band_set_isnodata_flag(), rt_band_set_ownsdata_flag(), rt_pixtype_size(), rt_raster_add_band(), rt_raster_get_height(), rt_raster_get_num_bands(), rt_raster_get_width(), rt_util_clamp_to_16BSI(), rt_util_clamp_to_16BUI(), rt_util_clamp_to_1BB(), rt_util_clamp_to_2BUI(), rt_util_clamp_to_32BSI(), rt_util_clamp_to_32BUI(), rt_util_clamp_to_32F(), rt_util_clamp_to_4BUI(), rt_util_clamp_to_8BSI(), rt_util_clamp_to_8BUI(), rt_util_dbl_trunc_warning(), rtalloc(), rtdealloc(), and rterror().

Referenced by RASTER_addBand(), RASTER_clip(), RASTER_mapAlgebra2(), RASTER_mapAlgebraExpr(), RASTER_mapAlgebraFct(), RASTER_mapAlgebraFctNgb(), RASTER_tile(), RASTER_union_transfn(), rt_raster_from_gdal_dataset(), and rt_raster_iterator().

Here is the call graph for this function:
Here is the caller graph for this function: