internal int writeChunks(Stream os, int currentGroup) { List <int> list = new List <int>(); for (int i = 0; i < queuedChunks.Count; i++) { PngChunk pngChunk = queuedChunks[i]; if (shouldWrite(pngChunk, currentGroup)) { if (ChunkHelper.IsCritical(pngChunk.Id) && !pngChunk.Id.Equals("PLTE")) { throw new PngjOutputException("bad chunk queued: " + pngChunk?.ToString()); } if (alreadyWrittenKeys.ContainsKey(pngChunk.Id) && !pngChunk.AllowsMultiple()) { throw new PngjOutputException("duplicated chunk does not allow multiple: " + pngChunk?.ToString()); } pngChunk.write(os); chunks.Add(pngChunk); alreadyWrittenKeys[pngChunk.Id] = ((!alreadyWrittenKeys.ContainsKey(pngChunk.Id)) ? 1 : (alreadyWrittenKeys[pngChunk.Id] + 1)); list.Add(i); pngChunk.ChunkGroup = currentGroup; } } for (int num = list.Count - 1; num >= 0; num--) { queuedChunks.RemoveAt(list[num]); } return(list.Count); }
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 = 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); }
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); }
public override ChunkRaw CreateRawChunk() { if (key.Length == 0) { throw new PngjException("Text chunk key must be non empty"); } MemoryStream memoryStream = new MemoryStream(); ChunkHelper.WriteBytesToStream(memoryStream, ChunkHelper.ToBytes(key)); memoryStream.WriteByte(0); memoryStream.WriteByte((byte)(compressed ? 1 : 0)); memoryStream.WriteByte(0); ChunkHelper.WriteBytesToStream(memoryStream, ChunkHelper.ToBytes(langTag)); memoryStream.WriteByte(0); ChunkHelper.WriteBytesToStream(memoryStream, ChunkHelper.ToBytesUTF8(translatedTag)); memoryStream.WriteByte(0); byte[] array = ChunkHelper.ToBytesUTF8(val); if (compressed) { array = ChunkHelper.compressBytes(array, compress: true); } ChunkHelper.WriteBytesToStream(memoryStream, array); byte[] array2 = memoryStream.ToArray(); ChunkRaw chunkRaw = createEmptyChunk(array2.Length, alloc: false); chunkRaw.Data = array2; return(chunkRaw); }
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); }
public override ChunkRaw CreateRawChunk() { MemoryStream memoryStream = new MemoryStream(); ChunkHelper.WriteBytesToStream(memoryStream, ChunkHelper.ToBytes(PalName)); memoryStream.WriteByte(0); memoryStream.WriteByte((byte)SampleDepth); int nentries = GetNentries(); for (int i = 0; i < nentries; i++) { for (int j = 0; j < 4; j++) { if (SampleDepth == 8) { PngHelperInternal.WriteByte(memoryStream, (byte)Palette[i * 5 + j]); } else { PngHelperInternal.WriteInt2(memoryStream, Palette[i * 5 + j]); } } PngHelperInternal.WriteInt2(memoryStream, Palette[i * 5 + 4]); } byte[] array = memoryStream.ToArray(); ChunkRaw chunkRaw = createEmptyChunk(array.Length, alloc: false); chunkRaw.Data = array; return(chunkRaw); }
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 static PngChunk Factory(ChunkRaw chunk, ImageInfo info) { PngChunk pngChunk = FactoryFromId(ChunkHelper.ToString(chunk.IdBytes), info); pngChunk.Length = chunk.Length; pngChunk.ParseFromRaw(chunk); return(pngChunk); }
internal static List <PngChunk> GetXById(List <PngChunk> list, string id, string innerid) { if (innerid == null) { return(ChunkHelper.FilterList(list, new ChunkPredicateId(id))); } return(ChunkHelper.FilterList(list, new ChunkPredicateId2(id, innerid))); }
internal static AbstractPngChunk Factory(ChunkRaw chunk, ImageInfo info) { var c = FactoryFromId(ChunkHelper.ToString(chunk.IdBytes), info); c.Length = chunk.Len; c.ParseFromRaw(chunk); return(c); }
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); } }
public override void ParseFromRaw(ChunkRaw c) { int num = -1; for (int i = 0; i < c.Data.Length; i++) { if (c.Data[i] == 0) { num = i; break; } } if (num <= 0 || num > c.Data.Length - 2) { throw new PngjException("bad sPLT chunk: no separator found"); } PalName = ChunkHelper.ToString(c.Data, 0, num); SampleDepth = PngHelperInternal.ReadInt1fromByte(c.Data, num + 1); num += 2; int num2 = (c.Data.Length - num) / ((SampleDepth == 8) ? 6 : 10); Palette = new int[num2 * 5]; int num3 = 0; for (int j = 0; j < num2; j++) { int num4; int num5; int num6; int num7; if (SampleDepth == 8) { num4 = PngHelperInternal.ReadInt1fromByte(c.Data, num++); num5 = PngHelperInternal.ReadInt1fromByte(c.Data, num++); num6 = PngHelperInternal.ReadInt1fromByte(c.Data, num++); num7 = PngHelperInternal.ReadInt1fromByte(c.Data, num++); } else { num4 = PngHelperInternal.ReadInt2fromBytes(c.Data, num); num += 2; num5 = PngHelperInternal.ReadInt2fromBytes(c.Data, num); num += 2; num6 = PngHelperInternal.ReadInt2fromBytes(c.Data, num); num += 2; num7 = PngHelperInternal.ReadInt2fromBytes(c.Data, num); num += 2; } int num8 = PngHelperInternal.ReadInt2fromBytes(c.Data, num); num += 2; Palette[num3++] = num4; Palette[num3++] = num5; Palette[num3++] = num6; Palette[num3++] = num7; Palette[num3++] = num8; } }
/// <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); }
public override ChunkRaw CreateRawChunk() { ChunkRaw chunkRaw = createEmptyChunk(profileName.Length + compressedProfile.Length + 2, alloc: true); Array.Copy(ChunkHelper.ToBytes(profileName), 0, chunkRaw.Data, 0, profileName.Length); chunkRaw.Data[profileName.Length] = 0; chunkRaw.Data[profileName.Length + 1] = 0; Array.Copy(compressedProfile, 0, chunkRaw.Data, profileName.Length + 2, compressedProfile.Length); return(chunkRaw); }
/** * this should be called only for ancillary chunks and PLTE (groups 1 - 3 - 5) **/ private static bool ShouldWrite(AbstractPngChunk c, int currentGroup) { if (currentGroup == CHUNK_GROUP_2_PLTE) { return(c.Id.Equals(ChunkHelper.PLTE, System.StringComparison.Ordinal)); } if (currentGroup % 2 == 0) { throw new PngjOutputException("bad chunk group?"); } int minChunkGroup; int maxChunkGroup; if (c.MustGoBeforePLTE()) { minChunkGroup = maxChunkGroup = ChunksList.CHUNK_GROUP_1_AFTERIDHR; } else if (c.MustGoBeforeIDAT()) { maxChunkGroup = ChunksList.CHUNK_GROUP_3_AFTERPLTE; minChunkGroup = c.MustGoAfterPLTE() ? ChunksList.CHUNK_GROUP_3_AFTERPLTE : ChunksList.CHUNK_GROUP_1_AFTERIDHR; } else { maxChunkGroup = ChunksList.CHUNK_GROUP_5_AFTERIDAT; minChunkGroup = ChunksList.CHUNK_GROUP_1_AFTERIDHR; } var preferred = maxChunkGroup; if (c.Priority) { preferred = minChunkGroup; } if (ChunkHelper.IsUnknown(c) && c.ChunkGroup > 0) { preferred = c.ChunkGroup; } if (currentGroup == preferred) { return(true); } if (currentGroup > preferred && currentGroup <= maxChunkGroup) { return(true); } return(false); }
public override void ParseFromRaw(ChunkRaw c) { int t = -1; for (int i = 0; i < c.Data.Length; i++) { // look for first zero if (c.Data[i] == 0) { t = i; break; } } if (t <= 0 || t > c.Data.Length - 2) { throw new PngjException("bad sPLT chunk: no separator found"); } PalName = ChunkHelper.ToString(c.Data, 0, t); SampleDepth = PngHelperInternal.ReadInt1fromByte(c.Data, t + 1); t += 2; int nentries = (c.Data.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(c.Data, t++); g = PngHelperInternal.ReadInt1fromByte(c.Data, t++); b = PngHelperInternal.ReadInt1fromByte(c.Data, t++); a = PngHelperInternal.ReadInt1fromByte(c.Data, t++); } else { r = PngHelperInternal.ReadInt2fromBytes(c.Data, t); t += 2; g = PngHelperInternal.ReadInt2fromBytes(c.Data, t); t += 2; b = PngHelperInternal.ReadInt2fromBytes(c.Data, t); t += 2; a = PngHelperInternal.ReadInt2fromBytes(c.Data, t); t += 2; } f = PngHelperInternal.ReadInt2fromBytes(c.Data, t); t += 2; Palette[ne++] = r; Palette[ne++] = g; Palette[ne++] = b; Palette[ne++] = a; Palette[ne++] = f; } }
/// <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(); } }
public 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 = 0L; }
/// <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(); } }
public override void ParseFromRaw(ChunkRaw chunk) { int num = ChunkHelper.PosNullByte(chunk.Data); profileName = PngHelperInternal.charsetLatin1.GetString(chunk.Data, 0, num); if ((chunk.Data[num + 1] & 0xFF) != 0) { throw new Exception("bad compression for ChunkTypeICCP"); } int num2 = chunk.Data.Length - (num + 2); compressedProfile = new byte[num2]; Array.Copy(chunk.Data, num + 2, compressedProfile, 0, num2); }
/// <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); }
internal void WriteChunk(Stream os) { if (IdBytes.Length != 4) { throw new PngjOutputException("bad chunkid [" + ChunkHelper.ToString(IdBytes) + "]"); } crcval = ComputeCrc(); PngHelperInternal.WriteInt4(os, Length); PngHelperInternal.WriteBytes(os, IdBytes); if (Length > 0) { PngHelperInternal.WriteBytes(os, Data, 0, Length); } PngHelperInternal.WriteInt4(os, crcval); }
public override void ParseFromRaw(ChunkRaw c) { int nullsFound = 0; int[] nullsIdx = new int[3]; for (int 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]); int i = nullsIdx[0] + 1; compressed = c.Data[i] == 0 ? false : true; 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) { byte[] 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); } }
public override void ParseFromRaw(ChunkRaw c) { int num = 0; int[] array = new int[3]; for (int i = 0; i < c.Data.Length; i++) { if (c.Data[i] == 0) { array[num] = i; num++; if (num == 1) { i += 2; } if (num == 3) { break; } } } if (num != 3) { throw new PngjException("Bad formed PngChunkITXT chunk"); } key = ChunkHelper.ToString(c.Data, 0, array[0]); int num2 = array[0] + 1; compressed = ((c.Data[num2] != 0) ? true : false); num2++; if (compressed && c.Data[num2] != 0) { throw new PngjException("Bad formed PngChunkITXT chunk - bad compression method "); } langTag = ChunkHelper.ToString(c.Data, num2, array[1] - num2); translatedTag = ChunkHelper.ToStringUTF8(c.Data, array[1] + 1, array[2] - array[1] - 1); num2 = array[2] + 1; if (compressed) { byte[] x = ChunkHelper.compressBytes(c.Data, num2, c.Data.Length - num2, compress: false); val = ChunkHelper.ToStringUTF8(x); } else { val = ChunkHelper.ToStringUTF8(c.Data, num2, c.Data.Length - num2); } }
public static bool shouldWrite(PngChunk c, int currentGroup) { if (currentGroup == 2) { return(c.Id.Equals("PLTE")); } if (currentGroup % 2 == 0) { throw new PngjOutputException("bad chunk group?"); } int num2; int num; if (c.mustGoBeforePLTE()) { num2 = (num = 1); } else if (c.mustGoBeforeIDAT()) { num = 3; num2 = ((!c.mustGoAfterPLTE()) ? 1 : 3); } else { num = 5; num2 = 1; } int num3 = num; if (c.Priority) { num3 = num2; } if (ChunkHelper.IsUnknown(c) && c.ChunkGroup > 0) { num3 = c.ChunkGroup; } if (currentGroup == num3) { return(true); } if (currentGroup > num3 && currentGroup <= num) { return(true); } return(false); }
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); }
public override ChunkRaw CreateRawChunk() { if (key.Length == 0) { throw new PngjException("Text chunk key must be non empty"); } MemoryStream memoryStream = new MemoryStream(); ChunkHelper.WriteBytesToStream(memoryStream, ChunkHelper.ToBytes(key)); memoryStream.WriteByte(0); memoryStream.WriteByte(0); byte[] bytes = ChunkHelper.compressBytes(ChunkHelper.ToBytes(val), compress: true); ChunkHelper.WriteBytesToStream(memoryStream, bytes); byte[] array = memoryStream.ToArray(); ChunkRaw chunkRaw = createEmptyChunk(array.Length, alloc: false); chunkRaw.Data = array; return(chunkRaw); }
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); }
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); }
public override void ParseFromRaw(ChunkRaw c) { int num = -1; for (int i = 0; i < c.Data.Length; i++) { if (c.Data[i] == 0) { num = i; break; } } if (num < 0 || num > c.Data.Length - 2) { throw new PngjException("bad zTXt chunk: no separator found"); } key = ChunkHelper.ToString(c.Data, 0, num); if (c.Data[num + 1] != 0) { throw new PngjException("bad zTXt chunk: unknown compression method"); } byte[] x = ChunkHelper.compressBytes(c.Data, num + 2, c.Data.Length - num - 2, compress: false); val = ChunkHelper.ToString(x); }