Coverage Report

Created: 2025-09-08 07:52

/src/openexr/src/lib/OpenEXR/ImfCompressor.h
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
#ifndef INCLUDED_IMF_COMPRESSOR_H
7
#define INCLUDED_IMF_COMPRESSOR_H
8
9
//-----------------------------------------------------------------------------
10
//
11
//  class Compressor
12
//
13
//-----------------------------------------------------------------------------
14
15
#include "ImfForward.h"
16
17
#include "ImfCompression.h"
18
19
#include "ImfContext.h"
20
21
#include "openexr_compression.h"
22
23
#include <ImathBox.h>
24
25
#include <stdlib.h>
26
#include <memory>
27
28
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
29
30
class IMF_EXPORT_TYPE Compressor
31
{
32
public:
33
    //---------------------------------------------
34
    // Constructor -- hdr is the header of the file
35
    // that will be compressed or uncompressed
36
    //---------------------------------------------
37
38
    IMF_EXPORT
39
    Compressor (const Header& hdr,
40
                exr_compression_t compression_type,
41
                size_t maxScanLineSize,
42
                int scanlines = -1);
43
44
    //-----------
45
    // Destructor
46
    //-----------
47
48
    IMF_EXPORT
49
    virtual ~Compressor ();
50
51
    Compressor (const Compressor& other)            = delete;
52
    Compressor& operator= (const Compressor& other) = delete;
53
    Compressor (Compressor&& other)                 = delete;
54
    Compressor& operator= (Compressor&& other)      = delete;
55
56
    //----------------------------------------------
57
    // Maximum number of scan lines processed by
58
    // a single call to compress() and uncompress().
59
    //----------------------------------------------
60
61
    IMF_EXPORT
62
    virtual int numScanLines () const;
63
64
    //--------------------------------------------
65
    // Format of the pixel data read and written
66
    // by the compress() and uncompress() methods.
67
    // The default implementation of format()
68
    // returns XDR.
69
    //--------------------------------------------
70
71
    enum IMF_EXPORT_ENUM Format
72
    {
73
        NATIVE, // the machine's native format
74
        XDR     // Xdr format
75
    };
76
77
    IMF_EXPORT
78
    virtual Format format () const;
79
80
    //----------------------------
81
    // Access to the file's header
82
    //----------------------------
83
84
0
    const Header& header () const { return _header; }
85
86
    //-------------------------------------------------------------------------
87
    // Compress an array of bytes that represents the contents of up to
88
    // numScanLines() scan lines:
89
    //
90
    //      inPtr   Input buffer (uncompressed data).
91
    //
92
    //      inSize    Number of bytes in the input buffer
93
    //
94
    //      minY    Minimum y coordinate of the scan lines to
95
    //        be compressed
96
    //
97
    //      outPtr    Pointer to output buffer
98
    //
99
    //      return value  Size of compressed data in output buffer
100
    //
101
    // Arrangement of uncompressed pixel data in the input buffer:
102
    //
103
    //  Before calling
104
    //
105
    //          compress (buf, size, minY, ...);
106
    //
107
    //  the InputFile::writePixels() method gathers pixel data from the
108
    //  frame buffer, fb, and places them in buffer buf, like this:
109
    //
110
    //  char *endOfBuf = buf;
111
    //
112
    //  for (int y = minY;
113
    //       y <= min (minY + numScanLines() - 1, header().dataWindow().max.y);
114
    //       ++y)
115
    //  {
116
    //      for (ChannelList::ConstIterator c = header().channels().begin();
117
    //     c != header().channels().end();
118
    //     ++c)
119
    //      {
120
    //    if (modp (y, c.channel().ySampling) != 0)
121
    //        continue;
122
    //
123
    //    for (int x = header().dataWindow().min.x;
124
    //         x <= header().dataWindow().max.x;
125
    //         ++x)
126
    //    {
127
    //        if (modp (x, c.channel().xSampling) != 0)
128
    //      continue;
129
    //
130
    //        Xdr::write<CharPtrIO> (endOfBuf, fb.pixel (c, x, y));
131
    //    }
132
    //      }
133
    //  }
134
    //
135
    //  int size = endOfBuf - buf;
136
    //
137
    //-------------------------------------------------------------------------
138
139
    virtual int
140
    compress (const char* inPtr, int inSize, int minY, const char*& outPtr);
141
142
    IMF_EXPORT
143
    virtual int compressTile (
144
        const char*            inPtr,
145
        int                    inSize,
146
        IMATH_NAMESPACE::Box2i range,
147
        const char*&           outPtr);
148
149
    //-------------------------------------------------------------------------
150
    // Uncompress an array of bytes that has been compressed by compress():
151
    //
152
    //      inPtr   Input buffer (compressed data).
153
    //
154
    //      inSize    Number of bytes in the input buffer
155
    //
156
    //      minY    Minimum y coordinate of the scan lines to
157
    //        be uncompressed
158
    //
159
    //      outPtr    Pointer to output buffer
160
    //
161
    //      return value  Size of uncompressed data in output buffer
162
    //
163
    //-------------------------------------------------------------------------
164
165
    virtual int uncompress (
166
        const char* inPtr, int inSize, int minY, const char*& outPtr);
167
168
    IMF_EXPORT
169
    virtual int uncompressTile (
170
        const char*            inPtr,
171
        int                    inSize,
172
        IMATH_NAMESPACE::Box2i range,
173
        const char*&           outPtr);
174
175
0
    void setExpectedSize (size_t sz) { _expectedSize = sz; }
176
0
    void setTileLevel (int lx, int ly) { _levelX = lx; _levelY = ly; }
177
178
0
    exr_storage_t storageType () const { return _store_type; }
179
0
    void setStorageType (exr_storage_t st) { _store_type = st; }
180
181
protected:
182
    Context _ctxt;
183
    const Header& _header;
184
185
    size_t _maxScanLineSize = 0;
186
    int _numScanLines = -1;
187
188
    exr_compression_t _comp_type;
189
    exr_storage_t _store_type;
190
191
    exr_decode_pipeline_t _decoder = EXR_DECODE_PIPELINE_INITIALIZER;
192
    exr_encode_pipeline_t _encoder = EXR_ENCODE_PIPELINE_INITIALIZER;
193
    bool _decoder_init = false;
194
    bool _encoder_init = false;
195
    std::unique_ptr<char[]> _memory_buffer;
196
    uint64_t _buf_sz = 0;
197
    size_t _expectedSize = 0;
198
199
    int _levelX = 0;
200
    int _levelY = 0;
201
202
    uint64_t runEncodeStep (
203
        const char* inPtr,
204
        int inSize,
205
        IMATH_NAMESPACE::Box2i range,
206
        const char*& outPtr);
207
    uint64_t runDecodeStep (
208
        const char* inPtr,
209
        int inSize,
210
        IMATH_NAMESPACE::Box2i range,
211
        const char*& outPtr);
212
};
213
214
//-----------------------------------------------------------------
215
// Construct a Compressor for compression type c:
216
//
217
//  maxScanLineSize Maximum number of bytes per uncompressed
218
//      scan line.
219
//
220
//  header    Header of the input or output file whose
221
//      pixels will be compressed or uncompressed.
222
//
223
//  return value  A pointer to a new Compressor object (it
224
//      is the caller's responsibility to delete
225
//      the object), or 0 (if c is NO_COMPRESSION).
226
//
227
//-----------------------------------------------------------------
228
229
IMF_EXPORT
230
Compressor*
231
newCompressor (Compression c, size_t maxScanLineSize, const Header& hdr);
232
233
//-----------------------------------------------------------------
234
// Construct a Compressor for compression type c for a tiled image:
235
//
236
//  tileLineSize  Maximum number of bytes per uncompressed
237
//      line in a tile.
238
//
239
//  numTileLines  Maximum number of lines in a tile.
240
//
241
//  header    Header of the input or output file whose
242
//      pixels will be compressed or uncompressed.
243
//
244
//  return value  A pointer to a new Compressor object (it
245
//      is the caller's responsibility to delete
246
//      the object), or 0 (if c is NO_COMPRESSION).
247
//
248
//-----------------------------------------------------------------
249
250
IMF_EXPORT
251
Compressor* newTileCompressor (
252
    Compression c, size_t tileLineSize, size_t numTileLines, const Header& hdr);
253
254
//-----------------------------------------------------------------
255
// Return the maximum number of scanlines in each chunk
256
// of a scanline image using the given compression scheme
257
//-----------------------------------------------------------------
258
259
IMF_EXPORT
260
int numLinesInBuffer (Compression comp);
261
262
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
263
264
#endif