Repository: SerenityOS/serenity
File: Userland/Libraries/LibGfx/ImageFormats/TIFFWriter.cpp (C++)

Code before:
}

static TIFF::Type type_for_ifd_entry(IFDEntry const& entry)
{
    VERIFY(!entry.value.is_empty());

    return entry.value[0].visit(
        [&](u32) {
            bool fits_in_short = true;
            for (auto const& v : entry.value) {
                if (v.get<u32>() > 0xFFFF) {
                    fits_in_short = false;
                    break;
                }
            }
            if (fits_in_short)
                return TIFF::Type::UnsignedShort;
            return TIFF::Type::UnsignedLong;
        },

        [](TIFF::Rational<u32>) { return TIFF::Type::UnsignedRational; },

        // FIXME: Add more types as needed.
        [](auto const&) -> TIFF::Type { TODO(); });
}

static u32 ifd_entry_value_count(IFDEntry const& entry)
{
    return entry.value.size();
}

static ErrorOr<void> encode_ifd_entry_value_data(Stream& stream, IFDEntry const& entry)
{
    switch (type_for_ifd_entry(entry)) {
    case TIFF::Type::UnsignedShort:
        for (auto const& value : entry.value)
            TRY(stream.write_value<LittleEndian<u16>>(value.get<u32>()));
        break;
    case TIFF::Type::UnsignedLong:
        for (auto const& value : entry.value)
            TRY(stream.write_value<LittleEndian<u32>>(value.get<u32>()));
        break;
    case TIFF::Type::UnsignedRational:
        for (auto const& v : entry.value) {
            auto const& rational = v.get<TIFF::Rational<u32>>();
            TRY(stream.write_value<LittleEndian<u32>>(rational.numerator));
            TRY(stream.write_value<LittleEndian<u32>>(rational.denominator));
        }
        break;
    default:
        VERIFY_NOT_REACHED();

Review comment (suggestion):
> This will do more to add commit for writing tags with type Undefined in the next commit.

I'm unable to understand what you meant here.

Code after:
}

static TIFF::Type type_for_ifd_entry(IFDEntry const& entry)
{
    VERIFY(!entry.value.is_empty());

    return entry.value[0].visit(
        [](ByteBuffer const&) { return TIFF::Type::Undefined; },

        [&](u32) {
            bool fits_in_short = true;
            for (auto const& v : entry.value) {
                if (v.get<u32>() > 0xFFFF) {
                    fits_in_short = false;
                    break;
                }
            }
            if (fits_in_short)
                return TIFF::Type::UnsignedShort;
            return TIFF::Type::UnsignedLong;
        },

        [](TIFF::Rational<u32>) { return TIFF::Type::UnsignedRational; },

        // FIXME: Add more types as needed.
        [](auto const&) -> TIFF::Type { TODO(); });
}

static u32 ifd_entry_value_count(IFDEntry const& entry)
{
    VERIFY(!entry.value.is_empty());
    if (entry.value[0].has<ByteBuffer>()) {
        VERIFY(entry.value.size() == 1);
        return entry.value[0].get<ByteBuffer>().size();
    }
    return entry.value.size();
}

static ErrorOr<void> encode_ifd_entry_value_data(Stream& stream, IFDEntry const& entry)
{
    switch (type_for_ifd_entry(entry)) {
    case TIFF::Type::UnsignedShort:
        for (auto const& value : entry.value)
            TRY(stream.write_value<LittleEndian<u16>>(value.get<u32>()));
        break;
    case TIFF::Type::UnsignedLong:
        for (auto const& value : entry.value)
            TRY(stream.write_value<LittleEndian<u32>>(value.get<u32>()));
        break;
    case TIFF::Type::UnsignedRational:
        for (auto const& v : entry.value) {