doc/user: Add captions to uncaptioned tables.
[Ale.git] / d2 / render_parse.h
blob6d1d7ddfb2f143cdef946ff8fc41bb523a729277
1 // Copyright 2004 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
4 /* This file is part of the Anti-Lamenessing Engine.
6 The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with the Anti-Lamenessing Engine; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef __render_parse_h__
22 #define __render_parse_h__
24 #include "render.h"
25 #include "render/combine.h"
26 #include "render/invariant.h"
27 #include "render/incremental.h"
28 #include "render/zero.h"
31 * Parse strings describing renderers, and return a renderer satisfying
32 * the string.
35 class render_parse {
36 private:
37 static int strpfix(const char *a, const char *b) {
38 return strncmp(a, b, strlen(a));
41 static void nomem() {
42 fprintf(stderr, "\n\n*** Error: unable to allocate memory in render_parse. ***\n\n");
43 exit(1);
46 static void syntax_error(char *explanation) {
47 fprintf(stderr, "\n\n*** Error: Render syntax: %s ***\n\n", explanation);
48 exit(1);
51 static filter::filter *get_SF_atomic(const char *type) {
52 double param;
54 if (!strcmp("sinc", type)) {
55 return new filter::sinc();
56 } else if (!strpfix("lanc:", type)) {
57 if (sscanf(type + strlen("lanc:"), "%lf", &param) != 1)
58 syntax_error("Unable to get lanczos diameter.");
59 return new filter::lanczos(param/2);
60 } else if (!strpfix("triangle:", type)) {
61 if (sscanf(type + strlen("triangle:"), "%lf", &param) != 1)
62 syntax_error("Unable to get triangle diameter.");
63 return new filter::triangle(param/2);
64 } else if (!strpfix("box:", type)) {
65 if (sscanf(type + strlen("box:"), "%lf", &param) != 1)
66 syntax_error("Unable to get box diameter.");
67 return new filter::box(param/2);
68 } else if (!strpfix("zero", type)) {
69 return new filter::zero();
70 } else {
71 fprintf(stderr, "get_SF_atomic type %s\n", type);
72 syntax_error("Unable to get filter.");
75 assert (0);
78 * This line should never be reached; it's included to avoid
79 * 'unreachable' messages emitted by some compilers.
82 return NULL;
85 static filter::filter *get_SF(const char *orig_type) {
86 char *type = strdup(orig_type);
87 if (type == NULL)
88 nomem();
90 char *star_index = (char *) type;
91 while (*star_index != '\0'
92 && *star_index != '*')
93 star_index++;
95 if (*star_index == '\0') {
96 free(type);
97 return get_SF_atomic(orig_type);
100 *star_index = '\0';
101 filter::filter *result = new filter::mult(
102 get_SF_atomic(type),
103 get_SF(star_index + 1));
104 *star_index = '*';
105 free(type);
106 return result;
109 public:
110 static filter::scaled_filter *get_SSF(const char *type) {
111 if (!strpfix("coarse:", type)) {
112 return new filter::scaled_filter(get_SF(type + strlen("coarse:")), 1);
113 } else if (!strpfix("fine:", type)) {
114 return new filter::scaled_filter(get_SF(type + strlen("fine:")), 0);
115 } else {
116 return new filter::scaled_filter(get_SF(type), 1);
120 static filter::ssfe *get_SSFE(const char *type) {
121 if (!strpfix("ex:", type)) {
122 return new filter::ssfe(get_SSF(type + strlen("ex:")), 1);
123 } else if (!strpfix("nex:", type)) {
124 return new filter::ssfe(get_SSF(type + strlen("nex:")), 0);
125 } else {
126 return new filter::ssfe(get_SSF(type), 1);
130 static render *get_invariant(const char *type) {
131 invariant *i;
132 if (!strpfix("min:", type)) {
133 i = new invariant(get_SSFE(type + strlen("min:")));
134 i->set_min();
135 } else if (!strpfix("max:", type)) {
136 i = new invariant(get_SSFE(type + strlen("max:")));
137 i->set_max();
138 } else if (!strpfix("last:", type)) {
139 i = new invariant(get_SSFE(type + strlen("last:")));
140 i->set_last();
141 } else if (!strpfix("first:", type)) {
142 i = new invariant(get_SSFE(type + strlen("first:")));
143 i->set_first();
144 } else if (!strpfix("avg:", type)) {
145 i = new invariant(get_SSFE(type + strlen("avg:")));
146 i->set_avg();
147 } else if (!strpfix("median:", type)) {
148 i = new invariant(get_SSFE(type + strlen("median:")));
149 i->set_median();
150 } else {
151 i = new invariant(get_SSFE(type));
152 i->set_avg();
155 for (int index = 0; index < render::render_count(); index++)
156 if (typeid(*render::render_num(index)) == typeid(incremental)
157 && i->equals(((incremental *)render::render_num(index))->get_invariant())) {
158 delete i;
159 return (incremental *)render::render_num(index);
160 } else if (typeid(*i->ssfe()->get_scaled_filter()->get_filter()) == typeid(filter::zero)) {
161 return new zero(i);
164 // fprintf(stderr, " new '%s'.\n", type);
166 return new incremental(i);
169 static render *get_combination(const char *ptype, const char *dtype) {
170 render *partial = get_invariant(ptype);
171 render *_default = get(dtype);
172 for (int index = 0; index < render::render_count(); index++)
173 if (typeid(*render::render_num(index)) == typeid(combine)
174 && _default == ((combine *)render::render_num(index))->get_default()
175 && partial == ((combine *)render::render_num(index))->get_partial()) {
176 return render::render_num(index);
179 // fprintf(stderr, " new '%s,%s'.\n", ptype, dtype);
181 return new combine(_default, partial);
184 static render *get(const char *orig_type) {
185 char *type = strdup(orig_type);
186 if (type == NULL)
187 nomem();
189 char *comma_index = (char *) type;
190 while (*comma_index != '\0'
191 && *comma_index != ',')
192 comma_index++;
194 if (*comma_index == '\0') {
195 free(type);
196 return get_invariant(orig_type);
199 *comma_index = '\0';
200 render *result = get_combination(type, comma_index + 1);
201 *comma_index = ',';
203 free(type);
204 return result;
209 #endif