Skip to content

Commit

Permalink
StAVImage::save() - expose Compression level parameter in API and app…
Browse files Browse the repository at this point in the history
…ly to PNG and JPEG
  • Loading branch information
gkv311 committed Jan 17, 2025
1 parent 614c904 commit 3f9c0b2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
16 changes: 14 additions & 2 deletions StShared/StAVImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,13 @@ bool StAVImage::save(const StString& theFilePath,
myCodecCtx->height = (int )anImage.getSizeY();
myCodecCtx->time_base.num = 1;
myCodecCtx->time_base.den = 1;
myCodecCtx->compression_level = 9; // 0..9

// PNG compression level is within [0..9] range
static const int PNG_QLOWER = 0;
static const int PNG_QUPPER = 9;
const float aRatio = stClamp(theParams.Compression >= 0.0f ? theParams.Compression : 1.0f, 0.0f, 1.0f);
int aQLevel = stRound(stLerp(float(PNG_QLOWER), float(PNG_QUPPER), aRatio));
myCodecCtx->compression_level = aQLevel;
break;
}
case ST_TYPE_JPEG:
Expand Down Expand Up @@ -746,7 +752,13 @@ bool StAVImage::save(const StString& theFilePath,
myCodecCtx->height = (int )anImage.getSizeY();
myCodecCtx->time_base.num = 1;
myCodecCtx->time_base.den = 1;
myCodecCtx->qmin = myCodecCtx->qmax = 5; // quality factor - lesser is better

// quantizer factor within 1..31 range, lesser is better
static const int JPEG_QLOWER = 1;
static const int JPEG_QUPPER = 31;
const float aRatio = stClamp(theParams.Compression >= 0.0f ? theParams.Compression : 0.10f, 0.0f, 1.0f);
int aQFactor = stRound(stLerp(float(JPEG_QLOWER), float(JPEG_QUPPER), aRatio));
myCodecCtx->qmin = myCodecCtx->qmax = aQFactor;
break;
}
case ST_TYPE_NONE:
Expand Down
4 changes: 3 additions & 1 deletion include/StImage/StImageFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ class StImageFile : public StImage {
ImageType SaveImageType;
/** Stereo format to be stored as metadata. */
StFormat StereoFormat;
/** Compression level within [0..1] range (0 minimal compression, 1 maximum, -1 default). */
float Compression;

SaveImageParams() : SaveImageType(ST_TYPE_NONE), StereoFormat(StFormat_AUTO) {}
SaveImageParams() : SaveImageType(ST_TYPE_NONE), StereoFormat(StFormat_AUTO), Compression(-1.0) {}
};

public:
Expand Down
10 changes: 10 additions & 0 deletions include/StTemplates/StTemplates.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,14 @@ inline Type stClamp(const Type theValue,
return theValue;
}

/**
* Compute linear interpolation between two values.
* @param theT interpolation coefficient 0..1
* @return interpolation result
*/
template<typename Type, typename ElemType>
inline Type stLerp(const Type& theFrom, const Type& theTo, const ElemType theT) {
return theFrom * (ElemType(1) - theT) + theTo * theT;
}

#endif //__StTemplates_H__
2 changes: 1 addition & 1 deletion include/StTemplates/StVec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ class StVec3 {
}

/**
* Compute linear interpolation between to vectors.
* Compute linear interpolation between two vectors.
* @param theT (const Element_t ) - interpolation coefficient 0..1;
* @return interpolation result.
*/
Expand Down

0 comments on commit 3f9c0b2

Please sign in to comment.