The first commit
This commit is contained in:
commit
cb86a0ca04
161
.gitignore
vendored
Normal file
161
.gitignore
vendored
Normal file
@ -0,0 +1,161 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
.pdm.toml
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
10
LICENSE.txt
Normal file
10
LICENSE.txt
Normal file
@ -0,0 +1,10 @@
|
||||
Copyright (c) 2024, void All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by the <copyright holder>.
|
||||
Neither the name of the <copyright holder> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1
MANIFEST.in
Normal file
1
MANIFEST.in
Normal file
@ -0,0 +1 @@
|
||||
recursive-include examples/
|
18
README.md
Normal file
18
README.md
Normal file
@ -0,0 +1,18 @@
|
||||
# DICOM Builder
|
||||
|
||||
The library came out after a research I made on the DICOM standard and `libdicom` library and could be useful for programmatically build DICOM files.
|
||||
|
||||
Check the examples directory for more details at 
|
||||
|
||||
More details about the DICOM standard at 
|
||||
|
||||
Note: the example uses pwn tools to build cyclic patterns. You can install via `pip install pwntools`.
|
||||
|
||||
This package can be installed via pip as well by cloning the repo and:
|
||||
|
||||
`pip install -e .`
|
||||
|
||||
Or `pip install dicombuilder` when the application is on PyPi
|
||||
|
||||
# License
|
||||
See the LICENSE.txt file
|
0
dicombuilder/__init__.py
Normal file
0
dicombuilder/__init__.py
Normal file
4904
dicombuilder/dicttable.py
Normal file
4904
dicombuilder/dicttable.py
Normal file
File diff suppressed because it is too large
Load Diff
51
dicombuilder/utils.py
Normal file
51
dicombuilder/utils.py
Normal file
@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
from .dicttable import find_by_name
|
||||
import struct
|
||||
|
||||
|
||||
def dcm_need_reserved(vr):
|
||||
reserved = ["AE", "AS", "AT", "CS", "DA", "DS", "DT", "FL", "FD", "IS", "LO", "LT", "PN", "SH",
|
||||
"SL", "SS", "ST", "TM", "UI", "UL", "US"]
|
||||
return vr.decode() not in reserved
|
||||
|
||||
|
||||
def dcm_write_file(buffer, filename):
|
||||
print(f"Writing file to {filename}")
|
||||
with open(sys.argv[1], "wb") as f:
|
||||
f.write(buffer)
|
||||
|
||||
|
||||
def dcm_add_header():
|
||||
return b'\x00' * 16 * 8
|
||||
|
||||
|
||||
def dcm_add_magic():
|
||||
return struct.pack("<I", 0x4d434944) # DICM
|
||||
|
||||
|
||||
def dcm_add_element(name, length, body):
|
||||
if length != 0xFFFFFFFF and len(body) < length:
|
||||
print(f"Invalid length for element {name} - expected \
|
||||
{length} found {len(body)}")
|
||||
sys.exit(-1)
|
||||
buf = b''
|
||||
element = find_by_name(name)
|
||||
if not element:
|
||||
print(f"Unable to find element {name} on the dict table")
|
||||
sys.exit(-1)
|
||||
tag, ul, _ = element
|
||||
|
||||
buf += struct.pack("<H", ((tag & 0xffff0000) >> 16)) # First 4 bytes of the tag
|
||||
buf += struct.pack("<H", (tag & 0xffff)) # Second 4 bytes of the tag
|
||||
buf += ul.encode() # UL
|
||||
need_reserved_byte = dcm_need_reserved(ul.encode())
|
||||
# Some tags require reserved byte and the length field is 4 bytes long
|
||||
if need_reserved_byte:
|
||||
buf += struct.pack("<H", 0x0)
|
||||
buf += struct.pack("<I", length)
|
||||
else:
|
||||
buf += struct.pack("<H", length)
|
||||
buf += body
|
||||
return buf
|
||||
|
31
examples/example.py
Normal file
31
examples/example.py
Normal file
@ -0,0 +1,31 @@
|
||||
from dicombuilder.utils import dcm_add_magic, dcm_add_header, dcm_add_element, dcm_write_file
|
||||
import struct
|
||||
from pwn import cyclic
|
||||
|
||||
buffer = b''
|
||||
buffer += dcm_add_header()
|
||||
buffer += dcm_add_magic()
|
||||
|
||||
# Size of this depends on the next meta elements.
|
||||
buffer += dcm_add_element("FileMetaInformationGroupLength", 4, struct.pack("<I", 0xA600))
|
||||
buffer += dcm_add_element("FileMetaInformationVersion", 2, struct.pack("<H", 0x01))
|
||||
buffer += dcm_add_element("MediaStorageSOPClassUID", 28, b"1.2.840.1000o.5.1.4.1.1.12.1")
|
||||
buffer += dcm_add_element("MediaStorageSOPInstanceUID", 44,
|
||||
b"1.3.12.2.1107.5.4.3.284980.19951129.170916.9")
|
||||
buffer += dcm_add_element("TransferSyntaxUID", 22, b"1.2.840.10008.1.2.4.50")
|
||||
buffer += dcm_add_element("ImplementationClassUID", 22, b"1.3.12.2.1107.5.4.3. ")
|
||||
buffer += dcm_add_element("ImplementationVersionName", 16, cyclic(16))
|
||||
buffer += dcm_add_element("SourceApplicationEntityTitle", 16, cyclic(16))
|
||||
|
||||
buffer += dcm_add_element("SpecificCharacterSet", 11, cyclic(11))
|
||||
buffer += dcm_add_element("ImageType", 11, cyclic(11))
|
||||
buffer += dcm_add_element("SOPClassUID", 11, cyclic(11))
|
||||
buffer += dcm_add_element("SOPInstanceUID", 11, cyclic(11))
|
||||
buffer += dcm_add_element("StudyDate", 8, cyclic(8))
|
||||
buffer += dcm_add_element("StudyTime", 6, cyclic(6))
|
||||
buffer += dcm_add_element("AccessionNumber", 11, cyclic(11))
|
||||
buffer += dcm_add_element("Modality", 11, cyclic(11))
|
||||
buffer += dcm_add_element("Manufacturer", 11, cyclic(11))
|
||||
buffer += dcm_add_element("InstitutionName", 11, cyclic(11))
|
||||
|
||||
dcm_write_file(buffer, "payload.dcm")
|
32
pyproject.toml
Normal file
32
pyproject.toml
Normal file
@ -0,0 +1,32 @@
|
||||
# pyproject.toml
|
||||
|
||||
[build-system]
|
||||
requires = ["setuptools>=61.0.0", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "dicombuilder"
|
||||
version = "1.0.0"
|
||||
description = "DICOM file builder"
|
||||
readme = "README.md"
|
||||
authors = [{ name = "", email = "" }]
|
||||
license = { file = "LICENSE.txt" }
|
||||
classifiers = [
|
||||
"License :: OSI Approved :: BSD License",
|
||||
"Programming Language :: Python",
|
||||
"Programming Language :: Python :: 3",
|
||||
]
|
||||
keywords = ["dicom", "dcom", "dicom-builder"]
|
||||
dependencies = [
|
||||
'tomli; python_version < "3.11"',
|
||||
]
|
||||
requires-python = ">=3.10"
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = []
|
||||
|
||||
[project.urls]
|
||||
Homepage = "https://github.com/voidz0r/dicom-builder"
|
||||
|
||||
[project.scripts]
|
||||
|
34
setup.py
Normal file
34
setup.py
Normal file
@ -0,0 +1,34 @@
|
||||
from distutils.core import setup
|
||||
|
||||
setup(
|
||||
# Application name:
|
||||
name="dicombuilder",
|
||||
|
||||
# Version number (initial):
|
||||
version="0.1.0",
|
||||
|
||||
# Application author details:
|
||||
author="void",
|
||||
author_email="void@voidzone.it",
|
||||
|
||||
# Packages
|
||||
packages=["dicombuilder"],
|
||||
|
||||
# Include additional files into the package
|
||||
include_package_data=True,
|
||||
|
||||
# Details
|
||||
url="http://pypi.python.org/pypi/dicombuilder/",
|
||||
|
||||
#
|
||||
# license="LICENSE.txt",
|
||||
description="DICOM Builder © 2024 by void is licensed under CC BY 4.0. \
|
||||
To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/",
|
||||
|
||||
# long_description=open("README.txt").read(),
|
||||
|
||||
# Dependent packages (distributions)
|
||||
install_requires=[
|
||||
"",
|
||||
],
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user