'Updates the width and height fields of the jpeg image.
Raises:
BadImageError if the image string is not a valid jpeg image.'

def _update_jpeg_dimensions(self):

    size = len(self._image_data)
    offset = 2
    while (offset < size):
        while ((offset < size) and (ord(self._image_data[offset]) != 255)):
            offset += 1
        while ((offset < size) and (ord(self._image_data[offset]) == 255)):
            offset += 1
        if ((offset < size) and ((ord(self._image_data[offset]) & 240) == 192) and (ord(self._image_data[offset]) != 196)):
            offset += 4
            if ((offset + 4) <= size):
                (self._height, self._width) = struct.unpack('>HH', self._image_data[offset:(offset + 4)])
                break
            else:
                raise BadImageError('Corrupt    JPEG    format')
        elif ((offset + 3) <= size):
            offset += 1
            offset += struct.unpack('>H', self._image_data[offset:(offset + 2)])[0]
        else:
            raise BadImageError('Corrupt    JPEG    format')
    if ((self._height is None) or (self._width is None)):
        raise BadImageError('Corrupt    JPEG    format')
