zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
U
T±dc@ã@sndZddlZddlZddlmZddlmZddlmZm    Z    ddl
m Z Gdd    „d    eƒZ Gd
d „d eƒZ Gd d „d eƒZdd„Zdd„Zd-dd„Zdd„Zdd„ZGdd„deƒZdd„ZGdd„de    j    ƒZGdd „d e    jƒZGd!d"„d"eƒZGd#d$„d$eƒZGd%d&„d&e    jƒZd.d'd(„Ze ej ee¡e !ej d)¡e "d*e¡e "d+e¡e #ej e¡e $d,e¡dS)/aÑ
Blizzard Mipmap Format (.blp)
Jerome Leclanche <jerome@leclan.ch>
 
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)ÚIntEnum)ÚBytesIOé)ÚImageÚ    ImageFile)Ú    deprecatec@seZdZdZdS)ÚFormatrN)Ú__name__Ú
__module__Ú __qualname__ÚJPEG©r r úId:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\PIL/BlpImagePlugin.pyr)src@seZdZdZdZdZdS)ÚEncodingrééN)r    r
r Ú UNCOMPRESSEDÚDXTZUNCOMPRESSED_RAW_BGRAr r r rr-src@seZdZdZdZdZdS)Ú AlphaEncodingrréN)r    r
r ÚDXT1ÚDXT3ÚDXT5r r r rr3srcCsŒtdtdtdi ¡D]X\}}| |¡r|t|ƒd…}||jkrt|›|›d|j›d|›ƒ||Sqdt›d|›d}t    |ƒ‚dS)    NZ BLP_FORMAT_Z BLP_ENCODING_ZBLP_ALPHA_ENCODING_é
Ú.zmodule 'z' has no attribute 'ú')
rrrÚitemsÚ
startswithÚlenÚ __members__rr    ÚAttributeError)ÚnameÚenumÚprefixÚmsgr r rÚ __getattr__9sý
 
 r%cCs*|d?d@d>|d?d@d>|d@d>fS)Né éréé?rr )Úir r rÚ
unpack_565Hsr+Fc    CsÄt|ƒd}tƒtƒtƒtƒf}t|ƒD]”}|d}t d||¡\}}}t|ƒ\}    }
} t|ƒ\} } }tdƒD]L}tdƒD]<}|d@}|d?}d}|dkr°|    |
| }}}nØ|dkrÊ| | |}}}n¾|dkr6||krd|    | d}d|
| d}d| |d}n$|    | d}|
| d}| |d}nR|dkrˆ||kr|d| |    d}d| |
d}d|| d}n d    \}}}}|r¦|| ||||g¡q||| |||g¡q|qnq(|S)
úE
    input: one "row" of data (i.e. will produce 4*width pixels)
    éz<HHIérréÿrr)rrrr©rÚ    bytearrayÚrangeÚstructÚ unpack_fromr+Úextend)ÚdataÚalphaÚblocksÚretÚblockÚidxÚcolor0Úcolor1ÚbitsÚr0Úg0Úb0Úr1Úg1Úb1Újr*ÚcontrolÚaÚrÚgÚbr r rÚ decode_dxt1LsD 
 
 
 
 rKc    CsÔt|ƒd}tƒtƒtƒtƒf}t|ƒD]¤}|d}|||d…}t d|¡}t d|d¡\}}t d|d¡\}t|ƒ\}    }
} t|ƒ\} } }tdƒD]2}d}tdƒD]}d||d    }||}|rÚd}|dL}n d
}|d M}|d 9}|d    d||?d @}|dkr"|    |
| }}}n’|dkr>| | |}}}nv|d    krzd    |    | d }d    |
| d }d    | |d }n:|d kr´d    | |    d }d    | |
d }d    || d }|| ||||g¡qªq˜q(|S)r,éz<8Bú<HHr-ú<Ié r.FrTéérrrr0)r6r8r9r:r;r>r<r=Úcoder?r@rArBrCrDrEÚhighr*Úalphacode_indexrGÚ
color_coderHrIrJr r rÚ decode_dxt3ƒsH  
 
 
 
 
rVc    CsÀt|ƒd}tƒtƒtƒtƒf}t|ƒD]}|d}|||d…}t d|¡\}}t d|d¡}|d|dd>B|dd>B|dd    >B}|d
|d d>B}    t d |d¡\}
} t d |d¡\} t|
ƒ\} }}t| ƒ\}}}tdƒD]Ì}tdƒD]¼}dd||}|dkr&|    |?d@}n0|dkrF|    d?|d >d@B}n||d?d@}|d
krf|}nt|d krv|}nd||kržd|||d |d}n<|dkr®d
}n,|dkr¾d}nd|||d |d}| dd||?d@}|d
kr| ||}}}n’|d kr*|||}}}nv|dkrfd| |d}d||d}d||d}n:|dkr d|| d}d||d}d||d}|| ||||g¡qøqêq(|S)zG
    input: one "row" of data (i.e. will produce 4 * width pixels)
    rLz<BBz<6Brrr-r.r(érrrMrNrOrrPér/r0)r6r8r9r:r;Za0Za1r>Z
