summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/jpeg
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/jpeg-xl/lib/jxl/jpeg')
-rw-r--r--third_party/jpeg-xl/lib/jxl/jpeg/dec_jpeg_data_writer.cc54
-rw-r--r--third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data.cc24
-rw-r--r--third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data_reader.cc8
-rw-r--r--third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data_reader.h2
-rw-r--r--third_party/jpeg-xl/lib/jxl/jpeg/jpeg_data.cc13
5 files changed, 55 insertions, 46 deletions
diff --git a/third_party/jpeg-xl/lib/jxl/jpeg/dec_jpeg_data_writer.cc b/third_party/jpeg-xl/lib/jxl/jpeg/dec_jpeg_data_writer.cc
index 64560d9ab0..31bb2dda23 100644
--- a/third_party/jpeg-xl/lib/jxl/jpeg/dec_jpeg_data_writer.cc
+++ b/third_party/jpeg-xl/lib/jxl/jpeg/dec_jpeg_data_writer.cc
@@ -18,6 +18,7 @@
#include "lib/jxl/base/bits.h"
#include "lib/jxl/base/byte_order.h"
#include "lib/jxl/base/common.h"
+#include "lib/jxl/base/status.h"
#include "lib/jxl/frame_dimensions.h"
#include "lib/jxl/image_bundle.h"
#include "lib/jxl/jpeg/dec_jpeg_serialization_state.h"
@@ -42,7 +43,7 @@ const size_t kJpegBitWriterChunkSize = 16384;
// Returns non-zero if and only if x has a zero byte, i.e. one of
// x & 0xff, x & 0xff00, ..., x & 0xff00000000000000 is zero.
-static JXL_INLINE uint64_t HasZeroByte(uint64_t x) {
+JXL_INLINE uint64_t HasZeroByte(uint64_t x) {
return (x - 0x0101010101010101ULL) & ~x & 0x8080808080808080ULL;
}
@@ -57,7 +58,7 @@ void JpegBitWriterInit(JpegBitWriter* bw,
bw->data = bw->chunk.buffer->data();
}
-static JXL_NOINLINE void SwapBuffer(JpegBitWriter* bw) {
+JXL_NOINLINE void SwapBuffer(JpegBitWriter* bw) {
bw->chunk.len = bw->pos;
bw->output->emplace_back(std::move(bw->chunk));
bw->chunk = OutputChunk(kJpegBitWriterChunkSize);
@@ -65,7 +66,7 @@ static JXL_NOINLINE void SwapBuffer(JpegBitWriter* bw) {
bw->pos = 0;
}
-static JXL_INLINE void Reserve(JpegBitWriter* bw, size_t n_bytes) {
+JXL_INLINE void Reserve(JpegBitWriter* bw, size_t n_bytes) {
if (JXL_UNLIKELY((bw->pos + n_bytes) > kJpegBitWriterChunkSize)) {
SwapBuffer(bw);
}
@@ -77,14 +78,14 @@ static JXL_INLINE void Reserve(JpegBitWriter* bw, size_t n_bytes) {
* This method is "careless" - caller must make sure that there is enough
* space in the output buffer. Emits up to 2 bytes to buffer.
*/
-static JXL_INLINE void EmitByte(JpegBitWriter* bw, int byte) {
+JXL_INLINE void EmitByte(JpegBitWriter* bw, int byte) {
bw->data[bw->pos] = byte;
bw->data[bw->pos + 1] = 0;
bw->pos += (byte != 0xFF ? 1 : 2);
}
-static JXL_INLINE void DischargeBitBuffer(JpegBitWriter* bw, int nbits,
- uint64_t bits) {
+JXL_INLINE void DischargeBitBuffer(JpegBitWriter* bw, int nbits,
+ uint64_t bits) {
// At this point we are ready to emit the put_buffer to the output.
// The JPEG format requires that after every 0xff byte in the entropy
// coded section, there is a zero byte, therefore we first check if any of
@@ -111,7 +112,7 @@ static JXL_INLINE void DischargeBitBuffer(JpegBitWriter* bw, int nbits,
bw->put_buffer = bits << bw->put_bits;
}
-static JXL_INLINE void WriteBits(JpegBitWriter* bw, int nbits, uint64_t bits) {
+JXL_INLINE void WriteBits(JpegBitWriter* bw, int nbits, uint64_t bits) {
JXL_DASSERT(nbits > 0);
bw->put_bits -= nbits;
if (JXL_UNLIKELY(bw->put_bits < 0)) {
@@ -142,12 +143,14 @@ bool JumpToByteBoundary(JpegBitWriter* bw, const uint8_t** pad_bits,
} else {
pad_pattern = 0;
const uint8_t* src = *pad_bits;
- // TODO(eustas): bitwise reading looks insanely ineffective...
+ // TODO(eustas): bitwise reading looks insanely ineffective!
while (n_bits--) {
pad_pattern <<= 1;
if (src >= pad_bits_end) return false;
- // TODO(eustas): DCHECK *src == {0, 1}
- pad_pattern |= !!*(src++);
+ uint8_t bit = *src;
+ src++;
+ JXL_ASSERT(bit <= 1);
+ pad_pattern |= bit;
}
*pad_bits = src;
}
@@ -187,21 +190,20 @@ void DCTCodingStateInit(DCTCodingState* s) {
s->refinement_bits_.reserve(64);
}
-static JXL_INLINE void WriteSymbol(int symbol, HuffmanCodeTable* table,
- JpegBitWriter* bw) {
+JXL_INLINE void WriteSymbol(int symbol, HuffmanCodeTable* table,
+ JpegBitWriter* bw) {
WriteBits(bw, table->depth[symbol], table->code[symbol]);
}
-static JXL_INLINE void WriteSymbolBits(int symbol, HuffmanCodeTable* table,
- JpegBitWriter* bw, int nbits,
- uint64_t bits) {
+JXL_INLINE void WriteSymbolBits(int symbol, HuffmanCodeTable* table,
+ JpegBitWriter* bw, int nbits, uint64_t bits) {
WriteBits(bw, nbits + table->depth[symbol],
bits | (table->code[symbol] << nbits));
}
// Emit all buffered data to the bit stream using the given Huffman code and
// bit writer.
-static JXL_INLINE void Flush(DCTCodingState* s, JpegBitWriter* bw) {
+JXL_INLINE void Flush(DCTCodingState* s, JpegBitWriter* bw) {
if (s->eob_run_ > 0) {
Reserve(bw, 16);
int nbits = FloorLog2Nonzero<uint32_t>(s->eob_run_);
@@ -233,11 +235,9 @@ static JXL_INLINE void Flush(DCTCodingState* s, JpegBitWriter* bw) {
// Buffer some more data at the end-of-band (the last non-zero or newly
// non-zero coefficient within the [Ss, Se] spectral band).
-static JXL_INLINE void BufferEndOfBand(DCTCodingState* s,
- HuffmanCodeTable* ac_huff,
- const int* new_bits_array,
- size_t new_bits_count,
- JpegBitWriter* bw) {
+JXL_INLINE void BufferEndOfBand(DCTCodingState* s, HuffmanCodeTable* ac_huff,
+ const int* new_bits_array,
+ size_t new_bits_count, JpegBitWriter* bw) {
if (s->eob_run_ == 0) {
s->cur_ac_huff_ = ac_huff;
}
@@ -530,7 +530,7 @@ bool EncodeDCTBlockSequential(const coeff_t* coeffs, HuffmanCodeTable* dc_huff,
int dc_nbits = (temp2 == 0) ? 0 : (FloorLog2Nonzero<uint32_t>(temp2) + 1);
WriteSymbol(dc_nbits, dc_huff, bw);
-#if false
+#if JXL_FALSE
// If the input is corrupt, this could be triggered. Checking is
// costly though, so it makes more sense to avoid this branch.
// (producing a corrupt JPEG when the input is corrupt, instead
@@ -543,7 +543,8 @@ bool EncodeDCTBlockSequential(const coeff_t* coeffs, HuffmanCodeTable* dc_huff,
int16_t r = 0;
for (size_t i = 1; i < 64; i++) {
- if ((temp = coeffs[kJPEGNaturalOrder[i]]) == 0) {
+ temp = coeffs[kJPEGNaturalOrder[i]];
+ if (temp == 0) {
r++;
} else {
temp2 = temp >> (8 * sizeof(coeff_t) - 1);
@@ -611,7 +612,8 @@ bool EncodeDCTBlockProgressive(const coeff_t* coeffs, HuffmanCodeTable* dc_huff,
}
int r = 0;
for (int k = Ss; k <= Se; ++k) {
- if ((temp = coeffs[kJPEGNaturalOrder[k]]) == 0) {
+ temp = coeffs[kJPEGNaturalOrder[k]];
+ if (temp == 0) {
r++;
continue;
}
@@ -884,8 +886,8 @@ SerializationStatus JXL_NOINLINE DoEncodeScan(const JPEGData& jpg,
return SerializationStatus::DONE;
}
-static SerializationStatus JXL_INLINE EncodeScan(const JPEGData& jpg,
- SerializationState* state) {
+SerializationStatus JXL_INLINE EncodeScan(const JPEGData& jpg,
+ SerializationState* state) {
const JPEGScanInfo& scan_info = jpg.scan_info[state->scan_index];
const bool is_progressive = state->is_progressive;
const int Al = is_progressive ? scan_info.Al : 0;
diff --git a/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data.cc b/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data.cc
index 97342553e5..d311908415 100644
--- a/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data.cc
+++ b/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data.cc
@@ -8,7 +8,7 @@
#include <brotli/encode.h>
#include "lib/jxl/codec_in_out.h"
-#include "lib/jxl/enc_fields.h"
+#include "lib/jxl/enc_bit_writer.h"
#include "lib/jxl/image_bundle.h"
#include "lib/jxl/jpeg/enc_jpeg_data_reader.h"
#include "lib/jxl/luminance.h"
@@ -76,7 +76,8 @@ bool GetMarkerPayload(const uint8_t* data, size_t size, ByteSpan* payload) {
Status DetectBlobs(jpeg::JPEGData& jpeg_data) {
JXL_DASSERT(jpeg_data.app_data.size() == jpeg_data.app_marker_type.size());
- bool have_exif = false, have_xmp = false;
+ bool have_exif = false;
+ bool have_xmp = false;
for (size_t i = 0; i < jpeg_data.app_data.size(); i++) {
auto& marker = jpeg_data.app_data[i];
if (marker.empty() || marker[0] != kApp1) {
@@ -165,7 +166,7 @@ Status ParseChunkedMarker(const jpeg::JPEGData& src, uint8_t marker_type,
if (!presence[index]) {
return JXL_FAILURE("Missing chunk.");
}
- chunks[index].AppendTo(output);
+ chunks[index].AppendTo(*output);
}
return true;
@@ -173,7 +174,7 @@ Status ParseChunkedMarker(const jpeg::JPEGData& src, uint8_t marker_type,
Status SetBlobsFromJpegData(const jpeg::JPEGData& jpeg_data, Blobs* blobs) {
for (size_t i = 0; i < jpeg_data.app_data.size(); i++) {
- auto& marker = jpeg_data.app_data[i];
+ const auto& marker = jpeg_data.app_data[i];
if (marker.empty() || marker[0] != kApp1) {
continue;
}
@@ -210,7 +211,7 @@ Status SetBlobsFromJpegData(const jpeg::JPEGData& jpeg_data, Blobs* blobs) {
return true;
}
-static inline bool IsJPG(const Span<const uint8_t> bytes) {
+inline bool IsJPG(const Span<const uint8_t> bytes) {
return bytes.size() >= 2 && bytes[0] == 0xFF && bytes[1] == 0xD8;
}
@@ -239,14 +240,16 @@ Status SetChromaSubsamplingFromJpegData(const JPEGData& jpg,
return JXL_FAILURE("Cannot recompress JPEGs with neither 1 nor 3 channels");
}
if (nbcomp == 3) {
- uint8_t hsample[3], vsample[3];
+ uint8_t hsample[3];
+ uint8_t vsample[3];
for (size_t i = 0; i < nbcomp; i++) {
hsample[i] = jpg.components[i].h_samp_factor;
vsample[i] = jpg.components[i].v_samp_factor;
}
JXL_RETURN_IF_ERROR(cs->Set(hsample, vsample));
} else if (nbcomp == 1) {
- uint8_t hsample[3], vsample[3];
+ uint8_t hsample[3];
+ uint8_t vsample[3];
for (size_t i = 0; i < 3; i++) {
hsample[i] = jpg.components[0].h_samp_factor;
vsample[i] = jpg.components[0].v_samp_factor;
@@ -330,7 +333,7 @@ Status EncodeJPEGData(JPEGData& jpeg_data, std::vector<uint8_t>* bytes,
{
PaddedBytes serialized_jpeg_data = std::move(writer).TakeBytes();
bytes->reserve(serialized_jpeg_data.size() + brotli_capacity);
- Bytes(serialized_jpeg_data).AppendTo(bytes);
+ Bytes(serialized_jpeg_data).AppendTo(*bytes);
}
BrotliEncoderState* brotli_enc =
@@ -394,8 +397,9 @@ Status DecodeImageJPG(const Span<const uint8_t> bytes, CodecInOut* io) {
io->metadata.m.SetIntensityTarget(kDefaultIntensityTarget);
io->metadata.m.SetUintSamples(BITS_IN_JSAMPLE);
- io->SetFromImage(Image3F(jpeg_data->width, jpeg_data->height),
- io->metadata.m.color_encoding);
+ JXL_ASSIGN_OR_RETURN(Image3F tmp,
+ Image3F::Create(jpeg_data->width, jpeg_data->height));
+ io->SetFromImage(std::move(tmp), io->metadata.m.color_encoding);
SetIntensityTarget(&io->metadata.m);
return true;
}
diff --git a/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data_reader.cc b/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data_reader.cc
index ce64dae47b..d1e8476db6 100644
--- a/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data_reader.cc
+++ b/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data_reader.cc
@@ -23,7 +23,7 @@ namespace jxl {
namespace jpeg {
namespace {
-static const int kBrunsliMaxSampling = 15;
+const int kBrunsliMaxSampling = 15;
// Macros for commonly used error conditions.
@@ -234,7 +234,7 @@ bool ProcessDHT(const uint8_t* data, const size_t len, JpegReadMode mode,
JPEGHuffmanCode huff;
huff.slot_id = ReadUint8(data, pos);
int huffman_index = huff.slot_id;
- int is_ac_table = (huff.slot_id & 0x10) != 0;
+ bool is_ac_table = ((huff.slot_id & 0x10) != 0);
HuffmanTableEntry* huff_lut;
if (is_ac_table) {
huffman_index -= 0x10;
@@ -292,7 +292,7 @@ bool ProcessDHT(const uint8_t* data, const size_t len, JpegReadMode mode,
}
huff.is_last = (*pos == start_pos + marker_len);
if (mode == JpegReadMode::kReadAll) {
- BuildJpegHuffmanTable(&huff.counts[0], &huff.values[0], huff_lut);
+ BuildJpegHuffmanTable(huff.counts.data(), huff.values.data(), huff_lut);
}
jpg->huffman_code.push_back(huff);
}
@@ -419,7 +419,7 @@ struct BitReaderState {
if (bits_left_ <= 16) {
while (bits_left_ <= 56) {
val_ <<= 8;
- val_ |= (uint64_t)GetNextByte();
+ val_ |= static_cast<uint64_t>(GetNextByte());
bits_left_ += 8;
}
}
diff --git a/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data_reader.h b/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data_reader.h
index 3fad820e9d..4389b2ffe3 100644
--- a/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data_reader.h
+++ b/third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data_reader.h
@@ -27,7 +27,7 @@ enum class JpegReadMode {
// If mode is kReadHeader, it fills in only the image dimensions in *jpg.
// Returns false if the data is not valid JPEG, or if it contains an unsupported
// JPEG feature.
-bool ReadJpeg(const uint8_t* data, const size_t len, JpegReadMode mode,
+bool ReadJpeg(const uint8_t* data, size_t len, JpegReadMode mode,
JPEGData* jpg);
} // namespace jpeg
diff --git a/third_party/jpeg-xl/lib/jxl/jpeg/jpeg_data.cc b/third_party/jpeg-xl/lib/jxl/jpeg/jpeg_data.cc
index 6744e6935a..aeb9914cca 100644
--- a/third_party/jpeg-xl/lib/jxl/jpeg/jpeg_data.cc
+++ b/third_party/jpeg-xl/lib/jxl/jpeg/jpeg_data.cc
@@ -5,6 +5,8 @@
#include "lib/jxl/jpeg/jpeg_data.h"
+#include <jxl/types.h>
+
#include "lib/jxl/base/printf_macros.h"
#include "lib/jxl/base/status.h"
#include "lib/jxl/common.h" // kMaxNumPasses, JPEGXL_ENABLE_TRANSCODE_JPEG
@@ -214,7 +216,7 @@ Status JPEGData::VisitFields(Visitor* visitor) {
huffman_code.resize(num_huff);
}
for (JPEGHuffmanCode& hc : huffman_code) {
- bool is_ac = hc.slot_id >> 4;
+ bool is_ac = ((hc.slot_id >> 4) != 0);
uint32_t id = hc.slot_id & 0xF;
JXL_RETURN_IF_ERROR(visitor->Bool(false, &is_ac));
JXL_RETURN_IF_ERROR(visitor->Bits(2, 0, &id));
@@ -240,7 +242,8 @@ Status JPEGData::VisitFields(Visitor* visitor) {
JXL_RETURN_IF_ERROR(visitor->U32(Bits(2), BitsOffset(2, 4),
BitsOffset(4, 8), BitsOffset(8, 1), 0,
&hc.values[i]));
- value_slots[hc.values[i] >> 6] |= (uint64_t)1 << (hc.values[i] & 0x3F);
+ value_slots[hc.values[i] >> 6] |= static_cast<uint64_t>(1)
+ << (hc.values[i] & 0x3F);
}
if (hc.values[num_symbols - 1] != kJpegHuffmanAlphabetSize) {
return JXL_FAILURE("Missing EOI symbol");
@@ -361,13 +364,13 @@ Status JPEGData::VisitFields(Visitor* visitor) {
for (uint32_t i = 0; i < nbit; i++) {
bool bbit = false;
JXL_RETURN_IF_ERROR(visitor->Bool(false, &bbit));
- padding_bits.push_back(bbit);
+ padding_bits.push_back(TO_JXL_BOOL(bbit));
}
} else {
for (uint8_t& bit : padding_bits) {
- bool bbit = bit;
+ bool bbit = FROM_JXL_BOOL(bit);
JXL_RETURN_IF_ERROR(visitor->Bool(false, &bbit));
- bit = bbit;
+ bit = TO_JXL_BOOL(bbit);
}
}
}