3 }\P8@sdZddlZddlmZddlmZmZdZdZdZ dZ dZ dZ dZ d d Zd!d d ZddZddZGdddeZGdddejZGdddejZGdddeZGdddeZejejeddejejdejdeejd edS)"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. N)BytesIO)Image ImageFilecCs*|d?d@d>|d?d@d>|d@d>fS)N r?r)ir r j/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-i584jbuk/Pillow/PIL/BlpImagePlugin.py unpack_5651srFcCst|d}ttttf}xt|D]}|d}tjd||\}}}t|\} } } t|\} } }x`tdD]R}xJtdD]<}|d@}|d?}d}|dkr| | | }}}n|dkr| | |}}}n|dkrB||krd| | d}d| | d}d| |d}n$| | d}| | d}| |d}nR|dkr||krd| | d}d| | d}d|| d}n d \}}}}|r||j||||gq||j|||gqWqvWq,W|S) zE input: one "row" of data (i.e. will produce 4*width pixels) zB|dd>B|dd >B}|d |d d>B} tjd |d\} } tjd |d\} t| \} }}t| \}}}xtdD]}xtdD]}dd||}|dkr2| |?d@}n0|dkrR| d?|d >d@B}n||d?d@}|d krr|}nt|d kr|}nd||krd|||d |d}n<|dkrd }n,|dkrd}nd|||d |d}| dd||?d@}|d kr| ||}}}n|d kr6|||}}}nv|dkrrd| |d}d||d}d||d}n:|dkrd|| d}d||d}d||d}||j||||gqWqWq,W|S)zG input: one "row" of data (i.e. will produce 4 * width pixels) r0zr?__doc__formatformat_descriptionrMrFr r r rr@s r@c@s(eZdZdZddZddZddZdS) _BLPBaseDecoderTc CsRy.|jjd|jjd|_|j|jWntjk rLtdYnXdS)NrrzTruncated Blp file)rr) fdseekrDrErF_loadrerrorIOError)rKbufferr r rdecodes  z_BLPBaseDecoder.decodecCsfg}x\tdD]P}y tjd|jjd\}}}}Wntjk rJPYnX|j||||fqW|S)Nz<4Br)rrrOr\rDr_append)rKrrr.r-r,r+r r r _read_palette)s z_BLPBaseDecoder._read_palettecCstjd|jjd\|_tjd|jjd\|_tjd|jjd\|_tjd|jjd\|_tjd|jjd\|_tjd|jjd|_ |j dkrtjd|jjd\|_tjd|jjd\|_ tjd|jjd |_ tjd|jjd |_ dS) Nzr?Z _pulls_fdrbrerFr r r rr[s  r[c@seZdZddZddZdS) BLP1Decoderc Cs|jtkr|jn|jdkr|jdkrt}|j}t|jj|j d}xVyt j d|jd\}Wnt j k r~PYnX||\}}}}|j |||gqNW|jt|qtd|jntd|jdS) Nrrr rzr?r^rhr r r rrfFsrfc@seZdZddZdS) BLP2Decoderc Cs:|j}t}|jj|jd|jdkr|jtkrt|jj |j d}xVyt j d|j d\}Wnt j k r~PYnX||\}}}}|j|||fqNWq(|jtkr |jtkr&|jdddd} xLt|jdddD]2} x,t|jj | t|jdD]} || 7}qWqWn|jtkr|jdddd} xt|jdddD]*} x"t|jj | D]} || 7}qvWq`Wnx|jtkr|jdddd} xTt|jdddD]*} x"t|jj | D]} || 7}qWqWntd |jntd |jntd |j|jt|dS) Nrrzr?r^r r r rrqrsrqcCs|dddkS)NrBLP1BLP2)ryrzr )pr r rsr|z.blprArB)F)rXriorrrrgrrrsZ"BLP_ENCODING_UNCOMPRESSED_RAW_BGRArtrvrwrr/r8r;NotImplementedErrorr<r@Z PyDecoderr[rfrqZ register_openrYZregister_extensionZregister_decoderr r r rs0  75H+*,;