internal int WriteChunks(IO.Stream os, int currentGroup)
        {
            List <int> written = new List <int>();

            for (int i = 0; i < queuedChunks.Count; i++)
            {
                PngChunk chunk = queuedChunks[i];

                if (!ShouldWrite(chunk, currentGroup))
                {
                    continue;
                }

                if (ChunkHelper.IsCritical(chunk.Id) && !chunk.Id.Equals(ChunkHelper.PLTE))
                {
                    throw new System.IO.IOException($"bad chunk queued: {chunk}");
                }
                if (alreadyWrittenKeys.ContainsKey(chunk.Id) && !chunk.AllowsMultiple())
                {
                    throw new System.IO.IOException($"duplicated chunk does not allow multiple: {chunk}");
                }

                chunk.Write(os);
                chunks.Add(chunk);
                alreadyWrittenKeys[chunk.Id] = alreadyWrittenKeys.ContainsKey(chunk.Id) ? alreadyWrittenKeys[chunk.Id] + 1 : 1;
                written.Add(i);
                chunk.ChunkGroup = currentGroup;
            }
            for (int k = written.Count - 1; k != -1; k--)
            {
                queuedChunks.RemoveAt(written[k]);
            }
            return(written.Count);
        }
コード例 #2
0
ファイル: PngChunkITXT.cs プロジェクト: Computdroid/pngcs
        public override ChunkRaw CreateRawChunk()
        {
            if (key.Length == 0)
            {
                throw new PngjException("Text chunk key must be non empty");
            }
            MemoryStream ba = new MemoryStream();

            ChunkHelper.WriteBytesToStream(ba, ChunkHelper.ToBytes(key));
            ba.WriteByte(0); // separator
            ba.WriteByte(compressed ? (byte)1 : (byte)0);
            ba.WriteByte(0); // compression method (always 0)
            ChunkHelper.WriteBytesToStream(ba, ChunkHelper.ToBytes(langTag));
            ba.WriteByte(0); // separator
            ChunkHelper.WriteBytesToStream(ba, ChunkHelper.ToBytesUTF8(translatedTag));
            ba.WriteByte(0); // separator
            byte[] textbytes = ChunkHelper.ToBytesUTF8(val);
            if (compressed)
            {
                textbytes = ChunkHelper.compressBytes(textbytes, true);
            }
            ChunkHelper.WriteBytesToStream(ba, textbytes);
            byte[]   b     = ba.ToArray();
            ChunkRaw chunk = createEmptyChunk(b.Length, false);

            chunk.Data = b;
            return(chunk);
        }
コード例 #3
0
ファイル: PngChunkZTXT.cs プロジェクト: Computdroid/pngcs
        public override void ParseFromRaw(ChunkRaw c)
        {
            int nullsep = -1;

            for (int i = 0; i < c.Data.Length; i++)   // look for first zero
            {
                if (c.Data[i] != 0)
                {
                    continue;
                }
                nullsep = i;
                break;
            }
            if (nullsep < 0 || nullsep > c.Data.Length - 2)
            {
                throw new PngjException("bad zTXt chunk: no separator found");
            }
            key = ChunkHelper.ToString(c.Data, 0, nullsep);
            int compmet = (int)c.Data[nullsep + 1];

            if (compmet != 0)
            {
                throw new PngjException("bad zTXt chunk: unknown compression method");
            }
            byte[] uncomp = ChunkHelper.compressBytes(c.Data, nullsep + 2, c.Data.Length - nullsep - 2, false); // uncompress
            val = ChunkHelper.ToString(uncomp);
        }
コード例 #4
0
        internal int writeChunks(Stream os, int currentGroup)
        {
            List <int> written = new List <int>();

            for (int i = 0; i < queuedChunks.Count; i++)
            {
                PngChunk c = queuedChunks[i];
                if (!shouldWrite(c, currentGroup))
                {
                    continue;
                }
                if (ChunkHelper.IsCritical(c.Id) && !c.Id.Equals(ChunkHelper.PLTE))
                {
                    throw new PngjOutputException("bad chunk queued: " + c);
                }
                if (alreadyWrittenKeys.ContainsKey(c.Id) && !c.AllowsMultiple())
                {
                    throw new PngjOutputException("duplicated chunk does not allow multiple: " + c);
                }
                c.write(os);
                chunks.Add(c);
                alreadyWrittenKeys[c.Id] = alreadyWrittenKeys.ContainsKey(c.Id) ? alreadyWrittenKeys[c.Id] + 1 : 1;
                written.Add(i);
                c.ChunkGroup = currentGroup;
            }
            for (int k = written.Count - 1; k >= 0; k--)
            {
                queuedChunks.RemoveAt(written[k]);
            }
            return(written.Count);
        }
