Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Chunks in a PNG

Tags:

I'm looking for a way to get and set those oddball chunks in a PNG file, namely in my case, the 'zTXt' chunk. Is there anything built into the .Net framework, or any other known assembly, that provides access this data? I'd like to avoid having to write an entire PNG reader/writer for this purpose if it has already been done.

Thank you!

Progress update: After more searching, there appears to be nothing pre-made for what I want to accomplish. I'm attempting to read through the file myself now, but am having trouble with the compressed part of the data. I've asked another question for this particular issue here: How do you use a DeflateStream on part of a file?

Once I get this working, I'll post the code here as an answer myself. (Unless someone else beats me to it of course.)

like image 225
YotaXP Avatar asked Apr 06 '09 21:04

YotaXP


2 Answers

The following should work: http://sourceforge.net/projects/pngnet/ Check it out using Subversion. It's a library for low-level access to png files and a small C# example.

like image 140
Peter Nowotnick Avatar answered Oct 12 '22 08:10

Peter Nowotnick


Does it have to be .net? Here's some ruby code to read through the chunks and write them back out again. Insert your processing in the middle

def extract_chunk(input, output)
   lenword = input.read(4) 
   length = lenword.unpack('N')[0]
   type = input.read(4)
   data = length>0 ? input.read(length) : ""
   crc = input.read(4)
   return nil if length<0 || !(('A'..'z')===type[0,1]) 

   #modify data here.

   output.write [data.length].pack('N')
   output.write type
   output.write data
   output.write crc(data)
   return type
end

def extract_png(input, output)
    hdr = input.read(8)
    raise "Not a PNG File" if hdr[0,4]!= "\211PNG"
    raise "file not in binary mode" if hdr[4,4]!="\r\n\032\n"
    output.write(hdr)
    loop do
      chunk_type = extract_chunk(input,output)
      p chunk_type
      break if  chunk_type.nil? || chunk_type == 'IEND' 
    end
end
like image 40
AShelly Avatar answered Oct 12 '22 09:10

AShelly