Coverage Report

Created: 2026-06-30 07:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/extras/enc/pgx.cc
Line
Count
Source
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#include "lib/extras/enc/pgx.h"
7
8
#include <jxl/codestream_header.h>
9
#include <jxl/types.h>
10
11
#include <cstdint>
12
#include <cstdio>
13
#include <cstring>
14
#include <memory>
15
#include <vector>
16
17
#include "lib/extras/enc/encode.h"
18
#include "lib/extras/packed_image.h"
19
#include "lib/jxl/base/byte_order.h"
20
#include "lib/jxl/base/common.h"
21
#include "lib/jxl/base/data_parallel.h"
22
#include "lib/jxl/base/status.h"
23
24
namespace jxl {
25
namespace extras {
26
namespace {
27
28
constexpr size_t kMaxHeaderSize = 200;
29
30
Status EncodeHeader(const PackedImage& image, const JxlBasicInfo& info,
31
0
                    char* header, int* chars_written) {
32
0
  if (info.alpha_bits > 0) {
33
0
    return JXL_FAILURE("PGX: can't store alpha");
34
0
  }
35
0
  if (info.num_color_channels != 1) {
36
0
    return JXL_FAILURE("PGX: must be grayscale");
37
0
  }
38
  // TODO(lode): verify other bit depths: for other bit depths such as 1 or 4
39
  // bits, have a test case to verify it works correctly. For bits > 16, we may
40
  // need to change the way external_image works.
41
0
  if (info.bits_per_sample != 8 && info.bits_per_sample != 16) {
42
0
    return JXL_FAILURE("PGX: bits other than 8 or 16 not yet supported");
43
0
  }
44
45
0
  unsigned int xsize = image.xsize;
46
0
  unsigned int ysize = image.ysize;
47
0
  if (xsize != image.xsize || ysize != image.ysize) {
48
0
    return JXL_FAILURE("PGX: image too large");
49
0
  }
50
51
  // Use ML (Big Endian), LM may not be well supported by all decoders.
52
0
  *chars_written = snprintf(header, kMaxHeaderSize, "PG ML + %u %u %u\n",
53
0
                            info.bits_per_sample, xsize, ysize);
54
0
  JXL_RETURN_IF_ERROR(static_cast<unsigned int>(*chars_written) <
55
0
                      kMaxHeaderSize);
56
0
  return true;
57
0
}
58
59
Status EncodeImagePGX(const PackedFrame& frame, const JxlBasicInfo& info,
60
0
                      std::vector<uint8_t>* bytes) {
61
0
  char header[kMaxHeaderSize];
62
0
  int header_size = 0;
63
0
  const PackedImage& color = frame.color;
64
0
  JXL_RETURN_IF_ERROR(EncodeHeader(color, info, header, &header_size));
65
66
0
  const JxlPixelFormat format = color.format;
67
0
  const uint8_t* in = reinterpret_cast<const uint8_t*>(color.pixels());
68
0
  JXL_RETURN_IF_ERROR(PackedImage::ValidateDataType(format.data_type));
69
0
  size_t data_bits_per_sample = PackedImage::BitsPerChannel(format.data_type);
70
0
  size_t bytes_per_sample = data_bits_per_sample / kBitsPerByte;
71
0
  size_t num_samples = static_cast<size_t>(color.xsize) * color.ysize;
72
73
0
  if (info.bits_per_sample != data_bits_per_sample) {
74
0
    return JXL_FAILURE("Bit depth does not match pixel data type");
75
0
  }
76
77
0
  std::vector<uint8_t> pixels(num_samples * bytes_per_sample);
78
79
0
  if (format.data_type == JXL_TYPE_UINT8) {
80
0
    memcpy(pixels.data(), in, num_samples * bytes_per_sample);
81
0
  } else if (format.data_type == JXL_TYPE_UINT16) {
82
0
    if (format.endianness != JXL_BIG_ENDIAN) {
83
0
      const uint8_t* p_in = in;
84
0
      uint8_t* p_out = pixels.data();
85
0
      for (size_t i = 0; i < num_samples; ++i, p_in += 2, p_out += 2) {
86
0
        StoreBE16(LoadLE16(p_in), p_out);
87
0
      }
88
0
    } else {
89
0
      memcpy(pixels.data(), in, num_samples * bytes_per_sample);
90
0
    }
91
0
  } else {
92
0
    return JXL_FAILURE("Unsupported pixel data type");
93
0
  }
94
95
0
  bytes->resize(static_cast<size_t>(header_size) + pixels.size());
96
0
  memcpy(bytes->data(), header, static_cast<size_t>(header_size));
97
0
  memcpy(bytes->data() + header_size, pixels.data(), pixels.size());
98
99
0
  return true;
100
0
}
101
102
class PGXEncoder : public Encoder {
103
 public:
104
0
  std::vector<JxlPixelFormat> AcceptedFormats() const override {
105
0
    std::vector<JxlPixelFormat> formats;
106
0
    for (const JxlDataType data_type : {JXL_TYPE_UINT8, JXL_TYPE_UINT16}) {
107
0
      for (JxlEndianness endianness : {JXL_BIG_ENDIAN, JXL_LITTLE_ENDIAN}) {
108
0
        formats.push_back(JxlPixelFormat{/*num_channels=*/1,
109
0
                                         /*data_type=*/data_type,
110
0
                                         /*endianness=*/endianness,
111
0
                                         /*align=*/0});
112
0
      }
113
0
    }
114
0
    return formats;
115
0
  }
116
  Status Encode(const PackedPixelFile& ppf, EncodedImage* encoded_image,
117
0
                ThreadPool* pool) const override {
118
0
    JXL_RETURN_IF_ERROR(VerifyBasicInfo(ppf.info));
119
0
    encoded_image->icc.assign(ppf.icc.begin(), ppf.icc.end());
120
0
    encoded_image->bitstreams.clear();
121
0
    encoded_image->bitstreams.reserve(ppf.frames.size());
122
0
    for (const auto& frame : ppf.frames) {
123
0
      JXL_RETURN_IF_ERROR(VerifyPackedImage(frame.color, ppf.info));
124
0
      encoded_image->bitstreams.emplace_back();
125
0
      JXL_RETURN_IF_ERROR(
126
0
          EncodeImagePGX(frame, ppf.info, &encoded_image->bitstreams.back()));
127
0
    }
128
0
    return true;
129
0
  }
130
};
131
132
}  // namespace
133
134
0
std::unique_ptr<Encoder> GetPGXEncoder() {
135
0
  return jxl::make_unique<PGXEncoder>();
136
0
}
137
138
}  // namespace extras
139
}  // namespace jxl