void WriteBitArray(BitArray ba) { WriteInt32(ba.bitcount); m_stream.Write(ba.bytes, 0, (ba.bitcount + 7) / 8); }
void CrushSlice(int[] lbm, int resx, int resy) { BitArray ba = new BitArray(); int top = resy; int left = resx; int bottom = 0; int right = 0; int npix = resx * resy; int nWhitePixels = 0; bool curPixelWhite = lbm[0] != 0; int currentPos = 0; int dataLen; ba.PushBits(resx, 16); ba.PushBits(resy, 16); ba.PushBits(curPixelWhite ? 1 : 0, 1); do { dataLen = 0; curPixelWhite = lbm[currentPos] > 0; //returns true if pixel is white // count the pixels until the color changes while ((currentPos < npix) && (curPixelWhite == lbm[currentPos] > 0)) { currentPos++; dataLen++; } if (curPixelWhite) { nWhitePixels += dataLen; // update extents int origpos = currentPos - dataLen; int endpos = currentPos - 1; int xstart = origpos % resx; int xend = endpos % resx; if (xstart + dataLen - 1 >= resx) { xend = resx - 1; if (xstart + dataLen - 1 > resx) xstart = 0; } if (top == resy) top = (origpos / resx); if (bottom < (endpos / resx)) bottom = (endpos / resx); if (left > xstart) left = xstart; if (right < xend) right = xend; } // store the count int keyLen = computeKeySize(dataLen); if (keyLen < 0) { // should not happen! CancelExport = true; return; } ba.PushBits(keyLen, 5); ba.PushBits(dataLen, keyLen + 1); } while (currentPos < npix); // write crushed slice to file WriteInt32(nWhitePixels); WriteInt32(left); WriteInt32(top); WriteInt32(right); WriteInt32(bottom); WriteBitArray(ba); }