Exemple #1
0
 DictElem GetColorspace()
 {
     if (ICCP != null || (Gamma == 1f && !HasCHRM))
     {
         return((ColorType & 2) == 0 ? DictName.DeviceGray : DictName.DeviceRGB);
     }
     else
     {
         DictArray array = new DictArray();
         Dict      dic   = new Dict();
         if ((ColorType & 2) == 0)
         {
             if (Gamma == 1f)
             {
                 return(DictName.DeviceGray);
             }
             array.Add(DictName.CalGray);
             dic.Put(DictName.Gamma, Gamma);
             dic.Put(DictName.WhitePoint, "[ 1 1 1 ]");
             array.Add(dic);
         }
         else
         {
             DictArray wp = new DictArray(new float[] { 1, 1, 1 });
             array.Add(DictName.CalRGB);
             if (Gamma != 1f)
             {
                 DictArray gm = new DictArray(new float[] { Gamma, Gamma, Gamma });
                 dic.Put(DictName.Gamma, gm);
             }
             if (HasCHRM)
             {
                 float z  = yW * ((xG - xB) * yR - (xR - xB) * yG + (xR - xG) * yB);
                 float YA = yR * ((xG - xB) * yW - (xW - xB) * yG + (xW - xG) * yB) / z;
                 float XA = YA * xR / yR;
                 float ZA = YA * ((1 - xR) / yR - 1);
                 float YB = -yG * ((xR - xB) * yW - (xW - xB) * yR + (xW - xR) * yB) / z;
                 float XB = YB * xG / yG;
                 float ZB = YB * ((1 - xG) / yG - 1);
                 float YC = yB * ((xR - xG) * yW - (xW - xG) * yW + (xW - xR) * yG) / z;
                 float XC = YC * xB / yB;
                 float ZC = YC * ((1 - xB) / yB - 1);
                 float XW = XA + XB + XC;
                 float YW = 1;//YA+YB+YC;
                 float ZW = ZA + ZB + ZC;
                 wp = new DictArray(new float[] { XW, YW, ZW });
                 DictArray matrix = new DictArray(new float[] { XA, YA, ZA, XB, YB, ZB, XC, YC, ZC });
                 dic.Put(DictName.Matrix, matrix);
             }
             dic.Put(DictName.WhitePoint, wp);
             array.Add(dic);
         }
         return(array);
     }
 }
