A simple PNG shell that works. Does anyone have a snippet to share?

I am looking for a way to get a buffer of image data in a PNG file and a way to get a PNG file into a buffer.

There are only two things I want to do.

It would be a dead simple shell that uses png.h Well, not quite dead just because of the terribly complex libpng API, but there is a concept.

I tried DevIL before. It is much easier to use than libpng. However, I used issues . In addition, Devil does too much. I need only the exact and average basic support for the PNG format, and not 20 other formats.

Then I find this page . I praised Pixel Fairy and Almighty Google for giving me an implementation on a silver platter ... Then it turns out that this spins the image: on the processed image, every fourth pixel in every scan line disappears. I am quite sure that I am reading the source that this should not happen! He had to zero out red and set green to blue. This did not happen either.

I also tried png ++. The problem that I encountered is that I could not get the data from PNG in a format compatible for loading in OpenGL, I would have to build another buffer. It just looked ugly, but I will definitely try png ++ again before I even think about giving Delilah another shot. Because png ++ worked, at least. He also got a heading aspect for him. However, this caused a bunch of compiler warnings.

Are there any other rivals? Anyone who worked directly with libpng would know how to do what I ask: one function that takes a file name and populates a buffer with 32 bits and sets two permission integers; one function that takes a buffer with 32 bits, two permission integers and a file name.

Update-edit: I found this one . Maybe something is there.

+4
source share
3 answers

This tutorial seems to have what you want.

From the link:

  //Here one of the pointers we've defined in the error handler section: //Array of row pointers. One for every row. rowPtrs = new png_bytep[imgHeight]; //Alocate a buffer with enough space. //(Don't use the stack, these blocks get big easilly) //This pointer was also defined in the error handling section, so we can clean it up on error. data = new char[imgWidth * imgHeight * bitdepth * channels / 8]; //This is the length in bytes, of one row. const unsigned int stride = imgWidth * bitdepth * channels / 8; //A little for-loop here to set all the row pointers to the starting //Adresses for every row in the buffer for (size_t i = 0; i < imgHeight; i++) { //Set the pointer to the data pointer + i times the row stride. //Notice that the row order is reversed with q. //This is how at least OpenGL expects it, //and how many other image loaders present the data. png_uint_32 q = (imgHeight- i - 1) * stride; rowPtrs[i] = (png_bytep)data + q; } //And here it is! The actuall reading of the image! //Read the imagedata and write it to the adresses pointed to //by rowptrs (in other words: our image databuffer) png_read_image(pngPtr, rowPtrs); 
+2
source

Sean Barrett wrote two files with a public file for a PNG image read / write .

+1
source

I would add CImg to the parameter list. Although it is an image library, the API is not as high as most (devil / imagemagick / freeimage / GIL). This is also the title.

The image class has a simple width and open access data elements. Under the hood, libpng is used (if you specify it using the preprocessor directive). Data is transferred to any type that you select for the template image object.

 CImg<uint8_t>myRGBA("fname.png"); myRGBA._data[0] = 255; //set red value of first pixel 
+1
source

Source: https://habr.com/ru/post/1394947/


All Articles