Exemple #1
0
            public IEnumerable <BigInteger> ReadRow(Stream stream, int width, int componentCount, BigInteger highestComponentValue)
            {
                var bytesPerValue = GetNumberOfBytesPerPixelComponent(highestComponentValue);

                var readCount = width * componentCount;
                var ret       = new List <BigInteger>();
                var buf       = new byte[bytesPerValue];

                for (int i = 0; i < readCount; ++i)
                {
                    if (!NetpbmUtil.ReadToFillBuffer(stream, buf))
                    {
                        throw new EndOfStreamException();
                    }

                    // values are big endian, BigInteger wants little
                    Array.Reverse(buf);

                    // is the potential sign bit set?
                    if ((buf[buf.Length - 1] & 0x80) != 0)
                    {
                        // add a zero byte at the end to make sure the number is positive
                        var newBuf = new byte[buf.Length + 1];
                        Array.Copy(buf, 0, newBuf, 0, buf.Length);
                        newBuf[buf.Length] = 0;
                        ret.Add(new BigInteger(newBuf));
                    }
                    else
                    {
                        ret.Add(new BigInteger(buf));
                    }
                }
                return(ret);
            }
Exemple #2
0
        private IEnumerable <TPixelFormat> ReadBinaryPBMRow <TPixelFormat>(Stream stream, int pixelCount, IImageFactory <TPixelFormat> imageFactory)
        {
            int byteCount = pixelCount / 8;

            if (pixelCount % 8 != 0)
            {
                ++byteCount;
            }

            var rowBytes = new byte[byteCount];

            if (!NetpbmUtil.ReadToFillBuffer(stream, rowBytes))
            {
                throw new EndOfStreamException();
            }

            for (int i = 0; i < pixelCount; ++i)
            {
                var byteOffset = i / 8;
                // leftmost bit is the top bit
                var bitShift = 7 - (i % 8);

                if ((rowBytes[byteOffset] & (1 << bitShift)) == 0)
                {
                    yield return(imageFactory.ZeroPixelComponentValue);
                }
                else
                {
                    yield return(imageFactory.BitmapOnPixelComponentValue);
                }
            }
        }
Exemple #3
0
            public IEnumerable <byte> ReadRow(Stream stream, int width, int componentCount, byte highestComponentValue)
            {
                var readCount = width * componentCount;
                var ret       = new byte[readCount];

                if (!NetpbmUtil.ReadToFillBuffer(stream, ret))
                {
                    throw new EndOfStreamException();
                }
                return(ret);
            }
Exemple #4
0
            public IEnumerable <ushort> ReadRow(Stream stream, int width, int componentCount, ushort highestComponentValue)
            {
                var readCount = width * componentCount;
                var ret       = new List <ushort>();
                var buf       = new byte[2];

                for (int i = 0; i < readCount; ++i)
                {
                    if (!NetpbmUtil.ReadToFillBuffer(stream, buf))
                    {
                        throw new EndOfStreamException();
                    }

                    // big-endian
                    ushort val = (ushort)(
                        ((uint)buf[0] << 8) |
                        ((uint)buf[1] << 0)
                        );
                    ret.Add(val);
                }
                return(ret);
            }