File: Mask.comp

package info (click to toggle)
mccode 3.5.19%2Bds5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,113,256 kB
  • sloc: ansic: 40,697; python: 25,137; yacc: 8,438; sh: 5,405; javascript: 4,596; lex: 1,632; cpp: 742; perl: 296; lisp: 273; makefile: 226; fortran: 132
file content (271 lines) | stat: -rw-r--r-- 8,946 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: Mask
*
* %I
*
* Written by: Erik Knudsen 
* Date: March 2014
* Version: 1.0
* Origin: DTU Physics
*
* A masking image object
*
* %D
* The Mask component takes an image as input either as an standard image file in png or pnm format, or as an ascii file (see below for format),
* and uses the image as a mask. For instance as a manner of measuring resolution for imaging applications.
* If the image is supplied as a png or pnm file, they interpretation of the pixels varies depending on the file. If the image is grayscale the pixel values are directly mapped
* to opacity (or transparency if invert is set) values in the range [0..1]. If the image has RGB channels the R channel is considered most significant, the B channel least significant.
* The resulting number, e.g. R*255^2 + G*255 + B, is then mapped to a real valued opacity.
* Additionally png images may have an alpha channel - which is then considered the least significant channel. Palette mapped pngs are as of yet _not_ supported.
* A regular ascii file may be supplied - in which case the file is like the one below
* #any initial line starting with a hash is silently ignored
* 0.0 1.0 0.0 1.0 0.0
* 0.5 0.0 0.5 0.0 0.5  
* 0.0 0.25 0.0 0.25 0.0
* 0.75 0.0 0.75 0.0 0.75
* 1.0 0.0 1.0 0.0 1.0
* 
* ...which defines a 5x5 mask with a kind of checkerboard pattern-  
* 
* N.b. If you want to use the png-option of the component you must have libpng installed _and_ link your compiled instrument to it. Assuming libpng is installed you may do this
* by adding "-DUSE_PNG=1 -lpng" to 1) the MCXTRACE_CFLAGS environment variable or 2) to the compiler flags textbox in the GUI. Open File->Configuration and edit the textbox. 
*
* The virtual option of the Mask, is intended as a help to use a png-image as a grayscale distribution. If the virtual flag is set, rays are porpagated to the mask plane
* and the pixel value at the intersection point is read, but the rays reamin unaffected. By extracting the output parameter masking it can subsequently be used. 
* 
* %P
* Input parameters:
* xwidth: [m]   Width of the masking object
* yheight:[m]   Height of the masking object
* mask:   [ ]   Name of file containing the masking image
* invert: [ ]   By default the values from the masking image are interepreted as opacity (1 is fully blocking). If invert is nonzero this is inverted and the values are considered as transparency (1 is fully transmissive)
* virtual:[ ]   Mask does not affect the neutron, but does read the pixel value of the pixel hit. Could be useful to have as a packing factor for a crystal .
* CALCULATED PARAMETERS:
* masking: [ ]  Opacity/transmission value. If virtual==0, each ray is downweighted according to the masking value.
* %E
*******************************************************************************/

DEFINE COMPONENT Mask

SETTING PARAMETERS (xwidth, yheight, string mask, invert=0, virtual=0)

//DEPENDENCY "-lpng -DUSE_PNG"

SHARE
%{
  %include "read_table-lib"
#if USE_PNG
#include <png.h>
#endif 
  
%}

DECLARE
%{
  t_Table table;
  int ny;
  int nx;
  double xmin;
  double ymin;
  double xmax;
  double ymax;
  int filter_row;
  double masking;
%}


