py_dds.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:PyDDS 作者: dph119 项目源码 文件源码
def write_to_png(self, fname):
        """Write out the pixel data to a .png file."""

        # Figure out which data to write out.
        # If the decompressed data is valid, use that.
        if self.data_is_decompressed:
            data = self.decompressed_data
        else:
            data = self.data

        assert data, 'data must be something valid at this point.'

        # Check to see if the data contains mipmaps
        # If it does, truncate out everything beyond mip0
        # TODO: Eventually add support to specify which mip level to write out
        if self.dds_header.dwMipMapCount > 0:
            # Calculate the size of mip 0 in bytes
            # (number of pixels in mip0) * (bytes/pixel)
            mip0_size = self.dds_header.dwWidth * self.dds_header.dwHeight * 4
            data = data[:mip0_size]

        self.logger.info('Creating PNG file: %s (width, height = %d,%d)', \
                         fname, self.dds_header.dwWidth, self.dds_header.dwHeight)

        fhandle = open(fname, 'wb')
        swizzled_data = self.swizzle_decompressed_bc1_to_png(data, self.dds_header.dwWidth)

        # TODO: Check if alpha really does exist in original data. Currently assuming it always does.
        writer = png.Writer(self.dds_header.dwWidth, self.dds_header.dwHeight, alpha=True)

        # PNG expects the data to be presented in "boxed row flat pixel" format:
        # list([R,G,B,A  R,G,B,A  R,G,B,A],
        #      [R,G,B,A  R,G,B,A  R,G,B,A])
        # Each row will be width * # components elements * # bytes/component
        formatted_data = zip(*(iter(swizzled_data),) * (self.dds_header.dwWidth * 4 * 1))

        writer.write(fhandle, formatted_data)
        fhandle.close()

        self.logger.info('Done creating PNG file.')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号