a :jg@@sdZddlmZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z mZGd d d eZGd d d eZGd ddeZdddddZd:ddddddZdddddZdddddZGd d!d!eZddd"d#d$ZGd%d&d&ejZGd'd(d(ejZGd)d*d*eZGd+d,d,eZGd-d.d.ejZd/d0d1d2d3d4d5Z e !ej"eee #ej"d6e $d7ee $d8ee %ej"e e &d9edS);a Blizzard Mipmap Format (.blp) Jerome Leclanche The contents of this file are hereby released in the public domain (CC0) Full text of the CC0 license: https://creativecommons.org/publicdomain/zero/1.0/ BLP1 files, used mostly in Warcraft III, are not fully supported. All types of BLP2 files used in World of Warcraft are supported. The BLP file structure consists of a header, up to 16 mipmaps of the texture Texture sizes must be powers of two, though the two dimensions do not have to be equal; 512x256 is valid, but 512x200 is not. The first mipmap (mipmap #0) is the full size image; each subsequent mipmap halves both dimensions. The final mipmap should be 1x1. BLP files come in many different flavours: * JPEG-compressed (type == 0) - only supported for BLP1. * RAW images (type == 1, encoding == 1). Each mipmap is stored as an array of 8-bit values, one per pixel, left to right, top to bottom. Each value is an index to the palette. * DXT-compressed (type == 1, encoding == 2): - DXT1 compression is used if alpha_encoding == 0. - An additional alpha bit is used if alpha_depth == 1. - DXT3 compression is used if alpha_encoding == 1. - DXT5 compression is used if alpha_encoding == 7. ) annotationsN)IntEnum)BytesIO)IO)Image ImageFilec@seZdZdZdS)FormatrN)__name__ __module__ __qualname__JPEGrr|d?d@d>|d@d>fS)N r?rr)rrrr unpack_565<sr!Fbytesboolz1tuple[bytearray, bytearray, bytearray, bytearray])dataalpharc Cst|d}ttttf}t|D]}|d}td||\}}}t|\} } } t|\} } }tdD]L}tdD]<}|d@}|d?}d}|dkr| | | }}}n|dkr| | |}}}n|dkr6||krd| | d}d| | d}d| |d}n$| | d}| | d}| |d}nR|dkr||kr|d| | d}d| | d}d|| d}n d \}}}}|r||||||gq||||||gq|qnq(|S) E input: one "row" of data (i.e. will produce 4*width pixels) z| ||}}}nv|d krzd | | d }d | |d }d | |d }n:|d krd | | d }d || d }d || d }||||||gqqq(|S)r&z<8Bhighralphacode_indexr@ color_coderArBrCrrr decode_dxt3ysH       rPc Cst|d}ttttf}t|D]}|d}|||d}td|\}}td|d}|d|dd>B|dd>B|dd >B} |d |d d>B} td |d\} } td |d\} t| \}}}t| \}}}tdD]}tdD]}dd||}|dkr&| |?d@}n0|dkrF| d?| d >d@B}n| |d?d@}|d krf|}nt|d krv|}nd||krd|||d |d}n<|dkrd }n,|dkrd}nd|||d |d}| dd||?d@}|d kr|||}}}n|d kr*|||}}}nv|dkrfd||d}d||d}d||d}n:|dkrd||d}d||d}d||d}||||||gqqq(|S)zG input: one "row" of data (i.e. will produce 4 * width pixels) rEzrrNZ alphacoder@rOrArBrCrrr decode_dxt5s^ ,           rSc@s eZdZdS)BLPFormatErrorN)r r r rrrrrTsrT)prefixrcCs|dddvS)Nr(BLP1BLP2r)rUrrr_acceptsrYc@s&eZdZdZdZdZddddZdS) BlpImageFilez Blizzard Mipmap Format BLPzBlizzard Mipmap FormatNonercCs|jd|_|jdtjtd|jd\|_|jdtjtd|jd|_ |jdvrr|j }nd t |j}t ||jrd nd |_ t|d |jd |jd dfg|_dS)Nr(rs<    95FB'0!