1 #include "config.h"
2 #include "ecl.h"
3 #include "assert.h"
4 #include <stdio.h>
5 
6 #define IMGDEBUG
7 
8 #if defined(IMGDEBUG)
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 
14 #include "img1.h"
15 #include "img2.h"
16 
17 int fd;
18 #endif
19 
20 static char extern_string[] = "int printf(string format, ...);";
21 static ecl_extern_entry externs[] =
22 {
23     {"printf", (void*)(long)printf},
24     {(void*)0, (void*)0}
25 };
26 
27 /*
28   int catImage(char in1[100*100*3],
29                char in2[100*100*3],
30                char out[200*100*3])
31 */
32 char catImage[] = "\n\
33 {\n\
34   int col;\n\
35   int row;\n\
36   int indx1;\n\
37   int indx2;\n\
38   int outIndx;\n\
39 \n\
40   indx1 = 0;\n\
41   indx2 = 0;\n\
42   outIndx = 0;\n\
43 \n\
44   for (row = 0; row < 100; row = row + 1) {\n\
45     for (col = 0; col < 100; col = col + 1) {\n\
46       output.out[outIndx]   = input1.in[indx1];   /* red */\n\
47       output.out[outIndx+1] = input1.in[indx1+1]; /* green */\n\
48       output.out[outIndx+2] = input1.in[indx1+2]; /* blue */\n\
49       outIndx = outIndx + 3;\n\
50       indx1 = indx1 + 3;\n\
51     }\n\
52     for (col = 0; col < 100; col = col + 1) {\n\
53       output.out[outIndx]   = input2.in[indx2];   /* red */\n\
54       output.out[outIndx+1] = input2.in[indx2+1]; /* green */\n\
55       output.out[outIndx+2] = input2.in[indx2+2]; /* blue */\n\
56       outIndx = outIndx + 3;\n\
57       indx2 = indx2 + 3;\n\
58     }\n\
59   }\n\
60 \n\
61   return 0;\n\
62 }";
63 
64 
65 /*
66   int cropImage(char in [100*100*3],
67                 char out[50*50*3],
68                 int inRow, int inCol)
69  */
70 char cropImage[] = "\n\
71 {\n\
72 \n\
73   int row;\n\
74   int col;\n\
75   int outIndx;\n\
76   int inIndx;\n\
77 \n\
78   for (row = 0; row < 50; row = row + 1) {\n\
79     for (col = 0; col < 50; col = col + 1) {\n\
80       outIndx = (row * 50 * 3) + (col * 3);\n\
81       inIndx = ((inRow + row) * 100 * 3) + (inCol + col) * 3;\n\
82       output.out[outIndx]   = input.in[inIndx];\n\
83       output.out[outIndx+1] = input.in[inIndx+1];\n\
84       output.out[outIndx+2] = input.in[inIndx+2];\n\
85     }\n\
86   }\n\
87   \n\
88   return 0;\n\
89 }";
90 
91 
92 /*
93   int edgeImage(char in [100*100*3],
94                 char out[100*100*3])
95  */
96 char edgeImage[] = "\n\
97 {\n\
98   int col;\n\
99   int row;\n\
100   int indx;\n\
101   int outIndx;\n\
102   int row0Indx;\n\
103   int row1Indx;\n\
104   int row2Indx;\n\
105   int tmprowIndx;\n\
106 \n\
107   double sum1;\n\
108   double sum2;\n\
109   double sum;\n\
110   double abssum1;\n\
111   double abssum2;\n\
112 \n\
113   /* Get the first two rows. */\n\
114   row0Indx = 0;\n\
115   row1Indx = 100;\n\
116 \n\
117   /* Write out the first row, all zeros. */\n\
118   for (col = 0; col < 100; col = col + 1)\n\
119     output.out[col] = 0;\n\
120 \n\
121   /* Now the rest of the image -- read in the next row, and write\n\
122   ** write out the current row.\n\
123   */\n\
124   for (row = 1; row < (100 - 1); row = row + 1) {\n\
125     indx = (row - 1) * 100;\n\
126     outIndx = (row - 1) * 100;\n\
127     row2Indx = indx;\n\
128 \n\
129     for ( col = 1; col < 100 - 1; col = col + 1 ) {\n\
130       sum1 = (double) input.in[row0Indx+col+1] - \n\
131 	(double) input.in[row0Indx+col-1] +\n\
132 	2.0 * ( (double) input.in[row1Indx+col+1] - \n\
133 		(double) input.in[row1Indx+col-1] ) +\n\
134 	(double) input.in[row2Indx+col+1] - \n\
135 	(double) input.in[row2Indx+col-1];\n\
136 \n\
137       sum2 = ( (double) input.in[row2Indx+col-1] + \n\
138 	       2.0 * (double) input.in[row2Indx+col] +\n\
139 	       (double) input.in[row2Indx+col+1] ) -\n\
140 	( (double) input.in[row0Indx+col-1] + \n\
141 	  2.0 * (double) input.in[row0Indx+col] +\n\
142 	  (double) input.in[row0Indx+col+1] );\n\
143       \n\
144       /* \n\
145        * The following 8 lines of code are just to avoid using sqrt since\n\
146        * we cannot 'move' it into E-code.\n\
147        * Now, \"why 0.2 of the smallest one?\" you would ask: because I say\n\
148        * so.\n\
149        *\n\
150        * sum = sqrt( sum1 * sum1 + sum2 * sum2 ); \n\
151        *\n\
152        */\n\
153 \n\
154       if (sum1 < 0.0) abssum1 = -sum1;\n\
155       else abssum1 = sum1;\n\
156       if (sum2 < 0.0) abssum2 = -sum2;\n\
157       else abssum2 = sum2;\n\
158 \n\
159       if (abssum1 == 0.0) sum = abssum2;\n\
160       if (abssum2 == 0.0) sum = abssum1;\n\
161 \n\
162       if (abssum1 > abssum2) sum = abssum1 + 0.2 * abssum2;\n\
163       else sum = 0.2 * abssum1 + abssum2;\n\
164 \n\
165       sum = sum / 1.8;		/* arbitrary scaling factor */\n\
166       if ( sum > 255 ) sum = 255;\n\
167       output.out[outIndx + col] = sum;\n\
168     }\n\
169     \n\
170     /* Rotate rows. */\n\
171     tmprowIndx = row0Indx;\n\
172     row0Indx = row1Indx;\n\
173     row1Indx = row2Indx;\n\
174     row2Indx = tmprowIndx;\n\
175   }\n\
176   \n\
177   /* And write the last row, zeros again. */\n\
178   outIndx = (100 - 2) * 100;\n\
179   for (col = 0; col < 100; col = col + 1)\n\
180     output.out[outIndx + col] = 0;\n\
181   \n\
182   return 0;\n\
183 }";
184 
185 /*
186   int enlargeImage(char in [100*100*3],
187        	           char out[200*200*3],
188 		   int  n = 2)
189  */
190 char enlargeImage[] = "\n\
191 {\n\
192   int col;\n\
193   int subcol;\n\
194   int row;\n\
195   int subrow;\n\
196 \n\
197   int rowIndx;\n\
198   int outIndx;\n\
199   int colIndx;\n\
200   int outColIndx;\n\
201   int outRowIndx;\n\
202 \n\
203   outRowIndx = 0;\n\
204   for (row = 0; row < 100; row = row + 1) {\n\
205     rowIndx = row * 100 * 3;\n\
206     for (subrow = 0; subrow < n; subrow = subrow + 1) {\n\
207     col = 0;\n\
208     colIndx = 0;\n\
209       for (; col < 100;) {\n\
210 	outIndx = outRowIndx * 200 * 3;\n\
211 \n\
212         subcol = 0;\n\
213         outColIndx = (col * n * 3);\n\
214 	for (; subcol < n;) {\n\
215 	  output.out[outIndx+outColIndx+0] = input.in[rowIndx+colIndx+0];\n\
216 	  output.out[outIndx+outColIndx+1] = input.in[rowIndx+colIndx+1];\n\
217 	  output.out[outIndx+outColIndx+2] = input.in[rowIndx+colIndx+2];\n\
218 	  subcol = subcol + 1;\n\
219 	  outColIndx = outColIndx + 3;\n\
220 	}\n\
221         col = col + 1;\n\
222 	colIndx = colIndx + 3;\n\
223       }\n\
224       outRowIndx = outRowIndx + 1;\n\
225     }\n\
226   }\n\
227 \n\
228   return 0;\n\
229 }";
230 
231 /*
232   int greyImage(unsigned char in [100*100*3],
233 	        unsigned char out[100*100])
234  */
235 char greyImage[] = "\n\
236 {\n\
237   int col;\n\
238   int row;\n\
239   int indx;\n\
240   int outIndx;\n\
241 \n\
242   indx = 0;\n\
243   outIndx = 0;\n\
244   for (row = 0; row < 100; row = row + 1) {\n\
245     for ( col = 0; col < 100; col = col + 1) {\n\
246       output.out[outIndx] =  \n\
247 	(unsigned char) ( (0.299 * input.in[indx]   +\n\
248 			   0.587 * input.in[indx+1] +\n\
249 			   0.114 * input.in[indx+2]) + \n\
250 			  0.5 );\n\
251       indx = indx + 3;\n\
252       outIndx = outIndx + 1;\n\
253     }\n\
254   }\n\
255   return 0;\n\
256 }";
257 
258 /*
259   int intGreyImage(unsigned char in [100*100*3],
260 	           unsigned char out[100*100])
261  */
262 char intGreyImage[] = "\n\
263 {\n\
264   int col;\n\
265   int row;\n\
266   int indx;\n\
267   int outIndx;\n\
268 \n\
269   indx = 0;\n\
270   outIndx = 0;\n\
271   for (row = 0; row < 100; row = row + 1) {\n\
272     for ( col = 0; col < 100; col = col + 1) {\n\
273       output.out[outIndx] =  \n\
274 	(unsigned char) (1 * (input.in[indx]   >> 2) +\n\
275 			 5 * (input.in[indx+1] >> 3) +\n\
276 			 1 * (input.in[indx+2] >> 3));\n\
277       indx = indx + 3;\n\
278       outIndx = outIndx + 1;\n\
279     }\n\
280   }\n\
281   return 0;\n\
282 }";
283 
284 
285 /*
286   int mixImage(unsigned char in1[100*100*3],
287 	       unsigned char in2[100*100*3],
288 	       unsigned char out[100*100*3],
289 	       float fade)
290  */
291 char mixImage[] = "\n\
292 {\n\
293   int col;\n\
294   int row;\n\
295   int indx;\n\
296   long longfactor;\n\
297 \n\
298   longfactor = (long)(fade * 65536);\n\
299   indx = 0;\n\
300   for (row = 0; row < 100; row = row + 1) {\n\
301     for (col = 0; col < 100; col = col + 1) {\n\
302 \n\
303       output.out[indx] = input1.in[indx] + \n\
304 	(((input2.in[indx] - input1.in[indx]) \n\
305 	  * longfactor) >> 16);\n\
306 \n\
307       output.out[indx+1] = input1.in[indx+1] + \n\
308 	(((input2.in[indx+1] - input1.in[indx+1]) \n\
309 	  * longfactor) >> 16);\n\
310 \n\
311       output.out[indx+2] = input1.in[indx+2] + \n\
312 	(((input2.in[indx+2] - input1.in[indx+2]) \n\
313 	  * longfactor) >> 16);\n\
314       \n\
315       indx = indx + 3;\n\
316     }\n\
317   }\n\
318 \n\
319   return 0;\n\
320 }";
321 
322 /*
323   int reduceImage(unsigned char in [100*100*3],
324 		  unsigned char out[50*50*3],
325 		  int  n = 2)
326  */
327 char reduceImage[] = "\n\
328 {\n\
329   int row;\n\
330   int inRow;\n\
331   int col;\n\
332   int inCol;\n\
333   int suprow;\n\
334   int supcol;\n\
335   int inIndx;\n\
336   int outIndx;\n\
337   int sumR;\n\
338   int sumG;\n\
339   int sumB;\n\
340   int n2 = n * n;\n\
341 \n\
342   for ( row = 0; row < 50; row = row + 1) {\n\
343     for (col = 0; col < 50; col = col + 1) {\n\
344       outIndx = (row * 50 * 3) + col * 3;\n\
345       sumR = 0; sumG = 0; sumB = 0;\n\
346       for (suprow = 0; suprow < n; suprow = suprow + 1) {\n\
347 	inRow = row * n + suprow;\n\
348 	for (supcol = 0; supcol < n; supcol = supcol + 1) {\n\
349 	  inCol = col * n + supcol;\n\
350 	  inIndx = (inRow * 100 * 3) + inCol * 3;\n\
351 	  sumR = sumR + input.in[inIndx];\n\
352 	  sumG = sumG + input.in[inIndx+1];\n\
353 	  sumB = sumB + input.in[inIndx+2];\n\
354 	}\n\
355       }\n\
356       output.out[outIndx+0] = sumR / n2;\n\
357       output.out[outIndx+1] = sumG / n2;\n\
358       output.out[outIndx+2] = sumB / n2;\n\
359     }\n\
360   }\n\
361   \n\
362   return 0;\n\
363 }";
364 
365 #define rdtsc(x)  __asm__ __volatile__("rdtsc\n\t" : "=A" (x));
366 
test_catImage()367 void test_catImage()
368 {
369 /*
370   int catImage(char in1[100*100*3],
371                char in2[100*100*3],
372                char out[200*100*3])
373 */
374   unsigned long long t1, t2;
375 
376   static IOField input1_field_list[] = {
377     {"in", "char[30000]", sizeof(char), 0},
378     {(void*)0, (void*)0, 0, 0}
379   };
380 
381   static IOField input2_field_list[] = {
382     {"in", "char[30000]", sizeof(char), 0},
383     {(void*)0, (void*)0, 0, 0}
384   };
385 
386   static IOField output_field_list[] = {
387     {"out", "char[60000]", sizeof(char), 0},
388     {(void*)0, (void*)0, 0, 0}
389   };
390 
391   char out[200*100*3];
392 
393   ecl_parse_context context = new_ecl_parse_context();
394   ecl_code gen_code;
395   long (*func)(char *, char *, char *), result;
396 
397   ecl_add_struct_type("input1_type", input1_field_list, context);
398   ecl_add_struct_type("input2_type", input2_field_list, context);
399   ecl_add_struct_type("output_type", output_field_list, context);
400   ecl_subroutine_declaration("int proc(input1_type input1, input2_type input2, output_type output)", context);
401 
402 
403   gen_code = ecl_code_gen(catImage, context);
404   func = (long (*)(char *, char *, char *)) (long) gen_code->func;
405 
406   if(!func) return;
407 
408   result = func(img1, img2, out);
409 
410   rdtsc(t1);
411   result = func(img1, img2, out);
412   rdtsc(t2);
413 
414   t2 -= t1;
415   printf("%0lld\n", t2);
416 
417   ecl_code_free(gen_code);
418   ecl_free_parse_context(context);
419 
420 #if defined(IMGDEBUG)
421   unlink("out.pnm");
422   fd = open("out.pnm", O_CREAT|O_TRUNC|O_RDWR, 0644);
423   if(fd < 0) {
424     perror("open");
425     exit(-1);
426   }
427 
428   if(write(fd, "P6\n200 100\n255\n", 15) != 15) {
429     perror("write");
430     exit(-1);
431   }
432 
433   if(write(fd, out, 200*100*3) != 200*100*3) {
434     perror("write");
435     exit(-1);
436   }
437 
438   close(fd);
439 #endif
440 }
441 
test_enlargeImage()442 void test_enlargeImage()
443 {
444 /*
445   int enlargeImage(char in [100*100*3],
446 	           char out[200*200*3],
447 		   int n)
448  */
449 
450   unsigned long long t1, t2;
451 
452   static IOField input_field_list[] = {
453     {"in",  "char[30000]", sizeof(char), 0},
454     {(void*)0, (void*)0, 0, 0}
455   };
456 
457   static IOField output_field_list[] = {
458     {"out",  "char[120000]", sizeof(char), 0},
459     {(void*)0, (void*)0, 0, 0}
460   };
461 
462   char out[200*200*3];
463 
464   ecl_parse_context context = new_ecl_parse_context();
465   ecl_code gen_code;
466   long (*func)(char *, char *, int), result;
467 
468   ecl_add_struct_type("input_type", input_field_list, context);
469   ecl_add_struct_type("output_type", output_field_list, context);
470   ecl_subroutine_declaration("int proc(input_type input, output_type output, int n)", context);
471 
472   gen_code = ecl_code_gen(enlargeImage, context);
473   func = (long (*)(char *, char *, int)) (long) gen_code->func;
474 
475   if(!func) return;
476 
477   result = func(img1, out, 2);
478 
479   rdtsc(t1);
480   result = func(img1, out, 2);
481   rdtsc(t2);
482 
483   t2 -= t1;
484   printf("%0lld\n", t2);
485 
486   ecl_code_free(gen_code);
487   ecl_free_parse_context(context);
488 
489 #if defined(IMGDEBUG)
490   unlink("out.pnm");
491   fd = open("out.pnm", O_CREAT|O_TRUNC|O_RDWR, 0644);
492   if(fd < 0) {
493     perror("open");
494     exit(-1);
495   }
496 
497   if(write(fd, "P6\n200 200\n255\n", 15) != 15) {
498     perror("write");
499     exit(-1);
500   }
501 
502   if(write(fd, out, 200*200*3) != 200*200*3) {
503     perror("write");
504     exit(-1);
505   }
506 
507   close(fd);
508 #endif
509 }
510 
test_reduceImage()511 void test_reduceImage()
512 {
513 /*
514   int reduceImage(unsigned char in [100*100*3],
515 		  unsigned char out[50*50*3],
516 		  int  n = 2)
517  */
518   unsigned long long t1, t2;
519 
520   static IOField input_field_list[] = {
521     {"in",  "unsigned integer[30000]", sizeof(unsigned char), 0},
522     {(void*)0, (void*)0, 0, 0}
523   };
524 
525   static IOField output_field_list[] = {
526     {"out",  "unsigned integer[7500]", sizeof(unsigned char), 0},
527     {(void*)0, (void*)0, 0, 0}
528   };
529 
530   unsigned char out[50*50*3];
531 
532   ecl_parse_context context = new_ecl_parse_context();
533   ecl_code gen_code;
534   long (*func)(char *, char *, int), result;
535 
536   ecl_add_struct_type("input_type", input_field_list, context);
537   ecl_add_struct_type("output_type", output_field_list, context);
538   ecl_subroutine_declaration("int proc(input_type input, output_type output, int n)", context);
539 
540 
541   gen_code = ecl_code_gen(reduceImage, context);
542   func = (long (*)(char *, char *, int)) (long) gen_code->func;
543 
544   if(!func) return;
545 
546   result = func(img1, out, 2);
547 
548   rdtsc(t1);
549   result = func(img1, out, 2);
550   rdtsc(t2);
551 
552   t2 -= t1;
553   printf("%0lld\n", t2);
554 
555   ecl_code_free(gen_code);
556   ecl_free_parse_context(context);
557 
558 #if defined(IMGDEBUG)
559   unlink("out.pnm");
560   fd = open("out.pnm", O_CREAT|O_TRUNC|O_RDWR, 0644);
561   if(fd < 0) {
562     perror("open");
563     exit(-1);
564   }
565 
566   if(write(fd, "P6\n50 50\n255\n", 13) != 13) {
567     perror("write");
568     exit(-1);
569   }
570 
571   if(write(fd, out, 50*50*3) != 50*50*3) {
572     perror("write");
573     exit(-1);
574   }
575 
576   close(fd);
577 #endif
578 }
579 
test_intGreyImage()580 void test_intGreyImage()
581 {
582 /*
583   int intGreyImage(unsigned char in [100*100*3],
584 	           unsigned char out[100*100])
585  */
586 
587   unsigned long long t1, t2;
588 
589   static IOField input_field_list[] = {
590     {"in",  "unsigned integer[30000]", sizeof(unsigned char), 0},
591     {(void*)0, (void*)0, 0, 0}
592   };
593 
594   static IOField output_field_list[] = {
595     {"out", "unsigned integer[30000]", sizeof(unsigned char), 0},
596     {(void*)0, (void*)0, 0, 0}
597   };
598 
599   unsigned char out[100*100];
600 
601   ecl_parse_context context = new_ecl_parse_context();
602   ecl_code gen_code;
603   long (*func)(char *, char *), result;
604 
605 
606   ecl_add_struct_type("input_type", input_field_list, context);
607   ecl_add_struct_type("output_type", output_field_list, context);
608   ecl_subroutine_declaration("int proc(input_type input, output_type output)", context);
609 
610   gen_code = ecl_code_gen(intGreyImage, context);
611   func = (long (*)(char *, char *)) (long) gen_code->func;
612 
613   if(!func) return;
614 
615   result = func(img1, out);
616 
617   rdtsc(t1);
618   result = func(img1, out);
619   rdtsc(t2);
620 
621   t2 -= t1;
622   printf("%0lld\n", t2);
623 
624   ecl_code_free(gen_code);
625   ecl_free_parse_context(context);
626 
627 #if defined(IMGDEBUG)
628   unlink("out.pnm");
629   fd = open("out.pnm", O_CREAT|O_TRUNC|O_RDWR, 0644);
630   if(fd < 0) {
631     perror("open");
632     exit(-1);
633   }
634 
635   if(write(fd, "P5\n100 100\n255\n", 15) != 15) {
636     perror("write");
637     exit(-1);
638   }
639 
640   if(write(fd, out, 100*100) != 100*100) {
641     perror("write");
642     exit(-1);
643   }
644 
645   close(fd);
646 #endif
647 }
648 
test_greyImage()649 void test_greyImage()
650 {
651 /*
652   int greyImage(unsigned char in [100*100*3],
653 	        unsigned char out[100*100])
654  */
655 
656   unsigned long long t1, t2;
657 
658   static IOField input_field_list[] = {
659     {"in",  "unsigned integer[30000]", sizeof(unsigned char), 0},
660     {(void*)0, (void*)0, 0, 0}
661   };
662 
663   static IOField output_field_list[] = {
664     {"out", "unsigned integer[10000]", sizeof(unsigned char), 0},
665     {(void*)0, (void*)0, 0, 0}
666   };
667 
668   unsigned char out[100*100];
669 
670   ecl_parse_context context = new_ecl_parse_context();
671   ecl_code gen_code;
672   long (*func)(char *, char *), result;
673 
674 
675   ecl_add_struct_type("input_type", input_field_list, context);
676   ecl_add_struct_type("output_type", output_field_list, context);
677   ecl_subroutine_declaration("int proc(input_type input, output_type output)", context);
678 
679   gen_code = ecl_code_gen(greyImage, context);
680   func = (long (*)(char *, char *)) (long) gen_code->func;
681 
682   if(!func) return;
683 
684   result = func(img1, out);
685 
686   rdtsc(t1);
687   result = func(img1, out);
688   rdtsc(t2);
689 
690   t2 -= t1;
691   printf("%0lld\n", t2);
692 
693   ecl_code_free(gen_code);
694   ecl_free_parse_context(context);
695 
696 #if defined(IMGDEBUG)
697   unlink("out.pnm");
698   fd = open("out.pnm", O_CREAT|O_TRUNC|O_RDWR, 0644);
699   if(fd < 0) {
700     perror("open");
701     exit(-1);
702   }
703 
704   if(write(fd, "P5\n100 100\n255\n", 15) != 15) {
705     perror("write");
706     exit(-1);
707   }
708 
709   if(write(fd, out, 100*100) != 100*100) {
710     perror("write");
711     exit(-1);
712   }
713 
714   close(fd);
715 #endif
716 }
717 
test_edgeImage()718 void test_edgeImage()
719 {
720 /*
721   int edgeImage(char in [100*100*3],
722                 char out[100*100*3])
723  */
724   unsigned long long t1, t2;
725 
726   static IOField input_field_list[] = {
727     {"in",  "char[30000]", sizeof(char), 0},
728     {(void*)0, (void*)0, 0, 0}
729   };
730 
731   static IOField output_field_list[] = {
732     {"out", "char[30000]", sizeof(char), 0},
733     {(void*)0, (void*)0, 0, 0}
734   };
735 
736   char in [100*100*3];
737   char out[100*100*3];
738 
739   ecl_parse_context context = new_ecl_parse_context();
740   ecl_code gen_code;
741   long (*func)(char *, char *), result;
742 
743 
744   ecl_add_struct_type("input_type", input_field_list, context);
745   ecl_add_struct_type("output_type", output_field_list, context);
746   ecl_subroutine_declaration("int proc(input_type input, output_type output)", context);
747 
748   gen_code = ecl_code_gen(edgeImage, context);
749   func = (long (*)(char *, char *)) (long) gen_code->func;
750 
751   if(!func) return;
752 
753   result = func(in, out);
754 
755   rdtsc(t1);
756   result = func(in, out);
757   rdtsc(t2);
758 
759   t2 -= t1;
760   printf("%0lld\n", t2);
761 
762   ecl_code_free(gen_code);
763   ecl_free_parse_context(context);
764 }
765 
test_cropImage()766 void test_cropImage()
767 {
768 /*
769   int cropImage(char in [100*100*3],
770                 char out[50*50*3],
771                 int inRow, int inCol)
772  */
773   unsigned long long t1, t2;
774 
775   static IOField input_field_list[] = {
776     {"in",  "char[30000]", sizeof(char), 0},
777     {(void*)0, (void*)0, 0, 0}
778   };
779 
780   static IOField output_field_list[] = {
781     {"out",  "char[7500]", sizeof(char), 0},
782     {(void*)0, (void*)0, 0, 0}
783   };
784 
785   char out[50*50*3];
786 
787   ecl_parse_context context = new_ecl_parse_context();
788   ecl_code gen_code;
789   long (*func)(char *, char *, int, int), result;
790 
791   ecl_add_struct_type("input_type", input_field_list, context);
792   ecl_add_struct_type("output_type", output_field_list, context);
793   ecl_subroutine_declaration("int proc(input_type input, output_type output, int inRow, int inCol)", context);
794 
795 
796   gen_code = ecl_code_gen(cropImage, context);
797   func = (long (*)(char *, char *, int, int)) (long) gen_code->func;
798 
799   if(!func) return;
800 
801   result = func(img1, out, 25, 25);
802 
803   rdtsc(t1);
804   result = func(img1, out, 25, 25);
805   rdtsc(t2);
806 
807   t2 -= t1;
808   printf("%0lld\n", t2);
809 
810   ecl_code_free(gen_code);
811   ecl_free_parse_context(context);
812 
813 #if defined(IMGDEBUG)
814   unlink("out.pnm");
815   fd = open("out.pnm", O_CREAT|O_TRUNC|O_RDWR, 0644);
816   if(fd < 0) {
817     perror("open");
818     exit(-1);
819   }
820 
821   if(write(fd, "P6\n50 50\n255\n", 13) != 13) {
822     perror("write");
823     exit(-1);
824   }
825 
826   if(write(fd, out, 50*50*3) != 50*50*3) {
827     perror("write");
828     exit(-1);
829   }
830 
831   close(fd);
832 #endif
833 }
834 
test_mixImage()835 void test_mixImage()
836 {
837 /*
838   int mixImage(unsigned char in1[100*100*3],
839 	       unsigned char in2[100*100*3],
840 	       unsigned char out[100*100*3],
841 	       float fade)
842  */
843   unsigned long long t1, t2;
844 
845   static IOField input1_field_list[] = {
846     {"in", "unsigned integer[30000]", sizeof(unsigned char), 0},
847     {(void*)0, (void*)0, 0, 0}
848   };
849 
850   static IOField input2_field_list[] = {
851     {"in", "unsigned integer[30000]", sizeof(unsigned char), 0},
852     {(void*)0, (void*)0, 0, 0}
853   };
854 
855   static IOField output_field_list[] = {
856     {"out", "unsigned integer[30000]", sizeof(unsigned char), 0},
857     {(void*)0, (void*)0, 0, 0}
858   };
859 
860   unsigned char out[100*100*3];
861 
862   ecl_parse_context context = new_ecl_parse_context();
863   ecl_code gen_code;
864   long (*func)(char *, char *, char *, int), result;
865 
866   ecl_assoc_externs(context, externs);
867   ecl_parse_for_context(extern_string, context);
868 
869   ecl_add_struct_type("input1_type", input1_field_list, context);
870   ecl_add_struct_type("input2_type", input2_field_list, context);
871   ecl_add_struct_type("output_type", output_field_list, context);
872   ecl_subroutine_declaration("int proc(input1_type input1, input2_type input2, output_type output, float fade)", context);
873 
874   gen_code = ecl_code_gen(mixImage, context);
875   func = (long (*)(char *, char *, char *, int)) (long) gen_code->func;
876 
877   if(!func) return;
878 
879   result = func(img1, img2, out, 1.0);
880 
881   rdtsc(t1);
882   result = func(img1, img2, out, 1.0);
883   rdtsc(t2);
884 
885   t2 -= t1;
886   printf("%0lld\n", t2);
887 
888   ecl_code_free(gen_code);
889   ecl_free_parse_context(context);
890 
891 #if defined(IMGDEBUG)
892   unlink("out.pnm");
893   fd = open("out.pnm", O_CREAT|O_TRUNC|O_RDWR, 0644);
894   if(fd < 0) {
895     perror("open");
896     exit(-1);
897   }
898 
899   if(write(fd, "P6\n100 100\n255\n", 15) != 15) {
900     perror("write");
901     exit(-1);
902   }
903 
904   if(write(fd, out, 100*100*3) != 100*100*3) {
905     perror("write");
906     exit(-1);
907   }
908 
909   close(fd);
910 #endif
911 }
912 
main(int argc,char ** argv)913 int main(int argc, char **argv)
914 {
915   if(argc != 2 || !*argv[1] || argv[1][1] || *argv[1]<'1' || *argv[1]>'7') {
916     fprintf(stderr, "Usage: imgxform <test-number>\n");
917     fprintf(stderr, "\t(1) enlargeImage()\n"
918 	    "\t(2) reduceImage()\n"
919 	    "\t(3) intGreyImage()\n"
920 	    "\t(4) greyImage()\n"
921 	    "\t(5) catImage()\n"
922 	    "\t(6) cropImage()\n"
923 	    "\t(7) mixImage()\n");
924     return -1;
925   }
926 
927   switch(*argv[1]) {
928   case '1':
929     test_enlargeImage();
930     break;
931   case '2':
932     test_reduceImage();
933     break;
934   case '3':
935     test_intGreyImage();
936     break;
937   case '4':
938     test_greyImage();
939     break;
940   case '5':
941     test_catImage();
942     break;
943   case '6':
944     test_cropImage();
945     break;
946   case '7':
947     test_mixImage();
948     break;
949   default:
950     printf("No such choice!\n");
951     return -1;
952   }
953   return 0;
954 }
955