compile_opt idl2, logical_predicate
im = read_image(filepath('elev_t.jpg', subdir=['examples','data']), /order)
im = total(float(im),1)
if min(im) eq 0 then im++
help, im
; simulate random missing data, half of the pixels are missing
im[floor(randomu(1, n_elements(im)/2)*n_elements(im))] = 0
dim = size(im, /dimension)
w = where(im ne 0, n)
ord = sort(im[w])
p0 = im[w[ord[n/50]]]
p1 = im[w[ord[-n/50]]]
print, p0, p1
tlb = widget_base(title='Shader example', /row)
w1 = widget_draw(tlb, xsize=dim[0], ysize=dim[1], retain=2)
w2 = widget_draw(tlb, xsize=dim[0], ysize=dim[1], $
graphics_level=2, retain=0, renderer=0)
widget_control, tlb, /realize
widget_control, w1, get_value=winid
wset, winid
tv, bytscl(im, min=p0, max=p1)
widget_control, w2, get_value=win
vertexProgram = $
[ 'void main (void) {', $
' gl_TexCoord[0] = gl_MultiTexCoord0;', $
' gl_Position = ftransform();', $
' gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;', $
'}' ]
fragProgram = $
['uniform sampler2D _IDL_ImageTexture;' $
,'uniform float min;' $
,'uniform float max;' $
,'uniform float kernel[25];' $
,'uniform vec2 _IDL_ImageStep;' $
,'void main(void)' $
,'{' $
,'vec2 adj = vec2(2*_IDL_ImageStep.x,2*_IDL_ImageStep.y);' $
,'vec2 tc = gl_TexCoord[0].st - adj;' $
,'float sum = 0.0;' $
,'float nsum = 0.0;' $
,'for (int i=0; i<4; i++) {' $
, 'adj.y = i * _IDL_ImageStep.y;' $
,' for (int j=0; j<4; j++) {' $
,' adj.x = j * _IDL_ImageStep.x;' $
,' float p = texture2D(_IDL_ImageTexture, tc + adj).r;' $
,' float k = kernel[j+i*5];' $
,' sum += p*k;' $
,' nsum += (p != 0)*k;' $
,' }' $
,'}' $
,'nsum += (nsum == 0);' $
,'sum = (sum/nsum - min) / (max - min);' $
,'gl_FragColor = vec4(sum,sum,sum,1.0);' $
,'}' $
]
g = gaussian_function([0.5,0.5],width=5,maximum=1)
shader = IDLgrShader()
shader->SetProperty, $
fragment_program_string=strjoin(fragProgram, string(10b)), $
vertex_program_string=strjoin(vertexProgram, string(10b))
shader->SetUniformVariable, 'min', float(p0)
shader->SetUniformVariable, 'max', float(p1)
shader->SetUniformVariable, 'kernel', float(g)
view = IDLgrView(viewplane_rect=[0,0,dim])
model = IDLgrModel()
img = IDLgrImage(float(im), internal_data_type=3, shader=shader)
view->Add, model
model->Add, img
win->Draw, view
xmanager, 'shader_tricks', tlb, /no_block
end