code: def rescale_rows(rows, rescale):
      
  
      # One factor for each channel
      fs = [float(2 ** s[1] - 1)/float(2 ** s[0] - 1)
            for s in rescale]
  
      # Assume all target_bitdepths are the same
      target_bitdepths = set(s[1] for s in rescale)
      assert len(target_bitdepths) == 1
      (target_bitdepth, ) = target_bitdepths
      typecode = 'BH'[target_bitdepth > 8]
  
      # Number of channels
      n_chans = len(rescale)
  
      for row in rows:
          rescaled_row = array(typecode, iter(row))
          for i in range(n_chans):
              channel = array(
                  typecode,
                  (int(round(fs[i] * x)) for x in row[i::n_chans]))
              rescaled_row[i::n_chans] = channel
          yield rescaled_row
docs: 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.