protected bool DecodeImageData()
{
int NullCode = -1;
int npix = iw * ih;
int available, clear, code_mask, code_size, end_of_information, in_code, old_code,
bits, code, count, i, datum, data_size, first, top, bi;
bool skipZero = false;
if (prefix == null)
prefix = new short[MaxStackSize];
if (suffix == null)
suffix = new byte[MaxStackSize];
if (pixelStack == null)
pixelStack = new byte[MaxStackSize+1];
m_line_stride = (iw * m_bpc + 7) / 8;
m_out = new byte[m_line_stride * ih];
int pass = 1;
int inc = interlace ? 8 : 1;
int line = 0;
int xpos = 0;
// Initialize GIF data stream decoder.
data_size = inp.ReadByte();
clear = 1 << data_size;
end_of_information = clear + 1;
available = clear + 2;
old_code = NullCode;
code_size = data_size + 1;
code_mask = (1 << code_size) - 1;
for (code = 0; code < clear; code++) {
prefix[code] = 0;
suffix[code] = (byte) code;
}
// Decode GIF pixel stream.
datum = bits = count = first = top = bi = 0;
for (i = 0; i < npix; ) {
if (top == 0) {
if (bits < code_size) {
// Load bytes until there are enough bits for a code.
if (count == 0) {
// Read a new data block.
count = ReadBlock();
if (count <= 0) {
skipZero = true;
break;
}
bi = 0;
}
datum += (((int) block[bi]) & 0xff) << bits;
bits += 8;
bi++;
count--;
continue;
}
// Get the next code.
code = datum & code_mask;
datum >>= code_size;
bits -= code_size;
// Interpret the code
if ((code > available) || (code == end_of_information))
break;
if (code == clear) {
// Reset decoder.
code_size = data_size + 1;
code_mask = (1 << code_size) - 1;
available = clear + 2;
old_code = NullCode;
continue;
}
if (old_code == NullCode) {
pixelStack[top++] = suffix[code];
old_code = code;
first = code;
continue;
}
in_code = code;
if (code == available) {
pixelStack[top++] = (byte) first;
code = old_code;
}
while (code > clear) {
pixelStack[top++] = suffix[code];
code = prefix[code];
}
first = ((int) suffix[code]) & 0xff;
// Add a new string to the string table,
if (available >= MaxStackSize)
break;
pixelStack[top++] = (byte) first;
prefix[available] = (short) old_code;
suffix[available] = (byte) first;
available++;
if (((available & code_mask) == 0) && (available < MaxStackSize)) {
code_size++;
code_mask += available;
}
old_code = in_code;
}
// Pop a pixel off the pixel stack.
top--;
i++;
SetPixel(xpos, line, pixelStack[top]);
++xpos;
if (xpos >= iw) {
xpos = 0;
line += inc;
if (line >= ih) {
if (interlace) {
do {
pass++;
switch (pass) {
case 2:
line = 4;
break;
case 3:
line = 2;
inc = 4;
break;
case 4:
line = 1;
inc = 2;
break;
default: // this shouldn't happen
line = ih - 1;
inc = 0;
break;
}
} while (line >= ih);
}
else {
line = ih - 1; // this shouldn't happen
inc = 0;
}
}
}
}
return skipZero;
}