26#ifndef PANGEA_JPEG_STREAM_H
27#define PANGEA_JPEG_STREAM_H
57 void (*
emit)(j_common_ptr, int) =
nullptr;
62 std::vector<unsigned char>
row_;
68 auto*
error =
reinterpret_cast<Error*
>(state->err);
69 state->err->format_message(state,
error->message);
70 std::longjmp(
error->recovery, 1);
75 static void message(j_common_ptr state,
int level) {
77 reinterpret_cast<Error*
>(state->err)->emit(state, level);
121 Image read(
const std::filesystem::path& source,
int width,
int height,
int maxSide) {
124 const auto bytes = std::filesystem::file_size(source);
125 if (!
bytes ||
bytes > 128 * 1024 * 1024 || width <= 0 || height <= 0 ||
126 width > 16384 || height > 16384 || maxSide < 0)
127 throw std::runtime_error(
"Invalid source JPEG size or dimensions");
128 input_ = fopen(source.c_str(),
"rb");
129 if (!
input_)
throw std::runtime_error(
"Cannot open source JPEG");
134 if (setjmp(
error_.recovery))
throw std::runtime_error(std::string(
"JPEG decode: ") +
error_.message);
138 if (
decoder_.image_width !=
static_cast<unsigned>(width) ||
139 decoder_.image_height !=
static_cast<unsigned>(height))
140 throw std::runtime_error(
"Source JPEG dimensions disagree with capture CSV");
142 for (
unsigned numerator = 8; numerator > 0; --numerator) {
144 jpeg_calc_output_dimensions(&
decoder_);
145 if (!maxSide || std::max(
decoder_.output_width,
decoder_.output_height) <=
static_cast<unsigned>(maxSide) || numerator == 1)
154 row_.resize(
static_cast<std::size_t
>(
image_.width) * 3);
157 JSAMPROW pixels =
row_.data();
158 if (jpeg_read_scanlines(&
decoder_, &pixels, 1) != 1)
159 throw std::runtime_error(
"Incomplete JPEG scanline");
160 auto* destination =
image_.pixels.data() +
static_cast<std::size_t
>(
row) *
image_.width * 4;
161 for (
int column = 0; column <
image_.width; ++column) {
162 destination[column * 4] =
row_[column * 3 + 2];
163 destination[column * 4 + 1] =
row_[column * 3 + 1];
164 destination[column * 4 + 2] =
row_[column * 3];
165 destination[column * 4 + 3] = 255;
169 if (
decoder_.err->num_warnings)
throw std::runtime_error(
"Damaged source JPEG");
182 void decode(
const std::filesystem::path& source,
const std::filesystem::path& target,
183 int width,
int height,
const std::function<
bool()>& stopped) {
186 if (width <= 0 || height <= 0 || width > 8192 || height > 8192)
187 throw std::runtime_error(
"Invalid streamed JPEG dimensions");
188 if (std::filesystem::exists(target))
throw std::runtime_error(
"Working raster already exists");
189 input_ = fopen(source.c_str(),
"rb");
190 if (!
input_)
throw std::runtime_error(
"Cannot open JPEG for streamed decoding");
191 const auto temporary = target.string() +
".partial";
192 output_ = std::make_unique<std::ofstream>(temporary, std::ios::binary | std::ios::trunc);
193 if (!*
output_)
throw std::runtime_error(
"Cannot create working raster");
194 row_.resize(
static_cast<std::size_t
>(width) * 4);
199 if (setjmp(
error_.recovery))
throw std::runtime_error(std::string(
"JPEG decode: ") +
error_.message);
203 if (
decoder_.image_width !=
static_cast<unsigned>(width) ||
204 decoder_.image_height !=
static_cast<unsigned>(height) ||
decoder_.progressive_mode)
205 throw std::runtime_error(
"Streamed JPEG requires matching dimensions and baseline encoding");
209 if (stopped && stopped())
throw std::runtime_error(
"Streamed source preparation canceled");
210 JSAMPROW pixels =
row_.data();
211 if (jpeg_read_scanlines(&
decoder_, &pixels, 1) != 1)
212 throw std::runtime_error(
"Incomplete JPEG scanline");
213 for (std::size_t pixel =
static_cast<std::size_t
>(width); pixel > 0; --pixel) {
214 const std::size_t source = (pixel - 1) * 3;
215 const std::size_t destination = (pixel - 1) * 4;
216 const unsigned char red =
row_[source];
217 const unsigned char green =
row_[source + 1];
218 const unsigned char blue =
row_[source + 2];
219 row_[destination] = blue;
220 row_[destination + 1] = green;
221 row_[destination + 2] = red;
222 row_[destination + 3] = 255;
224 output_->write(
reinterpret_cast<const char*
>(
row_.data()),
static_cast<std::streamsize
>(
row_.size()));
225 if (!*
output_)
throw std::runtime_error(
"Cannot write working raster");
228 if (!*
output_ ||
decoder_.err->num_warnings)
throw std::runtime_error(
"Damaged JPEG or working raster write");
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
static void message(j_common_ptr state, int level)
Check markers before forwarding the codec diagnostic.
~JpegStream()
Release any remaining codec and stream resources.
std::vector< unsigned char > row_
struct pangea_survey::JpegStream::Error error_
static void failure(j_common_ptr state)
Preserve the codec diagnostic and jump to the decode recovery point.
Image read(const std::filesystem::path &source, int width, int height, int maxSide)
Read a JPEG preview without retaining the compressed file in memory.
JpegStream(const JpegStream &)=delete
Noncopyable: the decoder and streams have a single owner.
JpegStream()=default
Construct an idle decoder; files are opened per operation.
jpeg_decompress_struct decoder_
void decode(const std::filesystem::path &source, const std::filesystem::path &target, int width, int height, const std::function< bool()> &stopped)
Stream a source JPEG into a new, atomically published raster file.
JpegStream & operator=(const JpegStream &)=delete
Nonassignable: active streams and recovery state cannot be copied.
std::unique_ptr< std::ofstream > output_
void reset() noexcept
Release per-decode state while retaining reusable pixel storage.
Reject invalid Huffman selectors before the bundled codec indexes its tables.
static void usher_jpeg_validate_marker(j_common_ptr state)
Validate marker parameters from an error manager's emit_message callback.
uint8_t row[LWIR_MAX_WIDTH *3U]
struct jpeg_error_mgr error
Integrity checks and bounded disk-backed intermediates for surveys.
void validateJpegScanMessage(j_common_ptr state)
Apply the shared JPEG marker bounds to the current decoder state.
void publishWorkingFile(const std::filesystem::path &temporary, const std::filesystem::path &target, bool replace=true)
Fsync a completed working file, rename it, then sync its directory.
char message[JMSG_LENGTH_MAX]
void(* emit)(j_common_ptr, int)
Decoded preview and its scale relative to the source camera image.
std::vector< unsigned char > pixels
~Operation()
Release the streams and codec on every operation exit.