Read image data from file and return as numpy array.

        Raise ValueError if format is unsupported.
        If any argument is False, the shape of the returned array might be
        different from the page shape.

        Parameters
        ----------
        squeeze : bool
            If True all length-1 dimensions (except X and Y) are
            squeezed out from result.
        colormapped : bool
            If True color mapping is applied for palette-indexed images.
        rgbonly : bool
            If True return RGB(A) image without additional extra samples.

def asarray(self, squeeze=True, colormapped=True, rgbonly=True):
        """Read image data from file and return as numpy array.

        Raise ValueError if format is unsupported.
        If any argument is False, the shape of the returned array might be
        different from the page shape.

        Parameters
        ----------
        squeeze : bool
            If True all length-1 dimensions (except X and Y) are
            squeezed out from result.
        colormapped : bool
            If True color mapping is applied for palette-indexed images.
        rgbonly : bool
            If True return RGB(A) image without additional extra samples.

        """
        fd = self.parent._fd
        if not fd:
            raise IOError("TIFF file is not open")
        if self.dtype is None:
            raise ValueError("data type not supported: %s%i" % (
                self.sample_format, self.bits_per_sample))
        if self.compression not in TIFF_DECOMPESSORS:
            raise ValueError("cannot decompress %s" % self.compression)
        if ('ycbcr_subsampling' in self.tags and
            self.tags['ycbcr_subsampling'].value not in (1, (1, 1))):
            raise ValueError("YCbCr subsampling not supported")
        tag = self.tags['sample_format']
        if tag.count != 1 and any((i-tag.value[0] for i in tag.value)):
            raise ValueError("sample formats don't match %s" % str(tag.value))

        dtype = self._dtype
        shape = self._shape
        image_width = self.image_width
        image_length = self.image_length
        typecode = self.parent.byte_order + dtype
        bits_per_sample = self.bits_per_sample

        if self.is_tiled:
            if 'tile_offsets' in self.tags:
                byte_counts = self.tile_byte_counts
                offsets = self.tile_offsets
            else:
                byte_counts = self.strip_byte_counts
                offsets = self.strip_offsets
            tile_width = self.tile_width
            tile_length = self.tile_length
            tw = (image_width + tile_width - 1) // tile_width
            tl = (image_length + tile_length - 1) // tile_length
            shape = shape[:-3] + (tl*tile_length, tw*tile_width, shape[-1])
            tile_shape = (tile_length, tile_width, shape[-1])
            runlen = tile_width
        else:
            byte_counts = self.strip_byte_counts
            offsets = self.strip_offsets
            runlen = image_width

        try:
            offsets[0]
        except TypeError:
            offsets = (offsets, )
            byte_counts = (byte_counts, )
        if any(o < 2 for o in offsets):
            raise ValueError("corrupted file")

        if (not self.is_tiled and (self.is_stk or (not self.compression
            and bits_per_sample in (8, 16, 32, 64)
            and all(offsets[i] == offsets[i+1] - byte_counts[i]
                    for i in range(len(offsets)-1))))):
            # contiguous data
            fd.seek(offsets[0])
            result = numpy.fromfile(fd, typecode, numpy.prod(shape))
            result = result.astype('=' + dtype)
        else:
            if self.planar_configuration == 'contig':
                runlen *= self.samples_per_pixel
            if bits_per_sample in (8, 16, 32, 64, 128):
                if (bits_per_sample * runlen) % 8:
                    raise ValueError("data and sample size mismatch")
                unpack = lambda x: numpy.fromstring(x, typecode)
            elif isinstance(bits_per_sample, tuple):
                unpack = lambda x: unpackrgb(x, typecode, bits_per_sample)
            else:
                unpack = lambda x: unpackints(x, typecode, bits_per_sample,
                                              runlen)
            decompress = TIFF_DECOMPESSORS[self.compression]
            if self.is_tiled:
                result = numpy.empty(shape, dtype)
                tw, tl, pl = 0, 0, 0
                for offset, bytecount in zip(offsets, byte_counts):
                    fd.seek(offset)
                    tile = unpack(decompress(fd.read(bytecount)))
                    tile.shape = tile_shape
                    result[0, pl, tl:tl+tile_length,
                           tw:tw+tile_width, :] = tile
                    del tile
                    tw += tile_width
                    if tw >= shape[-2]:
                        tw, tl = 0, tl + tile_length
                        if tl >= shape[-3]:
                            tl, pl = 0, pl + 1
                result = result[..., :image_length, :image_width, :]
            else:
                result = numpy.empty(shape, dtype).reshape(-1)
                index = 0
                for offset, bytecount in zip(offsets, byte_counts):
                    fd.seek(offset)
                    stripe = unpack(decompress(fd.read(bytecount)))
                    size = min(result.size, stripe.size)
                    result[index:index+size] = stripe[:size]
                    del stripe
                    index += size

        result.shape = self._shape

        if self.predictor == 'horizontal':
            # workaround bug in LSM510 software
            if not (self.parent.is_lsm and not self.compression):
                numpy.cumsum(result, axis=3, dtype=dtype, out=result)

        if colormapped and self.is_palette:
            if self.color_map.shape[1] >= 2**bits_per_sample:
                # FluoView and LSM might fail here
                result = numpy.take(self.color_map, result, axis=1)
        elif rgbonly and self.is_rgb and 'extra_samples' in self.tags:
            # return only RGB and first alpha channel if exists
            extra_samples = self.extra_samples
            if self.tags['extra_samples'].count == 1:
                extra_samples = (extra_samples, )
            for i, exs in enumerate(extra_samples):
                if exs in ('unassalpha', 'assocalpha'):
                    if self.planar_configuration == 'contig':
                        result = result[..., [0, 1, 2, 3+i]]
                    else:
                        result = result[:, [0, 1, 2, 3+i]]
                    break
            else:
                if self.planar_configuration == 'contig':
                    result = result[..., :3]
                else:
                    result = result[:, :3]

        if squeeze:
            try:
                result.shape = self.shape
            except ValueError:
                pass

        return result