[C]Question

Fermé
Dauphine - 7 nov. 2006 à 16:54
Char Snipeur Messages postés 9813 Date d'inscription vendredi 23 avril 2004 Statut Contributeur Dernière intervention 3 octobre 2023 - 8 nov. 2006 à 16:19
J'ai une image qui contient des niveaux de noirs et de gris.
Je charge cette image en mémoire.
Puis il me faut parcourir cette image pour calculer le minimum / maximum d'altitude de cette image.
L'atitude trouvé sera comparé à celle du fichier de description
Si la partie de l'image est inhomogene il me faut subdiviser l'image en 4 et si elle est homogene, on ne fait rien.
C'est pour pouvoir créer un quadtree.

Le problème que j'ai concerne la partie en gras et la création de mon quadtree.
Si quelqu'un pouvait m'aider, ce serait sympa.

Merci par avance

7 réponses

Char Snipeur Messages postés 9813 Date d'inscription vendredi 23 avril 2004 Statut Contributeur Dernière intervention 3 octobre 2023 1 298
8 nov. 2006 à 00:04
Salut.
Je voi pas ou est le problème, tu par avec deux variable min=255 et max=0.
tu parcour toute les cases du tableau de l'image et tu regarde sa valeur C
si C>max alors max =C
si C<min alors min =C
tu a alors le min et le max.
0
Salut,

Un fichier de chargement d'image a été fourni mais je n'arrive pas à le comprendre.
ESt-ce que tu pourrais m'indiquer quelle variable permet de donner le C que tu as écris dans ton algo ainsi que la variable qui permet de donner les dimensions de l'image.
ESt-ce que tu pourrais aussi m'indiquer l'algo pour la création d'un quadtree

Merci

#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <png.h>

typedef struct
{
  GLsizei width;
  GLsizei height;

  GLenum format;
  GLint internalFormat;
  GLuint id;

  GLubyte *texels;

} gl_texture_t;

// définition de la structure tex info à partir des informations lues
void
GetPNGtextureInfo (int color_type, gl_texture_t *texinfo)
{
  switch (color_type)
    {
    case PNG_COLOR_TYPE_GRAY:
      texinfo->format = GL_LUMINANCE;
      texinfo->internalFormat = 1;
      break;

    case PNG_COLOR_TYPE_GRAY_ALPHA:
      texinfo->format = GL_LUMINANCE_ALPHA;
      texinfo->internalFormat = 2;
      break;

    case PNG_COLOR_TYPE_RGB:
      texinfo->format = GL_RGB;
      texinfo->internalFormat = 3;
      break;

    case PNG_COLOR_TYPE_RGB_ALPHA:
      texinfo->format = GL_RGBA;
      texinfo->internalFormat = 4;
      break;

    default:
      /* Badness */
      break;
    }
}


gl_texture_t *
ReadPNGFromFile (const char *filename)
{
  gl_texture_t *texinfo;
  png_byte magic[8];
  png_structp png_ptr;
  png_infop info_ptr;
  int bit_depth, color_type;
  FILE *fp = NULL;
  png_bytep *row_pointers = NULL;
  int i;

  /* open image file */
  fp = fopen (filename, "rb");
  if (!fp)
    {
      fprintf (stderr, "error: couldn't open \"%s\"!\n", filename);
      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",
	       filename);
      fclose (fp);
      return NULL;
    }

  /* create a png read struct */
  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 */
  info_ptr = png_create_info_struct (png_ptr);
  if (!info_ptr)
    {
      fclose (fp);
      png_destroy_read_struct (&png_ptr, NULL, NULL);
      return NULL;
    }

  /* create our OpenGL texture object */
  texinfo = (gl_texture_t *)malloc (sizeof (gl_texture_t));

  /* initialize the setjmp for returning properly after a libpng
     error occured */
  if (setjmp (png_jmpbuf (png_ptr)))
    {
      fclose (fp);
      png_destroy_read_struct (&png_ptr, &info_ptr, NULL);

      if (row_pointers)
	free (row_pointers);

      if (texinfo)
	{
	  if (texinfo->texels)
	    free (texinfo->texels);

	  free (texinfo);
	}

      return NULL;
    }

  /* 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);

  /* 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);

  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);

  /* 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);

  /* get image format and components per pixel */
  GetPNGtextureInfo (color_type, texinfo);

  /* we can now allocate memory for storing pixel data */
  texinfo->texels = (GLubyte *)malloc (sizeof (GLubyte) * texinfo->width
	       * texinfo->height * texinfo->internalFormat);

  /* setup a pointer array.  Each one points at the begening of a row. */
  row_pointers = (png_bytep *)malloc (sizeof (png_bytep) * texinfo->height);

  for (i = 0; i < texinfo->height; ++i)
    {
      row_pointers[i] = (png_bytep)(texinfo->texels +
	((texinfo->height - (i + 1)) * texinfo->width * texinfo->internalFormat));
    }

  /* 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);

  /* we don't need row pointers anymore */
  free (row_pointers);

  fclose (fp);
  return texinfo;
}
0
Char Snipeur Messages postés 9813 Date d'inscription vendredi 23 avril 2004 Statut Contributeur Dernière intervention 3 octobre 2023 1 298
8 nov. 2006 à 11:11
Désoler, je ne connait pas ces fonctions.
Mais j'irai chercher dans png.h voir la definition de la variable "gl_texture_t". Tout doit être dedans.
Je pense que tu doit avoir :
struct gl_texture_t{...}
si tu comprend pas, met ici la def, je verrai si je peu t'aider.
0
Comment je fais pour accéder au source du fichier png.h et à la variable gl_texture_t .
Ils se trouvent dans quel répertoire ?
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Char Snipeur Messages postés 9813 Date d'inscription vendredi 23 avril 2004 Statut Contributeur Dernière intervention 3 octobre 2023 1 298
8 nov. 2006 à 11:45
Je ne sais pas ou il a été installer ton png.h, car ce n'est pas un fichier standard.
Mais si il le trouve lors d'une compilation, cherche dans les dossiers sité.
en théorie, il doit être dans un ossier include.
0
J'avais pas fait attention mais gl_texture_t ne fait pas partie de png.h mais a été crée dans le code que j'avais mis plus haut
Voici sa structure :
typedef struct
{
  GLsizei width;
  GLsizei height;

  GLenum format;
  GLint internalFormat;
  GLuint id;

  GLubyte *texels;

} gl_texture_t;
0
Char Snipeur Messages postés 9813 Date d'inscription vendredi 23 avril 2004 Statut Contributeur Dernière intervention 3 octobre 2023 1 298
8 nov. 2006 à 16:19
ba voila, c'est clair.
dsl, j'avais pas vu.
donc c clair, largeur : width, hauteur : height
les valeur (C) doivent être dans texels.
Par contre je ne sais pas l'ordre.
texels doit être de taille height×width, donc pour la recherche de min max, il suffit de faire une boucle :
for (int i=0;i<var.height*var.width;i++)
if(var.texels[i]>max) ....

j'espère que c'est bein ça. Bonne chance
0