BitMiracle.LibTiff.Classic.TiffTagMethods.setExtraSamples C# (CSharp) Method

setExtraSamples() private static method

Install extra samples information.
private static setExtraSamples ( TiffDirectory td, int &v, FieldValue ap ) : bool
td BitMiracle.LibTiff.Classic.Internal.TiffDirectory
v int
ap FieldValue
return bool
        private static bool setExtraSamples(TiffDirectory td, ref int v, FieldValue[] ap)
        {
            // XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below
            const short EXTRASAMPLE_COREL_UNASSALPHA = 999;

            v = ap[0].ToInt();
            if (v > td.td_samplesperpixel)
                return false;

            byte[] va = ap[1].ToByteArray();
            if (v > 0 && va == null)
            {
                // typically missing param
                return false;
            }

            for (int i = 0; i < v; i++)
            {
                if ((ExtraSample)va[i] > ExtraSample.UNASSALPHA)
                {
                    // XXX: Corel Draw is known to produce incorrect
                    // ExtraSamples tags which must be patched here if we
                    // want to be able to open some of the damaged TIFF files:
                    if (i < v - 1)
                    {
                        short s = BitConverter.ToInt16(va, i);
                        if (s == EXTRASAMPLE_COREL_UNASSALPHA)
                            va[i] = (byte)ExtraSample.UNASSALPHA;
                    }
                    else
                        return false;
                }
            }

            td.td_extrasamples = (short)v;
            td.td_sampleinfo = new ExtraSample[td.td_extrasamples];
            for (int i = 0; i < td.td_extrasamples; i++)
                td.td_sampleinfo[i] = (ExtraSample)va[i];

            return true;
        }