BitMiracle.LibTiff.Classic.FieldValue.ToShortArray C# (CSharp) 메소드

ToShortArray() 공개 메소드

Retrieves value converted to array of short values.

If value is array of short values then it retrieved unaltered.

If value is array of bytes then each pair of bytes is converted to short and added to resulting array. If value contains odd amount of bytes, then null is returned.

If value is array of ushort, int or uint values then each element of field value gets converted to short and added to resulting array.

If value is of any other type then null is returned.

public ToShortArray ( ) : short[]
리턴 short[]
        public short[] ToShortArray()
        {
            if (m_value == null)
                return null;

            Type t = m_value.GetType();
            if (t.IsArray)
            {
                if (m_value is short[])
                    return m_value as short[];
                else if (m_value is byte[])
                {
                    byte[] temp = m_value as byte[];
                    if (temp.Length % sizeof(short) != 0)
                        return null;

                    int totalShorts = temp.Length / sizeof(short);
                    short[] result = new short[totalShorts];

                    int byteOffset = 0;
                    for (int i = 0; i < totalShorts; i++)
                    {
                        short s = BitConverter.ToInt16(temp, byteOffset);
                        result[i] = s;
                        byteOffset += sizeof(short);
                    }

                    return result;
                }
                else if (m_value is ushort[])
                {
                    ushort[] temp = m_value as ushort[];
                    short[] result = new short[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                        result[i] = (short)temp[i];

                    return result;
                }
                else if (m_value is int[])
                {
                    int[] temp = m_value as int[];
                    short[] result = new short[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                        result[i] = (short)temp[i];

                    return result;
                }
                else if (m_value is uint[])
                {
                    uint[] temp = m_value as uint[];
                    short[] result = new short[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                        result[i] = (short)temp[i];

                    return result;
                }
            }

            return null;
        }

Usage Example

예제 #1
0
        private bool prettyPrintField(Stream fd, TiffTag tag, int value_count, object raw_data)
        {
            FieldValue value = new FieldValue(raw_data);

            short[]  sdata = value.ToShortArray();
            float[]  fdata = value.ToFloatArray();
            double[] ddata = value.ToDoubleArray();

            switch (tag)
            {
            case TiffTag.INKSET:
                if (sdata != null)
                {
                    fprintf(fd, "  Ink Set: ");
                    switch ((InkSet)sdata[0])
                    {
                    case InkSet.CMYK:
                        fprintf(fd, "CMYK\n");
                        break;

                    default:
                        fprintf(fd, "{0} (0x{1:x})\n", sdata[0], sdata[0]);
                        break;
                    }
                    return(true);
                }
                return(false);

            case TiffTag.DOTRANGE:
                if (sdata != null)
                {
                    fprintf(fd, "  Dot Range: {0}-{1}\n", sdata[0], sdata[1]);
                    return(true);
                }
                return(false);

            case TiffTag.WHITEPOINT:
                if (fdata != null)
                {
                    fprintf(fd, "  White Point: {0:G}-{1:G}\n", fdata[0], fdata[1]);
                    return(true);
                }
                return(false);

            case TiffTag.REFERENCEBLACKWHITE:
                if (fdata != null)
                {
                    fprintf(fd, "  Reference Black/White:\n");
                    for (short i = 0; i < 3; i++)
                    {
                        fprintf(fd, "    {0,2:D}: {1,5:G} {2,5:G}\n", i, fdata[2 * i + 0], fdata[2 * i + 1]);
                    }
                    return(true);
                }
                return(false);

            case TiffTag.XMLPACKET:
                string s = raw_data as string;
                if (s != null)
                {
                    fprintf(fd, "  XMLPacket (XMP Metadata):\n");
                    fprintf(fd, s.Substring(0, value_count));
                    fprintf(fd, "\n");
                    return(true);
                }
                return(false);

            case TiffTag.RICHTIFFIPTC:
                // XXX: for some weird reason RichTIFFIPTC tag defined
                // as array of LONG values.
                fprintf(fd, "  RichTIFFIPTC Data: <present>, {0} bytes\n", value_count * 4);
                return(true);

            case TiffTag.PHOTOSHOP:
                fprintf(fd, "  Photoshop Data: <present>, {0} bytes\n", value_count);
                return(true);

            case TiffTag.ICCPROFILE:
                fprintf(fd, "  ICC Profile: <present>, {0} bytes\n", value_count);
                return(true);

            case TiffTag.STONITS:
                if (ddata != null)
                {
                    fprintf(fd, "  Sample to Nits conversion factor: {0:e4}\n", ddata[0]);
                    return(true);
                }
                return(false);
            }

            return(false);
        }
All Usage Examples Of BitMiracle.LibTiff.Classic.FieldValue::ToShortArray