Coverage Report

Created: 2025-08-29 06:25

/src/openexr/src/lib/OpenEXR/ImfPreviewImageAttribute.cpp
Line
Count
Source (jump to first uncovered line)
1
//
2
// SPDX-License-Identifier: BSD-3-Clause
3
// Copyright (c) Contributors to the OpenEXR Project.
4
//
5
6
//-----------------------------------------------------------------------------
7
//
8
//  class PreviewImageAttribute
9
//
10
//-----------------------------------------------------------------------------
11
12
#define COMPILING_IMF_PREVIEW_IMAGE_ATTRIBUTE
13
#include "ImfPreviewImageAttribute.h"
14
15
#if defined(_MSC_VER)
16
// suppress warning about non-exported base classes
17
#    pragma warning(disable : 4251)
18
#    pragma warning(disable : 4275)
19
#endif
20
21
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
22
23
using namespace OPENEXR_IMF_INTERNAL_NAMESPACE;
24
25
template <>
26
IMF_EXPORT const char*
27
PreviewImageAttribute::staticTypeName ()
28
201
{
29
201
    return "preview";
30
201
}
31
32
template <>
33
IMF_EXPORT void
34
PreviewImageAttribute::writeValueTo (
35
    OPENEXR_IMF_INTERNAL_NAMESPACE::OStream& os, int version) const
36
0
{
37
0
    Xdr::write<StreamIO> (os, _value.width ());
38
0
    Xdr::write<StreamIO> (os, _value.height ());
39
40
0
    int                numPixels = _value.width () * _value.height ();
41
0
    const PreviewRgba* pixels    = _value.pixels ();
42
43
0
    for (int i = 0; i < numPixels; ++i)
44
0
    {
45
0
        Xdr::write<StreamIO> (os, pixels[i].r);
46
0
        Xdr::write<StreamIO> (os, pixels[i].g);
47
0
        Xdr::write<StreamIO> (os, pixels[i].b);
48
0
        Xdr::write<StreamIO> (os, pixels[i].a);
49
0
    }
50
0
}
51
52
template <>
53
IMF_EXPORT void
54
PreviewImageAttribute::readValueFrom (
55
    OPENEXR_IMF_INTERNAL_NAMESPACE::IStream& is, int size, int version)
56
0
{
57
0
    int width, height;
58
59
0
    Xdr::read<StreamIO> (is, width);
60
0
    Xdr::read<StreamIO> (is, height);
61
62
0
    if (width < 0 || height < 0)
63
0
    {
64
0
        throw IEX_NAMESPACE::InputExc (
65
0
            "Invalid dimensions in Preview Image Attribute");
66
0
    }
67
68
    // total attribute size should be four bytes per pixel + 8 bytes for width and height dimensions
69
0
    if (static_cast<uint64_t> (width) * static_cast<uint64_t> (height) * 4l +
70
0
            8l !=
71
0
        static_cast<uint64_t> (size))
72
0
    {
73
0
        throw IEX_NAMESPACE::InputExc (
74
0
            "Mismatch between Preview Image Attribute size and dimensions");
75
0
    }
76
77
0
    PreviewImage p (width, height);
78
79
0
    int          numPixels = p.width () * p.height ();
80
0
    PreviewRgba* pixels    = p.pixels ();
81
82
0
    for (int i = 0; i < numPixels; ++i)
83
0
    {
84
0
        Xdr::read<StreamIO> (is, pixels[i].r);
85
0
        Xdr::read<StreamIO> (is, pixels[i].g);
86
0
        Xdr::read<StreamIO> (is, pixels[i].b);
87
0
        Xdr::read<StreamIO> (is, pixels[i].a);
88
0
    }
89
90
0
    _value = p;
91
0
}
92
93
template class IMF_EXPORT_TEMPLATE_INSTANCE
94
    TypedAttribute<OPENEXR_IMF_INTERNAL_NAMESPACE::PreviewImage>;
95
96
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT