Getting resources from a manifest
Manifest data can include binary resources such as thumbnail and icon images which are referenced by JUMBF URIs in manifest data.
- Rust
- C++
- Python
The example below shows how to get resources from manifest data using the Rust library.
Use Reader::from_context to read a manifest and then use resource_to_stream to extract binary resources such as thumbnails:
use c2pa::{Context, Reader, Result};
use std::{fs::File, io::Cursor};
fn main() -> Result<()> {
let context = Context::new();
let stream = File::open("path/to/file.jpg")?;
let reader = Reader::from_context(context)
.with_stream("image/jpeg", stream)?;
if let Some(manifest) = reader.active_manifest() {
if let Some(thumbnail_ref) = manifest.thumbnail_ref() {
let mut thumbnail = Cursor::new(Vec::new());
reader.resource_to_stream(&thumbnail_ref.identifier, &mut thumbnail)?;
println!(
"wrote thumbnail {} of size {}",
thumbnail_ref.format,
thumbnail.get_ref().len()
);
}
}
Ok(())
}
This is how to get resources from a manifest using C++.
#include <iostream>
#include <fstream>
#include <string>
#include "c2pa.hpp"
#include <nlohmann/json.hpp>
// this example uses nlohmann json for parsing the manifest
using json = nlohmann::json;
using namespace std;
namespace fs = std::filesystem;
using namespace c2pa;
int main()
{
fs::path output_path = current_dir / "../target/example/training.jpg";
fs::path thumbnail_path = current_dir / "../target/example/thumbnail.jpg";
try
{
c2pa::Context context;
auto reader = Reader(context, output_path);
auto manifest_store_json = reader.json();
cout << "The new manifest is " << manifest_store_json << endl;
// get the active manifest
json manifest_store = json::parse(manifest_store_json);
if (manifest_store.contains("active_manifest"))
{
string active_manifest = manifest_store["active_manifest"];
json &manifest = manifest_store["manifests"][active_manifest];
string identifier = manifest["thumbnail"]["identifier"];
std::ofstream ofs(thumbnail_path, std::ios::binary);
reader.get_resource(identifier, ofs);
cout << "thumbnail written to " << thumbnail_path << endl;
}
}
catch (c2pa::C2paException const &e)
{
cout << "C2PA Error: " << e.what() << endl;
}
}
The example below shows how to get resources from manifest data using the Python library.
To retrieve binary resources such as thumbnails from the manifest data, use the resource_to_stream method with the associated identifier field value as the URI.
import json
from c2pa import Context, Reader
try:
with Context() as ctx:
with Reader("path/to/media_file.jpg", context=ctx) as reader:
manifest_store = json.loads(reader.json())
active_manifest_label = manifest_store.get("active_manifest")
if active_manifest_label:
manifest = manifest_store["manifests"][active_manifest_label]
# Get the URI to the manifest's thumbnail and write it to a file.
uri = manifest["thumbnail"]["identifier"]
with open("thumbnail_v2.jpg", "w+b") as thumb_file:
reader.resource_to_stream(uri, thumb_file)
except Exception as err:
print(err)