- Настроена среда на Ubuntu 24.04: проверены Node.js и PostgreSQL, выполнен npm run install:all.
- Пересобран bcrypt под Linux (npm rebuild / переустановка), после чего успешно отработал prisma seed (созданы пользователи и загружены СБЦ). - Создано виртуальное окружение backend/pdf_generator/venv и установлены зависимости из requirements.txt; backend настроен использовать python из этого venv при генерации PDF (pdf-python.service.ts). - Переключён AI-провайдер с LMStudio на Ollama: в ai.service.ts добавлена поддержка Ollama, в .env и .env.example выставлены AI_PROVIDER=ollama, OLLAMA_API_URL=http://192.168.88.160:11434 и OLLAMA_MODEL=gemma3n:e4b. - Перезапущены backend и frontend; проверено, что API /api/health отвечает, а Ollama (gemma3n:e4b) успешно обрабатывает chat-запросы.
This commit is contained in:
BIN
backend/pdf_generator/fonts/PTSans-Bold.ttf
Normal file
BIN
backend/pdf_generator/fonts/PTSans-Bold.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/PTSans-BoldItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/PTSans-BoldItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/PTSans-Italic.ttf
Normal file
BIN
backend/pdf_generator/fonts/PTSans-Italic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/PTSans-Regular.ttf
Normal file
BIN
backend/pdf_generator/fonts/PTSans-Regular.ttf
Normal file
Binary file not shown.
@@ -1,17 +1,5 @@
|
|||||||
# Шрифты для PDF сметы
|
# Шрифты для PDF
|
||||||
|
|
||||||
Для корректного отображения кириллицы в PDF положите сюда TTF-шрифты с поддержкой русского языка.
|
Для генерации PDF с кириллицей используются шрифты PT Sans из npm-пакета `@fontsource/pt-sans` (файлы в `node_modules/@fontsource/pt-sans/files/`).
|
||||||
|
|
||||||
**Варианты:**
|
При необходимости можно положить сюда свои TTF/WOFF (например, PTSans-Regular.ttf и PTSans-Bold.ttf) — тогда в `pdf.service.ts` нужно указать путь к этой папке через `path.join(process.cwd(), 'fonts', '...')`.
|
||||||
|
|
||||||
1. **PT Sans** (рекомендуется, лицензия OFL)
|
|
||||||
Скачайте с [Google Fonts](https://fonts.google.com/specimen/PT+Sans) и поместите в эту папку:
|
|
||||||
- `PTSans-Regular.ttf`
|
|
||||||
- `PTSans-Bold.ttf`
|
|
||||||
|
|
||||||
2. **Arial**
|
|
||||||
На Windows можно скопировать из `C:\Windows\Fonts\arial.ttf` (и при необходимости arialbd.ttf для жирного).
|
|
||||||
|
|
||||||
3. Любой другой TTF с кириллицей (например, DejaVu Sans, Open Sans).
|
|
||||||
|
|
||||||
После добавления шрифтов перезапустите backend.
|
|
||||||
|
|||||||
BIN
backend/pdf_generator/fonts/Raleway-Black.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-Black.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-BlackItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-BlackItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-Bold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-Bold.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-BoldItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-BoldItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-ExtraBold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-ExtraBold.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-ExtraBoldItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-ExtraBoldItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-ExtraLight.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-ExtraLight.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-ExtraLightItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-ExtraLightItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-Italic-VariableFont_wght.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-Italic-VariableFont_wght.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-Italic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-Italic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-Light.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-Light.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-LightItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-LightItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-Medium.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-Medium.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-MediumItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-MediumItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-Regular.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-Regular.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-SemiBold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-SemiBold.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-SemiBoldItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-SemiBoldItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-Thin.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-Thin.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-ThinItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-ThinItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Raleway-VariableFont_wght.ttf
Normal file
BIN
backend/pdf_generator/fonts/Raleway-VariableFont_wght.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-Black.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-Black.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-BlackItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-BlackItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-Bold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-Bold.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-BoldItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-BoldItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-ExtraBold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-ExtraBold.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-ExtraBoldItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-ExtraBoldItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-ExtraLight.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-ExtraLight.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-ExtraLightItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-ExtraLightItalic.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-Italic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-Italic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-Light.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-Light.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-LightItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-LightItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-Medium.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-Medium.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-MediumItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-MediumItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-Regular.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-Regular.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-SemiBold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-SemiBold.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-SemiBoldItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-SemiBoldItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-Thin.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-Thin.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-ThinItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-ThinItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto-VariableFont_wdth,wght.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto-VariableFont_wdth,wght.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Black.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Black.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-BlackItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-BlackItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Bold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Bold.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-BoldItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-BoldItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-ExtraBold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-ExtraBold.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-ExtraBoldItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-ExtraBoldItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-ExtraLight.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-ExtraLight.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Italic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Italic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Light.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Light.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-LightItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-LightItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Medium.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Medium.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-MediumItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-MediumItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Regular.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Regular.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-SemiBold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-SemiBold.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-SemiBoldItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-SemiBoldItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Thin.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-Thin.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_Condensed-ThinItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_Condensed-ThinItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Black.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Black.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-BlackItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-BlackItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Bold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Bold.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-BoldItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-BoldItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-ExtraBold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-ExtraBold.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-ExtraLight.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-ExtraLight.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Italic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Italic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Light.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Light.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-LightItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-LightItalic.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Medium.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Medium.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Regular.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Regular.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-SemiBold.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-SemiBold.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Thin.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-Thin.ttf
Normal file
Binary file not shown.
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-ThinItalic.ttf
Normal file
BIN
backend/pdf_generator/fonts/Roboto_SemiCondensed-ThinItalic.ttf
Normal file
Binary file not shown.
247
backend/pdf_generator/venv/bin/Activate.ps1
Normal file
247
backend/pdf_generator/venv/bin/Activate.ps1
Normal file
@@ -0,0 +1,247 @@
|
|||||||
|
<#
|
||||||
|
.Synopsis
|
||||||
|
Activate a Python virtual environment for the current PowerShell session.
|
||||||
|
|
||||||
|
.Description
|
||||||
|
Pushes the python executable for a virtual environment to the front of the
|
||||||
|
$Env:PATH environment variable and sets the prompt to signify that you are
|
||||||
|
in a Python virtual environment. Makes use of the command line switches as
|
||||||
|
well as the `pyvenv.cfg` file values present in the virtual environment.
|
||||||
|
|
||||||
|
.Parameter VenvDir
|
||||||
|
Path to the directory that contains the virtual environment to activate. The
|
||||||
|
default value for this is the parent of the directory that the Activate.ps1
|
||||||
|
script is located within.
|
||||||
|
|
||||||
|
.Parameter Prompt
|
||||||
|
The prompt prefix to display when this virtual environment is activated. By
|
||||||
|
default, this prompt is the name of the virtual environment folder (VenvDir)
|
||||||
|
surrounded by parentheses and followed by a single space (ie. '(.venv) ').
|
||||||
|
|
||||||
|
.Example
|
||||||
|
Activate.ps1
|
||||||
|
Activates the Python virtual environment that contains the Activate.ps1 script.
|
||||||
|
|
||||||
|
.Example
|
||||||
|
Activate.ps1 -Verbose
|
||||||
|
Activates the Python virtual environment that contains the Activate.ps1 script,
|
||||||
|
and shows extra information about the activation as it executes.
|
||||||
|
|
||||||
|
.Example
|
||||||
|
Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv
|
||||||
|
Activates the Python virtual environment located in the specified location.
|
||||||
|
|
||||||
|
.Example
|
||||||
|
Activate.ps1 -Prompt "MyPython"
|
||||||
|
Activates the Python virtual environment that contains the Activate.ps1 script,
|
||||||
|
and prefixes the current prompt with the specified string (surrounded in
|
||||||
|
parentheses) while the virtual environment is active.
|
||||||
|
|
||||||
|
.Notes
|
||||||
|
On Windows, it may be required to enable this Activate.ps1 script by setting the
|
||||||
|
execution policy for the user. You can do this by issuing the following PowerShell
|
||||||
|
command:
|
||||||
|
|
||||||
|
PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
|
||||||
|
|
||||||
|
For more information on Execution Policies:
|
||||||
|
https://go.microsoft.com/fwlink/?LinkID=135170
|
||||||
|
|
||||||
|
#>
|
||||||
|
Param(
|
||||||
|
[Parameter(Mandatory = $false)]
|
||||||
|
[String]
|
||||||
|
$VenvDir,
|
||||||
|
[Parameter(Mandatory = $false)]
|
||||||
|
[String]
|
||||||
|
$Prompt
|
||||||
|
)
|
||||||
|
|
||||||
|
<# Function declarations --------------------------------------------------- #>
|
||||||
|
|
||||||
|
<#
|
||||||
|
.Synopsis
|
||||||
|
Remove all shell session elements added by the Activate script, including the
|
||||||
|
addition of the virtual environment's Python executable from the beginning of
|
||||||
|
the PATH variable.
|
||||||
|
|
||||||
|
.Parameter NonDestructive
|
||||||
|
If present, do not remove this function from the global namespace for the
|
||||||
|
session.
|
||||||
|
|
||||||
|
#>
|
||||||
|
function global:deactivate ([switch]$NonDestructive) {
|
||||||
|
# Revert to original values
|
||||||
|
|
||||||
|
# The prior prompt:
|
||||||
|
if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) {
|
||||||
|
Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt
|
||||||
|
Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT
|
||||||
|
}
|
||||||
|
|
||||||
|
# The prior PYTHONHOME:
|
||||||
|
if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) {
|
||||||
|
Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME
|
||||||
|
Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME
|
||||||
|
}
|
||||||
|
|
||||||
|
# The prior PATH:
|
||||||
|
if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) {
|
||||||
|
Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH
|
||||||
|
Remove-Item -Path Env:_OLD_VIRTUAL_PATH
|
||||||
|
}
|
||||||
|
|
||||||
|
# Just remove the VIRTUAL_ENV altogether:
|
||||||
|
if (Test-Path -Path Env:VIRTUAL_ENV) {
|
||||||
|
Remove-Item -Path env:VIRTUAL_ENV
|
||||||
|
}
|
||||||
|
|
||||||
|
# Just remove VIRTUAL_ENV_PROMPT altogether.
|
||||||
|
if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) {
|
||||||
|
Remove-Item -Path env:VIRTUAL_ENV_PROMPT
|
||||||
|
}
|
||||||
|
|
||||||
|
# Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether:
|
||||||
|
if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) {
|
||||||
|
Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force
|
||||||
|
}
|
||||||
|
|
||||||
|
# Leave deactivate function in the global namespace if requested:
|
||||||
|
if (-not $NonDestructive) {
|
||||||
|
Remove-Item -Path function:deactivate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
<#
|
||||||
|
.Description
|
||||||
|
Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the
|
||||||
|
given folder, and returns them in a map.
|
||||||
|
|
||||||
|
For each line in the pyvenv.cfg file, if that line can be parsed into exactly
|
||||||
|
two strings separated by `=` (with any amount of whitespace surrounding the =)
|
||||||
|
then it is considered a `key = value` line. The left hand string is the key,
|
||||||
|
the right hand is the value.
|
||||||
|
|
||||||
|
If the value starts with a `'` or a `"` then the first and last character is
|
||||||
|
stripped from the value before being captured.
|
||||||
|
|
||||||
|
.Parameter ConfigDir
|
||||||
|
Path to the directory that contains the `pyvenv.cfg` file.
|
||||||
|
#>
|
||||||
|
function Get-PyVenvConfig(
|
||||||
|
[String]
|
||||||
|
$ConfigDir
|
||||||
|
) {
|
||||||
|
Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg"
|
||||||
|
|
||||||
|
# Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue).
|
||||||
|
$pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue
|
||||||
|
|
||||||
|
# An empty map will be returned if no config file is found.
|
||||||
|
$pyvenvConfig = @{ }
|
||||||
|
|
||||||
|
if ($pyvenvConfigPath) {
|
||||||
|
|
||||||
|
Write-Verbose "File exists, parse `key = value` lines"
|
||||||
|
$pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath
|
||||||
|
|
||||||
|
$pyvenvConfigContent | ForEach-Object {
|
||||||
|
$keyval = $PSItem -split "\s*=\s*", 2
|
||||||
|
if ($keyval[0] -and $keyval[1]) {
|
||||||
|
$val = $keyval[1]
|
||||||
|
|
||||||
|
# Remove extraneous quotations around a string value.
|
||||||
|
if ("'""".Contains($val.Substring(0, 1))) {
|
||||||
|
$val = $val.Substring(1, $val.Length - 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
$pyvenvConfig[$keyval[0]] = $val
|
||||||
|
Write-Verbose "Adding Key: '$($keyval[0])'='$val'"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $pyvenvConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
<# Begin Activate script --------------------------------------------------- #>
|
||||||
|
|
||||||
|
# Determine the containing directory of this script
|
||||||
|
$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
|
||||||
|
$VenvExecDir = Get-Item -Path $VenvExecPath
|
||||||
|
|
||||||
|
Write-Verbose "Activation script is located in path: '$VenvExecPath'"
|
||||||
|
Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)"
|
||||||
|
Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)"
|
||||||
|
|
||||||
|
# Set values required in priority: CmdLine, ConfigFile, Default
|
||||||
|
# First, get the location of the virtual environment, it might not be
|
||||||
|
# VenvExecDir if specified on the command line.
|
||||||
|
if ($VenvDir) {
|
||||||
|
Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir."
|
||||||
|
$VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/")
|
||||||
|
Write-Verbose "VenvDir=$VenvDir"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Next, read the `pyvenv.cfg` file to determine any required value such
|
||||||
|
# as `prompt`.
|
||||||
|
$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir
|
||||||
|
|
||||||
|
# Next, set the prompt from the command line, or the config file, or
|
||||||
|
# just use the name of the virtual environment folder.
|
||||||
|
if ($Prompt) {
|
||||||
|
Write-Verbose "Prompt specified as argument, using '$Prompt'"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value"
|
||||||
|
if ($pyvenvCfg -and $pyvenvCfg['prompt']) {
|
||||||
|
Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'"
|
||||||
|
$Prompt = $pyvenvCfg['prompt'];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)"
|
||||||
|
Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'"
|
||||||
|
$Prompt = Split-Path -Path $venvDir -Leaf
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Verbose "Prompt = '$Prompt'"
|
||||||
|
Write-Verbose "VenvDir='$VenvDir'"
|
||||||
|
|
||||||
|
# Deactivate any currently active virtual environment, but leave the
|
||||||
|
# deactivate function in place.
|
||||||
|
deactivate -nondestructive
|
||||||
|
|
||||||
|
# Now set the environment variable VIRTUAL_ENV, used by many tools to determine
|
||||||
|
# that there is an activated venv.
|
||||||
|
$env:VIRTUAL_ENV = $VenvDir
|
||||||
|
|
||||||
|
if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) {
|
||||||
|
|
||||||
|
Write-Verbose "Setting prompt to '$Prompt'"
|
||||||
|
|
||||||
|
# Set the prompt to include the env name
|
||||||
|
# Make sure _OLD_VIRTUAL_PROMPT is global
|
||||||
|
function global:_OLD_VIRTUAL_PROMPT { "" }
|
||||||
|
Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT
|
||||||
|
New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt
|
||||||
|
|
||||||
|
function global:prompt {
|
||||||
|
Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) "
|
||||||
|
_OLD_VIRTUAL_PROMPT
|
||||||
|
}
|
||||||
|
$env:VIRTUAL_ENV_PROMPT = $Prompt
|
||||||
|
}
|
||||||
|
|
||||||
|
# Clear PYTHONHOME
|
||||||
|
if (Test-Path -Path Env:PYTHONHOME) {
|
||||||
|
Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME
|
||||||
|
Remove-Item -Path Env:PYTHONHOME
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add the venv to the PATH
|
||||||
|
Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH
|
||||||
|
$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH"
|
||||||
70
backend/pdf_generator/venv/bin/activate
Normal file
70
backend/pdf_generator/venv/bin/activate
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
# This file must be used with "source bin/activate" *from bash*
|
||||||
|
# You cannot run it directly
|
||||||
|
|
||||||
|
deactivate () {
|
||||||
|
# reset old environment variables
|
||||||
|
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
|
||||||
|
PATH="${_OLD_VIRTUAL_PATH:-}"
|
||||||
|
export PATH
|
||||||
|
unset _OLD_VIRTUAL_PATH
|
||||||
|
fi
|
||||||
|
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
|
||||||
|
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
|
||||||
|
export PYTHONHOME
|
||||||
|
unset _OLD_VIRTUAL_PYTHONHOME
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Call hash to forget past commands. Without forgetting
|
||||||
|
# past commands the $PATH changes we made may not be respected
|
||||||
|
hash -r 2> /dev/null
|
||||||
|
|
||||||
|
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
|
||||||
|
PS1="${_OLD_VIRTUAL_PS1:-}"
|
||||||
|
export PS1
|
||||||
|
unset _OLD_VIRTUAL_PS1
|
||||||
|
fi
|
||||||
|
|
||||||
|
unset VIRTUAL_ENV
|
||||||
|
unset VIRTUAL_ENV_PROMPT
|
||||||
|
if [ ! "${1:-}" = "nondestructive" ] ; then
|
||||||
|
# Self destruct!
|
||||||
|
unset -f deactivate
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# unset irrelevant variables
|
||||||
|
deactivate nondestructive
|
||||||
|
|
||||||
|
# on Windows, a path can contain colons and backslashes and has to be converted:
|
||||||
|
if [ "${OSTYPE:-}" = "cygwin" ] || [ "${OSTYPE:-}" = "msys" ] ; then
|
||||||
|
# transform D:\path\to\venv to /d/path/to/venv on MSYS
|
||||||
|
# and to /cygdrive/d/path/to/venv on Cygwin
|
||||||
|
export VIRTUAL_ENV=$(cygpath /home/its/suety-sasha/geo/backend/pdf_generator/venv)
|
||||||
|
else
|
||||||
|
# use the path as-is
|
||||||
|
export VIRTUAL_ENV=/home/its/suety-sasha/geo/backend/pdf_generator/venv
|
||||||
|
fi
|
||||||
|
|
||||||
|
_OLD_VIRTUAL_PATH="$PATH"
|
||||||
|
PATH="$VIRTUAL_ENV/"bin":$PATH"
|
||||||
|
export PATH
|
||||||
|
|
||||||
|
# unset PYTHONHOME if set
|
||||||
|
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
|
||||||
|
# could use `if (set -u; : $PYTHONHOME) ;` in bash
|
||||||
|
if [ -n "${PYTHONHOME:-}" ] ; then
|
||||||
|
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
|
||||||
|
unset PYTHONHOME
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
|
||||||
|
_OLD_VIRTUAL_PS1="${PS1:-}"
|
||||||
|
PS1='(venv) '"${PS1:-}"
|
||||||
|
export PS1
|
||||||
|
VIRTUAL_ENV_PROMPT='(venv) '
|
||||||
|
export VIRTUAL_ENV_PROMPT
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Call hash to forget past commands. Without forgetting
|
||||||
|
# past commands the $PATH changes we made may not be respected
|
||||||
|
hash -r 2> /dev/null
|
||||||
27
backend/pdf_generator/venv/bin/activate.csh
Normal file
27
backend/pdf_generator/venv/bin/activate.csh
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# This file must be used with "source bin/activate.csh" *from csh*.
|
||||||
|
# You cannot run it directly.
|
||||||
|
|
||||||
|
# Created by Davide Di Blasi <davidedb@gmail.com>.
|
||||||
|
# Ported to Python 3.3 venv by Andrew Svetlov <andrew.svetlov@gmail.com>
|
||||||
|
|
||||||
|
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'
|
||||||
|
|
||||||
|
# Unset irrelevant variables.
|
||||||
|
deactivate nondestructive
|
||||||
|
|
||||||
|
setenv VIRTUAL_ENV /home/its/suety-sasha/geo/backend/pdf_generator/venv
|
||||||
|
|
||||||
|
set _OLD_VIRTUAL_PATH="$PATH"
|
||||||
|
setenv PATH "$VIRTUAL_ENV/"bin":$PATH"
|
||||||
|
|
||||||
|
|
||||||
|
set _OLD_VIRTUAL_PROMPT="$prompt"
|
||||||
|
|
||||||
|
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
|
||||||
|
set prompt = '(venv) '"$prompt"
|
||||||
|
setenv VIRTUAL_ENV_PROMPT '(venv) '
|
||||||
|
endif
|
||||||
|
|
||||||
|
alias pydoc python -m pydoc
|
||||||
|
|
||||||
|
rehash
|
||||||
69
backend/pdf_generator/venv/bin/activate.fish
Normal file
69
backend/pdf_generator/venv/bin/activate.fish
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
# This file must be used with "source <venv>/bin/activate.fish" *from fish*
|
||||||
|
# (https://fishshell.com/). You cannot run it directly.
|
||||||
|
|
||||||
|
function deactivate -d "Exit virtual environment and return to normal shell environment"
|
||||||
|
# reset old environment variables
|
||||||
|
if test -n "$_OLD_VIRTUAL_PATH"
|
||||||
|
set -gx PATH $_OLD_VIRTUAL_PATH
|
||||||
|
set -e _OLD_VIRTUAL_PATH
|
||||||
|
end
|
||||||
|
if test -n "$_OLD_VIRTUAL_PYTHONHOME"
|
||||||
|
set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME
|
||||||
|
set -e _OLD_VIRTUAL_PYTHONHOME
|
||||||
|
end
|
||||||
|
|
||||||
|
if test -n "$_OLD_FISH_PROMPT_OVERRIDE"
|
||||||
|
set -e _OLD_FISH_PROMPT_OVERRIDE
|
||||||
|
# prevents error when using nested fish instances (Issue #93858)
|
||||||
|
if functions -q _old_fish_prompt
|
||||||
|
functions -e fish_prompt
|
||||||
|
functions -c _old_fish_prompt fish_prompt
|
||||||
|
functions -e _old_fish_prompt
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
set -e VIRTUAL_ENV
|
||||||
|
set -e VIRTUAL_ENV_PROMPT
|
||||||
|
if test "$argv[1]" != "nondestructive"
|
||||||
|
# Self-destruct!
|
||||||
|
functions -e deactivate
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Unset irrelevant variables.
|
||||||
|
deactivate nondestructive
|
||||||
|
|
||||||
|
set -gx VIRTUAL_ENV /home/its/suety-sasha/geo/backend/pdf_generator/venv
|
||||||
|
|
||||||
|
set -gx _OLD_VIRTUAL_PATH $PATH
|
||||||
|
set -gx PATH "$VIRTUAL_ENV/"bin $PATH
|
||||||
|
|
||||||
|
# Unset PYTHONHOME if set.
|
||||||
|
if set -q PYTHONHOME
|
||||||
|
set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME
|
||||||
|
set -e PYTHONHOME
|
||||||
|
end
|
||||||
|
|
||||||
|
if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
|
||||||
|
# fish uses a function instead of an env var to generate the prompt.
|
||||||
|
|
||||||
|
# Save the current fish_prompt function as the function _old_fish_prompt.
|
||||||
|
functions -c fish_prompt _old_fish_prompt
|
||||||
|
|
||||||
|
# With the original prompt function renamed, we can override with our own.
|
||||||
|
function fish_prompt
|
||||||
|
# Save the return status of the last command.
|
||||||
|
set -l old_status $status
|
||||||
|
|
||||||
|
# Output the venv prompt; color taken from the blue of the Python logo.
|
||||||
|
printf "%s%s%s" (set_color 4B8BBE) '(venv) ' (set_color normal)
|
||||||
|
|
||||||
|
# Restore the return status of the previous command.
|
||||||
|
echo "exit $old_status" | .
|
||||||
|
# Output the original/"old" prompt.
|
||||||
|
_old_fish_prompt
|
||||||
|
end
|
||||||
|
|
||||||
|
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
|
||||||
|
set -gx VIRTUAL_ENV_PROMPT '(venv) '
|
||||||
|
end
|
||||||
8
backend/pdf_generator/venv/bin/normalizer
Executable file
8
backend/pdf_generator/venv/bin/normalizer
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
#!/home/its/suety-sasha/geo/backend/pdf_generator/venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from charset_normalizer.cli import cli_detect
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(cli_detect())
|
||||||
8
backend/pdf_generator/venv/bin/pip
Executable file
8
backend/pdf_generator/venv/bin/pip
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
#!/home/its/suety-sasha/geo/backend/pdf_generator/venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from pip._internal.cli.main import main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(main())
|
||||||
8
backend/pdf_generator/venv/bin/pip3
Executable file
8
backend/pdf_generator/venv/bin/pip3
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
#!/home/its/suety-sasha/geo/backend/pdf_generator/venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from pip._internal.cli.main import main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(main())
|
||||||
8
backend/pdf_generator/venv/bin/pip3.12
Executable file
8
backend/pdf_generator/venv/bin/pip3.12
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
#!/home/its/suety-sasha/geo/backend/pdf_generator/venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from pip._internal.cli.main import main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(main())
|
||||||
1
backend/pdf_generator/venv/bin/python
Symbolic link
1
backend/pdf_generator/venv/bin/python
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
python3
|
||||||
1
backend/pdf_generator/venv/bin/python3
Symbolic link
1
backend/pdf_generator/venv/bin/python3
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
/usr/bin/python3
|
||||||
1
backend/pdf_generator/venv/bin/python3.12
Symbolic link
1
backend/pdf_generator/venv/bin/python3.12
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
python3
|
||||||
@@ -0,0 +1,293 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from io import BytesIO
|
||||||
|
from typing import IO
|
||||||
|
|
||||||
|
from . import ExifTags, Image, ImageFile
|
||||||
|
|
||||||
|
try:
|
||||||
|
from . import _avif
|
||||||
|
|
||||||
|
SUPPORTED = True
|
||||||
|
except ImportError:
|
||||||
|
SUPPORTED = False
|
||||||
|
|
||||||
|
# Decoder options as module globals, until there is a way to pass parameters
|
||||||
|
# to Image.open (see https://github.com/python-pillow/Pillow/issues/569)
|
||||||
|
DECODE_CODEC_CHOICE = "auto"
|
||||||
|
DEFAULT_MAX_THREADS = 0
|
||||||
|
|
||||||
|
|
||||||
|
def get_codec_version(codec_name: str) -> str | None:
|
||||||
|
versions = _avif.codec_versions()
|
||||||
|
for version in versions.split(", "):
|
||||||
|
if version.split(" [")[0] == codec_name:
|
||||||
|
return version.split(":")[-1].split(" ")[0]
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _accept(prefix: bytes) -> bool | str:
|
||||||
|
if prefix[4:8] != b"ftyp":
|
||||||
|
return False
|
||||||
|
major_brand = prefix[8:12]
|
||||||
|
if major_brand in (
|
||||||
|
# coding brands
|
||||||
|
b"avif",
|
||||||
|
b"avis",
|
||||||
|
# We accept files with AVIF container brands; we can't yet know if
|
||||||
|
# the ftyp box has the correct compatible brands, but if it doesn't
|
||||||
|
# then the plugin will raise a SyntaxError which Pillow will catch
|
||||||
|
# before moving on to the next plugin that accepts the file.
|
||||||
|
#
|
||||||
|
# Also, because this file might not actually be an AVIF file, we
|
||||||
|
# don't raise an error if AVIF support isn't properly compiled.
|
||||||
|
b"mif1",
|
||||||
|
b"msf1",
|
||||||
|
):
|
||||||
|
if not SUPPORTED:
|
||||||
|
return (
|
||||||
|
"image file could not be identified because AVIF support not installed"
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _get_default_max_threads() -> int:
|
||||||
|
if DEFAULT_MAX_THREADS:
|
||||||
|
return DEFAULT_MAX_THREADS
|
||||||
|
if hasattr(os, "sched_getaffinity"):
|
||||||
|
return len(os.sched_getaffinity(0))
|
||||||
|
else:
|
||||||
|
return os.cpu_count() or 1
|
||||||
|
|
||||||
|
|
||||||
|
class AvifImageFile(ImageFile.ImageFile):
|
||||||
|
format = "AVIF"
|
||||||
|
format_description = "AVIF image"
|
||||||
|
__frame = -1
|
||||||
|
|
||||||
|
def _open(self) -> None:
|
||||||
|
if not SUPPORTED:
|
||||||
|
msg = "image file could not be opened because AVIF support not installed"
|
||||||
|
raise SyntaxError(msg)
|
||||||
|
|
||||||
|
if DECODE_CODEC_CHOICE != "auto" and not _avif.decoder_codec_available(
|
||||||
|
DECODE_CODEC_CHOICE
|
||||||
|
):
|
||||||
|
msg = "Invalid opening codec"
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
assert self.fp is not None
|
||||||
|
self._decoder = _avif.AvifDecoder(
|
||||||
|
self.fp.read(),
|
||||||
|
DECODE_CODEC_CHOICE,
|
||||||
|
_get_default_max_threads(),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get info from decoder
|
||||||
|
self._size, self.n_frames, self._mode, icc, exif, exif_orientation, xmp = (
|
||||||
|
self._decoder.get_info()
|
||||||
|
)
|
||||||
|
self.is_animated = self.n_frames > 1
|
||||||
|
|
||||||
|
if icc:
|
||||||
|
self.info["icc_profile"] = icc
|
||||||
|
if xmp:
|
||||||
|
self.info["xmp"] = xmp
|
||||||
|
|
||||||
|
if exif_orientation != 1 or exif:
|
||||||
|
exif_data = Image.Exif()
|
||||||
|
if exif:
|
||||||
|
exif_data.load(exif)
|
||||||
|
original_orientation = exif_data.get(ExifTags.Base.Orientation, 1)
|
||||||
|
else:
|
||||||
|
original_orientation = 1
|
||||||
|
if exif_orientation != original_orientation:
|
||||||
|
exif_data[ExifTags.Base.Orientation] = exif_orientation
|
||||||
|
exif = exif_data.tobytes()
|
||||||
|
if exif:
|
||||||
|
self.info["exif"] = exif
|
||||||
|
self.seek(0)
|
||||||
|
|
||||||
|
def seek(self, frame: int) -> None:
|
||||||
|
if not self._seek_check(frame):
|
||||||
|
return
|
||||||
|
|
||||||
|
# Set tile
|
||||||
|
self.__frame = frame
|
||||||
|
self.tile = [ImageFile._Tile("raw", (0, 0) + self.size, 0, self.mode)]
|
||||||
|
|
||||||
|
def load(self) -> Image.core.PixelAccess | None:
|
||||||
|
if self.tile:
|
||||||
|
# We need to load the image data for this frame
|
||||||
|
data, timescale, pts_in_timescales, duration_in_timescales = (
|
||||||
|
self._decoder.get_frame(self.__frame)
|
||||||
|
)
|
||||||
|
self.info["timestamp"] = round(1000 * (pts_in_timescales / timescale))
|
||||||
|
self.info["duration"] = round(1000 * (duration_in_timescales / timescale))
|
||||||
|
|
||||||
|
if self.fp and self._exclusive_fp:
|
||||||
|
self.fp.close()
|
||||||
|
self.fp = BytesIO(data)
|
||||||
|
|
||||||
|
return super().load()
|
||||||
|
|
||||||
|
def load_seek(self, pos: int) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def tell(self) -> int:
|
||||||
|
return self.__frame
|
||||||
|
|
||||||
|
|
||||||
|
def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
|
_save(im, fp, filename, save_all=True)
|
||||||
|
|
||||||
|
|
||||||
|
def _save(
|
||||||
|
im: Image.Image, fp: IO[bytes], filename: str | bytes, save_all: bool = False
|
||||||
|
) -> None:
|
||||||
|
info = im.encoderinfo.copy()
|
||||||
|
if save_all:
|
||||||
|
append_images = list(info.get("append_images", []))
|
||||||
|
else:
|
||||||
|
append_images = []
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
for ims in [im] + append_images:
|
||||||
|
total += getattr(ims, "n_frames", 1)
|
||||||
|
|
||||||
|
quality = info.get("quality", 75)
|
||||||
|
if not isinstance(quality, int) or quality < 0 or quality > 100:
|
||||||
|
msg = "Invalid quality setting"
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
duration = info.get("duration", 0)
|
||||||
|
subsampling = info.get("subsampling", "4:2:0")
|
||||||
|
speed = info.get("speed", 6)
|
||||||
|
max_threads = info.get("max_threads", _get_default_max_threads())
|
||||||
|
codec = info.get("codec", "auto")
|
||||||
|
if codec != "auto" and not _avif.encoder_codec_available(codec):
|
||||||
|
msg = "Invalid saving codec"
|
||||||
|
raise ValueError(msg)
|
||||||
|
range_ = info.get("range", "full")
|
||||||
|
tile_rows_log2 = info.get("tile_rows", 0)
|
||||||
|
tile_cols_log2 = info.get("tile_cols", 0)
|
||||||
|
alpha_premultiplied = bool(info.get("alpha_premultiplied", False))
|
||||||
|
autotiling = bool(info.get("autotiling", tile_rows_log2 == tile_cols_log2 == 0))
|
||||||
|
|
||||||
|
icc_profile = info.get("icc_profile", im.info.get("icc_profile"))
|
||||||
|
exif_orientation = 1
|
||||||
|
if exif := info.get("exif"):
|
||||||
|
if isinstance(exif, Image.Exif):
|
||||||
|
exif_data = exif
|
||||||
|
else:
|
||||||
|
exif_data = Image.Exif()
|
||||||
|
exif_data.load(exif)
|
||||||
|
if ExifTags.Base.Orientation in exif_data:
|
||||||
|
exif_orientation = exif_data.pop(ExifTags.Base.Orientation)
|
||||||
|
exif = exif_data.tobytes() if exif_data else b""
|
||||||
|
elif isinstance(exif, Image.Exif):
|
||||||
|
exif = exif_data.tobytes()
|
||||||
|
|
||||||
|
xmp = info.get("xmp")
|
||||||
|
|
||||||
|
if isinstance(xmp, str):
|
||||||
|
xmp = xmp.encode("utf-8")
|
||||||
|
|
||||||
|
advanced = info.get("advanced")
|
||||||
|
if advanced is not None:
|
||||||
|
if isinstance(advanced, dict):
|
||||||
|
advanced = advanced.items()
|
||||||
|
try:
|
||||||
|
advanced = tuple(advanced)
|
||||||
|
except TypeError:
|
||||||
|
invalid = True
|
||||||
|
else:
|
||||||
|
invalid = any(not isinstance(v, tuple) or len(v) != 2 for v in advanced)
|
||||||
|
if invalid:
|
||||||
|
msg = (
|
||||||
|
"advanced codec options must be a dict of key-value string "
|
||||||
|
"pairs or a series of key-value two-tuples"
|
||||||
|
)
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
# Setup the AVIF encoder
|
||||||
|
enc = _avif.AvifEncoder(
|
||||||
|
im.size,
|
||||||
|
subsampling,
|
||||||
|
quality,
|
||||||
|
speed,
|
||||||
|
max_threads,
|
||||||
|
codec,
|
||||||
|
range_,
|
||||||
|
tile_rows_log2,
|
||||||
|
tile_cols_log2,
|
||||||
|
alpha_premultiplied,
|
||||||
|
autotiling,
|
||||||
|
icc_profile or b"",
|
||||||
|
exif or b"",
|
||||||
|
exif_orientation,
|
||||||
|
xmp or b"",
|
||||||
|
advanced,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add each frame
|
||||||
|
frame_idx = 0
|
||||||
|
frame_duration = 0
|
||||||
|
cur_idx = im.tell()
|
||||||
|
is_single_frame = total == 1
|
||||||
|
try:
|
||||||
|
for ims in [im] + append_images:
|
||||||
|
# Get number of frames in this image
|
||||||
|
nfr = getattr(ims, "n_frames", 1)
|
||||||
|
|
||||||
|
for idx in range(nfr):
|
||||||
|
ims.seek(idx)
|
||||||
|
|
||||||
|
# Make sure image mode is supported
|
||||||
|
frame = ims
|
||||||
|
rawmode = ims.mode
|
||||||
|
if ims.mode not in {"RGB", "RGBA"}:
|
||||||
|
rawmode = "RGBA" if ims.has_transparency_data else "RGB"
|
||||||
|
frame = ims.convert(rawmode)
|
||||||
|
|
||||||
|
# Update frame duration
|
||||||
|
if isinstance(duration, (list, tuple)):
|
||||||
|
frame_duration = duration[frame_idx]
|
||||||
|
else:
|
||||||
|
frame_duration = duration
|
||||||
|
|
||||||
|
# Append the frame to the animation encoder
|
||||||
|
enc.add(
|
||||||
|
frame.tobytes("raw", rawmode),
|
||||||
|
frame_duration,
|
||||||
|
frame.size,
|
||||||
|
rawmode,
|
||||||
|
is_single_frame,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Update frame index
|
||||||
|
frame_idx += 1
|
||||||
|
|
||||||
|
if not save_all:
|
||||||
|
break
|
||||||
|
|
||||||
|
finally:
|
||||||
|
im.seek(cur_idx)
|
||||||
|
|
||||||
|
# Get the final output from the encoder
|
||||||
|
data = enc.finish()
|
||||||
|
if data is None:
|
||||||
|
msg = "cannot write file as AVIF (encoder returned None)"
|
||||||
|
raise OSError(msg)
|
||||||
|
|
||||||
|
fp.write(data)
|
||||||
|
|
||||||
|
|
||||||
|
Image.register_open(AvifImageFile.format, AvifImageFile, _accept)
|
||||||
|
if SUPPORTED:
|
||||||
|
Image.register_save(AvifImageFile.format, _save)
|
||||||
|
Image.register_save_all(AvifImageFile.format, _save_all)
|
||||||
|
Image.register_extensions(AvifImageFile.format, [".avif", ".avifs"])
|
||||||
|
Image.register_mime(AvifImageFile.format, "image/avif")
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
#
|
||||||
|
# The Python Imaging Library
|
||||||
|
# $Id$
|
||||||
|
#
|
||||||
|
# bitmap distribution font (bdf) file parser
|
||||||
|
#
|
||||||
|
# history:
|
||||||
|
# 1996-05-16 fl created (as bdf2pil)
|
||||||
|
# 1997-08-25 fl converted to FontFile driver
|
||||||
|
# 2001-05-25 fl removed bogus __init__ call
|
||||||
|
# 2002-11-20 fl robustification (from Kevin Cazabon, Dmitry Vasiliev)
|
||||||
|
# 2003-04-22 fl more robustification (from Graham Dumpleton)
|
||||||
|
#
|
||||||
|
# Copyright (c) 1997-2003 by Secret Labs AB.
|
||||||
|
# Copyright (c) 1997-2003 by Fredrik Lundh.
|
||||||
|
#
|
||||||
|
# See the README file for information on usage and redistribution.
|
||||||
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Parse X Bitmap Distribution Format (BDF)
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import BinaryIO
|
||||||
|
|
||||||
|
from . import FontFile, Image
|
||||||
|
|
||||||
|
|
||||||
|
def bdf_char(
|
||||||
|
f: BinaryIO,
|
||||||
|
) -> (
|
||||||
|
tuple[
|
||||||
|
str,
|
||||||
|
int,
|
||||||
|
tuple[tuple[int, int], tuple[int, int, int, int], tuple[int, int, int, int]],
|
||||||
|
Image.Image,
|
||||||
|
]
|
||||||
|
| None
|
||||||
|
):
|
||||||
|
# skip to STARTCHAR
|
||||||
|
while True:
|
||||||
|
s = f.readline()
|
||||||
|
if not s:
|
||||||
|
return None
|
||||||
|
if s.startswith(b"STARTCHAR"):
|
||||||
|
break
|
||||||
|
id = s[9:].strip().decode("ascii")
|
||||||
|
|
||||||
|
# load symbol properties
|
||||||
|
props = {}
|
||||||
|
while True:
|
||||||
|
s = f.readline()
|
||||||
|
if not s or s.startswith(b"BITMAP"):
|
||||||
|
break
|
||||||
|
i = s.find(b" ")
|
||||||
|
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
|
||||||
|
|
||||||
|
# load bitmap
|
||||||
|
bitmap = bytearray()
|
||||||
|
while True:
|
||||||
|
s = f.readline()
|
||||||
|
if not s or s.startswith(b"ENDCHAR"):
|
||||||
|
break
|
||||||
|
bitmap += s[:-1]
|
||||||
|
|
||||||
|
# The word BBX
|
||||||
|
# followed by the width in x (BBw), height in y (BBh),
|
||||||
|
# and x and y displacement (BBxoff0, BByoff0)
|
||||||
|
# of the lower left corner from the origin of the character.
|
||||||
|
width, height, x_disp, y_disp = (int(p) for p in props["BBX"].split())
|
||||||
|
|
||||||
|
# The word DWIDTH
|
||||||
|
# followed by the width in x and y of the character in device pixels.
|
||||||
|
dwx, dwy = (int(p) for p in props["DWIDTH"].split())
|
||||||
|
|
||||||
|
bbox = (
|
||||||
|
(dwx, dwy),
|
||||||
|
(x_disp, -y_disp - height, width + x_disp, -y_disp),
|
||||||
|
(0, 0, width, height),
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
im = Image.frombytes("1", (width, height), bitmap, "hex", "1")
|
||||||
|
except ValueError:
|
||||||
|
# deal with zero-width characters
|
||||||
|
im = Image.new("1", (width, height))
|
||||||
|
|
||||||
|
return id, int(props["ENCODING"]), bbox, im
|
||||||
|
|
||||||
|
|
||||||
|
class BdfFontFile(FontFile.FontFile):
|
||||||
|
"""Font file plugin for the X11 BDF format."""
|
||||||
|
|
||||||
|
def __init__(self, fp: BinaryIO) -> None:
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
s = fp.readline()
|
||||||
|
if not s.startswith(b"STARTFONT 2.1"):
|
||||||
|
msg = "not a valid BDF file"
|
||||||
|
raise SyntaxError(msg)
|
||||||
|
|
||||||
|
props = {}
|
||||||
|
comments = []
|
||||||
|
|
||||||
|
while True:
|
||||||
|
s = fp.readline()
|
||||||
|
if not s or s.startswith(b"ENDPROPERTIES"):
|
||||||
|
break
|
||||||
|
i = s.find(b" ")
|
||||||
|
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
|
||||||
|
if s[:i] in [b"COMMENT", b"COPYRIGHT"]:
|
||||||
|
if s.find(b"LogicalFontDescription") < 0:
|
||||||
|
comments.append(s[i + 1 : -1].decode("ascii"))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
c = bdf_char(fp)
|
||||||
|
if not c:
|
||||||
|
break
|
||||||
|
id, ch, (xy, dst, src), im = c
|
||||||
|
if 0 <= ch < len(self.glyph):
|
||||||
|
self.glyph[ch] = xy, dst, src, im
|
||||||
@@ -0,0 +1,498 @@
|
|||||||
|
"""
|
||||||
|
Blizzard Mipmap Format (.blp)
|
||||||
|
Jerome Leclanche <jerome@leclan.ch>
|
||||||
|
|
||||||
|
The contents of this file are hereby released in the public domain (CC0)
|
||||||
|
Full text of the CC0 license:
|
||||||
|
https://creativecommons.org/publicdomain/zero/1.0/
|
||||||
|
|
||||||
|
BLP1 files, used mostly in Warcraft III, are not fully supported.
|
||||||
|
All types of BLP2 files used in World of Warcraft are supported.
|
||||||
|
|
||||||
|
The BLP file structure consists of a header, up to 16 mipmaps of the
|
||||||
|
texture
|
||||||
|
|
||||||
|
Texture sizes must be powers of two, though the two dimensions do
|
||||||
|
not have to be equal; 512x256 is valid, but 512x200 is not.
|
||||||
|
The first mipmap (mipmap #0) is the full size image; each subsequent
|
||||||
|
mipmap halves both dimensions. The final mipmap should be 1x1.
|
||||||
|
|
||||||
|
BLP files come in many different flavours:
|
||||||
|
* JPEG-compressed (type == 0) - only supported for BLP1.
|
||||||
|
* RAW images (type == 1, encoding == 1). Each mipmap is stored as an
|
||||||
|
array of 8-bit values, one per pixel, left to right, top to bottom.
|
||||||
|
Each value is an index to the palette.
|
||||||
|
* DXT-compressed (type == 1, encoding == 2):
|
||||||
|
- DXT1 compression is used if alpha_encoding == 0.
|
||||||
|
- An additional alpha bit is used if alpha_depth == 1.
|
||||||
|
- DXT3 compression is used if alpha_encoding == 1.
|
||||||
|
- DXT5 compression is used if alpha_encoding == 7.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import abc
|
||||||
|
import os
|
||||||
|
import struct
|
||||||
|
from enum import IntEnum
|
||||||
|
from io import BytesIO
|
||||||
|
from typing import IO
|
||||||
|
|
||||||
|
from . import Image, ImageFile
|
||||||
|
|
||||||
|
|
||||||
|
class Format(IntEnum):
|
||||||
|
JPEG = 0
|
||||||
|
|
||||||
|
|
||||||
|
class Encoding(IntEnum):
|
||||||
|
UNCOMPRESSED = 1
|
||||||
|
DXT = 2
|
||||||
|
UNCOMPRESSED_RAW_BGRA = 3
|
||||||
|
|
||||||
|
|
||||||
|
class AlphaEncoding(IntEnum):
|
||||||
|
DXT1 = 0
|
||||||
|
DXT3 = 1
|
||||||
|
DXT5 = 7
|
||||||
|
|
||||||
|
|
||||||
|
def unpack_565(i: int) -> tuple[int, int, int]:
|
||||||
|
return ((i >> 11) & 0x1F) << 3, ((i >> 5) & 0x3F) << 2, (i & 0x1F) << 3
|
||||||
|
|
||||||
|
|
||||||
|
def decode_dxt1(
|
||||||
|
data: bytes, alpha: bool = False
|
||||||
|
) -> tuple[bytearray, bytearray, bytearray, bytearray]:
|
||||||
|
"""
|
||||||
|
input: one "row" of data (i.e. will produce 4*width pixels)
|
||||||
|
"""
|
||||||
|
|
||||||
|
blocks = len(data) // 8 # number of blocks in row
|
||||||
|
ret = (bytearray(), bytearray(), bytearray(), bytearray())
|
||||||
|
|
||||||
|
for block_index in range(blocks):
|
||||||
|
# Decode next 8-byte block.
|
||||||
|
idx = block_index * 8
|
||||||
|
color0, color1, bits = struct.unpack_from("<HHI", data, idx)
|
||||||
|
|
||||||
|
r0, g0, b0 = unpack_565(color0)
|
||||||
|
r1, g1, b1 = unpack_565(color1)
|
||||||
|
|
||||||
|
# Decode this block into 4x4 pixels
|
||||||
|
# Accumulate the results onto our 4 row accumulators
|
||||||
|
for j in range(4):
|
||||||
|
for i in range(4):
|
||||||
|
# get next control op and generate a pixel
|
||||||
|
|
||||||
|
control = bits & 3
|
||||||
|
bits = bits >> 2
|
||||||
|
|
||||||
|
a = 0xFF
|
||||||
|
if control == 0:
|
||||||
|
r, g, b = r0, g0, b0
|
||||||
|
elif control == 1:
|
||||||
|
r, g, b = r1, g1, b1
|
||||||
|
elif control == 2:
|
||||||
|
if color0 > color1:
|
||||||
|
r = (2 * r0 + r1) // 3
|
||||||
|
g = (2 * g0 + g1) // 3
|
||||||
|
b = (2 * b0 + b1) // 3
|
||||||
|
else:
|
||||||
|
r = (r0 + r1) // 2
|
||||||
|
g = (g0 + g1) // 2
|
||||||
|
b = (b0 + b1) // 2
|
||||||
|
elif control == 3:
|
||||||
|
if color0 > color1:
|
||||||
|
r = (2 * r1 + r0) // 3
|
||||||
|
g = (2 * g1 + g0) // 3
|
||||||
|
b = (2 * b1 + b0) // 3
|
||||||
|
else:
|
||||||
|
r, g, b, a = 0, 0, 0, 0
|
||||||
|
|
||||||
|
if alpha:
|
||||||
|
ret[j].extend([r, g, b, a])
|
||||||
|
else:
|
||||||
|
ret[j].extend([r, g, b])
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def decode_dxt3(data: bytes) -> tuple[bytearray, bytearray, bytearray, bytearray]:
|
||||||
|
"""
|
||||||
|
input: one "row" of data (i.e. will produce 4*width pixels)
|
||||||
|
"""
|
||||||
|
|
||||||
|
blocks = len(data) // 16 # number of blocks in row
|
||||||
|
ret = (bytearray(), bytearray(), bytearray(), bytearray())
|
||||||
|
|
||||||
|
for block_index in range(blocks):
|
||||||
|
idx = block_index * 16
|
||||||
|
block = data[idx : idx + 16]
|
||||||
|
# Decode next 16-byte block.
|
||||||
|
bits = struct.unpack_from("<8B", block)
|
||||||
|
color0, color1 = struct.unpack_from("<HH", block, 8)
|
||||||
|
|
||||||
|
(code,) = struct.unpack_from("<I", block, 12)
|
||||||
|
|
||||||
|
r0, g0, b0 = unpack_565(color0)
|
||||||
|
r1, g1, b1 = unpack_565(color1)
|
||||||
|
|
||||||
|
for j in range(4):
|
||||||
|
high = False # Do we want the higher bits?
|
||||||
|
for i in range(4):
|
||||||
|
alphacode_index = (4 * j + i) // 2
|
||||||
|
a = bits[alphacode_index]
|
||||||
|
if high:
|
||||||
|
high = False
|
||||||
|
a >>= 4
|
||||||
|
else:
|
||||||
|
high = True
|
||||||
|
a &= 0xF
|
||||||
|
a *= 17 # We get a value between 0 and 15
|
||||||
|
|
||||||
|
color_code = (code >> 2 * (4 * j + i)) & 0x03
|
||||||
|
|
||||||
|
if color_code == 0:
|
||||||
|
r, g, b = r0, g0, b0
|
||||||
|
elif color_code == 1:
|
||||||
|
r, g, b = r1, g1, b1
|
||||||
|
elif color_code == 2:
|
||||||
|
r = (2 * r0 + r1) // 3
|
||||||
|
g = (2 * g0 + g1) // 3
|
||||||
|
b = (2 * b0 + b1) // 3
|
||||||
|
elif color_code == 3:
|
||||||
|
r = (2 * r1 + r0) // 3
|
||||||
|
g = (2 * g1 + g0) // 3
|
||||||
|
b = (2 * b1 + b0) // 3
|
||||||
|
|
||||||
|
ret[j].extend([r, g, b, a])
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def decode_dxt5(data: bytes) -> tuple[bytearray, bytearray, bytearray, bytearray]:
|
||||||
|
"""
|
||||||
|
input: one "row" of data (i.e. will produce 4 * width pixels)
|
||||||
|
"""
|
||||||
|
|
||||||
|
blocks = len(data) // 16 # number of blocks in row
|
||||||
|
ret = (bytearray(), bytearray(), bytearray(), bytearray())
|
||||||
|
|
||||||
|
for block_index in range(blocks):
|
||||||
|
idx = block_index * 16
|
||||||
|
block = data[idx : idx + 16]
|
||||||
|
# Decode next 16-byte block.
|
||||||
|
a0, a1 = struct.unpack_from("<BB", block)
|
||||||
|
|
||||||
|
bits = struct.unpack_from("<6B", block, 2)
|
||||||
|
alphacode1 = bits[2] | (bits[3] << 8) | (bits[4] << 16) | (bits[5] << 24)
|
||||||
|
alphacode2 = bits[0] | (bits[1] << 8)
|
||||||
|
|
||||||
|
color0, color1 = struct.unpack_from("<HH", block, 8)
|
||||||
|
|
||||||
|
(code,) = struct.unpack_from("<I", block, 12)
|
||||||
|
|
||||||
|
r0, g0, b0 = unpack_565(color0)
|
||||||
|
r1, g1, b1 = unpack_565(color1)
|
||||||
|
|
||||||
|
for j in range(4):
|
||||||
|
for i in range(4):
|
||||||
|
# get next control op and generate a pixel
|
||||||
|
alphacode_index = 3 * (4 * j + i)
|
||||||
|
|
||||||
|
if alphacode_index <= 12:
|
||||||
|
alphacode = (alphacode2 >> alphacode_index) & 0x07
|
||||||
|
elif alphacode_index == 15:
|
||||||
|
alphacode = (alphacode2 >> 15) | ((alphacode1 << 1) & 0x06)
|
||||||
|
else: # alphacode_index >= 18 and alphacode_index <= 45
|
||||||
|
alphacode = (alphacode1 >> (alphacode_index - 16)) & 0x07
|
||||||
|
|
||||||
|
if alphacode == 0:
|
||||||
|
a = a0
|
||||||
|
elif alphacode == 1:
|
||||||
|
a = a1
|
||||||
|
elif a0 > a1:
|
||||||
|
a = ((8 - alphacode) * a0 + (alphacode - 1) * a1) // 7
|
||||||
|
elif alphacode == 6:
|
||||||
|
a = 0
|
||||||
|
elif alphacode == 7:
|
||||||
|
a = 255
|
||||||
|
else:
|
||||||
|
a = ((6 - alphacode) * a0 + (alphacode - 1) * a1) // 5
|
||||||
|
|
||||||
|
color_code = (code >> 2 * (4 * j + i)) & 0x03
|
||||||
|
|
||||||
|
if color_code == 0:
|
||||||
|
r, g, b = r0, g0, b0
|
||||||
|
elif color_code == 1:
|
||||||
|
r, g, b = r1, g1, b1
|
||||||
|
elif color_code == 2:
|
||||||
|
r = (2 * r0 + r1) // 3
|
||||||
|
g = (2 * g0 + g1) // 3
|
||||||
|
b = (2 * b0 + b1) // 3
|
||||||
|
elif color_code == 3:
|
||||||
|
r = (2 * r1 + r0) // 3
|
||||||
|
g = (2 * g1 + g0) // 3
|
||||||
|
b = (2 * b1 + b0) // 3
|
||||||
|
|
||||||
|
ret[j].extend([r, g, b, a])
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
class BLPFormatError(NotImplementedError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _accept(prefix: bytes) -> bool:
|
||||||
|
return prefix.startswith((b"BLP1", b"BLP2"))
|
||||||
|
|
||||||
|
|
||||||
|
class BlpImageFile(ImageFile.ImageFile):
|
||||||
|
"""
|
||||||
|
Blizzard Mipmap Format
|
||||||
|
"""
|
||||||
|
|
||||||
|
format = "BLP"
|
||||||
|
format_description = "Blizzard Mipmap Format"
|
||||||
|
|
||||||
|
def _open(self) -> None:
|
||||||
|
assert self.fp is not None
|
||||||
|
self.magic = self.fp.read(4)
|
||||||
|
if not _accept(self.magic):
|
||||||
|
msg = f"Bad BLP magic {repr(self.magic)}"
|
||||||
|
raise BLPFormatError(msg)
|
||||||
|
|
||||||
|
compression = struct.unpack("<i", self.fp.read(4))[0]
|
||||||
|
if self.magic == b"BLP1":
|
||||||
|
alpha = struct.unpack("<I", self.fp.read(4))[0] != 0
|
||||||
|
else:
|
||||||
|
encoding = struct.unpack("<b", self.fp.read(1))[0]
|
||||||
|
alpha = struct.unpack("<b", self.fp.read(1))[0] != 0
|
||||||
|
alpha_encoding = struct.unpack("<b", self.fp.read(1))[0]
|
||||||
|
self.fp.seek(1, os.SEEK_CUR) # mips
|
||||||
|
|
||||||
|
self._size = struct.unpack("<II", self.fp.read(8))
|
||||||
|
|
||||||
|
args: tuple[int, int, bool] | tuple[int, int, bool, int]
|
||||||
|
if self.magic == b"BLP1":
|
||||||
|
encoding = struct.unpack("<i", self.fp.read(4))[0]
|
||||||
|
self.fp.seek(4, os.SEEK_CUR) # subtype
|
||||||
|
|
||||||
|
args = (compression, encoding, alpha)
|
||||||
|
offset = 28
|
||||||
|
else:
|
||||||
|
args = (compression, encoding, alpha, alpha_encoding)
|
||||||
|
offset = 20
|
||||||
|
|
||||||
|
decoder = self.magic.decode()
|
||||||
|
|
||||||
|
self._mode = "RGBA" if alpha else "RGB"
|
||||||
|
self.tile = [ImageFile._Tile(decoder, (0, 0) + self.size, offset, args)]
|
||||||
|
|
||||||
|
|
||||||
|
class _BLPBaseDecoder(abc.ABC, ImageFile.PyDecoder):
|
||||||
|
_pulls_fd = True
|
||||||
|
|
||||||
|
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
|
||||||
|
try:
|
||||||
|
self._read_header()
|
||||||
|
self._load()
|
||||||
|
except struct.error as e:
|
||||||
|
msg = "Truncated BLP file"
|
||||||
|
raise OSError(msg) from e
|
||||||
|
return -1, 0
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def _load(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _read_header(self) -> None:
|
||||||
|
self._offsets = struct.unpack("<16I", self._safe_read(16 * 4))
|
||||||
|
self._lengths = struct.unpack("<16I", self._safe_read(16 * 4))
|
||||||
|
|
||||||
|
def _safe_read(self, length: int) -> bytes:
|
||||||
|
assert self.fd is not None
|
||||||
|
return ImageFile._safe_read(self.fd, length)
|
||||||
|
|
||||||
|
def _read_palette(self) -> list[tuple[int, int, int, int]]:
|
||||||
|
ret = []
|
||||||
|
for i in range(256):
|
||||||
|
try:
|
||||||
|
b, g, r, a = struct.unpack("<4B", self._safe_read(4))
|
||||||
|
except struct.error:
|
||||||
|
break
|
||||||
|
ret.append((b, g, r, a))
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def _read_bgra(
|
||||||
|
self, palette: list[tuple[int, int, int, int]], alpha: bool
|
||||||
|
) -> bytearray:
|
||||||
|
data = bytearray()
|
||||||
|
_data = BytesIO(self._safe_read(self._lengths[0]))
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
(offset,) = struct.unpack("<B", _data.read(1))
|
||||||
|
except struct.error:
|
||||||
|
break
|
||||||
|
b, g, r, a = palette[offset]
|
||||||
|
d: tuple[int, ...] = (r, g, b)
|
||||||
|
if alpha:
|
||||||
|
d += (a,)
|
||||||
|
data.extend(d)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
class BLP1Decoder(_BLPBaseDecoder):
|
||||||
|
def _load(self) -> None:
|
||||||
|
self._compression, self._encoding, alpha = self.args
|
||||||
|
|
||||||
|
if self._compression == Format.JPEG:
|
||||||
|
self._decode_jpeg_stream()
|
||||||
|
|
||||||
|
elif self._compression == 1:
|
||||||
|
if self._encoding in (4, 5):
|
||||||
|
palette = self._read_palette()
|
||||||
|
data = self._read_bgra(palette, alpha)
|
||||||
|
self.set_as_raw(data)
|
||||||
|
else:
|
||||||
|
msg = f"Unsupported BLP encoding {repr(self._encoding)}"
|
||||||
|
raise BLPFormatError(msg)
|
||||||
|
else:
|
||||||
|
msg = f"Unsupported BLP compression {repr(self._encoding)}"
|
||||||
|
raise BLPFormatError(msg)
|
||||||
|
|
||||||
|
def _decode_jpeg_stream(self) -> None:
|
||||||
|
from .JpegImagePlugin import JpegImageFile
|
||||||
|
|
||||||
|
(jpeg_header_size,) = struct.unpack("<I", self._safe_read(4))
|
||||||
|
jpeg_header = self._safe_read(jpeg_header_size)
|
||||||
|
assert self.fd is not None
|
||||||
|
self._safe_read(self._offsets[0] - self.fd.tell()) # What IS this?
|
||||||
|
data = self._safe_read(self._lengths[0])
|
||||||
|
data = jpeg_header + data
|
||||||
|
image = JpegImageFile(BytesIO(data))
|
||||||
|
Image._decompression_bomb_check(image.size)
|
||||||
|
if image.mode == "CMYK":
|
||||||
|
args = image.tile[0].args
|
||||||
|
assert isinstance(args, tuple)
|
||||||
|
image.tile = [image.tile[0]._replace(args=(args[0], "CMYK"))]
|
||||||
|
self.set_as_raw(image.convert("RGB").tobytes(), "BGR")
|
||||||
|
|
||||||
|
|
||||||
|
class BLP2Decoder(_BLPBaseDecoder):
|
||||||
|
def _load(self) -> None:
|
||||||
|
self._compression, self._encoding, alpha, self._alpha_encoding = self.args
|
||||||
|
|
||||||
|
palette = self._read_palette()
|
||||||
|
|
||||||
|
assert self.fd is not None
|
||||||
|
self.fd.seek(self._offsets[0])
|
||||||
|
|
||||||
|
if self._compression == 1:
|
||||||
|
# Uncompressed or DirectX compression
|
||||||
|
|
||||||
|
if self._encoding == Encoding.UNCOMPRESSED:
|
||||||
|
data = self._read_bgra(palette, alpha)
|
||||||
|
|
||||||
|
elif self._encoding == Encoding.DXT:
|
||||||
|
data = bytearray()
|
||||||
|
if self._alpha_encoding == AlphaEncoding.DXT1:
|
||||||
|
linesize = (self.state.xsize + 3) // 4 * 8
|
||||||
|
for yb in range((self.state.ysize + 3) // 4):
|
||||||
|
for d in decode_dxt1(self._safe_read(linesize), alpha):
|
||||||
|
data += d
|
||||||
|
|
||||||
|
elif self._alpha_encoding == AlphaEncoding.DXT3:
|
||||||
|
linesize = (self.state.xsize + 3) // 4 * 16
|
||||||
|
for yb in range((self.state.ysize + 3) // 4):
|
||||||
|
for d in decode_dxt3(self._safe_read(linesize)):
|
||||||
|
data += d
|
||||||
|
|
||||||
|
elif self._alpha_encoding == AlphaEncoding.DXT5:
|
||||||
|
linesize = (self.state.xsize + 3) // 4 * 16
|
||||||
|
for yb in range((self.state.ysize + 3) // 4):
|
||||||
|
for d in decode_dxt5(self._safe_read(linesize)):
|
||||||
|
data += d
|
||||||
|
else:
|
||||||
|
msg = f"Unsupported alpha encoding {repr(self._alpha_encoding)}"
|
||||||
|
raise BLPFormatError(msg)
|
||||||
|
else:
|
||||||
|
msg = f"Unknown BLP encoding {repr(self._encoding)}"
|
||||||
|
raise BLPFormatError(msg)
|
||||||
|
|
||||||
|
else:
|
||||||
|
msg = f"Unknown BLP compression {repr(self._compression)}"
|
||||||
|
raise BLPFormatError(msg)
|
||||||
|
|
||||||
|
self.set_as_raw(data)
|
||||||
|
|
||||||
|
|
||||||
|
class BLPEncoder(ImageFile.PyEncoder):
|
||||||
|
_pushes_fd = True
|
||||||
|
|
||||||
|
def _write_palette(self) -> bytes:
|
||||||
|
data = b""
|
||||||
|
assert self.im is not None
|
||||||
|
palette = self.im.getpalette("RGBA", "RGBA")
|
||||||
|
for i in range(len(palette) // 4):
|
||||||
|
r, g, b, a = palette[i * 4 : (i + 1) * 4]
|
||||||
|
data += struct.pack("<4B", b, g, r, a)
|
||||||
|
while len(data) < 256 * 4:
|
||||||
|
data += b"\x00" * 4
|
||||||
|
return data
|
||||||
|
|
||||||
|
def encode(self, bufsize: int) -> tuple[int, int, bytes]:
|
||||||
|
palette_data = self._write_palette()
|
||||||
|
|
||||||
|
offset = 20 + 16 * 4 * 2 + len(palette_data)
|
||||||
|
data = struct.pack("<16I", offset, *((0,) * 15))
|
||||||
|
|
||||||
|
assert self.im is not None
|
||||||
|
w, h = self.im.size
|
||||||
|
data += struct.pack("<16I", w * h, *((0,) * 15))
|
||||||
|
|
||||||
|
data += palette_data
|
||||||
|
|
||||||
|
for y in range(h):
|
||||||
|
for x in range(w):
|
||||||
|
data += struct.pack("<B", self.im.getpixel((x, y)))
|
||||||
|
|
||||||
|
return len(data), 0, data
|
||||||
|
|
||||||
|
|
||||||
|
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
|
if im.mode != "P":
|
||||||
|
msg = "Unsupported BLP image mode"
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
magic = b"BLP1" if im.encoderinfo.get("blp_version") == "BLP1" else b"BLP2"
|
||||||
|
fp.write(magic)
|
||||||
|
|
||||||
|
assert im.palette is not None
|
||||||
|
fp.write(struct.pack("<i", 1)) # Uncompressed or DirectX compression
|
||||||
|
|
||||||
|
alpha_depth = 1 if im.palette.mode == "RGBA" else 0
|
||||||
|
if magic == b"BLP1":
|
||||||
|
fp.write(struct.pack("<L", alpha_depth))
|
||||||
|
else:
|
||||||
|
fp.write(struct.pack("<b", Encoding.UNCOMPRESSED))
|
||||||
|
fp.write(struct.pack("<b", alpha_depth))
|
||||||
|
fp.write(struct.pack("<b", 0)) # alpha encoding
|
||||||
|
fp.write(struct.pack("<b", 0)) # mips
|
||||||
|
fp.write(struct.pack("<II", *im.size))
|
||||||
|
if magic == b"BLP1":
|
||||||
|
fp.write(struct.pack("<i", 5))
|
||||||
|
fp.write(struct.pack("<i", 0))
|
||||||
|
|
||||||
|
ImageFile._save(im, fp, [ImageFile._Tile("BLP", (0, 0) + im.size, 0, im.mode)])
|
||||||
|
|
||||||
|
|
||||||
|
Image.register_open(BlpImageFile.format, BlpImageFile, _accept)
|
||||||
|
Image.register_extension(BlpImageFile.format, ".blp")
|
||||||
|
Image.register_decoder("BLP1", BLP1Decoder)
|
||||||
|
Image.register_decoder("BLP2", BLP2Decoder)
|
||||||
|
|
||||||
|
Image.register_save(BlpImageFile.format, _save)
|
||||||
|
Image.register_encoder("BLP", BLPEncoder)
|
||||||
@@ -0,0 +1,517 @@
|
|||||||
|
#
|
||||||
|
# The Python Imaging Library.
|
||||||
|
# $Id$
|
||||||
|
#
|
||||||
|
# BMP file handler
|
||||||
|
#
|
||||||
|
# Windows (and OS/2) native bitmap storage format.
|
||||||
|
#
|
||||||
|
# history:
|
||||||
|
# 1995-09-01 fl Created
|
||||||
|
# 1996-04-30 fl Added save
|
||||||
|
# 1997-08-27 fl Fixed save of 1-bit images
|
||||||
|
# 1998-03-06 fl Load P images as L where possible
|
||||||
|
# 1998-07-03 fl Load P images as 1 where possible
|
||||||
|
# 1998-12-29 fl Handle small palettes
|
||||||
|
# 2002-12-30 fl Fixed load of 1-bit palette images
|
||||||
|
# 2003-04-21 fl Fixed load of 1-bit monochrome images
|
||||||
|
# 2003-04-23 fl Added limited support for BI_BITFIELDS compression
|
||||||
|
#
|
||||||
|
# Copyright (c) 1997-2003 by Secret Labs AB
|
||||||
|
# Copyright (c) 1995-2003 by Fredrik Lundh
|
||||||
|
#
|
||||||
|
# See the README file for information on usage and redistribution.
|
||||||
|
#
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from typing import IO, Any
|
||||||
|
|
||||||
|
from . import Image, ImageFile, ImagePalette
|
||||||
|
from ._binary import i16le as i16
|
||||||
|
from ._binary import i32le as i32
|
||||||
|
from ._binary import o8
|
||||||
|
from ._binary import o16le as o16
|
||||||
|
from ._binary import o32le as o32
|
||||||
|
|
||||||
|
#
|
||||||
|
# --------------------------------------------------------------------
|
||||||
|
# Read BMP file
|
||||||
|
|
||||||
|
BIT2MODE = {
|
||||||
|
# bits => mode, rawmode
|
||||||
|
1: ("P", "P;1"),
|
||||||
|
4: ("P", "P;4"),
|
||||||
|
8: ("P", "P"),
|
||||||
|
16: ("RGB", "BGR;15"),
|
||||||
|
24: ("RGB", "BGR"),
|
||||||
|
32: ("RGB", "BGRX"),
|
||||||
|
}
|
||||||
|
|
||||||
|
USE_RAW_ALPHA = False
|
||||||
|
|
||||||
|
|
||||||
|
def _accept(prefix: bytes) -> bool:
|
||||||
|
return prefix.startswith(b"BM")
|
||||||
|
|
||||||
|
|
||||||
|
def _dib_accept(prefix: bytes) -> bool:
|
||||||
|
return i32(prefix) in [12, 40, 52, 56, 64, 108, 124]
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Image plugin for the Windows BMP format.
|
||||||
|
# =============================================================================
|
||||||
|
class BmpImageFile(ImageFile.ImageFile):
|
||||||
|
"""Image plugin for the Windows Bitmap format (BMP)"""
|
||||||
|
|
||||||
|
# ------------------------------------------------------------- Description
|
||||||
|
format_description = "Windows Bitmap"
|
||||||
|
format = "BMP"
|
||||||
|
|
||||||
|
# -------------------------------------------------- BMP Compression values
|
||||||
|
COMPRESSIONS = {"RAW": 0, "RLE8": 1, "RLE4": 2, "BITFIELDS": 3, "JPEG": 4, "PNG": 5}
|
||||||
|
for k, v in COMPRESSIONS.items():
|
||||||
|
vars()[k] = v
|
||||||
|
|
||||||
|
def _bitmap(self, header: int = 0, offset: int = 0) -> None:
|
||||||
|
"""Read relevant info about the BMP"""
|
||||||
|
assert self.fp is not None
|
||||||
|
read, seek = self.fp.read, self.fp.seek
|
||||||
|
if header:
|
||||||
|
seek(header)
|
||||||
|
# read bmp header size @offset 14 (this is part of the header size)
|
||||||
|
file_info: dict[str, bool | int | tuple[int, ...]] = {
|
||||||
|
"header_size": i32(read(4)),
|
||||||
|
"direction": -1,
|
||||||
|
}
|
||||||
|
|
||||||
|
# -------------------- If requested, read header at a specific position
|
||||||
|
# read the rest of the bmp header, without its size
|
||||||
|
assert isinstance(file_info["header_size"], int)
|
||||||
|
header_data = ImageFile._safe_read(self.fp, file_info["header_size"] - 4)
|
||||||
|
|
||||||
|
# ------------------------------- Windows Bitmap v2, IBM OS/2 Bitmap v1
|
||||||
|
# ----- This format has different offsets because of width/height types
|
||||||
|
# 12: BITMAPCOREHEADER/OS21XBITMAPHEADER
|
||||||
|
if file_info["header_size"] == 12:
|
||||||
|
file_info["width"] = i16(header_data, 0)
|
||||||
|
file_info["height"] = i16(header_data, 2)
|
||||||
|
file_info["planes"] = i16(header_data, 4)
|
||||||
|
file_info["bits"] = i16(header_data, 6)
|
||||||
|
file_info["compression"] = self.COMPRESSIONS["RAW"]
|
||||||
|
file_info["palette_padding"] = 3
|
||||||
|
|
||||||
|
# --------------------------------------------- Windows Bitmap v3 to v5
|
||||||
|
# 40: BITMAPINFOHEADER
|
||||||
|
# 52: BITMAPV2HEADER
|
||||||
|
# 56: BITMAPV3HEADER
|
||||||
|
# 64: BITMAPCOREHEADER2/OS22XBITMAPHEADER
|
||||||
|
# 108: BITMAPV4HEADER
|
||||||
|
# 124: BITMAPV5HEADER
|
||||||
|
elif file_info["header_size"] in (40, 52, 56, 64, 108, 124):
|
||||||
|
file_info["y_flip"] = header_data[7] == 0xFF
|
||||||
|
file_info["direction"] = 1 if file_info["y_flip"] else -1
|
||||||
|
file_info["width"] = i32(header_data, 0)
|
||||||
|
file_info["height"] = (
|
||||||
|
i32(header_data, 4)
|
||||||
|
if not file_info["y_flip"]
|
||||||
|
else 2**32 - i32(header_data, 4)
|
||||||
|
)
|
||||||
|
file_info["planes"] = i16(header_data, 8)
|
||||||
|
file_info["bits"] = i16(header_data, 10)
|
||||||
|
file_info["compression"] = i32(header_data, 12)
|
||||||
|
# byte size of pixel data
|
||||||
|
file_info["data_size"] = i32(header_data, 16)
|
||||||
|
file_info["pixels_per_meter"] = (
|
||||||
|
i32(header_data, 20),
|
||||||
|
i32(header_data, 24),
|
||||||
|
)
|
||||||
|
file_info["colors"] = i32(header_data, 28)
|
||||||
|
file_info["palette_padding"] = 4
|
||||||
|
assert isinstance(file_info["pixels_per_meter"], tuple)
|
||||||
|
self.info["dpi"] = tuple(x / 39.3701 for x in file_info["pixels_per_meter"])
|
||||||
|
if file_info["compression"] == self.COMPRESSIONS["BITFIELDS"]:
|
||||||
|
masks = ["r_mask", "g_mask", "b_mask"]
|
||||||
|
if len(header_data) >= 48:
|
||||||
|
if len(header_data) >= 52:
|
||||||
|
masks.append("a_mask")
|
||||||
|
else:
|
||||||
|
file_info["a_mask"] = 0x0
|
||||||
|
for idx, mask in enumerate(masks):
|
||||||
|
file_info[mask] = i32(header_data, 36 + idx * 4)
|
||||||
|
else:
|
||||||
|
# 40 byte headers only have the three components in the
|
||||||
|
# bitfields masks, ref:
|
||||||
|
# https://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx
|
||||||
|
# See also
|
||||||
|
# https://github.com/python-pillow/Pillow/issues/1293
|
||||||
|
# There is a 4th component in the RGBQuad, in the alpha
|
||||||
|
# location, but it is listed as a reserved component,
|
||||||
|
# and it is not generally an alpha channel
|
||||||
|
file_info["a_mask"] = 0x0
|
||||||
|
for mask in masks:
|
||||||
|
file_info[mask] = i32(read(4))
|
||||||
|
assert isinstance(file_info["r_mask"], int)
|
||||||
|
assert isinstance(file_info["g_mask"], int)
|
||||||
|
assert isinstance(file_info["b_mask"], int)
|
||||||
|
assert isinstance(file_info["a_mask"], int)
|
||||||
|
file_info["rgb_mask"] = (
|
||||||
|
file_info["r_mask"],
|
||||||
|
file_info["g_mask"],
|
||||||
|
file_info["b_mask"],
|
||||||
|
)
|
||||||
|
file_info["rgba_mask"] = (
|
||||||
|
file_info["r_mask"],
|
||||||
|
file_info["g_mask"],
|
||||||
|
file_info["b_mask"],
|
||||||
|
file_info["a_mask"],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
msg = f"Unsupported BMP header type ({file_info['header_size']})"
|
||||||
|
raise OSError(msg)
|
||||||
|
|
||||||
|
# ------------------ Special case : header is reported 40, which
|
||||||
|
# ---------------------- is shorter than real size for bpp >= 16
|
||||||
|
assert isinstance(file_info["width"], int)
|
||||||
|
assert isinstance(file_info["height"], int)
|
||||||
|
self._size = file_info["width"], file_info["height"]
|
||||||
|
|
||||||
|
# ------- If color count was not found in the header, compute from bits
|
||||||
|
assert isinstance(file_info["bits"], int)
|
||||||
|
file_info["colors"] = (
|
||||||
|
file_info["colors"]
|
||||||
|
if file_info.get("colors", 0)
|
||||||
|
else (1 << file_info["bits"])
|
||||||
|
)
|
||||||
|
assert isinstance(file_info["colors"], int)
|
||||||
|
if offset == 14 + file_info["header_size"] and file_info["bits"] <= 8:
|
||||||
|
offset += 4 * file_info["colors"]
|
||||||
|
|
||||||
|
# ---------------------- Check bit depth for unusual unsupported values
|
||||||
|
self._mode, raw_mode = BIT2MODE.get(file_info["bits"], ("", ""))
|
||||||
|
if not self.mode:
|
||||||
|
msg = f"Unsupported BMP pixel depth ({file_info['bits']})"
|
||||||
|
raise OSError(msg)
|
||||||
|
|
||||||
|
# ---------------- Process BMP with Bitfields compression (not palette)
|
||||||
|
decoder_name = "raw"
|
||||||
|
if file_info["compression"] == self.COMPRESSIONS["BITFIELDS"]:
|
||||||
|
SUPPORTED: dict[int, list[tuple[int, ...]]] = {
|
||||||
|
32: [
|
||||||
|
(0xFF0000, 0xFF00, 0xFF, 0x0),
|
||||||
|
(0xFF000000, 0xFF0000, 0xFF00, 0x0),
|
||||||
|
(0xFF000000, 0xFF00, 0xFF, 0x0),
|
||||||
|
(0xFF000000, 0xFF0000, 0xFF00, 0xFF),
|
||||||
|
(0xFF, 0xFF00, 0xFF0000, 0xFF000000),
|
||||||
|
(0xFF0000, 0xFF00, 0xFF, 0xFF000000),
|
||||||
|
(0xFF000000, 0xFF00, 0xFF, 0xFF0000),
|
||||||
|
(0x0, 0x0, 0x0, 0x0),
|
||||||
|
],
|
||||||
|
24: [(0xFF0000, 0xFF00, 0xFF)],
|
||||||
|
16: [(0xF800, 0x7E0, 0x1F), (0x7C00, 0x3E0, 0x1F)],
|
||||||
|
}
|
||||||
|
MASK_MODES = {
|
||||||
|
(32, (0xFF0000, 0xFF00, 0xFF, 0x0)): "BGRX",
|
||||||
|
(32, (0xFF000000, 0xFF0000, 0xFF00, 0x0)): "XBGR",
|
||||||
|
(32, (0xFF000000, 0xFF00, 0xFF, 0x0)): "BGXR",
|
||||||
|
(32, (0xFF000000, 0xFF0000, 0xFF00, 0xFF)): "ABGR",
|
||||||
|
(32, (0xFF, 0xFF00, 0xFF0000, 0xFF000000)): "RGBA",
|
||||||
|
(32, (0xFF0000, 0xFF00, 0xFF, 0xFF000000)): "BGRA",
|
||||||
|
(32, (0xFF000000, 0xFF00, 0xFF, 0xFF0000)): "BGAR",
|
||||||
|
(32, (0x0, 0x0, 0x0, 0x0)): "BGRA",
|
||||||
|
(24, (0xFF0000, 0xFF00, 0xFF)): "BGR",
|
||||||
|
(16, (0xF800, 0x7E0, 0x1F)): "BGR;16",
|
||||||
|
(16, (0x7C00, 0x3E0, 0x1F)): "BGR;15",
|
||||||
|
}
|
||||||
|
if file_info["bits"] in SUPPORTED:
|
||||||
|
if (
|
||||||
|
file_info["bits"] == 32
|
||||||
|
and file_info["rgba_mask"] in SUPPORTED[file_info["bits"]]
|
||||||
|
):
|
||||||
|
assert isinstance(file_info["rgba_mask"], tuple)
|
||||||
|
raw_mode = MASK_MODES[(file_info["bits"], file_info["rgba_mask"])]
|
||||||
|
self._mode = "RGBA" if "A" in raw_mode else self.mode
|
||||||
|
elif (
|
||||||
|
file_info["bits"] in (24, 16)
|
||||||
|
and file_info["rgb_mask"] in SUPPORTED[file_info["bits"]]
|
||||||
|
):
|
||||||
|
assert isinstance(file_info["rgb_mask"], tuple)
|
||||||
|
raw_mode = MASK_MODES[(file_info["bits"], file_info["rgb_mask"])]
|
||||||
|
else:
|
||||||
|
msg = "Unsupported BMP bitfields layout"
|
||||||
|
raise OSError(msg)
|
||||||
|
else:
|
||||||
|
msg = "Unsupported BMP bitfields layout"
|
||||||
|
raise OSError(msg)
|
||||||
|
elif file_info["compression"] == self.COMPRESSIONS["RAW"]:
|
||||||
|
if file_info["bits"] == 32 and (
|
||||||
|
header == 22 or USE_RAW_ALPHA # 32-bit .cur offset
|
||||||
|
):
|
||||||
|
raw_mode, self._mode = "BGRA", "RGBA"
|
||||||
|
elif file_info["compression"] in (
|
||||||
|
self.COMPRESSIONS["RLE8"],
|
||||||
|
self.COMPRESSIONS["RLE4"],
|
||||||
|
):
|
||||||
|
decoder_name = "bmp_rle"
|
||||||
|
else:
|
||||||
|
msg = f"Unsupported BMP compression ({file_info['compression']})"
|
||||||
|
raise OSError(msg)
|
||||||
|
|
||||||
|
# --------------- Once the header is processed, process the palette/LUT
|
||||||
|
if self.mode == "P": # Paletted for 1, 4 and 8 bit images
|
||||||
|
# ---------------------------------------------------- 1-bit images
|
||||||
|
if not (0 < file_info["colors"] <= 65536):
|
||||||
|
msg = f"Unsupported BMP Palette size ({file_info['colors']})"
|
||||||
|
raise OSError(msg)
|
||||||
|
else:
|
||||||
|
assert isinstance(file_info["palette_padding"], int)
|
||||||
|
padding = file_info["palette_padding"]
|
||||||
|
palette = read(padding * file_info["colors"])
|
||||||
|
grayscale = True
|
||||||
|
indices = (
|
||||||
|
(0, 255)
|
||||||
|
if file_info["colors"] == 2
|
||||||
|
else list(range(file_info["colors"]))
|
||||||
|
)
|
||||||
|
|
||||||
|
# ----------------- Check if grayscale and ignore palette if so
|
||||||
|
for ind, val in enumerate(indices):
|
||||||
|
rgb = palette[ind * padding : ind * padding + 3]
|
||||||
|
if rgb != o8(val) * 3:
|
||||||
|
grayscale = False
|
||||||
|
|
||||||
|
# ------- If all colors are gray, white or black, ditch palette
|
||||||
|
if grayscale:
|
||||||
|
self._mode = "1" if file_info["colors"] == 2 else "L"
|
||||||
|
raw_mode = self.mode
|
||||||
|
else:
|
||||||
|
self._mode = "P"
|
||||||
|
self.palette = ImagePalette.raw(
|
||||||
|
"BGRX" if padding == 4 else "BGR", palette
|
||||||
|
)
|
||||||
|
|
||||||
|
# ---------------------------- Finally set the tile data for the plugin
|
||||||
|
self.info["compression"] = file_info["compression"]
|
||||||
|
args: list[Any] = [raw_mode]
|
||||||
|
if decoder_name == "bmp_rle":
|
||||||
|
args.append(file_info["compression"] == self.COMPRESSIONS["RLE4"])
|
||||||
|
else:
|
||||||
|
assert isinstance(file_info["width"], int)
|
||||||
|
args.append(((file_info["width"] * file_info["bits"] + 31) >> 3) & (~3))
|
||||||
|
args.append(file_info["direction"])
|
||||||
|
self.tile = [
|
||||||
|
ImageFile._Tile(
|
||||||
|
decoder_name,
|
||||||
|
(0, 0, file_info["width"], file_info["height"]),
|
||||||
|
offset or self.fp.tell(),
|
||||||
|
tuple(args),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
def _open(self) -> None:
|
||||||
|
"""Open file, check magic number and read header"""
|
||||||
|
# read 14 bytes: magic number, filesize, reserved, header final offset
|
||||||
|
assert self.fp is not None
|
||||||
|
head_data = self.fp.read(14)
|
||||||
|
# choke if the file does not have the required magic bytes
|
||||||
|
if not _accept(head_data):
|
||||||
|
msg = "Not a BMP file"
|
||||||
|
raise SyntaxError(msg)
|
||||||
|
# read the start position of the BMP image data (u32)
|
||||||
|
offset = i32(head_data, 10)
|
||||||
|
# load bitmap information (offset=raster info)
|
||||||
|
self._bitmap(offset=offset)
|
||||||
|
|
||||||
|
|
||||||
|
class BmpRleDecoder(ImageFile.PyDecoder):
|
||||||
|
_pulls_fd = True
|
||||||
|
|
||||||
|
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
|
||||||
|
assert self.fd is not None
|
||||||
|
rle4 = self.args[1]
|
||||||
|
data = bytearray()
|
||||||
|
x = 0
|
||||||
|
dest_length = self.state.xsize * self.state.ysize
|
||||||
|
while len(data) < dest_length:
|
||||||
|
pixels = self.fd.read(1)
|
||||||
|
byte = self.fd.read(1)
|
||||||
|
if not pixels or not byte:
|
||||||
|
break
|
||||||
|
num_pixels = pixels[0]
|
||||||
|
if num_pixels:
|
||||||
|
# encoded mode
|
||||||
|
if x + num_pixels > self.state.xsize:
|
||||||
|
# Too much data for row
|
||||||
|
num_pixels = max(0, self.state.xsize - x)
|
||||||
|
if rle4:
|
||||||
|
first_pixel = o8(byte[0] >> 4)
|
||||||
|
second_pixel = o8(byte[0] & 0x0F)
|
||||||
|
for index in range(num_pixels):
|
||||||
|
if index % 2 == 0:
|
||||||
|
data += first_pixel
|
||||||
|
else:
|
||||||
|
data += second_pixel
|
||||||
|
else:
|
||||||
|
data += byte * num_pixels
|
||||||
|
x += num_pixels
|
||||||
|
else:
|
||||||
|
if byte[0] == 0:
|
||||||
|
# end of line
|
||||||
|
while len(data) % self.state.xsize != 0:
|
||||||
|
data += b"\x00"
|
||||||
|
x = 0
|
||||||
|
elif byte[0] == 1:
|
||||||
|
# end of bitmap
|
||||||
|
break
|
||||||
|
elif byte[0] == 2:
|
||||||
|
# delta
|
||||||
|
bytes_read = self.fd.read(2)
|
||||||
|
if len(bytes_read) < 2:
|
||||||
|
break
|
||||||
|
right, up = self.fd.read(2)
|
||||||
|
data += b"\x00" * (right + up * self.state.xsize)
|
||||||
|
x = len(data) % self.state.xsize
|
||||||
|
else:
|
||||||
|
# absolute mode
|
||||||
|
if rle4:
|
||||||
|
# 2 pixels per byte
|
||||||
|
byte_count = byte[0] // 2
|
||||||
|
bytes_read = self.fd.read(byte_count)
|
||||||
|
for byte_read in bytes_read:
|
||||||
|
data += o8(byte_read >> 4)
|
||||||
|
data += o8(byte_read & 0x0F)
|
||||||
|
else:
|
||||||
|
byte_count = byte[0]
|
||||||
|
bytes_read = self.fd.read(byte_count)
|
||||||
|
data += bytes_read
|
||||||
|
if len(bytes_read) < byte_count:
|
||||||
|
break
|
||||||
|
x += byte[0]
|
||||||
|
|
||||||
|
# align to 16-bit word boundary
|
||||||
|
if self.fd.tell() % 2 != 0:
|
||||||
|
self.fd.seek(1, os.SEEK_CUR)
|
||||||
|
rawmode = "L" if self.mode == "L" else "P"
|
||||||
|
self.set_as_raw(bytes(data), rawmode, (0, self.args[-1]))
|
||||||
|
return -1, 0
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Image plugin for the DIB format (BMP alias)
|
||||||
|
# =============================================================================
|
||||||
|
class DibImageFile(BmpImageFile):
|
||||||
|
format = "DIB"
|
||||||
|
format_description = "Windows Bitmap"
|
||||||
|
|
||||||
|
def _open(self) -> None:
|
||||||
|
self._bitmap()
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# --------------------------------------------------------------------
|
||||||
|
# Write BMP file
|
||||||
|
|
||||||
|
|
||||||
|
SAVE = {
|
||||||
|
"1": ("1", 1, 2),
|
||||||
|
"L": ("L", 8, 256),
|
||||||
|
"P": ("P", 8, 256),
|
||||||
|
"RGB": ("BGR", 24, 0),
|
||||||
|
"RGBA": ("BGRA", 32, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _dib_save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
|
_save(im, fp, filename, False)
|
||||||
|
|
||||||
|
|
||||||
|
def _save(
|
||||||
|
im: Image.Image, fp: IO[bytes], filename: str | bytes, bitmap_header: bool = True
|
||||||
|
) -> None:
|
||||||
|
try:
|
||||||
|
rawmode, bits, colors = SAVE[im.mode]
|
||||||
|
except KeyError as e:
|
||||||
|
msg = f"cannot write mode {im.mode} as BMP"
|
||||||
|
raise OSError(msg) from e
|
||||||
|
|
||||||
|
info = im.encoderinfo
|
||||||
|
|
||||||
|
dpi = info.get("dpi", (96, 96))
|
||||||
|
|
||||||
|
# 1 meter == 39.3701 inches
|
||||||
|
ppm = tuple(int(x * 39.3701 + 0.5) for x in dpi)
|
||||||
|
|
||||||
|
stride = ((im.size[0] * bits + 7) // 8 + 3) & (~3)
|
||||||
|
header = 40 # or 64 for OS/2 version 2
|
||||||
|
image = stride * im.size[1]
|
||||||
|
|
||||||
|
if im.mode == "1":
|
||||||
|
palette = b"".join(o8(i) * 3 + b"\x00" for i in (0, 255))
|
||||||
|
elif im.mode == "L":
|
||||||
|
palette = b"".join(o8(i) * 3 + b"\x00" for i in range(256))
|
||||||
|
elif im.mode == "P":
|
||||||
|
palette = im.im.getpalette("RGB", "BGRX")
|
||||||
|
colors = len(palette) // 4
|
||||||
|
else:
|
||||||
|
palette = None
|
||||||
|
|
||||||
|
# bitmap header
|
||||||
|
if bitmap_header:
|
||||||
|
offset = 14 + header + colors * 4
|
||||||
|
file_size = offset + image
|
||||||
|
if file_size > 2**32 - 1:
|
||||||
|
msg = "File size is too large for the BMP format"
|
||||||
|
raise ValueError(msg)
|
||||||
|
fp.write(
|
||||||
|
b"BM" # file type (magic)
|
||||||
|
+ o32(file_size) # file size
|
||||||
|
+ o32(0) # reserved
|
||||||
|
+ o32(offset) # image data offset
|
||||||
|
)
|
||||||
|
|
||||||
|
# bitmap info header
|
||||||
|
fp.write(
|
||||||
|
o32(header) # info header size
|
||||||
|
+ o32(im.size[0]) # width
|
||||||
|
+ o32(im.size[1]) # height
|
||||||
|
+ o16(1) # planes
|
||||||
|
+ o16(bits) # depth
|
||||||
|
+ o32(0) # compression (0=uncompressed)
|
||||||
|
+ o32(image) # size of bitmap
|
||||||
|
+ o32(ppm[0]) # resolution
|
||||||
|
+ o32(ppm[1]) # resolution
|
||||||
|
+ o32(colors) # colors used
|
||||||
|
+ o32(colors) # colors important
|
||||||
|
)
|
||||||
|
|
||||||
|
fp.write(b"\0" * (header - 40)) # padding (for OS/2 format)
|
||||||
|
|
||||||
|
if palette:
|
||||||
|
fp.write(palette)
|
||||||
|
|
||||||
|
ImageFile._save(
|
||||||
|
im, fp, [ImageFile._Tile("raw", (0, 0) + im.size, 0, (rawmode, stride, -1))]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# --------------------------------------------------------------------
|
||||||
|
# Registry
|
||||||
|
|
||||||
|
|
||||||
|
Image.register_open(BmpImageFile.format, BmpImageFile, _accept)
|
||||||
|
Image.register_save(BmpImageFile.format, _save)
|
||||||
|
|
||||||
|
Image.register_extension(BmpImageFile.format, ".bmp")
|
||||||
|
|
||||||
|
Image.register_mime(BmpImageFile.format, "image/bmp")
|
||||||
|
|
||||||
|
Image.register_decoder("bmp_rle", BmpRleDecoder)
|
||||||
|
|
||||||
|
Image.register_open(DibImageFile.format, DibImageFile, _dib_accept)
|
||||||
|
Image.register_save(DibImageFile.format, _dib_save)
|
||||||
|
|
||||||
|
Image.register_extension(DibImageFile.format, ".dib")
|
||||||
|
|
||||||
|
Image.register_mime(DibImageFile.format, "image/bmp")
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
#
|
||||||
|
# The Python Imaging Library
|
||||||
|
# $Id$
|
||||||
|
#
|
||||||
|
# BUFR stub adapter
|
||||||
|
#
|
||||||
|
# Copyright (c) 1996-2003 by Fredrik Lundh
|
||||||
|
#
|
||||||
|
# See the README file for information on usage and redistribution.
|
||||||
|
#
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from typing import IO
|
||||||
|
|
||||||
|
from . import Image, ImageFile
|
||||||
|
|
||||||
|
_handler = None
|
||||||
|
|
||||||
|
|
||||||
|
def register_handler(handler: ImageFile.StubHandler | None) -> None:
|
||||||
|
"""
|
||||||
|
Install application-specific BUFR image handler.
|
||||||
|
|
||||||
|
:param handler: Handler object.
|
||||||
|
"""
|
||||||
|
global _handler
|
||||||
|
_handler = handler
|
||||||
|
|
||||||
|
|
||||||
|
# --------------------------------------------------------------------
|
||||||
|
# Image adapter
|
||||||
|
|
||||||
|
|
||||||
|
def _accept(prefix: bytes) -> bool:
|
||||||
|
return prefix.startswith((b"BUFR", b"ZCZC"))
|
||||||
|
|
||||||
|
|
||||||
|
class BufrStubImageFile(ImageFile.StubImageFile):
|
||||||
|
format = "BUFR"
|
||||||
|
format_description = "BUFR"
|
||||||
|
|
||||||
|
def _open(self) -> None:
|
||||||
|
assert self.fp is not None
|
||||||
|
if not _accept(self.fp.read(4)):
|
||||||
|
msg = "Not a BUFR file"
|
||||||
|
raise SyntaxError(msg)
|
||||||
|
|
||||||
|
self.fp.seek(-4, os.SEEK_CUR)
|
||||||
|
|
||||||
|
# make something up
|
||||||
|
self._mode = "F"
|
||||||
|
self._size = 1, 1
|
||||||
|
|
||||||
|
loader = self._load()
|
||||||
|
if loader:
|
||||||
|
loader.open(self)
|
||||||
|
|
||||||
|
def _load(self) -> ImageFile.StubHandler | None:
|
||||||
|
return _handler
|
||||||
|
|
||||||
|
|
||||||
|
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
|
if _handler is None or not hasattr(_handler, "save"):
|
||||||
|
msg = "BUFR save handler not installed"
|
||||||
|
raise OSError(msg)
|
||||||
|
_handler.save(im, fp, filename)
|
||||||
|
|
||||||
|
|
||||||
|
# --------------------------------------------------------------------
|
||||||
|
# Registry
|
||||||
|
|
||||||
|
Image.register_open(BufrStubImageFile.format, BufrStubImageFile, _accept)
|
||||||
|
Image.register_save(BufrStubImageFile.format, _save)
|
||||||
|
|
||||||
|
Image.register_extension(BufrStubImageFile.format, ".bufr")
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
#
|
||||||
|
# The Python Imaging Library.
|
||||||
|
# $Id$
|
||||||
|
#
|
||||||
|
# a class to read from a container file
|
||||||
|
#
|
||||||
|
# History:
|
||||||
|
# 1995-06-18 fl Created
|
||||||
|
# 1995-09-07 fl Added readline(), readlines()
|
||||||
|
#
|
||||||
|
# Copyright (c) 1997-2001 by Secret Labs AB
|
||||||
|
# Copyright (c) 1995 by Fredrik Lundh
|
||||||
|
#
|
||||||
|
# See the README file for information on usage and redistribution.
|
||||||
|
#
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import io
|
||||||
|
from collections.abc import Iterable
|
||||||
|
from typing import IO, AnyStr, NoReturn
|
||||||
|
|
||||||
|
|
||||||
|
class ContainerIO(IO[AnyStr]):
|
||||||
|
"""
|
||||||
|
A file object that provides read access to a part of an existing
|
||||||
|
file (for example a TAR file).
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, file: IO[AnyStr], offset: int, length: int) -> None:
|
||||||
|
"""
|
||||||
|
Create file object.
|
||||||
|
|
||||||
|
:param file: Existing file.
|
||||||
|
:param offset: Start of region, in bytes.
|
||||||
|
:param length: Size of region, in bytes.
|
||||||
|
"""
|
||||||
|
self.fh: IO[AnyStr] = file
|
||||||
|
self.pos = 0
|
||||||
|
self.offset = offset
|
||||||
|
self.length = length
|
||||||
|
self.fh.seek(offset)
|
||||||
|
|
||||||
|
##
|
||||||
|
# Always false.
|
||||||
|
|
||||||
|
def isatty(self) -> bool:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def seekable(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def seek(self, offset: int, mode: int = io.SEEK_SET) -> int:
|
||||||
|
"""
|
||||||
|
Move file pointer.
|
||||||
|
|
||||||
|
:param offset: Offset in bytes.
|
||||||
|
:param mode: Starting position. Use 0 for beginning of region, 1
|
||||||
|
for current offset, and 2 for end of region. You cannot move
|
||||||
|
the pointer outside the defined region.
|
||||||
|
:returns: Offset from start of region, in bytes.
|
||||||
|
"""
|
||||||
|
if mode == 1:
|
||||||
|
self.pos = self.pos + offset
|
||||||
|
elif mode == 2:
|
||||||
|
self.pos = self.length + offset
|
||||||
|
else:
|
||||||
|
self.pos = offset
|
||||||
|
# clamp
|
||||||
|
self.pos = max(0, min(self.pos, self.length))
|
||||||
|
self.fh.seek(self.offset + self.pos)
|
||||||
|
return self.pos
|
||||||
|
|
||||||
|
def tell(self) -> int:
|
||||||
|
"""
|
||||||
|
Get current file pointer.
|
||||||
|
|
||||||
|
:returns: Offset from start of region, in bytes.
|
||||||
|
"""
|
||||||
|
return self.pos
|
||||||
|
|
||||||
|
def readable(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def read(self, n: int = -1) -> AnyStr:
|
||||||
|
"""
|
||||||
|
Read data.
|
||||||
|
|
||||||
|
:param n: Number of bytes to read. If omitted, zero or negative,
|
||||||
|
read until end of region.
|
||||||
|
:returns: An 8-bit string.
|
||||||
|
"""
|
||||||
|
if n > 0:
|
||||||
|
n = min(n, self.length - self.pos)
|
||||||
|
else:
|
||||||
|
n = self.length - self.pos
|
||||||
|
if n <= 0: # EOF
|
||||||
|
return b"" if "b" in self.fh.mode else "" # type: ignore[return-value]
|
||||||
|
self.pos = self.pos + n
|
||||||
|
return self.fh.read(n)
|
||||||
|
|
||||||
|
def readline(self, n: int = -1) -> AnyStr:
|
||||||
|
"""
|
||||||
|
Read a line of text.
|
||||||
|
|
||||||
|
:param n: Number of bytes to read. If omitted, zero or negative,
|
||||||
|
read until end of line.
|
||||||
|
:returns: An 8-bit string.
|
||||||
|
"""
|
||||||
|
s: AnyStr = b"" if "b" in self.fh.mode else "" # type: ignore[assignment]
|
||||||
|
newline_character = b"\n" if "b" in self.fh.mode else "\n"
|
||||||
|
while True:
|
||||||
|
c = self.read(1)
|
||||||
|
if not c:
|
||||||
|
break
|
||||||
|
s = s + c
|
||||||
|
if c == newline_character or len(s) == n:
|
||||||
|
break
|
||||||
|
return s
|
||||||
|
|
||||||
|
def readlines(self, n: int | None = -1) -> list[AnyStr]:
|
||||||
|
"""
|
||||||
|
Read multiple lines of text.
|
||||||
|
|
||||||
|
:param n: Number of lines to read. If omitted, zero, negative or None,
|
||||||
|
read until end of region.
|
||||||
|
:returns: A list of 8-bit strings.
|
||||||
|
"""
|
||||||
|
lines = []
|
||||||
|
while True:
|
||||||
|
s = self.readline()
|
||||||
|
if not s:
|
||||||
|
break
|
||||||
|
lines.append(s)
|
||||||
|
if len(lines) == n:
|
||||||
|
break
|
||||||
|
return lines
|
||||||
|
|
||||||
|
def writable(self) -> bool:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def write(self, b: AnyStr) -> NoReturn:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def writelines(self, lines: Iterable[AnyStr]) -> NoReturn:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def truncate(self, size: int | None = None) -> int:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def __enter__(self) -> ContainerIO[AnyStr]:
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args: object) -> None:
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
def __iter__(self) -> ContainerIO[AnyStr]:
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __next__(self) -> AnyStr:
|
||||||
|
line = self.readline()
|
||||||
|
if not line:
|
||||||
|
msg = "end of region"
|
||||||
|
raise StopIteration(msg)
|
||||||
|
return line
|
||||||
|
|
||||||
|
def fileno(self) -> int:
|
||||||
|
return self.fh.fileno()
|
||||||
|
|
||||||
|
def flush(self) -> None:
|
||||||
|
self.fh.flush()
|
||||||
|
|
||||||
|
def close(self) -> None:
|
||||||
|
self.fh.close()
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
#
|
||||||
|
# The Python Imaging Library.
|
||||||
|
# $Id$
|
||||||
|
#
|
||||||
|
# Windows Cursor support for PIL
|
||||||
|
#
|
||||||
|
# notes:
|
||||||
|
# uses BmpImagePlugin.py to read the bitmap data.
|
||||||
|
#
|
||||||
|
# history:
|
||||||
|
# 96-05-27 fl Created
|
||||||
|
#
|
||||||
|
# Copyright (c) Secret Labs AB 1997.
|
||||||
|
# Copyright (c) Fredrik Lundh 1996.
|
||||||
|
#
|
||||||
|
# See the README file for information on usage and redistribution.
|
||||||
|
#
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from . import BmpImagePlugin, Image
|
||||||
|
from ._binary import i16le as i16
|
||||||
|
from ._binary import i32le as i32
|
||||||
|
|
||||||
|
#
|
||||||
|
# --------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def _accept(prefix: bytes) -> bool:
|
||||||
|
return prefix.startswith(b"\0\0\2\0")
|
||||||
|
|
||||||
|
|
||||||
|
##
|
||||||
|
# Image plugin for Windows Cursor files.
|
||||||
|
|
||||||
|
|
||||||
|
class CurImageFile(BmpImagePlugin.BmpImageFile):
|
||||||
|
format = "CUR"
|
||||||
|
format_description = "Windows Cursor"
|
||||||
|
|
||||||
|
def _open(self) -> None:
|
||||||
|
assert self.fp is not None
|
||||||
|
offset = self.fp.tell()
|
||||||
|
|
||||||
|
# check magic
|
||||||
|
s = self.fp.read(6)
|
||||||
|
if not _accept(s):
|
||||||
|
msg = "not a CUR file"
|
||||||
|
raise SyntaxError(msg)
|
||||||
|
|
||||||
|
# pick the largest cursor in the file
|
||||||
|
m = b""
|
||||||
|
for i in range(i16(s, 4)):
|
||||||
|
s = self.fp.read(16)
|
||||||
|
if not m:
|
||||||
|
m = s
|
||||||
|
elif s[0] > m[0] and s[1] > m[1]:
|
||||||
|
m = s
|
||||||
|
if not m:
|
||||||
|
msg = "No cursors were found"
|
||||||
|
raise TypeError(msg)
|
||||||
|
|
||||||
|
# load as bitmap
|
||||||
|
self._bitmap(i32(m, 12) + offset)
|
||||||
|
|
||||||
|
# patch up the bitmap height
|
||||||
|
self._size = self.size[0], self.size[1] // 2
|
||||||
|
self.tile = [self.tile[0]._replace(extents=(0, 0) + self.size)]
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# --------------------------------------------------------------------
|
||||||
|
|
||||||
|
Image.register_open(CurImageFile.format, CurImageFile, _accept)
|
||||||
|
|
||||||
|
Image.register_extension(CurImageFile.format, ".cur")
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
#
|
||||||
|
# The Python Imaging Library.
|
||||||
|
# $Id$
|
||||||
|
#
|
||||||
|
# DCX file handling
|
||||||
|
#
|
||||||
|
# DCX is a container file format defined by Intel, commonly used
|
||||||
|
# for fax applications. Each DCX file consists of a directory
|
||||||
|
# (a list of file offsets) followed by a set of (usually 1-bit)
|
||||||
|
# PCX files.
|
||||||
|
#
|
||||||
|
# History:
|
||||||
|
# 1995-09-09 fl Created
|
||||||
|
# 1996-03-20 fl Properly derived from PcxImageFile.
|
||||||
|
# 1998-07-15 fl Renamed offset attribute to avoid name clash
|
||||||
|
# 2002-07-30 fl Fixed file handling
|
||||||
|
#
|
||||||
|
# Copyright (c) 1997-98 by Secret Labs AB.
|
||||||
|
# Copyright (c) 1995-96 by Fredrik Lundh.
|
||||||
|
#
|
||||||
|
# See the README file for information on usage and redistribution.
|
||||||
|
#
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from . import Image
|
||||||
|
from ._binary import i32le as i32
|
||||||
|
from ._util import DeferredError
|
||||||
|
from .PcxImagePlugin import PcxImageFile
|
||||||
|
|
||||||
|
MAGIC = 0x3ADE68B1 # QUIZ: what's this value, then?
|
||||||
|
|
||||||
|
|
||||||
|
def _accept(prefix: bytes) -> bool:
|
||||||
|
return len(prefix) >= 4 and i32(prefix) == MAGIC
|
||||||
|
|
||||||
|
|
||||||
|
##
|
||||||
|
# Image plugin for the Intel DCX format.
|
||||||
|
|
||||||
|
|
||||||
|
class DcxImageFile(PcxImageFile):
|
||||||
|
format = "DCX"
|
||||||
|
format_description = "Intel DCX"
|
||||||
|
_close_exclusive_fp_after_loading = False
|
||||||
|
|
||||||
|
def _open(self) -> None:
|
||||||
|
# Header
|
||||||
|
assert self.fp is not None
|
||||||
|
s = self.fp.read(4)
|
||||||
|
if not _accept(s):
|
||||||
|
msg = "not a DCX file"
|
||||||
|
raise SyntaxError(msg)
|
||||||
|
|
||||||
|
# Component directory
|
||||||
|
self._offset = []
|
||||||
|
for i in range(1024):
|
||||||
|
offset = i32(self.fp.read(4))
|
||||||
|
if not offset:
|
||||||
|
break
|
||||||
|
self._offset.append(offset)
|
||||||
|
|
||||||
|
self._fp = self.fp
|
||||||
|
self.frame = -1
|
||||||
|
self.n_frames = len(self._offset)
|
||||||
|
self.is_animated = self.n_frames > 1
|
||||||
|
self.seek(0)
|
||||||
|
|
||||||
|
def seek(self, frame: int) -> None:
|
||||||
|
if not self._seek_check(frame):
|
||||||
|
return
|
||||||
|
if isinstance(self._fp, DeferredError):
|
||||||
|
raise self._fp.ex
|
||||||
|
self.frame = frame
|
||||||
|
self.fp = self._fp
|
||||||
|
self.fp.seek(self._offset[frame])
|
||||||
|
PcxImageFile._open(self)
|
||||||
|
|
||||||
|
def tell(self) -> int:
|
||||||
|
return self.frame
|
||||||
|
|
||||||
|
|
||||||
|
Image.register_open(DcxImageFile.format, DcxImageFile, _accept)
|
||||||
|
|
||||||
|
Image.register_extension(DcxImageFile.format, ".dcx")
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user