1 
2  /* ==================================================================
3     FILE: "/home/joze/pub/zimg/zimg/zimg_priv.h"
4     LAST MODIFIED: "Sa, 27 Aug 2005 14:34:02 CEST (joze)"
5     (C) 1999 - 2003 by Johannes Zellner
6     johannes@zellner.org
7     $Id: zimg_priv.h,v 1.21 2005/08/27 12:46:20 joze Exp $
8     ---
9     Copyright (c) 1999 - 2003, Johannes Zellner <johannes@zellner.org>
10     All rights reserved.
11 
12     Redistribution and use in source and binary forms, with or without
13     modification, are permitted provided that the following conditions
14     are met:
15 
16       * Redistributions of source code must retain the above copyright
17         notice, this list of conditions and the following disclaimer.
18       * Redistributions in binary form must reproduce the above copyright
19         notice, this list of conditions and the following disclaimer in the
20         documentation and/or other materials provided with the distribution.
21       * Neither the name of Johannes Zellner nor the names of contributors
22         to this software may be used to endorse or promote products derived
23         from this software without specific prior written permission.
24 
25     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26     ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28     A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
29     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30     EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31     PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33     LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34     NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36     ================================================================== */
37 
38 #ifndef lint
39 #ifdef MAIN
40 char *RCSDate = "$Date: 2005/08/27 12:46:20 $";
41 char *RCSRevision = "$Revision: 1.21 $";
42 int verbose = 0;
43 int debug = 0;
44 #define EXTERN
45 #else
46 extern char *RCSDate;
47 extern char *RCSRevision;
48 extern int verbose;
49 extern int debug;
50 #endif /* MAIN */
51 #endif /* lint */
52 
53 #include "config.h"
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <math.h>
59 #include <errno.h>
60 #include <assert.h>
61 
62 #include <gd.h>
63 
64 #include <unistd.h>
65 
66 /* define this to double, if the data
67  * storage and handling should be double */
68 #define FLOAT float
69 
70 /* for times() */
71 #include <sys/types.h>
72 #include <limits.h>
73 
74 #ifdef HAVE_SYS_TIMES_H
75 #    include <sys/times.h>
76 #endif
77 
78 #ifdef HAVE_REGEX_H
79 #   include <regex.h>
80 #endif
81 
82 #define PROGRAMNAME "zimg"
83 #define RCFILE "zimgrc"
84 
85 
86 #ifdef TRUE
87 #   undef TRUE
88 #endif /* TRUE */
89 #ifdef FALSE
90 #   undef FALSE
91 #endif /* FALSE */
92 
93 #define TRUE	(1)
94 #define FALSE	(0)
95 
96 /* available output formats
97  * note 1: some formats may not be available depending on the
98  *         libgd version.
99  * note: IMFORMAT_PPMorPGM chooses between PGM and PPM according to --gray
100  */
101 #ifdef GD_HAS_PNG
102 #define IMFORMAT_PNG	  0
103 #endif
104 #ifdef GD_HAS_GIF
105 #define IMFORMAT_GIF	  1
106 #endif
107 #define IMFORMAT_JPG	  2
108 #define IMFORMAT_PPM	  3
109 #define IMFORMAT_PGM	  4
110 #define IMFORMAT_PPMorPGM 99
111 
112 #define OK	(0)
113 #define ERROR	(-1)
114 #define UNSET	(-1)
115 
116 #define MAX_PATTERNS	(10)
117 #define MAX_ARGS	(1000)
118 
119 #define MATRIX_FORMAT		(-1)
120 #define ASCII_FORMAT		(0)
121 #define FLOAT_FORMAT		(1 << 0)
122 #define DOUBLE_FORMAT		(1 << 1)
123 #define CHAR_FORMAT		(1 << 2)
124 #define SHORT_FORMAT		(1 << 3)
125 #define INT_FORMAT		(1 << 4)
126 #define L_INT_FORMAT		(1 << 5)
127 #define U_CHAR_FORMAT		(1 << 6)
128 #define U_SHORT_FORMAT		(1 << 7)
129 #define U_INT_FORMAT		(1 << 8)
130 #define U_L_INT_FORMAT		(1 << 9)
131 #define COMPLEX_FLOAT_FORMAT	(1 << 10)
132 #define COMPLEX_DOUBLE_FORMAT	(1 << 11)
133 
134 #define GD_FORMAT	(7)
135 
136 #define RGBMAP		(1 << 26)
137 #define REDMAP		(1 << 27)
138 #define BLUEMAP		(1 << 28)
139 #define GRAYMAP		(1 << 29)
140 #define INVERT_MAP	(1 << 30)
141 #define XOR_MAP		(1 << 31)
142 
143 /*
144  * FIXME:
145  *   this works for 4 byte unsigned int
146  *   and for nothing else.
147  */
148 #define ALL_COLUMNS (0xffffffff)
149 
150 #define BLACK (-2)
151 
152 #define ACCESS2D(x, y, ptr, width) (*((ptr) + ((width) * (y)) + (x)))
153 
154 enum {
155     COMPLEX_ACTION_ABS   = (0),
156     COMPLEX_ACTION_PHASE = (1 << 0),
157     COMPLEX_ACTION_REAL  = (1 << 2),
158     COMPLEX_ACTION_IMAG  = (1 << 3),
159 };
160 
161 #define TRANSFORM_CHUNK_SIZE (0x100)
162 
163 #define TRANS(z) ((z)->transform[(z)->transform_valid])
164 
165 enum TRANSFORM_TYPE {
166     differentiate_TT,
167     curvature_TT,
168     smooth_TT,             /* p1 = sigma */
169     logarithmic_TT,        /* p1 = factor for log scaling */
170     fabs_TT,
171     absolute_TT,           /* p1 = min, p2 = max */
172     absolute_min_TT,       /* p1 = min, --absolute=min, */
173     absolute_max_TT,       /* p2 = max, --absolute=,max */
174     relative_TT,           /* p1 = min, p2 = max */
175     relative_min_TT,       /* p1 = min, --relative=min, */
176     relative_max_TT,       /* p2 = max, --relative=,max */
177 };
178 
179 typedef struct transform_t {
180     enum TRANSFORM_TYPE type;
181     double p1;
182     double p2;
183 } transform_t;
184 
185 typedef struct scale_t {
186     int x;
187     int y;
188     int pixels;
189 } scale_t;
190 
191 typedef enum zimg_label_flag_t {
192     ZIMG_HORIZONTAL = 0,
193     ZIMG_VERTICAL
194 } zimg_label_flag_t;
195 
196 typedef struct zimg_label_t {
197     int x;
198     int y;
199     char* text;
200     zimg_label_flag_t flag;
201     struct zimg_label_t* prev;
202     struct zimg_label_t* next;
203 } zimg_label_t;
204 
205 typedef struct zimg_vertex_t {
206     int x;
207     int y;
208     struct zimg_vertex_t* prev;
209     struct zimg_vertex_t* next;
210 } zimg_vertex_t;
211 
212 typedef enum zimg_polyline_flag_t {
213     ZIMG_LINE_ABSOLUTE = 0,
214     ZIMG_LINE_RELATIVE
215 } zimg_polyline_flag_t;
216 
217 typedef struct zimg_polyline_t {
218     zimg_vertex_t* vertex;
219     int n;
220     zimg_polyline_flag_t flag;
221     struct zimg_polyline_t* prev;
222     struct zimg_polyline_t* next;
223 } zimg_polyline_t;
224 
225 typedef struct zimg_color_t {
226     unsigned char set;
227     unsigned char red;
228     unsigned char green;
229     unsigned char blue;
230 } zimg_color_t;
231 
232 typedef struct crange_t {
233     unsigned char setMin, setMax;
234     float theMin, theMax;
235 } crange_t;
236 
237 typedef struct contour_t {
238     /*
239      * TODO:
240      *   add non-equally spaced
241      *   user definable levels.
242      */
243     int levels;
244     int log;
245     char background;
246     zimg_color_t color;
247 } contour_t;
248 
249 typedef struct zimg_data_t {
250 
251     unsigned int x;
252     unsigned int y;
253 
254     unsigned int len;    /* len = x * y */
255 
256     /* cropping */
257     unsigned int xleft;
258     unsigned int xright;
259     unsigned int ytop;
260     unsigned int ybottom;
261     unsigned char autocrop;
262 
263     unsigned int xibin;
264     unsigned int yibin;
265     double xdscale;
266     double ydscale;
267 
268 } zimg_data_t;
269 
270 typedef struct zimg_colorbox_t {
271     unsigned char show;
272     int levels;
273     char* fmt;
274     char* label;
275 } zimg_colorbox_t;
276 
277 #ifdef HAVE_REGEX_H
278 typedef struct zimg_options_t {
279     regex_t preg;
280     char* switches;
281 } zimg_options_t;
282 
283 enum { ZIMG_MAX_OPTIONS = 0xff };
284 #endif
285 
286 enum zimg_font { FONT_UNDEFINED = 0, FONT_TINY,
287     FONT_SMALL, FONT_LARGE, FONT_GIANT };
288 
289 typedef struct zimg_t {
290 
291     transform_t* transform;
292     int transform_valid;       /* number of valid entries in transform */
293     int transform_allocated;   /* number of allocated entries in transform */
294 
295     crange_t crange;
296 
297     int    color;              /* flag */
298     zimg_color_t xor_color;
299     int    rgbformulae[3];     /* see getcolor.c */
300     char*  colormapfile;       /* custom color map */
301     int    dump_colormap;      /* dump colormap to stdout */
302 
303     int    statistics;         /* flag */
304 
305     /*
306      * file stuff
307      */
308     char   oname[BUFSIZ];
309     FILE  *ifp;
310     FILE  *ofp;
311 
312     /*
313      * file format stuff
314      */
315     int    inputformat; /* ASCII or BINARY ( INT or FLOAT ) */
316     int    b_len;
317     int    skip;
318 
319 #ifdef HAVE_REGEX_H
320     unsigned int noptions;
321     zimg_options_t options[ZIMG_MAX_OPTIONS];
322 #endif
323 
324 #ifdef HAVE_POPEN
325     char* filter;
326 #endif
327 
328     int    swap;
329     unsigned int column;
330 
331     int  nn_pattern;
332     char pattern[MAX_PATTERNS][BUFSIZ];
333 
334     int complex_action;
335 
336 
337     /* data "source" dimensions */
338     zimg_data_t data;
339 
340 
341     /* image "target" dimensions */
342     unsigned int ximg;
343     unsigned int yimg;
344 
345     char* legend;
346 
347     int align[2];
348     int extend[4]; /* TODO: unused, see cmdln.c / misc.c */
349     zimg_color_t bordercolor;
350 
351     int interlace;
352     int imformat; /* which output image IMFORMAT_* ? */
353 #ifdef GD_JPEG_VERSION
354     int jpeg_quality;
355 #endif
356 
357     contour_t contour;
358 
359     zimg_polyline_t* line;
360     zimg_label_t* label;
361 
362     float fontsize;
363 #ifdef HAVE_GD_FREETYPE
364     char* fontspec;
365 #endif
366     zimg_color_t textcolor;
367 
368     char nda_specified;
369     double nda_thres;
370     double nda;
371     zimg_color_t nda_color;
372 
373     char* expr;
374     char* expr_source;
375     char* expr_object;
376 
377     zimg_colorbox_t colorbox;
378 
379 } zimg_t;
380 
381 
382 
383 /* cmdln.c */
384 void	readcmdln(int argc, char *argv[], zimg_t *z, int* ifiles, char*** iname);
385 void	init_z(zimg_t *z);
386 void	free_z(zimg_t *z);
387 void	parse_switches(zimg_t* z, char* line);
388 char*	checkmodeline(zimg_t* z, char* buf);
389 
390 /* color.c */
391 int*	get_next_custom_entry(FILE* fp);
392 int	getGdColorMap(gdImagePtr im, int gdcolor[0x100],
393        	const int color, int rgbformulae[3], char* colormapfile,
394        	const zimg_color_t* xor_color);
395 int	zimg_gdImageLineColorAllocate(gdImagePtr im,
396 	int red, int green, int blue);
397 int	zimg_dump_color(gdImagePtr im, int r, int g, int b);
398 
399 /* img.c */
400 void	differentiate(FLOAT *data, unsigned int width, unsigned int height);
401 void	torange(FLOAT* data, int len, FLOAT low, FLOAT high);
402 FLOAT*	curvature(FLOAT* data, unsigned int width, unsigned int height);
403 
404 /* misc.c */
405 void	help	(void);
406 void	license (void);
407 char*	version (void);
408 char	*ztime	(void);
409 void	perrorexit(const char *Zeile);
410 void	Fatal(const char *Zeile);
411 double	minmax	(float *data, unsigned char* flags, unsigned int len, float *min, float *max);
412 int	parse	(char **args, int maxargs, char *buf);
413 #ifndef HAVE_LOG1P
414 double	log1p(double arg);
415 #endif
416 FILE*	varopen(const char* filename, const char* mode
417 #ifdef HAVE_POPEN
418        	, const char* filter
419 #endif
420 	);
421 void	varclose(FILE* fp);
422 void	zimgGdImagePgm(gdImagePtr im, FILE* fp);
423 void	zimgGdImagePpm(gdImagePtr im, FILE* fp);
424 
425 /* read.c */
426 int	readfile	(zimg_t* z, float** data);
427 gdImagePtr fgets_dimension(zimg_t *z);
428 
429 /* contours.c */
430 int contours(zimg_t *z, float *data, gdImagePtr im,
431     int entries, float data_min, float data_max);
432 
433 /* smooth.c */
434 int smooth(float *data, int width, int height, float sigma);
435 
436 /* statistics.c */
437 int statistics(const float *data, unsigned char* flags,
438        	int width, int height, float min, float max, int levels);
439 
440 /* getcolor.c */
441 double GetColorValueFromFormula(int formula, double x /* gray */);
442