-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImpressionistDoc.cpp
246 lines (198 loc) · 6.95 KB
/
ImpressionistDoc.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
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
//
// 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 "ScatteredPointBrush.h"
#include "ScatteredLineBrush.h"
#include "ScatteredCircleBrush.h"
#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;
// 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 ScatteredPointBrush( this, "Scattered Points" );
ImpBrush::c_pBrushes[BRUSH_SCATTERED_LINES] = new ScatteredLineBrush( this, "Scattered Lines" );
ImpBrush::c_pBrushes[BRUSH_SCATTERED_CIRCLES] = new ScatteredCircleBrush( this, "Scattered Circles" );
// make one of the brushes current
m_pCurrentBrush = ImpBrush::c_pBrushes[0];
m_pCurrentDirection = 0;
}
//---------------------------------------------------------
// 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];
if (type != BRUSH_LINES && type != BRUSH_SCATTERED_LINES) {
m_pUI->m_strokeTypeChoice->deactivate();
m_pUI->m_LineWidthSlider->deactivate();
m_pUI->m_LineAngleSlider->deactivate();
}
else {
m_pUI->m_strokeTypeChoice->activate();
m_pUI->m_LineWidthSlider->activate();
m_pUI->m_LineAngleSlider->activate();
}
}
//---------------------------------------------------------
// Called by the UI when the user changes the stroke direction.
// type: one of the defined stroke directions.
//---------------------------------------------------------
void ImpressionistDoc::setStrokeDirection(int type)
{
m_pCurrentDirection = type;
}
//---------------------------------------------------------
// Returns the size of the brush.
//---------------------------------------------------------
int ImpressionistDoc::getSize()
{
return m_pUI->getSize();
}
//---------------------------------------------------------
// Returns the width of the brush.
//---------------------------------------------------------
int ImpressionistDoc::getLineWidth()
{
return m_pUI->getLineWidth();
}
//---------------------------------------------------------
// Returns the angle of the brush.
//---------------------------------------------------------
int ImpressionistDoc::getLineAngle()
{
return m_pUI->getLineAngle();
}
//---------------------------------------------------------
// Returns the alpha of the brush.
//---------------------------------------------------------
float ImpressionistDoc::getAlpha()
{
return m_pUI->getAlpha();
}
//---------------------------------------------------------
// 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);
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;
}
//----------------------------------------------------------------
// 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);
// 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 );
}