blob: cfe73fd06fde3e6a685d1c6c1fe258f136957c63 [file] [edit]
// 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_JPEG_WRITER_H_
#define THIRD_PARTY_CREDENTIO_FORMATS_JPEG_WRITER_H_
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>
#include "absl/base/nullability.h"
#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "async/async_context.h"
#include "formats/byte_range.h"
#include "formats/jpeg/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 {
// Writes C2PA manifest stores into JPEG images.
//
// The writer applies no exclusion policy of its own: unless an
// `ExclusionProcessor` is supplied, every byte of the source, other than the
// manifest store APP11 segment(s), is covered by the hard binding hash.
class JpegWriter {
public:
// Information about the C2PA manifest store embedded in the output asset.
struct OutputMetadata {
// Whether the C2PA active manifest embedded in the output asset has a
// timestamp.
bool timestamp_included = false;
};
// A function that determines byte ranges within a given JPEG segment to be
// excluded from the hard binding hash.
using ExclusionProcessor =
absl::AnyInvocable<absl::StatusOr<std::vector<ByteRange>>(
riegeli::Reader&, const JpegSegment&,
const GenerateManifestStoreRequest&) const>;
struct Options {
// If null, no bytes, other than the manifest store APP11 segment(s), are
// excluded from the hard binding hash.
ExclusionProcessor exclusion_processor = nullptr;
};
explicit JpegWriter(const ManifestStoreGenerator* absl_nonnull generator)
: JpegWriter(generator, Options()) {}
JpegWriter(const ManifestStoreGenerator* absl_nonnull generator,
Options 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`.
void Write(GenerateManifestStoreRequest request,
riegeli::Reader* absl_nonnull source,
riegeli::Writer* absl_nonnull destination,
OutputMetadata* absl_nullable output_metadata,
AsyncContext context,
absl::AnyInvocable<void(absl::Status) &&> cb) const;
// Blocking version.
absl::Status Write(
GenerateManifestStoreRequest request,
riegeli::Reader* absl_nonnull source,
riegeli::Writer* absl_nonnull destination,
OutputMetadata* absl_nullable output_metadata = nullptr) const;
// Generates a C2PA manifest store and embeds it into the given asset in
// place.
//
// The supplied writer must support random access and read mode.
void Write(GenerateManifestStoreRequest request,
riegeli::Writer* absl_nonnull asset,
OutputMetadata* absl_nullable output_metadata,
AsyncContext context,
absl::AnyInvocable<void(absl::Status) &&> cb) const;
// Blocking version.
absl::Status Write(
GenerateManifestStoreRequest request, riegeli::Writer* absl_nonnull asset,
OutputMetadata* absl_nullable output_metadata = nullptr) const;
private:
void WriteInternal(
GenerateManifestStoreRequest request,
riegeli::Reader* absl_nonnull source,
OutputMetadata* absl_nullable output_metadata,
std::shared_ptr<std::vector<ByteInstruction>> byte_instructions,
AsyncContext context, absl::AnyInvocable<void(absl::Status) &&> cb) const;
const ManifestStoreGenerator& generator_;
Options options_;
};
} // namespace credentio
#endif // THIRD_PARTY_CREDENTIO_FORMATS_JPEG_WRITER_H_