# Rewrite 5

- Move Libraries Source into pd directory and give them all their own CMakeLists.txt
- Partial rewrite core (color, autogenerated vec), lithium (now uses UNIQUE PTR for Commands), UI7
- Use MenuV2 as new standart in UI7
- Implementz ViewPort Pre alpha to UI7
- Add Line Drawing to DrawList (not Working)
- Implement a Complete new drievrs API (static Drivers)
- NO SUPPORT FOR SHARED LIBRARY BUILDS IN VERSION 5 YET
- Add Tools to Autogenerate Headers and Stuff
This commit is contained in:
2025-06-22 21:05:09 +02:00
parent 963fa72e41
commit 57634cbf4b
184 changed files with 13967 additions and 18453 deletions

158
pd/image/source/image.cpp Executable file
View File

@ -0,0 +1,158 @@
/*
MIT License
Copyright (c) 2024 - 2025 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.
*/
#ifdef PD_IMAGE_BUILD_SHARED
#define STB_IMAGE_IMPLEMENTATION
#endif
#include <pd/external/stb_image.h>
#include <cstring>
#include <memory>
#include <pd/image/image.hpp>
#include <pd/image/img_convert.hpp>
namespace PD {
PD_IMAGE_API void Image::Load(const std::string& path) {
u8* img = stbi_load(path.c_str(), &pWidth, &pHeight, &fmt, 4);
if (fmt == 3) {
stbi_image_free(img);
img = stbi_load(path.c_str(), &pWidth, &pHeight, &fmt, 3);
pBuffer = std::vector<PD::u8>(img, img + (pWidth * pHeight * 3));
pFmt = RGB;
stbi_image_free(img);
} else if (fmt == 4) {
pBuffer = std::vector<PD::u8>(img, img + (pWidth * pHeight * 4));
pFmt = RGBA;
stbi_image_free(img);
}
}
PD_IMAGE_API void Image::Load(const std::vector<u8>& buf) {
u8* img =
stbi_load_from_memory(buf.data(), buf.size(), &pWidth, &pHeight, &fmt, 4);
if (fmt == 3) {
stbi_image_free(img);
img = stbi_load_from_memory(buf.data(), buf.size(), &pWidth, &pHeight, &fmt,
3);
pBuffer = std::vector<PD::u8>(img, img + (pWidth * pHeight * 3));
pFmt = RGB;
stbi_image_free(img);
} else if (fmt == 4) {
pBuffer = std::vector<PD::u8>(img, img + (pWidth * pHeight * 4));
stbi_image_free(img);
pFmt = RGBA;
}
}
PD_IMAGE_API void Image::Copy(const std::vector<u8>& buf, int w, int h,
int bpp) {
this->fmt = bpp;
if (buf.size() != (size_t)w * h * bpp) {
// Size Error
return;
}
this->pBuffer.resize(w * h * bpp);
for (size_t i = 0; i < this->pBuffer.size(); i++) {
pBuffer[i] = buf[i];
}
}
PD_IMAGE_API void Image::Convert(Image::Ref img, Image::Format dst) {
if (img->pFmt == dst) {
return;
} else if (img->pFmt == Image::RGB && dst == Image::BGR) {
ImgConvert::ReverseBuf(img->pBuffer, 3, img->pWidth, img->pHeight);
img->pFmt = BGR;
} else if (img->pFmt == Image::RGB && dst == Image::RGBA) {
std::vector<PD::u8> cpy = img->pBuffer;
img->pBuffer.resize(img->pWidth * img->pHeight * 4);
ImgConvert::RGB24toRGBA32(img->pBuffer, cpy, img->pWidth, img->pHeight);
img->pFmt = RGBA;
} else if (img->pFmt == Image::RGBA && dst == Image::RGB) {
std::vector<PD::u8> cpy = img->pBuffer;
img->pBuffer.resize(img->pWidth * img->pHeight * 3);
ImgConvert::RGB32toRGBA24(img->pBuffer, cpy, img->pWidth, img->pHeight);
img->pFmt = RGB;
} else if (img->pFmt == Image::RGBA && dst == Image::RGB565) {
Convert(img, Image::RGB);
Convert(img, Image::RGB565);
} else if (img->pFmt == Image::RGB && dst == Image::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 = img->pBuffer;
img->pBuffer.resize(img->pWidth * img->pHeight * 2);
for (int y = 0; y < img->pWidth; y++) {
for (int x = 0; x < img->pHeight; x++) {
int src = (y * img->pWidth + x) * 3;
int dst = (y * img->pWidth + x) * 2;
u16 new_px = f(cpy[src + 0], cpy[src + 1], cpy[src + 2]);
img->pBuffer[dst + 0] = new_px >> 8;
img->pBuffer[dst + 1] = new_px & 0xff;
}
}
img->pFmt = RGB565;
}
}
PD_IMAGE_API int Image::Fmt2Bpp(Format fmt) {
switch (fmt) {
case RGBA:
case ABGR:
return 4;
break;
case RGB:
case BGR:
return 3;
break;
case RGB565:
return 2;
break;
default:
return 0;
break;
}
}
PD_IMAGE_API void Image::ReTile(Image::Ref img,
std::function<u32(int x, int y, int w)> src,
std::function<u32(int x, int y, int w)> dst) {
std::vector<PD::u8> cpy = img->pBuffer;
/** could use fmt here but for 565 that woulnt work as it is not supported by
* file loading where fmt is used */
int bpp = Fmt2Bpp(img->pFmt);
for (int y = 0; y < img->pHeight; y++) {
for (int x = 0; x < img->pWidth; x++) {
int src_idx = src(x, y, img->pWidth);
int dst_idx = dst(x, y, img->pWidth);
for (int i = 0; i < bpp; i++) {
img->pBuffer[dst_idx + i] = cpy[src_idx + i];
}
}
}
}
} // namespace PD

