Update databases/mariadb105-server to version 10.5.17
[dports.git] / graphics / simpleviewer / files / patch-src_main.cpp
1 --- src/main.cpp.orig   2019-11-13 08:52:44 UTC
2 +++ src/main.cpp
3 @@ -13,6 +13,7 @@
4  #include "viewer.h"
5  
6  #include <GLFW/glfw3.h>
7 +#include <png.h>
8  
9  #include <clocale>
10  #include <cstdio>
11 @@ -148,6 +149,64 @@ namespace
12          ::printf("(EE) GLFW error (%d) '%s'\n", e, error);
13      }
14  
15 +    // Based on https://gist.github.com/niw/5963798
16 +    void load_png_icon(const char *filename, GLFWimage *icon)
17 +    {
18 +        png_byte color_type;
19 +        png_byte bit_depth;
20 +
21 +        FILE *fp = fopen(filename, "rb");
22 +        if (!fp) return;
23 +
24 +        png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
25 +        if (!png) return;
26 +        png_infop info = png_create_info_struct(png);
27 +        if (!info) return;
28 +        if (setjmp(png_jmpbuf(png))) return;
29 +
30 +        png_init_io(png, fp);
31 +        png_read_info(png, info);
32 +
33 +        icon->width = png_get_image_width(png, info);
34 +        icon->height = png_get_image_height(png, info);
35 +        color_type = png_get_color_type(png, info);
36 +        bit_depth = png_get_bit_depth(png, info);
37 +
38 +        // Read any color_type into 8bit depth, RGBA format.
39 +        // See http://www.libpng.org/pub/png/libpng-manual.txt
40 +        if (bit_depth == 16)
41 +            png_set_strip_16(png);
42 +        if (color_type == PNG_COLOR_TYPE_PALETTE)
43 +            png_set_palette_to_rgb(png);
44 +        // PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16 bit depth.
45 +        if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
46 +            png_set_expand_gray_1_2_4_to_8(png);
47 +        if (png_get_valid(png, info, PNG_INFO_tRNS))
48 +            png_set_tRNS_to_alpha(png);
49 +        // These color_type don't have an alpha channel then fill it with 0xFF.
50 +        if (color_type == PNG_COLOR_TYPE_RGB ||
51 +          color_type == PNG_COLOR_TYPE_GRAY ||
52 +          color_type == PNG_COLOR_TYPE_PALETTE)
53 +            png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
54 +        if (color_type == PNG_COLOR_TYPE_GRAY ||
55 +          color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
56 +            png_set_gray_to_rgb(png);
57 +        png_read_update_info(png, info);
58 +
59 +        // Allocate contiguous memory region; caller has to delete[].
60 +        icon->pixels = new unsigned char[icon->width * icon->height * 4];
61 +
62 +        auto *row_pointers = new png_bytep[icon->height];
63 +        png_byte bpr = png_get_rowbytes(png, info);
64 +        for (int y = 0; y < icon->height; y++)
65 +            row_pointers[y] = icon->pixels + y * bpr;
66 +
67 +        png_read_image(png, row_pointers);
68 +        delete[] row_pointers;
69 +        png_destroy_read_struct(&png, &info, NULL);
70 +        fclose(fp);
71 +    }
72 +
73      void setup(GLFWwindow* window)
74      {
75          glfwMakeContextCurrent(window);
76 @@ -173,6 +232,18 @@ namespace
77  #if GLFW_VERSION_MAJOR >= 3 && GLFW_VERSION_MINOR >= 1
78          glfwSetDropCallback(window, callbackDrop);
79  #endif
80 +
81 +        GLFWimage icons[3];
82 +
83 +        load_png_icon("%%DATADIR%%/Icon-1024.png", &icons[0]);
84 +        load_png_icon("%%DATADIR%%/Icon-32.png", &icons[1]);
85 +        load_png_icon("%%DATADIR%%/Icon-16.png", &icons[2]);
86 +
87 +        glfwSetWindowIcon(window, 3, icons);
88 +
89 +        delete[] icons[2].pixels;
90 +        delete[] icons[1].pixels;
91 +        delete[] icons[0].pixels;
92      }
93  
94      GLFWwindow* createWindowedWindow(int width, int height, GLFWwindow* parent, const sConfig& config)