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.

lice_stb_write.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2021 Jean Pierre Cimalando
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // SPDX-License-Identifier: Apache-2.0
  16. //
  17. #define STBIW_WINDOWS_UTF8
  18. #define STB_IMAGE_WRITE_STATIC
  19. #define STB_IMAGE_WRITE_IMPLEMENTATION
  20. #if defined(__GNUC__)
  21. # pragma GCC diagnostic push
  22. # pragma GCC diagnostic ignored "-Wunused-function"
  23. #endif
  24. #include "stb_image_write.h"
  25. #if defined(__GNUC__)
  26. # pragma GCC diagnostic pop
  27. #endif
  28. //------------------------------------------------------------------------------
  29. #define WDL_NO_DEFINE_MINMAX
  30. #include "WDL/lice/lice.h"
  31. #include "WDL/wdltypes.h"
  32. #include <memory>
  33. static std::unique_ptr<unsigned char[]> bmp_to_stbi(LICE_IBitmap *bmp, unsigned ch)
  34. {
  35. unsigned w = (unsigned)bmp->getWidth();
  36. unsigned h = (unsigned)bmp->getHeight();
  37. unsigned srcspan = (unsigned)bmp->getRowSpan();
  38. bool srcflip = bmp->isFlipped();
  39. LICE_pixel *srcpx = bmp->getBits();
  40. std::unique_ptr<unsigned char[]> result;
  41. if (ch == 4) {
  42. unsigned char *dstpx = new unsigned char[4 * w * h];
  43. result.reset(dstpx);
  44. for (unsigned row = 0; row < h; ++row) {
  45. LICE_pixel *src = srcpx + srcspan * (srcflip ? (h - 1 - row) : row);
  46. unsigned char *dst = dstpx + row * (4 * w);
  47. for (unsigned col = 0; col < w; ++col, ++src, dst += 4) {
  48. LICE_pixel px = *src;
  49. dst[0] = LICE_GETR(px);
  50. dst[1] = LICE_GETG(px);
  51. dst[2] = LICE_GETB(px);
  52. dst[3] = LICE_GETA(px);
  53. }
  54. }
  55. }
  56. else if (ch == 3) {
  57. unsigned char *dstpx = new unsigned char[3 * w * h];
  58. result.reset(dstpx);
  59. for (unsigned row = 0; row < h; ++row) {
  60. LICE_pixel *src = srcpx + srcspan * (srcflip ? (h - 1 - row) : row);
  61. unsigned char *dst = dstpx + row * (3 * w);
  62. for (unsigned col = 0; col < w; ++col, ++src, dst += 3) {
  63. LICE_pixel px = *src;
  64. dst[0] = LICE_GETR(px);
  65. dst[1] = LICE_GETG(px);
  66. dst[2] = LICE_GETB(px);
  67. }
  68. }
  69. }
  70. return result;
  71. }
  72. bool LICE_WritePNG(const char *filename, LICE_IBitmap *bmp, bool wantalpha)
  73. {
  74. unsigned ch = wantalpha ? 4 : 3;
  75. std::unique_ptr<unsigned char[]> data = bmp_to_stbi(bmp, ch);
  76. if (!data)
  77. return false;
  78. return stbi_write_png(filename, bmp->getWidth(), bmp->getHeight(), (int)ch, data.get(), 0);
  79. }
  80. bool LICE_WriteJPG(const char *filename, LICE_IBitmap *bmp, int quality, bool force_baseline)
  81. {
  82. (void)force_baseline; // always baseline
  83. unsigned ch = 4;
  84. std::unique_ptr<unsigned char[]> data = bmp_to_stbi(bmp, ch);
  85. if (!data)
  86. return false;
  87. return stbi_write_jpg(filename, bmp->getWidth(), bmp->getHeight(), (int)ch, data.get(), quality);
  88. }