COPASI API  4.16.103
CQQtImageTexturizer.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 #include <QtOpenGL/QGLWidget>
7 #include <QtGui/QImage>
8 #include <algorithm>
9 
10 #ifdef __APPLE__
11 # include <OpenGL/gl.h>
12 # include <OpenGL/glu.h>
13 #else
14 # include <GL/gl.h>
15 # include <GL/glu.h>
16 # include <GL/glext.h>
17 # ifndef _WIN32
18 # define GLX_GLXEXT_PROTOTYPES
19 # include <GL/glx.h>
20 # endif // _WIN32
21 #endif // __APPLE__
22 
23 #include "CQQtImageTexturizer.h"
27 #include "copasi/UI/qtUtilities.h"
28 
29 // virtual
30 CLTextureSpec* CQQtImageTexturizer::operator()(const std::string& filename, const std::string& basedir)
31 {
32  std::string reference = to_absolute_path(filename, basedir);
33  CLTextureSpec* pTexture = NULL;
34  // test if the filename ends in "JPG","JPEG","jpeg" or "jpg"
35  std::string::size_type pos = reference.rfind(".");
36  std::string ending;
37 
38  if (pos != std::string::npos)
39  {
40  ++pos;
41  ending = reference.substr(pos);
42  std::transform(ending.begin(), ending.end(), ending.begin(), &tolower);
43  }
44 
45  if (!ending.empty())
46  {
47  if (ending == "jpeg" || ending == "jpg")
48  {
49  pTexture = create_texture_for_image(reference/*,"JPG"*/);
50  }
51  else if (ending == "png")
52  {
53  pTexture = create_texture_for_image(reference/*,"PNG"*/);
54  }
55  }
56 
57  return pTexture;
58 }
59 
60 CLTextureSpec* CQQtImageTexturizer::create_texture_for_image(const std::string& filename, const char* format)
61 {
62  CLTextureSpec* pResult = NULL;
63  // use UTF8 strings for filenames since this seems to be safer across platforms
64  QImage image(FROM_UTF8(filename), format);
65 
66  if (!image.isNull())
67  {
68  image = image.mirrored();
69  QImage glImage = QGLWidget::convertToGLFormat(image);
70  pResult = new CLTextureSpec;
71  pResult->mTextureWidth = pResult->mTextWidth = glImage.width();
72  pResult->mTextureHeight = pResult->mTextHeight = glImage.height();
73  pResult->mNumComponents = 4;
74  glGenTextures(1, &pResult->mTextureName);
75  assert(pResult->mTextureName != 0);
76  glBindTexture(GL_TEXTURE_2D, pResult->mTextureName);
77  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
78  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
79  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
80  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
81  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
82  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)pResult->mTextureWidth, (GLsizei)pResult->mTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, glImage.bits());
83  }
84  else
85  {
86  CCopasiMessage(CCopasiMessage::ERROR, MCLayout + 1, filename.c_str());
87  }
88 
89  return pResult;
90 }
#define MCLayout
static std::string to_absolute_path(const std::string &filename, const std::string &basedir)
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
#define GL_CLAMP_TO_EDGE
Definition: glext.h:95
virtual CLTextureSpec * operator()(const std::string &filename, const std::string &basedir)
double mTextureWidth
void transform(QGraphicsItem *item, const CLTransformation2D *trans, const CLGroup *group)
const CArrayAnnotation * pResult
double mTextureHeight
CLTextureSpec * create_texture_for_image(const std::string &filename, const char *format=NULL)
unsigned int mNumComponents