Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continuous brush stroke #84

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions src/image_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,28 @@ void ImageCanvas::paintEvent(QPaintEvent *event) {
}
}

QPoint ImageCanvas::_get_pen_pose(QMouseEvent * e) {
int x = e->x() / _scale;
int y = e->y() / _scale;
return QPoint(x,y);
}

void ImageCanvas::mouseMoveEvent(QMouseEvent * e) {
_mouse_pos.setX(e->x());
_mouse_pos.setY(e->y());

if (_button_is_pressed)
_drawFillCircle(e);

update();
if (_button_is_pressed) {
QPoint cur_point = _get_pen_pose(e);
_mask.drawLine(_prev_point, cur_point, _pen_size, _color);
_prev_point = cur_point;
}
update();
}

void ImageCanvas::setSizePen(int pen_size) {
_pen_size = pen_size;
}


void ImageCanvas::mouseReleaseEvent(QMouseEvent * e) {
if(e->button() == Qt::LeftButton) {
_button_is_pressed = false;
Expand Down Expand Up @@ -212,25 +219,14 @@ void ImageCanvas::mouseReleaseEvent(QMouseEvent * e) {

void ImageCanvas::mousePressEvent(QMouseEvent * e) {
setFocus();
if (e->button() == Qt::LeftButton) {
if (e->button() == Qt::LeftButton) {
_prev_point = _get_pen_pose(e);
_button_is_pressed = true;
_drawFillCircle(e);
_mask.drawFillCircle(_get_pen_pose(e), _pen_size, _color);
update();
}
}

void ImageCanvas::_drawFillCircle(QMouseEvent * e) {
if (_pen_size > 0) {
int x = e->x() / _scale - _pen_size / 2;
int y = e->y() / _scale - _pen_size / 2;
_mask.drawFillCircle(x, y, _pen_size, _color);
} else {
int x = (e->x()+0.5) / _scale ;
int y = (e->y()+0.5) / _scale ;
_mask.drawPixel(x, y, _color);
}
update();
}

void ImageCanvas::clearMask() {
_mask = ImageMask(_image.size());
_watershed = ImageMask(_image.size());
Expand Down
8 changes: 4 additions & 4 deletions src/image_canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public slots :
private:
MainWindow *_ui;

void _initPixmap();
void _drawFillCircle(QMouseEvent * e);
void _initPixmap();
QPoint _get_pen_pose(QMouseEvent * e);

QScrollArea *_scroll_parent ;
double _scale ;
Expand All @@ -71,9 +71,9 @@ public slots :
ColorMask _color ;
int _pen_size ;
bool _button_is_pressed;

QPoint _prev_point ;
};



#endif //IMAGE_CANVAS_H
#endif //IMAGE_CANVAS_H
52 changes: 34 additions & 18 deletions src/image_mask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,45 @@ ImageMask::ImageMask(QSize s) {
color.fill(QColor(0, 0, 0));
}

void ImageMask::drawFillCircle(int x, int y, int pen_size, ColorMask cm) {
QPen pen(QBrush(cm.id), 1.0);
void ImageMask::drawLine(QPoint p1, QPoint p2, int pen_size, ColorMask cm) {
QPainter painter_id(&id);
painter_id.setRenderHint(QPainter::Antialiasing, false);
painter_id.setPen(pen);
painter_id.setBrush(QBrush(cm.id));
painter_id.drawEllipse(x, y, pen_size, pen_size);
painter_id.setRenderHint(QPainter::Antialiasing, false);
painter_id.setPen(QPen(cm.id, pen_size, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter_id.drawLine(p1, p2);
painter_id.end();

QPainter painter_color(&color);
QPen pen_color(QBrush(cm.color), 1.0);
painter_color.setRenderHint(QPainter::Antialiasing, false);
painter_color.setPen(pen_color);
painter_color.setBrush(QBrush(cm.color));
painter_color.drawEllipse(x, y, pen_size, pen_size);
painter_color.end();
QPainter painter_color(&color);
painter_color.setRenderHint(QPainter::Antialiasing, false);
painter_color.setPen(QPen(cm.color, pen_size, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter_color.drawLine(p1, p2);
painter_color.end();
}
void ImageMask::drawFillCircle(QPoint point, int pen_size, ColorMask cm) {
if(pen_size<=1) {
id.setPixelColor(point, cm.id);
color.setPixelColor(point, cm.color);
}
else {
pen_size = (pen_size-1)/2;
QPen pen(QBrush(cm.id), 1.0);
QPainter painter_id(&id);
painter_id.setRenderHint(QPainter::Antialiasing, false);
painter_id.setPen(pen);
painter_id.setBrush(QBrush(cm.id));
painter_id.drawEllipse(point, pen_size, pen_size);
painter_id.end();

void ImageMask::drawPixel(int x, int y, ColorMask cm) {
id.setPixelColor(x, y, cm.id);
color.setPixelColor(x, y, cm.color);
QPainter painter_color(&color);
QPen pen_color(QBrush(cm.color), 1.0);
painter_color.setRenderHint(QPainter::Antialiasing, false);
painter_color.setPen(pen_color);
painter_color.setBrush(QBrush(cm.color));
painter_color.drawEllipse(point, pen_size, pen_size);
painter_color.end();
}
}

void ImageMask::updateColor(const Id2Labels & labels) {
idToColor(id, labels, &color);
}
Expand All @@ -54,4 +70,4 @@ void ImageMask::exchangeLabel(int x, int y, const Id2Labels& id_labels, ColorMas
id = mat2QImage(id_mat);
color = idToColor(id, id_labels);

}
}
6 changes: 3 additions & 3 deletions src/image_mask.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ struct ImageMask {
ImageMask(const QString &file, Id2Labels id_labels);
ImageMask(QSize s);

void drawFillCircle(int x, int y, int pen_size, ColorMask cm);
void drawPixel(int x, int y, ColorMask cm);
void drawLine(QPoint p1, QPoint p2, int pen_size, ColorMask cm);
void drawFillCircle(QPoint point, int pen_size, ColorMask cm);
void updateColor(const Id2Labels & labels);
void exchangeLabel(int x, int y, const Id2Labels & id_labels, ColorMask cm);
};

#endif
#endif