resource_saver_png.cpp 6.7 KB

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