BitMiracle.LibTiff.Classic.FieldValue.ToFloatArray C# (CSharp) Method

ToFloatArray() public method

Retrieves value converted to array of float values.

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

If value is array of bytes then each 4 bytes are converted to float and added to resulting array. If value contains amount of bytes that can't be divided by 4 without remainder, then null is returned.

If value is array of double values then each element of field value gets converted to float and added to resulting array.

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

public ToFloatArray ( ) : float[]
return float[]
        public float[] ToFloatArray()
        {
            if (m_value == null)
                return null;

            Type t = m_value.GetType();
            if (t.IsArray)
            {
                if (m_value is float[])
                    return m_value as float[];
                else if (m_value is double[])
                {
                    double[] temp = m_value as double[];
                    float[] result = new float[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                        result[i] = (float)temp[i];

                    return result;
                }
                else if (m_value is byte[])
                {
                    byte[] temp = m_value as byte[];
                    if (temp.Length % sizeof(float) != 0)
                        return null;

                    int tempPos = 0; 
                    
                    int floatCount = temp.Length / sizeof(float);
                    float[] result = new float[floatCount];
                    
                    for (int i = 0; i < floatCount; i++)
                    {
                        float f = BitConverter.ToSingle(temp, tempPos);
                        result[i] = f;
                        tempPos += sizeof(float);
                    }

                    return result;
                }
            }

            return null;
        }

Usage Example

Beispiel #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::ToFloatArray