blob: 6bb76ff9ddaa8905bb0e256d202a477a07e0cee6 [file]
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef THIRD_PARTY_CREDENTIO_FORMATS_TIFF_WRITER_H_
#define THIRD_PARTY_CREDENTIO_FORMATS_TIFF_WRITER_H_
#include <cstdint>
#include <utility>
#include <vector>
#include "absl/base/nullability.h"
#include "absl/container/flat_hash_set.h"
#include "absl/functional/any_invocable.h"
#include "absl/log/die_if_null.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "formats/byte_range.h"
#include "formats/tiff/reader.h"
#include "generator/generator.h"
#include "proto/generator.pb.h"
#include "riegeli/bytes/reader.h"
#include "riegeli/bytes/writer.h"
#include "utils/byte_instruction.h"
namespace credentio {
// Writer of C2PA manifest stores into TIFF images.
class TiffWriter {
public:
struct OutputMetadata {
bool timestamp_included = false;
};
// A function that determines byte ranges associated with a given IFD entry to
// be excluded from hashing.
using ExclusionProcessor =
absl::AnyInvocable<absl::StatusOr<std::vector<ByteRange>>(
riegeli::Reader&, const TiffImageFileDirectoryEntry&, TiffEndianness,
absl::flat_hash_set<uint32_t>&, const GenerateManifestStoreRequest&)
const>;
struct Options {
ExclusionProcessor exclusion_processor = nullptr;
};
// Creates a TIFF writer.
//
// The generator must outlive the writer.
explicit TiffWriter(const ManifestStoreGenerator* absl_nonnull generator)
: TiffWriter(generator, Options{}) {}
TiffWriter(const ManifestStoreGenerator* absl_nonnull generator,
Options options)
: generator_(*ABSL_DIE_IF_NULL(generator)),
options_(std::move(options)) {}
// Generates a C2PA manifest store and embeds it into a copy of the given
// asset. The riegeli::Reader and riegeli::Writer cannot be pointed to the
// same underlying data. The request must not include `hard_binding`.
// Fails if C2PA metadata already exists.
absl::Status Write(
GenerateManifestStoreRequest request,
riegeli::Reader* absl_nonnull source,
riegeli::Writer* absl_nonnull destination,
OutputMetadata* absl_nullable output_metadata = nullptr) const;
// Generates and embeds C2PA manifest in place into the given asset.
absl::Status Write(
GenerateManifestStoreRequest request, riegeli::Writer* absl_nonnull asset,
OutputMetadata* absl_nullable output_metadata = nullptr) const;
private:
absl::StatusOr<std::vector<ByteInstruction>> WriteInternal(
GenerateManifestStoreRequest request,
riegeli::Reader* absl_nonnull source,
OutputMetadata* absl_nullable output_metadata) const;
const ManifestStoreGenerator& generator_;
Options options_;
};
} // namespace credentio
#endif // THIRD_PARTY_CREDENTIO_FORMATS_TIFF_WRITER_H_