Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- spr->height = bmp->GetHeight();
- spr->pColData = new Pixel[spr->width * spr->height];
- for (int y = 0; y < spr->height; y++)
- for (int x = 0; x < spr->width; x++)
- {
- Gdiplus::Color c;
- bmp->GetPixel(x, y, &c);
- spr->SetPixel(x, y, olc::Pixel(c.GetRed(), c.GetGreen(), c.GetBlue(), c.GetAlpha()));
- }
- delete bmp;
- return olc::rcode::OK;
- }
- olc::rcode SaveImageResource(olc::Sprite* spr, const std::string& sImageFile) override
- {
- return olc::rcode::OK;
- }
- };
- }
- #endif
- // O------------------------------------------------------------------------------O
- // | END IMAGE LOADER: GDI+ |
- // O------------------------------------------------------------------------------O
- // O------------------------------------------------------------------------------O
- // | START IMAGE LOADER: libpng, default on linux, requires -lpng (libpng-dev) |
- // O------------------------------------------------------------------------------O
- #if defined(OLC_IMAGE_LIBPNG)
- #include <png.h>
- namespace olc
- {
- void pngReadStream(png_structp pngPtr, png_bytep data, png_size_t length)
- {
- png_voidp a = png_get_io_ptr(pngPtr);
- ((std::istream*)a)->read((char*)data, length);
- }
- class ImageLoader_LibPNG : public olc::ImageLoader
- {
- public:
- ImageLoader_LibPNG() : ImageLoader()
- {}
- olc::rcode LoadImageResource(olc::Sprite* spr, const std::string& sImageFile, olc::ResourcePack* pack) override
- {
- UNUSED(pack);
- // clear out existing sprite
- if (spr->pColData != nullptr) delete[] spr->pColData;
- ////////////////////////////////////////////////////////////////////////////
- // Use libpng, Thanks to Guillaume Cottenceau
- // https://gist.github.com/niw/5963798
- // Also reading png from streams
- // http://www.piko3d.net/tutorials/libpng-tutorial-loading-png-files-from-streams/
- png_structp png;
- png_infop info;
- auto loadPNG = [&]()
- {
- png_read_info(png, info);
- png_byte color_type;
- png_byte bit_depth;
- png_bytep* row_pointers;
- spr->width = png_get_image_width(png, info);
- spr->height = png_get_image_height(png, info);
- color_type = png_get_color_type(png, info);
- bit_depth = png_get_bit_depth(png, info);
- if (bit_depth == 16) png_set_strip_16(png);
- if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
- if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png);
- if (png_get_valid(png, info, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png);
- if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_PALETTE)
- png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
- if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
- png_set_gray_to_rgb(png);
- png_read_update_info(png, info);
- row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * spr->height);
- for (int y = 0; y < spr->height; y++) {
- row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png, info));
- }
- png_read_image(png, row_pointers);
- ////////////////////////////////////////////////////////////////////////////
- // Create sprite array
- spr->pColData = new Pixel[spr->width * spr->height];
- // Iterate through image rows, converting into sprite format
- for (int y = 0; y < spr->height; y++)
- {
- png_bytep row = row_pointers[y];
- for (int x = 0; x < spr->width; x++)
- {
- png_bytep px = &(row[x * 4]);
- spr->SetPixel(x, y, Pixel(px[0], px[1], px[2], px[3]));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement