1 #include "config.h"
2 #include "cod.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 int fd;
15 #endif
16 static char mixImage[] = "\n\
17 {\n\
18   int col;\n\
19   int row;\n\
20   int indx;\n\
21   long longfactor;\n\
22 \n\
23   longfactor = (long)(fade * 65536);\n\
24   indx = 0;\n\
25   for (row = 0; row < 100; row = row + 1) {\n\
26     for (col = 0; col < 100; col = col + 1) {\n\
27 \n\
28       output.out[indx] = input1.in[indx] + \n\
29 	(((input2.in[indx] - input1.in[indx]) \n\
30 	  * longfactor) >> 16);\n\
31 \n\
32       output.out[indx+1] = input1.in[indx+1] + \n\
33 	(((input2.in[indx+1] - input1.in[indx+1]) \n\
34 	  * longfactor) >> 16);\n\
35 \n\
36       output.out[indx+2] = input1.in[indx+2] + \n\
37 	(((input2.in[indx+2] - input1.in[indx+2]) \n\
38 	  * longfactor) >> 16);\n\
39       \n\
40       indx = indx + 3;\n\
41     }\n\
42   }\n\
43 \n\
44   return 0;\n\
45 }";
46 
47 static char extern_string[] = "int printf(string format, ...);";
48 static cod_extern_entry externs[] =
49 {
50     {"printf", (void*)(long)printf},
51     {(void*)0, (void*)0}
52 };
53 
54 /*
55   int mixImage(unsigned char in1[100*100*3],
56 	       unsigned char in2[100*100*3],
57 	       unsigned char out[100*100*3],
58 	       float fade)
59  */
60 
61 
main()62 int main()
63 {
64     static FMField input1_field_list[] = {
65 	{"in", "unsigned integer[30000]", sizeof(unsigned char), 0},
66 	{(void*)0, (void*)0, 0, 0}
67     };
68 
69     static FMField input2_field_list[] = {
70 	{"in", "unsigned integer[30000]", sizeof(unsigned char), 0},
71 	{(void*)0, (void*)0, 0, 0}
72     };
73 
74     static FMField output_field_list[] = {
75 	{"out", "unsigned integer[30000]", sizeof(unsigned char), 0},
76 	{(void*)0, (void*)0, 0, 0}
77     };
78     int i, index;
79 
80     unsigned char out[100*100*3];
81     unsigned char img1a[100*100*3];
82     unsigned char img2a[100*100*3];
83 
84     cod_parse_context context = new_cod_parse_context();
85     cod_code gen_code;
86     long (*func)(unsigned char *, unsigned char *, unsigned char *, float);
87 
88     cod_assoc_externs(context, externs);
89     cod_parse_for_context(extern_string, context);
90 
91     cod_add_simple_struct_type("input1_type", input1_field_list, context);
92     cod_add_simple_struct_type("input2_type", input2_field_list, context);
93     cod_add_simple_struct_type("output_type", output_field_list, context);
94     cod_subroutine_declaration("void proc(input1_type *input1, input2_type *input2, output_type output, float fade)", context);
95 
96 
97     gen_code = cod_code_gen(mixImage, context);
98     func = (long (*)(unsigned char *, unsigned char *, unsigned char *, float)) (long) gen_code->func;
99 
100     if(!func) return 1;
101 
102     index = 0;
103     for(i = 0; i < 100; i++) {
104 	int j;
105 	for(j = 0; j < 100; j++) {
106 	    int k;
107 	    for(k = 0; k < 3; k++) {
108 		img1a[index] = 0xfa;
109 		img2a[index] = 0x1a;
110 		index++;
111 	    }
112 	}
113     }
114     (void) func(img1a, img2a, out, 0.5);
115 
116     index = 0;
117     for(i = 0; i < 100; i++) {
118 	int j;
119 	for(j = 0; j < 100; j++) {
120 	    int k;
121 	    for(k = 0; k < 3; k++) {
122 		if (out[index] != 0x8a) {
123 		    printf("Out element [%d,%d,%d] is incorrect -> 0x%x\n",
124 			   i, j, k, out[index]);
125 		}
126 		index++;
127 	    }
128 	}
129     }
130 
131     cod_code_free(gen_code);
132     cod_free_parse_context(context);
133     return 0;
134 }
135 
136