294 lines
7.4 KiB
C++
294 lines
7.4 KiB
C++
#include "GraphicImage.h"
|
||
|
||
//#include "opencv2/opencv.hpp"
|
||
|
||
GraphicImage::GraphicImage(int width, int height)
|
||
{
|
||
m_mapping.scaling = MAP_OPER_SCALING(m_scale);
|
||
resize(width, height);
|
||
|
||
// cv::Mat mat(600,600, 3);
|
||
// cv::resize(mat, mat, cv::Size(400, 300));
|
||
// cv::imwrite("", mat);
|
||
// cv::line(mat, cv::Point(0,0), cv::Point(100,100), cv::Scalar(0,0,0));
|
||
// cv::putText(mat, "desc" ,cv::Point(10, 40),1,2,cv::Scalar(0,0,0),2);
|
||
}
|
||
|
||
GraphicImage::~GraphicImage()
|
||
{
|
||
__DELETE__(m_img);
|
||
__FREE__(m_rgb);
|
||
}
|
||
|
||
void GraphicImage::recreating()
|
||
{
|
||
if(m_img)
|
||
{
|
||
__DELETE__(m_img);
|
||
}
|
||
if(m_rgb)
|
||
{
|
||
__FREE__(m_rgb);
|
||
}
|
||
m_img = __NEW__(CxxImage, m_mapping.width, m_mapping.height, m_depth, m_channel, m_default);
|
||
int siz = size();
|
||
m_rgb = (unsigned char*)__MALLOC__(siz);
|
||
memset(m_rgb, m_default, siz);
|
||
}
|
||
|
||
void GraphicImage::resize(int w, int h)
|
||
{
|
||
m_mapping.width = w;
|
||
m_mapping.height = h;
|
||
recreating();
|
||
}
|
||
|
||
void GraphicImage::setScaling(int s)
|
||
{
|
||
m_scale = s;
|
||
m_mapping.scaling = MAP_OPER_SCALING(m_scale);
|
||
}
|
||
|
||
int GraphicImage::scale()
|
||
{
|
||
return m_scale;
|
||
}
|
||
|
||
double GraphicImage::scaling()
|
||
{
|
||
return m_mapping.scaling;
|
||
}
|
||
|
||
int GraphicImage::width()
|
||
{
|
||
return m_mapping.width;
|
||
}
|
||
|
||
int GraphicImage::height()
|
||
{
|
||
return m_mapping.height;
|
||
}
|
||
|
||
int GraphicImage::depth()
|
||
{
|
||
return m_depth;
|
||
}
|
||
|
||
int GraphicImage::channel()
|
||
{
|
||
return m_channel; //m_img->spectrum();
|
||
}
|
||
|
||
const TMapping& GraphicImage::mapping()
|
||
{
|
||
return m_mapping;
|
||
}
|
||
|
||
int GraphicImage::size()
|
||
{
|
||
return m_img ? m_img->size() : 0;
|
||
}
|
||
|
||
const char* GraphicImage::data()
|
||
{
|
||
return m_img ? (const char*)m_img->data() : nullptr;
|
||
}
|
||
|
||
void GraphicImage::clean()
|
||
{
|
||
if(m_img)
|
||
{
|
||
SafeMemset(m_img->data(), m_default, m_img->size());
|
||
}
|
||
}
|
||
|
||
const char* GraphicImage::image()
|
||
{
|
||
//for-surface-test
|
||
#if 0
|
||
HELP_COST_TIME("");
|
||
clean();
|
||
int64 tick = Tools::nowTime();
|
||
std::string tm = Tools::toTimeString(tick/SECOND) + "." + std::to_string(tick%SECOND);
|
||
drawText(Pointi(10,10), tm);
|
||
drawText(Pointi(30,30), std::string("duolun judge draw..."));
|
||
|
||
for(int i = 10; i < 400; i++)
|
||
{
|
||
drawLine(Pointi(120,55), Pointi(600,i));
|
||
}
|
||
|
||
drawLine(Pointi(50,50), Pointi(50,100));
|
||
drawLine(Pointi(50,50), Pointi(100,50));
|
||
drawLine(Pointi(100,100), Pointi(50,100));
|
||
drawLine(Pointi(100,100), Pointi(100,50));
|
||
|
||
drawLine(Pointi(200,200), Pointi(400,200), RGB_PURPLE);
|
||
drawLine(Pointi(200,200), Pointi(200,300), RGB_PURPLE);
|
||
drawLine(Pointi(300,400), Pointi(400,200), RGB_PURPLE);
|
||
drawLine(Pointi(300,400), Pointi(200,300), RGB_PURPLE);
|
||
|
||
drawDashedLine(Pointi(10,150), Pointi(300,150), 10, RGB_ORANGE);
|
||
drawDashedLine(Pointi(10,180), Pointi(300,180), 10, RGB_GREEN);
|
||
|
||
drawPoint(Pointi(500,420), RGB_RED, 5);
|
||
drawText(Pointi(500,425), std::string("P1"));
|
||
#endif
|
||
|
||
int offset_y = 1;
|
||
if(m_showTime)
|
||
{
|
||
int64 tick = Tools::nowTime();
|
||
std::string tm = Tools::toTimeString(tick/SECOND) + "." + std::to_string(tick%SECOND);
|
||
drawText(Pointi(1, offset_y), tm.c_str(), RGB_PERILLA, 16);
|
||
offset_y += 16;
|
||
}
|
||
//if(m_showVersion)
|
||
//{
|
||
// drawText(Pointi(1, offset_y), JUDGE_VERSION_INFO, RGB_BLUE, 16);
|
||
// offset_y += 16;
|
||
//}
|
||
|
||
toRgb();
|
||
return (const char*)m_rgb;
|
||
}
|
||
|
||
void GraphicImage::display()
|
||
{
|
||
#if defined(cimg_display) && (cimg_display > 0)
|
||
if(m_img != nullptr) m_img->display();
|
||
#endif
|
||
}
|
||
|
||
void GraphicImage::drawLine(const Pointi& p1, const Pointi& p2, const RgbColor& color, int thickness)
|
||
{
|
||
if(m_img != nullptr)
|
||
{
|
||
const unsigned char c[] = { color.R, color.G, color.B, color.A };
|
||
m_img->draw_line(p1.x, p1.y, p2.x, p2.y, c, 1, ~0U, true);
|
||
}
|
||
}
|
||
|
||
void GraphicImage::drawLine(const Linei& line, const RgbColor& color, int thickness)
|
||
{
|
||
drawLine(line.p1, line.p2, color, thickness);
|
||
}
|
||
|
||
void GraphicImage::drawDashedLine(const Pointi& p1, const Pointi& p2, int divide, const RgbColor& color, int thickness)
|
||
{
|
||
//double a = angle(p1.x, p1.y, p2.x, p2.y);
|
||
|
||
int dx = p2.x - p1.x;
|
||
int dy = p2.y - p1.y;
|
||
double d = std::sqrt(dx*dx + dy*dy);
|
||
|
||
double kx = double(p2.x - p1.x) / d;
|
||
double ky = double(p2.y - p1.y) / d;
|
||
|
||
if(divide % 2 == 0) divide++;
|
||
double t = d / divide;
|
||
|
||
for(int i = 0; i < divide; i += 2)
|
||
{
|
||
int x1 = p1.x + kx * t * i;
|
||
int y1 = p1.y + ky * t * i;
|
||
int x2 = p1.x + kx * t * (i+1);
|
||
int y2 = p1.y + ky * t * (i+1);
|
||
drawLine({x1, y1}, {x2, y2}, color, thickness);
|
||
}
|
||
}
|
||
|
||
void GraphicImage::drawDashedLine(const Linei& line, int divide, const RgbColor& color, int thickness)
|
||
{
|
||
drawDashedLine(line.p1, line.p2, divide, color, thickness);
|
||
}
|
||
|
||
void GraphicImage::drawDottedLine(const Pointi& p1, const Pointi& p2, int divide, const RgbColor& color, int radius)
|
||
{
|
||
//double a = angle(p1.x, p1.y, p2.x, p2.y);
|
||
|
||
int dx = p2.x - p1.x;
|
||
int dy = p2.y - p1.y;
|
||
double d = std::sqrt(dx*dx + dy*dy);
|
||
|
||
double kx = double(p2.x - p1.x) / d;
|
||
double ky = double(p2.y - p1.y) / d;
|
||
|
||
if(divide % 2 == 0) divide++;
|
||
double t = d / divide;
|
||
|
||
for(int i = 0; i < divide; i += 2)
|
||
{
|
||
int x1 = p1.x + kx * t * i;
|
||
int y1 = p1.y + ky * t * i;
|
||
int x2 = p1.x + kx * t * (i+1);
|
||
int y2 = p1.y + ky * t * (i+1);
|
||
drawPoint({x1, y1}, color, radius);
|
||
drawPoint({x2, y2}, color, radius);
|
||
}
|
||
}
|
||
|
||
void GraphicImage::drawDottedLine(const Linei& line, int divide, const RgbColor& color, int radius)
|
||
{
|
||
drawDottedLine(line.p1, line.p2, divide, color, radius);
|
||
}
|
||
|
||
void GraphicImage::drawText(const Pointi& p, const std::string& text, const RgbColor& color, int size)
|
||
{
|
||
if(m_img != nullptr && !text.empty())
|
||
{
|
||
const unsigned char c[] = { color.R, color.G, color.B, color.A };
|
||
m_img->draw_text(p.x, p.y, text.c_str(), c, 0, 1, size);
|
||
}
|
||
}
|
||
|
||
void GraphicImage::drawText2(const Pointi& p, const std::string& text, const RgbColor& color, const RgbColor& bg, int size)
|
||
{
|
||
if(m_img != nullptr && !text.empty())
|
||
{
|
||
const unsigned char c[] = { color.R, color.G, color.B, color.A };
|
||
const unsigned char b[] = { bg.R, bg.G, bg.B, bg.A };
|
||
m_img->draw_text(p.x, p.y, text.c_str(), c, b, 1, size);
|
||
}
|
||
}
|
||
|
||
void GraphicImage::drawPoint(const Pointi& p, const RgbColor& color, int radius)
|
||
{
|
||
if(m_img != nullptr)
|
||
{
|
||
const unsigned char c[] = { color.R, color.G, color.B, color.A };
|
||
//m_img->draw_point(p.x, p.y, c);
|
||
m_img->draw_circle(p.x, p.y, radius, c, 1);
|
||
//m_img->draw_circle(p.x, p.y, radius, c, 1, ~0U);
|
||
//m_img->draw_ellipse(p.x, p.y, radius,radius, 1,c);
|
||
}
|
||
}
|
||
|
||
void GraphicImage::toRgb()
|
||
{
|
||
HELP_COST_TIME("");
|
||
if(m_img != nullptr)
|
||
{
|
||
const unsigned char* ptrR = m_img->data(0,0,0,0);
|
||
const unsigned char* ptrG = m_img->data(0,0,0,1);
|
||
const unsigned char* ptrB = m_img->data(0,0,0,2);
|
||
const unsigned char* ptrA = m_img->data(0,0,0,3);
|
||
int wh = m_img->width() * m_img->height();
|
||
unsigned char* buff = m_rgb;
|
||
// RGB image
|
||
for(int i = 0; i < wh; ++i)
|
||
{
|
||
//*(buff++) = *(ptrR++);
|
||
//*(buff++) = *(ptrG++);
|
||
//*(buff++) = *(ptrB++);
|
||
//*(buff++) = *(ptrA++); //???杨海洋2023-07-10
|
||
buff[0] = *(ptrR++);
|
||
buff[1] = *(ptrG++);
|
||
buff[2] = *(ptrB++);
|
||
buff[3] = *(ptrA++);
|
||
buff += 4;
|
||
}
|
||
}
|
||
}
|
||
|