File: image.cpp

package info (click to toggle)
fox 1.0.52-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,788 kB
  • ctags: 13,384
  • sloc: cpp: 96,482; sh: 8,338; ansic: 1,935; makefile: 1,010; perl: 32
file content (351 lines) | stat: -rw-r--r-- 10,242 bytes parent folder | download
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
/********************************************************************************
*                                                                               *
*                                   Image Test                                  *
*                                                                               *
********************************************************************************/
#include "fx.h"


FXuchar grey_ramp[512*50*3];                 // Created images
FXuchar red_ramp[512*50*3];
FXuchar green_ramp[512*50*3];
FXuchar blue_ramp[512*50*3];


// Event Handler Object
class ImageWindow : public FXMainWindow {

  // Macro for class hierarchy declarations
  FXDECLARE(ImageWindow)

private:

  FXCanvas        *canvas;                    // Canvas to draw into
  FXColorWell     *backwell;                  // Color Well for background
  FXColorWell     *borderwell;                // Color Well for border
  FXColorWell     *textwell;                  // Color Well for text
  FXImage         *grey;
  FXImage         *red;
  FXImage         *green;
  FXImage         *blue;
  FXImage         *grey_nodither;
  FXImage         *red_nodither;
  FXImage         *green_nodither;
  FXImage         *blue_nodither;
  FXBMPImage      *picture;                   // Complete picture
  FXFont          *font;                      // Font for text

protected:
  ImageWindow(){}

public:

  // Message handlers
  long onCanvasRepaint(FXObject*,FXSelector,void*);
  long onCmdWell(FXObject*,FXSelector,void*);
  long onCmdRestore(FXObject*,FXSelector,void*);

public:

  // Messages for our class
  enum{
    ID_CANVAS=FXMainWindow::ID_LAST,
    ID_WELL,
    ID_RESTORE,
    ID_LAST
    };

public:

  // ImageWindow constructor
  ImageWindow(FXApp* a);

  // Initialize
  virtual void create();
  
  // ImageWindow destructor
  virtual ~ImageWindow();
  };



// Message Map for the Scribble App class
FXDEFMAP(ImageWindow) ImageWindowMap[]={

  //____Message_Type______________ID_______________Message_Handler___
  FXMAPFUNC(SEL_PAINT,   ImageWindow::ID_CANVAS,  ImageWindow::onCanvasRepaint),
  FXMAPFUNC(SEL_COMMAND, ImageWindow::ID_WELL,    ImageWindow::onCmdWell),
  FXMAPFUNC(SEL_COMMAND, ImageWindow::ID_RESTORE, ImageWindow::onCmdRestore),
  };



// Macro for the ScribbleApp class hierarchy implementation
FXIMPLEMENT(ImageWindow,FXMainWindow,ImageWindowMap,ARRAYNUMBER(ImageWindowMap))



