Coverage Report

Created: 2023-08-28 07:24

/src/libjxl/lib/extras/enc/pnm.cc
Line
Count
Source (jump to first uncovered line)
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/pnm.h"
7
8
#include <stdio.h>
9
#include <string.h>
10
11
#include <string>
12
#include <vector>
13
14
#include "lib/extras/packed_image.h"
15
#include "lib/jxl/base/byte_order.h"
16
#include "lib/jxl/base/compiler_specific.h"
17
#include "lib/jxl/base/printf_macros.h"
18
#include "lib/jxl/base/status.h"
19
#include "lib/jxl/color_management.h"
20
#include "lib/jxl/dec_external_image.h"
21
#include "lib/jxl/enc_color_management.h"
22
#include "lib/jxl/enc_external_image.h"
23
#include "lib/jxl/enc_image_bundle.h"
24
#include "lib/jxl/fields.h"  // AllDefault
25
#include "lib/jxl/image.h"
26
#include "lib/jxl/image_bundle.h"
27
28
namespace jxl {
29
namespace extras {
30
namespace {
31
32
constexpr size_t kMaxHeaderSize = 200;
33
34
class PNMEncoder : public Encoder {
35
 public:
36
  Status Encode(const PackedPixelFile& ppf, EncodedImage* encoded_image,
37
0
                ThreadPool* pool = nullptr) const override {
38
0
    JXL_RETURN_IF_ERROR(VerifyBasicInfo(ppf.info));
39
0
    if (!ppf.metadata.exif.empty() || !ppf.metadata.iptc.empty() ||
40
0
        !ppf.metadata.jumbf.empty() || !ppf.metadata.xmp.empty()) {
41
0
      JXL_WARNING("PNM encoder ignoring metadata - use a different codec");
42
0
    }
43
0
    encoded_image->icc = ppf.icc;
44
0
    encoded_image->bitstreams.clear();
45
0
    encoded_image->bitstreams.reserve(ppf.frames.size());
46
0
    for (const auto& frame : ppf.frames) {
47
0
      JXL_RETURN_IF_ERROR(VerifyPackedImage(frame.color, ppf.info));
48
0
      encoded_image->bitstreams.emplace_back();
49
0
      JXL_RETURN_IF_ERROR(
50
0
          EncodeFrame(ppf, frame, &encoded_image->bitstreams.back()));
51
0
    }
52
0
    for (size_t i = 0; i < ppf.extra_channels_info.size(); ++i) {
53
0
      const auto& ec_info = ppf.extra_channels_info[i].ec_info;
54
0
      encoded_image->extra_channel_bitstreams.emplace_back();
55
0
      auto& ec_bitstreams = encoded_image->extra_channel_bitstreams.back();
56
0
      for (const auto& frame : ppf.frames) {
57
0
        ec_bitstreams.emplace_back();
58
0
        JXL_RETURN_IF_ERROR(EncodeExtraChannel(frame.extra_channels[i],
59
0
                                               ec_info.bits_per_sample,
60
0
                                               &ec_bitstreams.back()));
61
0
      }
62
0
    }
63
0
    return true;
64
0
  }
65
66
 protected:
67
  virtual Status EncodeFrame(const PackedPixelFile& ppf,
68
                             const PackedFrame& frame,
69
                             std::vector<uint8_t>* bytes) const = 0;
70
  virtual Status EncodeExtraChannel(const PackedImage& image,
71
                                    size_t bits_per_sample,
72
                                    std::vector<uint8_t>* bytes) const = 0;
73
};
74
75
class PPMEncoder : public PNMEncoder {
76
 public:
77
0
  std::vector<JxlPixelFormat> AcceptedFormats() const override {
78
0
    return {JxlPixelFormat{3, JXL_TYPE_UINT8, JXL_BIG_ENDIAN, 0},
79
0
            JxlPixelFormat{3, JXL_TYPE_UINT16, JXL_BIG_ENDIAN, 0}};
80
0
  }
81
  Status EncodeFrame(const PackedPixelFile& ppf, const PackedFrame& frame,
82
0
                     std::vector<uint8_t>* bytes) const override {
83
0
    return EncodeImage(frame.color, ppf.info.bits_per_sample, bytes);
84
0
  }
85
  Status EncodeExtraChannel(const PackedImage& image, size_t bits_per_sample,
86
0
                            std::vector<uint8_t>* bytes) const override {
87
0
    return EncodeImage(image, bits_per_sample, bytes);
88
0
  }
89
90
 private:
91
  Status EncodeImage(const PackedImage& image, size_t bits_per_sample,
92
0
                     std::vector<uint8_t>* bytes) const {
93
0
    uint32_t maxval = (1u << bits_per_sample) - 1;
94
0
    char type = image.format.num_channels == 1 ? '5' : '6';
95
0
    char header[kMaxHeaderSize];
96
0
    size_t header_size =
97
0
        snprintf(header, kMaxHeaderSize, "P%c\n%" PRIuS " %" PRIuS "\n%u\n",
98
0
                 type, image.xsize, image.ysize, maxval);
99
0
    JXL_RETURN_IF_ERROR(header_size < kMaxHeaderSize);
100
0
    bytes->resize(header_size + image.pixels_size);
101
0
    memcpy(bytes->data(), header, header_size);
102
0
    memcpy(bytes->data() + header_size,
103
0
           reinterpret_cast<uint8_t*>(image.pixels()), image.pixels_size);
104
0
    return true;
105
0
  }
106
};
107
108
class PGMEncoder : public PPMEncoder {
109
 public:
110
0
  std::vector<JxlPixelFormat> AcceptedFormats() const override {
111
0
    return {JxlPixelFormat{1, JXL_TYPE_UINT8, JXL_BIG_ENDIAN, 0},
112
0
            JxlPixelFormat{1, JXL_TYPE_UINT16, JXL_BIG_ENDIAN, 0}};
113
0
  }
114
};
115
116
class PFMEncoder : public PNMEncoder {
117
 public:
118
0
  std::vector<JxlPixelFormat> AcceptedFormats() const override {
119
0
    std::vector<JxlPixelFormat> formats;
120
0
    for (const uint32_t num_channels : {1, 3}) {
121
0
      for (JxlEndianness endianness : {JXL_BIG_ENDIAN, JXL_LITTLE_ENDIAN}) {
122
0
        formats.push_back(JxlPixelFormat{/*num_channels=*/num_channels,
123
0
                                         /*data_type=*/JXL_TYPE_FLOAT,
124
0
                                         /*endianness=*/endianness,
125
0
                                         /*align=*/0});
126
0
      }
127
0
    }
128
0
    return formats;
129
0
  }
130
  Status EncodeFrame(const PackedPixelFile& ppf, const PackedFrame& frame,
131
0
                     std::vector<uint8_t>* bytes) const override {
132
0
    return EncodeImage(frame.color, bytes);
133
0
  }
134
  Status EncodeExtraChannel(const PackedImage& image, size_t bits_per_sample,
135
0
                            std::vector<uint8_t>* bytes) const override {
136
0
    return EncodeImage(image, bytes);
137
0
  }
138
139
 private:
140
  Status EncodeImage(const PackedImage& image,
141
0
                     std::vector<uint8_t>* bytes) const {
142
0
    char type = image.format.num_channels == 1 ? 'f' : 'F';
143
0
    double scale = image.format.endianness == JXL_LITTLE_ENDIAN ? -1.0 : 1.0;
144
0
    char header[kMaxHeaderSize];
145
0
    size_t header_size =
146
0
        snprintf(header, kMaxHeaderSize, "P%c\n%" PRIuS " %" PRIuS "\n%.1f\n",
147
0
                 type, image.xsize, image.ysize, scale);
148
0
    JXL_RETURN_IF_ERROR(header_size < kMaxHeaderSize);
149
0
    bytes->resize(header_size + image.pixels_size);
150
0
    memcpy(bytes->data(), header, header_size);
151
0
    const uint8_t* in = reinterpret_cast<const uint8_t*>(image.pixels());
152
0
    uint8_t* out = bytes->data() + header_size;
153
0
    for (size_t y = 0; y < image.ysize; ++y) {
154
0
      size_t y_out = image.ysize - 1 - y;
155
0
      const uint8_t* row_in = &in[y * image.stride];
156
0
      uint8_t* row_out = &out[y_out * image.stride];
157
0
      memcpy(row_out, row_in, image.stride);
158
0
    }
159
0
    return true;
160
0
  }
161
};
162
163
class PAMEncoder : public PNMEncoder {
164
 public:
165
0
  std::vector<JxlPixelFormat> AcceptedFormats() const override {
166
0
    std::vector<JxlPixelFormat> formats;
167
0
    for (const uint32_t num_channels : {1, 2, 3, 4}) {
168
0
      for (const JxlDataType data_type : {JXL_TYPE_UINT8, JXL_TYPE_UINT16}) {
169
0
        formats.push_back(JxlPixelFormat{/*num_channels=*/num_channels,
170
0
                                         /*data_type=*/data_type,
171
0
                                         /*endianness=*/JXL_BIG_ENDIAN,
172
0
                                         /*align=*/0});
173
0
      }
174
0
    }
175
0
    return formats;
176
0
  }
177
  Status EncodeFrame(const PackedPixelFile& ppf, const PackedFrame& frame,
178
0
                     std::vector<uint8_t>* bytes) const override {
179
0
    const PackedImage& color = frame.color;
180
0
    const auto& ec_info = ppf.extra_channels_info;
181
0
    JXL_RETURN_IF_ERROR(frame.extra_channels.size() == ec_info.size());
182
0
    for (const auto& ec : frame.extra_channels) {
183
0
      if (ec.xsize != color.xsize || ec.ysize != color.ysize) {
184
0
        return JXL_FAILURE("Extra channel and color size mismatch.");
185
0
      }
186
0
      if (ec.format.data_type != color.format.data_type ||
187
0
          ec.format.endianness != color.format.endianness) {
188
0
        return JXL_FAILURE("Extra channel and color format mismatch.");
189
0
      }
190
0
    }
191
0
    if (ppf.info.bits_per_sample != ppf.info.alpha_bits) {
192
0
      return JXL_FAILURE("Alpha bit depth does not match image bit depth");
193
0
    }
194
0
    for (const auto& it : ec_info) {
195
0
      if (it.ec_info.bits_per_sample != ppf.info.bits_per_sample) {
196
0
        return JXL_FAILURE(
197
0
            "Extra channel bit depth does not match image bit depth");
198
0
      }
199
0
    }
200
0
    const char* kColorTypes[4] = {"GRAYSCALE", "GRAYSCALE_ALPHA", "RGB",
201
0
                                  "RGB_ALPHA"};
202
0
    uint32_t maxval = (1u << ppf.info.bits_per_sample) - 1;
203
0
    uint32_t depth = color.format.num_channels + ec_info.size();
204
0
    char header[kMaxHeaderSize];
205
0
    size_t pos = 0;
206
0
    pos += snprintf(header + pos, kMaxHeaderSize - pos,
207
0
                    "P7\nWIDTH %" PRIuS "\nHEIGHT %" PRIuS
208
0
                    "\nDEPTH %u\n"
209
0
                    "MAXVAL %u\nTUPLTYPE %s\n",
210
0
                    color.xsize, color.ysize, depth, maxval,
211
0
                    kColorTypes[color.format.num_channels - 1]);
212
0
    JXL_RETURN_IF_ERROR(pos < kMaxHeaderSize);
213
0
    for (const auto& info : ec_info) {
214
0
      pos += snprintf(header + pos, kMaxHeaderSize - pos, "TUPLTYPE %s\n",
215
0
                      ExtraChannelTypeName(info.ec_info.type).c_str());
216
0
      JXL_RETURN_IF_ERROR(pos < kMaxHeaderSize);
217
0
    }
218
0
    pos += snprintf(header + pos, kMaxHeaderSize - pos, "ENDHDR\n");
219
0
    JXL_RETURN_IF_ERROR(pos < kMaxHeaderSize);
220
0
    size_t total_size = color.pixels_size;
221
0
    for (const auto& ec : frame.extra_channels) {
222
0
      total_size += ec.pixels_size;
223
0
    }
224
0
    bytes->resize(pos + total_size);
225
0
    memcpy(bytes->data(), header, pos);
226
    // If we have no extra channels, just copy color pixel data over.
227
0
    if (frame.extra_channels.empty()) {
228
0
      memcpy(bytes->data() + pos, reinterpret_cast<uint8_t*>(color.pixels()),
229
0
             color.pixels_size);
230
0
      return true;
231
0
    }
232
    // Interleave color and extra channels.
233
0
    const uint8_t* in = reinterpret_cast<const uint8_t*>(color.pixels());
234
0
    std::vector<const uint8_t*> ec_in(frame.extra_channels.size());
235
0
    for (size_t i = 0; i < frame.extra_channels.size(); ++i) {
236
0
      ec_in[i] =
237
0
          reinterpret_cast<const uint8_t*>(frame.extra_channels[i].pixels());
238
0
    }
239
0
    uint8_t* out = bytes->data() + pos;
240
0
    size_t pwidth = PackedImage::BitsPerChannel(color.format.data_type) / 8;
241
0
    for (size_t y = 0; y < color.ysize; ++y) {
242
0
      for (size_t x = 0; x < color.xsize; ++x) {
243
0
        memcpy(out, in, color.pixel_stride());
244
0
        out += color.pixel_stride();
245
0
        in += color.pixel_stride();
246
0
        for (auto& p : ec_in) {
247
0
          memcpy(out, p, pwidth);
248
0
          out += pwidth;
249
0
          p += pwidth;
250
0
        }
251
0
      }
252
0
    }
253
0
    return true;
254
0
  }
255
  Status EncodeExtraChannel(const PackedImage& image, size_t bits_per_sample,
256
0
                            std::vector<uint8_t>* bytes) const override {
257
0
    return true;
258
0
  }
259
260
 private:
261
0
  static std::string ExtraChannelTypeName(JxlExtraChannelType type) {
262
0
    switch (type) {
263
0
      case JXL_CHANNEL_ALPHA:
264
0
        return std::string("Alpha");
265
0
      case JXL_CHANNEL_DEPTH:
266
0
        return std::string("Depth");
267
0
      case JXL_CHANNEL_SPOT_COLOR:
268
0
        return std::string("SpotColor");
269
0
      case JXL_CHANNEL_SELECTION_MASK:
270
0
        return std::string("SelectionMask");
271
0
      case JXL_CHANNEL_BLACK:
272
0
        return std::string("Black");
273
0
      case JXL_CHANNEL_CFA:
274
0
        return std::string("CFA");
275
0
      case JXL_CHANNEL_THERMAL:
276
0
        return std::string("Thermal");
277
0
      default:
278
0
        return std::string("UNKNOWN");
279
0
    }
280
0
  }
281
};
282
283
}  // namespace
284
285
0
std::unique_ptr<Encoder> GetPPMEncoder() {
286
0
  return jxl::make_unique<PPMEncoder>();
287
0
}
288
289
0
std::unique_ptr<Encoder> GetPFMEncoder() {
290
0
  return jxl::make_unique<PFMEncoder>();
291
0
}
292
293
0
std::unique_ptr<Encoder> GetPGMEncoder() {
294
0
  return jxl::make_unique<PGMEncoder>();
295
0
}
296
297
0
std::unique_ptr<Encoder> GetPAMEncoder() {
298
0
  return jxl::make_unique<PAMEncoder>();
299
0
}
300
301
}  // namespace extras
302
}  // namespace jxl