Advertisement
Piratux

Untitled

Jan 31st, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1.  
  2. spr->height = bmp->GetHeight();
  3. spr->pColData = new Pixel[spr->width * spr->height];
  4.  
  5. for (int y = 0; y < spr->height; y++)
  6. for (int x = 0; x < spr->width; x++)
  7. {
  8. Gdiplus::Color c;
  9. bmp->GetPixel(x, y, &c);
  10. spr->SetPixel(x, y, olc::Pixel(c.GetRed(), c.GetGreen(), c.GetBlue(), c.GetAlpha()));
  11. }
  12. delete bmp;
  13. return olc::rcode::OK;
  14. }
  15.  
  16. olc::rcode SaveImageResource(olc::Sprite* spr, const std::string& sImageFile) override
  17. {
  18. return olc::rcode::OK;
  19. }
  20. };
  21. }
  22. #endif
  23. // O------------------------------------------------------------------------------O
  24. // | END IMAGE LOADER: GDI+ |
  25. // O------------------------------------------------------------------------------O
  26.  
  27.  
  28.  
  29.  
  30. // O------------------------------------------------------------------------------O
  31. // | START IMAGE LOADER: libpng, default on linux, requires -lpng (libpng-dev) |
  32. // O------------------------------------------------------------------------------O
  33. #if defined(OLC_IMAGE_LIBPNG)
  34. #include <png.h>
  35. namespace olc
  36. {
  37. void pngReadStream(png_structp pngPtr, png_bytep data, png_size_t length)
  38. {
  39. png_voidp a = png_get_io_ptr(pngPtr);
  40. ((std::istream*)a)->read((char*)data, length);
  41. }
  42.  
  43. class ImageLoader_LibPNG : public olc::ImageLoader
  44. {
  45. public:
  46. ImageLoader_LibPNG() : ImageLoader()
  47. {}
  48.  
  49. olc::rcode LoadImageResource(olc::Sprite* spr, const std::string& sImageFile, olc::ResourcePack* pack) override
  50. {
  51. UNUSED(pack);
  52.  
  53. // clear out existing sprite
  54. if (spr->pColData != nullptr) delete[] spr->pColData;
  55.  
  56. ////////////////////////////////////////////////////////////////////////////
  57. // Use libpng, Thanks to Guillaume Cottenceau
  58. // https://gist.github.com/niw/5963798
  59. // Also reading png from streams
  60. // http://www.piko3d.net/tutorials/libpng-tutorial-loading-png-files-from-streams/
  61. png_structp png;
  62. png_infop info;
  63.  
  64. auto loadPNG = [&]()
  65. {
  66. png_read_info(png, info);
  67. png_byte color_type;
  68. png_byte bit_depth;
  69. png_bytep* row_pointers;
  70. spr->width = png_get_image_width(png, info);
  71. spr->height = png_get_image_height(png, info);
  72. color_type = png_get_color_type(png, info);
  73. bit_depth = png_get_bit_depth(png, info);
  74. if (bit_depth == 16) png_set_strip_16(png);
  75. if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
  76. if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png);
  77. if (png_get_valid(png, info, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png);
  78. if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_PALETTE)
  79. png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
  80. if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  81. png_set_gray_to_rgb(png);
  82. png_read_update_info(png, info);
  83. row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * spr->height);
  84. for (int y = 0; y < spr->height; y++) {
  85. row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png, info));
  86. }
  87. png_read_image(png, row_pointers);
  88. ////////////////////////////////////////////////////////////////////////////
  89. // Create sprite array
  90. spr->pColData = new Pixel[spr->width * spr->height];
  91. // Iterate through image rows, converting into sprite format
  92. for (int y = 0; y < spr->height; y++)
  93. {
  94. png_bytep row = row_pointers[y];
  95. for (int x = 0; x < spr->width; x++)
  96. {
  97. png_bytep px = &(row[x * 4]);
  98. spr->SetPixel(x, y, Pixel(px[0], px[1], px[2], px[3]));
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement