-
Notifications
You must be signed in to change notification settings - Fork 3
/
OriginalView.cpp
183 lines (152 loc) · 4.01 KB
/
OriginalView.cpp
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
//
// originalview.cpp
//
// The code maintaining the original view of the input images
//
#include "impressionist.h"
#include "impressionistDoc.h"
#include "originalview.h"
#include <functional>
#include "impressionistUI.h"
#ifndef WIN32
#define min(a, b) ( ( (a)<(b) ) ? (a) : (b) )
#endif
extern Point CalGradient(const Point source, const Point target, const std::function<GLubyte*(int, int)> getPixel);
OriginalView::OriginalView(int x,
int y,
int w,
int h,
const char* l)
: Fl_Gl_Window(x,y,w,h,l)
{
m_nWindowWidth = w;
m_nWindowHeight = h;
}
void OriginalView::draw()
{
if(!valid())
{
glClearColor(0.7f, 0.7f, 0.7f, 1.0);
// We're only using 2-D, so turn off depth
glDisable( GL_DEPTH_TEST );
// Tell openGL to read from the front buffer when capturing
// out paint strokes
glReadBuffer( GL_FRONT );
ortho();
}
glClear( GL_COLOR_BUFFER_BIT );
int drawWidth, drawHeight;
if ( m_pDoc->m_ucBitmap )
{
// note that both OpenGL pixel storage and the Windows BMP format
// store pixels left-to-right, BOTTOM-to-TOP!! thus all the fiddling
// around with startrow.
m_nWindowWidth=w();
m_nWindowHeight=h();
GLvoid* bitstart;
// we are not using a scrollable window, so ignore it
Point scrollpos; // = GetScrollPosition();
scrollpos.x=scrollpos.y=0;
drawWidth = min( m_nWindowWidth, m_pDoc->m_nWidth );
drawHeight = min( m_nWindowHeight, m_pDoc->m_nHeight );
int startrow = m_pDoc->m_nHeight - (scrollpos.y + drawHeight);
if ( startrow < 0 )
startrow = 0;
// if (willFindEdge)
// {
// findEdge();
// willFindEdge = false;
//
// glPixelStorei(GL_PACK_ALIGNMENT, 1);
// glPixelStorei(GL_PACK_ROW_LENGTH, m_pDoc->m_nPaintWidth);
//
// glReadPixels(0,
// m_nWindowHeight - drawHeight,
// drawWidth,
// drawHeight,
// GL_RGB,
// GL_UNSIGNED_BYTE,
// m_pDoc->m_ucEdge + 3 * ((m_pDoc->m_nWidth * startrow) + scrollpos.x));
// glFlush();
// }
if(willFindEdge)
{
findEdge();
willFindEdge = false;
}
bitstart = m_pDoc->m_ucBitmap + 3 * ((m_pDoc->m_nWidth * startrow) + scrollpos.x);
// just copy image to GLwindow conceptually
glRasterPos2i( 0, m_nWindowHeight - drawHeight );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glPixelStorei( GL_UNPACK_ROW_LENGTH, m_pDoc->m_nWidth );
glDrawBuffer( GL_BACK );
glDrawPixels( drawWidth, drawHeight, GL_RGB, GL_UNSIGNED_BYTE, bitstart );
if(cursor.x >= 0 && cursor.y >= 0 && cursor.x < m_nWindowWidth && cursor.y < m_nWindowHeight)
{
displayCursor();
}
}
glFlush();
}
void OriginalView::refresh()
{
redraw();
}
void OriginalView::resizeWindow(int width,
int height)
{
resize(x(), y(), width, height);
}
void OriginalView::setCursor(const Point pt)
{
cursor = pt;
redraw();
}
void OriginalView::displayCursor()
{
glPointSize(5);
glBegin(GL_POINTS);
glColor3d(1, 0, 0);
glVertex2d(cursor.x, m_nWindowHeight - cursor.y);
glEnd();
}
void OriginalView::findEdge()
{
const int threshold = m_pDoc->m_pUI->getEdgeThreshold();
// glPointSize(1);
// glBegin(GL_POINTS);
for(int i=0;i < m_nWindowWidth; i++)
{
for(int j=0; j<m_nWindowHeight; j++)
{
Point grad = CalGradient(Point(i, j), Point(i, j), [&](int x, int y) {return m_pDoc->GetOriginalPixel(x, y); });
// if(grad.x !=0 || grad.y != 0)
// {
// printf("hey");
const bool color = sqrt(pow(grad.x, 2) + pow(grad.y, 2)) > threshold;
if(color)
{
// glColor3d(color, color, color);
// glVertex2d(i, j);
m_pDoc->m_ucEdge[(j*m_nWindowWidth + i) * 3] = 255;
m_pDoc->m_ucEdge[(j*m_nWindowWidth + i) * 3 + 1] = 255;
m_pDoc->m_ucEdge[(j*m_nWindowWidth + i) * 3 + 2] = 255;
}
else
{
m_pDoc->m_ucEdge[(j*m_nWindowWidth + i) * 3] = 0;
m_pDoc->m_ucEdge[(j*m_nWindowWidth + i) * 3 + 1] = 0;
m_pDoc->m_ucEdge[(j*m_nWindowWidth + i) * 3 + 2] = 0;
}
// }
}
}
// glEnd();
// glFlush();
}
void OriginalView::prepareFindEdge()
{
m_pDoc->m_ucBitmap = m_pDoc->m_ucEdge;
willFindEdge = true;
refresh();
}