Having taken an image I call get_histo_range(i,j), having called shot_histo_enable(1).
To find the black level offset I use this bit of code
shot_histo_enable(1)
jj = 0
myshoot()
repeat
test2 = get_histo_range(jj,jj)
jj = jj + 1
sleep(200)
until test2 > 0
shot_histo_enable(0)
print("Black Level = "..jj-1)
In other words, histogram data only kicks in from the offset, which on the G1X is 29 and for my other cams it’s 127
OK. First I forgot to mention that cameras can also override blacklevel, which in fact M3, G5 X and G7 X II do:
#define CAM_BLACK_LEVEL 2047 // not tested under all conditions
So, that explains why these cameras are 127 in get_histo_range. Your G1 X OTOH is showing the normal range, since you aren't using the settings that trigger alternate blacklevel.
Regarding your code, I would note that it's possible to have some outlier pixels that are below the nominal black level. I'd guess there are generally few enough they'd be rounded to zero in get_histo_range, but I can't guarantee it.
To get CHDKs idea of the blacklevel directly, you can use rawop.get_black_level, but note
1) The value is the raw value, not scaled to 10 bit like get_histo_range. You can use rawop.get_bits_per_pixel to get the sensor BPP if you want to scale to 10bit.
2) For cameras with variable black level like G1 X, it may be updated in the raw hook. Getting the value after shooting should be fine.