コード例 #5
0
ファイル: PngChunkSPLT.cs プロジェクト: Computdroid/pngcs
        public override ChunkRaw CreateRawChunk()
        {
            MemoryStream ba = new MemoryStream();

            ChunkHelper.WriteBytesToStream(ba, ChunkHelper.ToBytes(PalName));
            ba.WriteByte(0); // separator
            ba.WriteByte((byte)SampleDepth);
            int nentries = GetNentries();

            for (int n = 0; n < nentries; n++)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (SampleDepth == 8)
                    {
                        PngHelperInternal.WriteByte(ba, (byte)Palette[n * 5 + i]);
                    }
                    else
                    {
                        PngHelperInternal.WriteInt2(ba, Palette[n * 5 + i]);
                    }
                }
                PngHelperInternal.WriteInt2(ba, Palette[n * 5 + 4]);
            }
            byte[]   b     = ba.ToArray();
            ChunkRaw chunk = createEmptyChunk(b.Length, false);

            chunk.Data = b;
            return(chunk);
        }
コード例 #6
0
        internal static PngChunk Factory(ChunkRaw chunk, ImageInfo info)
        {
            PngChunk c = FactoryFromId(ChunkHelper.ToString(chunk.IdBytes), info);

            c.Length = chunk.Len;
            c.ParseFromRaw(chunk);
            return(c);
        }
コード例 #7
0
        public override void ParseFromRaw(ChunkRaw chunk)
        {
            byte[] data   = chunk.Data;
            int    length = data.Length;
            int    t      = -1;

            for (int i = 0; i < length; i++)           // look for first zero
            {
                if (data[i] == 0)
                {
                    t = i;
                    break;
                }
            }
            if (t <= 0 || t > length - 2)
            {
                throw new System.Exception("bad sPLT chunk: no separator found");
            }
            PalName     = ChunkHelper.ToString(data, 0, t);
            SampleDepth = PngHelperInternal.ReadInt1fromByte(data, t + 1);
            t          += 2;
            int nentries = (length - t) / (SampleDepth == 8 ? 6 : 10);

            Palette = new int[nentries * 5];
            int r, g, b, a, f, ne;

            ne = 0;
            for (int i = 0; i < nentries; i++)
            {
                if (SampleDepth == 8)
                {
                    r = PngHelperInternal.ReadInt1fromByte(data, t++);
                    g = PngHelperInternal.ReadInt1fromByte(data, t++);
                    b = PngHelperInternal.ReadInt1fromByte(data, t++);
                    a = PngHelperInternal.ReadInt1fromByte(data, t++);
                }
                else
                {
                    r  = PngHelperInternal.ReadInt2fromBytes(data, t);
                    t += 2;
                    g  = PngHelperInternal.ReadInt2fromBytes(data, t);
                    t += 2;
                    b  = PngHelperInternal.ReadInt2fromBytes(data, t);
                    t += 2;
                    a  = PngHelperInternal.ReadInt2fromBytes(data, t);
                    t += 2;
                }
                f             = PngHelperInternal.ReadInt2fromBytes(data, t);
                t            += 2;
                Palette[ne++] = r;
                Palette[ne++] = g;
                Palette[ne++] = b;
                Palette[ne++] = a;
                Palette[ne++] = f;
            }
        }
コード例 #8
0
ファイル: ChunksList.cs プロジェクト: xphillyx/BepisPlugins
 internal static List <PngChunk> GetXById(List <PngChunk> list, string id, string innerid)
 {
     if (innerid == null)
     {
         return(ChunkHelper.FilterList(list, new ChunkPredicateId(id)));
     }
     else
     {
         return(ChunkHelper.FilterList(list, new ChunkPredicateId2(id, innerid)));
     }
 }
コード例 #9
0
ファイル: ChunkRaw.cs プロジェクト: xphillyx/BepisPlugins
 /// <summary>
 /// Creates an empty raw chunk
 /// </summary>
 internal ChunkRaw(int length, string idb, bool alloc)
 {
     Id      = idb;
     IdBytes = ChunkHelper.ToBytes(Id);
     Data    = null;
     crcval  = 0;
     Len     = length;
     if (alloc)
     {
         AllocData();
     }
 }
コード例 #10
0
 /// <summary>
 /// Creates an empty raw chunk
 /// </summary>
 internal ChunkRaw(int length, string idb, bool alloc)
 {
     this.Id      = idb;
     this.IdBytes = ChunkHelper.ToBytes(Id);
     this.Data    = null;
     this.crcval  = 0;
     this.Len     = length;
     if (alloc)
     {
         AllocData();
     }
 }