alphacode1Z
alphacode2r<r=rRr?r@rArBrCrDrEr*rTZ    alphacoderGrUrHrIrJr r rÚ decode_dxt5¸s^ ,
 
 
 
 
 
 
 
 
 
 
rYc@s eZdZdS)ÚBLPFormatErrorN)r    r
r r r r rrZþsrZcCs|dd…dkS)Nr.©óBLP1óBLP2r )r#r r rÚ_acceptsr^c@s eZdZdZdZdZdd„ZdS)Ú BlpImageFilez 
    Blizzard Mipmap Format
    ÚBLPzBlizzard Mipmap FormatcCs¼|j d¡|_|j dtj¡t d|j d¡¡\|_|j dtj¡t d|j d¡¡|_    |jdkrr|j 
¡}nd    t |jƒ›}t |ƒ‚|jr”d
nd |_ |d |jd |j d dffg|_dS)Nr.r(ú<brrú<IIr-r[zBad BLP magic ÚRGBAÚRGB©rrr)ÚfpÚreadÚmagicÚseekÚosÚSEEK_CURr3ÚunpackÚ_blp_alpha_depthÚ_sizeÚdecodeÚreprrZÚmodeÚsizeÚtile)ÚselfÚdecoderr$r r rÚ_opens
 zBlpImageFile._openN)r    r
