public string GetVersionDescription()
        {
            try
            {
                var bytes = Directory.GetByteArray(PhotoshopDirectory.TagVersion);

                if (bytes == null)
                {
                    return(null);
                }

                var reader = new ByteArrayReader(bytes);

                var pos = 0;
                var ver = reader.GetInt32(0);
                pos += 4;
                pos++;
                var readerLength = reader.GetInt32(5);
                pos += 4;
                var readerStr = reader.GetString(9, readerLength * 2, Encoding.BigEndianUnicode);
                pos += readerLength * 2;
                var writerLength = reader.GetInt32(pos);
                pos += 4;
                var writerStr = reader.GetString(pos, writerLength * 2, Encoding.BigEndianUnicode);
                pos += writerLength * 2;
                var fileVersion = reader.GetInt32(pos);

                return($"{ver} ({readerStr}, {writerStr}) {fileVersion}");
            }
            catch
            {
                return(null);
            }
        }
Пример #2
0
 public virtual string GetSlicesDescription()
 {
     try
     {
         sbyte[] bytes = _directory.GetByteArray(PhotoshopDirectory.TagSlices);
         if (bytes == null)
         {
             return(null);
         }
         RandomAccessReader reader = new ByteArrayReader(bytes);
         int    nameLength         = reader.GetInt32(20);
         string name       = reader.GetString(24, nameLength * 2, "UTF-16");
         int    pos        = 24 + nameLength * 2;
         int    sliceCount = reader.GetInt32(pos);
         //pos += 4;
         return(Sharpen.Extensions.StringFormat("%s (%d,%d,%d,%d) %d Slices", name, reader.GetInt32(4), reader.GetInt32(8), reader.GetInt32(12), reader.GetInt32(16), sliceCount));
     }
     catch (IOException)
     {
         /*for (int i=0;i<sliceCount;i++){
          *  pos+=16;
          *  int slNameLen=getInt32(b,pos);
          *  pos+=4;
          *  String slName=new String(b, pos, slNameLen*2,"UTF-16");
          *  res+=slName;
          * }*/
         return(null);
     }
 }
