Beispiel #1
0
        private void saveRGB(string fileName, ImageFormat imageformat)
        {
            Bitmap     bmp     = new Bitmap(_width, _height, PixelFormat.Format24bppRgb);
            BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

            byte[] buf           = new byte[bmpData.Stride * bmpData.Height];
            int    nBitsPerDepth = (int)(_dict.Dictionary["BitsPerComponent"] as PDFNumber).GetValue();

            _dict.GetStream().Position = 0;
            BitsReader br = new BitsReader(_dict.GetStream());

            for (int i = 0; i < _height; ++i)
            {
                for (int j = 0; j < _width; ++j)
                {
                    buf[i * bmpData.Stride + j * 3 + 2] = (byte)br.ReadBits(nBitsPerDepth);
                    buf[i * bmpData.Stride + j * 3 + 1] = (byte)br.ReadBits(nBitsPerDepth);
                    buf[i * bmpData.Stride + j * 3]     = (byte)br.ReadBits(nBitsPerDepth);
                }
            }
            System.Runtime.InteropServices.Marshal.Copy(buf, 0, bmpData.Scan0, buf.Length);
            bmp.UnlockBits(bmpData);
            bmp.Save(fileName, imageformat);
            bmp.Dispose();
        }
Beispiel #2
0
        public static int Decode(Stream input, int nBitsPerDepth, Stream output)
        {
            input.Position = 0;
            BitsReader reader    = new BitsReader(input);
            int        clearCode = 1 << nBitsPerDepth;
            int        endOfCode = clearCode + 1;
            int        nBits     = nBitsPerDepth + 1;

            initDict(nBitsPerDepth);
            int code = reader.ReadBits(nBits);

            if (code != (1 << nBitsPerDepth))
            {
                return(-1);
            }
            code = reader.ReadBits(nBits);
            for (int i = 0; i < _dictionary[code].Length; ++i)
            {
                output.WriteByte(_dictionary[code][i]);
            }
            int old = code;

            code = reader.ReadBits(nBits);
            while (code != endOfCode)
            {
                if (_dictionaryLen == (1 << nBits) - 1)
                {
                    nBits++;
                }
                if (nBits > StandartNumOfBits)
                {
                    nBits = 12;
                }
                if (code < 0 || code > _dictionaryLen)
                {
                    return(-1);
                }

                if (code == clearCode)
                {
                    reset(nBitsPerDepth);
                    nBits = nBitsPerDepth + 1;
                    code  = reader.ReadBits(nBits);
                    for (int i = 0; i < _dictionary[code].Length; ++i)
                    {
                        output.WriteByte(_dictionary[code][i]);
                    }
                }
                else
                {
                    if (code == _dictionaryLen)
                    {
                        Array <byte> str = _dictionary[old];
                        str += str[0];
                        for (int i = 0; i < str.Length; ++i)
                        {
                            output.WriteByte(str[i]);
                        }
                        addInDict(str);
                    }
                    else
                    {
                        for (int i = 0; i < _dictionary[code].Length; ++i)
                        {
                            output.WriteByte(_dictionary[code][i]);
                        }
                        addInDict(_dictionary[old] + _dictionary[code][0]);
                    }
                }
                old  = code;
                code = reader.ReadBits(nBits);
            }

            return(0);
        }