public virtual bool resync_to_restart(jpeg_decompress_struct cinfo, int desired)
{
/* Always put up a warning. */
cinfo.WARNMS(J_MESSAGE_CODE.JWRN_MUST_RESYNC, cinfo.m_unread_marker, desired);
/* Outer loop handles repeated decision after scanning forward. */
int action = 1;
for ( ; ; )
{
if (cinfo.m_unread_marker < (int)JPEG_MARKER.SOF0)
{
/* invalid marker */
action = 2;
}
else if (cinfo.m_unread_marker < (int)JPEG_MARKER.RST0 ||
cinfo.m_unread_marker > (int)JPEG_MARKER.RST7)
{
/* valid non-restart marker */
action = 3;
}
else
{
if (cinfo.m_unread_marker == ((int)JPEG_MARKER.RST0 + ((desired + 1) & 7))
|| cinfo.m_unread_marker == ((int)JPEG_MARKER.RST0 + ((desired + 2) & 7)))
{
/* one of the next two expected restarts */
action = 3;
}
else if (cinfo.m_unread_marker == ((int)JPEG_MARKER.RST0 + ((desired - 1) & 7)) ||
cinfo.m_unread_marker == ((int)JPEG_MARKER.RST0 + ((desired - 2) & 7)))
{
/* a prior restart, so advance */
action = 2;
}
else
{
/* desired restart or too far away */
action = 1;
}
}
cinfo.TRACEMS(4, J_MESSAGE_CODE.JTRC_RECOVERY_ACTION, cinfo.m_unread_marker, action);
switch (action)
{
case 1:
/* Discard marker and let entropy decoder resume processing. */
cinfo.m_unread_marker = 0;
return true;
case 2:
/* Scan to the next marker, and repeat the decision loop. */
if (!cinfo.m_marker.next_marker())
return false;
break;
case 3:
/* Return without advancing past this marker. */
/* Entropy decoder will be forced to process an empty segment. */
return true;
}
}
}