-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImpressionistDoc.cpp
More file actions
381 lines (303 loc) · 10.3 KB
/
ImpressionistDoc.cpp
File metadata and controls
381 lines (303 loc) · 10.3 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//
// impressionistDoc.cpp
//
// It basically maintain the bitmap for answering the color query from the brush.
// It also acts as the bridge between brushes and UI (including views)
//
#include <FL/fl_ask.H>
#include "ImpressionistDoc.h"
#include "ImpressionistUI.h"
#include "ImpBrush.h"
// Include individual brush headers here.
#include "PointBrush.h"
#include "LineBrush.h"
#include "CircleBrush.h"
#include "ScatterPointBrush.h"
#include "ScatterLineBrush.h"
#include "ScatterCircleBrush.h"
#include "PressureBrush.h"
#include "BlurBrush.h"
#include <iostream>
using namespace std;
#define DESTROY(p) { if ((p)!=NULL) {delete [] p; p=NULL; } }
ImpressionistDoc::ImpressionistDoc()
{
// Set NULL image name as init.
m_imageName[0] ='\0';
m_nWidth = -1;
m_ucBitmap = NULL;
m_ucPainting = NULL;
// cursor array initialization
CursorHisPos = new Vector*[2];
CursorHisPos[0] = new Vector(0, 0);
CursorHisPos[1] = new Vector(0, 0);
rightMousePos = new Vector*[2];
rightMousePos[0] = new Vector(0, 0);
rightMousePos[1] = new Vector(0, 0);
// create one instance of each brush
ImpBrush::c_nBrushCount = NUM_BRUSH_TYPE;
ImpBrush::c_pBrushes = new ImpBrush* [ImpBrush::c_nBrushCount];
ImpBrush::c_pBrushes[BRUSH_POINTS] = new PointBrush( this, "Points" );
// Note: You should implement these 5 brushes. They are set the same (PointBrush) for now
ImpBrush::c_pBrushes[BRUSH_LINES]
= new LineBrush( this, "Lines" );
ImpBrush::c_pBrushes[BRUSH_CIRCLES]
= new CircleBrush( this, "Circles" );
ImpBrush::c_pBrushes[BRUSH_SCATTERED_POINTS]
= new ScatterPointBrush( this, "Scattered Points" );
ImpBrush::c_pBrushes[BRUSH_SCATTERED_LINES]
= new ScatterLineBrush( this, "Scattered Lines" );
ImpBrush::c_pBrushes[BRUSH_SCATTERED_CIRCLES]
= new ScatterCircleBrush( this, "Scattered Circles" );
ImpBrush::c_pBrushes[BRUSH_PRESSURE]
= new PressureBrush(this, "Pressure");
ImpBrush::c_pBrushes[BRUSH_BLUR]
= new BlurBrush(this, "Blur");
ImpBrush::c_pBrushes[BRUSH_SHARPEN]
= new BlurBrush(this, "Sharpen");
// make one of the brushes current
m_pCurrentBrush = ImpBrush::c_pBrushes[0];
this->blurFilter = new GLuFilter(blurMatrix);
this->sharpenFilter = new GLuFilter(sharpenMatrix);
// Init history manager
this->historyManager = new HistoryManager();
}
//---------------------------------------------------------
// Set the current UI
//---------------------------------------------------------
void ImpressionistDoc::setUI(ImpressionistUI* ui)
{
m_pUI = ui;
}
//---------------------------------------------------------
// Returns the active picture/painting name
//---------------------------------------------------------
char* ImpressionistDoc::getImageName()
{
return m_imageName;
}
//---------------------------------------------------------
// Called by the UI when the user changes the brush type.
// type: one of the defined brush types.
//---------------------------------------------------------
void ImpressionistDoc::setBrushType(int type)
{
m_pCurrentBrush = ImpBrush::c_pBrushes[type];
}
//---------------------------------------------------------
// Called by the UI when the user changes the stroke direction.
// type: one of the stroke directions
//---------------------------------------------------------
void ImpressionistDoc::setStrokeDirection(int type)
{
m_pCurrentDirection = type;
}
//---------------------------------------------------------
// Returns the size of the brush.
//---------------------------------------------------------
int ImpressionistDoc::getSize()
{
return m_pUI->getSize();
}
//---------------------------------------------------------
// Load the specified image
// This is called by the UI when the load image button is
// pressed.
//---------------------------------------------------------
int ImpressionistDoc::loadImage(char *iname)
{
// try to open the image to read
unsigned char* data;
int width,
height;
if ( (data=readBMP(iname, width, height))==NULL )
{
fl_alert("Can't load bitmap file");
return 0;
}
// reflect the fact of loading the new image
m_nWidth = width;
m_nPaintWidth = width;
m_nHeight = height;
m_nPaintHeight = height;
// release old storage
if ( m_ucBitmap ) delete [] m_ucBitmap;
if ( m_ucPainting ) delete [] m_ucPainting;
m_ucBitmap = data;
// allocate space for draw view
m_ucPainting = new unsigned char [width*height*3];
memset(m_ucPainting, 0, width*height*3);
this->historyManager->pushHistoryBitmap(m_ucPainting, m_nPaintWidth, m_nPaintHeight);
m_pUI->m_mainWindow->resize(m_pUI->m_mainWindow->x(),
m_pUI->m_mainWindow->y(),
width*2,
height+25);
// display it on origView
m_pUI->m_origView->resizeWindow(width, height);
m_pUI->m_origView->refresh();
// refresh paint view as well
m_pUI->m_paintView->resizeWindow(width, height);
m_pUI->m_paintView->refresh();
return 1;
}
//----------------------------------------------------------------
// Save the specified image
// This is called by the UI when the save image menu button is
// pressed.
//----------------------------------------------------------------
int ImpressionistDoc::saveImage(char *iname)
{
writeBMP(iname, m_nPaintWidth, m_nPaintHeight, m_ucPainting);
return 1;
}
//---------------------------------------------------------
// Mural the specified image
// This is called by the UI when the mural image button is
// pressed.
//---------------------------------------------------------
int ImpressionistDoc::muralImage(char *iname)
{
// try to open the image to read
unsigned char* data;
int width, height;
if ( (data=readBMP(iname, width, height))==NULL )
{
fl_alert("Can't load bitmap file");
return 0;
}
int tempWidth = m_nPaintWidth;
int tempHeight = m_nPaintHeight;
int tempSize = m_nPaintHeight * m_nPaintWidth * 3;
unsigned char* m_ucTempPainting = m_ucPainting;
// reflect the fact of loading the new image
m_nWidth = width;
m_nPaintWidth = width;
m_nHeight = height;
m_nPaintHeight = height;
// release old storage
if ( m_ucBitmap ) delete [] m_ucBitmap;
// if ( m_ucPainting ) delete [] m_ucPainting;
m_ucBitmap = data;
// allocate space for draw view
m_ucPainting = new unsigned char [width*height*3];
memset(m_ucPainting, 0, width*height*3);
for (int i = 0; i < tempSize; i++) {
int cur_Row = i / (3 * tempWidth);
int cur_Col = i - 3 * tempWidth * cur_Row;
m_ucPainting[cur_Row * 3 * width + cur_Col] = m_ucTempPainting[i];
}
m_pUI->m_mainWindow->resize(m_pUI->m_mainWindow->x(),
m_pUI->m_mainWindow->y(),
width*2,
height+25);
// display it on origView
m_pUI->m_origView->resizeWindow(width, height);
m_pUI->m_origView->refresh();
// refresh paint view as well
m_pUI->m_paintView->resizeWindow(width, height);
m_pUI->m_paintView->refresh();
return 1;
}
//----------------------------------------------------------------
// Clear the drawing canvas
// This is called by the UI when the clear canvas menu item is
// chosen
//-----------------------------------------------------------------
int ImpressionistDoc::clearCanvas()
{
// Release old storage
if ( m_ucPainting )
{
delete [] m_ucPainting;
// allocate space for draw view
m_ucPainting = new unsigned char [m_nPaintWidth*m_nPaintHeight*3];
memset(m_ucPainting, 0, m_nPaintWidth*m_nPaintHeight*3);
this->historyManager->pushHistoryBitmap(m_ucPainting, m_nPaintWidth, m_nPaintHeight);
// refresh paint view as well
m_pUI->m_paintView->refresh();
}
return 0;
}
//------------------------------------------------------------------
// Get the color of the pixel in the original image at coord x and y
//------------------------------------------------------------------
GLubyte* ImpressionistDoc::GetOriginalPixel( int x, int y )
{
if ( x < 0 )
x = 0;
else if ( x >= m_nWidth )
x = m_nWidth-1;
if ( y < 0 )
y = 0;
else if ( y >= m_nHeight )
y = m_nHeight-1;
return (GLubyte*)(m_ucBitmap + 3 * (y*m_nWidth + x));
}
//----------------------------------------------------------------
// Get the color of the pixel in the original image at point p
//----------------------------------------------------------------
GLubyte* ImpressionistDoc::GetOriginalPixel( const Point p )
{
return GetOriginalPixel( p.x, p.y );
}
/**
//----------------------------------------------------------------
// Automatically paint the canvas
// This is called by the UI when the auto-paint canvas menu item is
// chosen
//-----------------------------------------------------------------
void ImpressionistDoc::autoPaint()
{
clearCanvas();
cout<<"To be implemented"<<endl;
GLvoid* m_pPaintBitstart;
int m_nWindowHeight, m_nDrawHeight, m_nDrawWidth;
m_nWindowHeight = m_nPaintHeight;
m_nDrawHeight = m_nPaintHeight;
m_nDrawWidth = m_nPaintWidth;
int startrow = m_nPaintHeight - m_nPaintHeight;
m_pPaintBitstart = m_ucPainting + 3 * (m_nPaintWidth * startrow);
for (int x = 1; x < 10 ; x += 10) { // m_nPaintWidth
for (int y = 1; y < 10 ; y += 10) { // m_nPaintHeight
Point source(x, y);
Point end(x+20, y+20);
cout<< x << " , "<<y<<endl;
m_pCurrentBrush->BrushBegin( source, source );
m_pCurrentBrush->BrushMove( end, end );
// SaveCurrentContent();
// Tell openGL to read from the front buffer when capturing
// out paint strokes
// glReadBuffer(GL_FRONT);
// glPixelStorei( GL_PACK_ALIGNMENT, 1 );
// glPixelStorei( GL_PACK_ROW_LENGTH, m_nPaintWidth );
// glReadPixels( 0,
// m_nWindowHeight - m_nDrawHeight,
// m_nDrawWidth,
// m_nDrawHeight,
// GL_RGB,
// GL_UNSIGNED_BYTE,
// m_pPaintBitstart );
// // RestoreContent();
// glDrawBuffer(GL_BACK);
// glClear( GL_COLOR_BUFFER_BIT );
// glRasterPos2i( 0, m_nWindowHeight - m_nDrawHeight );
// glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
// glPixelStorei( GL_UNPACK_ROW_LENGTH, m_nPaintWidth );
// glDrawPixels( m_nDrawWidth,
// m_nDrawHeight,
// GL_RGB,
// GL_UNSIGNED_BYTE,
// m_pPaintBitstart);
// m_pCurrentBrush->BrushEnd( end, end );
}
}
//for (X, Y) in a set of values:
// Point source( 100 + m_nStartCol, m_nEndRow - 100 );
// Point target( 100, m_nWindowHeight - 100 );
// m_pCurrentBrush->BrushBegin( source, target );
// m_pCurrentBrush->BrushMove( source, target );
// // SaveCurrentContent();
// // RestoreContent();
// m_pCurrentBrush->BrushEnd( source, target );
}
*/