Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
igl::png Namespace Reference

Functions

IGL_INLINE bool readPNG (const std::string png_file, Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &R, Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &G, Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &B, Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &A)
 
IGL_INLINE bool render_to_png (const std::string png_file, const int width, const int height, const bool alpha=true, const bool fast=false)
 
IGL_INLINE std::thread render_to_png_async (const std::string png_file, const int width, const int height, const bool alpha=true, const bool fast=false)
 
IGL_INLINE bool texture_from_file (const std::string filename, GLuint &id)
 
IGL_INLINE bool texture_from_png (const std::string png_file, const bool flip, GLuint &id)
 
IGL_INLINE bool texture_from_png (const std::string png_file, GLuint &id)
 
IGL_INLINE bool texture_from_png (const std::string png_file, Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &R, Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &G, Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &B, Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &A)
 
IGL_INLINE bool writePNG (const Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &R, const Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &G, const Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &B, const Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &A, const std::string png_file)
 

Function Documentation

◆ readPNG()

IGL_INLINE bool igl::png::readPNG ( const std::string  png_file,
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  R,
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  G,
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  B,
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  A 
)
18{
19 int cols,rows,n;
20 unsigned char *data = stbi_load(png_file.c_str(), &cols, &rows, &n, 4);
21 if(data == NULL) {
22 return false;
23 }
24
25 R.resize(cols,rows);
26 G.resize(cols,rows);
27 B.resize(cols,rows);
28 A.resize(cols,rows);
29
30 for (unsigned i=0; i<rows; ++i) {
31 for (unsigned j=0; j<cols; ++j) {
32 R(j,rows-1-i) = data[4*(j + cols * i) + 0];
33 G(j,rows-1-i) = data[4*(j + cols * i) + 1];
34 B(j,rows-1-i) = data[4*(j + cols * i) + 2];
35 A(j,rows-1-i) = data[4*(j + cols * i) + 3];
36 }
37 }
38
39 igl::stbi_image_free(data);
40
41 return true;
42}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Definition PlainObjectBase.h:279

References Eigen::PlainObjectBase< Derived >::resize().

+ Here is the call graph for this function:

◆ render_to_png()

IGL_INLINE bool igl::png::render_to_png ( const std::string  png_file,
const int  width,
const int  height,
const bool  alpha = true,
const bool  fast = false 
)
19{
20 unsigned char * data = new unsigned char[4*width*height];
21 glReadPixels(
22 0,
23 0,
24 width,
25 height,
26 GL_RGBA,
27 GL_UNSIGNED_BYTE,
28 data);
29 //img->flip();
30 if(!alpha)
31 {
32 for(int i = 0;i<width;i++)
33 for(int j = 0;j<height;j++)
34 {
35 data[4*(i+j*width)+3] = 255;
36 }
37 }
38 bool ret = igl::stbi_write_png(png_file.c_str(), width, height, 4, data, 4*width*sizeof(unsigned char));
39 delete [] data;
40 return ret;
41}

◆ render_to_png_async()

IGL_INLINE std::thread igl::png::render_to_png_async ( const std::string  png_file,
const int  width,
const int  height,
const bool  alpha = true,
const bool  fast = false 
)
39{
40 // Part that should serial
41 unsigned char * data = new unsigned char[width*height];
42 glReadPixels(
43 0,
44 0,
45 width,
46 height,
47 GL_RGBA,
48 GL_UNSIGNED_BYTE,
49 data);
50 // Part that should be asynchronous
51 std::thread t(render_to_png_async_helper,data,width,height,png_file,alpha,fast);
52 t.detach();
53 return t;
54}
static IGL_INLINE bool render_to_png_async_helper(unsigned char *img, int width, int height, const std::string png_file, const bool alpha, const bool fast)
Definition render_to_png_async.cpp:12

References render_to_png_async_helper().

+ Here is the call graph for this function:

◆ texture_from_file()

IGL_INLINE bool igl::png::texture_from_file ( const std::string  filename,
GLuint id 
)
20{
21 using namespace igl::opengl;
22 using namespace std;
23 // dirname, basename, extension and filename
24 string d,b,ext,f;
25 pathinfo(filename,d,b,ext,f);
26 // Convert extension to lower case
27 transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
28 //if(ext == "tga")
29 //{
30 // return texture_from_tga(filename,id);
31 //}else
32 if(ext == "png")
33 {
34 return texture_from_png(filename,id);
35 }else
36 {
37#ifdef __APPLE__
38 // Convert to a temporary png file
39 string tmp = "/var/tmp/.texture_from_file.png";
40#define PATH_TO_CONVERT "/opt/local/bin/convert"
41 string command = STR(PATH_TO_CONVERT<<" \""<<filename<<"\" \""<<tmp<<"\"");
42 try
43 {
44 if(system(command.c_str())==0)
45 {
46 return texture_from_file(tmp.c_str(),id);
47 }else
48 {
49 cerr<<"texture_from_file: calling to convert ('"<<command<<"') failed..."<<endl;
50 return false;
51 }
52 }catch(int e)
53 {
54 cerr<<"^"<<__FUNCTION__<<": Calling to convert crashed..."<<endl;
55 return false;
56 }
57#else
58 return false;
59#endif
60 }
61}
#define STR(X)
Definition STR.h:17
Definition render_to_tga.h:16
IGL_INLINE bool texture_from_png(const std::string png_file, const bool flip, GLuint &id)
Definition texture_from_png.cpp:13
IGL_INLINE void pathinfo(const std::string &path, std::string &dirname, std::string &basename, std::string &extension, std::string &filename)
Definition pathinfo.cpp:16
STL namespace.
Definition term.c:39

