31 std::string::size_type pos = reference.rfind(
".");
34 if (pos != std::string::npos)
37 ending = reference.substr(pos);
38 std::transform(ending.begin(), ending.end(), ending.begin(), (int(*)(int))std::tolower);
43 if (ending ==
"jpeg" || ending ==
"jpg")
47 else if (ending ==
"png")
64 FILE *fp = fopen(filename.c_str(),
"rb");
68 struct jpeg_decompress_struct cinfo;
69 struct jpeg_error_mgr jerr;
70 cinfo.err = jpeg_std_error(&jerr);
71 jpeg_create_decompress(&cinfo);
72 jpeg_stdio_src(&cinfo, fp);
73 jpeg_read_header(&cinfo,
true);
74 cinfo.out_color_space = JCS_RGB;
75 jpeg_start_decompress(&cinfo);
77 unsigned int width = cinfo.output_width;
78 assert(cinfo.output_components == 3);
79 unsigned int row_stride = width * cinfo.output_components;
80 unsigned int height = cinfo.output_height;
86 GLubyte* pData =
new GLubyte[3 * width * height];
87 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
88 ((j_common_ptr) & cinfo, JPOOL_IMAGE, row_stride, 1);
90 while (cinfo.output_scanline < cinfo.output_height)
92 jpeg_read_scanlines(&cinfo, buffer, 1);
93 memcpy(pData + (cinfo.output_scanline - 1)*row_stride, buffer[0], row_stride);
96 jpeg_finish_decompress(&cinfo);
97 jpeg_destroy_decompress(&cinfo);
104 width = (
unsigned int)ceil(log(pTexture->
mTextureWidth) / log(2.0));
105 height = (
unsigned int)ceil(log(pTexture->
mTextureHeight) / log(2.0));
106 width = (1 << width);
107 height = (1 << height);
108 GLenum format = GL_RGB;
109 GLint internalFormat = GL_RGB;
110 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
111 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
113 while (w == 0 && width > 0 && height > 0)
117 height = height >> 1;
118 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
119 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
130 if (w < pTexture->mTextureWidth)
133 std::cerr <<
"Image texture to large. Scaling down to size that is supported by current OpenGL implementation." << std::endl;
137 GLubyte* newData =
new GLubyte[pTexture->
mNumComponents * width * height];
139 if (gluScaleImage(format, (GLint)pTexture->
mTextureWidth, (GLint)pTexture->
mTextureHeight, GL_UNSIGNED_BYTE, pData, width, height, GL_UNSIGNED_BYTE, newData) == 0)
156 if (pTexture != NULL)
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
165 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
166 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (GLsizei)pTexture->
mTextureWidth, (GLsizei)pTexture->
mTextureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);
184 FILE *fp = fopen(filename.c_str(),
"rb");
189 fread(header, 1, 8, fp);
191 if (!png_sig_cmp(header, 0, 8))
193 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
197 png_infop info_ptr = png_create_info_struct(png_ptr);
198 png_infop end_info = NULL;
202 end_info = png_create_info_struct(png_ptr);
206 png_init_io(png_ptr, fp);
207 png_set_sig_bytes(png_ptr, 8);
208 png_read_info(png_ptr, info_ptr);
209 unsigned int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
210 unsigned int color_type = png_get_color_type(png_ptr, info_ptr);
213 if (color_type == PNG_COLOR_TYPE_PALETTE)
215 png_set_palette_to_rgb(png_ptr);
219 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
221 png_set_gray_1_2_4_to_8(png_ptr);
225 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
227 png_set_tRNS_to_alpha(png_ptr);
233 png_set_strip_16(png_ptr);
239 png_set_packing(png_ptr);
243 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
245 png_set_gray_to_rgb(png_ptr);
249 if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY)
251 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
256 png_set_interlace_handling(png_ptr);
259 png_read_update_info(png_ptr, info_ptr);
260 unsigned int width = png_get_image_width(png_ptr, info_ptr);
261 unsigned int height = png_get_image_height(png_ptr, info_ptr);
269 GLubyte* pData =
new GLubyte[4 * width * height];
275 png_bytepp pRow_pointers =
new png_bytep[height];
278 for (i = 0; i < height; ++i)
280 pRow_pointers[i] = (&(pData[4 * width * i]));
283 png_read_image(png_ptr, pRow_pointers);
284 png_read_end(png_ptr, end_info);
290 width = (
unsigned int)ceil(log(pTexture->
mTextureWidth) / log(2.0));
291 height = (
unsigned int)ceil(log(pTexture->
mTextureHeight) / log(2.0));
292 width = (1 << width);
293 height = (1 << height);
294 GLenum format = GL_RGBA;
295 GLint internalFormat = GL_RGBA;
296 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
297 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
299 while (w == 0 && width > 0 && height > 0)
303 height = height >> 1;
304 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
305 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
316 if (w < pTexture->mTextureWidth)
319 std::cerr <<
"Image texture to large. Scaling down to size that is supported by current OpenGL implementation." << std::endl;
323 GLubyte* newData =
new GLubyte[pTexture->
mNumComponents * width * height];
325 if (gluScaleImage(format, (GLint)pTexture->
mTextureWidth, (GLint)pTexture->
mTextureHeight, GL_UNSIGNED_BYTE, pData, width, height, GL_UNSIGNED_BYTE, newData) == 0)
342 if (pTexture != NULL)
347 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
351 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
352 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (GLsizei)pTexture->
mTextureWidth, (GLsizei)pTexture->
mTextureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);
357 delete[] pRow_pointers;
363 png_destroy_read_struct(&png_ptr, (png_infopp)&info_ptr, (png_infopp)&end_info);
static std::string to_absolute_path(const std::string &filename, const std::string &basedir)
void transform(QGraphicsItem *item, const CLTransformation2D *trans, const CLGroup *group)
CLTextureSpec * create_texture_for_jpeg_image(const std::string &filename)
CLTextureSpec * create_texture_for_image(const std::string &filename, const std::string &basedir)
CLTextureSpec * create_texture_for_png_image(const std::string &filename)
unsigned int mNumComponents
virtual CLTextureSpec * operator()(const std::string &filename, const std::string &basedir)