void LibJpegDecompressor::ensureHeaderIsRead()

in cpp/spectrum/plugins/jpeg/LibJpegDecompressor.cpp [119:152]


void LibJpegDecompressor::ensureHeaderIsRead() {
  if (libJpegDecompressInfo.global_state < DSTATE_INHEADER) {
    if (_configuration.general.interpretMetadata()) {
      saveMetadataMarkers(libJpegDecompressInfo);
    }

    const auto result = jpeg_read_header(&libJpegDecompressInfo, true);
    SPECTRUM_ERROR_CSTR_IF_NOT(
        JPEG_HEADER_OK == result,
        codecs::error::DecompressorFailure,
        "jpeg_read_header_failed");
  }

  // must be executed between reading the header and starting the
  // decompression
  libJpegDecompressInfo.scale_num = _samplingRatio.numerator;
  libJpegDecompressInfo.scale_denom = _samplingRatio.denominator;

  // if we override the pixel type, then we'll want to set it at this point
  // in the decompress struct to allow libjpeg initialize correctly
  if (_overrideOutputPixelSpecification.hasValue()) {
    const auto colorModel =
        _overrideOutputPixelSpecification.value().colorModel;
    if (colorModel == image::pixel::colormodels::Gray) {
      libJpegDecompressInfo.out_color_space = JCS_GRAYSCALE;
    } else if (colorModel == image::pixel::colormodels::RGB) {
      libJpegDecompressInfo.out_color_space = JCS_RGB;
    } else {
      SPECTRUM_ERROR_STRING(
          codecs::error::DecompressorUnsupportedPixelSpecificationOverride,
          colorModel.identifier.toStdString());
    }
  }
}