Imported Upstream version 4.6.0
[debian/atlc] / src / non_gui / setup_arrays.c
1 /* atlc - arbitrary transmission line calculator, for the analysis of
2 transmission lines are directional couplers. 
3
4 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either package_version 2
9 of the License, or (at your option) any later package_version.
10
11 This program 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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
19 USA.
20
21 Dr. David Kirkby, e-mail drkirkby at ntlworld.com 
22
23 */
24 #include "config.h"
25
26
27 #ifdef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
30
31 #include "definitions.h"
32 #include "exit_codes.h"
33 #include "Erdata.h"
34
35 /* We need to set up the permittivity and cell_type arrays. A
36 complication arrises if a diaectric is found, that is not vacuum. 
37 If it is the only dielectric found, we calaculate C by saying C=C*Er. If
38 there are more than one dielectric, we need to calculate it twice. */
39
40 extern struct pixels Er_in_bitmap[MAX_DIFFERENT_PERMITTIVITIES];
41 extern struct pixels Er_on_command_line[MAX_DIFFERENT_PERMITTIVITIES];
42 extern int width, height;
43
44 extern double **Vij, **Er;
45 extern unsigned char *image_data;
46 extern unsigned char **cell_type;
47 extern int non_vacuum_found;
48 extern int coupler;
49 extern char *inputfile_name;
50
51 void setup_arrays(struct transmission_line_properties *data)
52 {
53    struct pixels pixels_found;
54    int conductor_found;
55    int conductors = 0;
56    int w,h, offset=-3, colour_mixture, i, z;
57    unsigned char red, green, blue;
58    int dielectric_found;
59    int new_colour_in_image;
60    data->dielectrics_in_bitmap=0;
61    pixels_found.red=0;
62    pixels_found.green=0;
63    pixels_found.blue=0;
64    pixels_found.white=0; 
65    pixels_found.other_colour=0; 
66
67    for(h=0;h<height;h++)
68    {
69       for (w=0; w<width;++w)
70       {
71          cell_type[w][height-1-h]=0;
72          dielectric_found=FALSE;
73          conductor_found=FALSE;
74          offset+=3;
75          if((w==0) && (offset%4!=0) && (h!=0)) 
76             offset++; 
77          if((w==0) && (offset%4!=0) && (h!=0)) 
78             offset++; 
79          if((w==0) && (offset%4!=0) && (h!=0)) 
80             offset++; 
81          blue=image_data[offset];
82          green=image_data[offset+1];
83          red=image_data[offset+2];
84          colour_mixture=256*256*red+256*green+blue;
85          if( colour_mixture == 0xff0000 ) /* +1V red */
86          {
87             cell_type[w][height-1-h]=CONDUCTOR_PLUS_ONE_V;
88             Vij[w][height-1-h]=1.0;
89             Er[w][height-1-h]=METAL_ER;
90             conductor_found=TRUE;
91             pixels_found.red++;
92          }
93          else if( colour_mixture == 0x00ff00 ) /* 0v green */
94          {
95             cell_type[w][height-1-h]=CONDUCTOR_ZERO_V;
96             Vij[w][height-1-h]=0.0;
97             conductor_found=TRUE;
98             pixels_found.green++;
99             Er[w][height-1-h]=METAL_ER;
100          }
101          else if( colour_mixture == 0x0000ff ) /* -1V blue */
102          {
103             cell_type[w][height-1-h]=CONDUCTOR_MINUS_ONE_V;
104             Vij[w][height-1-h]=-1.0;
105             conductor_found=TRUE;
106             pixels_found.blue++;
107             Er[w][height-1-h]=METAL_ER;
108             coupler=TRUE;
109             data->couplerQ=TRUE;
110          }
111          else /* A dielectric */
112          {
113             if(colour_mixture == 0xffffff ) /* White */
114                pixels_found.white++; /* Vacuum */
115             else
116                pixels_found.other_colour++; /* Some other dielectric */
117             cell_type[w][height-1-h]=DIELECTRIC;
118             Vij[w][height-1-h]=0.0;    /* XXXXXXXXXXx */
119             for(z=0;z<NUMBER_OF_DIELECTRICS_DEFINED;++z)
120             {
121                /* Check to see if the colour is one of the 10 known
122                about, without any need to define on the command line
123                */
124                if (colour_mixture == colours[z])
125                {
126                   Er[w][height-1-h]=Ers[z];
127                   dielectric_found=TRUE;
128                   if(z != 0)
129                   {
130                      non_vacuum_found=TRUE;
131                      data->found_this_dielectric=Ers[z];
132                   }
133                }
134             }
135             for(i=0;i<data->dielectrics_on_command_line;++i)
136             {
137                if (Er_on_command_line[i].other_colour ==  colour_mixture)
138                {
139                   Er[w][height-1-h]=Er_on_command_line[i].epsilon;
140                   dielectric_found=TRUE;
141                   data->found_this_dielectric=Er_on_command_line[i].epsilon;
142                   non_vacuum_found=TRUE;
143                }
144             }
145          }
146          if((dielectric_found == FALSE) && (conductor_found==FALSE))
147          {
148             fprintf(stderr,"Error#7: The colour r=0x%x g=0x%x b=0x%x (0x%02x%02x%02x) exists at pixel %d,%d.\n",red,green,blue,red,green,blue,w,h);
149             fprintf(stderr,"However, atlc does not know how to interpret this colour. This is not a\n");
150             fprintf(stderr,"conductor (pure red, green or blue), nor is it one of the %d dielectrics that\n",NUMBER_OF_DIELECTRICS_DEFINED);
151             fprintf(stderr,"are predefined in Erdata.h, nor is a corresponding dielectric constant defined\n");
152             fprintf(stderr,"on the command line with the -d option. Sometimes this occurs when a\n");
153             fprintf(stderr,"graphics package is used to make the bitmap, but it performs some form of\n");
154             fprintf(stderr,"anti-aliasing or smooting. If this is the case, redraw the image turning such\n"); 
155             fprintf(stderr,"options off. If this is not the case then re-run atlc adding the -d option\n");
156             fprintf(stderr,"to define the relative permittivity of the dielectric\n\n");
157             fprintf(stderr,"e.g. atlc -d %02x%02x%02x=1.9 %s  \n\n",red,green,blue, inputfile_name);
158             fprintf(stderr,"if this colour has a permittivity of 1.9. If there are multiple undefined\n");
159             fprintf(stderr,"dielectrics, then there will need to be multiple copies of the -d option given.\n");
160             exit_with_msg_and_exit_code("",UNDEFINED_COLOUR_IN_BITMAP);
161          }
162          /* We need to keep a record of the number of dielectrics in the image, 
163          and determine if they are defined on the command line, or if they are
164          defined on in the header file. */ 
165          if (dielectric_found == TRUE) 
166          {
167             new_colour_in_image=TRUE;
168             for (i=0; i< data->dielectrics_in_bitmap; ++i)
169             {
170                if (Er_in_bitmap[i].other_colour == colour_mixture) /* a known colour */
171                {
172                   new_colour_in_image=FALSE;
173                   if(colour_mixture != 0xffffff)
174                   {
175                      non_vacuum_found=TRUE;
176                   }
177                }
178             } 
179             if(new_colour_in_image==TRUE)
180             {
181               (data->dielectrics_in_bitmap)++;
182               Er_in_bitmap[i].other_colour=colour_mixture;
183               Er_in_bitmap[i].red=red;
184               Er_in_bitmap[i].green=green;
185               Er_in_bitmap[i].blue=blue;
186               Er_in_bitmap[i].epsilon=Er[w][height-1-h];
187             }
188          } /* end of if dielctric found */ 
189       } /* end of for w */
190    } /*end of for h */
191    /* The following prints a lot of data that is generally not wanted
192    but is when finding statistics of performance etc. */
193    if(data->verbose_level >=3)
194    {
195      printf("Red (+1 V conductor) pixels found   =        %8d \n",pixels_found.red);
196      printf("Green (0 V conductor) pixels found  =        %8d \n",pixels_found.green);
197      printf("Blue  (-1 V conductor) pixels found =        %8d \n",pixels_found.blue);
198      printf("White (vacuum dielectric) pixels found =     %8d \n",pixels_found.white);
199      printf("Others (not vacuum dielectic) pixels found = %8d \n",pixels_found.other_colour);
200      printf("Width  =                                     %8d \n",width);
201      printf("Height =                                     %8d \n",height);
202      printf("Pixels =                                     %8d \n",width*height);
203      printf("Number of Dielectrics found =                %8d \n", data->dielectrics_in_bitmap);
204      data->non_metallic_pixels=width*height-pixels_found.red-pixels_found.green -pixels_found.blue;
205      printf("Number of non-metallic pixels =              %8d \n", data->non_metallic_pixels);
206      printf("filename =             %30s \n",inputfile_name);
207      if (pixels_found.red > 0 )
208        conductors+=1;  
209      if (pixels_found.green > 0 )
210        conductors+=1;  
211      if (pixels_found.blue > 0 )
212        conductors+=1;  
213      printf("Number of Conductors  = %d \n", conductors);
214    }
215    /* The following should not be necessary, but may be as a test */
216    /* I'd like to Miguel Berg for noticcing a servere bug, where the
217    indeces of w and h were transposed, leading to crashes on Windoze
218    XP */
219    for(h=0;h<height;h++)
220    {
221      for (w=0; w<width;++w)
222      {
223        if((Vij[w][h] > 1.0) || (Vij[w][h]<-1.0))
224        {
225          fprintf(stderr,"Sorry, something is wrong Vij[%d][%d]=%f in %s %d\n",w,h,Vij[w][h], __FILE__,__LINE__);
226          exit_with_msg_and_exit_code("Exiting ....",VOLTAGE_OUT_OF_RANGE);
227        }
228      }
229    } 
230   /* Check two conductors and not next to each other, creating a short */
231   check_for_shorts();
232 } /* end of function */