Pro makehistogram_crosstalk, inimage,intitle,inxtitle,extralabels,psfile,jpgfile

    if (n_elements(inimage) gt 1) then return

    delim = path_sep()

    ; define forground and background colors
    white = 255
    black = 0

    ptype = ['ps','jpg']
    fg = 0
    theMedian = median(inimage)
    valid_pixels=where(finite(inimage),nvalid_pixels)
    sdev = stddev(inimage)
    _min = round(float(min(inimage))/1000.0)*1000 - 500
    _max = max(inimage)
    _nbins = 50
    _binsize = (_max - _min) / (_nbins - 1)
    theHist = histogram(inimage, nbins=_nbins, min=_min, max=_max, locations=theVals)
    theVals = theVals + _binsize/2.0
    ;_binsize = 1000
    ;theHist = histogram(inimage, binsize=_binsize , min=_min, max=_max)
    ;theVals = findgen(n_elements(theHist))* _binsize + _min + _binsize/2.0

    ; find the 80th percentile value--this will be used as the max of the plot range
    ; calculate cumulative counts
    cumulative=fltarr(n_elements(theHist))
    for j=0,n_elements(theHist)-1 do begin
       cumulative(j)=float(total(theHist(0:j)))/nvalid_pixels
    endfor
    percent80 = where(cumulative ge 0.9,countpercent80)
    if (countpercent80 eq 0) then begin
       dummy=max(thevals,maxindex)
       plotmax=thevals(maxindex)
    endif else begin
       plotmax = thevals[percent80[0]]
    endelse

    ; loop through plot types
    for i = 0, 1 do Begin
       plottype = ptype[i]
       !x.minor = 10
       !y.minor = 10
       if (plottype eq 'ps') then begin
         ;;set up for landscape Postscript plotting
         set_plot, 'ps'
         !p.font=-1
         device, filename= psfile, /landscape
         fsub = psfile           ;filename to be written at bottom of plot

         ;; set line and text characteristics for publication quality
         !P.CHARSIZE=1.2
         !P.CHARTHICK=4.
         !X.THICK=8.
         !Y.THICK=8.
         !P.THICK=8.
         !P.MULTI=0
         !P.TICKLEN=0.03
         symsize=1.5
         filenamecharsize=1.1

       endif

       if (plottype eq 'jpg') then begin
         ;set up to plot to memory buffer.  This plot will later be written from memory to a JPEG file.
         set_plot, 'z'
         device, set_resolution=[8000,6000]
         device, set_font='Courier'
         fsub = jpgfile          ;filename to be written at bottom of plot

         ; set line and text characteristics for publication quality
         !P.CHARSIZE=10
         !P.CHARTHICK=15.
         !X.THICK=20.
         !Y.THICK=20.
         !P.THICK=20.
         symsize=10.
         filenamecharsize=8.

       endif

       ; draw plot
       erase
       plot,theVals,theHist,psym=10,title=intitle,xtitle=inxtitle,charsize=1.10*!P.CHARSIZE,ytitle='number of pixels', $
         color=black,background=white,position=[0.15,0.15,0.8,0.9],ystyle=8,$
         xrange=[0, plotmax],xstyle=1,xtickinterval=5e3

       ; add file name to bottom of plot
       xyouts, 1.0, 40.0, fsub, color=0, charsize=filenamecharsize, /device

       ; overplot labels
       ; Add SCA, date & time stamps, read noise and gain to plot
       label_x_posn=(!X.CRANGE(1)-!X.CRANGE(0))*0.95+!X.CRANGE(0)
       label_y_posn=(!Y.CRANGE(1)-!Y.CRANGE(0))*0.95+!Y.CRANGE(0)
       label_offset=0.04*(!Y.CRANGE(1)-!Y.CRANGE(0))
       for labelcount=0, n_elements(extralabels)-1 do begin
         xyouts,label_x_posn,label_y_posn-label_offset*labelcount,extralabels[labelcount],color=0,$
          align=1.0,charsize=!P.CHARSIZE/1.25
       endfor

       ; draw curve for cumulative plot and second y-axis
       axis,yaxis=1,yrange=[0,100],/save,ytitle='Cumulative %',charsize=1.25*!P.CHARSIZE,$
         ytickv=indgen(11)*10.,yticks=10,yminor=5,color=black


       ; overplot cumulative counts
       oplot,theVals,cumulative*100.,linestyle=0,color=black,thick=!P.THICK*0.8

       ;; write JPG image file
       if (plottype eq 'jpg') then begin
         jpgimg = tvrd()
         write_jpeg, jpgfile, congrid(jpgimg, 1600, 1200, /interp, /center), quality=100
       endif

       device, /close
    endfor

End


