// // File: texture_image.cc // // (C) 2000-2006 Helmut Cantzler // // Licensed under the terms of the Lesser General Public License. // #include #include "texture_image.h" TextureImage::TextureImage(QWidget *parent) : QWidget(parent) { fn = fn_short = QString::null; homo_nr=0; resize(0,0); } const unsigned int* TextureImage::getHomoPoints(void) { if (homo_nr != 4) return NULL; unsigned int *points = new unsigned int[8]; points[0] = homo_points[0].x; points[1] = homo_points[0].y; points[2] = homo_points[1].x; points[3] = homo_points[1].y; points[4] = homo_points[2].x; points[5] = homo_points[2].y; points[6] = homo_points[3].x; points[7] = homo_points[3].y; return points; } const char* TextureImage::getFileName(void) const { return fn_short.toAscii().data(); } void TextureImage::paintEvent(QPaintEvent *e) { if ( !pix.isNull() ) { QPainter painter(this); painter.setClipRect(e->rect()); painter.drawPixmap(0, 0, pix); } } void TextureImage::drawPoint(Point p, unsigned int color) { img.setPixel(p.x, p.y, color); img.setPixel(p.x+1, p.y, color); img.setPixel(p.x-1, p.y, color); img.setPixel(p.x, p.y+1, color); img.setPixel(p.x, p.y-1, color); img.setPixel(p.x+2, p.y, 0x000000); img.setPixel(p.x-2, p.y, 0x000000); img.setPixel(p.x, p.y+2, 0x000000); img.setPixel(p.x, p.y-2, 0x000000); img.setPixel(p.x+1, p.y+1, 0x000000); img.setPixel(p.x+1, p.y-1, 0x000000); img.setPixel(p.x-1, p.y+1, 0x000000); img.setPixel(p.x-1, p.y-1, 0x000000); pix = QPixmap::fromImage(img); repaint(); } void TextureImage::mousePressEvent(QMouseEvent *e) { Point p; int point_colors[4] = { 0xFF00FF, 0x00FF00, 0xFFFF00, 0xFFFFFF }; p.x = e->x(); p.y = e->y(); if (e->button() == Qt::LeftButton && homo_nr < 4) if ( p.x >= 5 && p.x < pix.width()-5 && p.y >= 5 && p.y < pix.height()-5 ) { homo_points[homo_nr] = p; drawPoint(p, point_colors[homo_nr]); homo_nr++; } e->accept(); } void TextureImage::clearSelection(void) { if ( !img_org.isNull() ) { homo_nr=0; img = img_org; pix = QPixmap::fromImage(img); repaint(); } } void TextureImage::loadImage(void) { QList list = QImageReader::supportedImageFormats(); QString formats = "Images ("; for (int i = 0; i < list.size(); ++i) formats += " *." + QString(list.at(i).constData()).toLower(); formats += ")"; fn = QFileDialog::getOpenFileName(this, "Choose an image file", "", formats); if ( fn.contains('/') ) fn_short = fn.section('/', -1); else if ( fn.contains('\\') ) fn_short = fn.section('\\', -1); else fn_short = fn; if ( !fn.isEmpty() ) { homo_nr=0; img_org.load(fn); if (img_org.depth() <= 8) img_org = img_org.convertToFormat(QImage::Format_RGB32); img = img_org; pix = QPixmap::fromImage(img); resize(pix.size()); repaint(); } }