Coverage Report

Created: 2025-11-11 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/guetzli/guetzli/jpeg_data.h
Line
Count
Source
1
/*
2
 * Copyright 2016 Google Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
// Data structures that represent the contents of a jpeg file.
18
19
#ifndef GUETZLI_JPEG_DATA_H_
20
#define GUETZLI_JPEG_DATA_H_
21
22
#include <stddef.h>
23
#include <stdint.h>
24
#include <string>
25
#include <vector>
26
27
#include "guetzli/jpeg_error.h"
28
29
namespace guetzli {
30
31
static const int kDCTBlockSize = 64;
32
static const int kMaxComponents = 4;
33
static const int kMaxQuantTables = 4;
34
static const int kMaxHuffmanTables = 4;
35
static const int kJpegHuffmanMaxBitLength = 16;
36
static const int kJpegHuffmanAlphabetSize = 256;
37
static const int kJpegDCAlphabetSize = 12;
38
static const int kMaxDHTMarkers = 512;
39
40
static const uint8_t kDefaultQuantMatrix[2][64] = {
41
  { 16,  11,  10,  16,  24,  40,  51,  61,
42
    12,  12,  14,  19,  26,  58,  60,  55,
43
    14,  13,  16,  24,  40,  57,  69,  56,
44
    14,  17,  22,  29,  51,  87,  80,  62,
45
    18,  22,  37,  56,  68, 109, 103,  77,
46
    24,  35,  55,  64,  81, 104, 113,  92,
47
    49,  64,  78,  87, 103, 121, 120, 101,
48
    72,  92,  95,  98, 112, 100, 103,  99 },
49
  { 17,  18,  24,  47,  99,  99,  99,  99,
50
    18,  21,  26,  66,  99,  99,  99,  99,
51
    24,  26,  56,  99,  99,  99,  99,  99,
52
    47,  66,  99,  99,  99,  99,  99,  99,
53
    99,  99,  99,  99,  99,  99,  99,  99,
54
    99,  99,  99,  99,  99,  99,  99,  99,
55
    99,  99,  99,  99,  99,  99,  99,  99,
56
    99,  99,  99,  99,  99,  99,  99,  99 }
57
};
58
59
const int kJPEGNaturalOrder[80] = {
60
  0,   1,  8, 16,  9,  2,  3, 10,
61
  17, 24, 32, 25, 18, 11,  4,  5,
62
  12, 19, 26, 33, 40, 48, 41, 34,
63
  27, 20, 13,  6,  7, 14, 21, 28,
64
  35, 42, 49, 56, 57, 50, 43, 36,
65
  29, 22, 15, 23, 30, 37, 44, 51,
66
  58, 59, 52, 45, 38, 31, 39, 46,
67
  53, 60, 61, 54, 47, 55, 62, 63,
68
  // extra entries for safety in decoder
69
  63, 63, 63, 63, 63, 63, 63, 63,
70
  63, 63, 63, 63, 63, 63, 63, 63
71
};
72
73
const int kJPEGZigZagOrder[64] = {
74
  0,   1,  5,  6, 14, 15, 27, 28,
75
  2,   4,  7, 13, 16, 26, 29, 42,
76
  3,   8, 12, 17, 25, 30, 41, 43,
77
  9,  11, 18, 24, 31, 40, 44, 53,
78
  10, 19, 23, 32, 39, 45, 52, 54,
79
  20, 22, 33, 38, 46, 51, 55, 60,
80
  21, 34, 37, 47, 50, 56, 59, 61,
81
  35, 36, 48, 49, 57, 58, 62, 63
82
};
83
84
// Quantization values for an 8x8 pixel block.
85
struct JPEGQuantTable {
86
193k
  JPEGQuantTable() : values(kDCTBlockSize), precision(0),
87
193k
                     index(0), is_last(true) {}
88
89
  std::vector<int> values;
90
  int precision;
91
  // The index of this quantization table as it was parsed from the input JPEG.
92
  // Each DQT marker segment contains an 'index' field, and we save this index
93
  // here. Valid values are 0 to 3.
94
  int index;
95
  // Set to true if this table is the last one within its marker segment.
96
  bool is_last;
97
};
98
99
// Huffman code and decoding lookup table used for DC and AC coefficients.
100
struct JPEGHuffmanCode {
101
218k
  JPEGHuffmanCode() : counts(kJpegHuffmanMaxBitLength + 1),
102
218k
                      values(kJpegHuffmanAlphabetSize + 1),
103
218k
                      slot_id(0),
104
218k
                      is_last(true) {}
105
106
  // Bit length histogram.
107
  std::vector<int> counts;
108
  // Symbol values sorted by increasing bit lengths.
109
  std::vector<int> values;
110
  // The index of the Huffman code in the current set of Huffman codes. For AC
111
  // component Huffman codes, 0x10 is added to the index.
112
  int slot_id;
113
  // Set to true if this Huffman code is the last one within its marker segment.
114
  bool is_last;
115
};
116
117
// Huffman table indexes used for one component of one scan.
118
struct JPEGComponentScanInfo {
119
  int comp_idx;
120
  int dc_tbl_idx;
121
  int ac_tbl_idx;
122
};
123
124
// Contains information that is used in one scan.
125
struct JPEGScanInfo {
126
  // Parameters used for progressive scans (named the same way as in the spec):
127
  //   Ss : Start of spectral band in zig-zag sequence.
128
  //   Se : End of spectral band in zig-zag sequence.
129
  //   Ah : Successive approximation bit position, high.
130
  //   Al : Successive approximation bit position, low.
131
  int Ss;
132
  int Se;
133
  int Ah;
134
  int Al;
135
  std::vector<JPEGComponentScanInfo> components;
136
};
137
138
typedef int16_t coeff_t;
139
140
// Represents one component of a jpeg file.
141
struct JPEGComponent {
142
21.5k
  JPEGComponent() : id(0),
143
21.5k
                    h_samp_factor(1),
144
21.5k
                    v_samp_factor(1),
145
21.5k
                    quant_idx(0),
146
21.5k
                    width_in_blocks(0),
147
21.5k
                    height_in_blocks(0) {}
148
149
  // One-byte id of the component.
150
  int id;
151
  // Horizontal and vertical sampling factors.
152
  // In interleaved mode, each minimal coded unit (MCU) has
153
  // h_samp_factor x v_samp_factor DCT blocks from this component.
154
  int h_samp_factor;
155
  int v_samp_factor;
156
  // The index of the quantization table used for this component.
157
  size_t quant_idx;
158
  // The dimensions of the component measured in 8x8 blocks.
159
  int width_in_blocks;
160
  int height_in_blocks;
161
  int num_blocks;
162
  // The DCT coefficients of this component, laid out block-by-block, divided
163
  // through the quantization matrix values.
164
  std::vector<coeff_t> coeffs;
165
};
166
167
// Represents a parsed jpeg file.
168
struct JPEGData {
169
9.62k
  JPEGData() : width(0),
170
9.62k
               height(0),
171
9.62k
               version(0),
172
9.62k
               max_h_samp_factor(1),
173
9.62k
               max_v_samp_factor(1),
174
9.62k
               MCU_rows(0),
175
9.62k
               MCU_cols(0),
176
9.62k
               restart_interval(0),
177
9.62k
               original_jpg(NULL),
178
9.62k
               original_jpg_size(0),
179
9.62k
               error(JPEG_OK) {}
180
181
  bool Is420() const;
182
  bool Is444() const;
183
184
  int width;
185
  int height;
186
  int version;
187
  int max_h_samp_factor;
188
  int max_v_samp_factor;
189
  int MCU_rows;
190
  int MCU_cols;
191
  int restart_interval;
192
  std::vector<std::string> app_data;
193
  std::vector<std::string> com_data;
194
  std::vector<JPEGQuantTable> quant;
195
  std::vector<JPEGHuffmanCode> huffman_code;
196
  std::vector<JPEGComponent> components;
197
  std::vector<JPEGScanInfo> scan_info;
198
  std::vector<uint8_t> marker_order;
199
  std::vector<std::string> inter_marker_data;
200
  std::string tail_data;
201
  const uint8_t* original_jpg;
202
  size_t original_jpg_size;
203
  JPEGReadError error;
204
};
205
206
void InitJPEGDataForYUV444(int w, int h, JPEGData* jpg);
207
void SaveQuantTables(const int q[3][kDCTBlockSize], JPEGData* jpg);
208
209
}  // namespace guetzli
210
211
#endif  // GUETZLI_JPEG_DATA_H_