Пример #3
0
 public virtual string GetVersionDescription()
 {
     try
     {
         sbyte[] bytes = _directory.GetByteArray(PhotoshopDirectory.TagVersion);
         if (bytes == null)
         {
             return(null);
         }
         RandomAccessReader reader = new ByteArrayReader(bytes);
         int pos = 0;
         int ver = reader.GetInt32(0);
         pos += 4;
         pos++;
         int readerLength = reader.GetInt32(5);
         pos += 4;
         string readerStr = reader.GetString(9, readerLength * 2, "UTF-16");
         pos += readerLength * 2;
         int writerLength = reader.GetInt32(pos);
         pos += 4;
         string writerStr = reader.GetString(pos, writerLength * 2, "UTF-16");
         pos += writerLength * 2;
         int fileVersion = reader.GetInt32(pos);
         return(Sharpen.Extensions.StringFormat("%d (%s, %s) %d", ver, readerStr, writerStr, fileVersion));
     }
     catch (IOException)
     {
         return(null);
     }
 }
        public string?GetPrintScaleDescription()
        {
            try
            {
                var bytes = Directory.GetByteArray(PhotoshopDirectory.TagPrintScale);

                if (bytes == null)
                {
                    return(null);
                }

                var reader = new ByteArrayReader(bytes);
                var style  = reader.GetInt32(0);
                var locX   = reader.GetFloat32(2);
                var locY   = reader.GetFloat32(6);
                var scale  = reader.GetFloat32(10);

                return(style switch
                {
                    0 => $"Centered, Scale {scale:0.0##}",
                    1 => "Size to fit",
                    2 => $"User defined, X:{locX} Y:{locY}, Scale:{scale:0.0##}",
                    _ => $"Unknown {style:X4}, X:{locX} Y:{locY}, Scale:{scale:0.0##}",
                });
            }
        public string GetThumbnailDescription(int tagType)
        {
            try
            {
                var v = Directory.GetByteArray(tagType);

                if (v == null)
                {
                    return(null);
                }

                var reader = new ByteArrayReader(v);
                var format = reader.GetInt32(0);
                var width  = reader.GetInt32(4);
                var height = reader.GetInt32(8);
                // skip WidthBytes
                var totalSize = reader.GetInt32(16);
                var compSize  = reader.GetInt32(20);
                var bpp       = reader.GetInt32(24);
                // skip Number of planes

                return($"{(format == 1 ? "JpegRGB" : "RawRGB")}, {width}x{height}, Decomp {totalSize} bytes, {bpp} bpp, {compSize} bytes");
            }
            catch
            {
                return(null);
            }
        }
Пример #6
0
 public virtual string GetThumbnailDescription(int tagType)
 {
     try
     {
         sbyte[] v = _directory.GetByteArray(tagType);
         if (v == null)
         {
             return(null);
         }
         RandomAccessReader reader = new ByteArrayReader(v);
         //int pos = 0;
         int format = reader.GetInt32(0);
         //pos += 4;
         int width = reader.GetInt32(4);
         //pos += 4;
         int height = reader.GetInt32(8);
         //pos += 4;
         //pos += 4; //skip WidthBytes
         int totalSize = reader.GetInt32(16);
         //pos += 4;
         int compSize = reader.GetInt32(20);
         //pos += 4;
         int bpp = reader.GetInt32(24);
         //pos+=2;
         //pos+=2; //skip Number of planes
         //int thumbSize=v.length-pos;
         return(Sharpen.Extensions.StringFormat("%s, %dx%d, Decomp %d bytes, %d bpp, %d bytes", format == 1 ? "JpegRGB" : "RawRGB", width, height, totalSize, bpp, compSize));
     }
     catch (IOException)
     {
         return(null);
     }
 }
        public string GetSlicesDescription()
        {
            try
            {
                var bytes = Directory.GetByteArray(PhotoshopDirectory.TagSlices);

                if (bytes == null)
                {
                    return(null);
                }

                var reader = new ByteArrayReader(bytes);

                var nameLength = reader.GetInt32(20);
                var name       = reader.GetString(24, nameLength * 2, Encoding.BigEndianUnicode);
                var pos        = 24 + nameLength * 2;
                var sliceCount = reader.GetInt32(pos);
                return($"{name} ({reader.GetInt32(4)},{reader.GetInt32(8)},{reader.GetInt32(12)},{reader.GetInt32(16)}) {sliceCount} Slices");
            }
            catch
            {
                return(null);
            }
        }
Пример #8
0
        private string Get32BitNumberString(int tag)
        {
            sbyte[] bytes = _directory.GetByteArray(tag);
            if (bytes == null)
            {
                return(null);
            }
            RandomAccessReader reader = new ByteArrayReader(bytes);

            try
            {
                return(Sharpen.Extensions.StringFormat("%d", reader.GetInt32(0)));
            }
            catch (IOException)
            {
                return(null);
            }
        }
Пример #9
0
        public virtual string GetPrintScaleDescription()
        {
            try
            {
                sbyte[] bytes = _directory.GetByteArray(PhotoshopDirectory.TagPrintScale);
                if (bytes == null)
                {
                    return(null);
                }
                RandomAccessReader reader = new ByteArrayReader(bytes);
                int   style = reader.GetInt32(0);
                float locX  = reader.GetFloat32(2);
                float locY  = reader.GetFloat32(6);
                float scale = reader.GetFloat32(10);
                switch (style)
                {
                case 0:
                {
                    return("Centered, Scale " + scale);
                }

                case 1:
                {
                    return("Size to fit");
                }

                case 2:
                {
                    return(Sharpen.Extensions.StringFormat("User defined, X:%s Y:%s, Scale:%s", locX, locY, scale));
                }

                default:
                {
                    return(Sharpen.Extensions.StringFormat("Unknown %04X, X:%s Y:%s, Scale:%s", style, locX, locY, scale));
                }
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
        private string Get32BitNumberString(int tag)
        {
            var bytes = Directory.GetByteArray(tag);

            if (bytes == null)
            {
                return(null);
            }

            var reader = new ByteArrayReader(bytes);

            try
            {
                return($"{reader.GetInt32(0)}");
            }
            catch
            {
                return(null);
            }
        }
        public string GetPrintScaleDescription()
        {
            try
            {
                var bytes = Directory.GetByteArray(PhotoshopDirectory.TagPrintScale);

                if (bytes == null)
                {
                    return(null);
                }

                var reader = new ByteArrayReader(bytes);
                var style  = reader.GetInt32(0);
                var locX   = reader.GetFloat32(2);
                var locY   = reader.GetFloat32(6);
                var scale  = reader.GetFloat32(10);

                switch (style)
                {
                case 0:
                    return($"Centered, Scale {scale:0.0##}");

                case 1:
                    return("Size to fit");

                case 2:
                    return($"User defined, X:{locX} Y:{locY}, Scale:{scale:0.0##}");

                default:
                    return($"Unknown {style:X4}, X:{locX} Y:{locY}, Scale:{scale:0.0##}");
                }
            }
            catch
            {
                return(null);
            }
        }
Пример #12
0
        private string GetTagDataString(int tagType)
        {
            try
            {
                sbyte[] bytes = _directory.GetByteArray(tagType);
                if (bytes == null)
                {
                    return(_directory.GetString(tagType));
                }
                RandomAccessReader reader = new ByteArrayReader(bytes);
                int iccTagType            = reader.GetInt32(0);
                switch (iccTagType)
                {
                case IccTagTypeText:
                {
                    try
                    {
                        return(Sharpen.Runtime.GetStringForBytes(bytes, 8, bytes.Length - 8 - 1, "ASCII"));
                    }
                    catch (UnsupportedEncodingException)
                    {
                        return(Sharpen.Runtime.GetStringForBytes(bytes, 8, bytes.Length - 8 - 1));
                    }
                    goto case IccTagTypeDesc;
                }

                case IccTagTypeDesc:
                {
                    int stringLength = reader.GetInt32(8);
                    return(Sharpen.Runtime.GetStringForBytes(bytes, 12, stringLength - 1));
                }

                case IccTagTypeSig:
                {
                    return(IccReader.GetStringFromInt32(reader.GetInt32(8)));
                }

                case IccTagTypeMeas:
                {
                    int    observerType   = reader.GetInt32(8);
                    float  x              = reader.GetS15Fixed16(12);
                    float  y              = reader.GetS15Fixed16(16);
                    float  z              = reader.GetS15Fixed16(20);
                    int    geometryType   = reader.GetInt32(24);
                    float  flare          = reader.GetS15Fixed16(28);
                    int    illuminantType = reader.GetInt32(32);
                    string observerString;
                    switch (observerType)
                    {
                    case 0:
                    {
                        observerString = "Unknown";
                        break;
                    }

                    case 1:
                    {
                        observerString = "1931 2°";
                        break;
                    }

                    case 2:
                    {
                        observerString = "1964 10°";
                        break;
                    }

                    default:
                    {
                        observerString = Sharpen.Extensions.StringFormat("Unknown %d", observerType);
                        break;
                    }
                    }
                    string geometryString;
                    switch (geometryType)
                    {
                    case 0:
                    {
                        geometryString = "Unknown";
                        break;
                    }

                    case 1:
                    {
                        geometryString = "0/45 or 45/0";
                        break;
                    }

                    case 2:
                    {
                        geometryString = "0/d or d/0";
                        break;
                    }

                    default:
                    {
                        geometryString = Sharpen.Extensions.StringFormat("Unknown %d", observerType);
                        break;
                    }
                    }
                    string illuminantString;
                    switch (illuminantType)
                    {
                    case 0:
                    {
                        illuminantString = "unknown";
                        break;
                    }

                    case 1:
                    {
                        illuminantString = "D50";
                        break;
                    }

                    case 2:
                    {
                        illuminantString = "D65";
                        break;
                    }

                    case 3:
                    {
                        illuminantString = "D93";
                        break;
                    }

                    case 4:
                    {
                        illuminantString = "F2";
                        break;
                    }

                    case 5:
                    {
                        illuminantString = "D55";
                        break;
                    }

                    case 6:
                    {
                        illuminantString = "A";
                        break;
                    }

                    case 7:
                    {
                        illuminantString = "Equi-Power (E)";
                        break;
                    }

                    case 8:
                    {
                        illuminantString = "F8";
                        break;
                    }

                    default:
                    {
                        illuminantString = Sharpen.Extensions.StringFormat("Unknown %d", illuminantType);
                        break;
                    }
                    }
                    return(Sharpen.Extensions.StringFormat("%s Observer, Backing (%s, %s, %s), Geometry %s, Flare %d%%, Illuminant %s", observerString, x, y, z, geometryString, (long)System.Math.Round(flare * 100), illuminantString));
                }

                case IccTagTypeXyzArray:
                {
                    StringBuilder res   = new StringBuilder();
                    int           count = (bytes.Length - 8) / 12;
                    for (int i = 0; i < count; i++)
                    {
                        float x = reader.GetS15Fixed16(8 + i * 12);
                        float y = reader.GetS15Fixed16(8 + i * 12 + 4);
                        float z = reader.GetS15Fixed16(8 + i * 12 + 8);
                        if (i > 0)
                        {
                            res.Append(", ");
                        }
                        res.Append("(").Append(x).Append(", ").Append(y).Append(", ").Append(z).Append(")");
                    }
                    return(Sharpen.Extensions.ConvertToString(res));
                }

                case IccTagTypeMluc:
                {
                    int           int1 = reader.GetInt32(8);
                    StringBuilder res  = new StringBuilder();
                    res.Append(int1);
                    //int int2 = reader.getInt32(12);
                    //System.err.format("int1: %d, int2: %d\n", int1, int2);
                    for (int i = 0; i < int1; i++)
                    {
                        string str = IccReader.GetStringFromInt32(reader.GetInt32(16 + i * 12));
                        int    len = reader.GetInt32(16 + i * 12 + 4);
                        int    ofs = reader.GetInt32(16 + i * 12 + 8);
                        string name;
                        try
                        {
                            name = Sharpen.Runtime.GetStringForBytes(bytes, ofs, len, "UTF-16BE");
                        }
                        catch (UnsupportedEncodingException)
                        {
                            name = Sharpen.Runtime.GetStringForBytes(bytes, ofs, len);
                        }
                        res.Append(" ").Append(str).Append("(").Append(name).Append(")");
                    }
                    //System.err.format("% 3d: %s, len: %d, ofs: %d, \"%s\"\n", i, str, len,ofs,name);
                    return(Sharpen.Extensions.ConvertToString(res));
                }

                case IccTagTypeCurv:
                {
                    int           num = reader.GetInt32(8);
                    StringBuilder res = new StringBuilder();
                    for (int i = 0; i < num; i++)
                    {
                        if (i != 0)
                        {
                            res.Append(", ");
                        }
                        res.Append(FormatDoubleAsString(((float)reader.GetUInt16(12 + i * 2)) / 65535.0, 7, false));
                    }
                    //res+=String.format("%1.7g",Math.round(((float)iccReader.getInt16(b,12+i*2))/0.065535)/1E7);
                    return(Sharpen.Extensions.ConvertToString(res));
                }

                default:
                {
                    return(Sharpen.Extensions.StringFormat("%s(0x%08X): %d bytes", IccReader.GetStringFromInt32(iccTagType), iccTagType, bytes.Length));
                }
                }
            }
            catch (IOException)
            {
                // TODO decode these values during IccReader.extract so we can report any errors at that time
                // It is convention to return null if a description cannot be formulated.
                // If an error is to be reported, it should be done during the extraction process.
                return(null);
            }
        }
        public void ProcessChunk(string fourCc, byte[] payload)
        {
            switch (fourCc)
            {
            case "strh":
            {
                string error      = null;
                var    reader     = new ByteArrayReader(payload, isMotorolaByteOrder: false);
                string fccType    = null;
                string fccHandler = null;
                float  dwScale    = 0;
                float  dwRate     = 0;
                int    dwLength   = 0;
                try
                {
                    fccType    = reader.GetString(0, 4, Encoding.ASCII);
                    fccHandler = reader.GetString(4, 4, Encoding.ASCII);
                    //int dwFlags = reader.GetInt32(8);
                    //int wPriority = reader.GetInt16(12);
                    //int wLanguage = reader.GetInt16(14);
                    //int dwInitialFrames = reader.GetInt32(16);
                    dwScale = reader.GetFloat32(20);
                    dwRate  = reader.GetFloat32(24);
                    //int dwStart = reader.GetInt32(28);
                    dwLength = reader.GetInt32(32);
                    //int dwSuggestedBufferSize = reader.GetInt32(36);
                    //int dwQuality = reader.GetInt32(40);
                    //int dwSampleSize = reader.GetInt32(44);
                    //byte[] rcFrame = reader.GetBytes(48, 2);
                }
                catch (IOException e)
                {
                    error = "Exception reading AviRiff chunk 'strh' : " + e.Message;
                }

                var directory = new AviDirectory();
                if (error == null)
                {
                    if (fccType == "vids")
                    {
                        directory.Set(AviDirectory.TAG_FRAMES_PER_SECOND, (dwRate / dwScale));

                        double duration = dwLength / (dwRate / dwScale);
                        int    hours    = (int)duration / (int)(Math.Pow(60, 2));
                        int    minutes  = ((int)duration / (int)(Math.Pow(60, 1))) - (hours * 60);
                        int    seconds  = (int)Math.Round((duration / (Math.Pow(60, 0))) - (minutes * 60));
                        string time     = new DateTime(2000, 1, 1, hours, minutes, seconds).ToString("hh:mm:ss");

                        directory.Set(AviDirectory.TAG_DURATION, time);
                        directory.Set(AviDirectory.TAG_VIDEO_CODEC, fccHandler);
                    }
                    else
                    if (fccType == "auds")
                    {
                        directory.Set(AviDirectory.TAG_SAMPLES_PER_SECOND, (dwRate / dwScale));
                    }
                }
                else
                {
                    directory.AddError(error);
                }
                _directories.Add(directory);
                break;
            }

            case "avih":
            {
                string error     = null;
                var    reader    = new ByteArrayReader(payload, isMotorolaByteOrder: false);
                int    dwStreams = 0;
                int    dwWidth   = 0;
                int    dwHeight  = 0;
                try
                {
                    //int dwMicroSecPerFrame = reader.GetInt32(0);
                    //int dwMaxBytesPerSec = reader.GetInt32(4);
                    //int dwPaddingGranularity = reader.GetInt32(8);
                    //int dwFlags = reader.GetInt32(12);
                    //int dwTotalFrames = reader.GetInt32(16);
                    //int dwInitialFrames = reader.GetInt32(20);
                    dwStreams = reader.GetInt32(24);
                    //int dwSuggestedBufferSize = reader.GetInt32(28);
                    dwWidth  = reader.GetInt32(32);
                    dwHeight = reader.GetInt32(36);
                    //byte[] dwReserved = reader.GetBytes(40, 4);
                }
                catch (IOException e)
                {
                    error = "Exception reading AviRiff chunk 'avih' : " + e.Message;
                }

                var directory = new AviDirectory();
                if (error == null)
                {
                    directory.Set(AviDirectory.TAG_WIDTH, dwWidth);
                    directory.Set(AviDirectory.TAG_HEIGHT, dwHeight);
                    directory.Set(AviDirectory.TAG_STREAMS, dwStreams);
                }
                else
                {
                    directory.AddError(error);
                }
                _directories.Add(directory);
                break;
            }
            }
        }
        private string GetTagDataString(int tagType)
        {
            try
            {
                var bytes = Directory.GetByteArray(tagType);
                if (bytes == null)
                {
                    return(Directory.GetString(tagType));
                }

                var reader = new ByteArrayReader(bytes);

                var iccTagType = (IccTagType)reader.GetInt32(0);

                switch (iccTagType)
                {
                case IccTagType.Text:
                {
#if !NETSTANDARD1_3
                    try
                    {
                        return(Encoding.ASCII.GetString(bytes, 8, bytes.Length - 8 - 1));
                    }
                    catch
#endif
                    {
                        return(Encoding.UTF8.GetString(bytes, 8, bytes.Length - 8 - 1));
                    }
                }

                case IccTagType.Desc:
                {
                    var stringLength = reader.GetInt32(8);
                    return(Encoding.UTF8.GetString(bytes, 12, stringLength - 1));
                }

                case IccTagType.Sig:
                {
                    return(IccReader.GetStringFromUInt32(reader.GetUInt32(8)));
                }

                case IccTagType.Meas:
                {
                    var observerType   = reader.GetInt32(8);
                    var x              = reader.GetS15Fixed16(12);
                    var y              = reader.GetS15Fixed16(16);
                    var z              = reader.GetS15Fixed16(20);
                    var geometryType   = reader.GetInt32(24);
                    var flare          = reader.GetS15Fixed16(28);
                    var illuminantType = reader.GetInt32(32);

                    string observerString;
                    switch (observerType)
                    {
                    case 0:
                        observerString = "Unknown";
                        break;

                    case 1:
                        observerString = "1931 2\u00b0";
                        break;

                    case 2:
                        observerString = "1964 10\u00b0";
                        break;

                    default:
                        observerString = $"Unknown ({observerType})";
                        break;
                    }

                    string geometryString;
                    switch (geometryType)
                    {
                    case 0:
                        geometryString = "Unknown";
                        break;

                    case 1:
                        geometryString = "0/45 or 45/0";
                        break;

                    case 2:
                        geometryString = "0/d or d/0";
                        break;

                    default:
                        geometryString = $"Unknown ({observerType})";
                        break;
                    }

                    string illuminantString;
                    switch (illuminantType)
                    {
                    case 0:
                        illuminantString = "unknown";
                        break;

                    case 1:
                        illuminantString = "D50";
                        break;

                    case 2:
                        illuminantString = "D65";
                        break;

                    case 3:
                        illuminantString = "D93";
                        break;

                    case 4:
                        illuminantString = "F2";
                        break;

                    case 5:
                        illuminantString = "D55";
                        break;

                    case 6:
                        illuminantString = "A";
                        break;

                    case 7:
                        illuminantString = "Equi-Power (E)";
                        break;

                    case 8:
                        illuminantString = "F8";
                        break;

                    default:
                        illuminantString = $"Unknown ({illuminantType})";
                        break;
                    }

                    return($"{observerString} Observer, Backing ({x:0.###}, {y:0.###}, {z:0.###}), Geometry {geometryString}, Flare {(long)Math.Round(flare*100)}%, Illuminant {illuminantString}");
                }

                case IccTagType.XyzArray:
                {
                    var res   = new StringBuilder();
                    var count = (bytes.Length - 8) / 12;

                    for (var i = 0; i < count; i++)
                    {
                        var x = reader.GetS15Fixed16(8 + i * 12);
                        var y = reader.GetS15Fixed16(8 + i * 12 + 4);
                        var z = reader.GetS15Fixed16(8 + i * 12 + 8);
                        if (i > 0)
                        {
                            res.Append(", ");
                        }
                        res.AppendFormat("({0:0.####}, {1:0.####}, {2:0.####})", x, y, z);
                    }

                    return(res.ToString());
                }

                case IccTagType.Mluc:
                {
                    var int1 = reader.GetInt32(8);
                    var res  = new StringBuilder();
                    res.Append(int1);
                    for (var i = 0; i < int1; i++)
                    {
                        var    str = IccReader.GetStringFromUInt32(reader.GetUInt32(16 + i * 12));
                        var    len = reader.GetInt32(16 + i * 12 + 4);
                        var    ofs = reader.GetInt32(16 + i * 12 + 8);
                        string name;
                        try
                        {
                            name = Encoding.BigEndianUnicode.GetString(bytes, ofs, len);
                        }
                        catch
                        {
                            name = Encoding.UTF8.GetString(bytes, ofs, len);
                        }
                        res.Append(" ").Append(str).Append("(").Append(name).Append(")");
                    }
                    return(res.ToString());
                }

                case IccTagType.Curv:
                {
                    var num = reader.GetInt32(8);
                    var res = new StringBuilder();
                    for (var i = 0; i < num; i++)
                    {
                        if (i != 0)
                        {
                            res.Append(", ");
                        }
                        res.Append(FormatDoubleAsString(reader.GetUInt16(12 + i * 2) / 65535.0, 7, false));
                    }
                    return(res.ToString());
                }

                default:
                {
                    return($"{IccReader.GetStringFromUInt32(unchecked((uint)iccTagType))} (0x{(int)iccTagType:X8}): {bytes.Length} bytes");
                }
                }
            }
            catch (IOException)
            {
                // TODO decode these values during IccReader.extract so we can report any errors at that time
                // It is convention to return null if a description cannot be formulated.
                // If an error is to be reported, it should be done during the extraction process.
                return(null);
            }
        }