| 378 | } |
| 379 | |
| 380 | bool Jpeg::decodeWithTimeout( |
| 381 | const JpegConfig& config, |
| 382 | fl::span<const fl::u8> data, |
| 383 | Frame* frame, |
| 384 | fl::u32 timeout_ms, |
| 385 | float* mProgressout, |
| 386 | fl::string* error_message) { |
| 387 | |
| 388 | if (!frame) { |
| 389 | if (error_message) { |
| 390 | *error_message = "Frame pointer is null"; |
| 391 | } |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | auto decoder = createDecoder(config); |
| 396 | auto stream = fl::make_shared<fl::memorybuf>(data.size()); |
| 397 | stream->write(data); |
| 398 | |
| 399 | if (!decoder->begin(stream)) { |
| 400 | if (error_message) { |
| 401 | decoder->hasError(error_message); |
| 402 | } |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | fl::u32 start_time = fl::millis(); |
| 407 | fl::u32 deadline = start_time + timeout_ms; |
| 408 | |
| 409 | // Use callback-based decode with time budget check |
| 410 | DecodeResult result = decoder->decode(fl::function<bool()>([&]() { |
| 411 | if (mProgressout) { |
| 412 | *mProgressout = decoder->getProgress(); |
| 413 | } |
| 414 | return fl::millis() >= deadline; // Yield if time budget exceeded |
| 415 | })); |
| 416 | |
| 417 | if (result == DecodeResult::Success) { |
| 418 | Frame decoded = decoder->getCurrentFrame(); |
| 419 | |
| 420 | if (frame->getWidth() != decoded.getWidth() || frame->getHeight() != decoded.getHeight()) { |
| 421 | if (error_message) { |
| 422 | *error_message = "Target frame dimensions do not match decoded image dimensions"; |
| 423 | } |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | frame->copy(decoded); |
| 428 | return true; |
| 429 | } else if (result == DecodeResult::Error || decoder->hasError()) { |
| 430 | if (error_message) { |
| 431 | decoder->hasError(error_message); |
| 432 | } |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | // Partial completion due to timeout |
| 437 | if (mProgressout) { |
nothing calls this directly
no test coverage detected