// Construct ImageWindow
ImageWindow::ImageWindow(FXApp* a):FXMainWindow(a,"Image Application",NULL,NULL,DECOR_ALL,0,0,800,600){
  FXint x,y;
  FXVerticalFrame *canvasFrame;
  FXVerticalFrame *buttonFrame;
  FXHorizontalFrame *contents;

  FXColorDialog *colordlg=new FXColorDialog(this,"Color Dialog");

  contents=new FXHorizontalFrame(this,LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y,0,0,0,0, 0,0,0,0);

  // LEFT pane to contain the canvas
  canvasFrame=new FXVerticalFrame(contents,FRAME_SUNKEN|LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_TOP|LAYOUT_LEFT,0,0,0,0,10,10,10,10);

    // Label above the canvas
    new FXLabel(canvasFrame,"Canvas Frame",NULL,JUSTIFY_CENTER_X|LAYOUT_FILL_X);

    // Horizontal divider line
    new FXHorizontalSeparator(canvasFrame,SEPARATOR_GROOVE|LAYOUT_FILL_X);

    // Drawing canvas
    canvas=new FXCanvas(canvasFrame,this,ID_CANVAS,FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_TOP|LAYOUT_LEFT);


  // RIGHT pane for the buttons
  buttonFrame=new FXVerticalFrame(contents,FRAME_SUNKEN|LAYOUT_FILL_Y|LAYOUT_TOP|LAYOUT_LEFT,0,0,0,0,10,10,10,10);

    // Label above the buttons
    new FXLabel(buttonFrame,"Button Frame",NULL,JUSTIFY_CENTER_X|LAYOUT_FILL_X);

    // Horizontal divider line
    new FXHorizontalSeparator(buttonFrame,SEPARATOR_RIDGE|LAYOUT_FILL_X);

    new FXLabel(buttonFrame,"&Background\nColor well",NULL,JUSTIFY_CENTER_X|LAYOUT_FILL_X);
    backwell=new FXColorWell(buttonFrame,FXRGB(255,255,255),this,ID_WELL,LAYOUT_CENTER_X|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT,0,0,100,30);

    new FXLabel(buttonFrame,"B&order\nColor well",NULL,JUSTIFY_CENTER_X|LAYOUT_FILL_X);
    borderwell=new FXColorWell(buttonFrame,FXRGB(0,0,0),this,ID_WELL,LAYOUT_CENTER_X|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT,0,0,100,30);

    new FXLabel(buttonFrame,"&Text\nColor well",NULL,JUSTIFY_CENTER_X|LAYOUT_FILL_X);
    textwell=new FXColorWell(buttonFrame,FXRGB(0,0,0),this,ID_WELL,LAYOUT_CENTER_X|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT,0,0,100,30);

    // Button to draw
    new FXButton(buttonFrame,"&Colors...\tPop the color dialog",NULL,colordlg,FXWindow::ID_SHOW,FRAME_THICK|FRAME_RAISED|LAYOUT_FILL_X|LAYOUT_TOP|LAYOUT_LEFT,0,0,0,0,10,10,5,5);

    // Button to draw
    new FXButton(buttonFrame,"Save Image...\tRead back image and save to file",NULL,this,ID_RESTORE,FRAME_THICK|FRAME_RAISED|LAYOUT_FILL_X|LAYOUT_TOP|LAYOUT_LEFT,0,0,0,0,10,10,5,5);

    // Exit button
    new FXButton(buttonFrame,"E&xit\tQuit ImageApp",NULL,getApp(),FXApp::ID_QUIT,FRAME_THICK|FRAME_RAISED|LAYOUT_FILL_X|LAYOUT_TOP|LAYOUT_LEFT,0,0,0,0,10,10,5,5);

  // Create images with dithering
  grey=new FXImage(getApp(),grey_ramp,IMAGE_DITHER|IMAGE_SHMI|IMAGE_SHMP,512,50);
  red=new FXImage(getApp(),red_ramp,IMAGE_DITHER|IMAGE_SHMI|IMAGE_SHMP,512,50);
  green=new FXImage(getApp(),green_ramp,IMAGE_DITHER|IMAGE_SHMI|IMAGE_SHMP,512,50);
  blue=new FXImage(getApp(),blue_ramp,IMAGE_DITHER|IMAGE_SHMI|IMAGE_SHMP,512,50);

  // Create image with nearest color instead of dithering
  grey_nodither=new FXImage(getApp(),grey_ramp,IMAGE_NEAREST|IMAGE_SHMI|IMAGE_SHMP,512,50);
  red_nodither=new FXImage(getApp(),red_ramp,IMAGE_NEAREST|IMAGE_SHMI|IMAGE_SHMP,512,50);
  green_nodither=new FXImage(getApp(),green_ramp,IMAGE_NEAREST|IMAGE_SHMI|IMAGE_SHMP,512,50);
  blue_nodither=new FXImage(getApp(),blue_ramp,IMAGE_NEAREST|IMAGE_SHMI|IMAGE_SHMP,512,50);

  // Result image
  picture=new FXBMPImage(getApp(),NULL,IMAGE_SHMI|IMAGE_SHMP,850,600);
  //picture=new FXBMPImage(getApp(),NULL,0,850,600);

  // Fill the ramps
  for(x=0; x<512; x++){
    for(y=0; y<50; y++){
      grey_ramp[3*(y*512+x)]=x/2;
      grey_ramp[3*(y*512+x)+1]=x/2;
      grey_ramp[3*(y*512+x)+2]=x/2;
      }
    for(y=0; y<50; y++){
      red_ramp[3*(y*512+x)]=x/2;
      red_ramp[3*(y*512+x)+1]=0;
      red_ramp[3*(y*512+x)+2]=0;
      }
    for(y=0; y<50; y++){
      green_ramp[3*(y*512+x)]=0;
      green_ramp[3*(y*512+x)+1]=x/2;
      green_ramp[3*(y*512+x)+2]=0;
      }
    for(y=0; y<50; y++){
      blue_ramp[3*(y*512+x)]=0;
      blue_ramp[3*(y*512+x)+1]=0;
      blue_ramp[3*(y*512+x)+2]=x/2;
      }
    }

  // Make font
  font=new FXFont(getApp(),"times",36,FONTWEIGHT_BOLD);

  // Make a tip
  new FXTooltip(getApp());
  }



