iTextSharp.text.pdf.codec.BmpImage.ReadRLE4 C# (CSharp) Method

ReadRLE4() private method

private ReadRLE4 ( ) : Image
return Image
        private Image ReadRLE4()
        {
            // If imageSize field is not specified, calculate it.
            int imSize = (int)imageSize;
            if (imSize == 0) {
                imSize = (int)(bitmapFileSize - bitmapOffset);
            }

            // Read till we have the whole image
            byte[] values = new byte[imSize];
            int bytesRead = 0;
            while (bytesRead < imSize) {
                bytesRead += inputStream.Read(values, bytesRead,
                imSize - bytesRead);
            }

            // Decompress the RLE4 compressed data.
            byte[] val = DecodeRLE(false, values);

            // Invert it as it is bottom up format.
            if (isBottomUp) {

                byte[] inverted = val;
                val = new byte[width * height];
                int l = 0, index, lineEnd;

                for (int i = height-1; i >= 0; i--) {
                    index = i * width;
                    lineEnd = l + width;
                    while (l != lineEnd) {
                        val[l++] = inverted[index++];
                    }
                }
            }
            int stride = ((width + 1) / 2);
            byte[] bdata = new byte[stride * height];
            int ptr = 0;
            int sh = 0;
            for (int h = 0; h < height; ++h) {
                for (int w = 0; w < width; ++w) {
                    if ((w & 1) == 0)
                        bdata[sh + w / 2] = (byte)(val[ptr++] << 4);
                    else
                        bdata[sh + w / 2] |= (byte)(val[ptr++] & 0x0f);
                }
                sh += stride;
            }
            return IndexedModel(bdata, 4, 4);
        }