Thread: Reading PNG chunks.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    Reading PNG chunks.

    Hey there. I'm currently working on a project suggested by Mario F. However I'm stuck in a logical error I don't understand. The code seems right (of course it does) but next.length member gets a ridiculous value (>200000) yet next.name gets the correct first value ('IHDR'). I don't understand how this happens. It is said in the PNG specification that all chunks start with a 4-byte unsigned integer representing the length of the chunk's data section and then follows the chunk name which is also 4-byte-long. That leads me to think that if I have next.name correct, then I am obviously reading in the correct 4-byte unsigned integer for next.length.. Arrr =/

    Here's the code section:

    Code:
    bool imd::imd_png::parse_info_tags()
    {
        chunk c;
        this->skip_png_signature();
        this->get_next_chunk(c);
        return false;
    }
    
    void imd::imd_png::skip_png_signature()
    {
        fseek(this->file_handle, PNG_LENGTH_PNGSIGNATURE, SEEK_SET);
    }
    
    void imd::imd_png::get_next_chunk(imd::imd_png::chunk& next)
    {
        fread(&next.length, PNG_LENGTH_DATASIZE, 1, this->file_handle);
        fread(next.name, PNG_LENGTH_CHUNKNAME, 1, this->file_handle);
    
        if(next.length > 0)
        {
            if(next.data != 0) delete[] next.data;
            next.data = new char[next.length];
    
            fread(next.data, next.length, 1, this->file_handle);
        }
    
        fread(&next.crc, PNG_LENGTH_CRC, 1, this->file_handle);
        int a = 1;
    }
    The problematic function is (I think) get_next_chunk() but I fail to see the problem. parse_info_tags() shows how I am calling the get_next_chunk() but it's only in a test situation, it is not the actual real code.

    Any ideas ?

    Edit:

    PNG_LENGTH_DATASIZE = 4
    PNG_LENGTH_CHUNKNAME = 4
    PNG_LENGTH_CRC = 4
    PNG_LENGTH_PNGSIGNATURE = 8
    Last edited by Desolation; 03-12-2008 at 10:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  3. Replies: 4
    Last Post: 05-04-2007, 01:09 AM
  4. Reading a file in 1-kilobyte chunks...
    By Crilston in forum C Programming
    Replies: 1
    Last Post: 06-30-2005, 04:33 PM
  5. Reading chunks from a file
    By Skarr in forum Linux Programming
    Replies: 1
    Last Post: 09-07-2002, 04:53 AM