Take each row in rows (an iterator) and yield a fresh row with the pixels scaled according to the rescale parameters in the list `rescale`. Each element of `rescale` is a tuple of (source_bitdepth, target_bitdepth), with one element per channel.
(rows, rescale)
| 931 | |
| 932 | |
| 933 | def rescale_rows(rows, rescale): |
| 934 | """ |
| 935 | Take each row in rows (an iterator) and yield |
| 936 | a fresh row with the pixels scaled according to |
| 937 | the rescale parameters in the list `rescale`. |
| 938 | Each element of `rescale` is a tuple of |
| 939 | (source_bitdepth, target_bitdepth), |
| 940 | with one element per channel. |
| 941 | """ |
| 942 | |
| 943 | # One factor for each channel |
| 944 | fs = [float(2 ** s[1] - 1) / float(2 ** s[0] - 1) for s in rescale] |
| 945 | |
| 946 | # Assume all target_bitdepths are the same |
| 947 | target_bitdepths = set(s[1] for s in rescale) |
| 948 | assert len(target_bitdepths) == 1 |
| 949 | (target_bitdepth,) = target_bitdepths |
| 950 | typecode = "BH"[target_bitdepth > 8] |
| 951 | |
| 952 | # Number of channels |
| 953 | n_chans = len(rescale) |
| 954 | |
| 955 | for row in rows: |
| 956 | rescaled_row = array(typecode, iter(row)) |
| 957 | for i in range(n_chans): |
| 958 | channel = array(typecode, (int(round(fs[i] * x)) for x in row[i::n_chans])) |
| 959 | rescaled_row[i::n_chans] = channel |
| 960 | yield rescaled_row |
| 961 | |
| 962 | |
| 963 | def pack_rows(rows, bitdepth): |