References igl::pathinfo(), STR, texture_from_file(), and texture_from_png().

Referenced by texture_from_file().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ texture_from_png() [1/3]

IGL_INLINE bool igl::png::texture_from_png ( const std::string  png_file,
const bool  flip,
GLuint id 
)
14{
15 int width,height,n;
16 unsigned char *data = igl::stbi_load(png_file.c_str(), &width, &height, &n, 4);
17 if(data == NULL) {
18 return false;
19 }
20
21 // Why do I need to flip?
22 /*if(flip)
23 {
24 yimg.flip();
25 }*/
26
27 glGenTextures(1, &id);
28 glBindTexture(GL_TEXTURE_2D, id);
29 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
30 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
31 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
32 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
33 glTexImage2D(
34 GL_TEXTURE_2D, 0, GL_RGB,
35 width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
36 glBindTexture(GL_TEXTURE_2D, 0);
37
38 igl::stbi_image_free(data);
39
40 return true;
41}

Referenced by texture_from_file(), and texture_from_png().

+ Here is the caller graph for this function:

◆ texture_from_png() [2/3]

IGL_INLINE bool igl::png::texture_from_png ( const std::string  png_file,
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  R,
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  G,
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  B,
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  A 
)
56{
57 int width,height,n;
58 unsigned char *data = igl::stbi_load(png_file.c_str(), &width, &height, &n, 4);
59 if(data == NULL) {
60 return false;
61 }
62
63 R.resize(height,width);
64 G.resize(height,width);
65 B.resize(height,width);
66 A.resize(height,width);
67
68 for (unsigned j=0; j<height; ++j) {
69 for (unsigned i=0; i<width; ++i) {
70 // used to flip with libPNG, but I'm not sure if
71 // simply j*width + i wouldn't be better
72 // stb_image uses horizontal scanline an starts top-left corner
73 R(i,j) = data[4*( (width-1-i) + width * (height-1-j) )];
74 G(i,j) = data[4*( (width-1-i) + width * (height-1-j) ) + 1];
75 B(i,j) = data[4*( (width-1-i) + width * (height-1-j) ) + 2];
76 //A(i,j) = data[4*( (width-1-i) + width * (height-1-j) ) + 3];
77 }
78 }
79
80 igl::stbi_image_free(data);
81
82 return true;
83}

References Eigen::PlainObjectBase< Derived >::resize().

+ Here is the call graph for this function:

◆ texture_from_png() [3/3]

IGL_INLINE bool igl::png::texture_from_png ( const std::string  png_file,
GLuint id 
)
44{
45 return texture_from_png(png_file,false,id);
46}

References texture_from_png().

+ Here is the call graph for this function:

◆ writePNG()

IGL_INLINE bool igl::png::writePNG ( const Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  R,
const Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  G,
const Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  B,
const Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > &  A,
const std::string  png_file 
)
19{
20 assert((R.rows() == G.rows()) && (G.rows() == B.rows()) && (B.rows() == A.rows()));
21 assert((R.cols() == G.cols()) && (G.cols() == B.cols()) && (B.cols() == A.cols()));
22
23 const int comp = 4; // 4 Channels Red, Green, Blue, Alpha
24 const int stride_in_bytes = R.rows()*comp; // Length of one row in bytes
25 std::vector<unsigned char> data(R.size()*comp,0); // The image itself;
26
27 for (unsigned i = 0; i<R.rows();++i)
28 {
29 for (unsigned j = 0; j < R.cols(); ++j)
30 {
31 data[(j * R.rows() * comp) + (i * comp) + 0] = R(i,R.cols()-1-j);
32 data[(j * R.rows() * comp) + (i * comp) + 1] = G(i,R.cols()-1-j);
33 data[(j * R.rows() * comp) + (i * comp) + 2] = B(i,R.cols()-1-j);
34 data[(j * R.rows() * comp) + (i * comp) + 3] = A(i,R.cols()-1-j);
35 }
36 }
37
38 igl::stbi_write_png(png_file.c_str(), R.rows(), R.cols(), comp, data.data(), stride_in_bytes);
39
40 return true;
41}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index cols() const
Definition PlainObjectBase.h:153
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rows() const
Definition PlainObjectBase.h:151

References Eigen::PlainObjectBase< Derived >::cols(), and Eigen::PlainObjectBase< Derived >::rows().

+ Here is the call graph for this function: