Bitmap cleanup

This commit is contained in:
scawful
2024-01-10 23:13:33 -05:00
parent 2a69f7f42a
commit 002fb626c4
2 changed files with 38 additions and 61 deletions

View File

@@ -105,9 +105,14 @@ void PngReadCallback(png_structp png_ptr, png_bytep outBytes,
std::vector<uint8_t> *png_data =
reinterpret_cast<std::vector<uint8_t> *>(io_ptr);
size_t pos = png_data->size() - byteCountToRead;
memcpy(outBytes, png_data->data() + pos, byteCountToRead);
png_data->resize(pos); // Reduce the buffer size
static size_t pos = 0; // Position to read from
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,
@@ -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 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
// Set up transformations, e.g., strip 16-bit PNGs down to 8-bit, expand
// palettes, etc.
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);
}
// Apply necessary transformations...
// (Same as in your existing code)
// Update info structure with transformations
png_read_update_info(png_ptr, info_ptr);
// Read the file
std::vector<png_bytep> row_pointers(height);
std::vector<uint8_t> raw_data(width * height *
4); // Assuming 4 bytes per pixel (RGBA)
std::vector<png_bytep> row_pointers(height);
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_destroy_read_struct(&png_ptr, &info_ptr, NULL);
// Create SDL_Surface from raw pixel data
*outSurface = SDL_CreateRGBSurfaceWithFormatFrom(
raw_data.data(), width, height, 32, width * 4, SDL_PIXELFORMAT_RGBA32);
// Create an SDL_Surface
*outSurface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32,
SDL_PIXELFORMAT_RGBA32);
if (*outSurface == nullptr) {
SDL_Log("SDL_CreateRGBSurfaceWithFormatFrom failed: %s\n", SDL_GetError());
} else {
SDL_Log("Successfully created SDL_Surface from PNG data");
SDL_Log("SDL_CreateRGBSurfaceWithFormat failed: %s\n", SDL_GetError());
return;
}
// 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
Bitmap::Bitmap(int width, int height, int depth, int data_size) {

View File

@@ -148,8 +148,8 @@ class Bitmap {
auto texture() const { return texture_.get(); }
auto modified() const { return modified_; }
void set_modified(bool modified) { modified_ = modified; }
auto IsActive() const { return active_; }
auto SetActive(bool active) { active_ = active; }
auto is_active() const { return active_; }
auto set_active(bool active) { active_ = active; }
private:
struct SDL_Texture_Deleter {
@@ -204,23 +204,24 @@ class BitmapManager {
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) {
auto it = bitmap_cache_.find(id);
if (it != bitmap_cache_.end()) {
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]; }
void clear_cache() { bitmap_cache_.clear(); }
using value_type = std::pair<const int, std::shared_ptr<gfx::Bitmap>>;
using iterator =
@@ -234,16 +235,6 @@ class BitmapManager {
const_iterator end() const noexcept { return bitmap_cache_.end(); }
const_iterator cbegin() const noexcept { return bitmap_cache_.cbegin(); }
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