Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

png2img.cc 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // ----------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2007-2010 Fons Adriaensen <fons@linuxaudio.org>
  4. // Modified by falkTX on Jan 2015 for inclusion in Carla
  5. //
  6. // This program is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 2 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. //
  20. // ----------------------------------------------------------------------
  21. #include <png.h>
  22. #include <clxclient.h>
  23. namespace BLS1 {
  24. XImage *png2img (const char *file, X_display *disp, XftColor *bgnd)
  25. {
  26. FILE *F;
  27. png_byte hdr [8];
  28. png_structp png_ptr;
  29. png_infop png_info;
  30. const unsigned char **data, *p;
  31. int dx, dy, x, y, dp;
  32. float vr, vg, vb, va, br, bg, bb;
  33. unsigned long mr, mg, mb, pix;
  34. XImage *image;
  35. F = fopen (file, "r");
  36. if (!F)
  37. {
  38. fprintf (stderr, "Can't open '%s'\n", file);
  39. return 0;
  40. }
  41. fread (hdr, 1, 8, F);
  42. if (png_sig_cmp (hdr, 0, 8))
  43. {
  44. fprintf (stderr, "'%s' is not a PNG file\n", file);
  45. return 0;
  46. }
  47. fseek (F, 0, SEEK_SET);
  48. png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
  49. if (! png_ptr)
  50. {
  51. fclose (F);
  52. return 0;
  53. }
  54. png_info = png_create_info_struct (png_ptr);
  55. if (! png_info)
  56. {
  57. png_destroy_read_struct (&png_ptr, 0, 0);
  58. fclose (F);
  59. return 0;
  60. }
  61. if (setjmp (png_jmpbuf (png_ptr)))
  62. {
  63. png_destroy_read_struct (&png_ptr, &png_info, 0);
  64. fclose (F);
  65. fprintf (stderr, "png:longjmp()\n");
  66. return 0;
  67. }
  68. png_init_io (png_ptr, F);
  69. png_read_png (png_ptr, png_info,
  70. PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND,
  71. 0);
  72. // This requires libpng14 or later. If you still have an
  73. // older version, use the three commented lines instead.
  74. dx = png_get_image_width (png_ptr, png_info);
  75. dy = png_get_image_height (png_ptr, png_info);
  76. dp = (png_get_color_type (png_ptr, png_info) & PNG_COLOR_MASK_ALPHA) ? 4 : 3;
  77. // dx = png_info->width;
  78. // dy = png_info->height;
  79. // dp = (png_info->color_type & PNG_COLOR_MASK_ALPHA) ? 4 : 3;
  80. data = (const unsigned char **)(png_get_rows (png_ptr, png_info));
  81. image = XCreateImage (disp->dpy (),
  82. disp->dvi (),
  83. DefaultDepth (disp->dpy (), disp->dsn ()),
  84. ZPixmap, 0, 0, dx, dy, 32, 0);
  85. image->data = new char [image->height * image->bytes_per_line];
  86. mr = image->red_mask;
  87. mg = image->green_mask;
  88. mb = image->blue_mask;
  89. vr = mr / 255.0f;
  90. vg = mg / 255.0f;
  91. vb = mb / 255.0f;
  92. if (bgnd)
  93. {
  94. br = bgnd->color.red >> 8;
  95. bg = bgnd->color.green >> 8;
  96. bb = bgnd->color.blue >> 8;
  97. }
  98. else br = bg = bb = 0;
  99. for (y = 0; y < dy; y++)
  100. {
  101. p = data [y];
  102. for (x = 0; x < dx; x++)
  103. {
  104. va = (dp == 4) ? (p [3] / 255.0f) : 1;
  105. pix = ((unsigned long)((p [0] * va + (1 - va) * br) * vr) & mr)
  106. | ((unsigned long)((p [1] * va + (1 - va) * bg) * vg) & mg)
  107. | ((unsigned long)((p [2] * va + (1 - va) * bb) * vb) & mb);
  108. XPutPixel (image, x, y, pix);
  109. p += dp;
  110. }
  111. }
  112. png_destroy_read_struct (&png_ptr, &png_info, 0);
  113. fclose (F);
  114. return image;
  115. }
  116. }