Skip to content

Commit

Permalink
Image flip Raw data vertically (#2369)
Browse files Browse the repository at this point in the history
* add new method

* specify only RGBA8888 is supported

* add return if wrong pixel format

* fix indent

* Use AXASSERT

* Add back check for release

* format using clang-format

---------

Co-authored-by: Alexandre Konieczny <[email protected]>
  • Loading branch information
AlexandreK38 and Alexandre Konieczny authored Feb 7, 2025
1 parent fb850fd commit 0656f87
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
29 changes: 29 additions & 0 deletions core/platform/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,35 @@ bool Image::initWithRawData(const uint8_t* data,
return ret;
}

void Image::flipRawData()
{
AXASSERT(_pixelFormat == backend::PixelFormat::RGBA8, "only RGBA8888 can be flipped");
if (_pixelFormat != backend::PixelFormat::RGBA8)
{
AXLOGE("Cannot flip image. Unsupported pixel format.");
return;
}

uint8_t temp;
int idx1, idx2;

for (int w = 0; w < _width; w++)
{
for (int h = 0; h < _height / 2; h++)
{
idx1 = (h * _width + w) * 4;
idx2 = ((_height - h - 1) * _width + w) * 4;

for (int c = 0; c < 4; c++)
{
temp = _data[idx1 + c];
_data[idx1 + c] = _data[idx2 + c];
_data[idx2 + c] = temp;
}
}
}
}

bool Image::isPng(const uint8_t* data, ssize_t dataLen)
{
if (dataLen <= 8)
Expand Down
3 changes: 3 additions & 0 deletions core/platform/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ class AX_DLL Image : public Object
int bitsPerComponent,
bool preMulti = false);

// only RGBA8888 supported
void flipRawData();

// Getters
uint8_t* getData() { return _data + _offset; }
ssize_t getDataLen() { return _dataLen - _offset; }
Expand Down

0 comments on commit 0656f87

Please sign in to comment.