Beispiel #1
0
 /// <summary>
 /// Ad-hoc criteria for 'equivalent' chunks.
 /// </summary>
 ///  <remarks>
 /// Two chunks are equivalent if they have the same Id AND either:
 /// 1. they are Single
 /// 2. both are textual and have the same key
 /// 3. both are SPLT and have the same palette name
 /// Bear in mind that this is an ad-hoc, non-standard, nor required (nor wrong)
 /// criterion. Use it only if you find it useful. Notice that PNG allows to have
 /// repeated textual keys with same keys.
 /// </remarks>
 /// <param name="c1">Chunk1</param>
 /// <param name="c2">Chunk1</param>
 /// <returns>true if equivalent</returns>
 public static bool Equivalent(PngChunk c1, PngChunk c2)
 {
     if (c1 == c2)
     {
         return(true);
     }
     if (c1 == null || c2 == null || !c1.Id.Equals(c2.Id))
     {
         return(false);
     }
     // same id
     if (c1.GetType() != c2.GetType())
     {
         return(false); // should not happen
     }
     if (!c2.AllowsMultiple())
     {
         return(true);
     }
     if (c1 is PngChunkTextVar)
     {
         return(((PngChunkTextVar)c1).GetKey().Equals(((PngChunkTextVar)c2).GetKey()));
     }
     if (c1 is PngChunkSPLT)
     {
         return(((PngChunkSPLT)c1).PalName.Equals(((PngChunkSPLT)c2).PalName));
     }
     // unknown chunks that allow multiple? consider they don't match
     return(false);
 }
Beispiel #2
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);
        }
        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);
        }