Added triangle-textures projects
This commit is contained in:
parent
619adc3a0b
commit
f2b4a19de8
BIN
QT/triangle-textures/f.png
Normal file
BIN
QT/triangle-textures/f.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.1 KiB |
33
QT/triangle-textures/main.cpp
Normal file
33
QT/triangle-textures/main.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
// Glowny plik aplikacji, utworzony automatycznie przez QtCreator
|
||||||
|
// W wiekszosci przypadkow nie musimy tu nic zmieniac
|
||||||
|
|
||||||
|
// Dolaczamy plik naglowkowy klasy QApplication
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
// Dolaczamy plik naglowkowy klasy glownego widgetu (okna) aplikacji
|
||||||
|
#include "mywindow.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
|
||||||
|
// Tworzymy objekt QApplication. Zarzadza on zasobami calej aplikacji
|
||||||
|
// i jest niezbedny do stworzenia jakiejkolwiek aplikacji Qt posiadajacej GUI.
|
||||||
|
// Przekazujemy mu dwa argumenty argc i argv, poniewaz Qt moze rowniez
|
||||||
|
// przyjmowac argumenty z linii komend.
|
||||||
|
QApplication a(argc, argv);
|
||||||
|
|
||||||
|
// Tworzymy obiekt klasy MyWindow - glownego okna naszej aplikacji.
|
||||||
|
// Jest to klasa zdefiniowana przez nas.
|
||||||
|
// Jej definicja znajduje sie w plikach mainwindow.h i mainwindow.cpp
|
||||||
|
MyWindow w;
|
||||||
|
|
||||||
|
// w.setMouseTracking(true);
|
||||||
|
// Pokazujemy glowne okno aplikacji na ekranie. Domyslnie jest ono niewidoczne.
|
||||||
|
// Wszystkie widgety (elementy GUI) zawarte w glownym oknie beda rowniez widoczne.
|
||||||
|
w.show();
|
||||||
|
|
||||||
|
// Przekazujemy kontrole nad aplikacja do Qt. Program wchodzi w petle zdarzen
|
||||||
|
// tzn. zaczyna oczekiwac na akcje uzytkownika - klikniecia przycisku myszy,
|
||||||
|
// lub klawisza klawiatury itp.
|
||||||
|
return a.exec();
|
||||||
|
}
|
533
QT/triangle-textures/mywindow.cpp
Normal file
533
QT/triangle-textures/mywindow.cpp
Normal file
@ -0,0 +1,533 @@
|
|||||||
|
// Dolaczamy plik naglowkowy naszej klasy MyWindow
|
||||||
|
#include "mywindow.h"
|
||||||
|
|
||||||
|
// Dolaczamy plik naglowkowy zawierajacy definicje GUI
|
||||||
|
// Plik ten jest generowany automatycznie
|
||||||
|
// z pliku XML "mywindow.ui"
|
||||||
|
#include "ui_mywindow.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <QColor>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QColorDialog>
|
||||||
|
#include <iostream>
|
||||||
|
#include <float.h> // for float,double macros
|
||||||
|
#include <string.h>
|
||||||
|
#include <stack>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#define PI 3.1415
|
||||||
|
|
||||||
|
std::vector<float> matrixMul(std::vector<float> p, std::vector<float> m)
|
||||||
|
{
|
||||||
|
std::vector<float> res(3);
|
||||||
|
for(int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
res.push_back(0);
|
||||||
|
for(int j = 0; j < 3; j++)
|
||||||
|
{
|
||||||
|
res[i] += m[i * 3 + j] * p[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<float> matrixMul3x3(std::vector<float> m1, std::vector<float> m2)
|
||||||
|
{
|
||||||
|
std::vector<float> res(9, 0);
|
||||||
|
for(int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < 3; j++)
|
||||||
|
{
|
||||||
|
for(int k = 0; k < 3; k++)
|
||||||
|
{
|
||||||
|
res[i * 3 + j] += m1[i * 3 + k] * m2[k * 3 + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QColor mul(float v, QColor c){
|
||||||
|
int red = (int)(c.red() * v);
|
||||||
|
if(red > 255) red = 255;
|
||||||
|
int green = (int)(c.green() * v);
|
||||||
|
if(green > 255) green = 255;
|
||||||
|
int blue = (int)(c.blue() * v);
|
||||||
|
if(blue > 255) blue = 255;
|
||||||
|
return QColor(red, green, blue);
|
||||||
|
}
|
||||||
|
|
||||||
|
QColor add(QColor c1, QColor c2){
|
||||||
|
int red = (int)(c1.red() + c2.red());
|
||||||
|
if(red > 255) red = 255;
|
||||||
|
int green = (int)(c1.green() + c2.green());
|
||||||
|
if(green > 255) green = 255;
|
||||||
|
int blue = (int)(c1.blue() + c2.blue());
|
||||||
|
if(blue > 255) blue = 255;
|
||||||
|
return QColor(red, green, blue);
|
||||||
|
}
|
||||||
|
// Definicja konstruktora, wywolujemy najpierw
|
||||||
|
// konstruktor klasy nadrzednej, nastepnie tworzymy
|
||||||
|
// obiekt klasy Ui_MyWindow reprezentujacy GUI
|
||||||
|
MyWindow::MyWindow(QWidget *parent) :
|
||||||
|
QMainWindow(parent),
|
||||||
|
ui(new Ui::MyWindow)
|
||||||
|
{
|
||||||
|
// Wywolujemy funkcje tworzaca elementy GUI
|
||||||
|
// Jej definicja znajduje sie w pliku "ui_mywindow.h"
|
||||||
|
ui->setupUi(this);
|
||||||
|
|
||||||
|
// Pobieramy wymiary i wspolrzedne lewego gornego naroznika ramki
|
||||||
|
// i ustawiamy wartosci odpowiednich pol
|
||||||
|
// Uwaga: ramke "rysujFrame" wykorzystujemy tylko do
|
||||||
|
// wygodnego ustaiwenia tych wymiarow. Rysunek bedziemy wyswietlac
|
||||||
|
// bezposrednio w glownym oknie aplikacji.
|
||||||
|
szer = ui->frame1->width();
|
||||||
|
wys = ui->frame1->height();
|
||||||
|
poczX1 = ui->frame1->x();
|
||||||
|
poczY1 = ui->frame1->y();
|
||||||
|
poczX2 = ui->frame2->x();
|
||||||
|
poczY2 = ui->frame2->y();
|
||||||
|
|
||||||
|
// Tworzymy obiekt klasy QImage, o odpowiedniej szerokosci
|
||||||
|
// i wysokosci. Ustawiamy format bitmapy na 32 bitowe RGB
|
||||||
|
// (0xffRRGGBB).
|
||||||
|
img = new QImage(szer,wys,QImage::Format_RGB32);
|
||||||
|
f1_img = new QImage(szer,wys,QImage::Format_RGB32);
|
||||||
|
f2_img = new QImage(szer,wys,QImage::Format_RGB32);
|
||||||
|
|
||||||
|
loaded_img1 = new QImage("/home/davp/umk/2022_2023/GK/QT/alpha-blending/f.png");
|
||||||
|
|
||||||
|
// int width = loaded_img1->width();
|
||||||
|
// int height = loaded_img1->height();
|
||||||
|
// for(int x = 0; x < width; x++)
|
||||||
|
// for(int y = 0; y < height; y++){
|
||||||
|
// QColor color = GetPixel(loaded_img1, x, y);
|
||||||
|
// DrawPixel(loaded_img1, x, y, QColor(255, 255, 255));
|
||||||
|
// }
|
||||||
|
|
||||||
|
for(int x = 0; x < szer; x++)
|
||||||
|
for(int y = 0; y < wys; y++){
|
||||||
|
float xw = (float) x / (float)szer;
|
||||||
|
float yh = (float) y / (float) wys;
|
||||||
|
QColor color = GetInterpolatedColor(loaded_img1, xw, yh);
|
||||||
|
color.setAlpha(255);
|
||||||
|
DrawPixel(img, x, y, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Definicja destruktora
|
||||||
|
MyWindow::~MyWindow()
|
||||||
|
{
|
||||||
|
delete ui;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funkcja (slot) wywolywana po nacisnieciu przycisku "Wyjscie" (exitButton)
|
||||||
|
// Uwaga: polaczenie tej funkcji z sygnalem "clicked"
|
||||||
|
// emitowanym przez przycisk jest realizowane
|
||||||
|
// za pomoca funkcji QMetaObject::connectSlotsByName(MyWindow)
|
||||||
|
// znajdujacej sie w automatycznie generowanym pliku "ui_mywindow.h"
|
||||||
|
// Nie musimy wiec sami wywolywac funkcji "connect"
|
||||||
|
void MyWindow::on_exitButton_clicked()
|
||||||
|
{
|
||||||
|
// qApp to globalny wskaznik do obiektu reprezentujacego aplikacje
|
||||||
|
// quit() to funkcja (slot) powodujaca zakonczenie aplikacji z kodem 0 (brak bledu)
|
||||||
|
qApp->quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::on_erosion_clicked(){
|
||||||
|
Erosion(loaded_img1);
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::on_dilation_clicked(){
|
||||||
|
Dilation(loaded_img1);
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::on_opening_clicked(){
|
||||||
|
Open(loaded_img1);
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::on_closing_clicked(){
|
||||||
|
Close(loaded_img1);
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::UpdateImage(){
|
||||||
|
ClearImage(f1_img);
|
||||||
|
ClearImage(f2_img);
|
||||||
|
|
||||||
|
memcpy(f1_img->bits(), img->bits(), szer * wys * 4);
|
||||||
|
|
||||||
|
DrawTriangles();
|
||||||
|
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MyWindow::IsLit(QImage* im, int x, int y, int val){
|
||||||
|
int width = im->width();
|
||||||
|
int height = im->height();
|
||||||
|
if(x < 0 || y < 0 || x >= width || y >= height)
|
||||||
|
return false;
|
||||||
|
QColor pix = GetPixel(im, x, y);
|
||||||
|
return pix.red() == val && pix.green() == val && pix.blue() == val;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::Erosion(QImage* im){
|
||||||
|
int width = im->width();
|
||||||
|
int height = im->height();
|
||||||
|
|
||||||
|
memcpy(loaded_img2->bits(), loaded_img1->bits(), width * height * 4);
|
||||||
|
|
||||||
|
for(int x = 0; x < width; x++)
|
||||||
|
for(int y = 0; y < height; y++){
|
||||||
|
bool active = false;
|
||||||
|
for (int x2 = x - 1; x2 <= x + 1; x2++)
|
||||||
|
for (int y2 = y - 1; y2 <= y + 1; y2++)
|
||||||
|
if(IsLit(loaded_img1, x2, y2, 0)){
|
||||||
|
DrawPixel(loaded_img2, x, y, QColor(0, 0, 0));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
memcpy(loaded_img1->bits(), loaded_img2->bits(), width * height * 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::Dilation(QImage* im){
|
||||||
|
int width = im->width();
|
||||||
|
int height = im->height();
|
||||||
|
|
||||||
|
memcpy(loaded_img2->bits(), loaded_img1->bits(), width * height * 4);
|
||||||
|
|
||||||
|
for(int x = 0; x < width; x++)
|
||||||
|
for(int y = 0; y < height; y++){
|
||||||
|
bool active = false;
|
||||||
|
for (int x2 = x - 1; x2 <= x + 1; x2++)
|
||||||
|
for (int y2 = y - 1; y2 <= y + 1; y2++)
|
||||||
|
if(IsLit(loaded_img1, x2, y2, 255)){
|
||||||
|
DrawPixel(loaded_img2, x, y, QColor(255, 255, 255));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
memcpy(loaded_img1->bits(), loaded_img2->bits(), width * height * 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::Close(QImage* im){
|
||||||
|
Dilation(im);
|
||||||
|
Erosion(im);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::Open(QImage* im){
|
||||||
|
Erosion(im);
|
||||||
|
Dilation(im);
|
||||||
|
}
|
||||||
|
|
||||||
|
QColor MyWindow::GetPixel(QImage* img, int x, int y){
|
||||||
|
QColor res;
|
||||||
|
int width = img->width();
|
||||||
|
int height = img->height();
|
||||||
|
if(x >= width || y >= height || x < 0 || y < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
|
unsigned char* ptr = img->bits();
|
||||||
|
|
||||||
|
res.setBlue(ptr[width*4*y + 4*x]);
|
||||||
|
res.setGreen(ptr[width*4*y + 4*x + 1]);
|
||||||
|
res.setRed(ptr[width*4*y + 4*x + 2]);
|
||||||
|
res.setAlpha(ptr[width*4*y + 4*x + 3]);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QColor MyWindow::GetInterpolatedColor(QImage* im, float x, float y){
|
||||||
|
float width = im->width();
|
||||||
|
float height = im->height();
|
||||||
|
float xw = (x * width);
|
||||||
|
float yw = (y * height);
|
||||||
|
int x_f = (int) (x * width);
|
||||||
|
int y_f = (int) (y * height);
|
||||||
|
int x_c = ceil(x * width);
|
||||||
|
int y_c = ceil(y * height);
|
||||||
|
float a = (xw - (float) x_f);
|
||||||
|
float b = ((yw) - (float) y_f);
|
||||||
|
|
||||||
|
QColor P1 = GetPixel(im, x_f, y_c);
|
||||||
|
QColor P2 = GetPixel(im, x_c, y_c);
|
||||||
|
QColor P3 = GetPixel(im, x_c, y_f);
|
||||||
|
QColor P4 = GetPixel(im, x_f, y_f);
|
||||||
|
|
||||||
|
QColor v1 = mul(1.0f - a, P1);
|
||||||
|
QColor v2 = mul(a, P2);
|
||||||
|
QColor v3 = mul(a, P3);
|
||||||
|
QColor v4 = mul(1.0f - a, P4);
|
||||||
|
QColor top = add(v1, v2);
|
||||||
|
QColor bottom = add(v3, v4);
|
||||||
|
QColor btop = mul(b, top);
|
||||||
|
QColor bbottom = mul(1.0f - b, bottom);
|
||||||
|
QColor res = add(btop, bbottom);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GetUVW(Point P, std::vector<Point> points, std::vector<float>& res){
|
||||||
|
Point A = points[0];
|
||||||
|
Point B = points[1];
|
||||||
|
Point C = points[2];
|
||||||
|
float x = P.x;
|
||||||
|
float y = P.y;
|
||||||
|
float v_top = (x - A.x) * (C.y - A.y) - (C.x - A.x) * (y - A.y);
|
||||||
|
float v_bottom = (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y);
|
||||||
|
float w_top = (B.x - A.x) * (y - A.y) - (x - A.x) * (B.y - A.y);
|
||||||
|
float w_bottom = (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y);
|
||||||
|
float v = v_top / v_bottom;
|
||||||
|
float w = w_top / w_bottom;
|
||||||
|
float u = 1.0f - v - w;
|
||||||
|
res.push_back(u);
|
||||||
|
res.push_back(v);
|
||||||
|
res.push_back(w);
|
||||||
|
}
|
||||||
|
|
||||||
|
int MyWindow::FindPoint(int x, int y, float radius, int frame){
|
||||||
|
Point src_point(x, y);
|
||||||
|
float min_dist = FLT_MAX;
|
||||||
|
int min_id = -1;
|
||||||
|
std::vector<Point> points = frame == 0 ? points1 : points2;
|
||||||
|
for(int i = 0; i < (int)points.size(); i++){
|
||||||
|
float dist = (src_point - (Point)(points[i])).length();
|
||||||
|
if(dist < min_dist && dist < radius){
|
||||||
|
min_id = i;
|
||||||
|
min_dist = dist;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funkcja (slot) wywolywana po nacisnieciu przycisku myszy (w glownym oknie)
|
||||||
|
void MyWindow::mousePressEvent(QMouseEvent *event)
|
||||||
|
{
|
||||||
|
// Pobieramy wspolrzedne punktu klikniecia
|
||||||
|
int x = event->x();
|
||||||
|
int y = event->y();
|
||||||
|
|
||||||
|
// Sa to wspolrzedne wzgledem glownego okna,
|
||||||
|
// Musimy odjac od nich wpolrzedne lewego gornego naroznika rysunku
|
||||||
|
|
||||||
|
int x1 = x - poczX1;
|
||||||
|
int y1 = y - poczY1;
|
||||||
|
int x2 = x - poczX2;
|
||||||
|
int y2 = y - poczY2;
|
||||||
|
|
||||||
|
if(x1 < szer && y1 < wys && x1 >= 0 && y1 >= 0){
|
||||||
|
tmp_point_id = FindPoint(x1, y1, 20, 0);
|
||||||
|
if(tmp_point_id == -1 && points1.size() != 3){
|
||||||
|
mode = Add;
|
||||||
|
}else if(tmp_point_id != -1){
|
||||||
|
mode = Move;
|
||||||
|
}else{
|
||||||
|
mode = None;
|
||||||
|
}
|
||||||
|
switch(mode){
|
||||||
|
case Add:
|
||||||
|
points1.push_back(Point(x1, y1));
|
||||||
|
break;
|
||||||
|
case Move:
|
||||||
|
points1[tmp_point_id].x = x1;
|
||||||
|
points1[tmp_point_id].y = y1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}else if(x2 < szer && y2 < wys && x2 >= 0 && y2 >= 0){
|
||||||
|
tmp_point_id = FindPoint(x2, y2, 20, 1);
|
||||||
|
if(tmp_point_id == -1 && points2.size() != 3){
|
||||||
|
mode = Add;
|
||||||
|
}else if(tmp_point_id != -1){
|
||||||
|
mode = Move;
|
||||||
|
}else{
|
||||||
|
mode = None;
|
||||||
|
}
|
||||||
|
switch(mode){
|
||||||
|
case Add:
|
||||||
|
points2.push_back(Point(x2, y2));
|
||||||
|
break;
|
||||||
|
case Move:
|
||||||
|
points2[tmp_point_id].x = x2;
|
||||||
|
points2[tmp_point_id].y = y2;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}else
|
||||||
|
mode = None;
|
||||||
|
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::mouseMoveEvent(QMouseEvent *event)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Pobieramy wspolrzedne punktu klikniecia
|
||||||
|
int x = event->x();
|
||||||
|
int y = event->y();
|
||||||
|
|
||||||
|
// Sa to wspolrzedne wzgledem glownego okna,
|
||||||
|
// Musimy odjac od nich wpolrzedne lewego gornego naroznika rysunku
|
||||||
|
|
||||||
|
int x1 = x - poczX1;
|
||||||
|
int y1 = y - poczY1;
|
||||||
|
int x2 = x - poczX2;
|
||||||
|
int y2 = y - poczY2;
|
||||||
|
|
||||||
|
if(x1 < szer && y1 < wys && x1 >= 0 && y1 >= 0){
|
||||||
|
if(mode == Move){
|
||||||
|
points1[tmp_point_id].x = x1;
|
||||||
|
points1[tmp_point_id].y = y1;
|
||||||
|
}
|
||||||
|
}else if(x2 < szer && y2 < wys && x2 >= 0 && y2 >= 0){
|
||||||
|
if(mode == Move){
|
||||||
|
points2[tmp_point_id].x = x2;
|
||||||
|
points2[tmp_point_id].y = y2;
|
||||||
|
}
|
||||||
|
}else
|
||||||
|
mode = None;
|
||||||
|
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::DrawTriangles(){
|
||||||
|
if(points1.size() == 3 && points2.size() == 3)
|
||||||
|
for(int x = 0; x < szer; x++){
|
||||||
|
for(int y = 0; y < wys; y++){
|
||||||
|
std::vector<float> res;
|
||||||
|
GetUVW(Point(x, y), points2, res);
|
||||||
|
if(res[0] > 0 && res[1] > 0 && res[2] > 0){
|
||||||
|
float u = res[0];
|
||||||
|
float v = res[1];
|
||||||
|
float w = res[2];
|
||||||
|
Point A = points1[0];
|
||||||
|
Point B = points1[1];
|
||||||
|
Point C = points1[2];
|
||||||
|
Point P2 = A * (1.0f - v - w) + B * v + C * w;
|
||||||
|
DrawPixel(f2_img, x, y, GetInterpolatedColor(f1_img, (float) P2.x / szer, (float) P2.y / wys));
|
||||||
|
}
|
||||||
|
// DrawPixel(f1_img, x, y, QColor(255, 255, 255));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Point lastPoint(0, 0);
|
||||||
|
for(int i = 0; i < points1.size(); i+=1){
|
||||||
|
DrawSquare(f1_img, points1[i].x, points1[i].y, 15, QColor(255, 0, 0));
|
||||||
|
if(i != 0){
|
||||||
|
DrawLine(f1_img, lastPoint.x, lastPoint.y, points1[i].x, points1[i].y);
|
||||||
|
}
|
||||||
|
lastPoint = points1[i];
|
||||||
|
}
|
||||||
|
if(points1.size() == 3)
|
||||||
|
DrawLine(f1_img, points1[0].x, points1[0].y, points1[2].x, points1[2].y);
|
||||||
|
|
||||||
|
for(int i = 0; i < points2.size(); i+=1){
|
||||||
|
DrawSquare(f2_img, points2[i].x, points2[i].y, 15, QColor(255, 0, 0));
|
||||||
|
if(i != 0){
|
||||||
|
DrawLine(f2_img, lastPoint.x, lastPoint.y, points2[i].x, points2[i].y);
|
||||||
|
}
|
||||||
|
lastPoint = points2[i];
|
||||||
|
}
|
||||||
|
if(points2.size() == 3)
|
||||||
|
DrawLine(f2_img, points2[0].x, points2[0].y, points2[2].x, points2[2].y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyWindow::DrawSquare(QImage* im, int x, int y, int size, QColor color){
|
||||||
|
size /= 2;
|
||||||
|
for(int x1 = x - size; x1 < x + size; x1++){
|
||||||
|
for(int y1 = y - size; y1 < y + size; y1++){
|
||||||
|
if(x1 >= szer || y1 >= wys || x1 < 0 || y1 < 0)
|
||||||
|
continue;
|
||||||
|
DrawPixel(im, x1, y1, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funkcja "odmalowujaca" komponent
|
||||||
|
void MyWindow::paintEvent(QPaintEvent*)
|
||||||
|
{
|
||||||
|
QPainter p(this);
|
||||||
|
|
||||||
|
// UpdateImage();
|
||||||
|
|
||||||
|
p.drawImage(poczX1,poczY1,*f1_img);
|
||||||
|
p.drawImage(poczX2,poczY2,*f2_img);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MyWindow::DrawPixel(QImage* im, int x, int y, QColor color){
|
||||||
|
int width = im->width();
|
||||||
|
int height = im->height();
|
||||||
|
|
||||||
|
if(x >= width || y >= height || x < 0 || y < 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
unsigned char* ptr = im->bits();
|
||||||
|
|
||||||
|
ptr[width*4*y + 4*x] = color.blue();
|
||||||
|
ptr[width*4*y + 4*x + 1] = color.green();
|
||||||
|
ptr[width*4*y + 4*x + 2] = color.red();
|
||||||
|
ptr[width*4*y + 4*x + 3] = color.alpha();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MyWindow::ClearImage(QImage *img){
|
||||||
|
unsigned char* empty_val = (unsigned char*)malloc(4);
|
||||||
|
empty_val[0] = 0;
|
||||||
|
empty_val[1] = 0;
|
||||||
|
empty_val[2] = 0;
|
||||||
|
empty_val[3] = 255;
|
||||||
|
unsigned char* ptr = img->bits();
|
||||||
|
for(int i = 0; i < img->width(); i++){
|
||||||
|
for(int j = 0; j < img->height(); j++){
|
||||||
|
memcpy(ptr + 4 * (i + j * img->width()), empty_val, 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MyWindow::DrawLine(QImage *im, int x1, int y1, int x2, int y2){
|
||||||
|
if(x1 > x2){
|
||||||
|
std::swap(x1, x2);
|
||||||
|
std::swap(y1, y2);
|
||||||
|
}
|
||||||
|
float diff = x2 - x1;
|
||||||
|
float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX;
|
||||||
|
|
||||||
|
QColor color(255, 255, 255, 255);
|
||||||
|
|
||||||
|
if(abs(a) < 1.0f){
|
||||||
|
for(int x = x1; x <= x2; x++){
|
||||||
|
int x_form = x - x1;
|
||||||
|
int y = a * x_form + y1;
|
||||||
|
|
||||||
|
DrawPixel(im, x, y, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if(y1 > y2){
|
||||||
|
std::swap(x1, x2);
|
||||||
|
std::swap(y1, y2);
|
||||||
|
}
|
||||||
|
float diff = x2 - x1;
|
||||||
|
float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX;
|
||||||
|
for(int y = y1; y <= y2; y++){
|
||||||
|
int y_form = y - y1;
|
||||||
|
int x = ((float)(y_form) / a) + x1;
|
||||||
|
|
||||||
|
DrawPixel(im, x, y, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
181
QT/triangle-textures/mywindow.h
Normal file
181
QT/triangle-textures/mywindow.h
Normal file
@ -0,0 +1,181 @@
|
|||||||
|
// Plik naglowkowy klasy MyWindow
|
||||||
|
// Obiekt tej klasy to glowne okno naszej aplikacji
|
||||||
|
// Szkielet tego pliku jest tworzony przez QtCreator
|
||||||
|
// Mozemy do niego dodac deklaracje wlasnych pol i metod
|
||||||
|
|
||||||
|
#ifndef MYWINDOW_H
|
||||||
|
#define MYWINDOW_H
|
||||||
|
|
||||||
|
// Dolaczamy plik naglowkowy klasy QMainWindow,
|
||||||
|
// Klasa QMainWindow posiada swoj wlasny layout.
|
||||||
|
// latwo mozna do niej dodac pasek menu, widzety dokujace,
|
||||||
|
// pasek narzedzi i pasek statusu. Na srodku okna
|
||||||
|
// wyswietlanego przez QMainWindow znajduje sie obszar,
|
||||||
|
// ktory mozna wypelnic roznymi widgetami.
|
||||||
|
#include <QMainWindow>
|
||||||
|
|
||||||
|
// QPainter to klasa umozliwiajaca niskopoziomowe rysowanie
|
||||||
|
// na elementach GUI
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
|
// QImage to klasa pozwalajaca na niezalezna od sprzetu reprezentacje obrazu.
|
||||||
|
// Pozwala na bezposredni dostep do poszczegolnych pikseli,
|
||||||
|
// Bedziemy jej uzywali do tworzenia i przechowywania
|
||||||
|
// naszych rysunkow
|
||||||
|
#include <QImage>
|
||||||
|
|
||||||
|
// QMouseEvent to klasa obslugujaca zdarzenia zwiazane z myszka
|
||||||
|
// klikniecia, ruch myszka itp.
|
||||||
|
#include <QMouseEvent>
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#include <float.h> // for float,double macros
|
||||||
|
|
||||||
|
namespace Ui {
|
||||||
|
class MyWindow;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Mode {Add, Move, None};
|
||||||
|
|
||||||
|
|
||||||
|
struct Point{
|
||||||
|
int x, y;
|
||||||
|
|
||||||
|
Point(int x, int y){
|
||||||
|
this->x=x;
|
||||||
|
this->y=y;
|
||||||
|
}
|
||||||
|
|
||||||
|
float length(){
|
||||||
|
float mag = std::sqrt(std::pow(x, 2) + std::pow(y, 2));
|
||||||
|
return mag;
|
||||||
|
}
|
||||||
|
|
||||||
|
Point operator-(const Point& other){
|
||||||
|
return Point(this->x - other.x, this->y - other.y);
|
||||||
|
// float x = other.x - this->x;
|
||||||
|
// float y = other.y - this->y;
|
||||||
|
// float mag = std::sqrt(std::pow(x, 2) + std::pow(y, 2));
|
||||||
|
// return mag;
|
||||||
|
}
|
||||||
|
|
||||||
|
Point operator*(const float mul){
|
||||||
|
return Point(this->x * mul, this->y * mul);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point operator+(const Point& other){
|
||||||
|
return Point(this->x + other.x, this->y + other.y);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// MyWindow jest podklasa klasy QMainWindow.
|
||||||
|
class MyWindow : public QMainWindow
|
||||||
|
{
|
||||||
|
// Q_OBJECT jest to makro, ktore musi sie znajdowac
|
||||||
|
// we wszystkich klasach definiujacych wlasne sygnaly i sloty
|
||||||
|
// W naszej klasie nie jest ono potrzebne,
|
||||||
|
// ale QtCreator dodaje je automatycznie do kazdej klasy.
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Typowa deklaracja konstruktora w Qt.
|
||||||
|
// Parametr "parent" okresla rodzica komponenetu.
|
||||||
|
// W przypadku naszej klasy parametr ten wskazuje na null
|
||||||
|
// co oznacza, ze komponenet nie ma rodzica, jest to
|
||||||
|
// komponenet najwyzszego poziomu
|
||||||
|
explicit MyWindow(QWidget *parent = 0);
|
||||||
|
|
||||||
|
// Deklaracja destruktora
|
||||||
|
~MyWindow();
|
||||||
|
|
||||||
|
private:
|
||||||
|
// QtCreator pozwala na tworzenie GUI za pomoca graficznego kreatora.
|
||||||
|
// Skladniki interfejsu i ich wlasciwosci zapisane sa wowczas
|
||||||
|
// w pliku XML "nazwa_klasy.ui"
|
||||||
|
// Do poszczegolnych elementow GUI odwolujemy sie za pomoca zmiennej "ui"
|
||||||
|
Ui::MyWindow *ui;
|
||||||
|
|
||||||
|
int mode;
|
||||||
|
|
||||||
|
// Pole przechowujace obrazek
|
||||||
|
QImage *img;
|
||||||
|
|
||||||
|
QImage *f1_img;
|
||||||
|
QImage *f2_img;
|
||||||
|
|
||||||
|
QImage *loaded_img1;
|
||||||
|
QImage *loaded_img2;
|
||||||
|
|
||||||
|
// Pole przechowujace obrazek
|
||||||
|
QImage *img_tmp;
|
||||||
|
|
||||||
|
QImage *active_img;
|
||||||
|
|
||||||
|
// Pola przechowujace szerokosc i wysokosc rysunku
|
||||||
|
// oraz wspolrzedne jego lewego gornego naroznika
|
||||||
|
int szer;
|
||||||
|
int wys;
|
||||||
|
int poczX1;
|
||||||
|
int poczY1;
|
||||||
|
int poczX2;
|
||||||
|
int poczY2;
|
||||||
|
int startX, startY;
|
||||||
|
bool draw_finished;
|
||||||
|
int segment_count;
|
||||||
|
float alpha;
|
||||||
|
|
||||||
|
float rotation_angle;
|
||||||
|
std::vector<float> scale_vec;
|
||||||
|
std::vector<float> translation_vec;
|
||||||
|
std::vector<float> sh_vec;
|
||||||
|
|
||||||
|
std::vector<Point> points1;
|
||||||
|
std::vector<Point> points2;
|
||||||
|
|
||||||
|
int tmp_point_id = 0;
|
||||||
|
int tmp_shape_id = 0;
|
||||||
|
|
||||||
|
// Deklaracje funkcji
|
||||||
|
void czysc();
|
||||||
|
void rysuj1();
|
||||||
|
void rysuj2();
|
||||||
|
|
||||||
|
|
||||||
|
void UpdateTempImage();
|
||||||
|
void ApplyTempImage();
|
||||||
|
|
||||||
|
void ClearImage(QImage *img);
|
||||||
|
|
||||||
|
QColor GetPixel(QImage* img, int x, int y);
|
||||||
|
void DrawPixel(QImage* img, int x, int y, QColor color);
|
||||||
|
QColor GetInterpolatedColor(QImage* img, float x, float y);
|
||||||
|
void UpdateImage();
|
||||||
|
|
||||||
|
|
||||||
|
bool IsLit(QImage* im, int x, int y, int val);
|
||||||
|
void Erosion(QImage* im);
|
||||||
|
void Dilation(QImage* im);
|
||||||
|
void Close(QImage* im);
|
||||||
|
void Open(QImage* im);
|
||||||
|
int FindPoint(int x, int y, float radius, int frame);
|
||||||
|
void DrawTriangles();
|
||||||
|
void DrawSquare(QImage* im, int x, int y, int size, QColor color);
|
||||||
|
void DrawLine(QImage *im, int x1, int y1, int x2, int y2);
|
||||||
|
// Deklaracje slotow, czyli funkcji wywolywanych
|
||||||
|
// po wystapieniu zdarzen zwiazanych z GUI
|
||||||
|
// np. klikniecie na przycisk, ruch myszka
|
||||||
|
private slots:
|
||||||
|
void on_exitButton_clicked();
|
||||||
|
void paintEvent(QPaintEvent*);
|
||||||
|
void on_erosion_clicked();
|
||||||
|
void on_dilation_clicked();
|
||||||
|
void on_opening_clicked();
|
||||||
|
void on_closing_clicked();
|
||||||
|
void mousePressEvent(QMouseEvent *event);
|
||||||
|
void mouseMoveEvent(QMouseEvent *event);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // MYWINDOW_H
|
160
QT/triangle-textures/mywindow.ui
Normal file
160
QT/triangle-textures/mywindow.ui
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>MyWindow</class>
|
||||||
|
<widget class="QMainWindow" name="MyWindow">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>1236</width>
|
||||||
|
<height>726</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="mouseTracking">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>MyWindow</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="centralWidget">
|
||||||
|
<widget class="QFrame" name="frame1">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>20</x>
|
||||||
|
<y>20</y>
|
||||||
|
<width>600</width>
|
||||||
|
<height>600</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="mouseTracking">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="frameShape">
|
||||||
|
<enum>QFrame::NoFrame</enum>
|
||||||
|
</property>
|
||||||
|
<property name="frameShadow">
|
||||||
|
<enum>QFrame::Raised</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QGroupBox" name="groupBox">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>980</x>
|
||||||
|
<y>460</y>
|
||||||
|
<width>241</width>
|
||||||
|
<height>201</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="title">
|
||||||
|
<string>Opcje</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QPushButton" name="exitButton">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>90</x>
|
||||||
|
<y>30</y>
|
||||||
|
<width>75</width>
|
||||||
|
<height>23</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Wyjście</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QPushButton" name="erosion">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>30</x>
|
||||||
|
<y>80</y>
|
||||||
|
<width>80</width>
|
||||||
|
<height>25</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Erozja</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QPushButton" name="opening">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>30</x>
|
||||||
|
<y>140</y>
|
||||||
|
<width>80</width>
|
||||||
|
<height>25</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Otwarcie</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QPushButton" name="dilation">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>140</x>
|
||||||
|
<y>80</y>
|
||||||
|
<width>80</width>
|
||||||
|
<height>25</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Dylacja</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QPushButton" name="closing">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>140</x>
|
||||||
|
<y>140</y>
|
||||||
|
<width>80</width>
|
||||||
|
<height>25</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Zamknięcie</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
|
<widget class="QFrame" name="frame2">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>630</x>
|
||||||
|
<y>20</y>
|
||||||
|
<width>600</width>
|
||||||
|
<height>600</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="mouseTracking">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="frameShape">
|
||||||
|
<enum>QFrame::NoFrame</enum>
|
||||||
|
</property>
|
||||||
|
<property name="frameShadow">
|
||||||
|
<enum>QFrame::Raised</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
|
<widget class="QMenuBar" name="menuBar">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>1236</width>
|
||||||
|
<height>22</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QToolBar" name="mainToolBar">
|
||||||
|
<attribute name="toolBarArea">
|
||||||
|
<enum>TopToolBarArea</enum>
|
||||||
|
</attribute>
|
||||||
|
<attribute name="toolBarBreak">
|
||||||
|
<bool>false</bool>
|
||||||
|
</attribute>
|
||||||
|
</widget>
|
||||||
|
<widget class="QStatusBar" name="statusBar"/>
|
||||||
|
</widget>
|
||||||
|
<layoutdefault spacing="6" margin="11"/>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
BIN
QT/triangle-textures/p.png
Normal file
BIN
QT/triangle-textures/p.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 9.6 KiB |
20
QT/triangle-textures/triangle-textures.pro
Normal file
20
QT/triangle-textures/triangle-textures.pro
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#-------------------------------------------------
|
||||||
|
#
|
||||||
|
# Project created by QtCreator 2015-03-03T00:14:51
|
||||||
|
#
|
||||||
|
#-------------------------------------------------
|
||||||
|
|
||||||
|
QT += core gui
|
||||||
|
|
||||||
|
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||||
|
|
||||||
|
TARGET = triangle-textures
|
||||||
|
TEMPLATE = app
|
||||||
|
|
||||||
|
OUTPUT += Console
|
||||||
|
SOURCES += main.cpp\
|
||||||
|
mywindow.cpp
|
||||||
|
|
||||||
|
HEADERS += mywindow.h
|
||||||
|
|
||||||
|
FORMS += mywindow.ui
|
Loading…
Reference in New Issue
Block a user