90
pd/image/source/img_blur.cpp Executable file
View File

@ -0,0 +1,90 @@
/*
MIT License
Copyright (c) 2024 - 2025 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 <cstring>
#include <memory>
#include <pd/image/img_blur.hpp>
#include <pd/image/img_convert.hpp>
namespace PD {
namespace ImgBlur {
PD_IMAGE_API std::vector<float> GaussianKernel(int r, float si) {
/// Define radius as r to be shorter
int size = 2 * r + 1;
std::vector<float> kernel(size);
float sum = 0.0f;
for (int i = -r; i <= r; i++) {
kernel[i + r] = exp(-0.5f * (i * i) / (si * si));
sum += kernel[i + r];
}
for (int i = 0; i < size; i++) {
kernel[i] /= sum;
}
return kernel;
}
PD_IMAGE_API void GaussianBlur(std::vector<u8> &buf, int w, int h, float radius,
float si,
std::function<int(int, int, int)> idxfn) {
GaussianBlur(buf.data(), w, h, 4, radius, si, idxfn);
}
PD_IMAGE_API void GaussianBlur(void *buf, int w, int h, int bpp, float radius,
float si,
std::function<int(int, int, int)> idxfn) {
if (bpp != 4 && bpp != 3) {
return;
}
std::vector<float> kernel = GaussianKernel(radius, si);
int hks = kernel.size() / 2;
int end = w * h * bpp;
std::vector<unsigned char> res((u8 *)buf, ((u8 *)buf) + end);
ImgConvert::Reverse32(res, w, h);
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
float r = 0.0f, g = 0.0f, b = 0.0f;
for (int ky = -hks; ky <= hks; ky++) {
for (int kx = -hks; kx <= hks; kx++) {
int xoff = std::min(std::max(x + kx, 0), w - 1);
int yoff = std::min(std::max(y + ky, 0), h - 1);
int idx = idxfn(xoff, yoff, w) * 4;
float weight = kernel[ky + hks] * kernel[kx + hks];
r += ((u8 *)buf)[idx] * weight;
g += ((u8 *)buf)[idx + 1] * weight;
b += ((u8 *)buf)[idx + 2] * weight;
}
}
int idx = idxfn(x, y, w) * bpp;
res[idx] = std::min(std::max(int(r), 0), 255);
res[idx + 1] = std::min(std::max(int(g), 0), 255);
res[idx + 2] = std::min(std::max(int(b), 0), 255);
}
}
ImgConvert::Reverse32(res, w, h);
std::memcpy(buf, res.data(), res.size());
}
} // namespace ImgBlur
} // namespace PD

137
pd/image/source/img_convert.cpp Executable file
View File

@ -0,0 +1,137 @@
/*
MIT License
Copyright (c) 2024 - 2025 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/img_convert.hpp>
namespace PD::ImgConvert {
PD_IMAGE_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_IMAGE_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_IMAGE_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_IMAGE_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];
}
}
}
}
PD_IMAGE_API void RGB24toRGBA32(PD::Vec<u8> &out, const PD::Vec<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_IMAGE_API void RGB32toRGBA24(PD::Vec<u8> &out, const PD::Vec<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_IMAGE_API void Reverse32(PD::Vec<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_IMAGE_API void ReverseBuf(PD::Vec<u8> &buf, size_t bpp, int w, int h) {
PD::Vec<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