00001
00002
00003
00004
00005
00006
00007 #include "DocumentModelGridRemoval.h"
00008 #include "EngaugeAssert.h"
00009 #include "GridHealer.h"
00010 #include "GridRemoval.h"
00011 #include "Logger.h"
00012 #include <qdebug.h>
00013 #include <QImage>
00014 #include <qmath.h>
00015 #include "Transformation.h"
00016
00017 const double EPSILON = 0.000001;
00018
00019 GridRemoval::GridRemoval()
00020 {
00021 }
00022
00023 QPointF GridRemoval::clipX (const QPointF &posUnprojected,
00024 double xBoundary,
00025 const QPointF &posOther) const
00026 {
00027 double s = (xBoundary - posUnprojected.x()) / (posOther.x() - posUnprojected.x());
00028 ENGAUGE_ASSERT ((-1.0 * EPSILON < s) && (s < 1.0 + EPSILON));
00029
00030 return QPointF ((1.0 - s) * posUnprojected.x() + s * posOther.x(),
00031 (1.0 - s) * posUnprojected.y() + s * posOther.y());
00032 }
00033
00034 QPointF GridRemoval::clipY (const QPointF &posUnprojected,
00035 double yBoundary,
00036 const QPointF &posOther) const
00037 {
00038 double s = (yBoundary - posUnprojected.y()) / (posOther.y() - posUnprojected.y());
00039 ENGAUGE_ASSERT ((-1.0 * EPSILON < s) && (s < 1.0 + EPSILON));
00040
00041 return QPointF ((1.0 - s) * posUnprojected.x() + s * posOther.x(),
00042 (1.0 - s) * posUnprojected.y() + s * posOther.y());
00043 }
00044
00045 QPixmap GridRemoval::remove (const Transformation &transformation,
00046 const DocumentModelGridRemoval &modelGridRemoval,
00047 const QImage &imageBefore)
00048 {
00049 LOG4CPP_INFO_S ((*mainCat)) << "GridRemoval::remove"
00050 << " transformationIsDefined=" << (transformation.transformIsDefined() ? "true" : "false")
00051 << " removeDefinedGridLines=" << (modelGridRemoval.removeDefinedGridLines() ? "true" : "false");
00052
00053 QImage image = imageBefore;
00054
00055
00056 if (modelGridRemoval.removeDefinedGridLines() &&
00057 transformation.transformIsDefined()) {
00058
00059 GridHealer gridHealer (imageBefore,
00060 modelGridRemoval);
00061
00062 double yGraphMin = modelGridRemoval.startY();
00063 double yGraphMax = modelGridRemoval.stopY();
00064 for (int i = 0; i < modelGridRemoval.countX(); i++) {
00065 double xGraph = modelGridRemoval.startX() + i * modelGridRemoval.stepX();
00066
00067
00068 QPointF posScreenMin, posScreenMax;
00069 transformation.transformRawGraphToScreen (QPointF (xGraph,
00070 yGraphMin),
00071 posScreenMin);
00072 transformation.transformRawGraphToScreen (QPointF (xGraph,
00073 yGraphMax),
00074 posScreenMax);
00075
00076 removeLine (posScreenMin,
00077 posScreenMax,
00078 image,
00079 gridHealer);
00080 }
00081
00082 double xGraphMin = modelGridRemoval.startX();
00083 double xGraphMax = modelGridRemoval.stopX();
00084 for (int j = 0; j < modelGridRemoval.countY(); j++) {
00085 double yGraph = modelGridRemoval.startY() + j * modelGridRemoval.stepY();
00086
00087
00088 QPointF posScreenMin, posScreenMax;
00089 transformation.transformRawGraphToScreen (QPointF (xGraphMin,
00090 yGraph),
00091 posScreenMin);
00092 transformation.transformRawGraphToScreen (QPointF (xGraphMax,
00093 yGraph),
00094 posScreenMax);
00095
00096 removeLine (posScreenMin,
00097 posScreenMax,
00098 image,
00099 gridHealer);
00100 }
00101
00102
00103 gridHealer.heal (image);
00104 }
00105
00106 return QPixmap::fromImage (image);
00107 }
00108
00109 void GridRemoval::removeLine (const QPointF &posMin,
00110 const QPointF &posMax,
00111 QImage &image,
00112 GridHealer &gridHealer)
00113 {
00114 double w = image.width() - 1;
00115 double h = image.height() - 1;
00116
00117 QPointF pos1 = posMin;
00118 QPointF pos2 = posMax;
00119
00120
00121
00122 bool onLeft = (pos1.x() < 0 && pos2.x () < 0);
00123 bool onTop = (pos1.y() < 0 && pos2.y () < 0);
00124 bool onRight = (pos1.x() > w && pos2.x () > w);
00125 bool onBottom = (pos1.y() > h && pos2.y () > h);
00126 if (!onLeft && !onTop && !onRight && !onBottom) {
00127
00128
00129 if (pos1.x() < 0) { pos1 = clipX (pos1, 0, pos2); }
00130 if (pos2.x() < 0) { pos2 = clipX (pos2, 0, pos1); }
00131 if (pos1.y() < 0) { pos1 = clipY (pos1, 0, pos2); }
00132 if (pos2.y() < 0) { pos2 = clipY (pos2, 0, pos1); }
00133 if (pos1.x() > w) { pos1 = clipX (pos1, w, pos2); }
00134 if (pos2.x() > w) { pos2 = clipX (pos2, w, pos1); }
00135 if (pos1.y() > h) { pos1 = clipY (pos1, h, pos2); }
00136 if (pos2.y() > h) { pos2 = clipY (pos2, h, pos1); }
00137
00138
00139 double deltaX = qAbs (pos1.x() - pos2.x());
00140 double deltaY = qAbs (pos1.y() - pos2.y());
00141 if (deltaX > deltaY) {
00142
00143
00144 int xMin = qMin (pos1.x(), pos2.x());
00145 int xMax = qMax (pos1.x(), pos2.x());
00146 int yAtXMin = (pos1.x() < pos2.x() ? pos1.y() : pos2.y());
00147 int yAtXMax = (pos1.x() < pos2.x() ? pos2.y() : pos1.y());
00148 for (int x = xMin; x <= xMax; x++) {
00149 double s = (double) (x - xMin) / (double) (xMax - xMin);
00150 double yLine = (1.0 - s) * yAtXMin + s * yAtXMax;
00151 for (int yOffset = -1; yOffset <= 1; yOffset++) {
00152 int y = (int) (0.5 + yLine + yOffset);
00153 image.setPixel (x, y, QColor(Qt::white).rgb());
00154 gridHealer.erasePixel (x, y);
00155 }
00156 }
00157 } else {
00158
00159
00160 int yMin = qMin (pos1.y(), pos2.y());
00161 int yMax = qMax (pos1.y(), pos2.y());
00162 int xAtYMin = (pos1.y() < pos2.y() ? pos1.x() : pos2.x());
00163 int xAtYMax = (pos1.y() < pos2.y() ? pos2.x() : pos1.x());
00164 for (int y = yMin; y <= yMax; y++) {
00165 double s = (double) (y - yMin) / (double) (yMax - yMin);
00166 double xLine = (1.0 - s) * xAtYMin + s * xAtYMax;
00167 for (int xOffset = -1; xOffset <= 1; xOffset++) {
00168 int x = (int) (0.5 + xLine + xOffset);
00169 image.setPixel (x, y, QColor(Qt::white).rgb());
00170 gridHealer.erasePixel (x, y);
00171 }
00172 }
00173 }
00174 }
00175 }
00176