Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion c2pa-native-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c2pa-v0.67.1
c2pa-v0.73.1
10 changes: 6 additions & 4 deletions examples/sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
fixtures_dir = os.path.join(os.path.dirname(__file__), "../tests/fixtures/")
output_dir = os.path.join(os.path.dirname(__file__), "../output/")

# Ensure the output directory exists
# Ensure the output directory exists.
if not os.path.exists(output_dir):
os.makedirs(output_dir)

Expand All @@ -43,7 +43,7 @@
with open(fixtures_dir + "es256_private.key", "rb") as key_file:
key = key_file.read()

# Define a callback signer function
# Define a callback signer function.
def callback_signer_es256(data: bytes) -> bytes:
"""Callback function that signs data using ES256 algorithm."""
private_key = serialization.load_pem_private_key(
Expand All @@ -60,7 +60,6 @@ def callback_signer_es256(data: bytes) -> bytes:
# Create a manifest definition as a dictionary.
# This manifest follows the V2 manifest format.
manifest_definition = {
"claim_generator": "python_example",
"claim_generator_info": [{
"name": "python_example",
"version": "0.0.1",
Expand All @@ -87,7 +86,7 @@ def callback_signer_es256(data: bytes) -> bytes:
}

# Sign the image with the signer created above,
# which will use the callback signer
# which will use the callback signer.
print("\nSigning the image file...")

with c2pa.Signer.from_callback(
Expand All @@ -107,6 +106,9 @@ def callback_signer_es256(data: bytes) -> bytes:
print("\nReading signed image metadata:")
with open(output_dir + "A_signed.jpg", "rb") as file:
with c2pa.Reader("image/jpeg", file) as reader:
# The validation state will depend on loaded trust settings.
# Without loaded trust settings,
# the manifest validation_state will be "Invalid".
print(reader.json())

print("\nExample completed successfully!")
Expand Down
3 changes: 3 additions & 0 deletions examples/sign_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
print("\nReading signed image metadata:")
with open(output_dir + "C_signed.jpg", "rb") as file:
with c2pa.Reader("image/jpeg", file) as reader:
# The validation state will depend on loaded trust settings.
# Without loaded trust settings,
# the manifest validation_state will be "Invalid".
print(reader.json())

print("\nExample completed successfully!")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "c2pa-python"
version = "0.27.1"
version = "0.28.0"
requires-python = ">=3.10"
description = "Python bindings for the C2PA Content Authenticity Initiative (CAI) library"
readme = { file = "README.md", content-type = "text/markdown" }
Expand Down
4 changes: 4 additions & 0 deletions src/c2pa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
C2paError,
Reader,
C2paSigningAlg,
C2paDigitalSourceType,
C2paBuilderIntent,
C2paSignerInfo,
Signer,
Stream,
Expand All @@ -35,6 +37,8 @@
'C2paError',
'Reader',
'C2paSigningAlg',
'C2paDigitalSourceType',
'C2paBuilderIntent',
'C2paSignerInfo',
'Signer',
'Stream',
Expand Down
77 changes: 77 additions & 0 deletions src/c2pa/c2pa.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'c2pa_builder_free',
'c2pa_builder_set_no_embed',
'c2pa_builder_set_remote_url',
'c2pa_builder_set_intent',
'c2pa_builder_add_resource',
'c2pa_builder_add_ingredient_from_stream',
'c2pa_builder_add_action',
Expand Down Expand Up @@ -158,6 +159,37 @@ class C2paSigningAlg(enum.IntEnum):
ED25519 = 6


class C2paDigitalSourceType(enum.IntEnum):
"""List of possible digital source types."""
EMPTY = 0
TRAINED_ALGORITHMIC_DATA = 1
DIGITAL_CAPTURE = 2
COMPUTATIONAL_CAPTURE = 3
NEGATIVE_FILM = 4
POSITIVE_FILM = 5
PRINT = 6
HUMAN_EDITS = 7
COMPOSITE_WITH_TRAINED_ALGORITHMIC_MEDIA = 8
ALGORITHMICALLY_ENHANCED = 9
DIGITAL_CREATION = 10
DATA_DRIVEN_MEDIA = 11
TRAINED_ALGORITHMIC_MEDIA = 12
ALGORITHMIC_MEDIA = 13
SCREEN_CAPTURE = 14
VIRTUAL_RECORDING = 15
COMPOSITE = 16
COMPOSITE_CAPTURE = 17
COMPOSITE_SYNTHETIC = 18


class C2paBuilderIntent(enum.IntEnum):
"""Builder intent enumeration.
"""
CREATE = 0 # New digital creation with specified digital source type
EDIT = 1 # Edit of a pre-existing parent asset
UPDATE = 2 # Restricted version of Edit for non-editorial changes


# Mapping from C2paSigningAlg enum to string representation,
# as the enum value currently maps by default to an integer value.
_ALG_TO_STRING_BYTES_MAPPING = {
Expand Down Expand Up @@ -258,6 +290,7 @@ def _clear_error_state():
# Free the error to clear the state
_lib.c2pa_string_free(error)


class C2paSignerInfo(ctypes.Structure):
"""Configuration for a Signer."""
_fields_ = [
Expand Down Expand Up @@ -414,6 +447,10 @@ def _setup_function(func, argtypes, restype=None):
_setup_function(
_lib.c2pa_builder_set_remote_url, [
ctypes.POINTER(C2paBuilder), ctypes.c_char_p], ctypes.c_int)
_setup_function(
_lib.c2pa_builder_set_intent,
[ctypes.POINTER(C2paBuilder), ctypes.c_uint, ctypes.c_uint],
ctypes.c_int)
_setup_function(_lib.c2pa_builder_add_resource, [ctypes.POINTER(
C2paBuilder), ctypes.c_char_p, ctypes.POINTER(C2paStream)], ctypes.c_int)
_setup_function(_lib.c2pa_builder_add_ingredient_from_stream,
Expand Down Expand Up @@ -2570,6 +2607,46 @@ def set_remote_url(self, remote_url: str):
raise C2paError(
Builder._ERROR_MESSAGES['url_error'].format("Unknown error"))

def set_intent(
self,
intent: C2paBuilderIntent,
digital_source_type: C2paDigitalSourceType = (
C2paDigitalSourceType.EMPTY
)
):
"""Set the intent for the manifest.

The intent specifies what kind of manifest to create:
- CREATE: New with specified digital source type.
Must not have a parent ingredient.
- EDIT: Edit of a pre-existing parent asset.
Must have a parent ingredient.
- UPDATE: Restricted version of Edit for non-editorial changes.
Must have only one ingredient as a parent.

Args:
intent: The builder intent (C2paBuilderIntent enum value)
digital_source_type: The digital source type (required
for CREATE intent). Defaults to C2paDigitalSourceType.EMPTY
(for all other cases).

Raises:
C2paError: If there was an error setting the intent
"""
self._ensure_valid_state()

result = _lib.c2pa_builder_set_intent(
self._builder,
ctypes.c_uint(intent),
ctypes.c_uint(digital_source_type),
)

if result != 0:
error = _parse_operation_result_for_error(_lib.c2pa_error())
if error:
raise C2paError(error)
raise C2paError("Error setting intent for Builder: Unknown error")

def add_resource(self, uri: str, stream: Any):
"""Add a resource to the builder.

Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Placeholder
Loading