/**
         * 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);
        }