Skip to content
Merged
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
13 changes: 11 additions & 2 deletions include/avif/avif_cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,25 @@ struct UniquePtrDeleter
void operator()(avifEncoder * encoder) const { avifEncoderDestroy(encoder); }
void operator()(avifGainMap * gainMap) const { avifGainMapDestroy(gainMap); }
void operator()(avifImage * image) const { avifImageDestroy(image); }
void operator()(avifRGBImage * image) const { avifRGBImageFreePixels(image); }
};

// Use these unique_ptr to ensure the structs are automatically destroyed.
using DecoderPtr = std::unique_ptr<avifDecoder, UniquePtrDeleter>;
using EncoderPtr = std::unique_ptr<avifEncoder, UniquePtrDeleter>;
using GainMapPtr = std::unique_ptr<avifGainMap, UniquePtrDeleter>;
using ImagePtr = std::unique_ptr<avifImage, UniquePtrDeleter>;

// Automatically cleans the ressources of the avifRGBImage.
// To use when RGBImage actually owns the pixels. RGBImage can also be used as a view, in which case it does not own the pixels.
using RGBImagePtr = std::unique_ptr<avifRGBImage, UniquePtrDeleter>;
class RGBImageCleanup
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vignesh: Please review. This is modeled after absl::MakeCleanup. Thanks!

{
public:
RGBImageCleanup(avifRGBImage * rgb) : rgb_(rgb) {}
~RGBImageCleanup() { avifRGBImageFreePixels(rgb_); }

private:
avifRGBImage * rgb_ = nullptr;
};

} // namespace avif

Expand Down
12 changes: 12 additions & 0 deletions tests/gtest/avifbasictest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,17 @@ TEST(BasicTest, EncodeDecode) {
// testutil::WriteImage(decoded.get(), "/tmp/avifbasictest.png");
}

TEST(BasicTest, RGBImageCleanup) {
// Make sure the following does not create sanitizer bugs.
ImagePtr image =
testutil::CreateImage(/*width=*/12, /*height=*/34, /*depth=*/8,
AVIF_PIXEL_FORMAT_YUV420, AVIF_PLANES_ALL);

avifRGBImage rgb;
avifRGBImageSetDefaults(&rgb, image.get());
RGBImageCleanup cleanup(&rgb);
ASSERT_EQ(avifRGBImageAllocatePixels(&rgb), AVIF_RESULT_OK);
}

} // namespace
} // namespace avif
Loading