Thread: Read image data from an image to array

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    4

    Read image data from an image to array

    I would like to read a PNG image file using C (a c++ code is also acceptable) and store the data in an array. Does anyone know a library or bunch of code or a blueprint for how to do that?

    I know libpng and imagmagik. However; I want see the code and structure of it. I want to learn how a code do th
    at.


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    A quick check shows that libpng is open source, so you can always peruse the source code to find out.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2021
    Posts
    4
    I don't know you saw the source code of libpng or not.


    There are many headers and sources. Some of them along together do a specific job. It is hard to say for example what function does what.


    Also, I am not professional in reading source code. So, a simple level code is appropriate for me.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,811
    PNG is not a simple format, so there isn't simple code to read it.

    If you want simple code, then BMP is the file format for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,078
    What is that text image file extension used by some instructors? Maybe that will be better for an newbie.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Nov 2021
    Posts
    4
    I wrote a code for that, and also that was also not simple as it shows.


    A "bmp" image has big volume and it is not common like "png".


    So, I prefer to write one for "png".


    I don't want simple code. I want a simple structure code with comments that describes what happened in the algorithm.

  7. #7
    Registered User
    Join Date
    Oct 2019
    Posts
    46
    Quote Originally Posted by ALIN View Post

    I would like to read a PNG image file using C (a c++ code is also acceptable) and store the data in an array. Does anyone know a library or bunch of code or a blueprint for how to do that?

    It sounds like the libpng API should be what you're interested in. Am I wrong?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,811
    > I don't want simple code. I want a simple structure code with comments that describes what happened in the algorithm.
    Probably doesn't exist.

    Carefully annotated simple code for any passing noob is a hell of an investment for someone to make.
    For one thing, the code is not going to make use of any optimisations that might confuse the poor reader, and as a result, it would suck as an implementation.

    The same goes for anything else you want to study. There isn't a "noob" version of every single piece of code out there.
    Sooner or later, you need to stand on your own.

    There isn't just one algorithm, there are many.
    Portable Network Graphics - Wikipedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2021
    Posts
    4
    I rally don't know. I think I should start from that library.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,811
    Maybe you want this then.
    A simple libpng example program

    Just type "libpng example" into your favourite search engine and read a few examples.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Sep 2020
    Posts
    295
    Hi Alin,

    I've written a PNG file decoder earlier this year. Here's how it works,

    There is a small wrapper that has the signature, image size and some other details in it, including color depth and transparency.

    You then have 'chunks' of either metedata, possibly a colour palette, and then the main data blocks - see Portable Network Graphics (PNG) Specification (Second Edition)

    The data blocks consist of 'lines'. The first byte of the line tells you how this has been pre-filtered to help increase the redundancy in the pixel values, then pixel data for that line. See PNG Specification: Filter Algorithms

    These lines are then compressed using LZSS compressed and Huffman coding, following the "deflate" standard detailed in in RFC1951 and RFC1950 - Deflate - Wikipedia and
    The 'Deflate' algorithm uses either a fixed symbol dictionary or a custom dictionary. The custom dictionary is once again LZSS compressed and Huffman coded, using a smaller symbol set that can be optimized by the encoder.

    So to read to read a PNG file you have to be
    - Comfortable with reading 'chunked' data files
    - Able to 'undo' the pixel filtering algorithms
    - familiar with color palettes and transparency
    - able to unpack binary data
    - able to convert pixels between different color depths and formats (e.g. 4bpp to 24bpp)
    - Able to decompress Huffman Codes
    - Able to expand LZSS compression

    It's a big project with lots of learning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-16-2015, 06:28 AM
  2. Read a 24 bit image using a 2d array
    By leo191919 in forum C Programming
    Replies: 8
    Last Post: 10-29-2013, 12:20 PM
  3. Replies: 4
    Last Post: 03-13-2010, 05:10 AM
  4. Replies: 13
    Last Post: 11-20-2009, 04:43 PM
  5. Problems with Image Magick [Unable to Quantisize Image]
    By Maragato in forum C Programming
    Replies: 1
    Last Post: 09-18-2006, 10:41 PM

Tags for this Thread