JPEG Encoder Example

Description
FMETP STREAM includes a better solution for encoding jpeg, which is much efficient than Unity default class.
We provide few options of use case, depending on your own needs.
Async multi-threading solution is the best way for encoding or decoding large amount of images, while the single line command can keep your coding simple with small amount of frames.


//Option 1: simple command in one line
Texture2D.FMEncodeToJPG(Quality, ChromaSubsampling);
 
//Option 2: better performance
RawTextureData = Texture2D.GetRawTextureData();
RawTextureData.FMRawTextureDataToJPG(streamWidth, streamHeight, Quality, ChromaSubsampling);
 
//Option 3: Async, best performance when you have many frames
RawTextureData = CapturedTexture.GetRawTextureData();
bool AsyncEncoding = true;
Loom.RunAsync(() =>
{
  try { dataByte = RawTextureData.FMRawTextureDataToJPG(streamWidth, streamHeight, Quality, ChromaSubsampling); } catch { }
  AsyncEncoding = false;
 });
while (AsyncEncoding) yield return null;
//Option 1: simple command in one line
ReceivedTexture2D.FMLoadJPG(ref ReceivedTexture2D, inputByteData);
 
//Option 2: better performance
inputByteData.FMJPGToRawTextureData(ref RawTextureData, ref _width, ref _height, Mono ? TextureFormat.R8 : TextureFormat.RGB24);
ReceivedTexture2D.FMMatchResolution(ref ReceivedTexture2D, _width, _height);
ReceivedTexture2D.LoadRawTextureData(RawTextureData);
ReceivedTexture2D.Apply();
 
//Option 3: Async, best performance when you have many frames
bool AsyncDecoding = true;
Loom.RunAsync(() =>
{
  try { inputByteData.FMJPGToRawTextureData(ref RawTextureData, ref _width, ref _height, Mono ? TextureFormat.R8 : TextureFormat.RGB24); } catch { }
  AsyncDecoding = false;
});
while (AsyncDecoding) yield return null;
ReceivedTexture2D.FMMatchResolution(ref ReceivedTexture2D, _width, _height);
ReceivedTexture2D.LoadRawTextureData(RawTextureData);
ReceivedTexture2D.Apply();