'Resize images to fit in new view size and adjust all line number references accordingly'

def resized(self):

    for v in (self.left, self.right):
        changes = []
        for (i, (top, bot, kind)) in enumerate(v.changes):
            if (top in v.images):
                (img, oldw, oldlines) = v.images[top]
                (lines, w) = self.get_lines_for_image(img, v)
                if (lines != oldlines):
                    changes.append((i, lines, (lines - oldlines), img, w))
        for (i, lines, delta, img, w) in changes:
            (top, bot, kind) = v.changes[i]
            c = QTextCursor(v.document().findBlockByNumber((top + 1)))
            c.beginEditBlock()
            c.movePosition(c.StartOfBlock)
            if (delta > 0):
                for _ in xrange(delta):
                    c.insertBlock()
            else:
                c.movePosition(c.NextBlock, c.KeepAnchor, (- delta))
                c.removeSelectedText()
            c.endEditBlock()
            v.images[top] = (img, w, lines)
            def mapnum(x):
                return (x if (x <= top) else (x + delta))
            lnm = LineNumberMap()
            lnm.max_width = v.line_number_map.max_width
            for (x, val) in v.line_number_map.iteritems():
                dict.__setitem__(lnm, mapnum(x), val)
            v.line_number_map = lnm
            v.changes = [(mapnum(t), mapnum(b), k) for (t, b, k) in v.changes]
            v.headers = [(mapnum(x), name) for (x, name) in v.headers]
            v.images = OrderedDict(((mapnum(x), v) for (x, v) in v.images.iteritems()))
        v.viewport().update()
