/
ArtemNech
/
simpleOpenGL
Обзор
Документация
Войти
/
ArtemNech
/
simpleOpenGL
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
devOpt
backend/ogl_sys/CImageFactory.cpp
164 строки
5 KB
Artem Nechitaylenko
develop basket optimization
23 май 2026, 19:57
23 май 2026, 19:57
49e9273
Код
Авторство
О чём код?
// // Created by artem on 22.10.25. // #include "CImageFactory.h" #include <QImage> #include <QSvgRenderer> #include <qdebug.h> #include <QPainter> namespace ngraph { IImageElement CImageFactory::createFromPNG(const std::string& filepath) { ngraph::NSize p_size; auto bytes = loadImageBytes(filepath, p_size); // Создаем элемент с временными координатами (0,0) // Позицию установит объект позже IImageElement element(filepath, bytes, ImageType::STATIC, NRectF({0,0}, p_size.width(), p_size.height())); element.originalPath = filepath; return element; } std::vector<uint8_t> CImageFactory::loadImageBytes(const std::string& filepath, ngraph::NSize & size) { QImage image(QString::fromStdString(filepath)); if (image.isNull()) { // Fallback - красный квадрат return createTestImage(32, 32); } if (image.format() != QImage::Format_RGBA8888) { image = image.convertToFormat(QImage::Format_RGBA8888); } size = ngraph::NSize(image.width(), image.height()); std::vector<uint8_t> bytes(image.sizeInBytes()); memcpy(bytes.data(), image.bits(), image.sizeInBytes()); return bytes; } std::vector<uint8_t> CImageFactory::createTestImage(int width, int height) { std::vector<uint8_t> pixels(width * height * 4); for (int i = 0; i < width * height * 4; i += 4) { pixels[i] = 255; // R pixels[i+1] = 0; // G pixels[i+2] = 0; // B pixels[i+3] = 255; // A } return pixels; } IImageElement CImageFactory::createFromSVG(const std::string &filepath) { QSvgRenderer renderer(QString::fromStdString(filepath)); if (!renderer.isValid()) { qDebug() << "CImageFactory ERROR: Failed to load SVG file:" << QString::fromStdString(filepath); return {}; } QSize p_size = renderer.defaultSize(); if (!p_size.isValid() || p_size.width() <= 0 || p_size.height() <= 0) { qDebug() << "CImageFactory ERROR: Failed to collectVertices SVG file"; return {}; } QImage img(p_size, QImage::Format_RGBA8888); img.fill(Qt::transparent); QPainter painter(&img); if (!painter.isActive()) { qDebug() << "ERROR: Failed to create QPainter"; return {}; } renderer.render(&painter); painter.end(); ngraph::NRectF rect({0,0}, p_size.width(), p_size.height()); std::vector<uint8_t> bytes(img.sizeInBytes()); memcpy(bytes.data(), img.bits(), img.sizeInBytes()); IImageElement element("svg_" + std::to_string(std::hash<std::string>{}(filepath)), bytes, ngraph::ImageType::DYNAMIC, rect); element.originalPath = filepath; return element; } bool CImageFactory::scale_PNG(const double &zoom, IImageElement &element) { auto img_src = QImage(QString::fromStdString(element.originalPath)); if (img_src.isNull()) return false; int n_width = static_cast<int>(element.rect.width() * zoom); if (n_width == img_src.width()) return true; int n_height = static_cast<int>(element.rect.height() * zoom); auto img_scaled = img_src.scaled(n_width, n_height); std::vector<uint8_t> bytes(img_scaled.sizeInBytes()); memcpy(bytes.data(), img_scaled.bits(), img_scaled.sizeInBytes()); element.bytes = bytes; element.rect.set_width(n_width); element.rect.set_height(n_height); return true; } bool CImageFactory::scale_SVG(const double &zoom, IImageElement &element) { QSvgRenderer renderer(QString::fromStdString(element.originalPath)); if (!renderer.isValid()) { qDebug() << "CImageFactory ERROR: Failed to load SVG file:" << QString::fromStdString(element.originalPath); return false; } QSize p_size = renderer.defaultSize(); if (!p_size.isValid() || p_size.width() <= 0 || p_size.height() <= 0) { qDebug() << "CImageFactory ERROR: Failed to collectVertices SVG file"; return false; } QImage img_src(p_size, QImage::Format_RGBA8888); img_src.fill(Qt::transparent); QPainter painter(&img_src); if (!painter.isActive()) { qDebug() << "ERROR: Failed to create QPainter"; return false; } renderer.render(&painter); painter.end(); QImage scaled = img_src.scaled(static_cast<int>(element.rect.width() * zoom), static_cast<int>(element.rect.height() * zoom)); std::vector<uint8_t> bytes(scaled.sizeInBytes()); memcpy(bytes.data(), scaled.bits(), scaled.sizeInBytes()); auto n_w = element.rect.width() * zoom; auto n_h = element.rect.height() * zoom; element.rect.set_width(n_w); element.rect.set_height(n_h); element.bytes = bytes; return true; } } // namespace ngraph