Delete venv
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- venv/bin/Activate.ps1 +0 -247
- venv/bin/accelerate +0 -7
- venv/bin/accelerate-config +0 -7
- venv/bin/accelerate-estimate-memory +0 -7
- venv/bin/accelerate-launch +0 -7
- venv/bin/accelerate-merge-weights +0 -7
- venv/bin/activate +0 -69
- venv/bin/activate.csh +0 -26
- venv/bin/activate.fish +0 -69
- venv/bin/datasets-cli +0 -7
- venv/bin/evaluate-cli +0 -7
- venv/bin/f2py +0 -7
- venv/bin/fonttools +0 -7
- venv/bin/get_gprof +0 -75
- venv/bin/get_objgraph +0 -54
- venv/bin/hf +0 -7
- venv/bin/httpx +0 -7
- venv/bin/huggingface-cli +0 -7
- venv/bin/isympy +0 -7
- venv/bin/normalizer +0 -7
- venv/bin/numpy-config +0 -7
- venv/bin/pip +0 -8
- venv/bin/pip3 +0 -8
- venv/bin/pip3.10 +0 -8
- venv/bin/proton +0 -7
- venv/bin/proton-viewer +0 -7
- venv/bin/pyftmerge +0 -7
- venv/bin/pyftsubset +0 -7
- venv/bin/python +0 -3
- venv/bin/python3 +0 -3
- venv/bin/python3.10 +0 -3
- venv/bin/tiny-agents +0 -7
- venv/bin/torchfrtrace +0 -7
- venv/bin/torchrun +0 -7
- venv/bin/tqdm +0 -7
- venv/bin/transformers +0 -7
- venv/bin/transformers-cli +0 -7
- venv/bin/trl +0 -7
- venv/bin/ttx +0 -7
- venv/bin/undill +0 -22
- venv/lib/python3.10/site-packages/PIL/AvifImagePlugin.py +0 -291
- venv/lib/python3.10/site-packages/PIL/BdfFontFile.py +0 -122
- venv/lib/python3.10/site-packages/PIL/BlpImagePlugin.py +0 -497
- venv/lib/python3.10/site-packages/PIL/BmpImagePlugin.py +0 -515
- venv/lib/python3.10/site-packages/PIL/BufrStubImagePlugin.py +0 -75
- venv/lib/python3.10/site-packages/PIL/ContainerIO.py +0 -173
- venv/lib/python3.10/site-packages/PIL/CurImagePlugin.py +0 -75
- venv/lib/python3.10/site-packages/PIL/DcxImagePlugin.py +0 -83
- venv/lib/python3.10/site-packages/PIL/DdsImagePlugin.py +0 -624
- venv/lib/python3.10/site-packages/PIL/EpsImagePlugin.py +0 -479
venv/bin/Activate.ps1
DELETED
|
@@ -1,247 +0,0 @@
|
|
| 1 |
-
<#
|
| 2 |
-
.Synopsis
|
| 3 |
-
Activate a Python virtual environment for the current PowerShell session.
|
| 4 |
-
|
| 5 |
-
.Description
|
| 6 |
-
Pushes the python executable for a virtual environment to the front of the
|
| 7 |
-
$Env:PATH environment variable and sets the prompt to signify that you are
|
| 8 |
-
in a Python virtual environment. Makes use of the command line switches as
|
| 9 |
-
well as the `pyvenv.cfg` file values present in the virtual environment.
|
| 10 |
-
|
| 11 |
-
.Parameter VenvDir
|
| 12 |
-
Path to the directory that contains the virtual environment to activate. The
|
| 13 |
-
default value for this is the parent of the directory that the Activate.ps1
|
| 14 |
-
script is located within.
|
| 15 |
-
|
| 16 |
-
.Parameter Prompt
|
| 17 |
-
The prompt prefix to display when this virtual environment is activated. By
|
| 18 |
-
default, this prompt is the name of the virtual environment folder (VenvDir)
|
| 19 |
-
surrounded by parentheses and followed by a single space (ie. '(.venv) ').
|
| 20 |
-
|
| 21 |
-
.Example
|
| 22 |
-
Activate.ps1
|
| 23 |
-
Activates the Python virtual environment that contains the Activate.ps1 script.
|
| 24 |
-
|
| 25 |
-
.Example
|
| 26 |
-
Activate.ps1 -Verbose
|
| 27 |
-
Activates the Python virtual environment that contains the Activate.ps1 script,
|
| 28 |
-
and shows extra information about the activation as it executes.
|
| 29 |
-
|
| 30 |
-
.Example
|
| 31 |
-
Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv
|
| 32 |
-
Activates the Python virtual environment located in the specified location.
|
| 33 |
-
|
| 34 |
-
.Example
|
| 35 |
-
Activate.ps1 -Prompt "MyPython"
|
| 36 |
-
Activates the Python virtual environment that contains the Activate.ps1 script,
|
| 37 |
-
and prefixes the current prompt with the specified string (surrounded in
|
| 38 |
-
parentheses) while the virtual environment is active.
|
| 39 |
-
|
| 40 |
-
.Notes
|
| 41 |
-
On Windows, it may be required to enable this Activate.ps1 script by setting the
|
| 42 |
-
execution policy for the user. You can do this by issuing the following PowerShell
|
| 43 |
-
command:
|
| 44 |
-
|
| 45 |
-
PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
|
| 46 |
-
|
| 47 |
-
For more information on Execution Policies:
|
| 48 |
-
https://go.microsoft.com/fwlink/?LinkID=135170
|
| 49 |
-
|
| 50 |
-
#>
|
| 51 |
-
Param(
|
| 52 |
-
[Parameter(Mandatory = $false)]
|
| 53 |
-
[String]
|
| 54 |
-
$VenvDir,
|
| 55 |
-
[Parameter(Mandatory = $false)]
|
| 56 |
-
[String]
|
| 57 |
-
$Prompt
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
<# Function declarations --------------------------------------------------- #>
|
| 61 |
-
|
| 62 |
-
<#
|
| 63 |
-
.Synopsis
|
| 64 |
-
Remove all shell session elements added by the Activate script, including the
|
| 65 |
-
addition of the virtual environment's Python executable from the beginning of
|
| 66 |
-
the PATH variable.
|
| 67 |
-
|
| 68 |
-
.Parameter NonDestructive
|
| 69 |
-
If present, do not remove this function from the global namespace for the
|
| 70 |
-
session.
|
| 71 |
-
|
| 72 |
-
#>
|
| 73 |
-
function global:deactivate ([switch]$NonDestructive) {
|
| 74 |
-
# Revert to original values
|
| 75 |
-
|
| 76 |
-
# The prior prompt:
|
| 77 |
-
if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) {
|
| 78 |
-
Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt
|
| 79 |
-
Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT
|
| 80 |
-
}
|
| 81 |
-
|
| 82 |
-
# The prior PYTHONHOME:
|
| 83 |
-
if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) {
|
| 84 |
-
Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME
|
| 85 |
-
Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
# The prior PATH:
|
| 89 |
-
if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) {
|
| 90 |
-
Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH
|
| 91 |
-
Remove-Item -Path Env:_OLD_VIRTUAL_PATH
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
-
# Just remove the VIRTUAL_ENV altogether:
|
| 95 |
-
if (Test-Path -Path Env:VIRTUAL_ENV) {
|
| 96 |
-
Remove-Item -Path env:VIRTUAL_ENV
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
# Just remove VIRTUAL_ENV_PROMPT altogether.
|
| 100 |
-
if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) {
|
| 101 |
-
Remove-Item -Path env:VIRTUAL_ENV_PROMPT
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
# Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether:
|
| 105 |
-
if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) {
|
| 106 |
-
Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
# Leave deactivate function in the global namespace if requested:
|
| 110 |
-
if (-not $NonDestructive) {
|
| 111 |
-
Remove-Item -Path function:deactivate
|
| 112 |
-
}
|
| 113 |
-
}
|
| 114 |
-
|
| 115 |
-
<#
|
| 116 |
-
.Description
|
| 117 |
-
Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the
|
| 118 |
-
given folder, and returns them in a map.
|
| 119 |
-
|
| 120 |
-
For each line in the pyvenv.cfg file, if that line can be parsed into exactly
|
| 121 |
-
two strings separated by `=` (with any amount of whitespace surrounding the =)
|
| 122 |
-
then it is considered a `key = value` line. The left hand string is the key,
|
| 123 |
-
the right hand is the value.
|
| 124 |
-
|
| 125 |
-
If the value starts with a `'` or a `"` then the first and last character is
|
| 126 |
-
stripped from the value before being captured.
|
| 127 |
-
|
| 128 |
-
.Parameter ConfigDir
|
| 129 |
-
Path to the directory that contains the `pyvenv.cfg` file.
|
| 130 |
-
#>
|
| 131 |
-
function Get-PyVenvConfig(
|
| 132 |
-
[String]
|
| 133 |
-
$ConfigDir
|
| 134 |
-
) {
|
| 135 |
-
Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg"
|
| 136 |
-
|
| 137 |
-
# Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue).
|
| 138 |
-
$pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue
|
| 139 |
-
|
| 140 |
-
# An empty map will be returned if no config file is found.
|
| 141 |
-
$pyvenvConfig = @{ }
|
| 142 |
-
|
| 143 |
-
if ($pyvenvConfigPath) {
|
| 144 |
-
|
| 145 |
-
Write-Verbose "File exists, parse `key = value` lines"
|
| 146 |
-
$pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath
|
| 147 |
-
|
| 148 |
-
$pyvenvConfigContent | ForEach-Object {
|
| 149 |
-
$keyval = $PSItem -split "\s*=\s*", 2
|
| 150 |
-
if ($keyval[0] -and $keyval[1]) {
|
| 151 |
-
$val = $keyval[1]
|
| 152 |
-
|
| 153 |
-
# Remove extraneous quotations around a string value.
|
| 154 |
-
if ("'""".Contains($val.Substring(0, 1))) {
|
| 155 |
-
$val = $val.Substring(1, $val.Length - 2)
|
| 156 |
-
}
|
| 157 |
-
|
| 158 |
-
$pyvenvConfig[$keyval[0]] = $val
|
| 159 |
-
Write-Verbose "Adding Key: '$($keyval[0])'='$val'"
|
| 160 |
-
}
|
| 161 |
-
}
|
| 162 |
-
}
|
| 163 |
-
return $pyvenvConfig
|
| 164 |
-
}
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
<# Begin Activate script --------------------------------------------------- #>
|
| 168 |
-
|
| 169 |
-
# Determine the containing directory of this script
|
| 170 |
-
$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
|
| 171 |
-
$VenvExecDir = Get-Item -Path $VenvExecPath
|
| 172 |
-
|
| 173 |
-
Write-Verbose "Activation script is located in path: '$VenvExecPath'"
|
| 174 |
-
Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)"
|
| 175 |
-
Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)"
|
| 176 |
-
|
| 177 |
-
# Set values required in priority: CmdLine, ConfigFile, Default
|
| 178 |
-
# First, get the location of the virtual environment, it might not be
|
| 179 |
-
# VenvExecDir if specified on the command line.
|
| 180 |
-
if ($VenvDir) {
|
| 181 |
-
Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values"
|
| 182 |
-
}
|
| 183 |
-
else {
|
| 184 |
-
Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir."
|
| 185 |
-
$VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/")
|
| 186 |
-
Write-Verbose "VenvDir=$VenvDir"
|
| 187 |
-
}
|
| 188 |
-
|
| 189 |
-
# Next, read the `pyvenv.cfg` file to determine any required value such
|
| 190 |
-
# as `prompt`.
|
| 191 |
-
$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir
|
| 192 |
-
|
| 193 |
-
# Next, set the prompt from the command line, or the config file, or
|
| 194 |
-
# just use the name of the virtual environment folder.
|
| 195 |
-
if ($Prompt) {
|
| 196 |
-
Write-Verbose "Prompt specified as argument, using '$Prompt'"
|
| 197 |
-
}
|
| 198 |
-
else {
|
| 199 |
-
Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value"
|
| 200 |
-
if ($pyvenvCfg -and $pyvenvCfg['prompt']) {
|
| 201 |
-
Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'"
|
| 202 |
-
$Prompt = $pyvenvCfg['prompt'];
|
| 203 |
-
}
|
| 204 |
-
else {
|
| 205 |
-
Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)"
|
| 206 |
-
Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'"
|
| 207 |
-
$Prompt = Split-Path -Path $venvDir -Leaf
|
| 208 |
-
}
|
| 209 |
-
}
|
| 210 |
-
|
| 211 |
-
Write-Verbose "Prompt = '$Prompt'"
|
| 212 |
-
Write-Verbose "VenvDir='$VenvDir'"
|
| 213 |
-
|
| 214 |
-
# Deactivate any currently active virtual environment, but leave the
|
| 215 |
-
# deactivate function in place.
|
| 216 |
-
deactivate -nondestructive
|
| 217 |
-
|
| 218 |
-
# Now set the environment variable VIRTUAL_ENV, used by many tools to determine
|
| 219 |
-
# that there is an activated venv.
|
| 220 |
-
$env:VIRTUAL_ENV = $VenvDir
|
| 221 |
-
|
| 222 |
-
if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) {
|
| 223 |
-
|
| 224 |
-
Write-Verbose "Setting prompt to '$Prompt'"
|
| 225 |
-
|
| 226 |
-
# Set the prompt to include the env name
|
| 227 |
-
# Make sure _OLD_VIRTUAL_PROMPT is global
|
| 228 |
-
function global:_OLD_VIRTUAL_PROMPT { "" }
|
| 229 |
-
Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT
|
| 230 |
-
New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt
|
| 231 |
-
|
| 232 |
-
function global:prompt {
|
| 233 |
-
Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) "
|
| 234 |
-
_OLD_VIRTUAL_PROMPT
|
| 235 |
-
}
|
| 236 |
-
$env:VIRTUAL_ENV_PROMPT = $Prompt
|
| 237 |
-
}
|
| 238 |
-
|
| 239 |
-
# Clear PYTHONHOME
|
| 240 |
-
if (Test-Path -Path Env:PYTHONHOME) {
|
| 241 |
-
Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME
|
| 242 |
-
Remove-Item -Path Env:PYTHONHOME
|
| 243 |
-
}
|
| 244 |
-
|
| 245 |
-
# Add the venv to the PATH
|
| 246 |
-
Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH
|
| 247 |
-
$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/accelerate
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from accelerate.commands.accelerate_cli import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/accelerate-config
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from accelerate.commands.config import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/accelerate-estimate-memory
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from accelerate.commands.estimate import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/accelerate-launch
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from accelerate.commands.launch import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/accelerate-merge-weights
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from accelerate.commands.merge import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/activate
DELETED
|
@@ -1,69 +0,0 @@
|
|
| 1 |
-
# This file must be used with "source bin/activate" *from bash*
|
| 2 |
-
# you cannot run it directly
|
| 3 |
-
|
| 4 |
-
deactivate () {
|
| 5 |
-
# reset old environment variables
|
| 6 |
-
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
|
| 7 |
-
PATH="${_OLD_VIRTUAL_PATH:-}"
|
| 8 |
-
export PATH
|
| 9 |
-
unset _OLD_VIRTUAL_PATH
|
| 10 |
-
fi
|
| 11 |
-
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
|
| 12 |
-
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
|
| 13 |
-
export PYTHONHOME
|
| 14 |
-
unset _OLD_VIRTUAL_PYTHONHOME
|
| 15 |
-
fi
|
| 16 |
-
|
| 17 |
-
# This should detect bash and zsh, which have a hash command that must
|
| 18 |
-
# be called to get it to forget past commands. Without forgetting
|
| 19 |
-
# past commands the $PATH changes we made may not be respected
|
| 20 |
-
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
|
| 21 |
-
hash -r 2> /dev/null
|
| 22 |
-
fi
|
| 23 |
-
|
| 24 |
-
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
|
| 25 |
-
PS1="${_OLD_VIRTUAL_PS1:-}"
|
| 26 |
-
export PS1
|
| 27 |
-
unset _OLD_VIRTUAL_PS1
|
| 28 |
-
fi
|
| 29 |
-
|
| 30 |
-
unset VIRTUAL_ENV
|
| 31 |
-
unset VIRTUAL_ENV_PROMPT
|
| 32 |
-
if [ ! "${1:-}" = "nondestructive" ] ; then
|
| 33 |
-
# Self destruct!
|
| 34 |
-
unset -f deactivate
|
| 35 |
-
fi
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
# unset irrelevant variables
|
| 39 |
-
deactivate nondestructive
|
| 40 |
-
|
| 41 |
-
VIRTUAL_ENV=/home/ubuntu/arc-lm/train/venv
|
| 42 |
-
export VIRTUAL_ENV
|
| 43 |
-
|
| 44 |
-
_OLD_VIRTUAL_PATH="$PATH"
|
| 45 |
-
PATH="$VIRTUAL_ENV/"bin":$PATH"
|
| 46 |
-
export PATH
|
| 47 |
-
|
| 48 |
-
# unset PYTHONHOME if set
|
| 49 |
-
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
|
| 50 |
-
# could use `if (set -u; : $PYTHONHOME) ;` in bash
|
| 51 |
-
if [ -n "${PYTHONHOME:-}" ] ; then
|
| 52 |
-
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
|
| 53 |
-
unset PYTHONHOME
|
| 54 |
-
fi
|
| 55 |
-
|
| 56 |
-
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
|
| 57 |
-
_OLD_VIRTUAL_PS1="${PS1:-}"
|
| 58 |
-
PS1='(venv) '"${PS1:-}"
|
| 59 |
-
export PS1
|
| 60 |
-
VIRTUAL_ENV_PROMPT='(venv) '
|
| 61 |
-
export VIRTUAL_ENV_PROMPT
|
| 62 |
-
fi
|
| 63 |
-
|
| 64 |
-
# This should detect bash and zsh, which have a hash command that must
|
| 65 |
-
# be called to get it to forget past commands. Without forgetting
|
| 66 |
-
# past commands the $PATH changes we made may not be respected
|
| 67 |
-
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
|
| 68 |
-
hash -r 2> /dev/null
|
| 69 |
-
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/activate.csh
DELETED
|
@@ -1,26 +0,0 @@
|
|
| 1 |
-
# This file must be used with "source bin/activate.csh" *from csh*.
|
| 2 |
-
# You cannot run it directly.
|
| 3 |
-
# Created by Davide Di Blasi <davidedb@gmail.com>.
|
| 4 |
-
# Ported to Python 3.3 venv by Andrew Svetlov <andrew.svetlov@gmail.com>
|
| 5 |
-
|
| 6 |
-
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate'
|
| 7 |
-
|
| 8 |
-
# Unset irrelevant variables.
|
| 9 |
-
deactivate nondestructive
|
| 10 |
-
|
| 11 |
-
setenv VIRTUAL_ENV /home/ubuntu/arc-lm/train/venv
|
| 12 |
-
|
| 13 |
-
set _OLD_VIRTUAL_PATH="$PATH"
|
| 14 |
-
setenv PATH "$VIRTUAL_ENV/"bin":$PATH"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
set _OLD_VIRTUAL_PROMPT="$prompt"
|
| 18 |
-
|
| 19 |
-
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
|
| 20 |
-
set prompt = '(venv) '"$prompt"
|
| 21 |
-
setenv VIRTUAL_ENV_PROMPT '(venv) '
|
| 22 |
-
endif
|
| 23 |
-
|
| 24 |
-
alias pydoc python -m pydoc
|
| 25 |
-
|
| 26 |
-
rehash
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/activate.fish
DELETED
|
@@ -1,69 +0,0 @@
|
|
| 1 |
-
# This file must be used with "source <venv>/bin/activate.fish" *from fish*
|
| 2 |
-
# (https://fishshell.com/); you cannot run it directly.
|
| 3 |
-
|
| 4 |
-
function deactivate -d "Exit virtual environment and return to normal shell environment"
|
| 5 |
-
# reset old environment variables
|
| 6 |
-
if test -n "$_OLD_VIRTUAL_PATH"
|
| 7 |
-
set -gx PATH $_OLD_VIRTUAL_PATH
|
| 8 |
-
set -e _OLD_VIRTUAL_PATH
|
| 9 |
-
end
|
| 10 |
-
if test -n "$_OLD_VIRTUAL_PYTHONHOME"
|
| 11 |
-
set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME
|
| 12 |
-
set -e _OLD_VIRTUAL_PYTHONHOME
|
| 13 |
-
end
|
| 14 |
-
|
| 15 |
-
if test -n "$_OLD_FISH_PROMPT_OVERRIDE"
|
| 16 |
-
set -e _OLD_FISH_PROMPT_OVERRIDE
|
| 17 |
-
# prevents error when using nested fish instances (Issue #93858)
|
| 18 |
-
if functions -q _old_fish_prompt
|
| 19 |
-
functions -e fish_prompt
|
| 20 |
-
functions -c _old_fish_prompt fish_prompt
|
| 21 |
-
functions -e _old_fish_prompt
|
| 22 |
-
end
|
| 23 |
-
end
|
| 24 |
-
|
| 25 |
-
set -e VIRTUAL_ENV
|
| 26 |
-
set -e VIRTUAL_ENV_PROMPT
|
| 27 |
-
if test "$argv[1]" != "nondestructive"
|
| 28 |
-
# Self-destruct!
|
| 29 |
-
functions -e deactivate
|
| 30 |
-
end
|
| 31 |
-
end
|
| 32 |
-
|
| 33 |
-
# Unset irrelevant variables.
|
| 34 |
-
deactivate nondestructive
|
| 35 |
-
|
| 36 |
-
set -gx VIRTUAL_ENV /home/ubuntu/arc-lm/train/venv
|
| 37 |
-
|
| 38 |
-
set -gx _OLD_VIRTUAL_PATH $PATH
|
| 39 |
-
set -gx PATH "$VIRTUAL_ENV/"bin $PATH
|
| 40 |
-
|
| 41 |
-
# Unset PYTHONHOME if set.
|
| 42 |
-
if set -q PYTHONHOME
|
| 43 |
-
set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME
|
| 44 |
-
set -e PYTHONHOME
|
| 45 |
-
end
|
| 46 |
-
|
| 47 |
-
if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
|
| 48 |
-
# fish uses a function instead of an env var to generate the prompt.
|
| 49 |
-
|
| 50 |
-
# Save the current fish_prompt function as the function _old_fish_prompt.
|
| 51 |
-
functions -c fish_prompt _old_fish_prompt
|
| 52 |
-
|
| 53 |
-
# With the original prompt function renamed, we can override with our own.
|
| 54 |
-
function fish_prompt
|
| 55 |
-
# Save the return status of the last command.
|
| 56 |
-
set -l old_status $status
|
| 57 |
-
|
| 58 |
-
# Output the venv prompt; color taken from the blue of the Python logo.
|
| 59 |
-
printf "%s%s%s" (set_color 4B8BBE) '(venv) ' (set_color normal)
|
| 60 |
-
|
| 61 |
-
# Restore the return status of the previous command.
|
| 62 |
-
echo "exit $old_status" | .
|
| 63 |
-
# Output the original/"old" prompt.
|
| 64 |
-
_old_fish_prompt
|
| 65 |
-
end
|
| 66 |
-
|
| 67 |
-
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
|
| 68 |
-
set -gx VIRTUAL_ENV_PROMPT '(venv) '
|
| 69 |
-
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/datasets-cli
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from datasets.commands.datasets_cli import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/evaluate-cli
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from evaluate.commands.evaluate_cli import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/f2py
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from numpy.f2py.f2py2e import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/fonttools
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from fontTools.__main__ import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/get_gprof
DELETED
|
@@ -1,75 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
#
|
| 3 |
-
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
|
| 4 |
-
# Copyright (c) 2008-2016 California Institute of Technology.
|
| 5 |
-
# Copyright (c) 2016-2025 The Uncertainty Quantification Foundation.
|
| 6 |
-
# License: 3-clause BSD. The full license text is available at:
|
| 7 |
-
# - https://github.com/uqfoundation/dill/blob/master/LICENSE
|
| 8 |
-
'''
|
| 9 |
-
build profile graph for the given instance
|
| 10 |
-
|
| 11 |
-
running:
|
| 12 |
-
$ get_gprof <args> <instance>
|
| 13 |
-
|
| 14 |
-
executes:
|
| 15 |
-
gprof2dot -f pstats <args> <type>.prof | dot -Tpng -o <type>.call.png
|
| 16 |
-
|
| 17 |
-
where:
|
| 18 |
-
<args> are arguments for gprof2dot, such as "-n 5 -e 5"
|
| 19 |
-
<instance> is code to create the instance to profile
|
| 20 |
-
<type> is the class of the instance (i.e. type(instance))
|
| 21 |
-
|
| 22 |
-
For example:
|
| 23 |
-
$ get_gprof -n 5 -e 1 "import numpy; numpy.array([1,2])"
|
| 24 |
-
|
| 25 |
-
will create 'ndarray.call.png' with the profile graph for numpy.array([1,2]),
|
| 26 |
-
where '-n 5' eliminates nodes below 5% threshold, similarly '-e 1' eliminates
|
| 27 |
-
edges below 1% threshold
|
| 28 |
-
'''
|
| 29 |
-
|
| 30 |
-
if __name__ == "__main__":
|
| 31 |
-
import sys
|
| 32 |
-
if len(sys.argv) < 2:
|
| 33 |
-
print ("Please provide an object instance (e.g. 'import math; math.pi')")
|
| 34 |
-
sys.exit()
|
| 35 |
-
# grab args for gprof2dot
|
| 36 |
-
args = sys.argv[1:-1]
|
| 37 |
-
args = ' '.join(args)
|
| 38 |
-
# last arg builds the object
|
| 39 |
-
obj = sys.argv[-1]
|
| 40 |
-
obj = obj.split(';')
|
| 41 |
-
# multi-line prep for generating an instance
|
| 42 |
-
for line in obj[:-1]:
|
| 43 |
-
exec(line)
|
| 44 |
-
# one-line generation of an instance
|
| 45 |
-
try:
|
| 46 |
-
obj = eval(obj[-1])
|
| 47 |
-
except Exception:
|
| 48 |
-
print ("Error processing object instance")
|
| 49 |
-
sys.exit()
|
| 50 |
-
|
| 51 |
-
# get object 'name'
|
| 52 |
-
objtype = type(obj)
|
| 53 |
-
name = getattr(objtype, '__name__', getattr(objtype, '__class__', objtype))
|
| 54 |
-
|
| 55 |
-
# profile dumping an object
|
| 56 |
-
import dill
|
| 57 |
-
import os
|
| 58 |
-
import cProfile
|
| 59 |
-
#name = os.path.splitext(os.path.basename(__file__))[0]
|
| 60 |
-
cProfile.run("dill.dumps(obj)", filename="%s.prof" % name)
|
| 61 |
-
msg = "gprof2dot -f pstats %s %s.prof | dot -Tpng -o %s.call.png" % (args, name, name)
|
| 62 |
-
try:
|
| 63 |
-
res = os.system(msg)
|
| 64 |
-
except Exception:
|
| 65 |
-
print ("Please verify install of 'gprof2dot' to view profile graphs")
|
| 66 |
-
if res:
|
| 67 |
-
print ("Please verify install of 'gprof2dot' to view profile graphs")
|
| 68 |
-
|
| 69 |
-
# get stats
|
| 70 |
-
f_prof = "%s.prof" % name
|
| 71 |
-
import pstats
|
| 72 |
-
stats = pstats.Stats(f_prof, stream=sys.stdout)
|
| 73 |
-
stats.strip_dirs().sort_stats('cumtime')
|
| 74 |
-
stats.print_stats(20) #XXX: save to file instead of print top 20?
|
| 75 |
-
os.remove(f_prof)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/get_objgraph
DELETED
|
@@ -1,54 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
#
|
| 3 |
-
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
|
| 4 |
-
# Copyright (c) 2008-2016 California Institute of Technology.
|
| 5 |
-
# Copyright (c) 2016-2025 The Uncertainty Quantification Foundation.
|
| 6 |
-
# License: 3-clause BSD. The full license text is available at:
|
| 7 |
-
# - https://github.com/uqfoundation/dill/blob/master/LICENSE
|
| 8 |
-
"""
|
| 9 |
-
display the reference paths for objects in ``dill.types`` or a .pkl file
|
| 10 |
-
|
| 11 |
-
Notes:
|
| 12 |
-
the generated image is useful in showing the pointer references in
|
| 13 |
-
objects that are or can be pickled. Any object in ``dill.objects``
|
| 14 |
-
listed in ``dill.load_types(picklable=True, unpicklable=True)`` works.
|
| 15 |
-
|
| 16 |
-
Examples::
|
| 17 |
-
|
| 18 |
-
$ get_objgraph ArrayType
|
| 19 |
-
Image generated as ArrayType.png
|
| 20 |
-
"""
|
| 21 |
-
|
| 22 |
-
import dill as pickle
|
| 23 |
-
#pickle.debug.trace(True)
|
| 24 |
-
#import pickle
|
| 25 |
-
|
| 26 |
-
# get all objects for testing
|
| 27 |
-
from dill import load_types
|
| 28 |
-
load_types(pickleable=True,unpickleable=True)
|
| 29 |
-
from dill import objects
|
| 30 |
-
|
| 31 |
-
if __name__ == "__main__":
|
| 32 |
-
import sys
|
| 33 |
-
if len(sys.argv) != 2:
|
| 34 |
-
print ("Please provide exactly one file or type name (e.g. 'IntType')")
|
| 35 |
-
msg = "\n"
|
| 36 |
-
for objtype in list(objects.keys())[:40]:
|
| 37 |
-
msg += objtype + ', '
|
| 38 |
-
print (msg + "...")
|
| 39 |
-
else:
|
| 40 |
-
objtype = str(sys.argv[-1])
|
| 41 |
-
try:
|
| 42 |
-
obj = objects[objtype]
|
| 43 |
-
except KeyError:
|
| 44 |
-
obj = pickle.load(open(objtype,'rb'))
|
| 45 |
-
import os
|
| 46 |
-
objtype = os.path.splitext(objtype)[0]
|
| 47 |
-
try:
|
| 48 |
-
import objgraph
|
| 49 |
-
objgraph.show_refs(obj, filename=objtype+'.png')
|
| 50 |
-
except ImportError:
|
| 51 |
-
print ("Please install 'objgraph' to view object graphs")
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# EOF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/hf
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from huggingface_hub.cli.hf import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/httpx
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from httpx import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/huggingface-cli
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from huggingface_hub.commands.huggingface_cli import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/isympy
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from isympy import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/normalizer
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from charset_normalizer.cli import cli_detect
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(cli_detect())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/numpy-config
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from numpy._configtool import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/pip
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
# -*- coding: utf-8 -*-
|
| 3 |
-
import re
|
| 4 |
-
import sys
|
| 5 |
-
from pip._internal.cli.main import main
|
| 6 |
-
if __name__ == '__main__':
|
| 7 |
-
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
| 8 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/pip3
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
# -*- coding: utf-8 -*-
|
| 3 |
-
import re
|
| 4 |
-
import sys
|
| 5 |
-
from pip._internal.cli.main import main
|
| 6 |
-
if __name__ == '__main__':
|
| 7 |
-
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
| 8 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/pip3.10
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
# -*- coding: utf-8 -*-
|
| 3 |
-
import re
|
| 4 |
-
import sys
|
| 5 |
-
from pip._internal.cli.main import main
|
| 6 |
-
if __name__ == '__main__':
|
| 7 |
-
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
| 8 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/proton
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from triton.profiler.proton import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/proton-viewer
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from triton.profiler.viewer import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/pyftmerge
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from fontTools.merge import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/pyftsubset
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from fontTools.subset import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/python
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:df23391eb5ab66f005be15f792d55766d7ce96198e9e10df2aea72aec8c686ea
|
| 3 |
-
size 5937768
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/python3
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:df23391eb5ab66f005be15f792d55766d7ce96198e9e10df2aea72aec8c686ea
|
| 3 |
-
size 5937768
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/python3.10
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:df23391eb5ab66f005be15f792d55766d7ce96198e9e10df2aea72aec8c686ea
|
| 3 |
-
size 5937768
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/tiny-agents
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from huggingface_hub.inference._mcp.cli import app
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(app())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/torchfrtrace
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from tools.flight_recorder.fr_trace import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/torchrun
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from torch.distributed.run import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/tqdm
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from tqdm.cli import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/transformers
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from transformers.commands.transformers_cli import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/transformers-cli
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from transformers.commands.transformers_cli import main_cli
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main_cli())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/trl
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from trl.cli import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/ttx
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
import sys
|
| 3 |
-
from fontTools.ttx import main
|
| 4 |
-
if __name__ == '__main__':
|
| 5 |
-
if sys.argv[0].endswith('.exe'):
|
| 6 |
-
sys.argv[0] = sys.argv[0][:-4]
|
| 7 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/bin/undill
DELETED
|
@@ -1,22 +0,0 @@
|
|
| 1 |
-
#!/home/ubuntu/arc-lm/train/venv/bin/python
|
| 2 |
-
#
|
| 3 |
-
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
|
| 4 |
-
# Copyright (c) 2008-2016 California Institute of Technology.
|
| 5 |
-
# Copyright (c) 2016-2025 The Uncertainty Quantification Foundation.
|
| 6 |
-
# License: 3-clause BSD. The full license text is available at:
|
| 7 |
-
# - https://github.com/uqfoundation/dill/blob/master/LICENSE
|
| 8 |
-
"""
|
| 9 |
-
unpickle the contents of a pickled object file
|
| 10 |
-
|
| 11 |
-
Examples::
|
| 12 |
-
|
| 13 |
-
$ undill hello.pkl
|
| 14 |
-
['hello', 'world']
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
-
if __name__ == '__main__':
|
| 18 |
-
import sys
|
| 19 |
-
import dill
|
| 20 |
-
for file in sys.argv[1:]:
|
| 21 |
-
print (dill.load(open(file,'rb')))
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/lib/python3.10/site-packages/PIL/AvifImagePlugin.py
DELETED
|
@@ -1,291 +0,0 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
from io import BytesIO
|
| 5 |
-
from typing import IO
|
| 6 |
-
|
| 7 |
-
from . import ExifTags, Image, ImageFile
|
| 8 |
-
|
| 9 |
-
try:
|
| 10 |
-
from . import _avif
|
| 11 |
-
|
| 12 |
-
SUPPORTED = True
|
| 13 |
-
except ImportError:
|
| 14 |
-
SUPPORTED = False
|
| 15 |
-
|
| 16 |
-
# Decoder options as module globals, until there is a way to pass parameters
|
| 17 |
-
# to Image.open (see https://github.com/python-pillow/Pillow/issues/569)
|
| 18 |
-
DECODE_CODEC_CHOICE = "auto"
|
| 19 |
-
DEFAULT_MAX_THREADS = 0
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def get_codec_version(codec_name: str) -> str | None:
|
| 23 |
-
versions = _avif.codec_versions()
|
| 24 |
-
for version in versions.split(", "):
|
| 25 |
-
if version.split(" [")[0] == codec_name:
|
| 26 |
-
return version.split(":")[-1].split(" ")[0]
|
| 27 |
-
return None
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def _accept(prefix: bytes) -> bool | str:
|
| 31 |
-
if prefix[4:8] != b"ftyp":
|
| 32 |
-
return False
|
| 33 |
-
major_brand = prefix[8:12]
|
| 34 |
-
if major_brand in (
|
| 35 |
-
# coding brands
|
| 36 |
-
b"avif",
|
| 37 |
-
b"avis",
|
| 38 |
-
# We accept files with AVIF container brands; we can't yet know if
|
| 39 |
-
# the ftyp box has the correct compatible brands, but if it doesn't
|
| 40 |
-
# then the plugin will raise a SyntaxError which Pillow will catch
|
| 41 |
-
# before moving on to the next plugin that accepts the file.
|
| 42 |
-
#
|
| 43 |
-
# Also, because this file might not actually be an AVIF file, we
|
| 44 |
-
# don't raise an error if AVIF support isn't properly compiled.
|
| 45 |
-
b"mif1",
|
| 46 |
-
b"msf1",
|
| 47 |
-
):
|
| 48 |
-
if not SUPPORTED:
|
| 49 |
-
return (
|
| 50 |
-
"image file could not be identified because AVIF support not installed"
|
| 51 |
-
)
|
| 52 |
-
return True
|
| 53 |
-
return False
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def _get_default_max_threads() -> int:
|
| 57 |
-
if DEFAULT_MAX_THREADS:
|
| 58 |
-
return DEFAULT_MAX_THREADS
|
| 59 |
-
if hasattr(os, "sched_getaffinity"):
|
| 60 |
-
return len(os.sched_getaffinity(0))
|
| 61 |
-
else:
|
| 62 |
-
return os.cpu_count() or 1
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
class AvifImageFile(ImageFile.ImageFile):
|
| 66 |
-
format = "AVIF"
|
| 67 |
-
format_description = "AVIF image"
|
| 68 |
-
__frame = -1
|
| 69 |
-
|
| 70 |
-
def _open(self) -> None:
|
| 71 |
-
if not SUPPORTED:
|
| 72 |
-
msg = "image file could not be opened because AVIF support not installed"
|
| 73 |
-
raise SyntaxError(msg)
|
| 74 |
-
|
| 75 |
-
if DECODE_CODEC_CHOICE != "auto" and not _avif.decoder_codec_available(
|
| 76 |
-
DECODE_CODEC_CHOICE
|
| 77 |
-
):
|
| 78 |
-
msg = "Invalid opening codec"
|
| 79 |
-
raise ValueError(msg)
|
| 80 |
-
self._decoder = _avif.AvifDecoder(
|
| 81 |
-
self.fp.read(),
|
| 82 |
-
DECODE_CODEC_CHOICE,
|
| 83 |
-
_get_default_max_threads(),
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
# Get info from decoder
|
| 87 |
-
self._size, self.n_frames, self._mode, icc, exif, exif_orientation, xmp = (
|
| 88 |
-
self._decoder.get_info()
|
| 89 |
-
)
|
| 90 |
-
self.is_animated = self.n_frames > 1
|
| 91 |
-
|
| 92 |
-
if icc:
|
| 93 |
-
self.info["icc_profile"] = icc
|
| 94 |
-
if xmp:
|
| 95 |
-
self.info["xmp"] = xmp
|
| 96 |
-
|
| 97 |
-
if exif_orientation != 1 or exif:
|
| 98 |
-
exif_data = Image.Exif()
|
| 99 |
-
if exif:
|
| 100 |
-
exif_data.load(exif)
|
| 101 |
-
original_orientation = exif_data.get(ExifTags.Base.Orientation, 1)
|
| 102 |
-
else:
|
| 103 |
-
original_orientation = 1
|
| 104 |
-
if exif_orientation != original_orientation:
|
| 105 |
-
exif_data[ExifTags.Base.Orientation] = exif_orientation
|
| 106 |
-
exif = exif_data.tobytes()
|
| 107 |
-
if exif:
|
| 108 |
-
self.info["exif"] = exif
|
| 109 |
-
self.seek(0)
|
| 110 |
-
|
| 111 |
-
def seek(self, frame: int) -> None:
|
| 112 |
-
if not self._seek_check(frame):
|
| 113 |
-
return
|
| 114 |
-
|
| 115 |
-
# Set tile
|
| 116 |
-
self.__frame = frame
|
| 117 |
-
self.tile = [ImageFile._Tile("raw", (0, 0) + self.size, 0, self.mode)]
|
| 118 |
-
|
| 119 |
-
def load(self) -> Image.core.PixelAccess | None:
|
| 120 |
-
if self.tile:
|
| 121 |
-
# We need to load the image data for this frame
|
| 122 |
-
data, timescale, pts_in_timescales, duration_in_timescales = (
|
| 123 |
-
self._decoder.get_frame(self.__frame)
|
| 124 |
-
)
|
| 125 |
-
self.info["timestamp"] = round(1000 * (pts_in_timescales / timescale))
|
| 126 |
-
self.info["duration"] = round(1000 * (duration_in_timescales / timescale))
|
| 127 |
-
|
| 128 |
-
if self.fp and self._exclusive_fp:
|
| 129 |
-
self.fp.close()
|
| 130 |
-
self.fp = BytesIO(data)
|
| 131 |
-
|
| 132 |
-
return super().load()
|
| 133 |
-
|
| 134 |
-
def load_seek(self, pos: int) -> None:
|
| 135 |
-
pass
|
| 136 |
-
|
| 137 |
-
def tell(self) -> int:
|
| 138 |
-
return self.__frame
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 142 |
-
_save(im, fp, filename, save_all=True)
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def _save(
|
| 146 |
-
im: Image.Image, fp: IO[bytes], filename: str | bytes, save_all: bool = False
|
| 147 |
-
) -> None:
|
| 148 |
-
info = im.encoderinfo.copy()
|
| 149 |
-
if save_all:
|
| 150 |
-
append_images = list(info.get("append_images", []))
|
| 151 |
-
else:
|
| 152 |
-
append_images = []
|
| 153 |
-
|
| 154 |
-
total = 0
|
| 155 |
-
for ims in [im] + append_images:
|
| 156 |
-
total += getattr(ims, "n_frames", 1)
|
| 157 |
-
|
| 158 |
-
quality = info.get("quality", 75)
|
| 159 |
-
if not isinstance(quality, int) or quality < 0 or quality > 100:
|
| 160 |
-
msg = "Invalid quality setting"
|
| 161 |
-
raise ValueError(msg)
|
| 162 |
-
|
| 163 |
-
duration = info.get("duration", 0)
|
| 164 |
-
subsampling = info.get("subsampling", "4:2:0")
|
| 165 |
-
speed = info.get("speed", 6)
|
| 166 |
-
max_threads = info.get("max_threads", _get_default_max_threads())
|
| 167 |
-
codec = info.get("codec", "auto")
|
| 168 |
-
if codec != "auto" and not _avif.encoder_codec_available(codec):
|
| 169 |
-
msg = "Invalid saving codec"
|
| 170 |
-
raise ValueError(msg)
|
| 171 |
-
range_ = info.get("range", "full")
|
| 172 |
-
tile_rows_log2 = info.get("tile_rows", 0)
|
| 173 |
-
tile_cols_log2 = info.get("tile_cols", 0)
|
| 174 |
-
alpha_premultiplied = bool(info.get("alpha_premultiplied", False))
|
| 175 |
-
autotiling = bool(info.get("autotiling", tile_rows_log2 == tile_cols_log2 == 0))
|
| 176 |
-
|
| 177 |
-
icc_profile = info.get("icc_profile", im.info.get("icc_profile"))
|
| 178 |
-
exif_orientation = 1
|
| 179 |
-
if exif := info.get("exif"):
|
| 180 |
-
if isinstance(exif, Image.Exif):
|
| 181 |
-
exif_data = exif
|
| 182 |
-
else:
|
| 183 |
-
exif_data = Image.Exif()
|
| 184 |
-
exif_data.load(exif)
|
| 185 |
-
if ExifTags.Base.Orientation in exif_data:
|
| 186 |
-
exif_orientation = exif_data.pop(ExifTags.Base.Orientation)
|
| 187 |
-
exif = exif_data.tobytes() if exif_data else b""
|
| 188 |
-
elif isinstance(exif, Image.Exif):
|
| 189 |
-
exif = exif_data.tobytes()
|
| 190 |
-
|
| 191 |
-
xmp = info.get("xmp")
|
| 192 |
-
|
| 193 |
-
if isinstance(xmp, str):
|
| 194 |
-
xmp = xmp.encode("utf-8")
|
| 195 |
-
|
| 196 |
-
advanced = info.get("advanced")
|
| 197 |
-
if advanced is not None:
|
| 198 |
-
if isinstance(advanced, dict):
|
| 199 |
-
advanced = advanced.items()
|
| 200 |
-
try:
|
| 201 |
-
advanced = tuple(advanced)
|
| 202 |
-
except TypeError:
|
| 203 |
-
invalid = True
|
| 204 |
-
else:
|
| 205 |
-
invalid = any(not isinstance(v, tuple) or len(v) != 2 for v in advanced)
|
| 206 |
-
if invalid:
|
| 207 |
-
msg = (
|
| 208 |
-
"advanced codec options must be a dict of key-value string "
|
| 209 |
-
"pairs or a series of key-value two-tuples"
|
| 210 |
-
)
|
| 211 |
-
raise ValueError(msg)
|
| 212 |
-
|
| 213 |
-
# Setup the AVIF encoder
|
| 214 |
-
enc = _avif.AvifEncoder(
|
| 215 |
-
im.size,
|
| 216 |
-
subsampling,
|
| 217 |
-
quality,
|
| 218 |
-
speed,
|
| 219 |
-
max_threads,
|
| 220 |
-
codec,
|
| 221 |
-
range_,
|
| 222 |
-
tile_rows_log2,
|
| 223 |
-
tile_cols_log2,
|
| 224 |
-
alpha_premultiplied,
|
| 225 |
-
autotiling,
|
| 226 |
-
icc_profile or b"",
|
| 227 |
-
exif or b"",
|
| 228 |
-
exif_orientation,
|
| 229 |
-
xmp or b"",
|
| 230 |
-
advanced,
|
| 231 |
-
)
|
| 232 |
-
|
| 233 |
-
# Add each frame
|
| 234 |
-
frame_idx = 0
|
| 235 |
-
frame_duration = 0
|
| 236 |
-
cur_idx = im.tell()
|
| 237 |
-
is_single_frame = total == 1
|
| 238 |
-
try:
|
| 239 |
-
for ims in [im] + append_images:
|
| 240 |
-
# Get number of frames in this image
|
| 241 |
-
nfr = getattr(ims, "n_frames", 1)
|
| 242 |
-
|
| 243 |
-
for idx in range(nfr):
|
| 244 |
-
ims.seek(idx)
|
| 245 |
-
|
| 246 |
-
# Make sure image mode is supported
|
| 247 |
-
frame = ims
|
| 248 |
-
rawmode = ims.mode
|
| 249 |
-
if ims.mode not in {"RGB", "RGBA"}:
|
| 250 |
-
rawmode = "RGBA" if ims.has_transparency_data else "RGB"
|
| 251 |
-
frame = ims.convert(rawmode)
|
| 252 |
-
|
| 253 |
-
# Update frame duration
|
| 254 |
-
if isinstance(duration, (list, tuple)):
|
| 255 |
-
frame_duration = duration[frame_idx]
|
| 256 |
-
else:
|
| 257 |
-
frame_duration = duration
|
| 258 |
-
|
| 259 |
-
# Append the frame to the animation encoder
|
| 260 |
-
enc.add(
|
| 261 |
-
frame.tobytes("raw", rawmode),
|
| 262 |
-
frame_duration,
|
| 263 |
-
frame.size,
|
| 264 |
-
rawmode,
|
| 265 |
-
is_single_frame,
|
| 266 |
-
)
|
| 267 |
-
|
| 268 |
-
# Update frame index
|
| 269 |
-
frame_idx += 1
|
| 270 |
-
|
| 271 |
-
if not save_all:
|
| 272 |
-
break
|
| 273 |
-
|
| 274 |
-
finally:
|
| 275 |
-
im.seek(cur_idx)
|
| 276 |
-
|
| 277 |
-
# Get the final output from the encoder
|
| 278 |
-
data = enc.finish()
|
| 279 |
-
if data is None:
|
| 280 |
-
msg = "cannot write file as AVIF (encoder returned None)"
|
| 281 |
-
raise OSError(msg)
|
| 282 |
-
|
| 283 |
-
fp.write(data)
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
Image.register_open(AvifImageFile.format, AvifImageFile, _accept)
|
| 287 |
-
if SUPPORTED:
|
| 288 |
-
Image.register_save(AvifImageFile.format, _save)
|
| 289 |
-
Image.register_save_all(AvifImageFile.format, _save_all)
|
| 290 |
-
Image.register_extensions(AvifImageFile.format, [".avif", ".avifs"])
|
| 291 |
-
Image.register_mime(AvifImageFile.format, "image/avif")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/lib/python3.10/site-packages/PIL/BdfFontFile.py
DELETED
|
@@ -1,122 +0,0 @@
|
|
| 1 |
-
#
|
| 2 |
-
# The Python Imaging Library
|
| 3 |
-
# $Id$
|
| 4 |
-
#
|
| 5 |
-
# bitmap distribution font (bdf) file parser
|
| 6 |
-
#
|
| 7 |
-
# history:
|
| 8 |
-
# 1996-05-16 fl created (as bdf2pil)
|
| 9 |
-
# 1997-08-25 fl converted to FontFile driver
|
| 10 |
-
# 2001-05-25 fl removed bogus __init__ call
|
| 11 |
-
# 2002-11-20 fl robustification (from Kevin Cazabon, Dmitry Vasiliev)
|
| 12 |
-
# 2003-04-22 fl more robustification (from Graham Dumpleton)
|
| 13 |
-
#
|
| 14 |
-
# Copyright (c) 1997-2003 by Secret Labs AB.
|
| 15 |
-
# Copyright (c) 1997-2003 by Fredrik Lundh.
|
| 16 |
-
#
|
| 17 |
-
# See the README file for information on usage and redistribution.
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
"""
|
| 21 |
-
Parse X Bitmap Distribution Format (BDF)
|
| 22 |
-
"""
|
| 23 |
-
from __future__ import annotations
|
| 24 |
-
|
| 25 |
-
from typing import BinaryIO
|
| 26 |
-
|
| 27 |
-
from . import FontFile, Image
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def bdf_char(
|
| 31 |
-
f: BinaryIO,
|
| 32 |
-
) -> (
|
| 33 |
-
tuple[
|
| 34 |
-
str,
|
| 35 |
-
int,
|
| 36 |
-
tuple[tuple[int, int], tuple[int, int, int, int], tuple[int, int, int, int]],
|
| 37 |
-
Image.Image,
|
| 38 |
-
]
|
| 39 |
-
| None
|
| 40 |
-
):
|
| 41 |
-
# skip to STARTCHAR
|
| 42 |
-
while True:
|
| 43 |
-
s = f.readline()
|
| 44 |
-
if not s:
|
| 45 |
-
return None
|
| 46 |
-
if s.startswith(b"STARTCHAR"):
|
| 47 |
-
break
|
| 48 |
-
id = s[9:].strip().decode("ascii")
|
| 49 |
-
|
| 50 |
-
# load symbol properties
|
| 51 |
-
props = {}
|
| 52 |
-
while True:
|
| 53 |
-
s = f.readline()
|
| 54 |
-
if not s or s.startswith(b"BITMAP"):
|
| 55 |
-
break
|
| 56 |
-
i = s.find(b" ")
|
| 57 |
-
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
|
| 58 |
-
|
| 59 |
-
# load bitmap
|
| 60 |
-
bitmap = bytearray()
|
| 61 |
-
while True:
|
| 62 |
-
s = f.readline()
|
| 63 |
-
if not s or s.startswith(b"ENDCHAR"):
|
| 64 |
-
break
|
| 65 |
-
bitmap += s[:-1]
|
| 66 |
-
|
| 67 |
-
# The word BBX
|
| 68 |
-
# followed by the width in x (BBw), height in y (BBh),
|
| 69 |
-
# and x and y displacement (BBxoff0, BByoff0)
|
| 70 |
-
# of the lower left corner from the origin of the character.
|
| 71 |
-
width, height, x_disp, y_disp = (int(p) for p in props["BBX"].split())
|
| 72 |
-
|
| 73 |
-
# The word DWIDTH
|
| 74 |
-
# followed by the width in x and y of the character in device pixels.
|
| 75 |
-
dwx, dwy = (int(p) for p in props["DWIDTH"].split())
|
| 76 |
-
|
| 77 |
-
bbox = (
|
| 78 |
-
(dwx, dwy),
|
| 79 |
-
(x_disp, -y_disp - height, width + x_disp, -y_disp),
|
| 80 |
-
(0, 0, width, height),
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
try:
|
| 84 |
-
im = Image.frombytes("1", (width, height), bitmap, "hex", "1")
|
| 85 |
-
except ValueError:
|
| 86 |
-
# deal with zero-width characters
|
| 87 |
-
im = Image.new("1", (width, height))
|
| 88 |
-
|
| 89 |
-
return id, int(props["ENCODING"]), bbox, im
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
class BdfFontFile(FontFile.FontFile):
|
| 93 |
-
"""Font file plugin for the X11 BDF format."""
|
| 94 |
-
|
| 95 |
-
def __init__(self, fp: BinaryIO) -> None:
|
| 96 |
-
super().__init__()
|
| 97 |
-
|
| 98 |
-
s = fp.readline()
|
| 99 |
-
if not s.startswith(b"STARTFONT 2.1"):
|
| 100 |
-
msg = "not a valid BDF file"
|
| 101 |
-
raise SyntaxError(msg)
|
| 102 |
-
|
| 103 |
-
props = {}
|
| 104 |
-
comments = []
|
| 105 |
-
|
| 106 |
-
while True:
|
| 107 |
-
s = fp.readline()
|
| 108 |
-
if not s or s.startswith(b"ENDPROPERTIES"):
|
| 109 |
-
break
|
| 110 |
-
i = s.find(b" ")
|
| 111 |
-
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
|
| 112 |
-
if s[:i] in [b"COMMENT", b"COPYRIGHT"]:
|
| 113 |
-
if s.find(b"LogicalFontDescription") < 0:
|
| 114 |
-
comments.append(s[i + 1 : -1].decode("ascii"))
|
| 115 |
-
|
| 116 |
-
while True:
|
| 117 |
-
c = bdf_char(fp)
|
| 118 |
-
if not c:
|
| 119 |
-
break
|
| 120 |
-
id, ch, (xy, dst, src), im = c
|
| 121 |
-
if 0 <= ch < len(self.glyph):
|
| 122 |
-
self.glyph[ch] = xy, dst, src, im
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/lib/python3.10/site-packages/PIL/BlpImagePlugin.py
DELETED
|
@@ -1,497 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Blizzard Mipmap Format (.blp)
|
| 3 |
-
Jerome Leclanche <jerome@leclan.ch>
|
| 4 |
-
|
| 5 |
-
The contents of this file are hereby released in the public domain (CC0)
|
| 6 |
-
Full text of the CC0 license:
|
| 7 |
-
https://creativecommons.org/publicdomain/zero/1.0/
|
| 8 |
-
|
| 9 |
-
BLP1 files, used mostly in Warcraft III, are not fully supported.
|
| 10 |
-
All types of BLP2 files used in World of Warcraft are supported.
|
| 11 |
-
|
| 12 |
-
The BLP file structure consists of a header, up to 16 mipmaps of the
|
| 13 |
-
texture
|
| 14 |
-
|
| 15 |
-
Texture sizes must be powers of two, though the two dimensions do
|
| 16 |
-
not have to be equal; 512x256 is valid, but 512x200 is not.
|
| 17 |
-
The first mipmap (mipmap #0) is the full size image; each subsequent
|
| 18 |
-
mipmap halves both dimensions. The final mipmap should be 1x1.
|
| 19 |
-
|
| 20 |
-
BLP files come in many different flavours:
|
| 21 |
-
* JPEG-compressed (type == 0) - only supported for BLP1.
|
| 22 |
-
* RAW images (type == 1, encoding == 1). Each mipmap is stored as an
|
| 23 |
-
array of 8-bit values, one per pixel, left to right, top to bottom.
|
| 24 |
-
Each value is an index to the palette.
|
| 25 |
-
* DXT-compressed (type == 1, encoding == 2):
|
| 26 |
-
- DXT1 compression is used if alpha_encoding == 0.
|
| 27 |
-
- An additional alpha bit is used if alpha_depth == 1.
|
| 28 |
-
- DXT3 compression is used if alpha_encoding == 1.
|
| 29 |
-
- DXT5 compression is used if alpha_encoding == 7.
|
| 30 |
-
"""
|
| 31 |
-
|
| 32 |
-
from __future__ import annotations
|
| 33 |
-
|
| 34 |
-
import abc
|
| 35 |
-
import os
|
| 36 |
-
import struct
|
| 37 |
-
from enum import IntEnum
|
| 38 |
-
from io import BytesIO
|
| 39 |
-
from typing import IO
|
| 40 |
-
|
| 41 |
-
from . import Image, ImageFile
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
class Format(IntEnum):
|
| 45 |
-
JPEG = 0
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
class Encoding(IntEnum):
|
| 49 |
-
UNCOMPRESSED = 1
|
| 50 |
-
DXT = 2
|
| 51 |
-
UNCOMPRESSED_RAW_BGRA = 3
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
class AlphaEncoding(IntEnum):
|
| 55 |
-
DXT1 = 0
|
| 56 |
-
DXT3 = 1
|
| 57 |
-
DXT5 = 7
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def unpack_565(i: int) -> tuple[int, int, int]:
|
| 61 |
-
return ((i >> 11) & 0x1F) << 3, ((i >> 5) & 0x3F) << 2, (i & 0x1F) << 3
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
def decode_dxt1(
|
| 65 |
-
data: bytes, alpha: bool = False
|
| 66 |
-
) -> tuple[bytearray, bytearray, bytearray, bytearray]:
|
| 67 |
-
"""
|
| 68 |
-
input: one "row" of data (i.e. will produce 4*width pixels)
|
| 69 |
-
"""
|
| 70 |
-
|
| 71 |
-
blocks = len(data) // 8 # number of blocks in row
|
| 72 |
-
ret = (bytearray(), bytearray(), bytearray(), bytearray())
|
| 73 |
-
|
| 74 |
-
for block_index in range(blocks):
|
| 75 |
-
# Decode next 8-byte block.
|
| 76 |
-
idx = block_index * 8
|
| 77 |
-
color0, color1, bits = struct.unpack_from("<HHI", data, idx)
|
| 78 |
-
|
| 79 |
-
r0, g0, b0 = unpack_565(color0)
|
| 80 |
-
r1, g1, b1 = unpack_565(color1)
|
| 81 |
-
|
| 82 |
-
# Decode this block into 4x4 pixels
|
| 83 |
-
# Accumulate the results onto our 4 row accumulators
|
| 84 |
-
for j in range(4):
|
| 85 |
-
for i in range(4):
|
| 86 |
-
# get next control op and generate a pixel
|
| 87 |
-
|
| 88 |
-
control = bits & 3
|
| 89 |
-
bits = bits >> 2
|
| 90 |
-
|
| 91 |
-
a = 0xFF
|
| 92 |
-
if control == 0:
|
| 93 |
-
r, g, b = r0, g0, b0
|
| 94 |
-
elif control == 1:
|
| 95 |
-
r, g, b = r1, g1, b1
|
| 96 |
-
elif control == 2:
|
| 97 |
-
if color0 > color1:
|
| 98 |
-
r = (2 * r0 + r1) // 3
|
| 99 |
-
g = (2 * g0 + g1) // 3
|
| 100 |
-
b = (2 * b0 + b1) // 3
|
| 101 |
-
else:
|
| 102 |
-
r = (r0 + r1) // 2
|
| 103 |
-
g = (g0 + g1) // 2
|
| 104 |
-
b = (b0 + b1) // 2
|
| 105 |
-
elif control == 3:
|
| 106 |
-
if color0 > color1:
|
| 107 |
-
r = (2 * r1 + r0) // 3
|
| 108 |
-
g = (2 * g1 + g0) // 3
|
| 109 |
-
b = (2 * b1 + b0) // 3
|
| 110 |
-
else:
|
| 111 |
-
r, g, b, a = 0, 0, 0, 0
|
| 112 |
-
|
| 113 |
-
if alpha:
|
| 114 |
-
ret[j].extend([r, g, b, a])
|
| 115 |
-
else:
|
| 116 |
-
ret[j].extend([r, g, b])
|
| 117 |
-
|
| 118 |
-
return ret
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
def decode_dxt3(data: bytes) -> tuple[bytearray, bytearray, bytearray, bytearray]:
|
| 122 |
-
"""
|
| 123 |
-
input: one "row" of data (i.e. will produce 4*width pixels)
|
| 124 |
-
"""
|
| 125 |
-
|
| 126 |
-
blocks = len(data) // 16 # number of blocks in row
|
| 127 |
-
ret = (bytearray(), bytearray(), bytearray(), bytearray())
|
| 128 |
-
|
| 129 |
-
for block_index in range(blocks):
|
| 130 |
-
idx = block_index * 16
|
| 131 |
-
block = data[idx : idx + 16]
|
| 132 |
-
# Decode next 16-byte block.
|
| 133 |
-
bits = struct.unpack_from("<8B", block)
|
| 134 |
-
color0, color1 = struct.unpack_from("<HH", block, 8)
|
| 135 |
-
|
| 136 |
-
(code,) = struct.unpack_from("<I", block, 12)
|
| 137 |
-
|
| 138 |
-
r0, g0, b0 = unpack_565(color0)
|
| 139 |
-
r1, g1, b1 = unpack_565(color1)
|
| 140 |
-
|
| 141 |
-
for j in range(4):
|
| 142 |
-
high = False # Do we want the higher bits?
|
| 143 |
-
for i in range(4):
|
| 144 |
-
alphacode_index = (4 * j + i) // 2
|
| 145 |
-
a = bits[alphacode_index]
|
| 146 |
-
if high:
|
| 147 |
-
high = False
|
| 148 |
-
a >>= 4
|
| 149 |
-
else:
|
| 150 |
-
high = True
|
| 151 |
-
a &= 0xF
|
| 152 |
-
a *= 17 # We get a value between 0 and 15
|
| 153 |
-
|
| 154 |
-
color_code = (code >> 2 * (4 * j + i)) & 0x03
|
| 155 |
-
|
| 156 |
-
if color_code == 0:
|
| 157 |
-
r, g, b = r0, g0, b0
|
| 158 |
-
elif color_code == 1:
|
| 159 |
-
r, g, b = r1, g1, b1
|
| 160 |
-
elif color_code == 2:
|
| 161 |
-
r = (2 * r0 + r1) // 3
|
| 162 |
-
g = (2 * g0 + g1) // 3
|
| 163 |
-
b = (2 * b0 + b1) // 3
|
| 164 |
-
elif color_code == 3:
|
| 165 |
-
r = (2 * r1 + r0) // 3
|
| 166 |
-
g = (2 * g1 + g0) // 3
|
| 167 |
-
b = (2 * b1 + b0) // 3
|
| 168 |
-
|
| 169 |
-
ret[j].extend([r, g, b, a])
|
| 170 |
-
|
| 171 |
-
return ret
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
def decode_dxt5(data: bytes) -> tuple[bytearray, bytearray, bytearray, bytearray]:
|
| 175 |
-
"""
|
| 176 |
-
input: one "row" of data (i.e. will produce 4 * width pixels)
|
| 177 |
-
"""
|
| 178 |
-
|
| 179 |
-
blocks = len(data) // 16 # number of blocks in row
|
| 180 |
-
ret = (bytearray(), bytearray(), bytearray(), bytearray())
|
| 181 |
-
|
| 182 |
-
for block_index in range(blocks):
|
| 183 |
-
idx = block_index * 16
|
| 184 |
-
block = data[idx : idx + 16]
|
| 185 |
-
# Decode next 16-byte block.
|
| 186 |
-
a0, a1 = struct.unpack_from("<BB", block)
|
| 187 |
-
|
| 188 |
-
bits = struct.unpack_from("<6B", block, 2)
|
| 189 |
-
alphacode1 = bits[2] | (bits[3] << 8) | (bits[4] << 16) | (bits[5] << 24)
|
| 190 |
-
alphacode2 = bits[0] | (bits[1] << 8)
|
| 191 |
-
|
| 192 |
-
color0, color1 = struct.unpack_from("<HH", block, 8)
|
| 193 |
-
|
| 194 |
-
(code,) = struct.unpack_from("<I", block, 12)
|
| 195 |
-
|
| 196 |
-
r0, g0, b0 = unpack_565(color0)
|
| 197 |
-
r1, g1, b1 = unpack_565(color1)
|
| 198 |
-
|
| 199 |
-
for j in range(4):
|
| 200 |
-
for i in range(4):
|
| 201 |
-
# get next control op and generate a pixel
|
| 202 |
-
alphacode_index = 3 * (4 * j + i)
|
| 203 |
-
|
| 204 |
-
if alphacode_index <= 12:
|
| 205 |
-
alphacode = (alphacode2 >> alphacode_index) & 0x07
|
| 206 |
-
elif alphacode_index == 15:
|
| 207 |
-
alphacode = (alphacode2 >> 15) | ((alphacode1 << 1) & 0x06)
|
| 208 |
-
else: # alphacode_index >= 18 and alphacode_index <= 45
|
| 209 |
-
alphacode = (alphacode1 >> (alphacode_index - 16)) & 0x07
|
| 210 |
-
|
| 211 |
-
if alphacode == 0:
|
| 212 |
-
a = a0
|
| 213 |
-
elif alphacode == 1:
|
| 214 |
-
a = a1
|
| 215 |
-
elif a0 > a1:
|
| 216 |
-
a = ((8 - alphacode) * a0 + (alphacode - 1) * a1) // 7
|
| 217 |
-
elif alphacode == 6:
|
| 218 |
-
a = 0
|
| 219 |
-
elif alphacode == 7:
|
| 220 |
-
a = 255
|
| 221 |
-
else:
|
| 222 |
-
a = ((6 - alphacode) * a0 + (alphacode - 1) * a1) // 5
|
| 223 |
-
|
| 224 |
-
color_code = (code >> 2 * (4 * j + i)) & 0x03
|
| 225 |
-
|
| 226 |
-
if color_code == 0:
|
| 227 |
-
r, g, b = r0, g0, b0
|
| 228 |
-
elif color_code == 1:
|
| 229 |
-
r, g, b = r1, g1, b1
|
| 230 |
-
elif color_code == 2:
|
| 231 |
-
r = (2 * r0 + r1) // 3
|
| 232 |
-
g = (2 * g0 + g1) // 3
|
| 233 |
-
b = (2 * b0 + b1) // 3
|
| 234 |
-
elif color_code == 3:
|
| 235 |
-
r = (2 * r1 + r0) // 3
|
| 236 |
-
g = (2 * g1 + g0) // 3
|
| 237 |
-
b = (2 * b1 + b0) // 3
|
| 238 |
-
|
| 239 |
-
ret[j].extend([r, g, b, a])
|
| 240 |
-
|
| 241 |
-
return ret
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
class BLPFormatError(NotImplementedError):
|
| 245 |
-
pass
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
def _accept(prefix: bytes) -> bool:
|
| 249 |
-
return prefix.startswith((b"BLP1", b"BLP2"))
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
class BlpImageFile(ImageFile.ImageFile):
|
| 253 |
-
"""
|
| 254 |
-
Blizzard Mipmap Format
|
| 255 |
-
"""
|
| 256 |
-
|
| 257 |
-
format = "BLP"
|
| 258 |
-
format_description = "Blizzard Mipmap Format"
|
| 259 |
-
|
| 260 |
-
def _open(self) -> None:
|
| 261 |
-
self.magic = self.fp.read(4)
|
| 262 |
-
if not _accept(self.magic):
|
| 263 |
-
msg = f"Bad BLP magic {repr(self.magic)}"
|
| 264 |
-
raise BLPFormatError(msg)
|
| 265 |
-
|
| 266 |
-
compression = struct.unpack("<i", self.fp.read(4))[0]
|
| 267 |
-
if self.magic == b"BLP1":
|
| 268 |
-
alpha = struct.unpack("<I", self.fp.read(4))[0] != 0
|
| 269 |
-
else:
|
| 270 |
-
encoding = struct.unpack("<b", self.fp.read(1))[0]
|
| 271 |
-
alpha = struct.unpack("<b", self.fp.read(1))[0] != 0
|
| 272 |
-
alpha_encoding = struct.unpack("<b", self.fp.read(1))[0]
|
| 273 |
-
self.fp.seek(1, os.SEEK_CUR) # mips
|
| 274 |
-
|
| 275 |
-
self._size = struct.unpack("<II", self.fp.read(8))
|
| 276 |
-
|
| 277 |
-
args: tuple[int, int, bool] | tuple[int, int, bool, int]
|
| 278 |
-
if self.magic == b"BLP1":
|
| 279 |
-
encoding = struct.unpack("<i", self.fp.read(4))[0]
|
| 280 |
-
self.fp.seek(4, os.SEEK_CUR) # subtype
|
| 281 |
-
|
| 282 |
-
args = (compression, encoding, alpha)
|
| 283 |
-
offset = 28
|
| 284 |
-
else:
|
| 285 |
-
args = (compression, encoding, alpha, alpha_encoding)
|
| 286 |
-
offset = 20
|
| 287 |
-
|
| 288 |
-
decoder = self.magic.decode()
|
| 289 |
-
|
| 290 |
-
self._mode = "RGBA" if alpha else "RGB"
|
| 291 |
-
self.tile = [ImageFile._Tile(decoder, (0, 0) + self.size, offset, args)]
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
class _BLPBaseDecoder(abc.ABC, ImageFile.PyDecoder):
|
| 295 |
-
_pulls_fd = True
|
| 296 |
-
|
| 297 |
-
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
|
| 298 |
-
try:
|
| 299 |
-
self._read_header()
|
| 300 |
-
self._load()
|
| 301 |
-
except struct.error as e:
|
| 302 |
-
msg = "Truncated BLP file"
|
| 303 |
-
raise OSError(msg) from e
|
| 304 |
-
return -1, 0
|
| 305 |
-
|
| 306 |
-
@abc.abstractmethod
|
| 307 |
-
def _load(self) -> None:
|
| 308 |
-
pass
|
| 309 |
-
|
| 310 |
-
def _read_header(self) -> None:
|
| 311 |
-
self._offsets = struct.unpack("<16I", self._safe_read(16 * 4))
|
| 312 |
-
self._lengths = struct.unpack("<16I", self._safe_read(16 * 4))
|
| 313 |
-
|
| 314 |
-
def _safe_read(self, length: int) -> bytes:
|
| 315 |
-
assert self.fd is not None
|
| 316 |
-
return ImageFile._safe_read(self.fd, length)
|
| 317 |
-
|
| 318 |
-
def _read_palette(self) -> list[tuple[int, int, int, int]]:
|
| 319 |
-
ret = []
|
| 320 |
-
for i in range(256):
|
| 321 |
-
try:
|
| 322 |
-
b, g, r, a = struct.unpack("<4B", self._safe_read(4))
|
| 323 |
-
except struct.error:
|
| 324 |
-
break
|
| 325 |
-
ret.append((b, g, r, a))
|
| 326 |
-
return ret
|
| 327 |
-
|
| 328 |
-
def _read_bgra(
|
| 329 |
-
self, palette: list[tuple[int, int, int, int]], alpha: bool
|
| 330 |
-
) -> bytearray:
|
| 331 |
-
data = bytearray()
|
| 332 |
-
_data = BytesIO(self._safe_read(self._lengths[0]))
|
| 333 |
-
while True:
|
| 334 |
-
try:
|
| 335 |
-
(offset,) = struct.unpack("<B", _data.read(1))
|
| 336 |
-
except struct.error:
|
| 337 |
-
break
|
| 338 |
-
b, g, r, a = palette[offset]
|
| 339 |
-
d: tuple[int, ...] = (r, g, b)
|
| 340 |
-
if alpha:
|
| 341 |
-
d += (a,)
|
| 342 |
-
data.extend(d)
|
| 343 |
-
return data
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
class BLP1Decoder(_BLPBaseDecoder):
|
| 347 |
-
def _load(self) -> None:
|
| 348 |
-
self._compression, self._encoding, alpha = self.args
|
| 349 |
-
|
| 350 |
-
if self._compression == Format.JPEG:
|
| 351 |
-
self._decode_jpeg_stream()
|
| 352 |
-
|
| 353 |
-
elif self._compression == 1:
|
| 354 |
-
if self._encoding in (4, 5):
|
| 355 |
-
palette = self._read_palette()
|
| 356 |
-
data = self._read_bgra(palette, alpha)
|
| 357 |
-
self.set_as_raw(data)
|
| 358 |
-
else:
|
| 359 |
-
msg = f"Unsupported BLP encoding {repr(self._encoding)}"
|
| 360 |
-
raise BLPFormatError(msg)
|
| 361 |
-
else:
|
| 362 |
-
msg = f"Unsupported BLP compression {repr(self._encoding)}"
|
| 363 |
-
raise BLPFormatError(msg)
|
| 364 |
-
|
| 365 |
-
def _decode_jpeg_stream(self) -> None:
|
| 366 |
-
from .JpegImagePlugin import JpegImageFile
|
| 367 |
-
|
| 368 |
-
(jpeg_header_size,) = struct.unpack("<I", self._safe_read(4))
|
| 369 |
-
jpeg_header = self._safe_read(jpeg_header_size)
|
| 370 |
-
assert self.fd is not None
|
| 371 |
-
self._safe_read(self._offsets[0] - self.fd.tell()) # What IS this?
|
| 372 |
-
data = self._safe_read(self._lengths[0])
|
| 373 |
-
data = jpeg_header + data
|
| 374 |
-
image = JpegImageFile(BytesIO(data))
|
| 375 |
-
Image._decompression_bomb_check(image.size)
|
| 376 |
-
if image.mode == "CMYK":
|
| 377 |
-
args = image.tile[0].args
|
| 378 |
-
assert isinstance(args, tuple)
|
| 379 |
-
image.tile = [image.tile[0]._replace(args=(args[0], "CMYK"))]
|
| 380 |
-
self.set_as_raw(image.convert("RGB").tobytes(), "BGR")
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
class BLP2Decoder(_BLPBaseDecoder):
|
| 384 |
-
def _load(self) -> None:
|
| 385 |
-
self._compression, self._encoding, alpha, self._alpha_encoding = self.args
|
| 386 |
-
|
| 387 |
-
palette = self._read_palette()
|
| 388 |
-
|
| 389 |
-
assert self.fd is not None
|
| 390 |
-
self.fd.seek(self._offsets[0])
|
| 391 |
-
|
| 392 |
-
if self._compression == 1:
|
| 393 |
-
# Uncompressed or DirectX compression
|
| 394 |
-
|
| 395 |
-
if self._encoding == Encoding.UNCOMPRESSED:
|
| 396 |
-
data = self._read_bgra(palette, alpha)
|
| 397 |
-
|
| 398 |
-
elif self._encoding == Encoding.DXT:
|
| 399 |
-
data = bytearray()
|
| 400 |
-
if self._alpha_encoding == AlphaEncoding.DXT1:
|
| 401 |
-
linesize = (self.state.xsize + 3) // 4 * 8
|
| 402 |
-
for yb in range((self.state.ysize + 3) // 4):
|
| 403 |
-
for d in decode_dxt1(self._safe_read(linesize), alpha):
|
| 404 |
-
data += d
|
| 405 |
-
|
| 406 |
-
elif self._alpha_encoding == AlphaEncoding.DXT3:
|
| 407 |
-
linesize = (self.state.xsize + 3) // 4 * 16
|
| 408 |
-
for yb in range((self.state.ysize + 3) // 4):
|
| 409 |
-
for d in decode_dxt3(self._safe_read(linesize)):
|
| 410 |
-
data += d
|
| 411 |
-
|
| 412 |
-
elif self._alpha_encoding == AlphaEncoding.DXT5:
|
| 413 |
-
linesize = (self.state.xsize + 3) // 4 * 16
|
| 414 |
-
for yb in range((self.state.ysize + 3) // 4):
|
| 415 |
-
for d in decode_dxt5(self._safe_read(linesize)):
|
| 416 |
-
data += d
|
| 417 |
-
else:
|
| 418 |
-
msg = f"Unsupported alpha encoding {repr(self._alpha_encoding)}"
|
| 419 |
-
raise BLPFormatError(msg)
|
| 420 |
-
else:
|
| 421 |
-
msg = f"Unknown BLP encoding {repr(self._encoding)}"
|
| 422 |
-
raise BLPFormatError(msg)
|
| 423 |
-
|
| 424 |
-
else:
|
| 425 |
-
msg = f"Unknown BLP compression {repr(self._compression)}"
|
| 426 |
-
raise BLPFormatError(msg)
|
| 427 |
-
|
| 428 |
-
self.set_as_raw(data)
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
class BLPEncoder(ImageFile.PyEncoder):
|
| 432 |
-
_pushes_fd = True
|
| 433 |
-
|
| 434 |
-
def _write_palette(self) -> bytes:
|
| 435 |
-
data = b""
|
| 436 |
-
assert self.im is not None
|
| 437 |
-
palette = self.im.getpalette("RGBA", "RGBA")
|
| 438 |
-
for i in range(len(palette) // 4):
|
| 439 |
-
r, g, b, a = palette[i * 4 : (i + 1) * 4]
|
| 440 |
-
data += struct.pack("<4B", b, g, r, a)
|
| 441 |
-
while len(data) < 256 * 4:
|
| 442 |
-
data += b"\x00" * 4
|
| 443 |
-
return data
|
| 444 |
-
|
| 445 |
-
def encode(self, bufsize: int) -> tuple[int, int, bytes]:
|
| 446 |
-
palette_data = self._write_palette()
|
| 447 |
-
|
| 448 |
-
offset = 20 + 16 * 4 * 2 + len(palette_data)
|
| 449 |
-
data = struct.pack("<16I", offset, *((0,) * 15))
|
| 450 |
-
|
| 451 |
-
assert self.im is not None
|
| 452 |
-
w, h = self.im.size
|
| 453 |
-
data += struct.pack("<16I", w * h, *((0,) * 15))
|
| 454 |
-
|
| 455 |
-
data += palette_data
|
| 456 |
-
|
| 457 |
-
for y in range(h):
|
| 458 |
-
for x in range(w):
|
| 459 |
-
data += struct.pack("<B", self.im.getpixel((x, y)))
|
| 460 |
-
|
| 461 |
-
return len(data), 0, data
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 465 |
-
if im.mode != "P":
|
| 466 |
-
msg = "Unsupported BLP image mode"
|
| 467 |
-
raise ValueError(msg)
|
| 468 |
-
|
| 469 |
-
magic = b"BLP1" if im.encoderinfo.get("blp_version") == "BLP1" else b"BLP2"
|
| 470 |
-
fp.write(magic)
|
| 471 |
-
|
| 472 |
-
assert im.palette is not None
|
| 473 |
-
fp.write(struct.pack("<i", 1)) # Uncompressed or DirectX compression
|
| 474 |
-
|
| 475 |
-
alpha_depth = 1 if im.palette.mode == "RGBA" else 0
|
| 476 |
-
if magic == b"BLP1":
|
| 477 |
-
fp.write(struct.pack("<L", alpha_depth))
|
| 478 |
-
else:
|
| 479 |
-
fp.write(struct.pack("<b", Encoding.UNCOMPRESSED))
|
| 480 |
-
fp.write(struct.pack("<b", alpha_depth))
|
| 481 |
-
fp.write(struct.pack("<b", 0)) # alpha encoding
|
| 482 |
-
fp.write(struct.pack("<b", 0)) # mips
|
| 483 |
-
fp.write(struct.pack("<II", *im.size))
|
| 484 |
-
if magic == b"BLP1":
|
| 485 |
-
fp.write(struct.pack("<i", 5))
|
| 486 |
-
fp.write(struct.pack("<i", 0))
|
| 487 |
-
|
| 488 |
-
ImageFile._save(im, fp, [ImageFile._Tile("BLP", (0, 0) + im.size, 0, im.mode)])
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
Image.register_open(BlpImageFile.format, BlpImageFile, _accept)
|
| 492 |
-
Image.register_extension(BlpImageFile.format, ".blp")
|
| 493 |
-
Image.register_decoder("BLP1", BLP1Decoder)
|
| 494 |
-
Image.register_decoder("BLP2", BLP2Decoder)
|
| 495 |
-
|
| 496 |
-
Image.register_save(BlpImageFile.format, _save)
|
| 497 |
-
Image.register_encoder("BLP", BLPEncoder)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/lib/python3.10/site-packages/PIL/BmpImagePlugin.py
DELETED
|
@@ -1,515 +0,0 @@
|
|
| 1 |
-
#
|
| 2 |
-
# The Python Imaging Library.
|
| 3 |
-
# $Id$
|
| 4 |
-
#
|
| 5 |
-
# BMP file handler
|
| 6 |
-
#
|
| 7 |
-
# Windows (and OS/2) native bitmap storage format.
|
| 8 |
-
#
|
| 9 |
-
# history:
|
| 10 |
-
# 1995-09-01 fl Created
|
| 11 |
-
# 1996-04-30 fl Added save
|
| 12 |
-
# 1997-08-27 fl Fixed save of 1-bit images
|
| 13 |
-
# 1998-03-06 fl Load P images as L where possible
|
| 14 |
-
# 1998-07-03 fl Load P images as 1 where possible
|
| 15 |
-
# 1998-12-29 fl Handle small palettes
|
| 16 |
-
# 2002-12-30 fl Fixed load of 1-bit palette images
|
| 17 |
-
# 2003-04-21 fl Fixed load of 1-bit monochrome images
|
| 18 |
-
# 2003-04-23 fl Added limited support for BI_BITFIELDS compression
|
| 19 |
-
#
|
| 20 |
-
# Copyright (c) 1997-2003 by Secret Labs AB
|
| 21 |
-
# Copyright (c) 1995-2003 by Fredrik Lundh
|
| 22 |
-
#
|
| 23 |
-
# See the README file for information on usage and redistribution.
|
| 24 |
-
#
|
| 25 |
-
from __future__ import annotations
|
| 26 |
-
|
| 27 |
-
import os
|
| 28 |
-
from typing import IO, Any
|
| 29 |
-
|
| 30 |
-
from . import Image, ImageFile, ImagePalette
|
| 31 |
-
from ._binary import i16le as i16
|
| 32 |
-
from ._binary import i32le as i32
|
| 33 |
-
from ._binary import o8
|
| 34 |
-
from ._binary import o16le as o16
|
| 35 |
-
from ._binary import o32le as o32
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
-
# --------------------------------------------------------------------
|
| 39 |
-
# Read BMP file
|
| 40 |
-
|
| 41 |
-
BIT2MODE = {
|
| 42 |
-
# bits => mode, rawmode
|
| 43 |
-
1: ("P", "P;1"),
|
| 44 |
-
4: ("P", "P;4"),
|
| 45 |
-
8: ("P", "P"),
|
| 46 |
-
16: ("RGB", "BGR;15"),
|
| 47 |
-
24: ("RGB", "BGR"),
|
| 48 |
-
32: ("RGB", "BGRX"),
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
USE_RAW_ALPHA = False
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
def _accept(prefix: bytes) -> bool:
|
| 55 |
-
return prefix.startswith(b"BM")
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def _dib_accept(prefix: bytes) -> bool:
|
| 59 |
-
return i32(prefix) in [12, 40, 52, 56, 64, 108, 124]
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# =============================================================================
|
| 63 |
-
# Image plugin for the Windows BMP format.
|
| 64 |
-
# =============================================================================
|
| 65 |
-
class BmpImageFile(ImageFile.ImageFile):
|
| 66 |
-
"""Image plugin for the Windows Bitmap format (BMP)"""
|
| 67 |
-
|
| 68 |
-
# ------------------------------------------------------------- Description
|
| 69 |
-
format_description = "Windows Bitmap"
|
| 70 |
-
format = "BMP"
|
| 71 |
-
|
| 72 |
-
# -------------------------------------------------- BMP Compression values
|
| 73 |
-
COMPRESSIONS = {"RAW": 0, "RLE8": 1, "RLE4": 2, "BITFIELDS": 3, "JPEG": 4, "PNG": 5}
|
| 74 |
-
for k, v in COMPRESSIONS.items():
|
| 75 |
-
vars()[k] = v
|
| 76 |
-
|
| 77 |
-
def _bitmap(self, header: int = 0, offset: int = 0) -> None:
|
| 78 |
-
"""Read relevant info about the BMP"""
|
| 79 |
-
read, seek = self.fp.read, self.fp.seek
|
| 80 |
-
if header:
|
| 81 |
-
seek(header)
|
| 82 |
-
# read bmp header size @offset 14 (this is part of the header size)
|
| 83 |
-
file_info: dict[str, bool | int | tuple[int, ...]] = {
|
| 84 |
-
"header_size": i32(read(4)),
|
| 85 |
-
"direction": -1,
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
# -------------------- If requested, read header at a specific position
|
| 89 |
-
# read the rest of the bmp header, without its size
|
| 90 |
-
assert isinstance(file_info["header_size"], int)
|
| 91 |
-
header_data = ImageFile._safe_read(self.fp, file_info["header_size"] - 4)
|
| 92 |
-
|
| 93 |
-
# ------------------------------- Windows Bitmap v2, IBM OS/2 Bitmap v1
|
| 94 |
-
# ----- This format has different offsets because of width/height types
|
| 95 |
-
# 12: BITMAPCOREHEADER/OS21XBITMAPHEADER
|
| 96 |
-
if file_info["header_size"] == 12:
|
| 97 |
-
file_info["width"] = i16(header_data, 0)
|
| 98 |
-
file_info["height"] = i16(header_data, 2)
|
| 99 |
-
file_info["planes"] = i16(header_data, 4)
|
| 100 |
-
file_info["bits"] = i16(header_data, 6)
|
| 101 |
-
file_info["compression"] = self.COMPRESSIONS["RAW"]
|
| 102 |
-
file_info["palette_padding"] = 3
|
| 103 |
-
|
| 104 |
-
# --------------------------------------------- Windows Bitmap v3 to v5
|
| 105 |
-
# 40: BITMAPINFOHEADER
|
| 106 |
-
# 52: BITMAPV2HEADER
|
| 107 |
-
# 56: BITMAPV3HEADER
|
| 108 |
-
# 64: BITMAPCOREHEADER2/OS22XBITMAPHEADER
|
| 109 |
-
# 108: BITMAPV4HEADER
|
| 110 |
-
# 124: BITMAPV5HEADER
|
| 111 |
-
elif file_info["header_size"] in (40, 52, 56, 64, 108, 124):
|
| 112 |
-
file_info["y_flip"] = header_data[7] == 0xFF
|
| 113 |
-
file_info["direction"] = 1 if file_info["y_flip"] else -1
|
| 114 |
-
file_info["width"] = i32(header_data, 0)
|
| 115 |
-
file_info["height"] = (
|
| 116 |
-
i32(header_data, 4)
|
| 117 |
-
if not file_info["y_flip"]
|
| 118 |
-
else 2**32 - i32(header_data, 4)
|
| 119 |
-
)
|
| 120 |
-
file_info["planes"] = i16(header_data, 8)
|
| 121 |
-
file_info["bits"] = i16(header_data, 10)
|
| 122 |
-
file_info["compression"] = i32(header_data, 12)
|
| 123 |
-
# byte size of pixel data
|
| 124 |
-
file_info["data_size"] = i32(header_data, 16)
|
| 125 |
-
file_info["pixels_per_meter"] = (
|
| 126 |
-
i32(header_data, 20),
|
| 127 |
-
i32(header_data, 24),
|
| 128 |
-
)
|
| 129 |
-
file_info["colors"] = i32(header_data, 28)
|
| 130 |
-
file_info["palette_padding"] = 4
|
| 131 |
-
assert isinstance(file_info["pixels_per_meter"], tuple)
|
| 132 |
-
self.info["dpi"] = tuple(x / 39.3701 for x in file_info["pixels_per_meter"])
|
| 133 |
-
if file_info["compression"] == self.COMPRESSIONS["BITFIELDS"]:
|
| 134 |
-
masks = ["r_mask", "g_mask", "b_mask"]
|
| 135 |
-
if len(header_data) >= 48:
|
| 136 |
-
if len(header_data) >= 52:
|
| 137 |
-
masks.append("a_mask")
|
| 138 |
-
else:
|
| 139 |
-
file_info["a_mask"] = 0x0
|
| 140 |
-
for idx, mask in enumerate(masks):
|
| 141 |
-
file_info[mask] = i32(header_data, 36 + idx * 4)
|
| 142 |
-
else:
|
| 143 |
-
# 40 byte headers only have the three components in the
|
| 144 |
-
# bitfields masks, ref:
|
| 145 |
-
# https://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx
|
| 146 |
-
# See also
|
| 147 |
-
# https://github.com/python-pillow/Pillow/issues/1293
|
| 148 |
-
# There is a 4th component in the RGBQuad, in the alpha
|
| 149 |
-
# location, but it is listed as a reserved component,
|
| 150 |
-
# and it is not generally an alpha channel
|
| 151 |
-
file_info["a_mask"] = 0x0
|
| 152 |
-
for mask in masks:
|
| 153 |
-
file_info[mask] = i32(read(4))
|
| 154 |
-
assert isinstance(file_info["r_mask"], int)
|
| 155 |
-
assert isinstance(file_info["g_mask"], int)
|
| 156 |
-
assert isinstance(file_info["b_mask"], int)
|
| 157 |
-
assert isinstance(file_info["a_mask"], int)
|
| 158 |
-
file_info["rgb_mask"] = (
|
| 159 |
-
file_info["r_mask"],
|
| 160 |
-
file_info["g_mask"],
|
| 161 |
-
file_info["b_mask"],
|
| 162 |
-
)
|
| 163 |
-
file_info["rgba_mask"] = (
|
| 164 |
-
file_info["r_mask"],
|
| 165 |
-
file_info["g_mask"],
|
| 166 |
-
file_info["b_mask"],
|
| 167 |
-
file_info["a_mask"],
|
| 168 |
-
)
|
| 169 |
-
else:
|
| 170 |
-
msg = f"Unsupported BMP header type ({file_info['header_size']})"
|
| 171 |
-
raise OSError(msg)
|
| 172 |
-
|
| 173 |
-
# ------------------ Special case : header is reported 40, which
|
| 174 |
-
# ---------------------- is shorter than real size for bpp >= 16
|
| 175 |
-
assert isinstance(file_info["width"], int)
|
| 176 |
-
assert isinstance(file_info["height"], int)
|
| 177 |
-
self._size = file_info["width"], file_info["height"]
|
| 178 |
-
|
| 179 |
-
# ------- If color count was not found in the header, compute from bits
|
| 180 |
-
assert isinstance(file_info["bits"], int)
|
| 181 |
-
file_info["colors"] = (
|
| 182 |
-
file_info["colors"]
|
| 183 |
-
if file_info.get("colors", 0)
|
| 184 |
-
else (1 << file_info["bits"])
|
| 185 |
-
)
|
| 186 |
-
assert isinstance(file_info["colors"], int)
|
| 187 |
-
if offset == 14 + file_info["header_size"] and file_info["bits"] <= 8:
|
| 188 |
-
offset += 4 * file_info["colors"]
|
| 189 |
-
|
| 190 |
-
# ---------------------- Check bit depth for unusual unsupported values
|
| 191 |
-
self._mode, raw_mode = BIT2MODE.get(file_info["bits"], ("", ""))
|
| 192 |
-
if not self.mode:
|
| 193 |
-
msg = f"Unsupported BMP pixel depth ({file_info['bits']})"
|
| 194 |
-
raise OSError(msg)
|
| 195 |
-
|
| 196 |
-
# ---------------- Process BMP with Bitfields compression (not palette)
|
| 197 |
-
decoder_name = "raw"
|
| 198 |
-
if file_info["compression"] == self.COMPRESSIONS["BITFIELDS"]:
|
| 199 |
-
SUPPORTED: dict[int, list[tuple[int, ...]]] = {
|
| 200 |
-
32: [
|
| 201 |
-
(0xFF0000, 0xFF00, 0xFF, 0x0),
|
| 202 |
-
(0xFF000000, 0xFF0000, 0xFF00, 0x0),
|
| 203 |
-
(0xFF000000, 0xFF00, 0xFF, 0x0),
|
| 204 |
-
(0xFF000000, 0xFF0000, 0xFF00, 0xFF),
|
| 205 |
-
(0xFF, 0xFF00, 0xFF0000, 0xFF000000),
|
| 206 |
-
(0xFF0000, 0xFF00, 0xFF, 0xFF000000),
|
| 207 |
-
(0xFF000000, 0xFF00, 0xFF, 0xFF0000),
|
| 208 |
-
(0x0, 0x0, 0x0, 0x0),
|
| 209 |
-
],
|
| 210 |
-
24: [(0xFF0000, 0xFF00, 0xFF)],
|
| 211 |
-
16: [(0xF800, 0x7E0, 0x1F), (0x7C00, 0x3E0, 0x1F)],
|
| 212 |
-
}
|
| 213 |
-
MASK_MODES = {
|
| 214 |
-
(32, (0xFF0000, 0xFF00, 0xFF, 0x0)): "BGRX",
|
| 215 |
-
(32, (0xFF000000, 0xFF0000, 0xFF00, 0x0)): "XBGR",
|
| 216 |
-
(32, (0xFF000000, 0xFF00, 0xFF, 0x0)): "BGXR",
|
| 217 |
-
(32, (0xFF000000, 0xFF0000, 0xFF00, 0xFF)): "ABGR",
|
| 218 |
-
(32, (0xFF, 0xFF00, 0xFF0000, 0xFF000000)): "RGBA",
|
| 219 |
-
(32, (0xFF0000, 0xFF00, 0xFF, 0xFF000000)): "BGRA",
|
| 220 |
-
(32, (0xFF000000, 0xFF00, 0xFF, 0xFF0000)): "BGAR",
|
| 221 |
-
(32, (0x0, 0x0, 0x0, 0x0)): "BGRA",
|
| 222 |
-
(24, (0xFF0000, 0xFF00, 0xFF)): "BGR",
|
| 223 |
-
(16, (0xF800, 0x7E0, 0x1F)): "BGR;16",
|
| 224 |
-
(16, (0x7C00, 0x3E0, 0x1F)): "BGR;15",
|
| 225 |
-
}
|
| 226 |
-
if file_info["bits"] in SUPPORTED:
|
| 227 |
-
if (
|
| 228 |
-
file_info["bits"] == 32
|
| 229 |
-
and file_info["rgba_mask"] in SUPPORTED[file_info["bits"]]
|
| 230 |
-
):
|
| 231 |
-
assert isinstance(file_info["rgba_mask"], tuple)
|
| 232 |
-
raw_mode = MASK_MODES[(file_info["bits"], file_info["rgba_mask"])]
|
| 233 |
-
self._mode = "RGBA" if "A" in raw_mode else self.mode
|
| 234 |
-
elif (
|
| 235 |
-
file_info["bits"] in (24, 16)
|
| 236 |
-
and file_info["rgb_mask"] in SUPPORTED[file_info["bits"]]
|
| 237 |
-
):
|
| 238 |
-
assert isinstance(file_info["rgb_mask"], tuple)
|
| 239 |
-
raw_mode = MASK_MODES[(file_info["bits"], file_info["rgb_mask"])]
|
| 240 |
-
else:
|
| 241 |
-
msg = "Unsupported BMP bitfields layout"
|
| 242 |
-
raise OSError(msg)
|
| 243 |
-
else:
|
| 244 |
-
msg = "Unsupported BMP bitfields layout"
|
| 245 |
-
raise OSError(msg)
|
| 246 |
-
elif file_info["compression"] == self.COMPRESSIONS["RAW"]:
|
| 247 |
-
if file_info["bits"] == 32 and (
|
| 248 |
-
header == 22 or USE_RAW_ALPHA # 32-bit .cur offset
|
| 249 |
-
):
|
| 250 |
-
raw_mode, self._mode = "BGRA", "RGBA"
|
| 251 |
-
elif file_info["compression"] in (
|
| 252 |
-
self.COMPRESSIONS["RLE8"],
|
| 253 |
-
self.COMPRESSIONS["RLE4"],
|
| 254 |
-
):
|
| 255 |
-
decoder_name = "bmp_rle"
|
| 256 |
-
else:
|
| 257 |
-
msg = f"Unsupported BMP compression ({file_info['compression']})"
|
| 258 |
-
raise OSError(msg)
|
| 259 |
-
|
| 260 |
-
# --------------- Once the header is processed, process the palette/LUT
|
| 261 |
-
if self.mode == "P": # Paletted for 1, 4 and 8 bit images
|
| 262 |
-
# ---------------------------------------------------- 1-bit images
|
| 263 |
-
if not (0 < file_info["colors"] <= 65536):
|
| 264 |
-
msg = f"Unsupported BMP Palette size ({file_info['colors']})"
|
| 265 |
-
raise OSError(msg)
|
| 266 |
-
else:
|
| 267 |
-
assert isinstance(file_info["palette_padding"], int)
|
| 268 |
-
padding = file_info["palette_padding"]
|
| 269 |
-
palette = read(padding * file_info["colors"])
|
| 270 |
-
grayscale = True
|
| 271 |
-
indices = (
|
| 272 |
-
(0, 255)
|
| 273 |
-
if file_info["colors"] == 2
|
| 274 |
-
else list(range(file_info["colors"]))
|
| 275 |
-
)
|
| 276 |
-
|
| 277 |
-
# ----------------- Check if grayscale and ignore palette if so
|
| 278 |
-
for ind, val in enumerate(indices):
|
| 279 |
-
rgb = palette[ind * padding : ind * padding + 3]
|
| 280 |
-
if rgb != o8(val) * 3:
|
| 281 |
-
grayscale = False
|
| 282 |
-
|
| 283 |
-
# ------- If all colors are gray, white or black, ditch palette
|
| 284 |
-
if grayscale:
|
| 285 |
-
self._mode = "1" if file_info["colors"] == 2 else "L"
|
| 286 |
-
raw_mode = self.mode
|
| 287 |
-
else:
|
| 288 |
-
self._mode = "P"
|
| 289 |
-
self.palette = ImagePalette.raw(
|
| 290 |
-
"BGRX" if padding == 4 else "BGR", palette
|
| 291 |
-
)
|
| 292 |
-
|
| 293 |
-
# ---------------------------- Finally set the tile data for the plugin
|
| 294 |
-
self.info["compression"] = file_info["compression"]
|
| 295 |
-
args: list[Any] = [raw_mode]
|
| 296 |
-
if decoder_name == "bmp_rle":
|
| 297 |
-
args.append(file_info["compression"] == self.COMPRESSIONS["RLE4"])
|
| 298 |
-
else:
|
| 299 |
-
assert isinstance(file_info["width"], int)
|
| 300 |
-
args.append(((file_info["width"] * file_info["bits"] + 31) >> 3) & (~3))
|
| 301 |
-
args.append(file_info["direction"])
|
| 302 |
-
self.tile = [
|
| 303 |
-
ImageFile._Tile(
|
| 304 |
-
decoder_name,
|
| 305 |
-
(0, 0, file_info["width"], file_info["height"]),
|
| 306 |
-
offset or self.fp.tell(),
|
| 307 |
-
tuple(args),
|
| 308 |
-
)
|
| 309 |
-
]
|
| 310 |
-
|
| 311 |
-
def _open(self) -> None:
|
| 312 |
-
"""Open file, check magic number and read header"""
|
| 313 |
-
# read 14 bytes: magic number, filesize, reserved, header final offset
|
| 314 |
-
head_data = self.fp.read(14)
|
| 315 |
-
# choke if the file does not have the required magic bytes
|
| 316 |
-
if not _accept(head_data):
|
| 317 |
-
msg = "Not a BMP file"
|
| 318 |
-
raise SyntaxError(msg)
|
| 319 |
-
# read the start position of the BMP image data (u32)
|
| 320 |
-
offset = i32(head_data, 10)
|
| 321 |
-
# load bitmap information (offset=raster info)
|
| 322 |
-
self._bitmap(offset=offset)
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
class BmpRleDecoder(ImageFile.PyDecoder):
|
| 326 |
-
_pulls_fd = True
|
| 327 |
-
|
| 328 |
-
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
|
| 329 |
-
assert self.fd is not None
|
| 330 |
-
rle4 = self.args[1]
|
| 331 |
-
data = bytearray()
|
| 332 |
-
x = 0
|
| 333 |
-
dest_length = self.state.xsize * self.state.ysize
|
| 334 |
-
while len(data) < dest_length:
|
| 335 |
-
pixels = self.fd.read(1)
|
| 336 |
-
byte = self.fd.read(1)
|
| 337 |
-
if not pixels or not byte:
|
| 338 |
-
break
|
| 339 |
-
num_pixels = pixels[0]
|
| 340 |
-
if num_pixels:
|
| 341 |
-
# encoded mode
|
| 342 |
-
if x + num_pixels > self.state.xsize:
|
| 343 |
-
# Too much data for row
|
| 344 |
-
num_pixels = max(0, self.state.xsize - x)
|
| 345 |
-
if rle4:
|
| 346 |
-
first_pixel = o8(byte[0] >> 4)
|
| 347 |
-
second_pixel = o8(byte[0] & 0x0F)
|
| 348 |
-
for index in range(num_pixels):
|
| 349 |
-
if index % 2 == 0:
|
| 350 |
-
data += first_pixel
|
| 351 |
-
else:
|
| 352 |
-
data += second_pixel
|
| 353 |
-
else:
|
| 354 |
-
data += byte * num_pixels
|
| 355 |
-
x += num_pixels
|
| 356 |
-
else:
|
| 357 |
-
if byte[0] == 0:
|
| 358 |
-
# end of line
|
| 359 |
-
while len(data) % self.state.xsize != 0:
|
| 360 |
-
data += b"\x00"
|
| 361 |
-
x = 0
|
| 362 |
-
elif byte[0] == 1:
|
| 363 |
-
# end of bitmap
|
| 364 |
-
break
|
| 365 |
-
elif byte[0] == 2:
|
| 366 |
-
# delta
|
| 367 |
-
bytes_read = self.fd.read(2)
|
| 368 |
-
if len(bytes_read) < 2:
|
| 369 |
-
break
|
| 370 |
-
right, up = self.fd.read(2)
|
| 371 |
-
data += b"\x00" * (right + up * self.state.xsize)
|
| 372 |
-
x = len(data) % self.state.xsize
|
| 373 |
-
else:
|
| 374 |
-
# absolute mode
|
| 375 |
-
if rle4:
|
| 376 |
-
# 2 pixels per byte
|
| 377 |
-
byte_count = byte[0] // 2
|
| 378 |
-
bytes_read = self.fd.read(byte_count)
|
| 379 |
-
for byte_read in bytes_read:
|
| 380 |
-
data += o8(byte_read >> 4)
|
| 381 |
-
data += o8(byte_read & 0x0F)
|
| 382 |
-
else:
|
| 383 |
-
byte_count = byte[0]
|
| 384 |
-
bytes_read = self.fd.read(byte_count)
|
| 385 |
-
data += bytes_read
|
| 386 |
-
if len(bytes_read) < byte_count:
|
| 387 |
-
break
|
| 388 |
-
x += byte[0]
|
| 389 |
-
|
| 390 |
-
# align to 16-bit word boundary
|
| 391 |
-
if self.fd.tell() % 2 != 0:
|
| 392 |
-
self.fd.seek(1, os.SEEK_CUR)
|
| 393 |
-
rawmode = "L" if self.mode == "L" else "P"
|
| 394 |
-
self.set_as_raw(bytes(data), rawmode, (0, self.args[-1]))
|
| 395 |
-
return -1, 0
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
# =============================================================================
|
| 399 |
-
# Image plugin for the DIB format (BMP alias)
|
| 400 |
-
# =============================================================================
|
| 401 |
-
class DibImageFile(BmpImageFile):
|
| 402 |
-
format = "DIB"
|
| 403 |
-
format_description = "Windows Bitmap"
|
| 404 |
-
|
| 405 |
-
def _open(self) -> None:
|
| 406 |
-
self._bitmap()
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
#
|
| 410 |
-
# --------------------------------------------------------------------
|
| 411 |
-
# Write BMP file
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
SAVE = {
|
| 415 |
-
"1": ("1", 1, 2),
|
| 416 |
-
"L": ("L", 8, 256),
|
| 417 |
-
"P": ("P", 8, 256),
|
| 418 |
-
"RGB": ("BGR", 24, 0),
|
| 419 |
-
"RGBA": ("BGRA", 32, 0),
|
| 420 |
-
}
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
def _dib_save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 424 |
-
_save(im, fp, filename, False)
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
def _save(
|
| 428 |
-
im: Image.Image, fp: IO[bytes], filename: str | bytes, bitmap_header: bool = True
|
| 429 |
-
) -> None:
|
| 430 |
-
try:
|
| 431 |
-
rawmode, bits, colors = SAVE[im.mode]
|
| 432 |
-
except KeyError as e:
|
| 433 |
-
msg = f"cannot write mode {im.mode} as BMP"
|
| 434 |
-
raise OSError(msg) from e
|
| 435 |
-
|
| 436 |
-
info = im.encoderinfo
|
| 437 |
-
|
| 438 |
-
dpi = info.get("dpi", (96, 96))
|
| 439 |
-
|
| 440 |
-
# 1 meter == 39.3701 inches
|
| 441 |
-
ppm = tuple(int(x * 39.3701 + 0.5) for x in dpi)
|
| 442 |
-
|
| 443 |
-
stride = ((im.size[0] * bits + 7) // 8 + 3) & (~3)
|
| 444 |
-
header = 40 # or 64 for OS/2 version 2
|
| 445 |
-
image = stride * im.size[1]
|
| 446 |
-
|
| 447 |
-
if im.mode == "1":
|
| 448 |
-
palette = b"".join(o8(i) * 3 + b"\x00" for i in (0, 255))
|
| 449 |
-
elif im.mode == "L":
|
| 450 |
-
palette = b"".join(o8(i) * 3 + b"\x00" for i in range(256))
|
| 451 |
-
elif im.mode == "P":
|
| 452 |
-
palette = im.im.getpalette("RGB", "BGRX")
|
| 453 |
-
colors = len(palette) // 4
|
| 454 |
-
else:
|
| 455 |
-
palette = None
|
| 456 |
-
|
| 457 |
-
# bitmap header
|
| 458 |
-
if bitmap_header:
|
| 459 |
-
offset = 14 + header + colors * 4
|
| 460 |
-
file_size = offset + image
|
| 461 |
-
if file_size > 2**32 - 1:
|
| 462 |
-
msg = "File size is too large for the BMP format"
|
| 463 |
-
raise ValueError(msg)
|
| 464 |
-
fp.write(
|
| 465 |
-
b"BM" # file type (magic)
|
| 466 |
-
+ o32(file_size) # file size
|
| 467 |
-
+ o32(0) # reserved
|
| 468 |
-
+ o32(offset) # image data offset
|
| 469 |
-
)
|
| 470 |
-
|
| 471 |
-
# bitmap info header
|
| 472 |
-
fp.write(
|
| 473 |
-
o32(header) # info header size
|
| 474 |
-
+ o32(im.size[0]) # width
|
| 475 |
-
+ o32(im.size[1]) # height
|
| 476 |
-
+ o16(1) # planes
|
| 477 |
-
+ o16(bits) # depth
|
| 478 |
-
+ o32(0) # compression (0=uncompressed)
|
| 479 |
-
+ o32(image) # size of bitmap
|
| 480 |
-
+ o32(ppm[0]) # resolution
|
| 481 |
-
+ o32(ppm[1]) # resolution
|
| 482 |
-
+ o32(colors) # colors used
|
| 483 |
-
+ o32(colors) # colors important
|
| 484 |
-
)
|
| 485 |
-
|
| 486 |
-
fp.write(b"\0" * (header - 40)) # padding (for OS/2 format)
|
| 487 |
-
|
| 488 |
-
if palette:
|
| 489 |
-
fp.write(palette)
|
| 490 |
-
|
| 491 |
-
ImageFile._save(
|
| 492 |
-
im, fp, [ImageFile._Tile("raw", (0, 0) + im.size, 0, (rawmode, stride, -1))]
|
| 493 |
-
)
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
#
|
| 497 |
-
# --------------------------------------------------------------------
|
| 498 |
-
# Registry
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
Image.register_open(BmpImageFile.format, BmpImageFile, _accept)
|
| 502 |
-
Image.register_save(BmpImageFile.format, _save)
|
| 503 |
-
|
| 504 |
-
Image.register_extension(BmpImageFile.format, ".bmp")
|
| 505 |
-
|
| 506 |
-
Image.register_mime(BmpImageFile.format, "image/bmp")
|
| 507 |
-
|
| 508 |
-
Image.register_decoder("bmp_rle", BmpRleDecoder)
|
| 509 |
-
|
| 510 |
-
Image.register_open(DibImageFile.format, DibImageFile, _dib_accept)
|
| 511 |
-
Image.register_save(DibImageFile.format, _dib_save)
|
| 512 |
-
|
| 513 |
-
Image.register_extension(DibImageFile.format, ".dib")
|
| 514 |
-
|
| 515 |
-
Image.register_mime(DibImageFile.format, "image/bmp")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/lib/python3.10/site-packages/PIL/BufrStubImagePlugin.py
DELETED
|
@@ -1,75 +0,0 @@
|
|
| 1 |
-
#
|
| 2 |
-
# The Python Imaging Library
|
| 3 |
-
# $Id$
|
| 4 |
-
#
|
| 5 |
-
# BUFR stub adapter
|
| 6 |
-
#
|
| 7 |
-
# Copyright (c) 1996-2003 by Fredrik Lundh
|
| 8 |
-
#
|
| 9 |
-
# See the README file for information on usage and redistribution.
|
| 10 |
-
#
|
| 11 |
-
from __future__ import annotations
|
| 12 |
-
|
| 13 |
-
import os
|
| 14 |
-
from typing import IO
|
| 15 |
-
|
| 16 |
-
from . import Image, ImageFile
|
| 17 |
-
|
| 18 |
-
_handler = None
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def register_handler(handler: ImageFile.StubHandler | None) -> None:
|
| 22 |
-
"""
|
| 23 |
-
Install application-specific BUFR image handler.
|
| 24 |
-
|
| 25 |
-
:param handler: Handler object.
|
| 26 |
-
"""
|
| 27 |
-
global _handler
|
| 28 |
-
_handler = handler
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# --------------------------------------------------------------------
|
| 32 |
-
# Image adapter
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def _accept(prefix: bytes) -> bool:
|
| 36 |
-
return prefix.startswith((b"BUFR", b"ZCZC"))
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
class BufrStubImageFile(ImageFile.StubImageFile):
|
| 40 |
-
format = "BUFR"
|
| 41 |
-
format_description = "BUFR"
|
| 42 |
-
|
| 43 |
-
def _open(self) -> None:
|
| 44 |
-
if not _accept(self.fp.read(4)):
|
| 45 |
-
msg = "Not a BUFR file"
|
| 46 |
-
raise SyntaxError(msg)
|
| 47 |
-
|
| 48 |
-
self.fp.seek(-4, os.SEEK_CUR)
|
| 49 |
-
|
| 50 |
-
# make something up
|
| 51 |
-
self._mode = "F"
|
| 52 |
-
self._size = 1, 1
|
| 53 |
-
|
| 54 |
-
loader = self._load()
|
| 55 |
-
if loader:
|
| 56 |
-
loader.open(self)
|
| 57 |
-
|
| 58 |
-
def _load(self) -> ImageFile.StubHandler | None:
|
| 59 |
-
return _handler
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 63 |
-
if _handler is None or not hasattr(_handler, "save"):
|
| 64 |
-
msg = "BUFR save handler not installed"
|
| 65 |
-
raise OSError(msg)
|
| 66 |
-
_handler.save(im, fp, filename)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
# --------------------------------------------------------------------
|
| 70 |
-
# Registry
|
| 71 |
-
|
| 72 |
-
Image.register_open(BufrStubImageFile.format, BufrStubImageFile, _accept)
|
| 73 |
-
Image.register_save(BufrStubImageFile.format, _save)
|
| 74 |
-
|
| 75 |
-
Image.register_extension(BufrStubImageFile.format, ".bufr")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/lib/python3.10/site-packages/PIL/ContainerIO.py
DELETED
|
@@ -1,173 +0,0 @@
|
|
| 1 |
-
#
|
| 2 |
-
# The Python Imaging Library.
|
| 3 |
-
# $Id$
|
| 4 |
-
#
|
| 5 |
-
# a class to read from a container file
|
| 6 |
-
#
|
| 7 |
-
# History:
|
| 8 |
-
# 1995-06-18 fl Created
|
| 9 |
-
# 1995-09-07 fl Added readline(), readlines()
|
| 10 |
-
#
|
| 11 |
-
# Copyright (c) 1997-2001 by Secret Labs AB
|
| 12 |
-
# Copyright (c) 1995 by Fredrik Lundh
|
| 13 |
-
#
|
| 14 |
-
# See the README file for information on usage and redistribution.
|
| 15 |
-
#
|
| 16 |
-
from __future__ import annotations
|
| 17 |
-
|
| 18 |
-
import io
|
| 19 |
-
from collections.abc import Iterable
|
| 20 |
-
from typing import IO, AnyStr, NoReturn
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
class ContainerIO(IO[AnyStr]):
|
| 24 |
-
"""
|
| 25 |
-
A file object that provides read access to a part of an existing
|
| 26 |
-
file (for example a TAR file).
|
| 27 |
-
"""
|
| 28 |
-
|
| 29 |
-
def __init__(self, file: IO[AnyStr], offset: int, length: int) -> None:
|
| 30 |
-
"""
|
| 31 |
-
Create file object.
|
| 32 |
-
|
| 33 |
-
:param file: Existing file.
|
| 34 |
-
:param offset: Start of region, in bytes.
|
| 35 |
-
:param length: Size of region, in bytes.
|
| 36 |
-
"""
|
| 37 |
-
self.fh: IO[AnyStr] = file
|
| 38 |
-
self.pos = 0
|
| 39 |
-
self.offset = offset
|
| 40 |
-
self.length = length
|
| 41 |
-
self.fh.seek(offset)
|
| 42 |
-
|
| 43 |
-
##
|
| 44 |
-
# Always false.
|
| 45 |
-
|
| 46 |
-
def isatty(self) -> bool:
|
| 47 |
-
return False
|
| 48 |
-
|
| 49 |
-
def seekable(self) -> bool:
|
| 50 |
-
return True
|
| 51 |
-
|
| 52 |
-
def seek(self, offset: int, mode: int = io.SEEK_SET) -> int:
|
| 53 |
-
"""
|
| 54 |
-
Move file pointer.
|
| 55 |
-
|
| 56 |
-
:param offset: Offset in bytes.
|
| 57 |
-
:param mode: Starting position. Use 0 for beginning of region, 1
|
| 58 |
-
for current offset, and 2 for end of region. You cannot move
|
| 59 |
-
the pointer outside the defined region.
|
| 60 |
-
:returns: Offset from start of region, in bytes.
|
| 61 |
-
"""
|
| 62 |
-
if mode == 1:
|
| 63 |
-
self.pos = self.pos + offset
|
| 64 |
-
elif mode == 2:
|
| 65 |
-
self.pos = self.length + offset
|
| 66 |
-
else:
|
| 67 |
-
self.pos = offset
|
| 68 |
-
# clamp
|
| 69 |
-
self.pos = max(0, min(self.pos, self.length))
|
| 70 |
-
self.fh.seek(self.offset + self.pos)
|
| 71 |
-
return self.pos
|
| 72 |
-
|
| 73 |
-
def tell(self) -> int:
|
| 74 |
-
"""
|
| 75 |
-
Get current file pointer.
|
| 76 |
-
|
| 77 |
-
:returns: Offset from start of region, in bytes.
|
| 78 |
-
"""
|
| 79 |
-
return self.pos
|
| 80 |
-
|
| 81 |
-
def readable(self) -> bool:
|
| 82 |
-
return True
|
| 83 |
-
|
| 84 |
-
def read(self, n: int = -1) -> AnyStr:
|
| 85 |
-
"""
|
| 86 |
-
Read data.
|
| 87 |
-
|
| 88 |
-
:param n: Number of bytes to read. If omitted, zero or negative,
|
| 89 |
-
read until end of region.
|
| 90 |
-
:returns: An 8-bit string.
|
| 91 |
-
"""
|
| 92 |
-
if n > 0:
|
| 93 |
-
n = min(n, self.length - self.pos)
|
| 94 |
-
else:
|
| 95 |
-
n = self.length - self.pos
|
| 96 |
-
if n <= 0: # EOF
|
| 97 |
-
return b"" if "b" in self.fh.mode else "" # type: ignore[return-value]
|
| 98 |
-
self.pos = self.pos + n
|
| 99 |
-
return self.fh.read(n)
|
| 100 |
-
|
| 101 |
-
def readline(self, n: int = -1) -> AnyStr:
|
| 102 |
-
"""
|
| 103 |
-
Read a line of text.
|
| 104 |
-
|
| 105 |
-
:param n: Number of bytes to read. If omitted, zero or negative,
|
| 106 |
-
read until end of line.
|
| 107 |
-
:returns: An 8-bit string.
|
| 108 |
-
"""
|
| 109 |
-
s: AnyStr = b"" if "b" in self.fh.mode else "" # type: ignore[assignment]
|
| 110 |
-
newline_character = b"\n" if "b" in self.fh.mode else "\n"
|
| 111 |
-
while True:
|
| 112 |
-
c = self.read(1)
|
| 113 |
-
if not c:
|
| 114 |
-
break
|
| 115 |
-
s = s + c
|
| 116 |
-
if c == newline_character or len(s) == n:
|
| 117 |
-
break
|
| 118 |
-
return s
|
| 119 |
-
|
| 120 |
-
def readlines(self, n: int | None = -1) -> list[AnyStr]:
|
| 121 |
-
"""
|
| 122 |
-
Read multiple lines of text.
|
| 123 |
-
|
| 124 |
-
:param n: Number of lines to read. If omitted, zero, negative or None,
|
| 125 |
-
read until end of region.
|
| 126 |
-
:returns: A list of 8-bit strings.
|
| 127 |
-
"""
|
| 128 |
-
lines = []
|
| 129 |
-
while True:
|
| 130 |
-
s = self.readline()
|
| 131 |
-
if not s:
|
| 132 |
-
break
|
| 133 |
-
lines.append(s)
|
| 134 |
-
if len(lines) == n:
|
| 135 |
-
break
|
| 136 |
-
return lines
|
| 137 |
-
|
| 138 |
-
def writable(self) -> bool:
|
| 139 |
-
return False
|
| 140 |
-
|
| 141 |
-
def write(self, b: AnyStr) -> NoReturn:
|
| 142 |
-
raise NotImplementedError()
|
| 143 |
-
|
| 144 |
-
def writelines(self, lines: Iterable[AnyStr]) -> NoReturn:
|
| 145 |
-
raise NotImplementedError()
|
| 146 |
-
|
| 147 |
-
def truncate(self, size: int | None = None) -> int:
|
| 148 |
-
raise NotImplementedError()
|
| 149 |
-
|
| 150 |
-
def __enter__(self) -> ContainerIO[AnyStr]:
|
| 151 |
-
return self
|
| 152 |
-
|
| 153 |
-
def __exit__(self, *args: object) -> None:
|
| 154 |
-
self.close()
|
| 155 |
-
|
| 156 |
-
def __iter__(self) -> ContainerIO[AnyStr]:
|
| 157 |
-
return self
|
| 158 |
-
|
| 159 |
-
def __next__(self) -> AnyStr:
|
| 160 |
-
line = self.readline()
|
| 161 |
-
if not line:
|
| 162 |
-
msg = "end of region"
|
| 163 |
-
raise StopIteration(msg)
|
| 164 |
-
return line
|
| 165 |
-
|
| 166 |
-
def fileno(self) -> int:
|
| 167 |
-
return self.fh.fileno()
|
| 168 |
-
|
| 169 |
-
def flush(self) -> None:
|
| 170 |
-
self.fh.flush()
|
| 171 |
-
|
| 172 |
-
def close(self) -> None:
|
| 173 |
-
self.fh.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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9 |
-
#
|
| 10 |
-
# history:
|
| 11 |
-
# 96-05-27 fl Created
|
| 12 |
-
#
|
| 13 |
-
# Copyright (c) Secret Labs AB 1997.
|
| 14 |
-
# Copyright (c) Fredrik Lundh 1996.
|
| 15 |
-
#
|
| 16 |
-
# See the README file for information on usage and redistribution.
|
| 17 |
-
#
|
| 18 |
-
from __future__ import annotations
|
| 19 |
-
|
| 20 |
-
from . import BmpImagePlugin, Image
|
| 21 |
-
from ._binary import i16le as i16
|
| 22 |
-
from ._binary import i32le as i32
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
# --------------------------------------------------------------------
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def _accept(prefix: bytes) -> bool:
|
| 29 |
-
return prefix.startswith(b"\0\0\2\0")
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
##
|
| 33 |
-
# Image plugin for Windows Cursor files.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
class CurImageFile(BmpImagePlugin.BmpImageFile):
|
| 37 |
-
format = "CUR"
|
| 38 |
-
format_description = "Windows Cursor"
|
| 39 |
-
|
| 40 |
-
def _open(self) -> None:
|
| 41 |
-
assert self.fp is not None
|
| 42 |
-
offset = self.fp.tell()
|
| 43 |
-
|
| 44 |
-
# check magic
|
| 45 |
-
s = self.fp.read(6)
|
| 46 |
-
if not _accept(s):
|
| 47 |
-
msg = "not a CUR file"
|
| 48 |
-
raise SyntaxError(msg)
|
| 49 |
-
|
| 50 |
-
# pick the largest cursor in the file
|
| 51 |
-
m = b""
|
| 52 |
-
for i in range(i16(s, 4)):
|
| 53 |
-
s = self.fp.read(16)
|
| 54 |
-
if not m:
|
| 55 |
-
m = s
|
| 56 |
-
elif s[0] > m[0] and s[1] > m[1]:
|
| 57 |
-
m = s
|
| 58 |
-
if not m:
|
| 59 |
-
msg = "No cursors were found"
|
| 60 |
-
raise TypeError(msg)
|
| 61 |
-
|
| 62 |
-
# load as bitmap
|
| 63 |
-
self._bitmap(i32(m, 12) + offset)
|
| 64 |
-
|
| 65 |
-
# patch up the bitmap height
|
| 66 |
-
self._size = self.size[0], self.size[1] // 2
|
| 67 |
-
self.tile = [self.tile[0]._replace(extents=(0, 0) + self.size)]
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
#
|
| 71 |
-
# --------------------------------------------------------------------
|
| 72 |
-
|
| 73 |
-
Image.register_open(CurImageFile.format, CurImageFile, _accept)
|
| 74 |
-
|
| 75 |
-
Image.register_extension(CurImageFile.format, ".cur")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/lib/python3.10/site-packages/PIL/DcxImagePlugin.py
DELETED
|
@@ -1,83 +0,0 @@
|
|
| 1 |
-
#
|
| 2 |
-
# The Python Imaging Library.
|
| 3 |
-
# $Id$
|
| 4 |
-
#
|
| 5 |
-
# DCX file handling
|
| 6 |
-
#
|
| 7 |
-
# DCX is a container file format defined by Intel, commonly used
|
| 8 |
-
# for fax applications. Each DCX file consists of a directory
|
| 9 |
-
# (a list of file offsets) followed by a set of (usually 1-bit)
|
| 10 |
-
# PCX files.
|
| 11 |
-
#
|
| 12 |
-
# History:
|
| 13 |
-
# 1995-09-09 fl Created
|
| 14 |
-
# 1996-03-20 fl Properly derived from PcxImageFile.
|
| 15 |
-
# 1998-07-15 fl Renamed offset attribute to avoid name clash
|
| 16 |
-
# 2002-07-30 fl Fixed file handling
|
| 17 |
-
#
|
| 18 |
-
# Copyright (c) 1997-98 by Secret Labs AB.
|
| 19 |
-
# Copyright (c) 1995-96 by Fredrik Lundh.
|
| 20 |
-
#
|
| 21 |
-
# See the README file for information on usage and redistribution.
|
| 22 |
-
#
|
| 23 |
-
from __future__ import annotations
|
| 24 |
-
|
| 25 |
-
from . import Image
|
| 26 |
-
from ._binary import i32le as i32
|
| 27 |
-
from ._util import DeferredError
|
| 28 |
-
from .PcxImagePlugin import PcxImageFile
|
| 29 |
-
|
| 30 |
-
MAGIC = 0x3ADE68B1 # QUIZ: what's this value, then?
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def _accept(prefix: bytes) -> bool:
|
| 34 |
-
return len(prefix) >= 4 and i32(prefix) == MAGIC
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
##
|
| 38 |
-
# Image plugin for the Intel DCX format.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
class DcxImageFile(PcxImageFile):
|
| 42 |
-
format = "DCX"
|
| 43 |
-
format_description = "Intel DCX"
|
| 44 |
-
_close_exclusive_fp_after_loading = False
|
| 45 |
-
|
| 46 |
-
def _open(self) -> None:
|
| 47 |
-
# Header
|
| 48 |
-
s = self.fp.read(4)
|
| 49 |
-
if not _accept(s):
|
| 50 |
-
msg = "not a DCX file"
|
| 51 |
-
raise SyntaxError(msg)
|
| 52 |
-
|
| 53 |
-
# Component directory
|
| 54 |
-
self._offset = []
|
| 55 |
-
for i in range(1024):
|
| 56 |
-
offset = i32(self.fp.read(4))
|
| 57 |
-
if not offset:
|
| 58 |
-
break
|
| 59 |
-
self._offset.append(offset)
|
| 60 |
-
|
| 61 |
-
self._fp = self.fp
|
| 62 |
-
self.frame = -1
|
| 63 |
-
self.n_frames = len(self._offset)
|
| 64 |
-
self.is_animated = self.n_frames > 1
|
| 65 |
-
self.seek(0)
|
| 66 |
-
|
| 67 |
-
def seek(self, frame: int) -> None:
|
| 68 |
-
if not self._seek_check(frame):
|
| 69 |
-
return
|
| 70 |
-
if isinstance(self._fp, DeferredError):
|
| 71 |
-
raise self._fp.ex
|
| 72 |
-
self.frame = frame
|
| 73 |
-
self.fp = self._fp
|
| 74 |
-
self.fp.seek(self._offset[frame])
|
| 75 |
-
PcxImageFile._open(self)
|
| 76 |
-
|
| 77 |
-
def tell(self) -> int:
|
| 78 |
-
return self.frame
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
Image.register_open(DcxImageFile.format, DcxImageFile, _accept)
|
| 82 |
-
|
| 83 |
-
Image.register_extension(DcxImageFile.format, ".dcx")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/lib/python3.10/site-packages/PIL/DdsImagePlugin.py
DELETED
|
@@ -1,624 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
A Pillow plugin for .dds files (S3TC-compressed aka DXTC)
|
| 3 |
-
Jerome Leclanche <jerome@leclan.ch>
|
| 4 |
-
|
| 5 |
-
Documentation:
|
| 6 |
-
https://web.archive.org/web/20170802060935/http://oss.sgi.com/projects/ogl-sample/registry/EXT/texture_compression_s3tc.txt
|
| 7 |
-
|
| 8 |
-
The contents of this file are hereby released in the public domain (CC0)
|
| 9 |
-
Full text of the CC0 license:
|
| 10 |
-
https://creativecommons.org/publicdomain/zero/1.0/
|
| 11 |
-
"""
|
| 12 |
-
|
| 13 |
-
from __future__ import annotations
|
| 14 |
-
|
| 15 |
-
import io
|
| 16 |
-
import struct
|
| 17 |
-
import sys
|
| 18 |
-
from enum import IntEnum, IntFlag
|
| 19 |
-
from typing import IO
|
| 20 |
-
|
| 21 |
-
from . import Image, ImageFile, ImagePalette
|
| 22 |
-
from ._binary import i32le as i32
|
| 23 |
-
from ._binary import o8
|
| 24 |
-
from ._binary import o32le as o32
|
| 25 |
-
|
| 26 |
-
# Magic ("DDS ")
|
| 27 |
-
DDS_MAGIC = 0x20534444
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# DDS flags
|
| 31 |
-
class DDSD(IntFlag):
|
| 32 |
-
CAPS = 0x1
|
| 33 |
-
HEIGHT = 0x2
|
| 34 |
-
WIDTH = 0x4
|
| 35 |
-
PITCH = 0x8
|
| 36 |
-
PIXELFORMAT = 0x1000
|
| 37 |
-
MIPMAPCOUNT = 0x20000
|
| 38 |
-
LINEARSIZE = 0x80000
|
| 39 |
-
DEPTH = 0x800000
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# DDS caps
|
| 43 |
-
class DDSCAPS(IntFlag):
|
| 44 |
-
COMPLEX = 0x8
|
| 45 |
-
TEXTURE = 0x1000
|
| 46 |
-
MIPMAP = 0x400000
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
class DDSCAPS2(IntFlag):
|
| 50 |
-
CUBEMAP = 0x200
|
| 51 |
-
CUBEMAP_POSITIVEX = 0x400
|
| 52 |
-
CUBEMAP_NEGATIVEX = 0x800
|
| 53 |
-
CUBEMAP_POSITIVEY = 0x1000
|
| 54 |
-
CUBEMAP_NEGATIVEY = 0x2000
|
| 55 |
-
CUBEMAP_POSITIVEZ = 0x4000
|
| 56 |
-
CUBEMAP_NEGATIVEZ = 0x8000
|
| 57 |
-
VOLUME = 0x200000
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
# Pixel Format
|
| 61 |
-
class DDPF(IntFlag):
|
| 62 |
-
ALPHAPIXELS = 0x1
|
| 63 |
-
ALPHA = 0x2
|
| 64 |
-
FOURCC = 0x4
|
| 65 |
-
PALETTEINDEXED8 = 0x20
|
| 66 |
-
RGB = 0x40
|
| 67 |
-
LUMINANCE = 0x20000
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# dxgiformat.h
|
| 71 |
-
class DXGI_FORMAT(IntEnum):
|
| 72 |
-
UNKNOWN = 0
|
| 73 |
-
R32G32B32A32_TYPELESS = 1
|
| 74 |
-
R32G32B32A32_FLOAT = 2
|
| 75 |
-
R32G32B32A32_UINT = 3
|
| 76 |
-
R32G32B32A32_SINT = 4
|
| 77 |
-
R32G32B32_TYPELESS = 5
|
| 78 |
-
R32G32B32_FLOAT = 6
|
| 79 |
-
R32G32B32_UINT = 7
|
| 80 |
-
R32G32B32_SINT = 8
|
| 81 |
-
R16G16B16A16_TYPELESS = 9
|
| 82 |
-
R16G16B16A16_FLOAT = 10
|
| 83 |
-
R16G16B16A16_UNORM = 11
|
| 84 |
-
R16G16B16A16_UINT = 12
|
| 85 |
-
R16G16B16A16_SNORM = 13
|
| 86 |
-
R16G16B16A16_SINT = 14
|
| 87 |
-
R32G32_TYPELESS = 15
|
| 88 |
-
R32G32_FLOAT = 16
|
| 89 |
-
R32G32_UINT = 17
|
| 90 |
-
R32G32_SINT = 18
|
| 91 |
-
R32G8X24_TYPELESS = 19
|
| 92 |
-
D32_FLOAT_S8X24_UINT = 20
|
| 93 |
-
R32_FLOAT_X8X24_TYPELESS = 21
|
| 94 |
-
X32_TYPELESS_G8X24_UINT = 22
|
| 95 |
-
R10G10B10A2_TYPELESS = 23
|
| 96 |
-
R10G10B10A2_UNORM = 24
|
| 97 |
-
R10G10B10A2_UINT = 25
|
| 98 |
-
R11G11B10_FLOAT = 26
|
| 99 |
-
R8G8B8A8_TYPELESS = 27
|
| 100 |
-
R8G8B8A8_UNORM = 28
|
| 101 |
-
R8G8B8A8_UNORM_SRGB = 29
|
| 102 |
-
R8G8B8A8_UINT = 30
|
| 103 |
-
R8G8B8A8_SNORM = 31
|
| 104 |
-
R8G8B8A8_SINT = 32
|
| 105 |
-
R16G16_TYPELESS = 33
|
| 106 |
-
R16G16_FLOAT = 34
|
| 107 |
-
R16G16_UNORM = 35
|
| 108 |
-
R16G16_UINT = 36
|
| 109 |
-
R16G16_SNORM = 37
|
| 110 |
-
R16G16_SINT = 38
|
| 111 |
-
R32_TYPELESS = 39
|
| 112 |
-
D32_FLOAT = 40
|
| 113 |
-
R32_FLOAT = 41
|
| 114 |
-
R32_UINT = 42
|
| 115 |
-
R32_SINT = 43
|
| 116 |
-
R24G8_TYPELESS = 44
|
| 117 |
-
D24_UNORM_S8_UINT = 45
|
| 118 |
-
R24_UNORM_X8_TYPELESS = 46
|
| 119 |
-
X24_TYPELESS_G8_UINT = 47
|
| 120 |
-
R8G8_TYPELESS = 48
|
| 121 |
-
R8G8_UNORM = 49
|
| 122 |
-
R8G8_UINT = 50
|
| 123 |
-
R8G8_SNORM = 51
|
| 124 |
-
R8G8_SINT = 52
|
| 125 |
-
R16_TYPELESS = 53
|
| 126 |
-
R16_FLOAT = 54
|
| 127 |
-
D16_UNORM = 55
|
| 128 |
-
R16_UNORM = 56
|
| 129 |
-
R16_UINT = 57
|
| 130 |
-
R16_SNORM = 58
|
| 131 |
-
R16_SINT = 59
|
| 132 |
-
R8_TYPELESS = 60
|
| 133 |
-
R8_UNORM = 61
|
| 134 |
-
R8_UINT = 62
|
| 135 |
-
R8_SNORM = 63
|
| 136 |
-
R8_SINT = 64
|
| 137 |
-
A8_UNORM = 65
|
| 138 |
-
R1_UNORM = 66
|
| 139 |
-
R9G9B9E5_SHAREDEXP = 67
|
| 140 |
-
R8G8_B8G8_UNORM = 68
|
| 141 |
-
G8R8_G8B8_UNORM = 69
|
| 142 |
-
BC1_TYPELESS = 70
|
| 143 |
-
BC1_UNORM = 71
|
| 144 |
-
BC1_UNORM_SRGB = 72
|
| 145 |
-
BC2_TYPELESS = 73
|
| 146 |
-
BC2_UNORM = 74
|
| 147 |
-
BC2_UNORM_SRGB = 75
|
| 148 |
-
BC3_TYPELESS = 76
|
| 149 |
-
BC3_UNORM = 77
|
| 150 |
-
BC3_UNORM_SRGB = 78
|
| 151 |
-
BC4_TYPELESS = 79
|
| 152 |
-
BC4_UNORM = 80
|
| 153 |
-
BC4_SNORM = 81
|
| 154 |
-
BC5_TYPELESS = 82
|
| 155 |
-
BC5_UNORM = 83
|
| 156 |
-
BC5_SNORM = 84
|
| 157 |
-
B5G6R5_UNORM = 85
|
| 158 |
-
B5G5R5A1_UNORM = 86
|
| 159 |
-
B8G8R8A8_UNORM = 87
|
| 160 |
-
B8G8R8X8_UNORM = 88
|
| 161 |
-
R10G10B10_XR_BIAS_A2_UNORM = 89
|
| 162 |
-
B8G8R8A8_TYPELESS = 90
|
| 163 |
-
B8G8R8A8_UNORM_SRGB = 91
|
| 164 |
-
B8G8R8X8_TYPELESS = 92
|
| 165 |
-
B8G8R8X8_UNORM_SRGB = 93
|
| 166 |
-
BC6H_TYPELESS = 94
|
| 167 |
-
BC6H_UF16 = 95
|
| 168 |
-
BC6H_SF16 = 96
|
| 169 |
-
BC7_TYPELESS = 97
|
| 170 |
-
BC7_UNORM = 98
|
| 171 |
-
BC7_UNORM_SRGB = 99
|
| 172 |
-
AYUV = 100
|
| 173 |
-
Y410 = 101
|
| 174 |
-
Y416 = 102
|
| 175 |
-
NV12 = 103
|
| 176 |
-
P010 = 104
|
| 177 |
-
P016 = 105
|
| 178 |
-
OPAQUE_420 = 106
|
| 179 |
-
YUY2 = 107
|
| 180 |
-
Y210 = 108
|
| 181 |
-
Y216 = 109
|
| 182 |
-
NV11 = 110
|
| 183 |
-
AI44 = 111
|
| 184 |
-
IA44 = 112
|
| 185 |
-
P8 = 113
|
| 186 |
-
A8P8 = 114
|
| 187 |
-
B4G4R4A4_UNORM = 115
|
| 188 |
-
P208 = 130
|
| 189 |
-
V208 = 131
|
| 190 |
-
V408 = 132
|
| 191 |
-
SAMPLER_FEEDBACK_MIN_MIP_OPAQUE = 189
|
| 192 |
-
SAMPLER_FEEDBACK_MIP_REGION_USED_OPAQUE = 190
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
class D3DFMT(IntEnum):
|
| 196 |
-
UNKNOWN = 0
|
| 197 |
-
R8G8B8 = 20
|
| 198 |
-
A8R8G8B8 = 21
|
| 199 |
-
X8R8G8B8 = 22
|
| 200 |
-
R5G6B5 = 23
|
| 201 |
-
X1R5G5B5 = 24
|
| 202 |
-
A1R5G5B5 = 25
|
| 203 |
-
A4R4G4B4 = 26
|
| 204 |
-
R3G3B2 = 27
|
| 205 |
-
A8 = 28
|
| 206 |
-
A8R3G3B2 = 29
|
| 207 |
-
X4R4G4B4 = 30
|
| 208 |
-
A2B10G10R10 = 31
|
| 209 |
-
A8B8G8R8 = 32
|
| 210 |
-
X8B8G8R8 = 33
|
| 211 |
-
G16R16 = 34
|
| 212 |
-
A2R10G10B10 = 35
|
| 213 |
-
A16B16G16R16 = 36
|
| 214 |
-
A8P8 = 40
|
| 215 |
-
P8 = 41
|
| 216 |
-
L8 = 50
|
| 217 |
-
A8L8 = 51
|
| 218 |
-
A4L4 = 52
|
| 219 |
-
V8U8 = 60
|
| 220 |
-
L6V5U5 = 61
|
| 221 |
-
X8L8V8U8 = 62
|
| 222 |
-
Q8W8V8U8 = 63
|
| 223 |
-
V16U16 = 64
|
| 224 |
-
A2W10V10U10 = 67
|
| 225 |
-
D16_LOCKABLE = 70
|
| 226 |
-
D32 = 71
|
| 227 |
-
D15S1 = 73
|
| 228 |
-
D24S8 = 75
|
| 229 |
-
D24X8 = 77
|
| 230 |
-
D24X4S4 = 79
|
| 231 |
-
D16 = 80
|
| 232 |
-
D32F_LOCKABLE = 82
|
| 233 |
-
D24FS8 = 83
|
| 234 |
-
D32_LOCKABLE = 84
|
| 235 |
-
S8_LOCKABLE = 85
|
| 236 |
-
L16 = 81
|
| 237 |
-
VERTEXDATA = 100
|
| 238 |
-
INDEX16 = 101
|
| 239 |
-
INDEX32 = 102
|
| 240 |
-
Q16W16V16U16 = 110
|
| 241 |
-
R16F = 111
|
| 242 |
-
G16R16F = 112
|
| 243 |
-
A16B16G16R16F = 113
|
| 244 |
-
R32F = 114
|
| 245 |
-
G32R32F = 115
|
| 246 |
-
A32B32G32R32F = 116
|
| 247 |
-
CxV8U8 = 117
|
| 248 |
-
A1 = 118
|
| 249 |
-
A2B10G10R10_XR_BIAS = 119
|
| 250 |
-
BINARYBUFFER = 199
|
| 251 |
-
|
| 252 |
-
UYVY = i32(b"UYVY")
|
| 253 |
-
R8G8_B8G8 = i32(b"RGBG")
|
| 254 |
-
YUY2 = i32(b"YUY2")
|
| 255 |
-
G8R8_G8B8 = i32(b"GRGB")
|
| 256 |
-
DXT1 = i32(b"DXT1")
|
| 257 |
-
DXT2 = i32(b"DXT2")
|
| 258 |
-
DXT3 = i32(b"DXT3")
|
| 259 |
-
DXT4 = i32(b"DXT4")
|
| 260 |
-
DXT5 = i32(b"DXT5")
|
| 261 |
-
DX10 = i32(b"DX10")
|
| 262 |
-
BC4S = i32(b"BC4S")
|
| 263 |
-
BC4U = i32(b"BC4U")
|
| 264 |
-
BC5S = i32(b"BC5S")
|
| 265 |
-
BC5U = i32(b"BC5U")
|
| 266 |
-
ATI1 = i32(b"ATI1")
|
| 267 |
-
ATI2 = i32(b"ATI2")
|
| 268 |
-
MULTI2_ARGB8 = i32(b"MET1")
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
# Backward compatibility layer
|
| 272 |
-
module = sys.modules[__name__]
|
| 273 |
-
for item in DDSD:
|
| 274 |
-
assert item.name is not None
|
| 275 |
-
setattr(module, f"DDSD_{item.name}", item.value)
|
| 276 |
-
for item1 in DDSCAPS:
|
| 277 |
-
assert item1.name is not None
|
| 278 |
-
setattr(module, f"DDSCAPS_{item1.name}", item1.value)
|
| 279 |
-
for item2 in DDSCAPS2:
|
| 280 |
-
assert item2.name is not None
|
| 281 |
-
setattr(module, f"DDSCAPS2_{item2.name}", item2.value)
|
| 282 |
-
for item3 in DDPF:
|
| 283 |
-
assert item3.name is not None
|
| 284 |
-
setattr(module, f"DDPF_{item3.name}", item3.value)
|
| 285 |
-
|
| 286 |
-
DDS_FOURCC = DDPF.FOURCC
|
| 287 |
-
DDS_RGB = DDPF.RGB
|
| 288 |
-
DDS_RGBA = DDPF.RGB | DDPF.ALPHAPIXELS
|
| 289 |
-
DDS_LUMINANCE = DDPF.LUMINANCE
|
| 290 |
-
DDS_LUMINANCEA = DDPF.LUMINANCE | DDPF.ALPHAPIXELS
|
| 291 |
-
DDS_ALPHA = DDPF.ALPHA
|
| 292 |
-
DDS_PAL8 = DDPF.PALETTEINDEXED8
|
| 293 |
-
|
| 294 |
-
DDS_HEADER_FLAGS_TEXTURE = DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PIXELFORMAT
|
| 295 |
-
DDS_HEADER_FLAGS_MIPMAP = DDSD.MIPMAPCOUNT
|
| 296 |
-
DDS_HEADER_FLAGS_VOLUME = DDSD.DEPTH
|
| 297 |
-
DDS_HEADER_FLAGS_PITCH = DDSD.PITCH
|
| 298 |
-
DDS_HEADER_FLAGS_LINEARSIZE = DDSD.LINEARSIZE
|
| 299 |
-
|
| 300 |
-
DDS_HEIGHT = DDSD.HEIGHT
|
| 301 |
-
DDS_WIDTH = DDSD.WIDTH
|
| 302 |
-
|
| 303 |
-
DDS_SURFACE_FLAGS_TEXTURE = DDSCAPS.TEXTURE
|
| 304 |
-
DDS_SURFACE_FLAGS_MIPMAP = DDSCAPS.COMPLEX | DDSCAPS.MIPMAP
|
| 305 |
-
DDS_SURFACE_FLAGS_CUBEMAP = DDSCAPS.COMPLEX
|
| 306 |
-
|
| 307 |
-
DDS_CUBEMAP_POSITIVEX = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_POSITIVEX
|
| 308 |
-
DDS_CUBEMAP_NEGATIVEX = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_NEGATIVEX
|
| 309 |
-
DDS_CUBEMAP_POSITIVEY = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_POSITIVEY
|
| 310 |
-
DDS_CUBEMAP_NEGATIVEY = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_NEGATIVEY
|
| 311 |
-
DDS_CUBEMAP_POSITIVEZ = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_POSITIVEZ
|
| 312 |
-
DDS_CUBEMAP_NEGATIVEZ = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_NEGATIVEZ
|
| 313 |
-
|
| 314 |
-
DXT1_FOURCC = D3DFMT.DXT1
|
| 315 |
-
DXT3_FOURCC = D3DFMT.DXT3
|
| 316 |
-
DXT5_FOURCC = D3DFMT.DXT5
|
| 317 |
-
|
| 318 |
-
DXGI_FORMAT_R8G8B8A8_TYPELESS = DXGI_FORMAT.R8G8B8A8_TYPELESS
|
| 319 |
-
DXGI_FORMAT_R8G8B8A8_UNORM = DXGI_FORMAT.R8G8B8A8_UNORM
|
| 320 |
-
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = DXGI_FORMAT.R8G8B8A8_UNORM_SRGB
|
| 321 |
-
DXGI_FORMAT_BC5_TYPELESS = DXGI_FORMAT.BC5_TYPELESS
|
| 322 |
-
DXGI_FORMAT_BC5_UNORM = DXGI_FORMAT.BC5_UNORM
|
| 323 |
-
DXGI_FORMAT_BC5_SNORM = DXGI_FORMAT.BC5_SNORM
|
| 324 |
-
DXGI_FORMAT_BC6H_UF16 = DXGI_FORMAT.BC6H_UF16
|
| 325 |
-
DXGI_FORMAT_BC6H_SF16 = DXGI_FORMAT.BC6H_SF16
|
| 326 |
-
DXGI_FORMAT_BC7_TYPELESS = DXGI_FORMAT.BC7_TYPELESS
|
| 327 |
-
DXGI_FORMAT_BC7_UNORM = DXGI_FORMAT.BC7_UNORM
|
| 328 |
-
DXGI_FORMAT_BC7_UNORM_SRGB = DXGI_FORMAT.BC7_UNORM_SRGB
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
class DdsImageFile(ImageFile.ImageFile):
|
| 332 |
-
format = "DDS"
|
| 333 |
-
format_description = "DirectDraw Surface"
|
| 334 |
-
|
| 335 |
-
def _open(self) -> None:
|
| 336 |
-
if not _accept(self.fp.read(4)):
|
| 337 |
-
msg = "not a DDS file"
|
| 338 |
-
raise SyntaxError(msg)
|
| 339 |
-
(header_size,) = struct.unpack("<I", self.fp.read(4))
|
| 340 |
-
if header_size != 124:
|
| 341 |
-
msg = f"Unsupported header size {repr(header_size)}"
|
| 342 |
-
raise OSError(msg)
|
| 343 |
-
header_bytes = self.fp.read(header_size - 4)
|
| 344 |
-
if len(header_bytes) != 120:
|
| 345 |
-
msg = f"Incomplete header: {len(header_bytes)} bytes"
|
| 346 |
-
raise OSError(msg)
|
| 347 |
-
header = io.BytesIO(header_bytes)
|
| 348 |
-
|
| 349 |
-
flags, height, width = struct.unpack("<3I", header.read(12))
|
| 350 |
-
self._size = (width, height)
|
| 351 |
-
extents = (0, 0) + self.size
|
| 352 |
-
|
| 353 |
-
pitch, depth, mipmaps = struct.unpack("<3I", header.read(12))
|
| 354 |
-
struct.unpack("<11I", header.read(44)) # reserved
|
| 355 |
-
|
| 356 |
-
# pixel format
|
| 357 |
-
pfsize, pfflags, fourcc, bitcount = struct.unpack("<4I", header.read(16))
|
| 358 |
-
n = 0
|
| 359 |
-
rawmode = None
|
| 360 |
-
if pfflags & DDPF.RGB:
|
| 361 |
-
# Texture contains uncompressed RGB data
|
| 362 |
-
if pfflags & DDPF.ALPHAPIXELS:
|
| 363 |
-
self._mode = "RGBA"
|
| 364 |
-
mask_count = 4
|
| 365 |
-
else:
|
| 366 |
-
self._mode = "RGB"
|
| 367 |
-
mask_count = 3
|
| 368 |
-
|
| 369 |
-
masks = struct.unpack(f"<{mask_count}I", header.read(mask_count * 4))
|
| 370 |
-
self.tile = [ImageFile._Tile("dds_rgb", extents, 0, (bitcount, masks))]
|
| 371 |
-
return
|
| 372 |
-
elif pfflags & DDPF.LUMINANCE:
|
| 373 |
-
if bitcount == 8:
|
| 374 |
-
self._mode = "L"
|
| 375 |
-
elif bitcount == 16 and pfflags & DDPF.ALPHAPIXELS:
|
| 376 |
-
self._mode = "LA"
|
| 377 |
-
else:
|
| 378 |
-
msg = f"Unsupported bitcount {bitcount} for {pfflags}"
|
| 379 |
-
raise OSError(msg)
|
| 380 |
-
elif pfflags & DDPF.PALETTEINDEXED8:
|
| 381 |
-
self._mode = "P"
|
| 382 |
-
self.palette = ImagePalette.raw("RGBA", self.fp.read(1024))
|
| 383 |
-
self.palette.mode = "RGBA"
|
| 384 |
-
elif pfflags & DDPF.FOURCC:
|
| 385 |
-
offset = header_size + 4
|
| 386 |
-
if fourcc == D3DFMT.DXT1:
|
| 387 |
-
self._mode = "RGBA"
|
| 388 |
-
self.pixel_format = "DXT1"
|
| 389 |
-
n = 1
|
| 390 |
-
elif fourcc == D3DFMT.DXT3:
|
| 391 |
-
self._mode = "RGBA"
|
| 392 |
-
self.pixel_format = "DXT3"
|
| 393 |
-
n = 2
|
| 394 |
-
elif fourcc == D3DFMT.DXT5:
|
| 395 |
-
self._mode = "RGBA"
|
| 396 |
-
self.pixel_format = "DXT5"
|
| 397 |
-
n = 3
|
| 398 |
-
elif fourcc in (D3DFMT.BC4U, D3DFMT.ATI1):
|
| 399 |
-
self._mode = "L"
|
| 400 |
-
self.pixel_format = "BC4"
|
| 401 |
-
n = 4
|
| 402 |
-
elif fourcc == D3DFMT.BC5S:
|
| 403 |
-
self._mode = "RGB"
|
| 404 |
-
self.pixel_format = "BC5S"
|
| 405 |
-
n = 5
|
| 406 |
-
elif fourcc in (D3DFMT.BC5U, D3DFMT.ATI2):
|
| 407 |
-
self._mode = "RGB"
|
| 408 |
-
self.pixel_format = "BC5"
|
| 409 |
-
n = 5
|
| 410 |
-
elif fourcc == D3DFMT.DX10:
|
| 411 |
-
offset += 20
|
| 412 |
-
# ignoring flags which pertain to volume textures and cubemaps
|
| 413 |
-
(dxgi_format,) = struct.unpack("<I", self.fp.read(4))
|
| 414 |
-
self.fp.read(16)
|
| 415 |
-
if dxgi_format in (
|
| 416 |
-
DXGI_FORMAT.BC1_UNORM,
|
| 417 |
-
DXGI_FORMAT.BC1_TYPELESS,
|
| 418 |
-
):
|
| 419 |
-
self._mode = "RGBA"
|
| 420 |
-
self.pixel_format = "BC1"
|
| 421 |
-
n = 1
|
| 422 |
-
elif dxgi_format in (DXGI_FORMAT.BC2_TYPELESS, DXGI_FORMAT.BC2_UNORM):
|
| 423 |
-
self._mode = "RGBA"
|
| 424 |
-
self.pixel_format = "BC2"
|
| 425 |
-
n = 2
|
| 426 |
-
elif dxgi_format in (DXGI_FORMAT.BC3_TYPELESS, DXGI_FORMAT.BC3_UNORM):
|
| 427 |
-
self._mode = "RGBA"
|
| 428 |
-
self.pixel_format = "BC3"
|
| 429 |
-
n = 3
|
| 430 |
-
elif dxgi_format in (DXGI_FORMAT.BC4_TYPELESS, DXGI_FORMAT.BC4_UNORM):
|
| 431 |
-
self._mode = "L"
|
| 432 |
-
self.pixel_format = "BC4"
|
| 433 |
-
n = 4
|
| 434 |
-
elif dxgi_format in (DXGI_FORMAT.BC5_TYPELESS, DXGI_FORMAT.BC5_UNORM):
|
| 435 |
-
self._mode = "RGB"
|
| 436 |
-
self.pixel_format = "BC5"
|
| 437 |
-
n = 5
|
| 438 |
-
elif dxgi_format == DXGI_FORMAT.BC5_SNORM:
|
| 439 |
-
self._mode = "RGB"
|
| 440 |
-
self.pixel_format = "BC5S"
|
| 441 |
-
n = 5
|
| 442 |
-
elif dxgi_format == DXGI_FORMAT.BC6H_UF16:
|
| 443 |
-
self._mode = "RGB"
|
| 444 |
-
self.pixel_format = "BC6H"
|
| 445 |
-
n = 6
|
| 446 |
-
elif dxgi_format == DXGI_FORMAT.BC6H_SF16:
|
| 447 |
-
self._mode = "RGB"
|
| 448 |
-
self.pixel_format = "BC6HS"
|
| 449 |
-
n = 6
|
| 450 |
-
elif dxgi_format in (
|
| 451 |
-
DXGI_FORMAT.BC7_TYPELESS,
|
| 452 |
-
DXGI_FORMAT.BC7_UNORM,
|
| 453 |
-
DXGI_FORMAT.BC7_UNORM_SRGB,
|
| 454 |
-
):
|
| 455 |
-
self._mode = "RGBA"
|
| 456 |
-
self.pixel_format = "BC7"
|
| 457 |
-
n = 7
|
| 458 |
-
if dxgi_format == DXGI_FORMAT.BC7_UNORM_SRGB:
|
| 459 |
-
self.info["gamma"] = 1 / 2.2
|
| 460 |
-
elif dxgi_format in (
|
| 461 |
-
DXGI_FORMAT.R8G8B8A8_TYPELESS,
|
| 462 |
-
DXGI_FORMAT.R8G8B8A8_UNORM,
|
| 463 |
-
DXGI_FORMAT.R8G8B8A8_UNORM_SRGB,
|
| 464 |
-
):
|
| 465 |
-
self._mode = "RGBA"
|
| 466 |
-
if dxgi_format == DXGI_FORMAT.R8G8B8A8_UNORM_SRGB:
|
| 467 |
-
self.info["gamma"] = 1 / 2.2
|
| 468 |
-
else:
|
| 469 |
-
msg = f"Unimplemented DXGI format {dxgi_format}"
|
| 470 |
-
raise NotImplementedError(msg)
|
| 471 |
-
else:
|
| 472 |
-
msg = f"Unimplemented pixel format {repr(fourcc)}"
|
| 473 |
-
raise NotImplementedError(msg)
|
| 474 |
-
else:
|
| 475 |
-
msg = f"Unknown pixel format flags {pfflags}"
|
| 476 |
-
raise NotImplementedError(msg)
|
| 477 |
-
|
| 478 |
-
if n:
|
| 479 |
-
self.tile = [
|
| 480 |
-
ImageFile._Tile("bcn", extents, offset, (n, self.pixel_format))
|
| 481 |
-
]
|
| 482 |
-
else:
|
| 483 |
-
self.tile = [ImageFile._Tile("raw", extents, 0, rawmode or self.mode)]
|
| 484 |
-
|
| 485 |
-
def load_seek(self, pos: int) -> None:
|
| 486 |
-
pass
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
class DdsRgbDecoder(ImageFile.PyDecoder):
|
| 490 |
-
_pulls_fd = True
|
| 491 |
-
|
| 492 |
-
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
|
| 493 |
-
assert self.fd is not None
|
| 494 |
-
bitcount, masks = self.args
|
| 495 |
-
|
| 496 |
-
# Some masks will be padded with zeros, e.g. R 0b11 G 0b1100
|
| 497 |
-
# Calculate how many zeros each mask is padded with
|
| 498 |
-
mask_offsets = []
|
| 499 |
-
# And the maximum value of each channel without the padding
|
| 500 |
-
mask_totals = []
|
| 501 |
-
for mask in masks:
|
| 502 |
-
offset = 0
|
| 503 |
-
if mask != 0:
|
| 504 |
-
while mask >> (offset + 1) << (offset + 1) == mask:
|
| 505 |
-
offset += 1
|
| 506 |
-
mask_offsets.append(offset)
|
| 507 |
-
mask_totals.append(mask >> offset)
|
| 508 |
-
|
| 509 |
-
data = bytearray()
|
| 510 |
-
bytecount = bitcount // 8
|
| 511 |
-
dest_length = self.state.xsize * self.state.ysize * len(masks)
|
| 512 |
-
while len(data) < dest_length:
|
| 513 |
-
value = int.from_bytes(self.fd.read(bytecount), "little")
|
| 514 |
-
for i, mask in enumerate(masks):
|
| 515 |
-
masked_value = value & mask
|
| 516 |
-
# Remove the zero padding, and scale it to 8 bits
|
| 517 |
-
data += o8(
|
| 518 |
-
int(((masked_value >> mask_offsets[i]) / mask_totals[i]) * 255)
|
| 519 |
-
)
|
| 520 |
-
self.set_as_raw(data)
|
| 521 |
-
return -1, 0
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 525 |
-
if im.mode not in ("RGB", "RGBA", "L", "LA"):
|
| 526 |
-
msg = f"cannot write mode {im.mode} as DDS"
|
| 527 |
-
raise OSError(msg)
|
| 528 |
-
|
| 529 |
-
flags = DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PIXELFORMAT
|
| 530 |
-
bitcount = len(im.getbands()) * 8
|
| 531 |
-
pixel_format = im.encoderinfo.get("pixel_format")
|
| 532 |
-
args: tuple[int] | str
|
| 533 |
-
if pixel_format:
|
| 534 |
-
codec_name = "bcn"
|
| 535 |
-
flags |= DDSD.LINEARSIZE
|
| 536 |
-
pitch = (im.width + 3) * 4
|
| 537 |
-
rgba_mask = [0, 0, 0, 0]
|
| 538 |
-
pixel_flags = DDPF.FOURCC
|
| 539 |
-
if pixel_format == "DXT1":
|
| 540 |
-
fourcc = D3DFMT.DXT1
|
| 541 |
-
args = (1,)
|
| 542 |
-
elif pixel_format == "DXT3":
|
| 543 |
-
fourcc = D3DFMT.DXT3
|
| 544 |
-
args = (2,)
|
| 545 |
-
elif pixel_format == "DXT5":
|
| 546 |
-
fourcc = D3DFMT.DXT5
|
| 547 |
-
args = (3,)
|
| 548 |
-
else:
|
| 549 |
-
fourcc = D3DFMT.DX10
|
| 550 |
-
if pixel_format == "BC2":
|
| 551 |
-
args = (2,)
|
| 552 |
-
dxgi_format = DXGI_FORMAT.BC2_TYPELESS
|
| 553 |
-
elif pixel_format == "BC3":
|
| 554 |
-
args = (3,)
|
| 555 |
-
dxgi_format = DXGI_FORMAT.BC3_TYPELESS
|
| 556 |
-
elif pixel_format == "BC5":
|
| 557 |
-
args = (5,)
|
| 558 |
-
dxgi_format = DXGI_FORMAT.BC5_TYPELESS
|
| 559 |
-
if im.mode != "RGB":
|
| 560 |
-
msg = "only RGB mode can be written as BC5"
|
| 561 |
-
raise OSError(msg)
|
| 562 |
-
else:
|
| 563 |
-
msg = f"cannot write pixel format {pixel_format}"
|
| 564 |
-
raise OSError(msg)
|
| 565 |
-
else:
|
| 566 |
-
codec_name = "raw"
|
| 567 |
-
flags |= DDSD.PITCH
|
| 568 |
-
pitch = (im.width * bitcount + 7) // 8
|
| 569 |
-
|
| 570 |
-
alpha = im.mode[-1] == "A"
|
| 571 |
-
if im.mode[0] == "L":
|
| 572 |
-
pixel_flags = DDPF.LUMINANCE
|
| 573 |
-
args = im.mode
|
| 574 |
-
if alpha:
|
| 575 |
-
rgba_mask = [0x000000FF, 0x000000FF, 0x000000FF]
|
| 576 |
-
else:
|
| 577 |
-
rgba_mask = [0xFF000000, 0xFF000000, 0xFF000000]
|
| 578 |
-
else:
|
| 579 |
-
pixel_flags = DDPF.RGB
|
| 580 |
-
args = im.mode[::-1]
|
| 581 |
-
rgba_mask = [0x00FF0000, 0x0000FF00, 0x000000FF]
|
| 582 |
-
|
| 583 |
-
if alpha:
|
| 584 |
-
r, g, b, a = im.split()
|
| 585 |
-
im = Image.merge("RGBA", (a, r, g, b))
|
| 586 |
-
if alpha:
|
| 587 |
-
pixel_flags |= DDPF.ALPHAPIXELS
|
| 588 |
-
rgba_mask.append(0xFF000000 if alpha else 0)
|
| 589 |
-
|
| 590 |
-
fourcc = D3DFMT.UNKNOWN
|
| 591 |
-
fp.write(
|
| 592 |
-
o32(DDS_MAGIC)
|
| 593 |
-
+ struct.pack(
|
| 594 |
-
"<7I",
|
| 595 |
-
124, # header size
|
| 596 |
-
flags, # flags
|
| 597 |
-
im.height,
|
| 598 |
-
im.width,
|
| 599 |
-
pitch,
|
| 600 |
-
0, # depth
|
| 601 |
-
0, # mipmaps
|
| 602 |
-
)
|
| 603 |
-
+ struct.pack("11I", *((0,) * 11)) # reserved
|
| 604 |
-
# pfsize, pfflags, fourcc, bitcount
|
| 605 |
-
+ struct.pack("<4I", 32, pixel_flags, fourcc, bitcount)
|
| 606 |
-
+ struct.pack("<4I", *rgba_mask) # dwRGBABitMask
|
| 607 |
-
+ struct.pack("<5I", DDSCAPS.TEXTURE, 0, 0, 0, 0)
|
| 608 |
-
)
|
| 609 |
-
if fourcc == D3DFMT.DX10:
|
| 610 |
-
fp.write(
|
| 611 |
-
# dxgi_format, 2D resource, misc, array size, straight alpha
|
| 612 |
-
struct.pack("<5I", dxgi_format, 3, 0, 0, 1)
|
| 613 |
-
)
|
| 614 |
-
ImageFile._save(im, fp, [ImageFile._Tile(codec_name, (0, 0) + im.size, 0, args)])
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
def _accept(prefix: bytes) -> bool:
|
| 618 |
-
return prefix.startswith(b"DDS ")
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
Image.register_open(DdsImageFile.format, DdsImageFile, _accept)
|
| 622 |
-
Image.register_decoder("dds_rgb", DdsRgbDecoder)
|
| 623 |
-
Image.register_save(DdsImageFile.format, _save)
|
| 624 |
-
Image.register_extension(DdsImageFile.format, ".dds")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv/lib/python3.10/site-packages/PIL/EpsImagePlugin.py
DELETED
|
@@ -1,479 +0,0 @@
|
|
| 1 |
-
#
|
| 2 |
-
# The Python Imaging Library.
|
| 3 |
-
# $Id$
|
| 4 |
-
#
|
| 5 |
-
# EPS file handling
|
| 6 |
-
#
|
| 7 |
-
# History:
|
| 8 |
-
# 1995-09-01 fl Created (0.1)
|
| 9 |
-
# 1996-05-18 fl Don't choke on "atend" fields, Ghostscript interface (0.2)
|
| 10 |
-
# 1996-08-22 fl Don't choke on floating point BoundingBox values
|
| 11 |
-
# 1996-08-23 fl Handle files from Macintosh (0.3)
|
| 12 |
-
# 2001-02-17 fl Use 're' instead of 'regex' (Python 2.1) (0.4)
|
| 13 |
-
# 2003-09-07 fl Check gs.close status (from Federico Di Gregorio) (0.5)
|
| 14 |
-
# 2014-05-07 e Handling of EPS with binary preview and fixed resolution
|
| 15 |
-
# resizing
|
| 16 |
-
#
|
| 17 |
-
# Copyright (c) 1997-2003 by Secret Labs AB.
|
| 18 |
-
# Copyright (c) 1995-2003 by Fredrik Lundh
|
| 19 |
-
#
|
| 20 |
-
# See the README file for information on usage and redistribution.
|
| 21 |
-
#
|
| 22 |
-
from __future__ import annotations
|
| 23 |
-
|
| 24 |
-
import io
|
| 25 |
-
import os
|
| 26 |
-
import re
|
| 27 |
-
import subprocess
|
| 28 |
-
import sys
|
| 29 |
-
import tempfile
|
| 30 |
-
from typing import IO
|
| 31 |
-
|
| 32 |
-
from . import Image, ImageFile
|
| 33 |
-
from ._binary import i32le as i32
|
| 34 |
-
|
| 35 |
-
# --------------------------------------------------------------------
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
split = re.compile(r"^%%([^:]*):[ \t]*(.*)[ \t]*$")
|
| 39 |
-
field = re.compile(r"^%[%!\w]([^:]*)[ \t]*$")
|
| 40 |
-
|
| 41 |
-
gs_binary: str | bool | None = None
|
| 42 |
-
gs_windows_binary = None
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def has_ghostscript() -> bool:
|
| 46 |
-
global gs_binary, gs_windows_binary
|
| 47 |
-
if gs_binary is None:
|
| 48 |
-
if sys.platform.startswith("win"):
|
| 49 |
-
if gs_windows_binary is None:
|
| 50 |
-
import shutil
|
| 51 |
-
|
| 52 |
-
for binary in ("gswin32c", "gswin64c", "gs"):
|
| 53 |
-
if shutil.which(binary) is not None:
|
| 54 |
-
gs_windows_binary = binary
|
| 55 |
-
break
|
| 56 |
-
else:
|
| 57 |
-
gs_windows_binary = False
|
| 58 |
-
gs_binary = gs_windows_binary
|
| 59 |
-
else:
|
| 60 |
-
try:
|
| 61 |
-
subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL)
|
| 62 |
-
gs_binary = "gs"
|
| 63 |
-
except OSError:
|
| 64 |
-
gs_binary = False
|
| 65 |
-
return gs_binary is not False
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
def Ghostscript(
|
| 69 |
-
tile: list[ImageFile._Tile],
|
| 70 |
-
size: tuple[int, int],
|
| 71 |
-
fp: IO[bytes],
|
| 72 |
-
scale: int = 1,
|
| 73 |
-
transparency: bool = False,
|
| 74 |
-
) -> Image.core.ImagingCore:
|
| 75 |
-
"""Render an image using Ghostscript"""
|
| 76 |
-
global gs_binary
|
| 77 |
-
if not has_ghostscript():
|
| 78 |
-
msg = "Unable to locate Ghostscript on paths"
|
| 79 |
-
raise OSError(msg)
|
| 80 |
-
assert isinstance(gs_binary, str)
|
| 81 |
-
|
| 82 |
-
# Unpack decoder tile
|
| 83 |
-
args = tile[0].args
|
| 84 |
-
assert isinstance(args, tuple)
|
| 85 |
-
length, bbox = args
|
| 86 |
-
|
| 87 |
-
# Hack to support hi-res rendering
|
| 88 |
-
scale = int(scale) or 1
|
| 89 |
-
width = size[0] * scale
|
| 90 |
-
height = size[1] * scale
|
| 91 |
-
# resolution is dependent on bbox and size
|
| 92 |
-
res_x = 72.0 * width / (bbox[2] - bbox[0])
|
| 93 |
-
res_y = 72.0 * height / (bbox[3] - bbox[1])
|
| 94 |
-
|
| 95 |
-
out_fd, outfile = tempfile.mkstemp()
|
| 96 |
-
os.close(out_fd)
|
| 97 |
-
|
| 98 |
-
infile_temp = None
|
| 99 |
-
if hasattr(fp, "name") and os.path.exists(fp.name):
|
| 100 |
-
infile = fp.name
|
| 101 |
-
else:
|
| 102 |
-
in_fd, infile_temp = tempfile.mkstemp()
|
| 103 |
-
os.close(in_fd)
|
| 104 |
-
infile = infile_temp
|
| 105 |
-
|
| 106 |
-
# Ignore length and offset!
|
| 107 |
-
# Ghostscript can read it
|
| 108 |
-
# Copy whole file to read in Ghostscript
|
| 109 |
-
with open(infile_temp, "wb") as f:
|
| 110 |
-
# fetch length of fp
|
| 111 |
-
fp.seek(0, io.SEEK_END)
|
| 112 |
-
fsize = fp.tell()
|
| 113 |
-
# ensure start position
|
| 114 |
-
# go back
|
| 115 |
-
fp.seek(0)
|
| 116 |
-
lengthfile = fsize
|
| 117 |
-
while lengthfile > 0:
|
| 118 |
-
s = fp.read(min(lengthfile, 100 * 1024))
|
| 119 |
-
if not s:
|
| 120 |
-
break
|
| 121 |
-
lengthfile -= len(s)
|
| 122 |
-
f.write(s)
|
| 123 |
-
|
| 124 |
-
if transparency:
|
| 125 |
-
# "RGBA"
|
| 126 |
-
device = "pngalpha"
|
| 127 |
-
else:
|
| 128 |
-
# "pnmraw" automatically chooses between
|
| 129 |
-
# PBM ("1"), PGM ("L"), and PPM ("RGB").
|
| 130 |
-
device = "pnmraw"
|
| 131 |
-
|
| 132 |
-
# Build Ghostscript command
|
| 133 |
-
command = [
|
| 134 |
-
gs_binary,
|
| 135 |
-
"-q", # quiet mode
|
| 136 |
-
f"-g{width:d}x{height:d}", # set output geometry (pixels)
|
| 137 |
-
f"-r{res_x:f}x{res_y:f}", # set input DPI (dots per inch)
|
| 138 |
-
"-dBATCH", # exit after processing
|
| 139 |
-
"-dNOPAUSE", # don't pause between pages
|
| 140 |
-
"-dSAFER", # safe mode
|
| 141 |
-
f"-sDEVICE={device}",
|
| 142 |
-
f"-sOutputFile={outfile}", # output file
|
| 143 |
-
# adjust for image origin
|
| 144 |
-
"-c",
|
| 145 |
-
f"{-bbox[0]} {-bbox[1]} translate",
|
| 146 |
-
"-f",
|
| 147 |
-
infile, # input file
|
| 148 |
-
# showpage (see https://bugs.ghostscript.com/show_bug.cgi?id=698272)
|
| 149 |
-
"-c",
|
| 150 |
-
"showpage",
|
| 151 |
-
]
|
| 152 |
-
|
| 153 |
-
# push data through Ghostscript
|
| 154 |
-
try:
|
| 155 |
-
startupinfo = None
|
| 156 |
-
if sys.platform.startswith("win"):
|
| 157 |
-
startupinfo = subprocess.STARTUPINFO()
|
| 158 |
-
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
| 159 |
-
subprocess.check_call(command, startupinfo=startupinfo)
|
| 160 |
-
with Image.open(outfile) as out_im:
|
| 161 |
-
out_im.load()
|
| 162 |
-
return out_im.im.copy()
|
| 163 |
-
finally:
|
| 164 |
-
try:
|
| 165 |
-
os.unlink(outfile)
|
| 166 |
-
if infile_temp:
|
| 167 |
-
os.unlink(infile_temp)
|
| 168 |
-
except OSError:
|
| 169 |
-
pass
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
def _accept(prefix: bytes) -> bool:
|
| 173 |
-
return prefix.startswith(b"%!PS") or (
|
| 174 |
-
len(prefix) >= 4 and i32(prefix) == 0xC6D3D0C5
|
| 175 |
-
)
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
##
|
| 179 |
-
# Image plugin for Encapsulated PostScript. This plugin supports only
|
| 180 |
-
# a few variants of this format.
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
class EpsImageFile(ImageFile.ImageFile):
|
| 184 |
-
"""EPS File Parser for the Python Imaging Library"""
|
| 185 |
-
|
| 186 |
-
format = "EPS"
|
| 187 |
-
format_description = "Encapsulated Postscript"
|
| 188 |
-
|
| 189 |
-
mode_map = {1: "L", 2: "LAB", 3: "RGB", 4: "CMYK"}
|
| 190 |
-
|
| 191 |
-
def _open(self) -> None:
|
| 192 |
-
(length, offset) = self._find_offset(self.fp)
|
| 193 |
-
|
| 194 |
-
# go to offset - start of "%!PS"
|
| 195 |
-
self.fp.seek(offset)
|
| 196 |
-
|
| 197 |
-
self._mode = "RGB"
|
| 198 |
-
|
| 199 |
-
# When reading header comments, the first comment is used.
|
| 200 |
-
# When reading trailer comments, the last comment is used.
|
| 201 |
-
bounding_box: list[int] | None = None
|
| 202 |
-
imagedata_size: tuple[int, int] | None = None
|
| 203 |
-
|
| 204 |
-
byte_arr = bytearray(255)
|
| 205 |
-
bytes_mv = memoryview(byte_arr)
|
| 206 |
-
bytes_read = 0
|
| 207 |
-
reading_header_comments = True
|
| 208 |
-
reading_trailer_comments = False
|
| 209 |
-
trailer_reached = False
|
| 210 |
-
|
| 211 |
-
def check_required_header_comments() -> None:
|
| 212 |
-
"""
|
| 213 |
-
The EPS specification requires that some headers exist.
|
| 214 |
-
This should be checked when the header comments formally end,
|
| 215 |
-
when image data starts, or when the file ends, whichever comes first.
|
| 216 |
-
"""
|
| 217 |
-
if "PS-Adobe" not in self.info:
|
| 218 |
-
msg = 'EPS header missing "%!PS-Adobe" comment'
|
| 219 |
-
raise SyntaxError(msg)
|
| 220 |
-
if "BoundingBox" not in self.info:
|
| 221 |
-
msg = 'EPS header missing "%%BoundingBox" comment'
|
| 222 |
-
raise SyntaxError(msg)
|
| 223 |
-
|
| 224 |
-
def read_comment(s: str) -> bool:
|
| 225 |
-
nonlocal bounding_box, reading_trailer_comments
|
| 226 |
-
try:
|
| 227 |
-
m = split.match(s)
|
| 228 |
-
except re.error as e:
|
| 229 |
-
msg = "not an EPS file"
|
| 230 |
-
raise SyntaxError(msg) from e
|
| 231 |
-
|
| 232 |
-
if not m:
|
| 233 |
-
return False
|
| 234 |
-
|
| 235 |
-
k, v = m.group(1, 2)
|
| 236 |
-
self.info[k] = v
|
| 237 |
-
if k == "BoundingBox":
|
| 238 |
-
if v == "(atend)":
|
| 239 |
-
reading_trailer_comments = True
|
| 240 |
-
elif not bounding_box or (trailer_reached and reading_trailer_comments):
|
| 241 |
-
try:
|
| 242 |
-
# Note: The DSC spec says that BoundingBox
|
| 243 |
-
# fields should be integers, but some drivers
|
| 244 |
-
# put floating point values there anyway.
|
| 245 |
-
bounding_box = [int(float(i)) for i in v.split()]
|
| 246 |
-
except Exception:
|
| 247 |
-
pass
|
| 248 |
-
return True
|
| 249 |
-
|
| 250 |
-
while True:
|
| 251 |
-
byte = self.fp.read(1)
|
| 252 |
-
if byte == b"":
|
| 253 |
-
# if we didn't read a byte we must be at the end of the file
|
| 254 |
-
if bytes_read == 0:
|
| 255 |
-
if reading_header_comments:
|
| 256 |
-
check_required_header_comments()
|
| 257 |
-
break
|
| 258 |
-
elif byte in b"\r\n":
|
| 259 |
-
# if we read a line ending character, ignore it and parse what
|
| 260 |
-
# we have already read. if we haven't read any other characters,
|
| 261 |
-
# continue reading
|
| 262 |
-
if bytes_read == 0:
|
| 263 |
-
continue
|
| 264 |
-
else:
|
| 265 |
-
# ASCII/hexadecimal lines in an EPS file must not exceed
|
| 266 |
-
# 255 characters, not including line ending characters
|
| 267 |
-
if bytes_read >= 255:
|
| 268 |
-
# only enforce this for lines starting with a "%",
|
| 269 |
-
# otherwise assume it's binary data
|
| 270 |
-
if byte_arr[0] == ord("%"):
|
| 271 |
-
msg = "not an EPS file"
|
| 272 |
-
raise SyntaxError(msg)
|
| 273 |
-
else:
|
| 274 |
-
if reading_header_comments:
|
| 275 |
-
check_required_header_comments()
|
| 276 |
-
reading_header_comments = False
|
| 277 |
-
# reset bytes_read so we can keep reading
|
| 278 |
-
# data until the end of the line
|
| 279 |
-
bytes_read = 0
|
| 280 |
-
byte_arr[bytes_read] = byte[0]
|
| 281 |
-
bytes_read += 1
|
| 282 |
-
continue
|
| 283 |
-
|
| 284 |
-
if reading_header_comments:
|
| 285 |
-
# Load EPS header
|
| 286 |
-
|
| 287 |
-
# if this line doesn't start with a "%",
|
| 288 |
-
# or does start with "%%EndComments",
|
| 289 |
-
# then we've reached the end of the header/comments
|
| 290 |
-
if byte_arr[0] != ord("%") or bytes_mv[:13] == b"%%EndComments":
|
| 291 |
-
check_required_header_comments()
|
| 292 |
-
reading_header_comments = False
|
| 293 |
-
continue
|
| 294 |
-
|
| 295 |
-
s = str(bytes_mv[:bytes_read], "latin-1")
|
| 296 |
-
if not read_comment(s):
|
| 297 |
-
m = field.match(s)
|
| 298 |
-
if m:
|
| 299 |
-
k = m.group(1)
|
| 300 |
-
if k.startswith("PS-Adobe"):
|
| 301 |
-
self.info["PS-Adobe"] = k[9:]
|
| 302 |
-
else:
|
| 303 |
-
self.info[k] = ""
|
| 304 |
-
elif s[0] == "%":
|
| 305 |
-
# handle non-DSC PostScript comments that some
|
| 306 |
-
# tools mistakenly put in the Comments section
|
| 307 |
-
pass
|
| 308 |
-
else:
|
| 309 |
-
msg = "bad EPS header"
|
| 310 |
-
raise OSError(msg)
|
| 311 |
-
elif bytes_mv[:11] == b"%ImageData:":
|
| 312 |
-
# Check for an "ImageData" descriptor
|
| 313 |
-
# https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577413_pgfId-1035096
|
| 314 |
-
|
| 315 |
-
# If we've already read an "ImageData" descriptor,
|
| 316 |
-
# don't read another one.
|
| 317 |
-
if imagedata_size:
|
| 318 |
-
bytes_read = 0
|
| 319 |
-
continue
|
| 320 |
-
|
| 321 |
-
# Values:
|
| 322 |
-
# columns
|
| 323 |
-
# rows
|
| 324 |
-
# bit depth (1 or 8)
|
| 325 |
-
# mode (1: L, 2: LAB, 3: RGB, 4: CMYK)
|
| 326 |
-
# number of padding channels
|
| 327 |
-
# block size (number of bytes per row per channel)
|
| 328 |
-
# binary/ascii (1: binary, 2: ascii)
|
| 329 |
-
# data start identifier (the image data follows after a single line
|
| 330 |
-
# consisting only of this quoted value)
|
| 331 |
-
image_data_values = byte_arr[11:bytes_read].split(None, 7)
|
| 332 |
-
columns, rows, bit_depth, mode_id = (
|
| 333 |
-
int(value) for value in image_data_values[:4]
|
| 334 |
-
)
|
| 335 |
-
|
| 336 |
-
if bit_depth == 1:
|
| 337 |
-
self._mode = "1"
|
| 338 |
-
elif bit_depth == 8:
|
| 339 |
-
try:
|
| 340 |
-
self._mode = self.mode_map[mode_id]
|
| 341 |
-
except ValueError:
|
| 342 |
-
break
|
| 343 |
-
else:
|
| 344 |
-
break
|
| 345 |
-
|
| 346 |
-
# Parse the columns and rows after checking the bit depth and mode
|
| 347 |
-
# in case the bit depth and/or mode are invalid.
|
| 348 |
-
imagedata_size = columns, rows
|
| 349 |
-
elif bytes_mv[:5] == b"%%EOF":
|
| 350 |
-
break
|
| 351 |
-
elif trailer_reached and reading_trailer_comments:
|
| 352 |
-
# Load EPS trailer
|
| 353 |
-
s = str(bytes_mv[:bytes_read], "latin-1")
|
| 354 |
-
read_comment(s)
|
| 355 |
-
elif bytes_mv[:9] == b"%%Trailer":
|
| 356 |
-
trailer_reached = True
|
| 357 |
-
elif bytes_mv[:14] == b"%%BeginBinary:":
|
| 358 |
-
bytecount = int(byte_arr[14:bytes_read])
|
| 359 |
-
self.fp.seek(bytecount, os.SEEK_CUR)
|
| 360 |
-
bytes_read = 0
|
| 361 |
-
|
| 362 |
-
# A "BoundingBox" is always required,
|
| 363 |
-
# even if an "ImageData" descriptor size exists.
|
| 364 |
-
if not bounding_box:
|
| 365 |
-
msg = "cannot determine EPS bounding box"
|
| 366 |
-
raise OSError(msg)
|
| 367 |
-
|
| 368 |
-
# An "ImageData" size takes precedence over the "BoundingBox".
|
| 369 |
-
self._size = imagedata_size or (
|
| 370 |
-
bounding_box[2] - bounding_box[0],
|
| 371 |
-
bounding_box[3] - bounding_box[1],
|
| 372 |
-
)
|
| 373 |
-
|
| 374 |
-
self.tile = [
|
| 375 |
-
ImageFile._Tile("eps", (0, 0) + self.size, offset, (length, bounding_box))
|
| 376 |
-
]
|
| 377 |
-
|
| 378 |
-
def _find_offset(self, fp: IO[bytes]) -> tuple[int, int]:
|
| 379 |
-
s = fp.read(4)
|
| 380 |
-
|
| 381 |
-
if s == b"%!PS":
|
| 382 |
-
# for HEAD without binary preview
|
| 383 |
-
fp.seek(0, io.SEEK_END)
|
| 384 |
-
length = fp.tell()
|
| 385 |
-
offset = 0
|
| 386 |
-
elif i32(s) == 0xC6D3D0C5:
|
| 387 |
-
# FIX for: Some EPS file not handled correctly / issue #302
|
| 388 |
-
# EPS can contain binary data
|
| 389 |
-
# or start directly with latin coding
|
| 390 |
-
# more info see:
|
| 391 |
-
# https://web.archive.org/web/20160528181353/http://partners.adobe.com/public/developer/en/ps/5002.EPSF_Spec.pdf
|
| 392 |
-
s = fp.read(8)
|
| 393 |
-
offset = i32(s)
|
| 394 |
-
length = i32(s, 4)
|
| 395 |
-
else:
|
| 396 |
-
msg = "not an EPS file"
|
| 397 |
-
raise SyntaxError(msg)
|
| 398 |
-
|
| 399 |
-
return length, offset
|
| 400 |
-
|
| 401 |
-
def load(
|
| 402 |
-
self, scale: int = 1, transparency: bool = False
|
| 403 |
-
) -> Image.core.PixelAccess | None:
|
| 404 |
-
# Load EPS via Ghostscript
|
| 405 |
-
if self.tile:
|
| 406 |
-
self.im = Ghostscript(self.tile, self.size, self.fp, scale, transparency)
|
| 407 |
-
self._mode = self.im.mode
|
| 408 |
-
self._size = self.im.size
|
| 409 |
-
self.tile = []
|
| 410 |
-
return Image.Image.load(self)
|
| 411 |
-
|
| 412 |
-
def load_seek(self, pos: int) -> None:
|
| 413 |
-
# we can't incrementally load, so force ImageFile.parser to
|
| 414 |
-
# use our custom load method by defining this method.
|
| 415 |
-
pass
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
# --------------------------------------------------------------------
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes, eps: int = 1) -> None:
|
| 422 |
-
"""EPS Writer for the Python Imaging Library."""
|
| 423 |
-
|
| 424 |
-
# make sure image data is available
|
| 425 |
-
im.load()
|
| 426 |
-
|
| 427 |
-
# determine PostScript image mode
|
| 428 |
-
if im.mode == "L":
|
| 429 |
-
operator = (8, 1, b"image")
|
| 430 |
-
elif im.mode == "RGB":
|
| 431 |
-
operator = (8, 3, b"false 3 colorimage")
|
| 432 |
-
elif im.mode == "CMYK":
|
| 433 |
-
operator = (8, 4, b"false 4 colorimage")
|
| 434 |
-
else:
|
| 435 |
-
msg = "image mode is not supported"
|
| 436 |
-
raise ValueError(msg)
|
| 437 |
-
|
| 438 |
-
if eps:
|
| 439 |
-
# write EPS header
|
| 440 |
-
fp.write(b"%!PS-Adobe-3.0 EPSF-3.0\n")
|
| 441 |
-
fp.write(b"%%Creator: PIL 0.1 EpsEncode\n")
|
| 442 |
-
# fp.write("%%CreationDate: %s"...)
|
| 443 |
-
fp.write(b"%%%%BoundingBox: 0 0 %d %d\n" % im.size)
|
| 444 |
-
fp.write(b"%%Pages: 1\n")
|
| 445 |
-
fp.write(b"%%EndComments\n")
|
| 446 |
-
fp.write(b"%%Page: 1 1\n")
|
| 447 |
-
fp.write(b"%%ImageData: %d %d " % im.size)
|
| 448 |
-
fp.write(b'%d %d 0 1 1 "%s"\n' % operator)
|
| 449 |
-
|
| 450 |
-
# image header
|
| 451 |
-
fp.write(b"gsave\n")
|
| 452 |
-
fp.write(b"10 dict begin\n")
|
| 453 |
-
fp.write(b"/buf %d string def\n" % (im.size[0] * operator[1]))
|
| 454 |
-
fp.write(b"%d %d scale\n" % im.size)
|
| 455 |
-
fp.write(b"%d %d 8\n" % im.size) # <= bits
|
| 456 |
-
fp.write(b"[%d 0 0 -%d 0 %d]\n" % (im.size[0], im.size[1], im.size[1]))
|
| 457 |
-
fp.write(b"{ currentfile buf readhexstring pop } bind\n")
|
| 458 |
-
fp.write(operator[2] + b"\n")
|
| 459 |
-
if hasattr(fp, "flush"):
|
| 460 |
-
fp.flush()
|
| 461 |
-
|
| 462 |
-
ImageFile._save(im, fp, [ImageFile._Tile("eps", (0, 0) + im.size)])
|
| 463 |
-
|
| 464 |
-
fp.write(b"\n%%%%EndBinary\n")
|
| 465 |
-
fp.write(b"grestore end\n")
|
| 466 |
-
if hasattr(fp, "flush"):
|
| 467 |
-
fp.flush()
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
# --------------------------------------------------------------------
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
Image.register_open(EpsImageFile.format, EpsImageFile, _accept)
|
| 474 |
-
|
| 475 |
-
Image.register_save(EpsImageFile.format, _save)
|
| 476 |
-
|
| 477 |
-
Image.register_extensions(EpsImageFile.format, [".ps", ".eps"])
|
| 478 |
-
|
| 479 |
-
Image.register_mime(EpsImageFile.format, "application/postscript")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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