Skip to content

Commit

Permalink
ImageData destructor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Nov 12, 2023
1 parent ffd3e3f commit 912bf28
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/graphics/ImageData.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#include "ImageData.h"

#include <assert.h>

inline int min(int a, int b) {
return (a < b) ? a : b;
}

inline int max(int a, int b) {
return (a > b) ? a : b;
}

ImageData::ImageData(ImageFormat format, uint width, uint height, void* data)
: format(format), width(width), height(height), data(data) {
}
Expand All @@ -8,7 +18,37 @@ ImageData::~ImageData() {
switch (format) {
case ImageFormat::rgb888:
case ImageFormat::rgba8888:
delete[] data;
delete[] (ubyte*)data;
break;
}
}

ImageData* add_atlas_margins(ImageData* image, int grid_size) {
// RGBA is only supported
assert(image->getFormat() == ImageFormat::rgba8888);
assert(image->getWidth() == image->getHeight());

int srcwidth = image->getWidth();
int srcheight = image->getHeight();
int dstwidth = srcwidth + grid_size * 2;
int dstheight = srcheight + grid_size * 2;

const ubyte* srcdata = (const ubyte*)image->getData();
ubyte* dstdata = new ubyte[dstwidth*dstheight * 4];

int imgres = image->getWidth() / grid_size;
for (int row = 0; row < grid_size; row++) {
for (int col = 0; col < grid_size; col++) {
int ox = 1 + col * (imgres + 2);
int oy = 1 + row * (imgres + 2);
for (int ly = -1; ly <= imgres; ly++) {
for (int lx = -1; lx <= imgres; lx++) {
int sy = max(min(ly, imgres-1), 0);
int sx = max(min(lx, imgres-1), 0);
for (int c = 0; c < 4; c++)
dstdata[((oy+ly) * dstwidth + ox + lx) * 4 + c] = srcdata[(sy * srcwidth + sx) * 4 + c];
}
}
}
}
}

0 comments on commit 912bf28

Please sign in to comment.