MATLAB: How to read lossy jpeg compressed sequence (seq) files

.seqcompressionjpeglossysequence

EDIT: I solved it, please see file exchange link below
I have have a very big amount of compressed SEQ data (lossy jpg, ~60 TB) that I have to export to AVI with StreamPix (NorPix) prior to analysis in Matlab. I could save a lot of HDD space if I could read the compressed images in the SEQ file directly.
I was able to read a test sequence (see attachment) header using the code from
Output:
Version: 5
HeaderSize: 8192
Description: 'StreamPix 7.4.0.0 (x64)'
ImageWidth: 640
ImageHeight: 480
ImageBitDepth: 8
ImageBitDepthReal: 8
ImageSizeBytes: 307200
ImageFormat: 'Monochrome'
AllocatedFrames: 153
Origin: 0
TrueImageSize: 921608
FrameRate: 20.1316
Compression: 1
but I am not able to recreate an image apart from random pixels.
I have in mind that image size is variable in compressed SEQ. Can someone give me a hint of what could help? Does one generally have to uncompress the image in order to read it (eg. Decoding, Dequatization, Inverse DCT, Upsampling, Color Transform)?
Thanks and regards, Paul
Code:
fid = fopen(fileName,'r','b');
% Use the Little Endian machine format ordering for reading bytes
endianType = 'ieee-le';
% Read header
OFB = {28,1,'long'};
fseek(fid,OFB{1}, 'bof');
headerInfo.Version = fread(fid, OFB{2}, OFB{3}, endianType);
% headerInfo.Version
%







OFB = {32,4/4,'long'};
fseek(fid,OFB{1}, 'bof');
headerInfo.HeaderSize = fread(fid,OFB{2},OFB{3}, endianType);
if headerInfo.Version >=5
display('Version 5+ detected, overriding reported header size')
headerInfo.HeaderSize = 8192
end
% headerInfo.HeaderSize
%
OFB = {592,1,'long'};
fseek(fid,OFB{1}, 'bof');
DescriptionFormat = fread(fid,OFB{2},OFB{3}, endianType)';
OFB = {36,512,'ushort'};
fseek(fid,OFB{1}, 'bof');
headerInfo.Description = fread(fid,OFB{2},OFB{3}, endianType)';
if DescriptionFormat == 0 %#ok Unicode
headerInfo.Description = native2unicode(headerInfo.Description);
elseif DescriptionFormat == 1 %#ok ASCII
headerInfo.Description = char(headerInfo.Description);
end
% headerInfo.Description
%
OFB = {548,24,'uint32'};
fseek(fid,OFB{1}, 'bof');
tmp = fread(fid,OFB{2},OFB{3}, 0, endianType);
headerInfo.ImageWidth = tmp(1);
headerInfo.ImageHeight = tmp(2);
headerInfo.ImageBitDepth = tmp(3);
headerInfo.ImageBitDepthReal = tmp(4);
headerInfo.ImageSizeBytes = tmp(5);
vals = [0,100,101,200:100:600,610,620,700,800,900];
fmts = {'Unknown','Monochrome','Raw Bayer','BGR','Planar','RGB',...
'BGRx', 'YUV422', 'YUV422_20', 'YUV422_PPACKED', 'UVY422', 'UVY411', 'UVY444'};
headerInfo.ImageFormat = fmts{vals == tmp(6)};
%
OFB = {572,1,'ushort'};
fseek(fid,OFB{1}, 'bof');
headerInfo.AllocatedFrames = fread(fid,OFB{2},OFB{3}, endianType);
% headerInfo.AllocatedFrames
%
OFB = {576,1,'ushort'};
fseek(fid,OFB{1}, 'bof');
headerInfo.Origin = fread(fid,OFB{2},OFB{3}, endianType);
% headerInfo.Origin
%
OFB = {580,1,'ulong'};
fseek(fid,OFB{1}, 'bof');
headerInfo.TrueImageSize = fread(fid,OFB{2},OFB{3}, endianType);
% headerInfo.TrueImageSize
%
OFB = {584,1,'double'};
fseek(fid,OFB{1}, 'bof');
headerInfo.FrameRate = fread(fid,OFB{2},OFB{3}, endianType);
% headerInfo.FrameRate
%
OFB = {620,1,'uint8'};
fseek(fid,OFB{1}, 'bof');
headerInfo.Compression = fread(fid,OFB{2},OFB{3}, endianType);
% headerInfo.Compression
imageOffset = 8192;
imgOut = uint8(zeros(headerInfo.ImageWidth,headerInfo.ImageHeight));
switch headerInfo.ImageBitDepthReal
case 8
bitstr = 'uint8';
case {10,12,14,16}
bitstr = 'uint16';
end
nread = 0;
fseek(fid, imageOffset + nread * headerInfo.TrueImageSize, 'bof');
numPixels = headerInfo.ImageWidth * headerInfo.ImageHeight;
Vec = fread(fid, numPixels, bitstr, endianType);
img= reshape(Vec,headerInfo.ImageWidth,headerInfo.ImageHeight)';
I = uint8(img);
imshow(I);