コード例 #11
0
 /// <summary>
 /// Constructs an empty chunk
 /// </summary>
 /// <param name="id"></param>
 /// <param name="imgInfo"></param>
 protected PngChunk(string id, ImageInfo imgInfo)
 {
     Id         = id;
     ImgInfo    = imgInfo;
     Crit       = ChunkHelper.IsCritical(id);
     Pub        = ChunkHelper.IsPublic(id);
     Safe       = ChunkHelper.IsSafeToCopy(id);
     Priority   = false;
     ChunkGroup = -1;
     Length     = -1;
     Offset     = 0;
 }
コード例 #12
0
ファイル: PngMetadata.cs プロジェクト: Computdroid/pngcs
        /// <summary>Queues the chunk at the writer</summary>
        /// <param name="chunk">Chunk, ready for write</param>
        /// <param name="lazyOverwrite">Ovewrite lazily equivalent chunks</param>
        /// <remarks>Warning: the overwriting applies to equivalent chunks, see <c>ChunkPredicateEquiv</c>
        /// and will only make sense for queued (not yet writen) chunks
        /// </remarks>
        public void QueueChunk(PngChunk chunk, bool lazyOverwrite)
        {
            ChunksListForWrite cl = getChunkListW();

            if (ReadOnly)
            {
                throw new PngjException("cannot set chunk : readonly metadata");
            }
            if (lazyOverwrite)
            {
                ChunkHelper.TrimList(cl.GetQueuedChunks(), new ChunkPredicateEquiv(chunk));
            }
            cl.Queue(chunk);
        }
コード例 #13
0
        public override void ParseFromRaw(ChunkRaw chunk)
        {
            byte[] data       = chunk.Data;
            int    length     = data.Length;
            int    nullsFound = 0;

            int[] nullsIdx = new int[3];
            for (int k = 0; k < length; k++)
            {
                if (data[k] != 0)
                {
                    continue;
                }
                nullsIdx[nullsFound] = k;
                nullsFound++;
                if (nullsFound == 1)
                {
                    k += 2;
                }
                if (nullsFound == 3)
                {
                    break;
                }
            }
            if (nullsFound != 3)
            {
                throw new System.Exception("Bad formed PngChunkITXT chunk");
            }
            key = ChunkHelper.ToString(data, 0, nullsIdx[0]);
            int i = nullsIdx[0] + 1;

            compressed = data[i] == 0 ? false : true;
            i++;
            if (compressed && data[i] != 0)
            {
                throw new System.Exception("Bad formed PngChunkITXT chunk - bad compression method ");
            }
            langTag       = ChunkHelper.ToString(data, i, nullsIdx[1] - i);
            translatedTag = ChunkHelper.ToStringUTF8(data, nullsIdx[1] + 1, nullsIdx[2] - nullsIdx[1] - 1);
            i             = nullsIdx[2] + 1;
            if (compressed)
            {
                byte[] bytes = ChunkHelper.CompressBytes(data, i, length - i, false);
                val = ChunkHelper.ToStringUTF8(bytes);
            }
            else
            {
                val = ChunkHelper.ToStringUTF8(data, i, length - i);
            }
        }
        /**
         * this should be called only for ancillary chunks and PLTE (groups 1 - 3 - 5)
         **/
        static bool ShouldWrite(PngChunk chunk, int currentGroup)
        {
            if (currentGroup == CHUNK_GROUP_2_PLTE)
            {
                return(chunk.Id.Equals(ChunkHelper.PLTE));
            }
            if (currentGroup % 2 == 0)
            {
                throw new System.IO.IOException("bad chunk group?");
            }
            int minChunkGroup, maxChunkGroup;

            if (chunk.MustGoBeforePLTE())
            {
                minChunkGroup = maxChunkGroup = ChunksList.CHUNK_GROUP_1_AFTERIDHR;
            }
            else if (chunk.MustGoBeforeIDAT())
            {
                maxChunkGroup = ChunksList.CHUNK_GROUP_3_AFTERPLTE;
                minChunkGroup = chunk.MustGoAfterPLTE()
                                        ? ChunksList.CHUNK_GROUP_3_AFTERPLTE
                                        : ChunksList.CHUNK_GROUP_1_AFTERIDHR;
            }
            else
            {
                maxChunkGroup = ChunksList.CHUNK_GROUP_5_AFTERIDAT;
                minChunkGroup = ChunksList.CHUNK_GROUP_1_AFTERIDHR;
            }

            int preferred = maxChunkGroup;

            if (chunk.Priority)
            {
                preferred = minChunkGroup;
            }
            if (ChunkHelper.IsUnknown(chunk) && chunk.ChunkGroup > 0)
            {
                preferred = chunk.ChunkGroup;
            }

            if (currentGroup == preferred)
            {
                return(true);
            }
            if (currentGroup > preferred && currentGroup <= maxChunkGroup)
            {
                return(true);
            }
            return(false);
        }
