Bonjour, j'ai repris un code trouvé sur ce site permettant de charger des textures à partir d'un jpeg. Ce code utilise la librairie libjpeg. Je l'ai juste adapté pour le mettre en c++ (avec une classe au lieu d'une structure). Tout compile parfaitement (bien que le compilo m'indique d'utiliser fopen peu ne pas être sécurisé). En revanche, quand je lance l'exe, un plantage à lieu au niveau de l'appel de la fonction jpeg_read_header(..) dans la méthode ReadJPEGFromFile

J'utilise Visual C++ 2005 express :

texture.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include <jpeglib.h>
using namespace std;
/* Microsoft Visual C++ */
#ifdef _MSC_VER
#pragma comment (lib, "libjpeg.lib")
#endif	/* _MSC_VER */
 
#include "h/texture.h"
 
bool GlTexture_t::ReadJPEGFromFile (const char *filename)
{
 
  FILE *fp = NULL;
  struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr jerr;
  JSAMPROW j;
  int i;
 
  /* open image file */
  fp = fopen (filename, "rb");
  if (!fp)
    {
      fprintf (stderr, "error: couldn't open \"%s\"!\n", filename);
      return 1;
    }
 
 
 
  /* create and configure decompressor */
  jpeg_create_decompress (&cinfo);
  cinfo.err = jpeg_std_error (&jerr);
  jpeg_stdio_src (&cinfo, fp);
 
 
 
  /* read header and prepare for decompression */
cerr<<"ici"<<endl;
  jpeg_read_header (&cinfo, TRUE);
 
  jpeg_start_decompress (&cinfo);
 
  /* initialize image's member variables */
  width = cinfo.image_width;
  height = cinfo.image_height;
  internalFormat = cinfo.num_components;
 
 
 
  if (cinfo.num_components == 1)
    format = GL_LUMINANCE;
  else
    format = GL_RGB;
 
  texels = new GLubyte [width * height * internalFormat];
 
  /* extract each scanline of the image */
  for (i = 0; i < height; ++i)
    {
      j = (texels +
	((height - (i + 1)) * width * internalFormat));
      jpeg_read_scanlines (&cinfo, &j, 1);
    }
 
  /* finish decompression and release memory */
  jpeg_finish_decompress (&cinfo);
  jpeg_destroy_decompress (&cinfo);
 
  fclose (fp);
 
  return 0;
}
 
bool GlTexture_t::loadJPEGTexture (const char *filename)
{
  GLuint tex_id = 0;
 
  ReadJPEGFromFile (filename);
 
  if (this && texels)
    {
      /* generate texture */
      glGenTextures (1, &id);
      glBindTexture (GL_TEXTURE_2D, id);
 
      /* setup some parameters for texture filters and mipmapping */
      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
      /*
      glTexImage2D (GL_TEXTURE_2D, 0, jpeg_tex->internalFormat,
		    jpeg_tex->width, jpeg_tex->height, 0, jpeg_tex->format,
		    GL_UNSIGNED_BYTE, jpeg_tex->texels);
      */
 
      gluBuild2DMipmaps (GL_TEXTURE_2D, internalFormat,width, height, format, GL_UNSIGNED_BYTE, texels);
    }
 
  return true;
}
GLuint GlTexture_t::get_id()
{
	return id;
}
texture.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef TEXTURE_H
#define TEXTURE_H
 
#include <GL/glut.h>
 
class GlTexture_t
{
	private :
 
		GLsizei width;
		GLsizei height;
 
		GLenum format;
		GLint internalFormat;
		GLuint id;
 
		GLubyte *texels;
 
	public :
		bool ReadJPEGFromFile (const char *);
		bool loadJPEGTexture (const char *);
		GLuint get_id();
 
};
 
#endif