Added: better detection of jpeg size

This commit is contained in:
vl 2010-07-08 16:01:01 +02:00
parent b9bd5c8fce
commit 4c06c20712

View file

@ -3052,9 +3052,14 @@ void CBitmap::loadSize(NLMISC::IStream &f, uint32 &retWidth, uint32 &retHeight)
break;
}
// end of file chunk
else if (chunkName == NL_MAKEFOURCC('I', 'E', 'N', 'D'))
{
break;
}
// skip data of this chunk
f.seek(chunkLength, IStream::current);
// skip data of this chunk and CRC32
f.seek(chunkLength+4, IStream::current);
}
catch(...)
{
@ -3065,7 +3070,8 @@ void CBitmap::loadSize(NLMISC::IStream &f, uint32 &retWidth, uint32 &retHeight)
}
else if(fileType == JPG_HEADER)
{
uint16 blockMarker = 0;
uint8 blockMarker1 = 0;
uint8 blockMarker2 = 0;
uint16 blockSize = 0;
bool eof = false;
@ -3073,24 +3079,50 @@ void CBitmap::loadSize(NLMISC::IStream &f, uint32 &retWidth, uint32 &retHeight)
{
try
{
// since we read the first marker
// we should skip this (blockSize should not be equal to zero)
if (blockSize > 1)
// marker of a block
f.serial(blockMarker1);
if (blockMarker1 == 0xff)
{
// marker of a block
f.serial(blockMarker);
NLMISC_BSWAP16(blockMarker);
f.serial(blockMarker2);
// 0xffd9 is the end of an image
if (blockMarker == 0xffd9) break;
// 0xff00 is only found in image data
if (blockMarker2 == 0x00)
{
// image data 0xff
}
// 0xffda is image data
else if (blockMarker2 == 0xda)
{
// next data is image data which must end with 0xffd9
}
// 0xffd9 is the end of an image
else if (blockMarker2 == 0xd9)
{
// real end of file
break;
}
else if (blockMarker2 == 0xdd || blockMarker2 == 0xdc)
{
f.seek(4, IStream::current);
}
else if (blockMarker2 == 0xdf)
{
f.seek(3, IStream::current);
}
else if (blockMarker2 >= 0xd0 && blockMarker2 <= 0xd8)
{
// no content
}
else
{
// size of a block
f.serial(blockSize);
NLMISC_BSWAP16(blockSize);
// frame marker (which contains image width and height)
if (blockMarker >= 0xffc0 && blockMarker <= 0xffc3)
if (blockMarker2 >= 0xc0 && blockMarker2 <= 0xc3)
{
uint8 imagePrecision = 0; // sample precision
uint32 imageSize = 0; // width and height
@ -3107,6 +3139,8 @@ void CBitmap::loadSize(NLMISC::IStream &f, uint32 &retWidth, uint32 &retHeight)
// skip the block
f.seek(blockSize - 2, IStream::current);
}
}
}
catch(...)
{
eof = true;