Fade edges of image

This commit is contained in:
craig.p.drummond
2013-03-22 20:55:47 +00:00
committed by craig.p.drummond
parent 9236166785
commit a65f9df18f
2 changed files with 83 additions and 4 deletions

View File

@@ -23,15 +23,90 @@
#include "textbrowser.h"
#include <QPainter>
#include <QDebug>
static const int constMinSize=300;
static int border=-1;
static int minSize=-1;
static int maxSize=-1;
static void fadeEdges(QImage &img, int edgeSize)
{
unsigned char *data=img.bits();
int width=img.width()*4;
int height=img.height();
for (int i=0; i<edgeSize; ++i) {
int alpha=((i+1.0)/(edgeSize*1.0))*255;
int offset=i*img.bytesPerLine();
for(int column=i*4; column<width; column+=4) {
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
// ARGB
data[offset+column] = alpha;
#else
// BGRA
data[offset+column+3] = alpha;
#endif
}
offset=img.bytesPerLine()*(height-1);
for(int column=i*4; column<width; column+=4) {
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
// ARGB
data[offset+column] = alpha;
#else
// BGRA
data[offset+column+3] = alpha;
#endif
}
for (int j=i; j<height; ++j) {
offset=(img.bytesPerLine()*j)+(i*4);
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
// ARGB
data[offset] = alpha;
#else
// BGRA
data[offset+3] = alpha;
#endif
offset=img.bytesPerLine()*j;
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
// ARGB
data[offset+(width-4)] = alpha;
#else
// BGRA
data[offset+3+(width-4)] = alpha;
#endif
}
width-=4;
height--;
}
}
TextBrowser::TextBrowser(QWidget *p)
: QTextBrowser(p)
, drawImage(false)
{
orig=font().pointSize();
if (-1==minSize) {
minSize=fontMetrics().height()*18;
minSize=(((int)(minSize/100))*100)+(minSize%100 ? 100 : 0);
maxSize=minSize*2;
border=minSize/4;
}
}
void TextBrowser::setImage(const QImage &img)
{
if (drawImage && (!img.isNull() || (img.isNull()!=image.isNull()))) {
image=img;
if (!image.isNull() && (image.width()<constMinSize || image.height()<constMinSize)) {
image=image.scaled(QSize(constMinSize, constMinSize), Qt::KeepAspectRatio, Qt::SmoothTransformation);
if (!image.isNull()) {
if (image.width()<minSize || image.height()<minSize) {
image=image.scaled(QSize(maxSize, maxSize), Qt::KeepAspectRatio, Qt::SmoothTransformation);
} else if (image.width()>maxSize || image.height()>maxSize) {
image=image.scaled(QSize(maxSize, maxSize), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
image=image.convertToFormat(QImage::Format_ARGB32);
fadeEdges(image, border);
}
viewport()->update();
}
@@ -56,6 +131,10 @@ void TextBrowser::paintEvent(QPaintEvent *e)
if (drawImage && isReadOnly() && !image.isNull()) {
QPainter p(viewport());
p.setOpacity(0.15);
// QRect r(rect());
// int xpos=r.x()+(r.width()>image.width() ? ((r.width()-image.width())/2) : 0);
// int ypos=r.y()+(r.height()>image.height() ? ((r.height()-image.height())/2) : 0);
// p.drawImage(xpos, ypos, image);
p.fillRect(rect(), QBrush(image));
} else if (!viewport()->autoFillBackground()) {
QPainter p(viewport());

View File

@@ -31,7 +31,7 @@
class TextBrowser : public QTextBrowser
{
public:
TextBrowser(QWidget *p) : QTextBrowser(p) , drawImage(false) { orig=font().pointSize(); }
TextBrowser(QWidget *p);
virtual ~TextBrowser() { }
void setZoom(int diff) { if (diff) zoomIn(diff); }