/* PZT_graph.c implements common drawing functions. PoZeTools v 0.52, march 31, 2005. Copyright (c) 2002-2005 Pieter Suurmond. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Any person wishing to distribute modifications to the Software is requested to send the modifications to the original developer so that they can be incorporated into the canonical version. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include /* Needed by "gt.h". */ #include #include /* For trunc() and fabs(). */ #include "gt.h" /* Minimal JPEG graphics toolkit. */ #include "PZT_graph.h" /* Needs "gt.h" to be included first. */ #include "PZT_complex_math.h" /*-----------------------------------------------------------------------------*/ /* See headerfile. */ int findScaleMarks(int pixels, double *min, double *max, double *step) { int d; double topMark, botMark, s, h; short swapped = 0; if (*min > *max) return 1; /* We do INTEGER arithmetic in FP representation. */ *min *= 10000.0; if (*min >= 0.0) (*min) += 0.5; else (*min) -= 0.5; *min = trunc(*min); *max *= 10000.0; if (*max >= 0.0) (*max) += 0.5; else (*max) -= 0.5; *max = trunc(*max); if ((*min == 0.0) && (*max == 0.0)) /* Nasty exception. */ { botMark = -1000.0; topMark = 1000.0; s = 1000.0; d = 2; } else { if (*max < -*min) /* Swap (also when (*max <= 0.0)). */ { h = *max; *max = -1.0 * (*min); *min = -1.0 * h; swapped = 1; } if (*max < 1.0) { /* Don't zoom in too much. */ s = topMark = 1.0; d = 1; } else /* Zooming out is not restricted here (yet). */ { topMark = 1.0; while (topMark < *max) topMark *= 10.0; /* Up by factors of 10. */ while (topMark >= *max) topMark *= 0.1; /* Down by factors of 10. */ d = (int)(1.0 - kPZT_epsilon + (*max / topMark)); s = topMark; /* Max 10 divisions. */ topMark *= (double)d; /* Min 2 divisions. */ } botMark = 0.0; if (*min >= 0.0) /* Unipolar, do we desire zero-line? */ { do { botMark += s; d--; } while (botMark < *min); /* Minimal 1 division. */ if (botMark == topMark) { if (topMark != 1.0) { topMark += s; d++; } } botMark -= s; d++; } else { /* Bipolar, ensure that zero-line is always reached by step. */ do { botMark -= s; d++; } while (botMark > *min); } } /* Enough pixels for the divisions? */ if (pixels > 130 * d) s *= 0.1; /* Max 200 divisions. */ else if (pixels > 65 * d) s *= 0.2; /* Max 100 divisions. */ else if (pixels > 26 * d) s *= 0.5; /* Max 40 divisions. */ else if (pixels > 13 * d) s *= 1.0; /* Max 20 divisions. */ else { s *= 2.0; /* Max 10 divisions. */ h = topMark / s; /* topMark and s are always positive. */ if (h != trunc(h)) topMark += 0.5 * s; /* May not always be necessary... */ h = fabs(botMark) / s; if (h != trunc(h)) botMark -= 0.5 * s; } if (swapped) { h = topMark; topMark = -1.0 * botMark; botMark = -1.0 * h; } *min = 0.0001 * botMark; /* Make FP-represented ints floats again. */ *max = 0.0001 * topMark; *step = 0.0001 * s; return 0; } /*----------------------------------------------------------------------------*/ void arrow(RGBPICp p, int x, int y, int orient) /* 0 = to the right, 1 = up. */ { if (orient) { gtDrawLine(p, x, y, x, y + 16, 0,0,0); gtDrawLine(p, x, y, x - 3, y + 3, 0,0,0); gtDrawLine(p, x, y, x + 3, y + 3, 0,0,0); } else { gtDrawLine(p, x, y, x - 16, y, 0,0,0); gtDrawLine(p, x, y, x - 3, y - 3, 0,0,0); gtDrawLine(p, x, y, x - 3, y + 3, 0,0,0); } } /*----------------------------------------------------------------------------------*/ int jpeg_error(const char* name_version, const char* msg) { RGBPICp pic; int e; printf("\ %s 200 OK\r\n\ Server: %s\r\n\ Pragma: no-cache\r\n\ Content-type: image/jpeg\r\n\ \r\n", getenv("SERVER_PROTOCOL"), getenv("SERVER_SOFTWARE")); e = gtCreateMemRGB(&pic, /* Receive pointer to RGBPIC object here. */ 512, 64, /* Specify width and height. */ 255, 255, 255); /* Specify white background. */ gtDrawString(pic, 2, 2, /* x,y. */ 255, 0, 0, /* r,g,b. */ 0, name_version); gtDrawString(pic, 2, 16, /* x,y. */ 255, 0, 0, /* r,g,b. */ 0, msg); /* 0=white bg. */ if (!e) e = gtOutputMemRGB(pic, stdout, 75); gtDestroyMemRGB(&pic); return e; }