summaryrefslogtreecommitdiffstats
path: root/kolourpaint/patches/color_eraser_speedup.diff
blob: b6e51d5dd8bac11ae90feb01f3321ad45eafbe6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
[probably no longer applies without modification]

Attempts to improve the performance of the Color Eraser & Eraser
by drawing only _unique_ rectangles across interpolation lines
and by not drawing pixmaps when the user has a solid rectangular
brush.

- appears to decrease the performance of the Eraser (QRegion
  overhead?).
- reduces code clarity
- unsure of whether it increases performance of Color Eraser
  (sometimes it seems faster, sometimes not)

Index: tools/kptoolpen.cpp
===================================================================
RCS file: /home/kde/kdenonbeta/kolourpaint/tools/kptoolpen.cpp,v
retrieving revision 1.9
diff -u -p -r1.9 kptoolpen.cpp
--- tools/kptoolpen.cpp	6 Dec 2003 06:53:36 -0000	1.9
+++ tools/kptoolpen.cpp	6 Dec 2003 06:55:46 -0000
@@ -34,12 +34,13 @@
 #include <qapplication.h>
 #include <qbitmap.h>
 #include <qcursor.h>
-#include <qimage.h>
-#include <qpainter.h>
-#include <qpixmap.h>
 #if DEBUG_KP_TOOL_PEN
     #include <qdatetime.h>
 #endif
+#include <qimage.h>
+#include <qpainter.h>
+#include <qpixmap.h>
+#include <qregion.h>
 
 #include <kdebug.h>
 #include <tdelocale.h>