// Destroy ImageWindow
ImageWindow::~ImageWindow(){
  delete grey;
  delete red;
  delete green;
  delete blue;
  delete grey_nodither;
  delete red_nodither;
  delete green_nodither;
  delete blue_nodither;
  delete picture;
  delete font;
  }


// Create and initialize
void ImageWindow::create(){

  // Create the windows
  FXMainWindow::create();

  // Create images
  grey->create();
  red->create();
  green->create();
  blue->create();
  grey_nodither->create();
  red_nodither->create();
  green_nodither->create();
  blue_nodither->create();

  picture->create();

  // Font too
  font->create();

  // Make it appear
  show(PLACEMENT_SCREEN);

  // First time repaint
  canvas->update();

  }


// Handle the clear message
long ImageWindow::onCanvasRepaint(FXObject*,FXSelector,void* ptr){
  FXEvent *event=(FXEvent*)ptr;
  FXuint pat;

  // We caused a redraw, so redo it all
  if(event->synthetic){
    FXDCWindow dc(picture);

    // Erase the canvas, color comes from well
    dc.setForeground(backwell->getRGBA());

    dc.fillRectangle(0,0,picture->getWidth(),picture->getHeight());

    // Draw images
    dc.drawImage(grey,10,10);
    dc.drawImage(grey_nodither,10,60);
    dc.drawImage(red,10,130);
    dc.drawImage(red_nodither,10,180);
    dc.drawImage(green,10,250);
    dc.drawImage(green_nodither,10,300);
    dc.drawImage(blue,10,370);
    dc.drawImage(blue_nodither,10,420);

    // Draw patterns
    dc.setFillStyle(FILL_OPAQUESTIPPLED);
    dc.setForeground(FXRGB(0,0,0));
    dc.setBackground(FXRGB(255,255,255));
    for(pat=STIPPLE_0; pat<=STIPPLE_16; pat+=1){
      dc.setStipple((FXStipplePattern)pat);
      dc.fillRectangle(10+(512*pat)/17,490,31,50);
      }
    dc.setFillStyle(FILL_SOLID);

    // Draw borders
    dc.setForeground(borderwell->getRGBA());
    dc.drawRectangle(10,10,512,50);
    dc.drawRectangle(10,60,512,50);

    dc.drawRectangle(10,130,512,50);
    dc.drawRectangle(10,180,512,50);

    dc.drawRectangle(10,250,512,50);
    dc.drawRectangle(10,300,512,50);

    dc.drawRectangle(10,370,512,50);
    dc.drawRectangle(10,420,512,50);

    dc.drawRectangle(10,490,512,50);

    // Draw text
    dc.setTextFont(font);
    dc.setForeground(textwell->getRGBA());
    dc.drawText(540,60,"Grey",4);
    dc.drawText(540,180,"Red",3);
    dc.drawText(540,300,"Green",5);
    dc.drawText(540,420,"Blue",4);
    dc.drawText(540,540,"Patterns",8);
    }

  // Now repaint the screen
  FXDCWindow sdc(canvas,event);

  // Clear whole thing
  sdc.setForeground(backwell->getRGBA());
  sdc.fillRectangle(0,0,canvas->getWidth(),canvas->getHeight());

  // Paint image
  sdc.drawImage(picture,0,0);

  return 1;
  }


// Color well got changed
long ImageWindow::onCmdWell(FXObject*,FXSelector,void*){
  canvas->update();
  return 1;
  }


// Restore image from off-screen pixmap
long ImageWindow::onCmdRestore(FXObject*,FXSelector,void*){
  FXFileDialog savedialog(this,"Save BMP");
  savedialog.setDirectory(".");
  if(savedialog.execute()){
    FXFileStream outfile;
    if(outfile.open(savedialog.getFilename(),FXStreamSave)){
      picture->restore();
      picture->savePixels(outfile);
      outfile.close();
      }
    }
  return 1;
  }


// Here we begin
int main(int argc,char *argv[]){

  // Make application
  FXApp application("Image","FoxText");

  // Start app
  application.init(argc,argv);

  // Make window
  new ImageWindow(&application);

  // Create the application's windows
  application.create();

  // Run the application
  return application.run();
  }