Coverage Report

Created: 2025-07-18 07:09

/src/guetzli/guetzli/jpeg_data.cc
Line
Count
Source (jump to first uncovered line)
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
#include "guetzli/jpeg_data.h"
18
19
#include <assert.h>
20
#include <string.h>
21
22
namespace guetzli {
23
24
4.79k
bool JPEGData::Is420() const {
25
4.79k
  return (components.size() == 3 &&
26
4.79k
          max_h_samp_factor == 2 &&
27
4.79k
          max_v_samp_factor == 2 &&
28
4.79k
          components[0].h_samp_factor == 2 &&
29
4.79k
          components[0].v_samp_factor == 2 &&
30
4.79k
          components[1].h_samp_factor == 1 &&
31
4.79k
          components[1].v_samp_factor == 1 &&
32
4.79k
          components[2].h_samp_factor == 1 &&
33
4.79k
          components[2].v_samp_factor == 1);
34
4.79k
}
35
36
3.42k
bool JPEGData::Is444() const {
37
3.42k
  return (components.size() == 3 &&
38
3.42k
          max_h_samp_factor == 1 &&
39
3.42k
          max_v_samp_factor == 1 &&
40
3.42k
          components[0].h_samp_factor == 1 &&
41
3.42k
          components[0].v_samp_factor == 1 &&
42
3.42k
          components[1].h_samp_factor == 1 &&
43
3.42k
          components[1].v_samp_factor == 1 &&
44
3.42k
          components[2].h_samp_factor == 1 &&
45
3.42k
          components[2].v_samp_factor == 1);
46
3.42k
}
47
48
0
void InitJPEGDataForYUV444(int w, int h, JPEGData* jpg) {
49
0
  jpg->width = w;
50
0
  jpg->height = h;
51
0
  jpg->max_h_samp_factor = 1;
52
0
  jpg->max_v_samp_factor = 1;
53
0
  jpg->MCU_rows = (h + 7) >> 3;
54
0
  jpg->MCU_cols = (w + 7) >> 3;
55
0
  jpg->quant.resize(3);
56
0
  jpg->components.resize(3);
57
0
  for (int i = 0; i < 3; ++i) {
58
0
    JPEGComponent* c = &jpg->components[i];
59
0
    c->id = i;
60
0
    c->h_samp_factor = 1;
61
0
    c->v_samp_factor = 1;
62
0
    c->quant_idx = i;
63
0
    c->width_in_blocks = jpg->MCU_cols;
64
0
    c->height_in_blocks = jpg->MCU_rows;
65
0
    c->num_blocks = c->width_in_blocks * c->height_in_blocks;
66
0
    c->coeffs.resize(c->num_blocks * kDCTBlockSize);
67
0
  }
68
0
}
69
70
166k
void SaveQuantTables(const int q[3][kDCTBlockSize], JPEGData* jpg) {
71
166k
  const size_t kTableSize = kDCTBlockSize * sizeof(q[0][0]);
72
166k
  jpg->quant.clear();
73
166k
  int num_tables = 0;
74
604k
  for (size_t i = 0; i < jpg->components.size(); ++i) {
75
437k
    JPEGComponent* comp = &jpg->components[i];
76
    // Check if we have this quant table already.
77
437k
    bool found = false;
78
504k
    for (int j = 0; j < num_tables; ++j) {
79
303k
      if (memcmp(&q[i][0], &jpg->quant[j].values[0], kTableSize) == 0) {
80
236k
        comp->quant_idx = j;
81
236k
        found = true;
82
236k
        break;
83
236k
      }
84
303k
    }
85
437k
    if (!found) {
86
200k
      JPEGQuantTable table;
87
200k
      memcpy(&table.values[0], &q[i][0], kTableSize);
88
200k
      table.precision = 0;
89
13.0M
      for (int k = 0; k < kDCTBlockSize; ++k) {
90
12.8M
        assert(table.values[k] >= 0);
91
12.8M
        assert(table.values[k] < (1 << 16));
92
12.8M
        if (table.values[k] > 0xff) {
93
2.22k
          table.precision = 1;
94
2.22k
        }
95
12.8M
      }
96
200k
      table.index = num_tables;
97
200k
      comp->quant_idx = num_tables;
98
200k
      jpg->quant.push_back(table);
99
200k
      ++num_tables;
100
200k
    }
101
437k
  }
102
166k
}
103
104
}  // namespace guetzli