Skip to content

Commit 819c6bc

Browse files
authored
Add new modifier, +i, to -A option. (#8909)
This modifier makes the program only report the high-resolution BoundingBox and end the program. Mostly handy for externals that can thus inquire image size without spending time to rasterize it.
1 parent 2b09780 commit 819c6bc

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

doc/rst/source/psconvert.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Synopsis
1313
.. include:: common_SYN_OPTs.rst_
1414

1515
**gmt psconvert** *psfiles*
16-
[ |-A|\ [**+r**][**+u**] ]
16+
[ |-A|\ [**+i**][**+r**][**+u**] ]
1717
[ |-C|\ *gs_option* ]
1818
[ |-D|\ *outdir* ]
1919
[ |-E|\ *resolution* ]
@@ -65,10 +65,13 @@ Optional Arguments
6565

6666
.. _-A:
6767

68-
**-A**\ [**+r**][**+u**]
68+
**-A**\ [**+i**][**+r**][**+u**]
6969
Adjust the BoundingBox and HiResBoundingBox to the minimum required
7070
by the image content. Two modifiers may be used as well:
7171

72+
- **+i** - Report the adjusted BoundingBox, position in paper and return, that is:
73+
width, height, x0, y0, x1, y1. All in cm. Note that this ignores the effect of the
74+
|-H| and |-I| options, which are applied after the BoundingBox is computed.
7275
- **+r** - We *round* the HighResBoundingBox instead of using the ``ceil`` function.
7376
This is going against Adobe Law but can be useful when creating very small images
7477
where the difference of one pixel might matter. If |-V| is used we also report

src/psconvert.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#define THIS_MODULE_MODERN_NAME "psconvert"
3838
#define THIS_MODULE_LIB "core"
3939
#define THIS_MODULE_PURPOSE "Convert [E]PS file(s) to other formats using Ghostscript"
40-
#define THIS_MODULE_KEYS "<X{+,FI)"
40+
#define THIS_MODULE_KEYS "<X{+,FI),AD)"
4141
#define THIS_MODULE_NEEDS ""
4242
#define THIS_MODULE_OPTIONS "-V"
4343

@@ -97,9 +97,10 @@ struct PSCONVERT_CTRL {
9797
struct PSCONVERT_In { /* Input file info */
9898
unsigned int n_files;
9999
} In;
100-
struct PSCONVERT_A { /* -A[+r][+u] */
100+
struct PSCONVERT_A { /* -A[+i][+r][+u] */
101101
bool active;
102102
bool crop; /* Round HiRes BB instead of ceil (+r) */
103+
bool only_BB; /* Only reports Width/Height of HiRes BB */
103104
bool round; /* Round HiRes BB instead of ceil (+r) */
104105
bool strip; /* Remove the -U time-stamp (+u)*/
105106
bool media; /* If true we must crop to current media size in PS_MEDIA. Only issued indirectly by gmt end */
@@ -259,6 +260,8 @@ GMT_LOCAL int psconvert_parse_new_A_settings (struct GMT_CTRL *GMT, char *arg, s
259260
gmt_M_unused (GMT);
260261
Ctrl->A.crop = true;
261262

263+
if (strstr (arg, "i"))
264+
Ctrl->A.only_BB = true;
262265
if (strstr (arg, "r"))
263266
Ctrl->A.round = true;
264267
if (strstr (arg, "u"))
@@ -447,6 +450,8 @@ GMT_LOCAL int psconvert_parse_A_settings (struct GMT_CTRL *GMT, char *arg, struc
447450
strncat (I_option, &arg[k], j-k); /* Append margin argument */
448451
}
449452
/* These are newer -A modifiers */
453+
if (gmt_get_modifier (arg, 'i', string)) /* Just report the width/height of High-res BB*/
454+
strcat (A_option, "+i");
450455
if (gmt_get_modifier (arg, 'r', string))
451456
strcat (A_option, "+r");
452457
if (gmt_get_modifier (arg, 'u', string))
@@ -667,7 +672,7 @@ static int usage (struct GMTAPI_CTRL *API, int level) {
667672
const char *name = gmt_show_name_and_purpose (API, THIS_MODULE_LIB, THIS_MODULE_CLASSIC_NAME, THIS_MODULE_PURPOSE);
668673
const char *Z = (API->GMT->current.setting.run_mode == GMT_CLASSIC) ? " [-Z]" : "";
669674
if (level == GMT_MODULE_PURPOSE) return (GMT_NOERROR);
670-
GMT_Usage (API, 0, "usage: %s <psfiles> [-A[+r][+u]] [-C<gs_option>] [-D<dir>] [-E<resolution>] "
675+
GMT_Usage (API, 0, "usage: %s <psfiles> [-A[+i][+r][+u]] [-C<gs_option>] [-D<dir>] [-E<resolution>] "
671676
"[-F<out_name>] [-G<gs_path>] [-H<scale>] [-I[+m<margins>][+s[m]<width>[/<height>]][+S<scale>]] [-L<list>] [-Mb|f<psfile>] "
672677
"[-N[+f<fade>][+g<backfill>][+k<fadefill>][+p[<pen>]]] [-P] [-Q[g|p|t]1|2|4] [-S] [-Tb|e|E|f|F|g|G|j|m|s|t[+m][+q<quality>]] [%s] "
673678
"[-W[+a<mode>[<alt>]][+c][+f<minfade>/<maxfade>][+g][+k][+l<lodmin>/<lodmax>][+n<name>][+o<folder>][+t<title>][+u<URL>]]%s [-!]"
@@ -683,8 +688,9 @@ static int usage (struct GMTAPI_CTRL *API, int level) {
683688
if (API->external)
684689
GMT_Usage (API, -2, "Note: To access the current internal GMT plot, specify <psfiles> as \"=\".");
685690
GMT_Message (API, GMT_TIME_NONE, "\n OPTIONAL ARGUMENTS:\n");
686-
GMT_Usage (API, 1, "\n-A[+r][+u]");
691+
GMT_Usage (API, 1, "\n-A[+i][+r][+u]");
687692
GMT_Usage (API, -2, "Adjust the BoundingBox to the minimum required by the image contents. Optional modifiers:");
693+
GMT_Usage (API, 3, "+i Report the adjusted BoundingBox, position in paper and return.");
688694
GMT_Usage (API, 3, "+r Force rounding of HighRes BoundingBox instead of ceil.");
689695
GMT_Usage (API, 3, "+u Strip out time-stamps (produced by GMT -U options).");
690696
GMT_Usage (API, 1, "\n-C<gs_option>");
@@ -2082,8 +2088,8 @@ EXTERN_MSC int GMT_psconvert(void *V_API, int mode, void *args) {
20822088
/* We only use the High resolution BB */
20832089
if ((strstr(line,"%%HiResBoundingBox:"))) {
20842090
sscanf (&line[19], "%s %s %s %s", c1, c2, c3, c4);
2085-
x0 = atof (c1); y0 = atof (c2);
2086-
x1 = atof (c3); y1 = atof (c4);
2091+
x0 = atof(c1); y0 = atof(c2);
2092+
x1 = atof(c3); y1 = atof(c4);
20872093
x0 -= Ctrl->I.margin[XLO]; x1 += Ctrl->I.margin[XHI]; /* If not given, margin = 0/0/0/0 */
20882094
y0 -= Ctrl->I.margin[YLO]; y1 += Ctrl->I.margin[YHI];
20892095
if (x1 <= x0 || y1 <= y0) {
@@ -2136,6 +2142,29 @@ EXTERN_MSC int GMT_psconvert(void *V_API, int mode, void *args) {
21362142
Return(GMT_RUNTIME_ERROR);
21372143
}
21382144
if (got_BB) GMT_Report(API, GMT_MSG_INFORMATION, "[%g %g %g %g]...\n", x0, y0, x1, y1);
2145+
if (Ctrl->A.only_BB) { /* Output the Width, Height and position on page and return. */
2146+
double out[6];
2147+
struct GMT_RECORD *Out = NULL;
2148+
2149+
if (GMT_Init_IO (API, GMT_IS_DATASET, GMT_IS_PLP, GMT_OUT, GMT_ADD_DEFAULT, 0, NULL) != GMT_NOERROR) { /* Establishes data output */
2150+
Return (API->error);
2151+
}
2152+
if (GMT_Begin_IO (API, GMT_IS_DATASET, GMT_OUT, GMT_HEADER_OFF) != GMT_NOERROR) {
2153+
Return (API->error);
2154+
}
2155+
if ((error = GMT_Set_Columns (API, GMT_OUT, 6, GMT_COL_FIX_NO_TEXT)) != GMT_NOERROR) Return (error);
2156+
2157+
Out = gmt_new_record(GMT, out, NULL);
2158+
out[0] = (x1-x0) * 2.54/72.0; out[1] = (y1-y0) * 2.54/72.0; out[2] = x0 * 2.54/72.0;
2159+
out[3] = y0 * 2.54/72.0; out[4] = x1 * 2.54/72.0; out[5] = y1 * 2.54/72.0;
2160+
GMT_Put_Record(API, GMT_WRITE_DATA, Out);
2161+
2162+
if (GMT_End_IO (API, GMT_OUT, 0) != GMT_NOERROR) { /* Disables further data output */
2163+
Return (API->error);
2164+
}
2165+
2166+
Return(GMT_OK);
2167+
}
21392168
}
21402169

21412170
/* Open temporary file to be processed by Ghostscript. When -Te or -TE is used, tmp_file is for keeps */

0 commit comments

Comments
 (0)