]> git.imager.perl.org - imager.git/blob - lib/Imager/Preprocess.pm
[rt.cpan.org #65385] Patch for Imager::Color->hsv
[imager.git] / lib / Imager / Preprocess.pm
1 package Imager::Preprocess;
2 use strict;
3 require Exporter;
4 use vars qw(@ISA @EXPORT $VERSION);
5
6 @EXPORT = qw(preprocess);
7 @ISA = qw(Exporter);
8
9 $VERSION = "1.000";
10
11
12 sub preprocess {
13   my $src = shift @ARGV;
14   my $dest = shift @ARGV
15     or usage();
16
17   open SRC, "< $src"
18   or die "Cannot open $src: $!\n";
19
20   my $cond;
21   my $cond_line;
22   my $save_code;
23   my @code;
24   my $code_line;
25   my @out;
26   my $failed;
27
28   push @out, 
29     "#define IM_ROUND_8(x) ((int)((x)+0.5))\n",
30     "#define IM_ROUND_double(x) (x)\n",
31     "#define IM_LIMIT_8(x) ((x) < 0 ? 0 : (x) > 255 ? 255 : (x))\n",
32     "#define IM_LIMIT_double(x) ((x) < 0.0 ? 0.0 : (x) > 1.0 ? 1.0 : (x))\n",
33     "#line 1 \"$src\"\n";
34   while (defined(my $line = <SRC>)) {
35     if ($line =~ /^\#code\s+(\S.+)$/) {
36       $save_code
37         and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
38       
39       $cond = $1;
40       $cond_line = $.;
41       $code_line = $. + 1;
42       $save_code = 1;
43     }
44     elsif ($line =~ /^\#code\s*$/) {
45       $save_code
46         and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
47       
48       $cond = '';
49       $cond_line = 0;
50       $code_line = $. + 1;
51       $save_code = 1;
52     }
53     elsif ($line =~ /^\#\/code\s*$/) {
54       $save_code
55         or do { warn "$src:$.:#/code without #code\n"; ++$failed; next; };
56       
57       if ($cond) {
58         push @out, "#line $cond_line \"$src\"\n";
59         push @out, "  if ($cond) {\n";
60       }
61       push @out,
62         "#undef IM_EIGHT_BIT\n",
63         "#define IM_EIGHT_BIT 1\n",
64         "#undef IM_FILL_COMBINE\n",
65         "#define IM_FILL_COMBINE(fill) ((fill)->combine)\n",
66         "#undef IM_FILL_FILLER\n",
67         "#define IM_FILL_FILLER(fill) ((fill)->f_fill_with_color)\n";
68       push @out, "#line $code_line \"$src\"\n";
69       push @out, byte_samples(@code);
70       push @out, "  }\n", "  else {\n"
71         if $cond;
72       push @out, 
73         "#undef IM_EIGHT_BIT\n",
74         "#undef IM_FILL_COMBINE\n",
75         "#define IM_FILL_COMBINE(fill) ((fill)->combinef)\n",
76         "#undef IM_FILL_FILLER\n",
77         "#define IM_FILL_FILLER(fill) ((fill)->f_fill_with_fcolor)\n";
78       push @out, "#line $code_line \"$src\"\n";
79       push @out, double_samples(@code);
80       push @out, "  }\n"
81         if $cond;
82       push @out, "#line ",$.+1," \"$src\"\n";
83       @code = ();
84       $save_code = 0;
85     }
86     elsif ($save_code) {
87       push @code, $line;
88     }
89     else {
90       push @out, $line;
91     }
92   }
93   
94   if ($save_code) {
95     warn "$src:$code_line:#code block not closed by EOF\n";
96     ++$failed;
97   }
98
99   close SRC;
100   
101   $failed 
102     and die "Errors during parsing, aborting\n";
103   
104   open DEST, "> $dest"
105     or die "Cannot open $dest: $!\n";
106   print DEST @out;
107   close DEST;
108 }
109   
110 sub byte_samples {
111   # important we make a copy
112   my @lines = @_;
113   
114   for (@lines) {
115     s/\bIM_GPIX\b/i_gpix/g;
116     s/\bIM_GLIN\b/i_glin/g;
117     s/\bIM_PPIX\b/i_ppix/g;
118     s/\bIM_PLIN\b/i_plin/g;
119     s/\bIM_GSAMP\b/i_gsamp/g;
120     s/\bIM_SAMPLE_MAX\b/255/g;
121     s/\bIM_SAMPLE_MAX2\b/65025/g;
122     s/\bIM_SAMPLE_T/i_sample_t/g;
123     s/\bIM_COLOR\b/i_color/g;
124     s/\bIM_WORK_T\b/int/g;
125     s/\bIM_Sf\b/"%d"/g;
126     s/\bIM_Wf\b/"%d"/g;
127     s/\bIM_SUFFIX\((\w+)\)/$1_8/g;
128     s/\bIM_ROUND\(/IM_ROUND_8(/g;
129     s/\bIM_ADAPT_COLORS\(/i_adapt_colors(/g;
130     s/\bIM_LIMIT\(/IM_LIMIT_8(/g;
131     s/\bIM_RENDER_LINE\(/i_render_line(/g;
132     s/\bIM_FILL_COMBINE_F\b/i_fill_combine_f/g;
133   }
134   
135   @lines;
136 }
137
138 sub double_samples {
139   # important we make a copy
140   my @lines = @_;
141   
142   for (@lines) {
143     s/\bIM_GPIX\b/i_gpixf/g;
144     s/\bIM_GLIN\b/i_glinf/g;
145     s/\bIM_PPIX\b/i_ppixf/g;
146     s/\bIM_PLIN\b/i_plinf/g;
147     s/\bIM_GSAMP\b/i_gsampf/g;
148     s/\bIM_SAMPLE_MAX\b/1.0/g;
149     s/\bIM_SAMPLE_MAX2\b/1.0/g;
150     s/\bIM_SAMPLE_T/i_fsample_t/g;
151     s/\bIM_COLOR\b/i_fcolor/g;
152     s/\bIM_WORK_T\b/double/g;
153     s/\bIM_Sf\b/"%f"/g;
154     s/\bIM_Wf\b/"%f"/g;
155     s/\bIM_SUFFIX\((\w+)\)/$1_double/g;
156     s/\bIM_ROUND\(/IM_ROUND_double(/g;
157     s/\bIM_ADAPT_COLORS\(/i_adapt_fcolors(/g;
158     s/\bIM_LIMIT\(/IM_LIMIT_double(/g;
159     s/\bIM_RENDER_LINE\(/i_render_linef(/g;
160     s/\bIM_FILL_COMBINE_F\b/i_fill_combinef_f/g;
161   }
162
163   @lines;
164 }
165
166 1;
167
168 __END__
169
170 =head1 NAME
171
172 =for stopwords preprocessor
173
174 Imager::Preprocess - simple preprocessor for handling multiple sample sizes
175
176 =head1 SYNOPSIS
177
178   /* in the source: */
179   #code condition true to work with 8-bit samples
180   ... code using preprocessor types/values ...
181   #/code
182
183   perl -MImager -epreprocess foo.im foo.c
184
185 =head1 DESCRIPTION
186
187 This is a simple preprocessor that aims to reduce duplication of
188 source code when implementing an algorithm both for 8-bit samples and
189 double samples in Imager.
190
191 Imager's C<Makefile.PL> currently scans the F<MANIFEST> for F<.im>
192 files and adds Makefile files to convert these to F<.c> files.
193
194 The beginning of a sample-independent section of code is preceded by:
195
196   #code expression
197
198 where I<expression> should return true if processing should be done at
199 8-bits/sample.
200
201 You can also use a #code block around a function definition to produce
202 8-bit and double sample versions of a function.  In this case #code
203 has no expression and you will need to use IM_SUFFIX() to produce
204 different function names.
205
206 The end of a sample-independent section of code is terminated by:
207
208   #/code
209
210 #code sections cannot be nested.
211
212 #/code without a starting #code is an error.
213
214 The following types and values are defined in a #code section:
215
216 =over
217
218 =item *
219
220 IM_GPIX(C<im>, C<x>, C<y>, C<&col>)
221
222 =item *
223
224 IM_GLIN(C<im>, C<l>, C<r>, C<y>, C<colors>)
225
226 =item *
227
228 IM_PPIX(C<im>, C<x>, C<y>, C<&col>)
229
230 =item *
231
232 IM_PLIN(C<im>, C<x>, C<y>, C<colors>)
233
234 =item *
235
236 IM_GSAMP(C<im>, C<l>, C<r>, C<y>, C<samples>, C<chans>, C<chan_count>)
237
238 These correspond to the appropriate image function, eg. IM_GPIX()
239 becomes i_gpix() or i_gpixf() as appropriate.
240
241 =item *
242
243 IM_ADAPT_COLORS(C<dest_channels>, C<src_channels>, C<colors>, C<count>)
244
245 Call i_adapt_colors() or i_adapt_fcolors().
246
247 =item *
248
249 IM_FILL_COMBINE(C<fill>) - retrieve the combine function from a fill
250 object.
251
252 =item *
253
254 IM_FILL_FILLER(C<fill>) - retrieve the fill_with_* function from a fill
255 object.
256
257 =item *
258
259 IM_SAMPLE_MAX - maximum value for a sample
260
261 =item *
262
263 IM_SAMPLE_MAX2 - maximum value for a sample, squared
264
265 =item *
266
267 IM_SAMPLE_T - type of a sample (i_sample_t or i_fsample_t)
268
269 =item *
270
271 IM_COLOR - color type, either i_color or i_fcolor.
272
273 =item *
274
275 IM_WORK_T - working sample type, either int or double.
276
277 =item *
278
279 IM_Sf - format string for the sample type, C<"%d"> or C<"%f">.
280
281 =item *
282
283 IM_Wf - format string for the work type, C<"%d"> or C<"%f">.
284
285 =item *
286
287 IM_SUFFIX(identifier) - adds _8 or _double onto the end of identifier.
288
289 =item *
290
291 IM_EIGHT_BIT - this is a macro defined only in 8-bit/sample code.
292
293 =back
294
295 Other types, functions and values may be added in the future.
296
297 =head1 AUTHOR
298
299 Tony Cook <tony@imager.perl.org>
300
301 =cut