Skip to content

Commit

Permalink
Update grdimage.c
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWessel committed Dec 27, 2023
1 parent d1f3a73 commit 1ec6041
Showing 1 changed file with 41 additions and 38 deletions.
79 changes: 41 additions & 38 deletions src/grdimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ struct GRDIMAGE_CTRL {
struct GRDIMAGE_N { /* -N */
bool active;
} N;
struct GRDIMAGE_Q { /* -Q[r/g/b][+z<value>] */
struct GRDIMAGE_Q { /* -Q[r/g/b][+i][+z<value>] */
bool active;
bool transp_color; /* true if a color was given */
bool z_given; /* true if a z-value was given */
double rgb[4]; /* Pixel value for transparency in images */
double value; /* If +z is used this z-value will give us the r/g/b via CPT */
bool z_given; /* true if a z-value was given */
bool mask_color; /* true if a z-value was given */
bool invert; /* If true we turn transparency = 1 - transparency [i.e., opacity] */
double rgb[4]; /* Pixel value for transparency in images */
double value; /* If +z is used this z-value will give us the r/g/b via CPT */
} Q;
struct GRDIMAGE_T { /* -T[s][o[<pen>]] */
bool active;
Expand All @@ -115,15 +117,15 @@ struct GRDIMAGE_CTRL {
struct GRDIMAGE_CONF {
/* Configuration structure for things to pass around to sub-functions */
unsigned int colormask_offset; /* Either 0 or 3 depending on -Q */
bool int_mode; /* true if we are to apply illumination */
unsigned int int_mode; /* 2 if we are to apply illumination (grid) or 1 (constant) */
unsigned int *actual_row; /* Array with actual rows as a function of pseudo row */
unsigned int *actual_col; /* Array of actual columns as a function of pseudo col */
int64_t n_columns, n_rows; /* Signed dimensions for use in OpenMP loops (if implemented) */
uint64_t nm; /* Number of pixels in the image */
int64_t n_columns, n_rows; /* Signed dimensions for use in OpenMP loops (if implemented) */
uint64_t nm; /* Number of pixels in the image */
struct GMT_PALETTE *P; /* Pointer to the active palette [NULL if image] */
struct GMT_GRID *Grid; /* Pointer to the active grid [NULL if image] */
struct GMT_GRID *Intens; /* Pointer to the active intensity grid [NULL if no intensity] */
struct GMT_IMAGE *Image; /* Pointer to the active image [NUYLL if grid] */
struct GMT_IMAGE *Image; /* Pointer to the active image [NULL if grid] */
};

static void *New_Ctrl (struct GMT_CTRL *GMT) { /* Allocate and initialize a new control structure */
Expand All @@ -146,6 +148,7 @@ static void Free_Ctrl (struct GMT_CTRL *GMT, struct GRDIMAGE_CTRL *C) { /* Deall
gmt_M_str_free (C->In.file);
gmt_M_str_free (C->A.file);
gmt_M_str_free (C->C.file);
gmt_M_str_free (C->C.savecpt);
gmt_M_str_free (C->I.file);
gmt_M_str_free (C->I.azimuth);
gmt_M_str_free (C->I.ambient);
Expand Down Expand Up @@ -410,7 +413,7 @@ static int parse (struct GMT_CTRL *GMT, struct GRDIMAGE_CTRL *Ctrl, struct GMT_O
n_errors += gmt_M_repeated_module_option (API, Ctrl->N.active);
n_errors += gmt_get_no_argument (GMT, opt->arg, opt->option, 0);
break;
case 'Q': /* PS3 colormasking */
case 'Q': /* PS3 colormasking -Q[<color>][+i][+z<value>] */
n_errors += gmt_M_repeated_module_option (API, Ctrl->Q.active);
if ((c = strstr (opt->arg, "+z"))) { /* Gave a z-value */
if (c[2]) {
Expand All @@ -421,7 +424,7 @@ static int parse (struct GMT_CTRL *GMT, struct GRDIMAGE_CTRL *Ctrl, struct GMT_O
GMT_Report (API, GMT_MSG_ERROR, "Option -Q: The +z modifier requires a valid z-value\n");
n_errors++;
}
c[0] = '\0'; /* Chop off modifier */
c[0] = '\0'; /* Chop off modifiers */
}
if (opt->arg[0]) { /* Change input image transparency pixel color */
if (gmt_getrgb (GMT, opt->arg, Ctrl->Q.rgb)) { /* Change input image transparency pixel color */
Expand Down Expand Up @@ -499,7 +502,7 @@ static int parse (struct GMT_CTRL *GMT, struct GRDIMAGE_CTRL *Ctrl, struct GMT_O
n_errors += gmt_M_check_condition (GMT, Ctrl->G.rgb[GMT_FGD][0] < 0 && Ctrl->G.rgb[GMT_BGD][0] < 0,
"Option -G: Only one of fore/back-ground can be transparent for 1-bit images\n");
n_errors += gmt_M_check_condition (GMT, Ctrl->M.active && Ctrl->Q.active,
"SOption -Q: Cannot use -M when doing colormasking\n");
"Option -Q: Cannot use -M when doing colormasking\n");
n_errors += gmt_M_check_condition (GMT, Ctrl->E.device_dpi && Ctrl->Q.active,
"Option -Q: Cannot use -Ei when doing colormasking\n");
n_errors += gmt_M_check_condition (GMT, Ctrl->A.return_image && Ctrl->Out.file == NULL,
Expand All @@ -509,7 +512,7 @@ static int parse (struct GMT_CTRL *GMT, struct GRDIMAGE_CTRL *Ctrl, struct GMT_O
n_errors += gmt_M_check_condition (GMT, Ctrl->A.active && (GMT->common.B.active[GMT_PRIMARY] || GMT->common.B.active[GMT_SECONDARY]),
"Option -A: Cannot draw base frame (-B) when creating just a raster image\n");
n_errors += gmt_M_check_condition (GMT, Ctrl->Q.transp_color && Ctrl->Q.z_given,
"Option Q: Cannot both specify a r/g/b and a z-value\n");
"Option Q: Cannot both specify a r/g/b and a grid z-value\n");
return (n_errors ? GMT_PARSE_ERROR : GMT_NOERROR);
}

Expand Down Expand Up @@ -731,8 +734,8 @@ GMT_LOCAL void grdimage_grd_gray_with_intensity (struct GMT_CTRL *GMT, struct GR
uint64_t byte, kk_s, kk_i, node_s, node_i;
double rgb[4] = {0.0, 0.0, 0.0, 0.0};
struct GMT_GRID_HEADER *H_s = Conf->Grid->header; /* Pointer to the active data header */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */
bool different = (Conf->int_mode && !gmt_M_grd_same_region (GMT,Conf->Grid,Conf->Intens)); /* True of they differ in padding */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode == 2) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */
bool different = (Conf->int_mode == 2 && !gmt_M_grd_same_region (GMT,Conf->Grid,Conf->Intens)); /* True of they differ in padding */

GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "Basic z(x,y) -> gray image with illumination.\n");
#ifdef _OPENMP
Expand All @@ -746,11 +749,11 @@ GMT_LOCAL void grdimage_grd_gray_with_intensity (struct GMT_CTRL *GMT, struct GR
node_s = kk_s + Conf->actual_col[scol]; /* Current grid node */
index = gmt_get_rgb_from_z (GMT, Conf->P, Conf->Grid->data[node_s], rgb);
if (index != GRDIMAGE_NAN_INDEX) { /* Add in the effect of illumination */
if (Conf->int_mode) { /* Intensity value comes from a co-registered grid */
if (Conf->int_mode == 2) { /* Intensity value comes from a co-registered grid */
node_i = kk_i + Conf->actual_col[scol]; /* Current intensity node */
gmt_illuminate (GMT, Conf->Intens->data[node_i], rgb);
}
else /* A constant (ambient) intensity was given via -I */
else if (Conf->int_mode == 1) /* A constant (ambient) intensity was given via -I */
gmt_illuminate (GMT, Ctrl->I.value, rgb);
}
image[byte++] = gmt_M_u255 (rgb[0]); /* Color table only has grays, just use r since r = g = b here */
Expand Down Expand Up @@ -788,8 +791,8 @@ GMT_LOCAL void grdimage_grd_c2s_with_intensity (struct GMT_CTRL *GMT, struct GRD
uint64_t byte, kk_s, kk_i, node_s, node_i;
double rgb[4] = {0.0, 0.0, 0.0, 0.0};
struct GMT_GRID_HEADER *H_s = Conf->Grid->header; /* Pointer to the active data header */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */
bool different = (Conf->int_mode && !gmt_M_grd_same_region (GMT,Conf->Grid,Conf->Intens)); /* True of they differ in padding */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode == 2) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */
bool different = (Conf->int_mode == 2 && !gmt_M_grd_same_region (GMT,Conf->Grid,Conf->Intens)); /* True of they differ in padding */

GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "Basic z(x,y) -> color image with no illumination.\n");
#ifdef _OPENMP
Expand All @@ -803,11 +806,11 @@ GMT_LOCAL void grdimage_grd_c2s_with_intensity (struct GMT_CTRL *GMT, struct GRD
node_s = kk_s + Conf->actual_col[scol]; /* Current data grid node */
index = gmt_get_rgb_from_z (GMT, Conf->P, Conf->Grid->data[node_s], rgb);
if (index != GRDIMAGE_NAN_INDEX) { /* Deal with illumination */
if (Conf->int_mode) { /* Intensity value comes from the grid */
if (Conf->int_mode == 2) { /* Intensity value comes from the grid */
node_i = kk_i + Conf->actual_col[scol]; /* Current intensity grid node */
gmt_illuminate (GMT, Conf->Intens->data[node_i], rgb);
}
else /* A constant (ambient) intensity was given via -I */
else if (Conf->int_mode == 1) /* A constant (ambient) intensity was given via -I */
gmt_illuminate (GMT, Ctrl->I.value, rgb);
}
image[byte++] = gmt_M_u255 (gmt_M_yiq (rgb));
Expand Down Expand Up @@ -872,8 +875,8 @@ GMT_LOCAL void grdimage_grd_color_with_intensity (struct GMT_CTRL *GMT, struct G
uint64_t byte, kk_s, kk_i, node_s, node_i;
double rgb[4] = {0.0, 0.0, 0.0, 0.0};
struct GMT_GRID_HEADER *H_s = Conf->Grid->header; /* Pointer to the active data header */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */
bool different = (Conf->int_mode && !gmt_M_grd_same_region (GMT,Conf->Grid,Conf->Intens)); /* True of they differ in padding */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode == 2) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */
bool different = (Conf->int_mode == 2 && !gmt_M_grd_same_region (GMT,Conf->Grid,Conf->Intens)); /* True of they differ in padding */

GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "Basic z(x,y) -> color image with no illumination.\n");
#ifdef _OPENMP
Expand All @@ -887,11 +890,11 @@ GMT_LOCAL void grdimage_grd_color_with_intensity (struct GMT_CTRL *GMT, struct G
node_s = kk_s + Conf->actual_col[scol]; /* Current grid node */
index = gmt_get_rgb_from_z (GMT, Conf->P, Conf->Grid->data[node_s], rgb);
if (index != GRDIMAGE_NAN_INDEX) { /* Deal with illumination */
if (Conf->int_mode) { /* Intensity value comes from the grid */
if (Conf->int_mode == 2) { /* Intensity value comes from the grid */
node_i = kk_i + Conf->actual_col[scol]; /* Current intensity node */
gmt_illuminate (GMT, Conf->Intens->data[node_i], rgb);
}
else /* A constant (ambient) intensity was given via -I */
else if (Conf->int_mode == 1) /* A constant (ambient) intensity was given via -I */
gmt_illuminate (GMT, Ctrl->I.value, rgb);
}
for (k = 0; k < 3; k++) image[byte++] = gmt_M_u255 (rgb[k]);
Expand All @@ -907,8 +910,8 @@ GMT_LOCAL void grdimage_grd_color_with_intensity_CM (struct GMT_CTRL *GMT, struc
uint64_t byte, kk_s, kk_i, node_s, node_i;
double rgb[4] = {0.0, 0.0, 0.0, 0.0};
struct GMT_GRID_HEADER *H_s = Conf->Grid->header; /* Pointer to the active data header */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */
bool different = (Conf->int_mode && !gmt_M_grd_same_region (GMT,Conf->Grid,Conf->Intens));
struct GMT_GRID_HEADER *H_i = (Conf->int_mode == 2) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */
bool different = (Conf->int_mode == 2 && !gmt_M_grd_same_region (GMT,Conf->Grid,Conf->Intens));

GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "Basic z(x,y) -> color image with no illumination.\n");
/* Determine NaN rgb 0-255 triple ones */
Expand All @@ -926,11 +929,11 @@ GMT_LOCAL void grdimage_grd_color_with_intensity_CM (struct GMT_CTRL *GMT, struc
for (k = 0; k < 3; k++) image[byte++] = n_rgb[k];
}
else { /* Deal with illumination for non-NaN nodes */
if (Conf->int_mode) { /* Intensity value comes from the grid */
if (Conf->int_mode == 2) { /* Intensity value comes from the grid */
node_i = kk_i + Conf->actual_col[scol]; /* Current intensity node */
gmt_illuminate (GMT, Conf->Intens->data[node_i], rgb);
}
else /* A constant (ambient) intensity was given via -I */
else if (Conf->int_mode == 1) /* A constant (ambient) intensity was given via -I */
gmt_illuminate (GMT, Ctrl->I.value, rgb);
for (k = 0; k < 3; k++) i_rgb[k] = image[byte++] = gmt_M_u255 (rgb[k]);
index = (i_rgb[0]*256 + i_rgb[1])*256 + i_rgb[2]; /* The index into the cube for the selected NaN color */
Expand All @@ -956,7 +959,7 @@ GMT_LOCAL void grdimage_img_gray_with_intensity (struct GMT_CTRL *GMT, struct GR
uint64_t byte, kk_s, node_s, node_i;
double rgb[4] = {0.0, 0.0, 0.0, 0.0};
struct GMT_GRID_HEADER *H_s = Conf->Image->header; /* Pointer to the active image header */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode == 2) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */

#ifdef _OPENMP
#pragma omp parallel for private(srow,byte,kk_s,scol,node_s,node_i,rgb) shared(GMT,Conf,Ctrl,H_s,H_i,image)
Expand All @@ -967,11 +970,11 @@ GMT_LOCAL void grdimage_img_gray_with_intensity (struct GMT_CTRL *GMT, struct GR
for (scol = 0; scol < Conf->n_columns; scol++) { /* Compute rgb for each pixel along this scanline */
node_s = kk_s + Conf->actual_col[scol]; /* Start of current pixel node */
rgb[0] = rgb[1] = rgb[2] = gmt_M_is255 (Conf->Image->data[node_s++]);
if (Conf->int_mode) { /* Intensity value comes from the grid, so update node */
if (Conf->int_mode == 2) { /* Intensity value comes from the grid, so update node */
node_i = gmt_M_ijp (H_i, Conf->actual_row[srow], Conf->actual_col[scol]);
gmt_illuminate (GMT, Conf->Intens->data[node_i], rgb); /* Apply illumination to this color */
}
else /* A constant (ambient) intensity was given via -I */
else if (Conf->int_mode == 1) /* A constant (ambient) intensity was given via -I */
gmt_illuminate (GMT, Ctrl->I.value, rgb); /* Apply constant illumination to this color */
image[byte++] = gmt_M_u255 (rgb[0]);
}
Expand Down Expand Up @@ -1043,7 +1046,7 @@ GMT_LOCAL void grdimage_img_c2s_with_intensity (struct GMT_CTRL *GMT, struct GRD
uint64_t byte, kk_s, node_s, node_i;
double rgb[4] = {0.0, 0.0, 0.0, 0.0};
struct GMT_GRID_HEADER *H_s = Conf->Image->header; /* Pointer to the active image header */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode == 2) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */

#ifdef _OPENMP
#pragma omp parallel for private(srow,byte,kk_s,k,scol,node_s,node_i,rgb) shared(GMT,Conf,Ctrl,H_s,H_i,image)
Expand All @@ -1056,11 +1059,11 @@ GMT_LOCAL void grdimage_img_c2s_with_intensity (struct GMT_CTRL *GMT, struct GRD
for (k = 0; k < 3; k++) rgb[k] = gmt_M_is255 (Conf->Image->data[node_s++]);
if (transparency && Conf->Image->data[node_s] < 255) /* Dealing with an image with transparency values less than 255 */
grdimage_img_set_transparency (GMT, Conf->Image->data[node_s], rgb);
if (Conf->int_mode) { /* Intensity value comes from the grid, so update node */
if (Conf->int_mode == 2) { /* Intensity value comes from the grid, so update node */
node_i = gmt_M_ijp (H_i, Conf->actual_row[srow], Conf->actual_col[scol]);
gmt_illuminate (GMT, Conf->Intens->data[node_i], rgb); /* Apply illumination to this color */
}
else /* A constant (ambient) intensity was given via -I */
else if (Conf->int_mode == 1) /* A constant (ambient) intensity was given via -I */
gmt_illuminate (GMT, Ctrl->I.value, rgb); /* Apply constant illumination to this color */
image[byte++] = gmt_M_u255 (gmt_M_yiq (rgb));
}
Expand Down Expand Up @@ -1130,7 +1133,7 @@ GMT_LOCAL void grdimage_img_color_with_intensity (struct GMT_CTRL *GMT, struct G
uint64_t byte, kk_s, node_s, node_i;
double rgb[4] = {0.0, 0.0, 0.0, 0.0};
struct GMT_GRID_HEADER *H_s = Conf->Image->header; /* Pointer to the active image header */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */
struct GMT_GRID_HEADER *H_i = (Conf->int_mode == 2) ? Conf->Intens->header : NULL; /* Pointer to the active intensity header */

#ifdef _OPENMP
#pragma omp parallel for private(srow,byte,kk_s,k,scol,node_s,node_i,rgb) shared(GMT,Conf,Ctrl,H_s,H_i,image)
Expand All @@ -1143,11 +1146,11 @@ GMT_LOCAL void grdimage_img_color_with_intensity (struct GMT_CTRL *GMT, struct G
for (k = 0; k < 3; k++) rgb[k] = gmt_M_is255 (Conf->Image->data[node_s++]);
if (transparency && Conf->Image->data[node_s] < 255) /* Dealing with an image with transparency values less than 255 */
grdimage_img_set_transparency (GMT, Conf->Image->data[node_s], rgb);
if (Conf->int_mode) { /* Intensity value comes from the grid, so update node */
if (Conf->int_mode == 2) { /* Intensity value comes from the grid, so update node */
node_i = gmt_M_ijp (H_i, Conf->actual_row[srow], Conf->actual_col[scol]);
gmt_illuminate (GMT, Conf->Intens->data[node_i], rgb); /* Apply illumination to this color */
}
else /* A constant (ambient) intensity was given via -I */
else if (Conf->int_mode == 1) /* A constant (ambient) intensity was given via -I */
gmt_illuminate (GMT, Ctrl->I.value, rgb); /* Apply constant illumination to this color */
for (k = 0; k < 3; k++) image[byte++] = gmt_M_u255 (rgb[k]); /* Scale up to integer 0-255 range */
}
Expand Down Expand Up @@ -1832,7 +1835,7 @@ EXTERN_MSC int GMT_grdimage (void *V_API, int mode, void *args) {
Conf->Intens = Intens_proj;
Conf->n_columns = header_work->n_columns;
Conf->n_rows = header_work->n_rows;
Conf->int_mode = use_intensity_grid;
Conf->int_mode = (use_intensity_grid) ? 2 : ((Ctrl->I.constant) ? 1 : 0); /* 2 is variable grid intensity, 1 is constant intensity, 0 no intensity */
Conf->nm = header_work->nm;

if (byte_image_no_cmap) { /* Check if we have a nan_value (No data pixel) */
Expand Down

0 comments on commit 1ec6041

Please sign in to comment.