r Ú__doc__ÚformatÚformat_descriptionrvr r r rr_sr_c@s8eZdZdZdd„Zdd„Zdd„Zdd    „Zd
d „Zd S) Ú_BLPBaseDecoderTc
CsLz| ¡| ¡Wn2tjk
rF}zd}t|ƒ|‚W5d}~XYnXdS)NzTruncated BLP file)éÿÿÿÿr)Ú_read_blp_headerÚ_loadr3ÚerrorÚOSError)rtÚbufferÚer$r r rro$s z_BLPBaseDecoder.decodecCsä|j d¡t d| d¡¡\|_t d| d¡¡\|_t d| d¡¡\|_t d| d¡¡\|_|j dt    j
¡t d| d¡¡|_ t |t ƒr¸t d| d¡¡\|_|j dt    j
¡t d| d¡¡|_t d| d¡¡|_dS)    Nr.ú<irarrbr-ú<16Ié@)Úfdrir3rlÚ
_safe_readÚ_blp_compressionÚ _blp_encodingrmÚ_blp_alpha_encodingrjrkrrÚ
isinstanceÚ BLP1DecoderÚ _blp_offsetsÚ _blp_lengths)rtr r rr|-s 
z _BLPBaseDecoder._read_blp_headercCst |j|¡S)N)rr†r…)rtÚlengthr r rr†@sz_BLPBaseDecoder._safe_readc    Csdg}tdƒD]R}zt d| d¡¡\}}}}Wntjk
rJYq`YnX| ||||f¡q |S)Néú<4Br.)r2r3rlr†r~Úappend)rtr9r*rJrIrHrGr r rÚ _read_paletteCs  z_BLPBaseDecoder._read_palettec
Csˆtƒ}t| |jd¡ƒ}zt d| d¡¡\}Wntjk
rLYq„YnX||\}}}}|||f}    |jrx|    |f7}    |     |    ¡q|S)Nrú<Br)
r1rr†rr3rlrgr~rmr5)
rtÚpaletter6Ú_dataÚoffsetrJrIrHrGÚdr r rÚ
_read_bgraMs
 
 
 z_BLPBaseDecoder._read_bgraN)    r    r
r Z    _pulls_fdror|r†r’r˜r r r rrz!s     
rzc@seZdZdd„Zdd„ZdS)r‹cCs‚|jtjkr| ¡nh|jdkrf|jdkrL| ¡}| |¡}| t|ƒ¡q~dt    |jƒ›}t
|ƒ‚ndt    |jƒ›}t
|ƒ‚dS)Nr)r.r(zUnsupported BLP encoding zUnsupported BLP compression ) r‡rr Ú_decode_jpeg_streamrˆr’r˜Ú
set_as_rawÚbytesrprZ)rtr”r6r$r r rr}^s 
 
 
 
 
zBLP1Decoder._loadc Csäddlm}t d| d¡¡\}| |¡}| |jd|j ¡¡| |jd¡}||}t    |ƒ}||ƒ}t
  |j ¡|j dkr¬|jd\}}}}    ||||    ddffg|_| d¡ ¡\}
} } t
 d| | |
f¡}| | ¡¡dS)Nr)Ú JpegImageFilerNr.rZCMYKrd)ZJpegImagePluginrœr3rlr†rŒr…ÚtellrrrZ_decompression_bomb_checkrrrqrsÚconvertÚsplitÚmergeršÚtobytes) rtrœZjpeg_header_sizeZ jpeg_headerr6ÚimageZ decoder_nameZextentsr–ÚargsrHrIrJr r rr™ns 
 
zBLP1Decoder._decode_jpeg_streamN)r    r
r r}r™r r r rr‹]sr‹c@seZdZdd„ZdS)Ú BLP2DecodercCsÚ| ¡}|j |jd¡|jdkr°|jtjkr@| |¡}qÈ|jtj    kr–t
ƒ}|j t j krº|jdddd}t|jdddƒD]*}t| |¡t|jƒdD] }||7}q¨qŒnÚ|j t jkr|jdddd}t|jdddƒD]"}t| |¡ƒD]}||7}qqônz|j t jkr||jdddd}t|jdddƒD]$}t| |¡ƒD]}||7}qfqTndt|j ƒ›}t|ƒ‚nd    t|jƒ›}t|ƒ‚nd
t|jƒ›}t|ƒ‚| t|ƒ¡dS) Nrrrr.r-)r7rLzUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )r’r…rirŒr‡rˆrrr˜rr1r‰rrrrr2rKr†ÚboolrmrrVrrYrprZršr›)rtr”r6ZlinesizeZybr—r$r r rr}‚s@   ÿ
 
 
zBLP2Decoder._loadN)r    r
r r}r r r rr¤sr¤c@s eZdZdZdd„Zdd„ZdS)Ú
BLPEncoderTc    CsZd}|j dd¡}tdƒD]:}||d|dd…\}}}}|t d||||¡7}q|S)Nórcrr.rr)ÚimZ
getpaletter2r3Úpack)rtr6r”r*rHrIrJrGr r rÚ_write_palette³s   zBLPEncoder._write_palettec    
Csš| ¡}dt|ƒ}tjd|fdžŽ}|jj\}}|tjd||fdžŽ7}||7}t|ƒD].}t|ƒD] }|t d|j ||f¡¡7}qhq\t|ƒd|fS)Né”rƒ)rrrrrrrrrrrrrrrr“r)rªrr3r©r¨rrr2Zgetpixel)    rtÚbufsizeZ palette_datar–r6ÚwÚhÚyÚxr r rÚencode»s     zBLPEncoder.encodeN)r    r
r Z
_pushes_fdrªr±r r r rr¦°sr¦cCs
|jdkrd}t|ƒ‚|j d¡dkr*dnd}| |¡| t dd¡¡| t d    tj¡¡| t d    |j    jd
krxdnd ¡¡| t d    d ¡¡| t d    d ¡¡| tjd|j
žŽ¡|dkræ| t dd ¡¡| t dd ¡¡t   ||dd|j
d |jfg¡dS)NÚPzUnsupported BLP image modeZ blp_versionÚBLP1r\r]r‚rrarcrrbr(r`re)rb) rqÚ
ValueErrorZ encoderinfoÚgetÚwriter3r©rrr”rrrÚ_save)r¨rfÚfilenameZsave_allr$rhr r rr·Ís
 
"r·z.blpr³ZBLP2r`)F)F)%rwrjr3r"rÚiorÚrrZ
_deprecaterrrrr%r+rKrVrYÚNotImplementedErrorrZr^r_Z    PyDecoderrzr‹r¤Z    PyEncoderr¦r·Z register_openrxZregister_extensionZregister_decoderZ register_saveZregister_encoderr r r rÚ<module>s8   
75F<$/