2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team.
7 * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
52 #include "gromacs/fileio/gmxfio.h"
53 #include "gromacs/math/functions.h"
54 #include "gromacs/math/utilities.h"
55 #include "gromacs/utility/binaryinformation.h"
56 #include "gromacs/utility/cstringutil.h"
57 #include "gromacs/utility/exceptions.h"
58 #include "gromacs/utility/fatalerror.h"
59 #include "gromacs/utility/futil.h"
60 #include "gromacs/utility/programcontext.h"
61 #include "gromacs/utility/smalloc.h"
65 static const char mapper[] =
66 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+{}|;:',<.>/"
68 #define NMAP static_cast<long int>(sizeof(mapper) / sizeof(mapper[0]))
70 real** mk_matrix(int nx, int ny, gmx_bool b1D)
81 for (i = 0; (i < nx); i++)
85 m[i] = &(m[0][i * ny]);
96 void done_matrix(int nx, real*** m)
100 for (i = 0; (i < nx); i++)
108 static bool operator==(t_xpmelmt e1, t_xpmelmt e2)
110 return (e1.c1 == e2.c1) && (e1.c2 == e2.c2);
113 //! Return the index of the first element that matches \c c, or -1 if not found.
114 t_matelmt searchcmap(ArrayRef<const t_mapping> map, t_xpmelmt c)
116 auto findIt = std::find_if(map.begin(), map.end(), [&c](const auto& m) { return (m.code == c); });
117 return (findIt == map.end()) ? -1 : (findIt - map.begin());
120 //! Read the mapping table from in, return number of entries
121 static std::vector<t_mapping> getcmap(FILE* in, const char* fn)
125 char code[STRLEN], desc[STRLEN];
127 std::vector<t_mapping> m;
129 if (fgets2(line, STRLEN - 1, in) == nullptr)
132 "Not enough lines in colormap file %s"
133 "(just wanted to read number of entries)",
136 sscanf(line, "%d", &n);
138 for (i = 0; (i < n); i++)
140 if (fgets2(line, STRLEN - 1, in) == nullptr)
143 "Not enough lines in colormap file %s"
144 "(should be %d, found only %d)",
149 sscanf(line, "%s%s%lf%lf%lf", code, desc, &r, &g, &b);
150 m[i].code.c1 = code[0];
152 m[i].desc = gmx_strdup(desc);
161 std::vector<t_mapping> readcmap(const char* fn)
163 FilePtr in = openLibraryFile(fn);
164 return getcmap(in.get(), fn);
167 void printcmap(FILE* out, int n, t_mapping map[])
171 fprintf(out, "%d\n", n);
172 for (i = 0; (i < n); i++)
175 "%c%c %20s %10g %10g %10g\n",
176 map[i].code.c1 ? map[i].code.c1 : ' ',
177 map[i].code.c2 ? map[i].code.c2 : ' ',
185 void writecmap(const char* fn, int n, t_mapping map[])
189 out = gmx_fio_fopen(fn, "w");
190 printcmap(out, n, map);
194 static char* fgetline(char** line, int llmax, int* llalloc, FILE* in)
198 if (llmax > *llalloc)
200 srenew(*line, llmax + 1);
203 fg = fgets(*line, llmax, in);
209 static void skipstr(char* line)
215 while ((line[c] != ' ') && (line[c] != '\0'))
220 while (line[c] != '\0')
222 line[c - i] = line[c];
228 static char* line2string(char** line)
232 if (*line != nullptr)
234 while (((*line)[0] != '\"') && ((*line)[0] != '\0'))
239 if ((*line)[0] != '\"')
246 while (((*line)[i] != '\"') && ((*line)[i] != '\0'))
251 if ((*line)[i] != '\"')
264 //! If a label named \c label is found in \c line, return it. Otherwise return empty string.
265 static std::optional<std::string> findLabelInLine(const std::string& line, const std::string& label)
267 std::regex re(".*\\s" + label + ":[\\s]*\"(.*)\"");
269 if (std::regex_search(line, match, re) && match.size() > 1)
271 return std::make_optional<std::string>(match.str(1));
276 //! Read and return a matrix from \c in
277 static t_matrix read_xpm_entry(FILE* in)
279 char * line_buf = nullptr, *line = nullptr, *str;
280 std::optional<std::string> title, legend, xLabel, yLabel, matrixType;
281 int i, m, col_len, nch = 0, llmax;
283 unsigned int r, g, b;
285 gmx_bool bGetOnWithIt, bSetLine;
290 while ((nullptr != fgetline(&line_buf, llmax, &llalloc, in))
291 && (std::strncmp(line_buf, "static", 6) != 0))
293 std::string lineString = line_buf;
294 if (!title.has_value())
296 title = findLabelInLine(lineString, "title");
298 if (!legend.has_value())
300 legend = findLabelInLine(lineString, "legend");
302 if (!xLabel.has_value())
304 xLabel = findLabelInLine(lineString, "x-label");
306 if (!yLabel.has_value())
308 yLabel = findLabelInLine(lineString, "y-label");
310 if (!matrixType.has_value())
312 matrixType = findLabelInLine(lineString, "type");
316 if (!line_buf || strncmp(line_buf, "static", 6) != 0)
318 gmx_input("Invalid XPixMap");
323 mm.title = title.value_or("");
324 mm.legend = legend.value_or("");
325 mm.label_x = xLabel.value_or("");
326 mm.label_y = yLabel.value_or("");
328 if (matrixType.has_value() && (gmx_strcasecmp(matrixType->c_str(), "Discrete") == 0))
345 bGetOnWithIt = FALSE;
346 while (!bGetOnWithIt && (nullptr != fgetline(&line_buf, llmax, &llalloc, in)))
349 while ((line[0] != '\"') && (line[0] != '\0'))
357 sscanf(line, "%d %d %d %d", &(mm.nx), &(mm.ny), &nmap, &nch);
360 gmx_fatal(FARGS, "Sorry can only read xpm's with at most 2 characters per pixel\n");
362 if (mm.nx <= 0 || mm.ny <= 0)
364 gmx_fatal(FARGS, "Dimensions of xpm-file have to be larger than 0\n");
366 llmax = std::max(STRLEN, mm.nx + 10);
372 fprintf(debug, "mm.nx %d mm.ny %d nmap %d nch %d\n", mm.nx, mm.ny, nmap, nch);
376 gmx_fatal(FARGS, "Number of characters per pixel not found in xpm\n");
382 while ((m < nmap) && (nullptr != fgetline(&line_buf, llmax, &llalloc, in)))
384 line = std::strchr(line_buf, '\"');
388 /* Read xpm color map entry */
389 mm.map[m].code.c1 = line[0];
392 mm.map[m].code.c2 = 0;
396 mm.map[m].code.c2 = line[1];
399 str = std::strchr(line, '#');
404 while (std::isxdigit(str[col_len]))
410 sscanf(line, "%*s #%2x%2x%2x", &r, &g, &b);
411 mm.map[m].rgb.r = r / 255.0;
412 mm.map[m].rgb.g = g / 255.0;
413 mm.map[m].rgb.b = b / 255.0;
415 else if (col_len == 12)
417 sscanf(line, "%*s #%4x%4x%4x", &r, &g, &b);
418 mm.map[m].rgb.r = r / 65535.0;
419 mm.map[m].rgb.g = g / 65535.0;
420 mm.map[m].rgb.b = b / 65535.0;
424 gmx_file("Unsupported or invalid colormap in X PixMap");
429 str = std::strchr(line, 'c');
436 gmx_file("Unsupported or invalid colormap in X PixMap");
438 fprintf(stderr, "Using white for color \"%s", str);
443 line = std::strchr(line, '\"');
446 mm.map[m].desc = gmx_strdup(line);
453 "Number of read colors map entries (%d) does not match the number in the header "
459 /* Read axes, if there are any */
468 GMX_RELEASE_ASSERT(line, "Need to have valid line to parse");
469 if (strstr(line, "x-axis"))
471 line = std::strstr(line, "x-axis");
473 mm.axis_x.reserve(mm.nx + 1);
474 while (sscanf(line, "%lf", &u) == 1)
476 if (ssize(mm.axis_x) > mm.nx)
478 gmx_fatal(FARGS, "Too many x-axis labels in xpm (max %d)", mm.nx);
480 else if (ssize(mm.axis_x) == mm.nx)
482 mm.flags |= MAT_SPATIAL_X;
484 mm.axis_x.push_back(u);
488 else if (std::strstr(line, "y-axis"))
490 line = std::strstr(line, "y-axis");
492 mm.axis_y.reserve(mm.ny + 1);
493 while (sscanf(line, "%lf", &u) == 1)
495 if (ssize(mm.axis_y) > mm.ny)
497 gmx_fatal(FARGS, "Too many y-axis labels in xpm (max %d)", mm.ny);
499 else if (ssize(mm.axis_y) == mm.ny)
501 mm.flags |= MAT_SPATIAL_Y;
503 mm.axis_y.push_back(u);
507 } while ((line[0] != '\"') && (nullptr != fgetline(&line_buf, llmax, &llalloc, in)));
510 mm.matrix.resize(mm.nx, mm.ny);
511 int rowIndex = mm.ny - 1;
520 if (rowIndex % (1 + mm.ny / 100) == 0)
522 fprintf(stderr, "%3d%%\b\b\b\b", (100 * (mm.ny - rowIndex)) / mm.ny);
524 while ((line[0] != '\"') && (line[0] != '\0'))
530 gmx_fatal(FARGS, "Not enough characters in row %d of the matrix\n", rowIndex + 1);
535 for (i = 0; i < mm.nx; i++)
537 c.c1 = line[nch * i];
544 c.c2 = line[nch * i + 1];
546 mm.matrix(i, rowIndex) = searchcmap(mm.map, c);
550 } while ((rowIndex >= 0) && (nullptr != fgetline(&line_buf, llmax, &llalloc, in)));
553 gmx_incons("Not enough rows in the matrix");
560 std::vector<t_matrix> read_xpm_matrix(const char* fnm)
563 char* line = nullptr;
566 in = gmx_fio_fopen(fnm, "r");
568 std::vector<t_matrix> mat;
569 while (nullptr != fgetline(&line, STRLEN, &llalloc, in))
571 if (std::strstr(line, "/* XPM */"))
573 mat.emplace_back(read_xpm_entry(in));
580 gmx_file("Invalid XPixMap");
588 real** matrix2real(t_matrix* in, real** out)
592 std::vector<real> rmap(in->map.size());
594 for (gmx::index i = 0; i != ssize(in->map); ++i)
596 if ((in->map[i].desc == nullptr) || (sscanf(in->map[i].desc, "%lf", &tmp) != 1))
599 "Could not convert matrix to reals,\n"
600 "color map entry %zd has a non-real description: \"%s\"\n",
611 for (int i = 0; i < in->nx; i++)
613 snew(out[i], in->ny);
616 for (int i = 0; i < in->nx; i++)
618 for (int j = 0; j < in->ny; j++)
620 out[i][j] = rmap[in->matrix(i, j)];
624 fprintf(stderr, "Converted a %dx%d matrix with %zu levels to reals\n", in->nx, in->ny, in->map.size());
629 static void write_xpm_header(FILE* out,
630 const std::string& title,
631 const std::string& legend,
632 const std::string& label_x,
633 const std::string& label_y,
636 fprintf(out, "/* XPM */\n");
637 fprintf(out, "/* This file can be converted to EPS by the GROMACS program xpm2ps */\n");
638 fprintf(out, "/* title: \"%s\" */\n", title.c_str());
639 fprintf(out, "/* legend: \"%s\" */\n", legend.c_str());
640 fprintf(out, "/* x-label: \"%s\" */\n", label_x.c_str());
641 fprintf(out, "/* y-label: \"%s\" */\n", label_y.c_str());
644 fprintf(out, "/* type: \"Discrete\" */\n");
648 fprintf(out, "/* type: \"Continuous\" */\n");
652 static int calc_nmid(int nlevels, real lo, real mid, real hi)
654 /* Take care that we have at least 1 entry in the mid to hi range
656 return std::min(std::max(0, static_cast<int>(((mid - lo) / (hi - lo)) * (nlevels - 1))), nlevels - 1);
660 write_xpm_map3(FILE* out, int n_x, int n_y, int* nlevels, real lo, real mid, real hi, t_rgb rlo, t_rgb rmid, t_rgb rhi)
663 double r, g, b, clev_lo, clev_hi;
665 if (*nlevels > NMAP * NMAP)
668 "Warning, too many levels (%d) in matrix, using %d only\n",
670 static_cast<int>(NMAP * NMAP));
671 *nlevels = NMAP * NMAP;
673 else if (*nlevels < 2)
675 fprintf(stderr, "Warning, too few levels (%d) in matrix, using 2 instead\n", *nlevels);
678 if (!((mid >= lo) && (mid < hi)))
680 gmx_fatal(FARGS, "Lo: %f, Mid: %f, Hi: %f\n", lo, mid, hi);
683 fprintf(out, "static char *gromacs_xpm[] = {\n");
684 fprintf(out, "\"%d %d %d %d\",\n", n_x, n_y, *nlevels, (*nlevels <= NMAP) ? 1 : 2);
686 nmid = calc_nmid(*nlevels, lo, mid, hi);
688 clev_hi = (*nlevels - 1 - nmid);
689 for (i = 0; (i < nmid); i++)
691 r = rlo.r + (i * (rmid.r - rlo.r) / clev_lo);
692 g = rlo.g + (i * (rmid.g - rlo.g) / clev_lo);
693 b = rlo.b + (i * (rmid.b - rlo.b) / clev_lo);
695 "\"%c%c c #%02X%02X%02X \" /* \"%.3g\" */,\n",
697 (*nlevels <= NMAP) ? ' ' : mapper[i / NMAP],
698 static_cast<unsigned int>(std::round(255 * r)),
699 static_cast<unsigned int>(std::round(255 * g)),
700 static_cast<unsigned int>(std::round(255 * b)),
701 ((nmid - i) * lo + i * mid) / clev_lo);
703 for (i = 0; (i < (*nlevels - nmid)); i++)
705 r = rmid.r + (i * (rhi.r - rmid.r) / clev_hi);
706 g = rmid.g + (i * (rhi.g - rmid.g) / clev_hi);
707 b = rmid.b + (i * (rhi.b - rmid.b) / clev_hi);
709 "\"%c%c c #%02X%02X%02X \" /* \"%.3g\" */,\n",
710 mapper[(i + nmid) % NMAP],
711 (*nlevels <= NMAP) ? ' ' : mapper[(i + nmid) / NMAP],
712 static_cast<unsigned int>(std::round(255 * r)),
713 static_cast<unsigned int>(std::round(255 * g)),
714 static_cast<unsigned int>(std::round(255 * b)),
715 ((*nlevels - 1 - nmid - i) * mid + i * hi) / clev_hi);
719 static void pr_simple_cmap(FILE* out, real lo, real hi, int nlevel, t_rgb rlo, t_rgb rhi, int i0)
724 for (i = 0; (i < nlevel); i++)
726 fac = (i + 1.0) / (nlevel);
727 r = rlo.r + fac * (rhi.r - rlo.r);
728 g = rlo.g + fac * (rhi.g - rlo.g);
729 b = rlo.b + fac * (rhi.b - rlo.b);
731 "\"%c%c c #%02X%02X%02X \" /* \"%.3g\" */,\n",
732 mapper[(i + i0) % NMAP],
733 (nlevel <= NMAP) ? ' ' : mapper[(i + i0) / NMAP],
734 static_cast<unsigned int>(std::round(255 * r)),
735 static_cast<unsigned int>(std::round(255 * g)),
736 static_cast<unsigned int>(std::round(255 * b)),
737 lo + fac * (hi - lo));
741 static void pr_discrete_cmap(FILE* out, int* nlevel, int i0)
744 { 1.0, 1.0, 1.0 }, /* white */
745 { 1.0, 0.0, 0.0 }, /* red */
746 { 1.0, 1.0, 0.0 }, /* yellow */
747 { 0.0, 0.0, 1.0 }, /* blue */
748 { 0.0, 1.0, 0.0 }, /* green */
749 { 1.0, 0.0, 1.0 }, /* purple */
750 { 1.0, 0.4, 0.0 }, /* orange */
751 { 0.0, 1.0, 1.0 }, /* cyan */
752 { 1.0, 0.4, 0.4 }, /* pink */
753 { 1.0, 1.0, 0.0 }, /* yellow */
754 { 0.4, 0.4, 1.0 }, /* lightblue */
755 { 0.4, 1.0, 0.4 }, /* lightgreen */
756 { 1.0, 0.4, 1.0 }, /* lightpurple */
757 { 1.0, 0.7, 0.4 }, /* lightorange */
758 { 0.4, 1.0, 1.0 }, /* lightcyan */
759 { 0.0, 0.0, 0.0 } /* black */
764 *nlevel = std::min(16, *nlevel);
766 for (i = 0; (i < n); i++)
769 "\"%c%c c #%02X%02X%02X \" /* \"%3d\" */,\n",
770 mapper[(i + i0) % NMAP],
771 (n <= NMAP) ? ' ' : mapper[(i + i0) / NMAP],
772 static_cast<unsigned int>(round(255 * rgbd[i].r)),
773 static_cast<unsigned int>(round(255 * rgbd[i].g)),
774 static_cast<unsigned int>(round(255 * rgbd[i].b)),
780 static void write_xpm_map_split(FILE* out,
783 const int* nlevel_top,
788 gmx_bool bDiscreteColor,
797 ntot = *nlevel_top + *nlevel_bot;
800 gmx_fatal(FARGS, "Warning, too many levels (%d) in matrix", ntot);
803 fprintf(out, "static char *gromacs_xpm[] = {\n");
804 fprintf(out, "\"%d %d %d %d\",\n", n_x, n_y, ntot, 1);
808 pr_discrete_cmap(out, nlevel_bot, 0);
812 pr_simple_cmap(out, lo_bot, hi_bot, *nlevel_bot, rlo_bot, rhi_bot, 0);
815 pr_simple_cmap(out, lo_top, hi_top, *nlevel_top, rlo_top, rhi_top, *nlevel_bot);
819 static void write_xpm_map(FILE* out, int n_x, int n_y, int* nlevels, real lo, real hi, t_rgb rlo, t_rgb rhi)
822 real invlevel, r, g, b;
824 if (*nlevels > NMAP * NMAP)
827 "Warning, too many levels (%d) in matrix, using %d only\n",
829 static_cast<int>(NMAP * NMAP));
830 *nlevels = NMAP * NMAP;
832 else if (*nlevels < 2)
834 fprintf(stderr, "Warning, too few levels (%d) in matrix, using 2 instead\n", *nlevels);
838 fprintf(out, "static char *gromacs_xpm[] = {\n");
839 fprintf(out, "\"%d %d %d %d\",\n", n_x, n_y, *nlevels, (*nlevels <= NMAP) ? 1 : 2);
841 invlevel = 1.0 / (*nlevels - 1);
842 for (i = 0; (i < *nlevels); i++)
844 nlo = *nlevels - 1 - i;
845 r = (nlo * rlo.r + i * rhi.r) * invlevel;
846 g = (nlo * rlo.g + i * rhi.g) * invlevel;
847 b = (nlo * rlo.b + i * rhi.b) * invlevel;
849 "\"%c%c c #%02X%02X%02X \" /* \"%.3g\" */,\n",
851 (*nlevels <= NMAP) ? ' ' : mapper[i / NMAP],
852 static_cast<unsigned int>(std::round(255 * r)),
853 static_cast<unsigned int>(std::round(255 * g)),
854 static_cast<unsigned int>(std::round(255 * b)),
855 (nlo * lo + i * hi) * invlevel);
859 static void writeXpmAxis(FILE* out, const char* axis, ArrayRef<const real> label)
865 for (gmx::index i = 0; i != ssize(label); ++i)
871 fprintf(out, "*/\n");
873 fprintf(out, "/* %s-axis: ", axis);
875 fprintf(out, "%g ", label[i]);
877 fprintf(out, "*/\n");
880 static void write_xpm_data(FILE* out, int n_x, int n_y, real** mat, real lo, real hi, int nlevels)
885 invlevel = (nlevels - 1) / (hi - lo);
886 for (j = n_y - 1; (j >= 0); j--)
888 if (j % (1 + n_y / 100) == 0)
890 fprintf(stderr, "%3d%%\b\b\b\b", (100 * (n_y - j)) / n_y);
893 for (i = 0; (i < n_x); i++)
895 c = roundToInt((mat[i][j] - lo) * invlevel);
906 fprintf(out, "%c", mapper[c]);
910 fprintf(out, "%c%c", mapper[c % NMAP], mapper[c / NMAP]);
915 fprintf(out, "\",\n");
919 fprintf(out, "\"\n");
924 static void write_xpm_data3(FILE* out, int n_x, int n_y, real** mat, real lo, real mid, real hi, int nlevels)
926 int i, j, c = 0, nmid;
927 real invlev_lo, invlev_hi;
929 nmid = calc_nmid(nlevels, lo, mid, hi);
930 invlev_hi = (nlevels - 1 - nmid) / (hi - mid);
931 invlev_lo = (nmid) / (mid - lo);
933 for (j = n_y - 1; (j >= 0); j--)
935 if (j % (1 + n_y / 100) == 0)
937 fprintf(stderr, "%3d%%\b\b\b\b", (100 * (n_y - j)) / n_y);
940 for (i = 0; (i < n_x); i++)
942 if (mat[i][j] >= mid)
944 c = nmid + roundToInt((mat[i][j] - mid) * invlev_hi);
946 else if (mat[i][j] >= lo)
948 c = roundToInt((mat[i][j] - lo) * invlev_lo);
965 fprintf(out, "%c", mapper[c]);
969 fprintf(out, "%c%c", mapper[c % NMAP], mapper[c / NMAP]);
974 fprintf(out, "\",\n");
978 fprintf(out, "\"\n");
983 static void write_xpm_data_split(FILE* out,
995 real invlev_top, invlev_bot;
997 invlev_top = (nlevel_top - 1) / (hi_top - lo_top);
998 invlev_bot = (nlevel_bot - 1) / (hi_bot - lo_bot);
1000 for (j = n_y - 1; (j >= 0); j--)
1002 if (j % (1 + n_y / 100) == 0)
1004 fprintf(stderr, "%3d%%\b\b\b\b", (100 * (n_y - j)) / n_y);
1007 for (i = 0; (i < n_x); i++)
1011 c = nlevel_bot + roundToInt((mat[i][j] - lo_top) * invlev_top);
1012 if ((c < nlevel_bot) || (c >= nlevel_bot + nlevel_top))
1015 "Range checking i = %d, j = %d, c = %d, bot = %d, top = %d "
1027 c = roundToInt((mat[i][j] - lo_bot) * invlev_bot);
1028 if ((c < 0) || (c >= nlevel_bot + nlevel_bot))
1031 "Range checking i = %d, j = %d, c = %d, bot = %d, top = %d "
1046 fprintf(out, "%c", mapper[c]);
1050 fprintf(out, "\",\n");
1054 fprintf(out, "\"\n");
1059 void write_xpm_m(FILE* out, t_matrix m)
1064 bOneChar = (m.map[0].code.c2 == 0);
1065 write_xpm_header(out, m.title, m.legend, m.label_x, m.label_y, m.bDiscrete);
1066 fprintf(out, "static char *gromacs_xpm[] = {\n");
1067 fprintf(out, "\"%d %d %zu %d\",\n", m.nx, m.ny, m.map.size(), bOneChar ? 1 : 2);
1068 for (const auto& map : m.map)
1071 "\"%c%c c #%02X%02X%02X \" /* \"%s\" */,\n",
1073 bOneChar ? ' ' : map.code.c2,
1074 static_cast<unsigned int>(round(map.rgb.r * 255)),
1075 static_cast<unsigned int>(round(map.rgb.g * 255)),
1076 static_cast<unsigned int>(round(map.rgb.b * 255)),
1079 writeXpmAxis(out, "x", m.axis_x);
1080 writeXpmAxis(out, "y", m.axis_y);
1081 for (int j = m.ny - 1; (j >= 0); j--)
1083 if (j % (1 + m.ny / 100) == 0)
1085 fprintf(stderr, "%3d%%\b\b\b\b", (100 * (m.ny - j)) / m.ny);
1090 for (int i = 0; (i < m.nx); i++)
1092 fprintf(out, "%c", m.map[m.matrix(i, j)].code.c1);
1097 for (int i = 0; (i < m.nx); i++)
1099 c = m.map[m.matrix(i, j)].code;
1100 fprintf(out, "%c%c", c.c1, c.c2);
1105 fprintf(out, "\",\n");
1109 fprintf(out, "\"\n");
1114 void write_xpm3(FILE* out,
1116 const std::string& title,
1117 const std::string& legend,
1118 const std::string& label_x,
1119 const std::string& label_y,
1134 * Writes a colormap varying as rlo -> rmid -> rhi.
1139 gmx_fatal(FARGS, "hi (%g) <= lo (%g)", hi, lo);
1142 write_xpm_header(out, title, legend, label_x, label_y, FALSE);
1143 write_xpm_map3(out, n_x, n_y, nlevels, lo, mid, hi, rlo, rmid, rhi);
1144 writeXpmAxis(out, "x", ArrayRef<real>(axis_x, axis_x + n_x + ((flags & MAT_SPATIAL_X) != 0U ? 1 : 0)));
1145 writeXpmAxis(out, "y", ArrayRef<real>(axis_y, axis_y + n_y + ((flags & MAT_SPATIAL_Y) != 0U ? 1 : 0)));
1146 write_xpm_data3(out, n_x, n_y, mat, lo, mid, hi, *nlevels);
1149 void write_xpm_split(FILE* out,
1151 const std::string& title,
1152 const std::string& legend,
1153 const std::string& label_x,
1154 const std::string& label_y,
1168 gmx_bool bDiscreteColor,
1173 * Writes a colormap varying as rlo -> rmid -> rhi.
1176 if (hi_top <= lo_top)
1178 gmx_fatal(FARGS, "hi_top (%g) <= lo_top (%g)", hi_top, lo_top);
1180 if (hi_bot <= lo_bot)
1182 gmx_fatal(FARGS, "hi_bot (%g) <= lo_bot (%g)", hi_bot, lo_bot);
1184 if (bDiscreteColor && (*nlevel_bot >= 16))
1186 gmx_impl("Can not plot more than 16 discrete colors");
1189 write_xpm_header(out, title, legend, label_x, label_y, FALSE);
1190 write_xpm_map_split(
1191 out, n_x, n_y, nlevel_top, lo_top, hi_top, rlo_top, rhi_top, bDiscreteColor, nlevel_bot, lo_bot, hi_bot, rlo_bot, rhi_bot);
1192 writeXpmAxis(out, "x", ArrayRef<real>(axis_x, axis_x + n_x + ((flags & MAT_SPATIAL_X) != 0U ? 1 : 0)));
1193 writeXpmAxis(out, "y", ArrayRef<real>(axis_y, axis_y + n_y + ((flags & MAT_SPATIAL_Y) != 0U ? 1 : 0)));
1194 write_xpm_data_split(out, n_x, n_y, mat, lo_top, hi_top, *nlevel_top, lo_bot, hi_bot, *nlevel_bot);
1197 void write_xpm(FILE* out,
1199 const std::string& title,
1200 const std::string& legend,
1201 const std::string& label_x,
1202 const std::string& label_y,
1215 * title matrix title
1216 * legend label for the continuous legend
1217 * label_x label for the x-axis
1218 * label_y label for the y-axis
1219 * n_x, n_y size of the matrix
1220 * axis_x[] the x-ticklabels
1221 * axis_y[] the y-ticklables
1222 * *matrix[] element x,y is matrix[x][y]
1223 * lo output lower than lo is set to lo
1224 * hi output higher than hi is set to hi
1225 * rlo rgb value for level lo
1226 * rhi rgb value for level hi
1227 * nlevels number of color levels for the output
1232 gmx_fatal(FARGS, "hi (%f) <= lo (%f)", hi, lo);
1235 write_xpm_header(out, title, legend, label_x, label_y, FALSE);
1236 write_xpm_map(out, n_x, n_y, nlevels, lo, hi, rlo, rhi);
1237 writeXpmAxis(out, "x", ArrayRef<real>(axis_x, axis_x + n_x + ((flags & MAT_SPATIAL_X) != 0U ? 1 : 0)));
1238 writeXpmAxis(out, "y", ArrayRef<real>(axis_y, axis_y + n_y + ((flags & MAT_SPATIAL_Y) != 0U ? 1 : 0)));
1239 write_xpm_data(out, n_x, n_y, mat, lo, hi, *nlevels);