@@ -416,31 +417,28 @@ void kpToolPen::draw (const QPoint &this
             rect = neededRect (rect, m_brushPixmap [m_mouseButton].width ());
 
     #if DEBUG_KP_TOOL_PEN
-        if (m_mode & WashesPixmaps)
-        {
-            kdDebug () << "Washing pixmap (w=" << rect.width ()
-                       << ",h=" << rect.height () << ")" << endl;
-        }
+        kdDebug () << "kpToolPen::draw() interpolate: area (w=" << rect.width ()
+                    << ",h=" << rect.height () << ")" << endl;
         QTime timer;
         int convAndWashTime;
     #endif
         
-        QPixmap pixmap = document ()->getPixmapAt (rect);
-        QPainter painter (&pixmap);
+        // optimsation - only render intersections of rectangles once
+        bool delayedDraw = ((m_mode & SquareBrushes) &&
+                                ((m_mode & WashesPixmaps) ||
+                                (m_mode == Eraser)/*solid rectangular brush*/));
+            
+            
+        QPixmap pixmap;
+        QPainter painter;
+        pixmap = document ()->getPixmapAt (rect);
+        painter.begin (&pixmap);
         painter.setPen (color (m_mouseButton));
 
-        TQImage image;
-        if (m_mode & WashesPixmaps)
-        {
         #if DEBUG_KP_TOOL_PEN
+        if (m_mode & WashesPixmaps)
             timer.start ();
         #endif
-            image = pixmap.convertToImage ();
-        #if DEBUG_KP_TOOL_PEN
-            convAndWashTime = timer.restart ();
-            kdDebug () << "\tconvert to image: " << convAndWashTime << " ms" << endl;    
-        #endif
-        }
         
         bool didSomething = false;
 
@@ -453,10 +451,21 @@ void kpToolPen::draw (const QPoint &this
         else if (m_mode & (DrawsPixmaps | WashesPixmaps))
         {
             TQRgb colorToReplace;
+            TQImage image;
+            QRegion region;
             
             if (m_mode & WashesPixmaps)
+            {
                 colorToReplace = color (1 - m_mouseButton).rgb ();
 
+                image = pixmap.convertToImage ();
+        
+            #if DEBUG_KP_TOOL_PEN
+                convAndWashTime = timer.restart ();
+                kdDebug () << "\tconvert to image: " << convAndWashTime << " ms" << endl;    
+            #endif
+            }
+
             // Sweeps a pixmap along a line (modified Bresenham's line algorithm,
             // see MODIFIED comment below).
             //
@@ -485,19 +494,27 @@ void kpToolPen::draw (const QPoint &this
             int x = 0;
             int y = 0;
 
-            if (m_mode & WashesPixmaps)
+            if (delayedDraw)
             {
-                if (wash (&painter, image,
-                          colorToReplace,
-                          rect, plotx + rect.left (), ploty + rect.top ()))
-                {
-                    didSomething = true;
-                }
+                region = region.unite (hotRect (plotx + rect.left (), ploty + rect.top ()));
             }
             else
             {
-                painter.drawPixmap (hotPoint (plotx, ploty), m_brushPixmap [m_mouseButton]);
-                didSomething = true;
+                if (m_mode & WashesPixmaps)
+                {
+                    if (wash (&painter, image,
+                              colorToReplace,
+                              rect, plotx + rect.left (), ploty + rect.top ()))
+                    {
+                        didSomething = true;
+                    }
+                }
+                else
+                {
+                    painter.drawPixmap (hotPoint (plotx, ploty),
+                                        m_brushPixmap [m_mouseButton]);
+                    didSomething = true;
+                }
             }
 
             for (int i = 0; i <= inc; i++)
@@ -541,39 +558,115 @@ void kpToolPen::draw (const QPoint &this
                         // is more than 1 point, of course).  This is in contrast to the
                         // ordinary line algorithm which can create diagonal adjacencies.
 
+                        if (delayedDraw)
+                        {
+                            region = region.unite (hotRect (plotx + rect.left (), oldploty + rect.top ()));
+                        }
+                        else
+                        {
+                            if (m_mode & WashesPixmaps)
+                            {
+                                if (wash (&painter, image,
+                                          colorToReplace,
+                                          rect, plotx + rect.left (), oldploty + rect.top ()))
+                                {
+                                    didSomething = true;
+                                }
+                            }
+                            else
+                            {
+                                painter.drawPixmap (hotPoint (plotx, oldploty),
+                                                    m_brushPixmap [m_mouseButton]);
+                                didSomething = true;
+                            }
+                        }
+                    }
+
+                    if (delayedDraw)
+                    {
+                        region = region.unite (hotRect (plotx + rect.left (), ploty + rect.top ()));
+                    }
+                    else
+                    {
                         if (m_mode & WashesPixmaps)
                         {
                             if (wash (&painter, image,
                                       colorToReplace,
-                                      rect, plotx + rect.left (), oldploty + rect.top ()))
+                                      rect, plotx + rect.left (), ploty + rect.top ()))
                             {
                                 didSomething = true;
                             }
                         }
                         else
                         {
-                            painter.drawPixmap (hotPoint (plotx, oldploty), m_brushPixmap [m_mouseButton]);
+                            painter.drawPixmap (hotPoint (plotx, ploty),
+                                                m_brushPixmap [m_mouseButton]);
                             didSomething = true;
                         }
                     }
-
+                }
+            }
+            
+            if (delayedDraw)
+            {
+                QMemArray <QRect> rects = region.rects ();
+                
+                int numRects = rects.count ();
+            #if DEBUG_KP_TOOL_PEN
+                kdDebug () << "\tdelayed draw now happening: numRects="
+                           << numRects << endl;
+                int convImageMS = 0;
+                int washMS = 0;
+                int setDocMS = 0;
+                QTime timer;
+            #endif
+                for (int i = 0; i < numRects; i++)
+                {
+                    QRect r = rects [i];
+                    QPixmap pm = document ()->getPixmapAt (r);
+                #if DEBUG_KP_TOOL_PEN && 0
+                    kdDebug () << "\tr=" << r << endl;
+                #endif
+            
+                    bool drew = false;
+                    
                     if (m_mode & WashesPixmaps)
                     {
+                        timer.start ();
+                        
                         if (wash (&painter, image,
                                   colorToReplace,
-                                  rect, plotx + rect.left (), ploty + rect.top ()))
+                                  rect, r))
                         {
-                            didSomething = true;
+                            drew = true;
                         }
+                        washMS += timer.restart ();
                     }
                     else
                     {
-                        painter.drawPixmap (hotPoint (plotx, ploty), m_brushPixmap [m_mouseButton]);
+                        painter.setBrush (color (m_mouseButton));
+                        painter.drawRect (r.x () - rect.x (),
+                                          r.y () - rect.y (),
+                                          r.width (),
+                                          r.height ());
+                        drew = true;
+                    }
+                    
+                    if (drew)
+                    {
+                        m_currentCommand->updateBoundingRect (r);
                         didSomething = true;
+                        setDocMS += timer.restart ();
                     }
                 }
+                
+            #if DEBUG_KP_TOOL_PEN
+                kdDebug () << "convImageMS=" << convImageMS
+                           << " washMS=" << washMS
+                           << " setDocMS=" << setDocMS
+                           << endl;
+            #endif
             }
-
         }
 
         painter.end ();