Пример #1
0
        private void initICCNotIndexed(Bitmap bmp, ICCBased icc)
        {
            BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);

            byte[] buf = new byte[bmpData.Stride * bmpData.Height];
            System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, buf, 0, buf.Length);
            _dict.Dictionary.AddItem("BitsPerComponent", new PDFNumber(8));
            _dict.Dictionary.AddItem("ColorSpace", icc);
            MemoryStream stream = _dict.GetStream();

            if (icc.CountComponents == 4)
            {
                stream.Write(buf, 0, buf.Length);
            }
            else if (icc.CountComponents == 3)
            {
                for (int i = 0; i < bmp.Height; ++i)
                {
                    for (int j = 0; j < bmp.Width; ++j)
                    {
                        stream.WriteByte(buf[i * bmpData.Stride + j * 3 + 2]);
                        stream.WriteByte(buf[i * bmpData.Stride + j * 3 + 1]);
                        stream.WriteByte(buf[i * bmpData.Stride + j * 3]);
                    }
                }
            }
            else if (icc.CountComponents == 1)
            {
                for (int i = 0; i < bmp.Height; ++i)
                {
                    for (int j = 0; j < bmp.Width; ++j)
                    {
                        stream.WriteByte(buf[i * bmpData.Stride + j]);
                    }
                }
            }

            bmp.UnlockBits(bmpData);
        }
Пример #2
0
        private void initICC(Bitmap bmp)
        {
            int indexProperty = Array.IndexOf(bmp.PropertyIdList, 34675);

            if (indexProperty == -1)
            {
                throw new PDFInvalidICCException();
            }
            ICCBased icc = new ICCBased(bmp.PropertyItems[indexProperty].Value);

            switch (bmp.PixelFormat)
            {
            case PixelFormat.Format1bppIndexed:
            case PixelFormat.Format4bppIndexed:
            case PixelFormat.Format8bppIndexed:
            case PixelFormat.Indexed:
                initICCIndexed(bmp, icc);
                break;

            default:
                initICCNotIndexed(bmp, icc);
                break;
            }
        }
Пример #3
0
        private void initICCIndexed(Bitmap bmp, ICCBased icc)
        {
            int index = 32;

            switch (bmp.PixelFormat)
            {
            case PixelFormat.Format1bppIndexed:
                index /= 1;
                _dict.Dictionary.AddItem("BitsPerComponent", new PDFNumber(1));
                break;

            case PixelFormat.Format4bppIndexed:
                index /= 4;
                _dict.Dictionary.AddItem("BitsPerComponent", new PDFNumber(4));
                break;

            case PixelFormat.Indexed:
            case PixelFormat.Format8bppIndexed:
                index /= 8;
                _dict.Dictionary.AddItem("BitsPerComponent", new PDFNumber(8));
                break;
            }
            System.Drawing.Color[] colors = bmp.Palette.Entries;
            byte[] buf;
            if (icc.CountComponents == 4)
            {
                buf = new byte[colors.Length * 4];
                for (int i = 0; i < colors.Length; ++i)
                {
                    buf[i * 3]     = colors[i].A;
                    buf[i * 3 + 1] = colors[i].R;
                    buf[i * 3 + 2] = colors[i].G;
                    buf[i * 3 + 3] = colors[i].B;
                }
            }
            else
            {
                buf = new byte[colors.Length * 3];
                for (int i = 0; i < colors.Length; ++i)
                {
                    buf[i * 3]     = colors[i].R;
                    buf[i * 3 + 1] = colors[i].G;
                    buf[i * 3 + 2] = colors[i].B;
                }
            }
            PDFArray array = new PDFArray();

            array.AddItem(new PDFName("Indexed"));
            array.AddItem(icc);
            array.AddItem(new PDFNumber(colors.Length - 1));
            array.AddItem(new PDFString(buf, true));
            _dict.Dictionary.AddItem("ColorSpace", array);
            BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);

            buf = new byte[bmpData.Stride * bmpData.Height];
            System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, buf, 0, buf.Length);

            int countByte = (_width - _width % index) / index + 1;

            if (_width % index == 0)
            {
                --countByte;
            }
            int width      = _width - (countByte - 1) * index;
            int countByteW = (width - width % (index / 4)) / (index / 4) + 1;

            if (width % (index / 4) == 0)
            {
                --countByteW;
            }

            MemoryStream stream = _dict.GetStream();

            for (int i = 0; i < _height; ++i)
            {
                stream.Write(buf, i * bmpData.Stride, (countByte - 1) * 4 + countByteW);
            }

            bmp.UnlockBits(bmpData);
        }