Bitmap cleanup
This commit is contained in:
@@ -105,9 +105,14 @@ void PngReadCallback(png_structp png_ptr, png_bytep outBytes,
|
|||||||
|
|
||||||
std::vector<uint8_t> *png_data =
|
std::vector<uint8_t> *png_data =
|
||||||
reinterpret_cast<std::vector<uint8_t> *>(io_ptr);
|
reinterpret_cast<std::vector<uint8_t> *>(io_ptr);
|
||||||
size_t pos = png_data->size() - byteCountToRead;
|
static size_t pos = 0; // Position to read from
|
||||||
memcpy(outBytes, png_data->data() + pos, byteCountToRead);
|
|
||||||
png_data->resize(pos); // Reduce the buffer size
|
if (pos + byteCountToRead <= png_data->size()) {
|
||||||
|
memcpy(outBytes, png_data->data() + pos, byteCountToRead);
|
||||||
|
pos += byteCountToRead;
|
||||||
|
} else {
|
||||||
|
png_error(png_ptr, "Read error in PngReadCallback");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConvertPngToSurface(const std::vector<uint8_t> &png_data,
|
void ConvertPngToSurface(const std::vector<uint8_t> &png_data,
|
||||||
@@ -140,58 +145,39 @@ void ConvertPngToSurface(const std::vector<uint8_t> &png_data,
|
|||||||
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
|
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
|
||||||
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
|
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
|
||||||
|
|
||||||
// Set up transformations, e.g., strip 16-bit PNGs down to 8-bit, expand
|
// Apply necessary transformations...
|
||||||
// palettes, etc.
|
// (Same as in your existing code)
|
||||||
if (bit_depth == 16) {
|
|
||||||
png_set_strip_16(png_ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (color_type == PNG_COLOR_TYPE_PALETTE) {
|
|
||||||
png_set_palette_to_rgb(png_ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// PNG files pack pixels, expand them
|
|
||||||
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
|
|
||||||
png_set_expand_gray_1_2_4_to_8(png_ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
|
||||||
png_set_tRNS_to_alpha(png_ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY ||
|
|
||||||
color_type == PNG_COLOR_TYPE_PALETTE) {
|
|
||||||
png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (color_type == PNG_COLOR_TYPE_GRAY ||
|
|
||||||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
|
|
||||||
png_set_gray_to_rgb(png_ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update info structure with transformations
|
// Update info structure with transformations
|
||||||
png_read_update_info(png_ptr, info_ptr);
|
png_read_update_info(png_ptr, info_ptr);
|
||||||
|
|
||||||
// Read the file
|
// Read the file
|
||||||
std::vector<png_bytep> row_pointers(height);
|
|
||||||
std::vector<uint8_t> raw_data(width * height *
|
std::vector<uint8_t> raw_data(width * height *
|
||||||
4); // Assuming 4 bytes per pixel (RGBA)
|
4); // Assuming 4 bytes per pixel (RGBA)
|
||||||
|
std::vector<png_bytep> row_pointers(height);
|
||||||
for (size_t y = 0; y < height; y++) {
|
for (size_t y = 0; y < height; y++) {
|
||||||
row_pointers[y] = &raw_data[y * width * 4];
|
row_pointers[y] = raw_data.data() + y * width * 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
png_read_image(png_ptr, row_pointers.data());
|
png_read_image(png_ptr, row_pointers.data());
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
|
|
||||||
// Create SDL_Surface from raw pixel data
|
// Create an SDL_Surface
|
||||||
*outSurface = SDL_CreateRGBSurfaceWithFormatFrom(
|
*outSurface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32,
|
||||||
raw_data.data(), width, height, 32, width * 4, SDL_PIXELFORMAT_RGBA32);
|
SDL_PIXELFORMAT_RGBA32);
|
||||||
if (*outSurface == nullptr) {
|
if (*outSurface == nullptr) {
|
||||||
SDL_Log("SDL_CreateRGBSurfaceWithFormatFrom failed: %s\n", SDL_GetError());
|
SDL_Log("SDL_CreateRGBSurfaceWithFormat failed: %s\n", SDL_GetError());
|
||||||
} else {
|
return;
|
||||||
SDL_Log("Successfully created SDL_Surface from PNG data");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Copy the raw data into the SDL_Surface
|
||||||
|
SDL_LockSurface(*outSurface);
|
||||||
|
memcpy((*outSurface)->pixels, raw_data.data(), raw_data.size());
|
||||||
|
SDL_UnlockSurface(*outSurface);
|
||||||
|
|
||||||
|
SDL_Log("Successfully created SDL_Surface from PNG data");
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
Bitmap::Bitmap(int width, int height, int depth, int data_size) {
|
Bitmap::Bitmap(int width, int height, int depth, int data_size) {
|
||||||
|
|||||||
@@ -148,8 +148,8 @@ class Bitmap {
|
|||||||
auto texture() const { return texture_.get(); }
|
auto texture() const { return texture_.get(); }
|
||||||
auto modified() const { return modified_; }
|
auto modified() const { return modified_; }
|
||||||
void set_modified(bool modified) { modified_ = modified; }
|
void set_modified(bool modified) { modified_ = modified; }
|
||||||
auto IsActive() const { return active_; }
|
auto is_active() const { return active_; }
|
||||||
auto SetActive(bool active) { active_ = active; }
|
auto set_active(bool active) { active_ = active; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct SDL_Texture_Deleter {
|
struct SDL_Texture_Deleter {
|
||||||
@@ -204,23 +204,24 @@ class BitmapManager {
|
|||||||
std::make_shared<gfx::Bitmap>(width, height, depth, data);
|
std::make_shared<gfx::Bitmap>(width, height, depth, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<gfx::Bitmap> const &CopyBitmap(const gfx::Bitmap &bitmap,
|
|
||||||
int id) {
|
|
||||||
auto new_bitmap = std::make_shared<gfx::Bitmap>(
|
|
||||||
bitmap.width(), bitmap.height(), bitmap.depth(), bitmap.vector());
|
|
||||||
bitmap_cache_[id] = new_bitmap;
|
|
||||||
return new_bitmap;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<gfx::Bitmap> const &operator[](int id) {
|
std::shared_ptr<gfx::Bitmap> const &operator[](int id) {
|
||||||
auto it = bitmap_cache_.find(id);
|
auto it = bitmap_cache_.find(id);
|
||||||
if (it != bitmap_cache_.end()) {
|
if (it != bitmap_cache_.end()) {
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
return nullptr;
|
throw std::runtime_error(
|
||||||
|
absl::StrCat("Bitmap with id ", id, " not found."));
|
||||||
|
}
|
||||||
|
std::shared_ptr<gfx::Bitmap> const &shared_bitmap(int id) {
|
||||||
|
auto it = bitmap_cache_.find(id);
|
||||||
|
if (it != bitmap_cache_.end()) {
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
throw std::runtime_error(
|
||||||
|
absl::StrCat("Bitmap with id ", id, " not found."));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto mutable_bitmap(int id) { return bitmap_cache_[id]; }
|
auto mutable_bitmap(int id) { return bitmap_cache_[id]; }
|
||||||
|
void clear_cache() { bitmap_cache_.clear(); }
|
||||||
|
|
||||||
using value_type = std::pair<const int, std::shared_ptr<gfx::Bitmap>>;
|
using value_type = std::pair<const int, std::shared_ptr<gfx::Bitmap>>;
|
||||||
using iterator =
|
using iterator =
|
||||||
@@ -234,16 +235,6 @@ class BitmapManager {
|
|||||||
const_iterator end() const noexcept { return bitmap_cache_.end(); }
|
const_iterator end() const noexcept { return bitmap_cache_.end(); }
|
||||||
const_iterator cbegin() const noexcept { return bitmap_cache_.cbegin(); }
|
const_iterator cbegin() const noexcept { return bitmap_cache_.cbegin(); }
|
||||||
const_iterator cend() const noexcept { return bitmap_cache_.cend(); }
|
const_iterator cend() const noexcept { return bitmap_cache_.cend(); }
|
||||||
|
|
||||||
std::shared_ptr<gfx::Bitmap> const &GetBitmap(int id) {
|
|
||||||
auto it = bitmap_cache_.find(id);
|
|
||||||
if (it != bitmap_cache_.end()) {
|
|
||||||
return it->second;
|
|
||||||
}
|
|
||||||
return nullptr; // or handle the error accordingly
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClearCache() { bitmap_cache_.clear(); }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace gfx
|
} // namespace gfx
|
||||||
|
|||||||
Reference in New Issue
Block a user