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.

130 lines
3.7KB

  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 AT1 {
  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 r, 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. r = fread (hdr, 8, 1, F );
  42. if ((r != 1) || 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. dx = png_get_image_width (png_ptr, png_info);
  73. dy = png_get_image_height (png_ptr, png_info);
  74. dp = (png_get_color_type (png_ptr, png_info) & PNG_COLOR_MASK_ALPHA) ? 4 : 3;
  75. data = (const unsigned char **)(png_get_rows (png_ptr, png_info));
  76. image = XCreateImage (disp->dpy (),
  77. disp->dvi (),
  78. DefaultDepth (disp->dpy (), disp->dsn ()),
  79. ZPixmap, 0, 0, dx, dy, 32, 0);
  80. image->data = new char [image->height * image->bytes_per_line];
  81. mr = image->red_mask;
  82. mg = image->green_mask;
  83. mb = image->blue_mask;
  84. vr = mr / 255.0f;
  85. vg = mg / 255.0f;
  86. vb = mb / 255.0f;
  87. if (bgnd)
  88. {
  89. br = bgnd->color.red >> 8;
  90. bg = bgnd->color.green >> 8;
  91. bb = bgnd->color.blue >> 8;
  92. }
  93. else br = bg = bb = 0;
  94. for (y = 0; y < dy; y++)
  95. {
  96. p = data [y];
  97. for (x = 0; x < dx; x++)
  98. {
  99. va = (dp == 4) ? (p [3] / 255.0f) : 1;
  100. pix = ((unsigned long)((p [0] * va + (1 - va) * br) * vr) & mr)
  101. | ((unsigned long)((p [1] * va + (1 - va) * bg) * vg) & mg)
  102. | ((unsigned long)((p [2] * va + (1 - va) * bb) * vb) & mb);
  103. XPutPixel (image, x, y, pix);
  104. p += dp;
  105. }
  106. }
  107. png_destroy_read_struct (&png_ptr, &png_info, 0);
  108. fclose (F);
  109. return image;
  110. }
  111. }