From b3da5d3fab557c8bc44c070e297c18eeaab67551 Mon Sep 17 00:00:00 2001 From: Takahito Tejima Date: Wed, 29 Apr 2015 17:37:43 -0700 Subject: [PATCH] remove libpng and use stb_image_write instead --- examples/CMakeLists.txt | 8 --- examples/common/CMakeLists.txt | 1 + examples/common/gl_framebuffer.cpp | 55 ++++-------------- .../{glImaging => common}/stb_image_write.h | 0 examples/glImaging/CMakeLists.txt | 11 ---- examples/glImaging/glImaging.cpp | 4 +- examples/glPtexViewer/CMakeLists.txt | 8 --- examples/glPtexViewer/glPtexViewer.cpp | 56 +++---------------- 8 files changed, 19 insertions(+), 124 deletions(-) rename examples/{glImaging => common}/stb_image_write.h (100%) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index aa6f9fe2..e5f097f5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -22,14 +22,6 @@ # language governing permissions and limitations under the Apache License. # -# optional dependency - enables screenshots in some examples -find_package(PNG) -if (PNG_FOUND) - include_directories("${PNG_INCLUDE_DIRS}") - list(APPEND PLATFORM_LIBRARIES "${PNG_LIBRARIES}") - add_definitions(-DOPENSUBDIV_HAS_PNG) -endif() - add_subdirectory(common) if (NOT NO_OPENGL) diff --git a/examples/common/CMakeLists.txt b/examples/common/CMakeLists.txt index 81730d82..9c3c5c12 100644 --- a/examples/common/CMakeLists.txt +++ b/examples/common/CMakeLists.txt @@ -42,6 +42,7 @@ set(EXAMPLES_COMMON_HEADER_FILES objAnim.h patchColors.h simple_math.h + stb_image_write.h stopwatch.h ) diff --git a/examples/common/gl_framebuffer.cpp b/examples/common/gl_framebuffer.cpp index 33c8c326..a75b6ce4 100644 --- a/examples/common/gl_framebuffer.cpp +++ b/examples/common/gl_framebuffer.cpp @@ -30,11 +30,12 @@ #include #include -#ifdef OPENSUBDIV_HAS_PNG - #include - #include -#endif +#define STB_IMAGE_WRITE_IMPLEMENTATION 1 +#include "stb_image_write.h" +#if _MSC_VER +#define snprintf _snprintf +#endif GLFrameBuffer::GLFrameBuffer() : _width(0), _height(0), @@ -303,12 +304,10 @@ private: void GLFrameBuffer::Screenshot() const { -#ifdef OPENSUBDIV_HAS_PNG - int width = GetWidth(), height = GetHeight(); - void * buf = malloc(GetWidth() * GetHeight() * 4 /*RGBA*/); + std::vector data(width*height*4 /*RGBA*/); PixelStoreState pixelStore; @@ -326,7 +325,7 @@ GLFrameBuffer::Screenshot() const { glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, _frameBufferColor ); - glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data[0]); glActiveTexture( restoreActiveTexture ); glBindTexture( GL_TEXTURE_2D, restoreBinding ); @@ -339,44 +338,10 @@ GLFrameBuffer::Screenshot() const { char fname[64]; snprintf(fname, 64, "screenshot.%d.png", counter++); - if (FILE * f = fopen( fname, "w" )) { + // flip vertical + stbi_write_png(fname, width, height, 4, &data[width*4*(height-1)], -width*4); - png_structp png_ptr = - png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - assert(png_ptr); - - png_infop info_ptr = - png_create_info_struct(png_ptr); - assert(info_ptr); - - png_set_IHDR(png_ptr, info_ptr, width, height, 8, - PNG_COLOR_TYPE_RGB_ALPHA, - PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_DEFAULT, - PNG_FILTER_TYPE_DEFAULT ); - - png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); - - png_bytep rows_ptr[ height ]; - for(int i = 0; i #include "../common/patchColors.h" +#include "../common/stb_image_write.h" // common.obj has an implementation. #include "init_shapes.h" -#define STB_IMAGE_WRITE_IMPLEMENTATION 1 -#include "stb_image_write.h" - using namespace OpenSubdiv; static const char *shaderSource = diff --git a/examples/glPtexViewer/CMakeLists.txt b/examples/glPtexViewer/CMakeLists.txt index 69187045..322ea9a7 100644 --- a/examples/glPtexViewer/CMakeLists.txt +++ b/examples/glPtexViewer/CMakeLists.txt @@ -58,14 +58,6 @@ if( OPENCL_FOUND ) include_directories("${OPENCL_INCLUDE_DIRS}") endif() -# optional dependency - enables screenshots -find_package(PNG) -if (PNG_FOUND) - include_directories("${PNG_INCLUDE_DIRS}") - list(APPEND PLATFORM_LIBRARIES "${PNG_LIBRARIES}") - add_definitions(-DOPENSUBDIV_HAS_PNG) -endif() - #------------------------------------------------------------------------------- _stringify("${SHADER_FILES}" INC_FILES) diff --git a/examples/glPtexViewer/glPtexViewer.cpp b/examples/glPtexViewer/glPtexViewer.cpp index d9585c7b..63711bb3 100644 --- a/examples/glPtexViewer/glPtexViewer.cpp +++ b/examples/glPtexViewer/glPtexViewer.cpp @@ -50,11 +50,6 @@ GLFWmonitor* g_primary = 0; #include #include -#ifdef OPENSUBDIV_HAS_PNG - #include - #include -#endif - #include #include #include @@ -124,6 +119,7 @@ OpenSubdiv::Osd::GLMeshInterface *g_mesh; #include "../common/gl_hud.h" #include "../common/patchColors.h" #include "../common/hdr_reader.h" +#include "../common/stb_image_write.h" static const char *g_defaultShaderSource = #if defined(GL_ARB_tessellation_shader) || defined(GL_VERSION_4_0) @@ -1887,20 +1883,16 @@ display() { //------------------------------------------------------------------------------ void screenshot(int multiplier=4) { -#ifdef OPENSUBDIV_HAS_PNG + int oldwidth = g_width, oldheight = g_height, width = multiplier * g_width, height = multiplier * g_height; - reshape(g_window, width, height); - display(); - void * buf = malloc(width * height * 4); - - glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); + std::vector data(width*height*4 /*RGBA*/); glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_ALIGNMENT, 1); @@ -1914,11 +1906,10 @@ screenshot(int multiplier=4) { glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, g_imageShader.frameBufferTexture ); - glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data[0]); glActiveTexture( restoreActiveTexture ); glBindTexture( GL_TEXTURE_2D, restoreBinding ); - glPopClientAttrib(); reshape(g_window, oldwidth, oldheight); @@ -1928,43 +1919,10 @@ screenshot(int multiplier=4) { char fname[64]; snprintf(fname, 64, "screenshot.%d.png", counter++); - if (FILE * f = fopen( fname, "w" )) { + // flip vertical + stbi_write_png(fname, width, height, 4, &data[width*4*(height-1)], -width*4); - png_structp png_ptr = - png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - assert(png_ptr); - - png_infop info_ptr = - png_create_info_struct(png_ptr); - assert(info_ptr); - - png_set_IHDR(png_ptr, info_ptr, width, height, 8, - PNG_COLOR_TYPE_RGB_ALPHA, - PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_DEFAULT, - PNG_FILTER_TYPE_DEFAULT ); - - png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); - - png_bytep rows_ptr[ height ]; - for(int i = 0; i