resource_saver_png.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*************************************************************************/
  2. /* resource_saver_png.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "resource_saver_png.h"
  31. #include "core/image.h"
  32. #include "os/file_access.h"
  33. #include "project_settings.h"
  34. #include "scene/resources/texture.h"
  35. #include <png.h>
  36. static void _write_png_data(png_structp png_ptr, png_bytep data, png_size_t p_length) {
  37. FileAccess *f = (FileAccess *)png_get_io_ptr(png_ptr);
  38. f->store_buffer((const uint8_t *)data, p_length);
  39. }
  40. Error ResourceSaverPNG::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  41. Ref<ImageTexture> texture = p_resource;
  42. ERR_FAIL_COND_V(!texture.is_valid(), ERR_INVALID_PARAMETER);
  43. ERR_EXPLAIN("Can't save empty texture as PNG");
  44. ERR_FAIL_COND_V(!texture->get_width() || !texture->get_height(), ERR_INVALID_PARAMETER);
  45. Ref<Image> img = texture->get_data();
  46. Error err = save_image(p_path, img);
  47. if (err == OK) {
  48. }
  49. return err;
  50. };
  51. Error ResourceSaverPNG::save_image(const String &p_path, const Ref<Image> &p_img) {
  52. Ref<Image> img = p_img->duplicate();
  53. if (img->is_compressed())
  54. img->decompress();
  55. ERR_FAIL_COND_V(img->is_compressed(), ERR_INVALID_PARAMETER);
  56. png_structp png_ptr;
  57. png_infop info_ptr;
  58. png_bytep *row_pointers;
  59. /* initialize stuff */
  60. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  61. ERR_FAIL_COND_V(!png_ptr, ERR_CANT_CREATE);
  62. info_ptr = png_create_info_struct(png_ptr);
  63. ERR_FAIL_COND_V(!info_ptr, ERR_CANT_CREATE);
  64. if (setjmp(png_jmpbuf(png_ptr))) {
  65. ERR_FAIL_V(ERR_CANT_OPEN);
  66. }
  67. //change this
  68. Error err;
  69. FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  70. if (err) {
  71. ERR_FAIL_V(err);
  72. }
  73. png_set_write_fn(png_ptr, f, _write_png_data, NULL);
  74. /* write header */
  75. if (setjmp(png_jmpbuf(png_ptr))) {
  76. ERR_FAIL_V(ERR_CANT_OPEN);
  77. }
  78. int pngf = 0;
  79. int cs = 0;
  80. switch (img->get_format()) {
  81. case Image::FORMAT_L8: {
  82. pngf = PNG_COLOR_TYPE_GRAY;
  83. cs = 1;
  84. } break;
  85. case Image::FORMAT_LA8: {
  86. pngf = PNG_COLOR_TYPE_GRAY_ALPHA;
  87. cs = 2;
  88. } break;
  89. case Image::FORMAT_RGB8: {
  90. pngf = PNG_COLOR_TYPE_RGB;
  91. cs = 3;
  92. } break;
  93. case Image::FORMAT_RGBA8: {
  94. pngf = PNG_COLOR_TYPE_RGB_ALPHA;
  95. cs = 4;
  96. } break;
  97. default: {
  98. if (img->detect_alpha()) {
  99. img->convert(Image::FORMAT_RGBA8);
  100. pngf = PNG_COLOR_TYPE_RGB_ALPHA;
  101. cs = 4;
  102. } else {
  103. img->convert(Image::FORMAT_RGB8);
  104. pngf = PNG_COLOR_TYPE_RGB;
  105. cs = 3;
  106. }
  107. }
  108. }
  109. int w = img->get_width();
  110. int h = img->get_height();
  111. png_set_IHDR(png_ptr, info_ptr, w, h,
  112. 8, pngf, PNG_INTERLACE_NONE,
  113. PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  114. png_write_info(png_ptr, info_ptr);
  115. /* write bytes */
  116. if (setjmp(png_jmpbuf(png_ptr))) {
  117. memdelete(f);
  118. ERR_FAIL_V(ERR_CANT_OPEN);
  119. }
  120. PoolVector<uint8_t>::Read r = img->get_data().read();
  121. row_pointers = (png_bytep *)memalloc(sizeof(png_bytep) * h);
  122. for (int i = 0; i < h; i++) {
  123. row_pointers[i] = (png_bytep)&r[i * w * cs];
  124. }
  125. png_write_image(png_ptr, row_pointers);
  126. memfree(row_pointers);
  127. /* end write */
  128. if (setjmp(png_jmpbuf(png_ptr))) {
  129. memdelete(f);
  130. ERR_FAIL_V(ERR_CANT_OPEN);
  131. }
  132. png_write_end(png_ptr, NULL);
  133. memdelete(f);
  134. /* cleanup heap allocation */
  135. png_destroy_write_struct(&png_ptr, &info_ptr);
  136. return OK;
  137. }
  138. bool ResourceSaverPNG::recognize(const RES &p_resource) const {
  139. return (p_resource.is_valid() && p_resource->is_class("ImageTexture"));
  140. }
  141. void ResourceSaverPNG::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  142. if (Object::cast_to<Texture>(*p_resource)) {
  143. p_extensions->push_back("png");
  144. }
  145. }
  146. ResourceSaverPNG::ResourceSaverPNG() {
  147. Image::save_png_func = &save_image;
  148. };