OiO.lk Blog C# Reading PNG data regarding
C#

Reading PNG data regarding


I am trying to read PNG data with libpng, but I am not able to figure out the last step.

I am trying to read png pixel data. Here is what I came up with

#include <png.h>
#include <pngconf.h>
#include <stdio.h>

#define BYTES_TO_CHECK 8

// checks the file and returns the file pointer
FILE *check_png(char *file_name) {
    char buffer[BYTES_TO_CHECK];
    FILE *fP = fopen(file_name, "rb");
    if (fP == NULL) {
        printf("FILE NOT FOUND!");
        exit(0);
    }
    fread(buffer, 1, BYTES_TO_CHECK, fP);
    int check = png_sig_cmp(buffer, 0, BYTES_TO_CHECK);
    if (check == 1) {
        printf("NOT A PNG");
        exit(0);
    }
    return fP;
}


int main() {

    png_structp png_ptr;
    png_infop info_ptr;
    png_uint_32 height;
    png_uint_32 width;

    printf("ASCII Generator\nPlease Enter the PNG file name\t");
    char file_name[100] = "google.png";
    /*scanf("%s", file_name);*/
    FILE *fP = check_png(file_name);

    png_ptr = png_create_read_struct("1.6.44", NULL, NULL, NULL);
    info_ptr = png_create_info_struct(png_ptr);

    if (png_ptr == NULL)
        printf("ERROR PNG POINTER IS NULL");

    else if (info_ptr == NULL)
        printf("ERROR INFO POINTER IS NULL");

    png_init_io(png_ptr, fP); // initialising input output
    png_set_sig_bytes(png_ptr, BYTES_TO_CHECK); // tells that some bytes are already read

    png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);   // reads png

    height = png_get_image_height(png_ptr, info_ptr);
    width = png_get_image_width(png_ptr, info_ptr);
    printf("%d %d", height, width);

Now to read pixels value I am having some issues

png_bytep row_pointer[height];
png_read_image(png_ptr, row_pointer);

Returns Read Error and it aborts. Then I tried

png_bytepp row_pointer = png_get_rows(png_ptr, info_ptr);

Which returns a array of pointers to the pixel data for each row, but it is all null.

for (int i = 0; i < height; i++) {
        printf("%s", row_pointer[i][50]);
    }

I also tried png_read_rows but to no avail.



You need to sign in to view this answers

Exit mobile version