INITIALIZE
%{
  
  /*do some checks on input parameters*/
  if (invert){
    /*invert should be either 0 or one*/
    invert=1;
  }

  do{
#if USE_PNG
    if (strstr(mask+strlen(mask)-4,".png")!=NULL){
      /*filename ends with .png - use libpng to read the mask*/
      /*read file and insert mask values into table*/
      /*copied form libpng docs*/
      int is_png=0;
      const int number=1;
      char header[8];
      const int ERROR=1;
      const int NOT_PNG=2;

      FILE *fp = fopen(mask, "rb");
      if (!fp){
        fprintf(stderr,"Error(%s): cant open file %s for reading\n", NAME_CURRENT_COMP,mask); 
        exit(1);
      }
      fread(header, 1, number, fp);
      is_png = !png_sig_cmp(header, 0, number);

      if (!is_png){
        fprintf(stderr,"Error(%s): %s does not appear to be a png file\n", NAME_CURRENT_COMP,mask); 
        exit(1);
      }

      //png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,user_error_fn, user_warning_fn);
      png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
      /*last two arguments are error handling functions*/

      if (!png_ptr){
        fprintf(stderr,"Error(%s): Could not set up the png reading buffer structure\n", NAME_CURRENT_COMP); 
        exit(1);
      }

      png_init_io(png_ptr,fp);
      /*we used number bytes of the png file to indentify it as being png*/
      png_set_sig_bytes(png_ptr, number);

      png_infop info_ptr = png_create_info_struct(png_ptr);
      if (!info_ptr){
        png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
        exit(1);
      }

      /*do read the image using the high level interface and apply the transform 
       * toexpand 1, 2, and 4 bits to bytes*/
      png_read_png(png_ptr,info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_EXPAND, NULL);
      /*image is now available in png_ptr - extfact dimensions*/
      int height=png_get_image_height(png_ptr,info_ptr);
      int width=png_get_image_width(png_ptr,info_ptr);
      int bd=png_get_bit_depth(png_ptr,info_ptr);
      if(bd<8){
        bd=8;
      }
      unsigned int ct=png_get_color_type(png_ptr,info_ptr);

      Table_Init(&(table),height,width);

      png_bytep *row_pointers=png_get_rows(png_ptr,info_ptr);
      printf("I just read a png image with size %d by %d, bit depth %d, and color type %d\n",width, height, bd, ct);
      fclose(fp);

      int j,r,c,tuplelen;
      unsigned long tuple_normalizer;
      switch (ct){
        case PNG_COLOR_TYPE_GRAY:
          if(bd==8){
            tuplelen=1;
            tuple_normalizer=0xFF;
          }else if(bd==16){
            tuplelen=2;
            tuple_normalizer=0xFFFF;
          }
          break;
        case PNG_COLOR_TYPE_GRAY_ALPHA:
          if(bd==8){
            tuplelen=2;
            tuple_normalizer=0xFFFF;
          }else if(bd==16){
            tuplelen=2;
            tuple_normalizer=0xFFFFFFFF;
          }
          break;
        case PNG_COLOR_TYPE_RGB:
          if(bd==8){
            tuplelen=3;
            tuple_normalizer=0xFFFFFF;
          }else if(bd==16){
            tuplelen=3;
            tuple_normalizer=0xFFFFFFFFFFFF;
          }
          break;
        case PNG_COLOR_TYPE_RGB_ALPHA:
          if(bd==8){
            tuplelen=4;
            tuple_normalizer=0xFFFFFFFF;
          }else if (bd==16){
            tuplelen=4;
            tuple_normalizer=0xFFFFFFFFFFFFFFFF;
          }
          break;
        default:
          fprintf(stderr,"Error (%s): Unsupported type of png image (allowed are GRAY, GRAY_ALPHA, RGB, RGB_ALPHA)\n","NAME_CURRENT_COMP");
          exit(1);
      }
      for (r=0;r<height;r++){
        for (c=0;c<width;c++){
          unsigned long pixel=0;
          for (j=0;j<tuplelen;j++){
            pixel |= (row_pointers[r][c*tuplelen+j]<<8*j);
          }
          /*Data table should contain opacity values. Possibly invert the image*/
          /*Also png images are stored top-to-bottom, i.e. the first stored row corresponds to y=-yheight/2. Hence, switch the order.*/
          table.data[(height-r-1)*width+c]=((double)pixel)/tuple_normalizer;
        }
      }
      break;
    }
#endif
#if USEPBM
    if ( 
        (strstr(mask+strlen(mask)-4,".pnm")!=NULL) ||
        (strstr(mask+strlen(mask)-4,".pam")!=NULL) ||
        (strstr(mask+strlen(mask)-4,".pbm")!=NULL)
       ){
      /*filename ends with .pnm, .pam, or .pbm - its a pnm file*/
      fprintf(stderr,"Many apologies(%s): pnm formats are not supported yet\n");
      exit(1); 
    }
#endif
  /*read the masking file*/ 
    int status;
    if ( (status=Table_Read(&(table),mask,0))==-1){
      fprintf(stderr,"Error: Could not parse file \"%s\" in COMP %s\n",mask,NAME_CURRENT_COMP);
      exit(-1);
    }
  }while(0);/*this is to allow breaking out so .png and .pnm will take precedence*/

  /*set some image vals*/
  xmax= xwidth/2.0;
  xmin=-xwidth/2.0;
  ymax= yheight/2.0;
  ymin=-yheight/2.0;
  nx=table.columns;
  ny=table.rows;

%}

TRACE
%{
  int i,j;


  PROP_Z0;
  if (x>xmin && x<xmax && y>ymin && y<ymax){
    i = floor((x - xmin)*nx/(xwidth));
    j = floor((y - ymin)*ny/(yheight));
    if (invert){  
      masking=Table_Index(table,j,i);
    }else{
      masking=1.0-Table_Index(table,j,i);
    }
    if(!virtual){
        p*=masking;
    }
    SCATTER;
  }



%}

MCDISPLAY
%{
  /* A bit ugly; hard-coded dimensions. */
  
  rectangle("xy",0,0,0,xwidth,yheight);
%}

END