Exemple #2
0
        void ReadPng()
        {
            for (int i = 0; i < PNGID.Length; i++)
            {
                Assert(PNGID[i] == Inp.ReadByte());
            }
            byte[] buffer = new byte[1024];
            while (true)
            {
                int  len       = GetInt(Inp);
                uint chunktype = GetUInt(Inp);
                Assert(len >= 0);
                if (chunktype == IHDR) // header
                {
                    Width  = GetInt(Inp);
                    Height = GetInt(Inp);

                    BitDepth          = Inp.ReadByte();
                    ColorType         = Inp.ReadByte();
                    CompressionMethod = Inp.ReadByte();
                    FilterMethod      = Inp.ReadByte();
                    InterlaceMethod   = Inp.ReadByte();
                }
                else if (chunktype == IDAT) // the image data
                {
                    Util.Copy(Inp, Idat, len, buffer);
                }
                else if (chunktype == tRNS) // transparency information
                {
                    switch (ColorType)
                    {
                    case 0:
                        if (len >= 2)
                        {
                            len -= 2;
                            int gray = GetWord(Inp);
                            if (BitDepth == 16)
                            {
                                TransRedGray = gray;
                            }
                            else
                            {
                                Additional.Put(DictName.Mask, "[ " + gray + " " + gray + " ]");
                            }
                        }
                        break;

                    case 2:
                        if (len >= 6)
                        {
                            len -= 6;
                            int red   = GetWord(Inp);
                            int green = GetWord(Inp);
                            int blue  = GetWord(Inp);
                            if (BitDepth == 16)
                            {
                                TransRedGray = red;
                                TransGreen   = green;
                                TransBlue    = blue;
                            }
                            else
                            {
                                Additional.Put(DictName.Mask, "[ " + red + " " + red + " " + green + " " + green + " " + blue + " " + blue + " ]");
                            }
                        }
                        break;

                    case 3:
                        if (len > 0)
                        {
                            Trans = new byte[len];
                            for (int k = 0; k < len; ++k)
                            {
                                Trans[k] = ( byte )Inp.ReadByte();
                            }
                            len = 0;
                        }
                        break;
                    }
                    Util.Skip(Inp, len);
                }
                else if (chunktype == PLTE) // contains the palette; list of colors.
                {
                    if (ColorType == 3)
                    {
                        DictArray colorspace = new DictArray();
                        colorspace.Add(DictName.Indexed);
                        colorspace.Add(GetColorspace());
                        colorspace.Add(len / 3 - 1);
                        ColorTable = new byte[len];
                        int ix = 0; while ((len--) > 0)
                        {
                            ColorTable[ix++] = ( byte )Inp.ReadByte();
                        }
                        colorspace.Add(new PdfByteStr(ColorTable));
                        Additional.Put(DictName.ColorSpace, colorspace);
                    }
                    else
                    {
                        Util.Skip(Inp, len);
                    }
                }
                else if (chunktype == pHYs) // Currently nothing is done with this info.
                {
                    int dx   = GetInt(Inp);
                    int dy   = GetInt(Inp);
                    int unit = Inp.ReadByte();
                    if (unit == 1)
                    {
                        DpiX = ( int )(( float )dx * 0.0254f + 0.5f);
                        DpiY = ( int )(( float )dy * 0.0254f + 0.5f);
                    }
                    else
                    {
                        if (dy != 0)
                        {
                            XYRatio = ( float )dx / ( float )dy;
                        }
                    }
                }
                else if (chunktype == cHRM) // gives the chromaticity coordinates of the display primaries and white point.
                {
                    xW      = ( float )GetInt(Inp) / 100000f;
                    yW      = ( float )GetInt(Inp) / 100000f;
                    xR      = ( float )GetInt(Inp) / 100000f;
                    yR      = ( float )GetInt(Inp) / 100000f;
                    xG      = ( float )GetInt(Inp) / 100000f;
                    yG      = ( float )GetInt(Inp) / 100000f;
                    xB      = ( float )GetInt(Inp) / 100000f;
                    yB      = ( float )GetInt(Inp) / 100000f;
                    HasCHRM = !(Math.Abs(xW) < 0.0001f || Math.Abs(yW) < 0.0001f || Math.Abs(xR) < 0.0001f || Math.Abs(yR) < 0.0001f ||
                                Math.Abs(xG) < 0.0001f || Math.Abs(yG) < 0.0001f || Math.Abs(xB) < 0.0001f || Math.Abs(yB) < 0.0001f);
                }
                else if (chunktype == sRGB) // indicates that the standard sRGB color space is used.
                {
                    int ri = Inp.ReadByte();
                    Intent  = Intents[ri];
                    Gamma   = 2.2f;
                    xW      = 0.3127f; yW = 0.329f; xR = 0.64f; yR = 0.33f;
                    xG      = 0.3f;    yG = 0.6f;   xB = 0.15f; yB = 0.06f;
                    HasCHRM = true;
                }
                else if (chunktype == gAMA)
                {
                    int gm = GetInt(Inp);
                    if (gm != 0)
                    {
                        Gamma = 100000f / ( float )gm;
                        if (!HasCHRM)
                        {
                            xW      = 0.3127f; yW = 0.329f; xR = 0.64f; yR = 0.33f;
                            xG      = 0.3f;    yG = 0.6f;   xB = 0.15f; yB = 0.06f;
                            HasCHRM = true;
                        }
                    }
                }
                else if (chunktype == iCCP)
                {
                    // Console.WriteLine( "iCCP chunk found" );
                    do
                    {
                        len -= 1;
                    } while (Inp.ReadByte() != 0);
                    Inp.ReadByte(); len -= 1;
                    byte[] icc = new byte[len];
                    Util.ReadN(Inp, icc, 0, len);
                    icc  = Util.Inflate(icc);
                    ICCP = ICC_Profile.GetInstance(icc);
                }
                else if (chunktype == IEND)
                {
                    break;
                }
                else
                {
                    Util.Skip(Inp, len);
                }
                Util.Skip(Inp, 4);
            }
        }