Lire une image PNG libpng

shub85 Messages postés 23 Date d'inscription mercredi 21 novembre 2007 Statut Membre Dernière intervention 27 février 2008 - 15 févr. 2008 à 11:46
ktata_ismail Messages postés 7 Date d'inscription samedi 10 juin 2006 Statut Membre Dernière intervention 18 mars 2009 - 10 juin 2008 à 14:43
Bonjour, je voulais savoir si quelqu'un utilise libpng ou si il l'as utilisé. Je cherche à récupérer mes valeurs pixels d'une image 1280*960 , 16 bits en niveaux de gris.
J'obtient des résultats mais j'ai l'impression que mon image n'est pa décompressée. 
Voici mon code:

//Lecture d'une image depuis un  fichier

png_info_struct *ReadPNGFromFile (const char *filename);

FILE *fp;

    typedef byte png_byte;
    png_byte magic[8];

   
/* open image file */
    fp = fopen (
"c:\\users\\pierra\\desktop\\imagespng\\png1.png", "rb");
// recherche de l'image png
    if (!fp)
        {
        fprintf (stderr, "error: couldn't open "%s"!\n",
"c:\\users\\pierra\\desktop\\abc.png");
//test ouverture png
   
        getch();// pause

       
//return NULL;
        }

    /* read magic number */
    fread (magic, 1, sizeof (magic), fp);

    /* check for valid magic number */
    if (!png_check_sig (magic, sizeof (magic)))
        {
        fprintf (stderr, "error: "%s" is not a valid PNG image!\n","c:\\users\\pierra\\desktop\\imagespng\\png1.png");
        fclose (fp);
//        return NULL;
        }

   

/// Création des structures png_struct et png_info

    /* create a png read struct */
    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png_ptr)
        {
        fclose (fp);
        //return NULL;
        }

    /* create a png info struct */
    png_infop info_ptr = png_create_info_struct (png_ptr);
    if (!info_ptr)
        {
        fclose (fp);
        png_destroy_read_struct (&png_ptr, NULL, NULL);
        //return NULL;
        }

 

// Initialisation de la source de lecture du fichier

/* setup libpng for using standard C fread() function with our FILE pointer */
png_init_io (png_ptr, fp);

///* tell libpng that we have already read the magic number */
png_set_sig_bytes (png_ptr, sizeof (magic));

///* read png info */
png_read_info (png_ptr, info_ptr);

// Transformation de l'image source

int bit_depth, color_type;

/* get some usefull information from header */
bit_depth = png_get_bit_depth (png_ptr, info_ptr);
color_type = png_get_color_type (png_ptr, info_ptr);

/* convert index color images to RGB images */

if (color_type == PNG_COLOR_TYPE_PALETTE)
  png_set_palette_to_rgb (png_ptr);
 

/* convert 1-2-4 bits grayscale images to 8 bits grayscale. */

if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
  png_set_gray_1_2_4_to_8 (png_ptr);

if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
  png_set_tRNS_to_alpha (png_ptr);

// Images peuvent stocker un canal de couleur sur 16 bits qu'il faut alors ramener à 8 bits

if (bit_depth == 16)
  png_set_strip_16 (png_ptr);
else if (bit_depth < 8)
 png_set_packing (png_ptr);

/* update info structure to apply transformations */
png_read_update_info (png_ptr, info_ptr);

// Récupération des informations sur l'image

/* retrieve updated information */

png_get_IHDR (png_ptr, info_ptr,(png_uint_32*)(&texinfo->width),
              (png_uint_32*)(&texinfo->height),
              &bit_depth,
              &color_type,NULL, NULL,NULL);

png_bytep row_pointers[960];

   for (int row = 0; row < h; row++)
   {
      row_pointers[row] = (png_bytep)png_malloc(png_ptr, png_get_rowbytes(png_ptr,info_ptr));
   }

// Lecture des données pixels

/* we can now allocate memory for storing pixel data */

/* Read pixel data using row pointers */
png_read_image (png_ptr, row_pointers);

/* Finish decompression and release memory */
png_read_end (png_ptr, NULL);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);

1 réponse

ktata_ismail Messages postés 7 Date d'inscription samedi 10 juin 2006 Statut Membre Dernière intervention 18 mars 2009
10 juin 2008 à 14:43
Salut,
SVP, est ce que tu as trouvé une solution pour ta question?
Et, j'ai essayé de faire tourné ton exemple mais il affiche des erreurs:
C:\example.c(827) : error C2065: 'png_info_struct' : undeclared identifier
C:\example.c(827) : warning C4013: 'ReadPNGFromFile' undefined; assuming extern returning int
C:\example.c(827) : error C2143: syntax error : missing ')' before 'type'
C:\example.c(827) : error C2059: syntax error : ')'
C:\example.c(828) : error C2065: 'FILE' : undeclared identifier
C:\example.c(828) : error C2065: 'fp' : undeclared identifier

Si qq un peut me guider pour tester la libraie png?

Merci pour vos efforts.
                        Ismk.
0
Rejoignez-nous