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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "texture.h"
  25. #include <png.h>
  26. struct File
  27. {
  28. File() {
  29. offset = 0;
  30. }
  31. unsigned char *data;
  32. unsigned int offset;
  33. };
  34. void png_read_from_mem(png_structp png_ptr, png_bytep data, png_size_t size)
  35. {
  36. File *file = (File*)png_get_io_ptr(png_ptr);
  37. /* Copy data from image buffer */
  38. memcpy(data, file->data + file->offset, size);
  39. /* Advance in the file */
  40. file->offset += size;
  41. }
  42. Texture *TextureLoader::loadPNG(unsigned char *fileData)
  43. {
  44. File file;
  45. file.data = fileData;
  46. if(png_sig_cmp(file.data, 0, 8))
  47. return NULL;
  48. png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  49. if(!png_ptr)
  50. return NULL;
  51. png_infop info_ptr = png_create_info_struct(png_ptr);
  52. if(!info_ptr) {
  53. png_destroy_read_struct(&png_ptr, NULL, NULL);
  54. return NULL;
  55. }
  56. if(setjmp(png_jmpbuf(png_ptr))) {
  57. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  58. return NULL;
  59. }
  60. // Set "png_read" callback function and give source of data
  61. png_set_read_fn(png_ptr, (png_voidp *)&file, png_read_from_mem);
  62. png_read_info(png_ptr, info_ptr);
  63. int bitDepth = png_get_bit_depth(png_ptr, info_ptr);
  64. int colourType = png_get_color_type(png_ptr, info_ptr);
  65. if(colourType == PNG_COLOR_TYPE_PALETTE)
  66. png_set_palette_to_rgb(png_ptr);
  67. if(bitDepth < 8 && (colourType == PNG_COLOR_TYPE_GRAY || colourType == PNG_COLOR_TYPE_GRAY_ALPHA))
  68. png_set_expand_gray_1_2_4_to_8(png_ptr);
  69. if(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
  70. png_set_tRNS_to_alpha(png_ptr);
  71. if(bitDepth < 8)
  72. png_set_packing(png_ptr);
  73. else if(bitDepth == 16)
  74. png_set_strip_16(png_ptr);
  75. png_read_update_info(png_ptr, info_ptr);
  76. png_uint_32 width, height;
  77. png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colourType, NULL, NULL, NULL);
  78. int components;
  79. switch(colourType)
  80. {
  81. case PNG_COLOR_TYPE_GRAY:
  82. components = 1;
  83. break;
  84. case PNG_COLOR_TYPE_GRAY_ALPHA:
  85. components = 2;
  86. break;
  87. case PNG_COLOR_TYPE_RGB:
  88. components = 3;
  89. break;
  90. case PNG_COLOR_TYPE_RGB_ALPHA:
  91. components = 4;
  92. break;
  93. default:
  94. if(png_ptr)
  95. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  96. return NULL;
  97. };
  98. unsigned char *pixels = new unsigned char[width * height * components];
  99. png_bytep *row_pointers = new png_bytep[height];
  100. for(unsigned int i = 0; i < height; ++i)
  101. row_pointers[i] = (png_bytep)(pixels + (i * width * components));
  102. png_read_image(png_ptr, row_pointers);
  103. png_read_end(png_ptr, NULL);
  104. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  105. delete[] row_pointers;
  106. Texture *texture = new Texture(width, height, components, pixels);
  107. delete[] pixels;
  108. return texture;
  109. }