No Description
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.

textureloader.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "textureloader.h"
  24. #include <cstdio>
  25. #include <png.h>
  26. TexturePtr TextureLoader::loadPNG(const unsigned char *fileData, unsigned int fileSize)
  27. {
  28. TexturePtr texture;
  29. FILE *pngFile = fmemopen((void*)fileData, fileSize, "rb");
  30. if(!pngFile)
  31. return texture;
  32. png_byte sig[8];
  33. if(!fread(&sig, 8, 1, pngFile))
  34. return texture;
  35. if(png_sig_cmp(sig, 0, 8))
  36. return texture;
  37. png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  38. if(!png_ptr)
  39. return texture;
  40. png_infop info_ptr = png_create_info_struct(png_ptr);
  41. if(!info_ptr) {
  42. png_destroy_read_struct(&png_ptr, NULL, NULL);
  43. return texture;
  44. }
  45. if(setjmp(png_jmpbuf(png_ptr))) {
  46. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  47. return texture;
  48. }
  49. png_init_io(png_ptr, pngFile);
  50. png_set_sig_bytes(png_ptr, 8);
  51. png_read_info(png_ptr, info_ptr);
  52. int bitDepth = png_get_bit_depth(png_ptr, info_ptr);
  53. int colourType = png_get_color_type(png_ptr, info_ptr);
  54. if(colourType == PNG_COLOR_TYPE_PALETTE)
  55. png_set_palette_to_rgb(png_ptr);
  56. if(bitDepth < 8 && (colourType == PNG_COLOR_TYPE_GRAY || colourType == PNG_COLOR_TYPE_GRAY_ALPHA))
  57. png_set_expand_gray_1_2_4_to_8(png_ptr);
  58. if(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
  59. png_set_tRNS_to_alpha(png_ptr);
  60. if(bitDepth < 8)
  61. png_set_packing(png_ptr);
  62. else if(bitDepth == 16)
  63. png_set_strip_16(png_ptr);
  64. png_read_update_info(png_ptr, info_ptr);
  65. png_uint_32 width, height;
  66. png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colourType, NULL, NULL, NULL);
  67. int components;
  68. switch(colourType)
  69. {
  70. case PNG_COLOR_TYPE_GRAY:
  71. components = 1;
  72. break;
  73. case PNG_COLOR_TYPE_GRAY_ALPHA:
  74. components = 2;
  75. break;
  76. case PNG_COLOR_TYPE_RGB:
  77. components = 3;
  78. break;
  79. case PNG_COLOR_TYPE_RGB_ALPHA:
  80. components = 4;
  81. break;
  82. default:
  83. if(png_ptr)
  84. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  85. return texture;
  86. };
  87. unsigned char *pixels = new unsigned char[width * height * components];
  88. png_bytep *row_pointers = new png_bytep[height];
  89. for(unsigned int i = 0; i < height; ++i)
  90. row_pointers[i] = (png_bytep)(pixels + (i * width * components));
  91. png_read_image(png_ptr, row_pointers);
  92. png_read_end(png_ptr, NULL);
  93. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  94. fclose(pngFile);
  95. delete[] row_pointers;
  96. texture = TexturePtr(new Texture(width, height, components, pixels));
  97. delete[] pixels;
  98. return texture;
  99. }