implement Image::Convert and Image::Flip
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2026 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/image/convert.hpp>
|
||||
|
||||
namespace PD::ImgConvert {
|
||||
|
||||
PD_API void RGB24toRGBA32(std::vector<u8>& out, const std::vector<u8>& in,
|
||||
const int& w, const int& h) {
|
||||
// Converts RGB24 to RGBA32
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
int src = (y * w + x) * 3;
|
||||
int dst = (y * w + x) * 4;
|
||||
out[dst + 0] = in[src + 0];
|
||||
out[dst + 1] = in[src + 1];
|
||||
out[dst + 2] = in[src + 2];
|
||||
out[dst + 3] = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_API void RGB32toRGBA24(std::vector<u8>& out, const std::vector<u8>& in,
|
||||
const int& w, const int& h) {
|
||||
// Converts RGB24 to RGBA32
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
int src = (y * w + x) * 4;
|
||||
int dst = (y * w + x) * 3;
|
||||
out[dst + 0] = in[src + 0];
|
||||
out[dst + 1] = in[src + 1];
|
||||
out[dst + 2] = in[src + 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_API void Reverse32(std::vector<u8>& buf, const int& w, const int& h) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
for (int y = 0; y < h; y++) {
|
||||
int i = y * w + x;
|
||||
u8 t0 = buf[i + 0];
|
||||
u8 t1 = buf[i + 1];
|
||||
buf[i + 0] = buf[i + 3];
|
||||
buf[i + 1] = buf[i + 2];
|
||||
buf[i + 3] = t0;
|
||||
buf[i + 2] = t1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_API void ReverseBuf(std::vector<u8>& buf, size_t bpp, int w, int h) {
|
||||
std::vector<u8> cpy = buf;
|
||||
for (int x = 0; x < w; x++) {
|
||||
for (int y = 0; y < h; y++) {
|
||||
int pos = (y * w + x) * bpp;
|
||||
for (size_t i = 0; i < bpp; i++) {
|
||||
buf[pos + bpp - 1 - i] = cpy[pos + i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace PD::ImgConvert
|
||||
+75
-1
@@ -1,3 +1,4 @@
|
||||
#include <pd/image/convert.hpp>
|
||||
#include <pd/image/image.hpp>
|
||||
|
||||
#if defined(PD_INCLUDE_STB_IMAGE)
|
||||
@@ -54,6 +55,53 @@ PD_API void Image::Copy(const std::vector<u8>& pixels, int w, int h, int bpp) {
|
||||
pFormat = GuessFmtFromBpp(bpp);
|
||||
}
|
||||
|
||||
PD_API void Image::Convert(Format dst) {
|
||||
if (pFormat == dst) {
|
||||
return;
|
||||
} else if (pFormat == Format::RGB && dst == Format::BGR) {
|
||||
ImgConvert::ReverseBuf(pData, 3, pSize.x, pSize.y);
|
||||
pFormat = Format::BGR;
|
||||
} else if (pFormat == Format::RGB && dst == Format::RGBA) {
|
||||
std::vector<PD::u8> cpy = pData;
|
||||
pData.resize(pSize.x * pSize.y * 4);
|
||||
ImgConvert::RGB24toRGBA32(pData, cpy, pSize.x, pSize.y);
|
||||
pFormat = Format::RGBA;
|
||||
} else if (pFormat == Format::RGBA && dst == Format::RGB) {
|
||||
std::vector<PD::u8> cpy = pData;
|
||||
pData.resize(pSize.x * pSize.y * 3);
|
||||
ImgConvert::RGB32toRGBA24(pData, cpy, pSize.x, pSize.y);
|
||||
pFormat = Format::RGB;
|
||||
} else if (pFormat == Format::RGBA && dst == Format::BGRA) {
|
||||
for (int i = 0; i < (pSize.x * pSize.y * 4); i += 4) {
|
||||
u8 _tmp = pData[i + 0];
|
||||
pData[i + 0] = pData[i + 2];
|
||||
pData[i + 2] = _tmp;
|
||||
}
|
||||
} else if (pFormat == Format::RGBA && dst == Format::RGB565) {
|
||||
Convert(Format::RGB);
|
||||
Convert(Format::RGB565);
|
||||
} else if (pFormat == Format::RGB && dst == Format::RGB565) {
|
||||
auto f = [](u8 r, u8 g, u8 b) -> u16 {
|
||||
u16 _r = (r >> 3);
|
||||
u16 _g = (g >> 2);
|
||||
u16 _b = (b >> 3);
|
||||
return (_r << 11) | (_g << 5) | _b;
|
||||
};
|
||||
std::vector<PD::u8> cpy = pData;
|
||||
pData.resize(pSize.x * pSize.y * 2);
|
||||
for (int y = 0; y < pSize.x; y++) {
|
||||
for (int x = 0; x < pSize.y; x++) {
|
||||
int src = (y * pSize.x + x) * 3;
|
||||
int dst = (y * pSize.x + x) * 2;
|
||||
u16 new_px = f(cpy[src + 0], cpy[src + 1], cpy[src + 2]);
|
||||
pData[dst + 0] = new_px >> 8;
|
||||
pData[dst + 1] = new_px & 0xff;
|
||||
}
|
||||
}
|
||||
pFormat = Format::RGB565;
|
||||
}
|
||||
}
|
||||
|
||||
PD_API int Image::Format2Bpp(Format fmt) {
|
||||
switch (fmt) {
|
||||
case Format::RGBA:
|
||||
@@ -84,7 +132,33 @@ PD_API Image::Format Image::GuessFmtFromBpp(int bpp) {
|
||||
}
|
||||
|
||||
PD_API void Image::Flip(bool hz, bool vt) {
|
||||
// TODO
|
||||
auto bpp = Format2Bpp(pFormat);
|
||||
int rlen = pSize.x * bpp; // calculate as less as possible
|
||||
if (hz) {
|
||||
for (int j = 0; j < pSize.y; j++) {
|
||||
int roff = j * rlen;
|
||||
for (int i = 0; i < pSize.x / 2; i++) {
|
||||
int src = roff + (i * bpp);
|
||||
int dst = roff + (pSize.x - 1 - i) * bpp;
|
||||
for (int k = 0; k < bpp; k++) {
|
||||
PD::u8 tmp = pData[dst + k];
|
||||
pData[dst + k] = pData[src + k];
|
||||
pData[src + k] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (vt) {
|
||||
for (int j = 0; j < pSize.y / 2; j++) {
|
||||
int rsrc = j * rlen;
|
||||
int rdst = (pSize.y - 1 - j) * rlen;
|
||||
for (int i = 0; i < rlen; i++) { // swap the entire row
|
||||
PD::u8 tmp = pData[rdst + i];
|
||||
pData[rdst + i] = pData[rsrc + i];
|
||||
pData[rsrc + i] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace PD
|
||||
Reference in New Issue
Block a user