My code has gotten too complicated by mixing the old set_shot_histo and get_histo_range together with my new stuff. It also will be too hard to explain. So here's my new proposal.
1. Leave the old 2 routines the way they were. They will behave almost exactly like they have in the past (except fixing a few bugs).
But I don't think a histogram of the previous shot is useful. A histogram before taking a shot is very useful for framing the shot and setting exposure, but it doesn't tell you much after the shot, except giving an idea of over and underexposure in high contrast images. I think I can do that better in a new routine though.
2. I'll add two new routines:
To enable shot metering, you call:
npix=set_shot_meter(set_options,xp1,yp1,xp2,yp2,lowp,highp)
xp1,yp1,xp2,yp2 is the area of the image that will be used for metering. Values are in percent of the active area.
lowp,highp are the pixel levels in percentage for the "mini histogram" result (see below).
options: 0 disable, 1 enable, 2 set reference shot? future expansion
To disable shot metering, you call the set function without arguments or with option==0:
set_shot_meter(0)
To retrieve the data after a shot:
ev96rel, nlow, nhigh = get_shot_meter(get_options)
ev96rel is the change in ev from the reference shot, which can be added to the current exposure values so the next shot matches the brightness of the reference shot.
nlow is the number of pixel samples that are below the lowp value set above. This would be like:
nlow=get_histo_range(0,lowp), only the value would be more accurate
nhigh is the number of pixel samples that are above the highp value set above. It would be like:
nhigh=get_histo_range(highp,1023)
The number of pixels between nhigh and nlow would be: npix-nhigh-nlow. So you have a "mini histogram" with 3 ranges that is based on the actual number of pixels, and uses the full value of the pixel without bit shifting.
One of the "set_options" might be to use the actual, 12 or 14 bit pixel level as the cutoff, for greater accuracy. If you specify the high range as the white level, that could tell you how many totally blown pixels there are, or maybe how many hot pixels there are in a night image.
get_options could be used to return different data, like the actual log2(sum) instead of a relative value. That way you could save it, or compare it to an absolute value taken from a previous shot with the same area, without having a recent reference shot. You supply your own value from a reference shot taken in the past.
a possible set_option could be to use the entire image for the mini-histogram, and the area from set_shot_meter for the ev96rel value. That way, if something happened outside your metering area, like the sun setting or moon rising, you could detect it and adjust exposure if needed.
So I've got some more re-working of the code to do before I can put anything out. I welcome questions, comments or suggestions.