Skip to content

Commit

Permalink
jpeg: add encodingProcess and num_color_components SOF members
Browse files Browse the repository at this point in the history
  • Loading branch information
enen92 committed Dec 29, 2023
1 parent b4f145e commit 7d36cdd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions include/exiv2/jpgimage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class EXIV2API JpegBase : public Image {
virtual int writeHeader(BasicIo& oIo) const = 0;
//@}

int num_color_components_{-1}; //!< image number of color components
std::string sof_encoding_process_; //!< image encoding process

private:
//! @name Manipulators
//@{
Expand Down Expand Up @@ -153,6 +156,18 @@ class EXIV2API JpegImage : public JpegBase {
//! @name Accessors
//@{
[[nodiscard]] std::string mimeType() const override;
/*!
@brief Get the number of color components of the JPEG Image
*/
[[nodiscard]] int numColorComponents() const {
return num_color_components_;
}
/*!
@brief Get the encoding process of the JPEG Image derived from the Start of Frame (SOF) markers
*/
[[nodiscard]] std::string encodingProcess() const {
return sof_encoding_process_;
}
//@}

protected:
Expand Down
37 changes: 37 additions & 0 deletions src/jpgimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
#include "error.hpp"
#include "futils.hpp"
#include "helper_functions.hpp"
#include "i18n.h" // NLS support.
#include "image_int.hpp"
#include "jpgimage.hpp"
#include "photoshop.hpp"
#include "safe_op.hpp"
#include "tags_int.hpp"
#include "utils.hpp"

#ifdef _WIN32
Expand Down Expand Up @@ -49,14 +51,40 @@ constexpr byte rst1_ = 0xd0; //!< JPEG Restart 0 Marker (from 0xD0 to 0xD7 ther

// Start of Frame markers, nondifferential Huffman-coding frames
constexpr byte sof0_ = 0xc0; //!< JPEG Start-Of-Frame marker
constexpr byte sof1_ = 0xc1; //!< JPEG Start-Of-Frame marker
constexpr byte sof2_ = 0xc2; //!< JPEG Start-Of-Frame marker
constexpr byte sof3_ = 0xc3; //!< JPEG Start-Of-Frame marker

// Start of Frame markers, differential Huffman-coding frames
constexpr byte sof5_ = 0xc5; //!< JPEG Start-Of-Frame marker
constexpr byte sof6_ = 0xc6; //!< JPEG Start-Of-Frame marker
constexpr byte sof7_ = 0xc6; //!< JPEG Start-Of-Frame marker

// Start of Frame markers, differential arithmetic-coding frames
constexpr byte sof9_ = 0xc9; //!< JPEG Start-Of-Frame marker
constexpr byte sof10_ = 0xca; //!< JPEG Start-Of-Frame marker
constexpr byte sof11_ = 0xcb; //!< JPEG Start-Of-Frame marker
constexpr byte sof13_ = 0xcd; //!< JPEG Start-Of-Frame marker
constexpr byte sof14_ = 0xce; //!< JPEG Start-Of-Frame marker
constexpr byte sof15_ = 0xcf; //!< JPEG Start-Of-Frame marker

// JPEG process SOF markers
constexpr Internal::TagDetails jpegProcessMarkerTags[] = {
{sof0_, N_("Baseline DCT, Huffman coding")},
{sof1_, N_("Extended sequential DCT, Huffman coding")},
{sof2_, N_("Progressive DCT, Huffman coding")},
{sof3_, N_("Lossless, Huffman coding")},
{sof5_, N_("Sequential DCT, differential Huffman coding")},
{sof6_, N_("Progressive DCT, differential Huffman coding")},
{sof7_, N_("Lossless, Differential Huffman coding")},
{sof9_, N_("Extended sequential DCT, arithmetic coding")},
{sof10_, N_("Progressive DCT, arithmetic coding")},
{sof11_, N_("Lossless, arithmetic coding")},
{sof13_, N_("Sequential DCT, differential arithmetic coding")},
{sof14_, N_("Progressive DCT, differential arithmetic coding")},
{sof15_, N_("Lossless, differential arithmetic coding")},
};

constexpr auto exifId_ = "Exif\0\0"; //!< Exif identifier
// constexpr auto jfifId_ = "JFIF\0"; //!< JFIF identifier
constexpr auto xmpId_ = "http://ns.adobe.com/xap/1.0/\0"; //!< XMP packet identifier
Expand Down Expand Up @@ -159,6 +187,15 @@ void JpegBase::readMetadata() {
std::copy(sizebuf.begin(), sizebuf.end(), buf.begin());
}

auto itSofMarker = std::find_if(std::begin(jpegProcessMarkerTags), std::end(jpegProcessMarkerTags),
[&](Internal::TagDetails tag) { return tag.val_ == marker; });
if (itSofMarker != std::end(jpegProcessMarkerTags)) {
sof_encoding_process_ = itSofMarker->label_;
if (size >= 7 && buf.c_data(7)) {
num_color_components_ = *buf.c_data(7);
}
}

if (!foundExifData && marker == app1_ && size >= 8 // prevent out-of-bounds read in memcmp on next line
&& buf.cmpBytes(2, exifId_, 6) == 0) {
ByteOrder bo = ExifParser::decode(exifData_, buf.c_data(8), size - 8);
Expand Down

0 comments on commit 7d36cdd

Please sign in to comment.