コード例 #15
0
ファイル: ChunkRaw.cs プロジェクト: xphillyx/BepisPlugins
 internal void WriteChunk(Stream os)
 {
     if (IdBytes.Length != 4)
     {
         throw new PngjOutputException("bad chunkid [" + ChunkHelper.ToString(IdBytes) + "]");
     }
     crcval = ComputeCrc();
     PngHelperInternal.WriteInt4(os, Len);
     PngHelperInternal.WriteBytes(os, IdBytes);
     if (Len > 0)
     {
         PngHelperInternal.WriteBytes(os, Data, 0, Len);
     }
     //Console.WriteLine("writing chunk " + this.ToString() + "crc=" + crcval);
     PngHelperInternal.WriteInt4(os, crcval);
 }
コード例 #16
0
ファイル: PngChunkZTXT.cs プロジェクト: Computdroid/pngcs
        public override ChunkRaw CreateRawChunk()
        {
            if (key.Length == 0)
            {
                throw new PngjException("Text chunk key must be non empty");
            }
            MemoryStream ba = new MemoryStream();

            ChunkHelper.WriteBytesToStream(ba, ChunkHelper.ToBytes(key));
            ba.WriteByte(0); // separator
            ba.WriteByte(0); // compression method: 0
            byte[] textbytes = ChunkHelper.compressBytes(ChunkHelper.ToBytes(val), true);
            ChunkHelper.WriteBytesToStream(ba, textbytes);
            byte[]   b     = ba.ToArray();
            ChunkRaw chunk = createEmptyChunk(b.Length, false);

            chunk.Data = b;
            return(chunk);
        }
コード例 #17
0
ファイル: PngChunkICCP.cs プロジェクト: Computdroid/pngcs
 /// <summary>
 /// Sets profile name and profile
 /// </summary>
 /// <param name="name">profile name </param>
 /// <param name="profile">profile (latin1 string)</param>
 public void SetProfileNameAndContent(String name, String profile)
 {
     SetProfileNameAndContent(name, ChunkHelper.ToBytes(profileName));
 }
コード例 #18
0
ファイル: PngChunkICCP.cs プロジェクト: Computdroid/pngcs
 public String GetProfileAsString()
 {
     return(ChunkHelper.ToString(GetProfile()));
 }
コード例 #19
0
ファイル: PngChunkICCP.cs プロジェクト: Computdroid/pngcs
 /// <summary>
 /// This uncompresses the string!
 /// </summary>
 /// <returns></returns>
 public byte[] GetProfile()
 {
     return(ChunkHelper.compressBytes(compressedProfile, false));
 }
コード例 #20
0
ファイル: PngChunkICCP.cs プロジェクト: Computdroid/pngcs
 /// <summary>
 /// Sets profile name and profile
 /// </summary>
 /// <param name="name">profile name </param>
 /// <param name="profile">profile (uncompressed)</param>
 public void SetProfileNameAndContent(String name, byte[] profile)
 {
     profileName       = name;
     compressedProfile = ChunkHelper.compressBytes(profile, true);
 }
コード例 #21
0
ファイル: ChunksList.cs プロジェクト: xphillyx/BepisPlugins
 /// <summary>
 /// Finds all chunks "equivalent" to this one
 /// </summary>
 /// <param name="chunk"></param>
 /// <returns>Empty if nothing found</returns>
 public List <PngChunk> GetEquivalent(PngChunk chunk)
 {
     return(ChunkHelper.FilterList(chunks, new ChunkPredicateEquiv(chunk)));
 }
コード例 #22
0
 internal ChunkRaw(int length, byte[] idbytes, bool alloc) : this(length, ChunkHelper.ToString(idbytes), alloc)
 {
 }
コード例 #23
0
ファイル: ChunkRaw.cs プロジェクト: xphillyx/BepisPlugins
 /// <summary>
 /// Just id and length
 /// </summary>
 /// <returns></returns>
 public override string ToString()
 {
     return("chunkid=" + ChunkHelper.ToString(IdBytes) + " len=" + Len);
 }
コード例 #24
0
 /// <summary>
 /// Check for match
 /// </summary>
 /// <param name="c"></param>
 /// <returns></returns>
 public bool Matches(PngChunk c)
 {
     return(ChunkHelper.Equivalent(c, chunk));
 }
コード例 #25
0
ファイル: PngChunk.cs プロジェクト: lukebmx81/BepisPlugins
        public ChunkRaw createEmptyChunk(int len, bool alloc)
        {
            ChunkRaw c = new ChunkRaw(len, ChunkHelper.ToBytes(Id), alloc);

            return(c);
        }