예제 #1
0
        public override ChunkRaw CreateRawChunk()
        {
            if (Key.Length == 0)
            {
                throw new PngjException("Text chunk key must be non empty");
            }

            using var 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
            var textbytes = ChunkHelper.ToBytesUTF8(Val);

            if (compressed)
            {
                textbytes = ChunkHelper.CompressBytes(textbytes, true);
            }

            ChunkHelper.WriteBytesToStream(ba, textbytes);
            var b     = ba.ToArray();
            var chunk = CreateEmptyChunk(b.Length, false);

            chunk.Data = b;
            return(chunk);
        }
예제 #2
0
        public override void ParseFromRaw(ChunkRaw c)
        {
            if (c is null)
            {
                throw new System.ArgumentNullException(nameof(c));
            }

            var nullsFound = 0;
            var nullsIdx   = new int[3];

            for (var k = 0; k < c.Data.Length; k++)
            {
                if (c.Data[k] != 0)
                {
                    continue;
                }

                nullsIdx[nullsFound] = k;
                nullsFound++;
                if (nullsFound == 1)
                {
                    k += 2;
                }

                if (nullsFound == 3)
                {
                    break;
                }
            }

            if (nullsFound != 3)
            {
                throw new PngjException("Bad formed PngChunkITXT chunk");
            }

            Key = ChunkHelper.ToString(c.Data, 0, nullsIdx[0]);
            var i = nullsIdx[0] + 1;

            compressed = c.Data[i] != 0;
            i++;
            if (compressed && c.Data[i] != 0)
            {
                throw new PngjException("Bad formed PngChunkITXT chunk - bad compression method ");
            }

            langTag       = ChunkHelper.ToString(c.Data, i, nullsIdx[1] - i);
            translatedTag = ChunkHelper.ToStringUTF8(c.Data, nullsIdx[1] + 1, nullsIdx[2] - nullsIdx[1] - 1);
            i             = nullsIdx[2] + 1;
            if (compressed)
            {
                var bytes = ChunkHelper.CompressBytes(c.Data, i, c.Data.Length - i, false);
                Val = ChunkHelper.ToStringUTF8(bytes);
            }
            else
            {
                Val = ChunkHelper.ToStringUTF8(c.Data, i, c.Data.Length - i);
            }
        }
예제 #3
0
        /// <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)
        {
            if (profile is null)
            {
                throw new ArgumentNullException(nameof(profile));
            }

            profileName       = name;
            compressedProfile = ChunkHelper.CompressBytes(profile, true);
        }
예제 #4
0
        public override ChunkRaw CreateRawChunk()
        {
            if (Key.Length == 0)
            {
                throw new PngjException("Text chunk key must be non empty");
            }

            using var ba = new MemoryStream();
            ChunkHelper.WriteBytesToStream(ba, ChunkHelper.ToBytes(Key));
            ba.WriteByte(0); // separator
            ba.WriteByte(0); // compression method: 0
            var textbytes = ChunkHelper.CompressBytes(ChunkHelper.ToBytes(Val), true);

            ChunkHelper.WriteBytesToStream(ba, textbytes);
            var b     = ba.ToArray();
            var chunk = CreateEmptyChunk(b.Length, false);

            chunk.Data = b;
            return(chunk);
        }
예제 #5
0
        public override void ParseFromRaw(ChunkRaw c)
        {
            if (c is null)
            {
                throw new ArgumentNullException(nameof(c));
            }

            var nullsep = -1;

            for (var 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);
            var compmet = (int)c.Data[nullsep + 1];

            if (compmet != 0)
            {
                throw new PngjException("bad zTXt chunk: unknown compression method");
            }

            var uncomp = ChunkHelper.CompressBytes(c.Data, nullsep + 2, c.Data.Length - nullsep - 2, false); // uncompress

            Val = ChunkHelper.ToString(uncomp);
        }
예제 #6
0
 /// <summary>
 /// This uncompresses the string!
 /// </summary>
 /// <returns></returns>
 public byte[] GetProfile()
 {
     return(ChunkHelper.CompressBytes(compressedProfile, false));
 }