Merge lp:~myro-cpp-dev/myro-c++/smart_pointers into lp:myro-c++
- smart_pointers
- Merge into trunk
Proposed by
John Hoare
Status: | Merged | ||||
---|---|---|---|---|---|
Merged at revision: | 88 | ||||
Proposed branch: | lp:~myro-cpp-dev/myro-c++/smart_pointers | ||||
Merge into: | lp:myro-c++ | ||||
Diff against target: |
2055 lines (+688/-605) 22 files modified
headers/CMakeLists.txt (+1/-1) headers/ColorPicture.h (+0/-38) headers/Filter.h (+2/-2) headers/Graphics.h (+109/-37) headers/GrayPicture.h (+0/-36) headers/MyroSmartPointer.h (+58/-0) headers/Picture.h (+27/-19) headers/Scribbler.h (+1/-1) src/CMakeLists.txt (+1/-1) src/ColorPicture.cpp (+0/-82) src/Filter.cpp (+2/-2) src/Graphics.cpp (+325/-230) src/GrayPicture.cpp (+0/-73) src/Picture.cpp (+104/-23) src/Scribbler.cpp (+13/-13) src/VideoStream.cpp (+5/-4) test/graphics/circles.cpp (+28/-19) test/take_snapshot.cpp (+1/-1) test/test_blob.cpp (+3/-7) test/test_camera.cpp (+1/-5) test/test_camera_fourwindow.cpp (+4/-8) test/test_camera_nonblocking.cpp (+3/-3) |
||||
To merge this branch: | bzr merge lp:~myro-cpp-dev/myro-c++/smart_pointers | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
John Hoare | Pending | ||
Review via email: mp+68767@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
- 93. By John Hoare
-
Set the MyroSmartPointer.h to install in the CMakeLists.txt file.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'headers/CMakeLists.txt' |
2 | --- headers/CMakeLists.txt 2011-05-02 05:46:27 +0000 |
3 | +++ headers/CMakeLists.txt 2011-07-22 02:32:35 +0000 |
4 | @@ -1,5 +1,5 @@ |
5 | |
6 | |
7 | -INSTALL_FILES(/include/Myro-Cpp FILES ColorPicture.h Filter.h GrayPicture.h MyroIO.h Myro.h MyroForwardDec.h Picture.h Robot.h Scribbler.h VideoStream.h MyroCImg.h Graphics.h) |
8 | +INSTALL_FILES(/include/Myro-Cpp FILES Filter.h MyroSmartPointer.h MyroIO.h Myro.h MyroForwardDec.h Picture.h Robot.h Scribbler.h VideoStream.h MyroCImg.h Graphics.h) |
9 | |
10 | add_subdirectory(CImg) |
11 | |
12 | === removed file 'headers/ColorPicture.h' |
13 | --- headers/ColorPicture.h 2011-04-17 13:57:45 +0000 |
14 | +++ headers/ColorPicture.h 1970-01-01 00:00:00 +0000 |
15 | @@ -1,38 +0,0 @@ |
16 | -#ifndef __COLOR_PICTURE_H__ |
17 | -#define __COLOR_PICTURE_H__ |
18 | - |
19 | -#include <Picture.h> |
20 | - |
21 | -/** @addtogroup picture |
22 | - * @{ |
23 | - */ |
24 | - |
25 | -/// Object Representing a Color Picture (also used for Blob Pictures) |
26 | -class ColorPicture: public Picture { |
27 | - |
28 | - public: |
29 | - //functions for students to use, if they need to create a picture. |
30 | - ColorPicture(); |
31 | - ColorPicture(int width, int height); |
32 | - ColorPicture(ColorPicture& pic); |
33 | - |
34 | - //functions for scribbler code to use |
35 | - ColorPicture(unsigned char * data, int width, int height); |
36 | - ~ColorPicture(); |
37 | - |
38 | - virtual Pixel getPixel(int x, int y); |
39 | - virtual void setPixel(int x, int y, Pixel pix); |
40 | - virtual void show(std::string windowname); |
41 | - virtual void show(); |
42 | - virtual Picture* clone(); |
43 | - virtual bool loadPicture(const char* filename); |
44 | - virtual void savePicture(const char* filename); |
45 | - private: |
46 | - void loadInterlacedImage(unsigned char* img); |
47 | - //myro_img image_data; |
48 | -}; |
49 | - |
50 | - |
51 | -///@} |
52 | - |
53 | -#endif |
54 | |
55 | === modified file 'headers/Filter.h' |
56 | --- headers/Filter.h 2011-01-27 22:54:33 +0000 |
57 | +++ headers/Filter.h 2011-07-22 02:32:35 +0000 |
58 | @@ -40,7 +40,7 @@ |
59 | * is extended to allow for a filter to yield data from its |
60 | * application. |
61 | */ |
62 | - void applyFilter(Picture*); |
63 | + void applyFilter(PicturePtr); |
64 | |
65 | /** |
66 | * The function used by applyFilter to actually process |
67 | @@ -48,7 +48,7 @@ |
68 | * if you do not overwrite this function, then the filter's |
69 | * default behavior is to do nothing. |
70 | */ |
71 | - virtual void filter(Picture*); |
72 | + virtual void filter(PicturePtr); |
73 | |
74 | }; |
75 | |
76 | |
77 | === modified file 'headers/Graphics.h' |
78 | --- headers/Graphics.h 2011-06-23 02:35:48 +0000 |
79 | +++ headers/Graphics.h 2011-07-22 02:32:35 +0000 |
80 | @@ -9,15 +9,35 @@ |
81 | */ |
82 | |
83 | |
84 | +/// Pixel from Picture.h and Color are the same. |
85 | +typedef Pixel Color; |
86 | + |
87 | // Forward declartions |
88 | class CImg_display; |
89 | class GraphicsObject; |
90 | class Point; |
91 | - |
92 | -typedef std::list<GraphicsObject*> GraphicsObjectList; |
93 | -typedef std::list<GraphicsObject*>::iterator GOL_reg; |
94 | -/// Pixel from Picture.h and Color are the same. |
95 | -typedef Pixel Color; |
96 | +class GraphWin; |
97 | +class DrawMessage; |
98 | + |
99 | +struct draw_message_common; |
100 | +struct point_draw_msg; |
101 | +struct bb_draw_msg; |
102 | +struct circle_draw_msg; |
103 | +struct line_draw_msg; |
104 | +struct polygon_draw_msg; |
105 | +struct text_draw_msg; |
106 | +struct base_msg; |
107 | + |
108 | +struct DrawMessage{ |
109 | + draw_message_common* common; |
110 | + base_msg* data; |
111 | + base_msg* extra; |
112 | +}; |
113 | + |
114 | +typedef std::list<DrawMessage*> GraphicsObjectList; |
115 | +typedef std::list<DrawMessage*>::iterator GOL_reg; |
116 | + |
117 | + |
118 | |
119 | /** |
120 | * Create a color "object" with the given r,g,b values |
121 | @@ -37,6 +57,7 @@ |
122 | */ |
123 | Color string_to_color(std::string color); |
124 | |
125 | + |
126 | /** |
127 | * A GraphWin object represents a window on the screen where graphical images |
128 | * may be drawn. A program may define any number of GraphWins. Each GraphWin |
129 | @@ -127,6 +148,7 @@ |
130 | // TODO: This should be private, but I'm getting errors here... |
131 | void check_and_update(); |
132 | private: |
133 | + static bool draw_message(DrawMessage* msg, myro_img& canvas); |
134 | GOL_reg draw(GraphicsObject* obj); |
135 | void undraw(GOL_reg reg); |
136 | void init(); |
137 | @@ -146,6 +168,7 @@ |
138 | * class which can be drawn onto a GraphWin. |
139 | */ |
140 | class GraphicsObject{ |
141 | + |
142 | public: |
143 | GraphicsObject(); |
144 | GraphicsObject(Color fill, Color outline, int width); |
145 | @@ -154,12 +177,18 @@ |
146 | void setFill(Color color); |
147 | /// Set the Fill of the shape |
148 | void setFill(std::string color); |
149 | + /// Get the Fill of the shape |
150 | + Color getFill(); |
151 | /// Set the outline of the shape |
152 | void setOutline(Color color); |
153 | /// Set the outline of the shape |
154 | void setOutline(std::string color); |
155 | + /// Get the Outline of the shape |
156 | + Color getOutline(); |
157 | /// Set the width of the outline around the shape |
158 | void setWidth(int width); |
159 | + /// Get the width of the outline around the shape |
160 | + int getWidth(); |
161 | /// Draw the object to a given GraphWin |
162 | void draw(GraphWin* canvas); |
163 | /// Draw the object to a given GraphWin |
164 | @@ -178,12 +207,9 @@ |
165 | /// This function gets implemented by child classes to implement |
166 | /// how to draw itself, the GraphWin calls this on all of its |
167 | /// registered GraphicObjects. |
168 | - virtual void draw_command(myro_img& canvas)=0; |
169 | - GraphWin* canvas; |
170 | - GOL_reg registration; |
171 | - Color fill; |
172 | - Color outline; |
173 | - int width; |
174 | + //virtual void draw_command(myro_img& canvas)=0; |
175 | + DrawMessage* drawData; |
176 | +// boost::shared_ptr<GraphicsObject::DrawPair> drawdata; |
177 | friend class GraphWin; |
178 | }; |
179 | |
180 | @@ -208,10 +234,9 @@ |
181 | Point operator-(const Point &rhs); |
182 | virtual void move(int dx, int dy); |
183 | protected: |
184 | - virtual void draw_command(myro_img& canvas); |
185 | + //virtual void draw_command(myro_img& canvas); |
186 | private: |
187 | - int x; |
188 | - int y; |
189 | + point_draw_msg* data; |
190 | }; |
191 | |
192 | /** |
193 | @@ -226,8 +251,7 @@ |
194 | Point getP2(); |
195 | Point getCenter(); |
196 | protected: |
197 | - Point p1; |
198 | - Point p2; |
199 | + bb_draw_msg* data; |
200 | }; |
201 | |
202 | /** |
203 | @@ -239,7 +263,7 @@ |
204 | Oval(Point p1, Point p2); |
205 | Oval* clone(); |
206 | protected: |
207 | - virtual void draw_command(myro_img& canvas); |
208 | + //virtual void draw_command(myro_img& canvas); |
209 | }; |
210 | |
211 | /** |
212 | @@ -251,9 +275,8 @@ |
213 | Circle(Point center, int radius); |
214 | int getRadius(); |
215 | // virtual void draw_command(myro_img& canvas); |
216 | - private: |
217 | - Point centerPoint; |
218 | - int radius; |
219 | + protected: |
220 | + circle_draw_msg* extra; |
221 | }; |
222 | |
223 | /// Represents a Rectangle |
224 | @@ -262,20 +285,19 @@ |
225 | /// Creates a Rectangle formed with the points p1 and p2. |
226 | Rectangle(Point p1, Point p2); |
227 | protected: |
228 | - virtual void draw_command(myro_img& canvas); |
229 | + //virtual void draw_command(myro_img& canvas); |
230 | }; |
231 | |
232 | /// Represents a Line, or Arrow |
233 | class Line : public _BBox{ |
234 | public: |
235 | /// Creates a Line (or Arrow) from p1 to p2 |
236 | - Line(Point p1, Point p2); |
237 | + Line(Point p1, Point p2, std::string arrow_type="none"); |
238 | /// str can be "first", "last", "both", or none. Default is "none" |
239 | void setArrow(std::string type); |
240 | protected: |
241 | - virtual void draw_command(myro_img& canvas); |
242 | - private: |
243 | - std::string arrow_type; |
244 | + //virtual void draw_command(myro_img& canvas); |
245 | + line_draw_msg* extra; |
246 | friend class Polygon; |
247 | }; |
248 | |
249 | @@ -288,12 +310,10 @@ |
250 | std::vector<Point> getPoints(); |
251 | virtual void move(int dx, int dy); |
252 | protected: |
253 | - virtual void draw_command(myro_img& canvas); |
254 | + //virtual void draw_command(myro_img& canvas); |
255 | private: |
256 | std::vector<Point> update_points(); |
257 | - cil::CImg<int> pts; |
258 | - std::vector<Point> points; |
259 | - std::vector<Line> outline_lines; |
260 | + polygon_draw_msg * data; |
261 | }; |
262 | |
263 | /// Used for displaying text on the GraphWin |
264 | @@ -333,15 +353,67 @@ |
265 | ///(default false) |
266 | void drawBackground(bool background); |
267 | protected: |
268 | - virtual void draw_command(myro_img& canvas); |
269 | + //virtual void draw_command(myro_img& canvas); |
270 | virtual void move(int dx, int dy); |
271 | private: |
272 | - std::string text; |
273 | - std::string font; |
274 | - int size; |
275 | - bool background; |
276 | - std::string style; |
277 | - Point anchor; |
278 | -}; |
279 | + text_draw_msg* data; |
280 | +}; |
281 | + |
282 | +// Declaration of "draw messages" |
283 | +enum graphics_object_type{ |
284 | + GRAPHICS_OBJECT_POINT, |
285 | + GRAPHICS_OBJECT_OVAL, |
286 | + GRAPHICS_OBJECT_CIRCLE, |
287 | + GRAPHICS_OBJECT_RECTANGLE, |
288 | + GRAPHICS_OBJECT_LINE, |
289 | + GRAPHICS_OBJECT_POLYGON, |
290 | + GRAPHICS_OBJECT_TEXT |
291 | +}; |
292 | +struct draw_message_common{ |
293 | + int refCount; |
294 | + graphics_object_type type; |
295 | + GOL_reg registration; |
296 | + Color fill; |
297 | + Color outline; |
298 | + int width; |
299 | + GraphWin* canvas; |
300 | +}; |
301 | + |
302 | +struct base_msg{}; |
303 | + |
304 | +struct point_draw_msg : public base_msg{ |
305 | + int x; |
306 | + int y; |
307 | +}; |
308 | + |
309 | +struct bb_draw_msg : public base_msg{ |
310 | + Point pt1; |
311 | + Point pt2; |
312 | +}; |
313 | + |
314 | +struct circle_draw_msg : public base_msg{ |
315 | + Point centerPoint; |
316 | + int radius; |
317 | +}; |
318 | + |
319 | +struct line_draw_msg : public base_msg{ |
320 | + std::string arrow_type; |
321 | +}; |
322 | + |
323 | +struct polygon_draw_msg : public base_msg{ |
324 | + cil::CImg<int> pts; |
325 | + std::vector<Point> points; |
326 | + std::vector<Line> outline_lines; |
327 | +}; |
328 | + |
329 | +struct text_draw_msg : public base_msg{ |
330 | + std::string text; |
331 | + std::string font; |
332 | + int size; |
333 | + bool background; |
334 | + std::string style; |
335 | + Point anchor; |
336 | +}; |
337 | + |
338 | |
339 | ///@} |
340 | |
341 | === removed file 'headers/GrayPicture.h' |
342 | --- headers/GrayPicture.h 2011-04-17 13:57:45 +0000 |
343 | +++ headers/GrayPicture.h 1970-01-01 00:00:00 +0000 |
344 | @@ -1,36 +0,0 @@ |
345 | -#ifndef __GRAY_PICTURE_H__ |
346 | -#define __GRAY_PICTURE_H__ |
347 | - |
348 | -#include <Picture.h> |
349 | - |
350 | -/** @addtogroup picture |
351 | - * @{ |
352 | - */ |
353 | - |
354 | -/// Object Representing a Gray Picture |
355 | -class GrayPicture: public Picture { |
356 | - |
357 | - public: |
358 | - |
359 | - GrayPicture(); |
360 | - GrayPicture(int width, int height); |
361 | - ~GrayPicture(); |
362 | - |
363 | - GrayPicture(unsigned char * data, int width, int height); |
364 | - GrayPicture(GrayPicture& pic); |
365 | - |
366 | - virtual Pixel getPixel(int x, int y); |
367 | - virtual void setPixel(int x, int y, Pixel pix); |
368 | - virtual void show(std::string windowname); |
369 | - virtual void show(); |
370 | - virtual Picture* clone(); |
371 | - virtual bool loadPicture(const char* filename); |
372 | - virtual void savePicture(const char* filename); |
373 | - |
374 | - private: |
375 | - //myro_img image_data; |
376 | -}; |
377 | - |
378 | -///@} |
379 | - |
380 | -#endif |
381 | |
382 | === added file 'headers/MyroSmartPointer.h' |
383 | --- headers/MyroSmartPointer.h 1970-01-01 00:00:00 +0000 |
384 | +++ headers/MyroSmartPointer.h 2011-07-22 02:32:35 +0000 |
385 | @@ -0,0 +1,58 @@ |
386 | +#pragma once |
387 | + |
388 | +template <class T> |
389 | +class MyroSmartPointer { |
390 | + private: |
391 | + T* ptr; // pointer to the value |
392 | + long* count; // shared number of owners |
393 | + |
394 | + public: |
395 | + // initialize pointer with existing pointer |
396 | + // - requires that the pointer p is a return value of new |
397 | + explicit MyroSmartPointer (T* p=0) |
398 | + : ptr(p), count(new long(1)) { |
399 | + } |
400 | + |
401 | + // copy pointer (one more owner) |
402 | + MyroSmartPointer (const MyroSmartPointer<T>& p) throw() |
403 | + : ptr(p.ptr), count(p.count) { |
404 | + ++*count; |
405 | + } |
406 | + |
407 | + // destructor (delete value if this was the last owner) |
408 | + ~MyroSmartPointer () throw() { |
409 | + dispose(); |
410 | + } |
411 | + |
412 | + // assignment (unshare old and share new value) |
413 | + MyroSmartPointer<T>& operator= (const MyroSmartPointer<T>& p) throw() { |
414 | + if (this != &p) { |
415 | + dispose(); |
416 | + ptr = p.ptr; |
417 | + count = p.count; |
418 | + ++*count; |
419 | + } |
420 | + return *this; |
421 | + } |
422 | + |
423 | + // access the value to which the pointer refers |
424 | + T& operator*() const throw() { |
425 | + return *ptr; |
426 | + } |
427 | + T* operator->() const throw() { |
428 | + return ptr; |
429 | + } |
430 | + |
431 | + T* get() const{ |
432 | + return ptr; |
433 | + } |
434 | + |
435 | + private: |
436 | + void dispose() { |
437 | + if (--*count == 0) { |
438 | + delete count; |
439 | + delete ptr; |
440 | + } |
441 | + } |
442 | + |
443 | +}; |
444 | |
445 | === modified file 'headers/Picture.h' |
446 | --- headers/Picture.h 2011-04-17 13:57:45 +0000 |
447 | +++ headers/Picture.h 2011-07-22 02:32:35 +0000 |
448 | @@ -4,10 +4,13 @@ |
449 | #include<exception> |
450 | #include<string> |
451 | #include<MyroCImg.h> |
452 | +#include<MyroSmartPointer.h> |
453 | /** @defgroup picture Picture Operations |
454 | * The collection of all Picture Related commands and operations |
455 | * @{ |
456 | */ |
457 | +class Picture; |
458 | +typedef MyroSmartPointer<Picture> PicturePtr; |
459 | |
460 | /** |
461 | * A pixel structure that respesents a single pixel of an Image. |
462 | @@ -77,16 +80,18 @@ |
463 | |
464 | Picture(); |
465 | Picture(int width, int height); |
466 | + Picture(unsigned char * data, int channels, int width, int height); |
467 | + Picture(Picture& pic); |
468 | ~Picture(); |
469 | |
470 | /** |
471 | * @return A pixel struct representing the pixel at x,y |
472 | */ |
473 | - virtual Pixel getPixel(int x, int y)=0; |
474 | + virtual Pixel getPixel(int x, int y); |
475 | /** |
476 | * Set the value of a pixel at x,y to be the given pixel pix. |
477 | */ |
478 | - virtual void setPixel(int x, int y, Pixel pix)=0; |
479 | + virtual void setPixel(int x, int y, Pixel pix); |
480 | /** |
481 | * Display the picture on the screen. |
482 | * |
483 | @@ -98,7 +103,7 @@ |
484 | * a new window, if it already exists, it will draw the image |
485 | * into that window. |
486 | */ |
487 | - virtual void show(std::string windowname)=0; |
488 | + virtual void show(std::string windowname); |
489 | /** |
490 | * Display the picture on the screen. |
491 | * |
492 | @@ -106,7 +111,7 @@ |
493 | * window, before the program continues running. |
494 | * |
495 | */ |
496 | - virtual void show()=0; |
497 | + virtual void show(); |
498 | /** |
499 | * Get a pointer to the underlying memory representing the |
500 | * image. |
501 | @@ -127,7 +132,7 @@ |
502 | * |
503 | * @return A clone of this picture |
504 | */ |
505 | - virtual Picture* clone() = 0; |
506 | + virtual PicturePtr clone(); |
507 | |
508 | int getHeight(); |
509 | int getWidth(); |
510 | @@ -140,7 +145,7 @@ |
511 | * @return True if load sucessful, false if fails (for wrong |
512 | * colorspace, etc.) |
513 | */ |
514 | - virtual bool loadPicture(const char* filename)=0; |
515 | + virtual bool loadPicture(const char* filename); |
516 | |
517 | /** |
518 | * Save an image to a file, only jpeg files are supported. |
519 | @@ -149,7 +154,7 @@ |
520 | * @param filename The name of the file you save (without the .jpg |
521 | * file extention) |
522 | */ |
523 | - virtual void savePicture(const char* filename)=0; |
524 | + virtual void savePicture(const char* filename); |
525 | |
526 | /** |
527 | * Static method to print out an error message when a user has |
528 | @@ -158,6 +163,8 @@ |
529 | static void out_of_bounds_error(int width, int height, |
530 | int given_width, int given_height); |
531 | |
532 | + private: |
533 | + void loadInterlacedImage(unsigned char* img); |
534 | protected: |
535 | |
536 | //unsigned char * image_data; |
537 | @@ -165,24 +172,25 @@ |
538 | myro_img image_data; |
539 | int width; |
540 | int height; |
541 | + int channels; |
542 | }; |
543 | // functions added for lab 6 by Nick |
544 | -int getWidth(Picture *p); |
545 | -int getHeight(Picture *p); |
546 | -void show(Picture *p, std::string windowname); |
547 | -void show(Picture *p); |
548 | -Pixel getPixel(Picture *p, int x, int y); |
549 | -int getPixelValue_grey(Picture *p, int x, int y); |
550 | -void setPixel(Picture* p, int x, int y, Pixel pix); |
551 | -void setPixelColor(Picture *p, int x, int y, int R, int G, int B); |
552 | -Picture* clone(Picture* p); |
553 | +int getWidth(PicturePtr p); |
554 | +int getHeight(PicturePtr p); |
555 | +void show(PicturePtr p, std::string windowname); |
556 | +void show(PicturePtr p); |
557 | +Pixel getPixel(PicturePtr p, int x, int y); |
558 | +int getPixelValue_grey(PicturePtr p, int x, int y); |
559 | +void setPixel(PicturePtr p, int x, int y, Pixel pix); |
560 | +void setPixelColor(PicturePtr p, int x, int y, int R, int G, int B); |
561 | +PicturePtr clone(PicturePtr p); |
562 | |
563 | /// Create a picture object from a saved image |
564 | -Picture* loadPicture(const char* filename); |
565 | +PicturePtr loadPicture(const char* filename); |
566 | /// Load an image into the given Picture object |
567 | -void loadPicture(Picture * p, const char* filename); |
568 | +void loadPicture(PicturePtr p, const char* filename); |
569 | /// Save the Image to a file |
570 | -void savePicture(Picture * p, const char* filename); |
571 | +void savePicture(PicturePtr p, const char* filename); |
572 | |
573 | ///@} |
574 | |
575 | |
576 | === modified file 'headers/Scribbler.h' |
577 | --- headers/Scribbler.h 2011-06-28 01:17:27 +0000 |
578 | +++ headers/Scribbler.h 2011-07-22 02:32:35 +0000 |
579 | @@ -470,7 +470,7 @@ |
580 | * @param type - Valid values, "color", "gray", and "blob" |
581 | * @return unsigned char array containing the image information. |
582 | */ |
583 | - Picture * takePicture(std::string type = "color"); |
584 | + PicturePtr takePicture(std::string type = "color"); |
585 | |
586 | /* |
587 | * Not yet functional. This version is intended to be used for |
588 | |
589 | === modified file 'src/CMakeLists.txt' |
590 | --- src/CMakeLists.txt 2011-05-02 05:46:27 +0000 |
591 | +++ src/CMakeLists.txt 2011-07-22 02:32:35 +0000 |
592 | @@ -4,7 +4,7 @@ |
593 | |
594 | GET_DIRECTORY_PROPERTY(myrocpp_jpeg_SRCS DIRECTORY ${PROJECT_SOURCE_DIR}/jpeg DEFINITION myrocpp_jpeg_SRCS) |
595 | |
596 | -ADD_LIBRARY(myro-cpp SHARED Myro.cpp MyroInternals.cpp Scribbler.cpp serial.cpp Robot.cpp ColorPicture.cpp GrayPicture.cpp Picture.cpp VideoStream.cpp Filter.cpp Graphics.cpp xcolors.cpp jmemsrc.c) |
597 | +ADD_LIBRARY(myro-cpp SHARED Myro.cpp MyroInternals.cpp Scribbler.cpp serial.cpp Robot.cpp Picture.cpp VideoStream.cpp Filter.cpp Graphics.cpp xcolors.cpp jmemsrc.c) |
598 | |
599 | TARGET_LINK_LIBRARIES(myro-cpp ${GUI_LIBRARIES} ${PROJECT_BINARY_DIR}/jpeg/libmyrocpp_jpeg.a) |
600 | |
601 | |
602 | === removed file 'src/ColorPicture.cpp' |
603 | --- src/ColorPicture.cpp 2011-05-09 21:30:54 +0000 |
604 | +++ src/ColorPicture.cpp 1970-01-01 00:00:00 +0000 |
605 | @@ -1,82 +0,0 @@ |
606 | -#define cimg_use_jpeg |
607 | -#include "ColorPicture.h" |
608 | -#include <cstdio> |
609 | -#include "MyroInternals.h" |
610 | - |
611 | -using namespace std; |
612 | - |
613 | -ColorPicture::ColorPicture(): Picture() {}; |
614 | - |
615 | -ColorPicture::ColorPicture(unsigned char * data, int width, int height) |
616 | - : Picture(width, height){ |
617 | - |
618 | - image_data.assign(width,height,1,3); |
619 | - loadInterlacedImage(data); |
620 | - this->width = width; |
621 | - this->height = height; |
622 | -} |
623 | - |
624 | -ColorPicture::ColorPicture(int width, int height): |
625 | - Picture(width, height) |
626 | -{ |
627 | - image_data.assign(width,height,1,3,0); |
628 | - this->width = width; |
629 | - this->height = height; |
630 | -} |
631 | - |
632 | -ColorPicture::ColorPicture(ColorPicture& pic): |
633 | - Picture(pic.width, pic.height){ |
634 | - image_data.assign(pic.image_data); |
635 | -} |
636 | - |
637 | -Pixel ColorPicture::getPixel(int x, int y) { |
638 | - if ( x >= width || y >= height ) |
639 | - Picture::out_of_bounds_error(width,height,x,y); |
640 | - Pixel result; |
641 | - result.R = image_data(x,y,0,0); |
642 | - result.G = image_data(x,y,0,1); |
643 | - result.B = image_data(x,y,0,2); |
644 | - return result; |
645 | -} |
646 | - |
647 | -void ColorPicture::setPixel(int x, int y, Pixel pix) { |
648 | - if ( x >= width || y >= height ) |
649 | - Picture::out_of_bounds_error(width,height,x,y); |
650 | - image_data(x,y,0,0) = pix.R; |
651 | - image_data(x,y,0,1) = pix.G; |
652 | - image_data(x,y,0,2) = pix.B; |
653 | -} |
654 | - |
655 | -void ColorPicture::show(std::string windowname){ |
656 | - displayMan.set_picture_window(image_data, windowname.c_str()); |
657 | -} |
658 | - |
659 | -void ColorPicture::show(){ |
660 | - const std::string windowname = "Color Picture"; |
661 | - displayMan.set_picture_window(image_data, windowname.c_str()); |
662 | - displayMan.block_on(windowname.c_str()); |
663 | -} |
664 | - |
665 | -Picture* ColorPicture::clone(){ |
666 | - Picture* newpic = new ColorPicture(*this); |
667 | - return newpic; |
668 | -} |
669 | - |
670 | -bool ColorPicture::loadPicture(const char* filename){ |
671 | - /* And we're done! */ |
672 | - image_data.load_jpeg(filename); |
673 | - return true; |
674 | -} |
675 | - |
676 | -void ColorPicture::savePicture(const char* filename){ |
677 | - /* And we're done! */ |
678 | - image_data.save_jpeg(filename); |
679 | -} |
680 | - |
681 | -void ColorPicture::loadInterlacedImage(unsigned char* img){ |
682 | - cimg_forXY(image_data,x,y){ |
683 | - image_data(x,y,0,0) = img[y*width*3+x*3]; |
684 | - image_data(x,y,0,1) = img[y*width*3+x*3+1]; |
685 | - image_data(x,y,0,2) = img[y*width*3+x*3+2]; |
686 | - } |
687 | -} |
688 | |
689 | === modified file 'src/Filter.cpp' |
690 | --- src/Filter.cpp 2010-12-22 22:39:12 +0000 |
691 | +++ src/Filter.cpp 2011-07-22 02:32:35 +0000 |
692 | @@ -6,10 +6,10 @@ |
693 | Filter::~Filter() { |
694 | } |
695 | |
696 | -void Filter::applyFilter(Picture * image) { |
697 | +void Filter::applyFilter(PicturePtr image) { |
698 | this->filter(image); |
699 | } |
700 | |
701 | -void Filter::filter(Picture* image) { |
702 | +void Filter::filter(PicturePtr image) { |
703 | return; |
704 | } |
705 | |
706 | === modified file 'src/Graphics.cpp' |
707 | --- src/Graphics.cpp 2011-06-21 13:38:57 +0000 |
708 | +++ src/Graphics.cpp 2011-07-22 02:32:35 +0000 |
709 | @@ -3,6 +3,7 @@ |
710 | #include <iostream> |
711 | #include <assert.h> |
712 | #include <cmath> |
713 | +#include <cstdlib> |
714 | |
715 | static unsigned char red[] = {255,0,0}; |
716 | |
717 | @@ -81,6 +82,8 @@ |
718 | thread = NULL; |
719 | //delete thread; |
720 | } |
721 | + //while (!drawlist.empty()) |
722 | + // undraw(drawlist.front()+1); |
723 | } |
724 | |
725 | void GraphWin::init(){ |
726 | @@ -147,14 +150,25 @@ |
727 | } |
728 | |
729 | GOL_reg GraphWin::draw(GraphicsObject* obj){ |
730 | - drawlist.push_back(obj); |
731 | + drawlist.push_back(obj->drawData); |
732 | + obj->drawData->common->refCount++; |
733 | GOL_reg registration = --drawlist.end(); |
734 | this->check_and_update(); |
735 | return registration; |
736 | } |
737 | |
738 | void GraphWin::undraw(GOL_reg reg){ |
739 | + DrawMessage* msg = *reg; |
740 | + msg->common->refCount--; |
741 | drawlist.erase(reg); |
742 | + if ( msg->common->refCount <= 0 ){ |
743 | + std::cerr << "Cleaning up Object" << std::endl; |
744 | + // Then we have to clean it up, because the object has gone away |
745 | + if ( msg->extra ) free( msg->extra ); |
746 | + if ( msg->data ) free( msg->data ); |
747 | + if ( msg->common ) free( msg->common ); |
748 | + if ( msg ) free( msg ); |
749 | + } |
750 | this->check_and_update(); |
751 | } |
752 | |
753 | @@ -170,7 +184,7 @@ |
754 | result = background; |
755 | } |
756 | for(GOL_reg it = drawlist.begin(); it != drawlist.end(); it++) |
757 | - (*it)->draw_command(result); |
758 | + assert(GraphWin::draw_message((*it), result)); |
759 | |
760 | if ( thread ) |
761 | thread->change_image(result); |
762 | @@ -181,32 +195,204 @@ |
763 | update(); |
764 | } |
765 | |
766 | +void draw_line(Point p1, Point p2, std::string arrow_type, int width, |
767 | + unsigned char* outlinecolor, myro_img& canvas){ |
768 | + if ( arrow_type == "first" ){ |
769 | + canvas.draw_arrow(p2.getX(), p2.getY(), p1.getX(), p1.getY(), outlinecolor); |
770 | + } |
771 | + else if ( arrow_type == "last" ){ |
772 | + canvas.draw_arrow(p1.getX(), p1.getY(), p2.getX(), p2.getY(), outlinecolor); |
773 | + } |
774 | + else if ( arrow_type == "both" ){ |
775 | + canvas.draw_arrow(p2.getX(), p2.getY(), p1.getX(), p1.getY(), outlinecolor); |
776 | + canvas.draw_arrow(p1.getX(), p1.getY(), p2.getX(), p2.getY(), outlinecolor); |
777 | + } |
778 | + if ( width > 1 ){ |
779 | + int x0=p1.getX(),x1=p2.getX(),y0=p1.getY(),y1=p2.getY(); |
780 | + bool steep = abs(y0-y1) > abs(x0-x1); |
781 | + if ( steep ){ |
782 | + swap(x0,y0); |
783 | + swap(x1,y1); |
784 | + } |
785 | + if ( x0 > x1 ){ |
786 | + swap(x0,x1); |
787 | + swap(y0,y1); |
788 | + } |
789 | + int deltax = x1-x0; |
790 | + int deltay = abs(y1-y0); |
791 | + float error = 0; |
792 | + float deltaerr = (float)(deltay) / (deltax); |
793 | + int ystep = y0 < y1 ? 1 : -1; |
794 | + int y = y0; |
795 | + // This is a sort of weird bresenhams thing where I sweep the end |
796 | + // points so i can draw a "thick" line |
797 | + for (int x = x0; x <= x1; x++){ |
798 | + if ( steep ) |
799 | + canvas.draw_circle(y,x,ceil(width/2.0),outlinecolor); |
800 | + else |
801 | + canvas.draw_circle(x,y,ceil(width/2.0),outlinecolor); |
802 | + error += deltaerr; |
803 | + if ( error >= 0.5 ){ |
804 | + y += ystep; |
805 | + error -= 1.0; |
806 | + } |
807 | + } |
808 | + } |
809 | + else if (width == 1){ |
810 | + canvas.draw_line(p1.getX(), p1.getY(), p2.getX(), p2.getY(), outlinecolor); |
811 | + } |
812 | +} |
813 | + |
814 | +std::vector<Point> update_points(polygon_draw_msg* msg, Color lineColor, int width){ |
815 | + Point p; |
816 | + std::vector<Point> ret; |
817 | + msg->outline_lines.clear(); |
818 | + msg->outline_lines.push_back(Line(msg->points[msg->points.size()-1],msg->points[0])); |
819 | + |
820 | + for (int i = 0; i < (int)msg->points.size(); i++){ |
821 | + if (i+1 < (int)msg->points.size()) |
822 | + msg->outline_lines.push_back(Line(msg->points[i],msg->points[i+1])); |
823 | + ret.push_back(msg->points[i]); |
824 | + p = msg->points[i]; |
825 | + msg->pts(i,0) = p.getX(); |
826 | + msg->pts(i,1) = p.getY(); |
827 | + } |
828 | + for (int i = 0; i < (int)msg->outline_lines.size(); i++){ |
829 | + msg->outline_lines[i].setWidth(width); |
830 | + msg->outline_lines[i].setOutline(lineColor); |
831 | + } |
832 | + return ret; |
833 | +} |
834 | + |
835 | +bool GraphWin::draw_message(DrawMessage* msg, myro_img& canvas){ |
836 | + unsigned char color[] = {msg->common->fill.R, msg->common->fill.G, msg->common->fill.B}; |
837 | + unsigned char outlinecolor[] = {msg->common->outline.R, msg->common->outline.G, msg->common->outline.B}; |
838 | + int width = msg->common->width; |
839 | + switch(msg->common->type){ |
840 | + case GRAPHICS_OBJECT_POINT: |
841 | + { |
842 | + point_draw_msg* data = (point_draw_msg*)data; |
843 | + int x = data->x, y = data->y; |
844 | + canvas(x,y,0,0) = msg->common->outline.R; |
845 | + canvas(x,y,0,1) = msg->common->outline.G; |
846 | + canvas(x,y,0,2) = msg->common->outline.B; |
847 | + } |
848 | + break; |
849 | + case GRAPHICS_OBJECT_OVAL: |
850 | + case GRAPHICS_OBJECT_CIRCLE: |
851 | + { |
852 | + bb_draw_msg* data = (bb_draw_msg*)msg->data; |
853 | + Point center = Point((data->pt1.getX()+data->pt2.getX())/2, |
854 | + (data->pt1.getY()+data->pt2.getY())/2); |
855 | + int r0 = abs(center.getX() - data->pt2.getX()); |
856 | + int r1 = abs(center.getY() - data->pt2.getY()); |
857 | + if ( width ){ |
858 | + canvas.draw_ellipse(center.getX(),center.getY(), r0, r1, 0, outlinecolor); |
859 | + canvas.draw_ellipse(center.getX(), center.getY(), r0-width, r1-width, 0, color); |
860 | + } |
861 | + else{ |
862 | + canvas.draw_ellipse(center.getX(),center.getY(), r0, r1, 0, color); |
863 | + } |
864 | + } |
865 | + break; |
866 | + case GRAPHICS_OBJECT_RECTANGLE: |
867 | + { |
868 | + bb_draw_msg* data = (bb_draw_msg*)msg->data; |
869 | + if ( width ){ |
870 | + bb_draw_msg* data = (bb_draw_msg*)msg->data; |
871 | + int leftx = std::min(data->pt1.getX(),data->pt2.getX())+width;; |
872 | + int rightx = std::max(data->pt1.getX(), data->pt2.getX())-width; |
873 | + int topy = std::min(data->pt1.getY(), data->pt2.getY())+width; |
874 | + int bottomy = std::max(data->pt1.getY(), data->pt2.getY())-width; |
875 | + canvas.draw_rectangle(data->pt1.getX(), data->pt1.getY(), |
876 | + data->pt2.getX(), data->pt2.getY(), outlinecolor); |
877 | + canvas.draw_rectangle(leftx,topy,rightx,bottomy, color); |
878 | + } |
879 | + else{ |
880 | + canvas.draw_rectangle(data->pt1.getX(), data->pt1.getY(), |
881 | + data->pt2.getX(), data->pt2.getY(), color); |
882 | + } |
883 | + } |
884 | + break; |
885 | + case GRAPHICS_OBJECT_LINE: |
886 | + { |
887 | + std::string arrow_type = ((line_draw_msg*)(msg->extra))->arrow_type; |
888 | + bb_draw_msg* data = (bb_draw_msg*)msg->data; |
889 | + Point p1 = data->pt1; |
890 | + Point p2 = data->pt2; |
891 | + draw_line(p1, p2, arrow_type, width, outlinecolor, canvas); |
892 | + } |
893 | + break; |
894 | + case GRAPHICS_OBJECT_POLYGON: |
895 | + { |
896 | + polygon_draw_msg* data = (polygon_draw_msg*)msg->data; |
897 | + std::vector<Point> debug = update_points(data,msg->common->outline,width); |
898 | + canvas.draw_polygon(data->pts, color); |
899 | + if ( width == 1 ){ |
900 | + canvas.draw_polygon(data->pts, outlinecolor, 1, ~0U); |
901 | + } else if ( width > 1 ){ |
902 | + for (int i = 0; i < (int)data->outline_lines.size(); i++) |
903 | + draw_line(data->outline_lines[i].getP1(), data->outline_lines[i].getP2(), |
904 | + "none", width, outlinecolor, canvas); |
905 | + } |
906 | + } |
907 | + break; |
908 | + case GRAPHICS_OBJECT_TEXT: |
909 | + { |
910 | + text_draw_msg* data = (text_draw_msg*)msg->data; |
911 | + if ( data->background ){ |
912 | + canvas.draw_text(data->anchor.getX(), data->anchor.getY(), data->text.c_str(), |
913 | + outlinecolor, color, 1, data->size); |
914 | + } |
915 | + else{ |
916 | + canvas.draw_text(data->anchor.getX(), data->anchor.getY(), data->text.c_str(), |
917 | + outlinecolor, 0, 1, data->size); |
918 | + } |
919 | + } |
920 | + break; |
921 | + default: |
922 | + return false; |
923 | + break; |
924 | + } |
925 | + return true; |
926 | +} |
927 | + |
928 | // ==================== |
929 | // GraphicsObject |
930 | // ==================== |
931 | GraphicsObject::GraphicsObject() |
932 | -: canvas(NULL), |
933 | - fill(color_rgb(255,255,255)), |
934 | - outline(color_rgb(0,0,0)), |
935 | - width(1) |
936 | -{} |
937 | +{ |
938 | + drawData = new DrawMessage(); |
939 | + drawData->common = new draw_message_common(); |
940 | + //drawData = (DrawMessage*)malloc(sizeof(DrawMessage)); |
941 | + //drawData->common = (draw_message_common*)malloc(sizeof(draw_message_common)); |
942 | + drawData->common->refCount = 1; |
943 | + drawData->common->canvas = NULL; |
944 | + drawData->common->fill = color_rgb(255,255,255); |
945 | + drawData->common->outline = color_rgb(0,0,0); |
946 | + drawData->common->width = 1; |
947 | + drawData->data = drawData->extra = NULL; |
948 | +} |
949 | |
950 | GraphicsObject::GraphicsObject(Color fill, Color outline, int width) |
951 | -: canvas(NULL), |
952 | - fill(fill), |
953 | - outline(outline), |
954 | - width(width) |
955 | -{} |
956 | +{ |
957 | + drawData = new DrawMessage(); |
958 | + drawData->common = new draw_message_common(); |
959 | + drawData->common->refCount = 1; |
960 | + drawData->common->canvas = NULL; |
961 | + drawData->common->fill = fill; |
962 | + drawData->common->outline = outline; |
963 | + drawData->common->width = width; |
964 | + drawData->data = drawData->extra = NULL; |
965 | +} |
966 | |
967 | GraphicsObject::~GraphicsObject(){ |
968 | - if (this->canvas) |
969 | - this->undraw(); |
970 | } |
971 | |
972 | void GraphicsObject::setFill(Color color){ |
973 | - fill=color; |
974 | - if ( this->canvas ) |
975 | - this->canvas->check_and_update(); |
976 | + drawData->common->fill=color; |
977 | + if ( drawData->common->canvas ) |
978 | + drawData->common->canvas->check_and_update(); |
979 | } |
980 | |
981 | void GraphicsObject::setFill(std::string color){ |
982 | @@ -214,10 +400,14 @@ |
983 | this->setFill(c); |
984 | } |
985 | |
986 | +Color GraphicsObject::getFill(){ |
987 | + return drawData->common->fill; |
988 | +} |
989 | + |
990 | void GraphicsObject::setOutline(Color color){ |
991 | - outline=color; |
992 | - if ( this->canvas ) |
993 | - this->canvas->check_and_update(); |
994 | + drawData->common->outline=color; |
995 | + if ( drawData->common->canvas ) |
996 | + drawData->common->canvas->check_and_update(); |
997 | } |
998 | |
999 | void GraphicsObject::setOutline(std::string color){ |
1000 | @@ -225,17 +415,25 @@ |
1001 | this->setOutline(c); |
1002 | } |
1003 | |
1004 | +Color GraphicsObject::getOutline(){ |
1005 | + return drawData->common->outline; |
1006 | +} |
1007 | + |
1008 | void GraphicsObject::setWidth(int width){ |
1009 | - this->width = width; |
1010 | - if ( this->canvas ) |
1011 | - this->canvas->check_and_update(); |
1012 | + drawData->common->width = width; |
1013 | + if ( drawData->common->canvas ) |
1014 | + drawData->common->canvas->check_and_update(); |
1015 | +} |
1016 | + |
1017 | +int GraphicsObject::getWidth(){ |
1018 | + return drawData->common->width; |
1019 | } |
1020 | |
1021 | void GraphicsObject::draw(GraphWin* canvas){ |
1022 | - if ( !this->canvas ){ |
1023 | - this->canvas = canvas; |
1024 | - this->registration = this->canvas->draw(this); |
1025 | - assert( *registration == this ); |
1026 | + if ( !drawData->common->canvas ){ |
1027 | + drawData->common->canvas = canvas; |
1028 | + drawData->common->registration = drawData->common->canvas->draw(this); |
1029 | + //assert( *(drawData->common->registration) == this ); |
1030 | } |
1031 | else { |
1032 | // Throw an exception or something |
1033 | @@ -247,9 +445,9 @@ |
1034 | } |
1035 | |
1036 | void GraphicsObject::undraw(){ |
1037 | - if ( this->canvas ){ |
1038 | - this->canvas->undraw(this->registration); |
1039 | - canvas = NULL; |
1040 | + if ( drawData->common->canvas ){ |
1041 | + drawData->common->canvas->undraw(drawData->common->registration); |
1042 | + drawData->common->canvas = NULL; |
1043 | } |
1044 | else{ |
1045 | // Throw an exception |
1046 | @@ -259,28 +457,41 @@ |
1047 | // ==================== |
1048 | // Point |
1049 | // ==================== |
1050 | -Point::Point(): x(0),y(0){} |
1051 | +Point::Point() |
1052 | +{ |
1053 | + drawData->common->type = GRAPHICS_OBJECT_POINT; |
1054 | + //data = (point_draw_msg*)malloc(sizeof(point_draw_msg)); |
1055 | + data = new point_draw_msg(); |
1056 | + drawData->data = data; |
1057 | + data->x = 0; |
1058 | + data->y = 0; |
1059 | +} |
1060 | Point::Point(int x, int y) |
1061 | -: x(x), y(y) |
1062 | -{} |
1063 | +{ |
1064 | + drawData->common->type = GRAPHICS_OBJECT_POINT; |
1065 | + data = new point_draw_msg(); |
1066 | + drawData->data = data; |
1067 | + data->x = x; |
1068 | + data->y = y; |
1069 | +} |
1070 | /* |
1071 | Point::Point(const Point& p){ |
1072 | this->x = p.x; |
1073 | this->y = p.y; |
1074 | } |
1075 | */ |
1076 | -Point* Point::clone(){ return new Point(x,y); } |
1077 | -int Point::getX(){ return x; } |
1078 | -int Point::getY(){ return y; } |
1079 | +Point* Point::clone(){ return new Point(data->x,data->y); } |
1080 | +int Point::getX(){ return data->x; } |
1081 | +int Point::getY(){ return data->y; } |
1082 | void Point::move(int dx, int dy){ |
1083 | - x += dx; |
1084 | - y += dy; |
1085 | - if ( this->canvas ) |
1086 | - this->canvas->check_and_update(); |
1087 | + data->x += dx; |
1088 | + data->y += dy; |
1089 | + if ( drawData->common->canvas ) |
1090 | + drawData->common->canvas->check_and_update(); |
1091 | } |
1092 | Point& Point::operator+=(const Point& rhs){ |
1093 | - x += rhs.x; |
1094 | - y += rhs.y; |
1095 | + data->x += rhs.data->x; |
1096 | + data->y += rhs.data->y; |
1097 | return *this; |
1098 | } |
1099 | Point Point::operator+(const Point& other){ |
1100 | @@ -289,8 +500,8 @@ |
1101 | return result; |
1102 | } |
1103 | Point& Point::operator-=(const Point& rhs){ |
1104 | - x -= rhs.x; |
1105 | - y -= rhs.y; |
1106 | + data->x -= rhs.data->x; |
1107 | + data->y -= rhs.data->y; |
1108 | return *this; |
1109 | } |
1110 | Point Point::operator-(const Point& other){ |
1111 | @@ -298,32 +509,35 @@ |
1112 | result -= other; |
1113 | return result; |
1114 | } |
1115 | +/* |
1116 | void Point::draw_command(myro_img& canvas){ |
1117 | - canvas(x,y,0,0) = outline.R; |
1118 | - canvas(x,y,0,1) = outline.G; |
1119 | - canvas(x,y,0,2) = outline.B; |
1120 | } |
1121 | +*/ |
1122 | |
1123 | // ==================== |
1124 | // _BBox |
1125 | // ==================== |
1126 | -_BBox::_BBox(Point p1, Point p2) |
1127 | -: p1(p1), p2(p2) |
1128 | -{} |
1129 | +_BBox::_BBox(Point p1, Point p2){ |
1130 | + //data = (bb_draw_msg*)malloc(sizeof(bb_draw_msg)); |
1131 | + data = new bb_draw_msg(); |
1132 | + drawData->data = data; |
1133 | + data->pt1 = p1; |
1134 | + data->pt2 = p2; |
1135 | +} |
1136 | |
1137 | void _BBox::move(int dx, int dy){ |
1138 | Point d(dx,dy); |
1139 | - this->p1 += d; |
1140 | - this->p2 += d; |
1141 | - if ( this->canvas ) |
1142 | - this->canvas->check_and_update(); |
1143 | + data->pt1 += d; |
1144 | + data->pt2 += d; |
1145 | + if ( drawData->common->canvas ) |
1146 | + drawData->common->canvas->check_and_update(); |
1147 | } |
1148 | |
1149 | -Point _BBox::getP1(){ return p1; }; |
1150 | -Point _BBox::getP2(){ return p2; }; |
1151 | +Point _BBox::getP1(){ return data->pt1; }; |
1152 | +Point _BBox::getP2(){ return data->pt2; }; |
1153 | Point _BBox::getCenter(){ |
1154 | - return Point((p1.getX()+p2.getX())/2, |
1155 | - (p1.getY()+p2.getY())/2); |
1156 | + return Point((data->pt1.getX()+data->pt2.getX())/2, |
1157 | + (data->pt1.getY()+data->pt2.getY())/2); |
1158 | } |
1159 | |
1160 | // ==================== |
1161 | @@ -332,37 +546,28 @@ |
1162 | Oval::Oval(Point p1, Point p2) |
1163 | : _BBox(p1,p2) |
1164 | { |
1165 | + drawData->common->type = GRAPHICS_OBJECT_OVAL; |
1166 | } |
1167 | |
1168 | Oval* Oval::clone(){ |
1169 | - return new Oval(this->p1, this->p2); |
1170 | -} |
1171 | - |
1172 | -void Oval::draw_command(myro_img& canvas){ |
1173 | - Point center = this->getCenter(); |
1174 | - unsigned char color[] = {fill.R, fill.G, fill.B}; |
1175 | - unsigned char outlinecolor[] = {outline.R, outline.G, outline.B}; |
1176 | - int r0 = abs(center.getX() - this->getP2().getX()); |
1177 | - int r1 = abs(center.getY() - this->getP2().getY()); |
1178 | - if ( this->width ){ |
1179 | - canvas.draw_ellipse(center.getX(),center.getY(), r0, r1, 0, outlinecolor); |
1180 | - canvas.draw_ellipse(center.getX(), center.getY(), r0-width, r1-width, 0, color); |
1181 | - } |
1182 | - else |
1183 | - canvas.draw_ellipse(center.getX(),center.getY(), r0, r1, 0, color); |
1184 | + return new Oval(data->pt1, data->pt2); |
1185 | } |
1186 | |
1187 | // ==================== |
1188 | // Circle |
1189 | // ==================== |
1190 | Circle::Circle(Point center, int radius) |
1191 | -: Oval(Point(center.getX()-radius,center.getY()-radius),Point(center.getX()+radius,center.getY()+radius)), |
1192 | - centerPoint(center), |
1193 | - radius(radius) |
1194 | -{} |
1195 | +: Oval(Point(center.getX()-radius,center.getY()-radius),Point(center.getX()+radius,center.getY()+radius)) |
1196 | +{ |
1197 | + drawData->common->type = GRAPHICS_OBJECT_CIRCLE; |
1198 | + extra = new circle_draw_msg(); |
1199 | + drawData->extra = extra; |
1200 | + extra->centerPoint = center; |
1201 | + extra->radius = radius; |
1202 | +} |
1203 | |
1204 | int Circle::getRadius(){ |
1205 | - return radius; |
1206 | + return extra->radius; |
1207 | } |
1208 | /* |
1209 | void Circle::draw_command(myro_img& canvas){ |
1210 | @@ -375,183 +580,86 @@ |
1211 | // ==================== |
1212 | Rectangle::Rectangle(Point p1, Point p2) |
1213 | : _BBox(p1,p2) |
1214 | -{} |
1215 | - |
1216 | -void Rectangle::draw_command(myro_img& canvas){ |
1217 | - unsigned char color[] = {fill.R, fill.G, fill.B}; |
1218 | - if ( this->width ){ |
1219 | - unsigned char outlinecolor[] = {outline.R, outline.G, outline.B}; |
1220 | - int leftx = std::min(p1.getX(),p2.getX())+width;; |
1221 | - int rightx = std::max(p1.getX(), p2.getX())-width; |
1222 | - int topy = std::min(p1.getY(), p2.getY())+width; |
1223 | - int bottomy = std::max(p1.getY(), p2.getY())-width; |
1224 | - canvas.draw_rectangle(p1.getX(), p1.getY(), p2.getX(), p2.getY(), outlinecolor); |
1225 | - canvas.draw_rectangle(leftx,topy,rightx,bottomy, color); |
1226 | - } |
1227 | - else{ |
1228 | - canvas.draw_rectangle(p1.getX(), p1.getY(), p2.getX(), p2.getY(), color); |
1229 | - } |
1230 | +{ |
1231 | + drawData->common->type = GRAPHICS_OBJECT_RECTANGLE; |
1232 | } |
1233 | |
1234 | // ==================== |
1235 | // Line |
1236 | // ==================== |
1237 | -Line::Line(Point p1, Point p2) |
1238 | -: _BBox(p1,p2), |
1239 | - arrow_type("none") |
1240 | -{} |
1241 | +Line::Line(Point p1, Point p2, std::string arrow_type) |
1242 | +: _BBox(p1,p2) |
1243 | +{ |
1244 | + drawData->common->type = GRAPHICS_OBJECT_LINE; |
1245 | + extra = new line_draw_msg(); |
1246 | + drawData->extra = extra; |
1247 | + extra->arrow_type = arrow_type; |
1248 | +} |
1249 | |
1250 | void Line::setArrow(std::string type){ |
1251 | if ( type == "first" || |
1252 | type == "last" || |
1253 | type == "both" || |
1254 | type == "none" ) |
1255 | - this->arrow_type = type; |
1256 | + extra->arrow_type = type; |
1257 | else{ |
1258 | std::cerr << "Line::setArrow(): Bad Type: " << type << std::endl; |
1259 | // TODO: Throw Exception |
1260 | } |
1261 | } |
1262 | |
1263 | -void Line::draw_command(myro_img& canvas){ |
1264 | - unsigned char color[] = {outline.R, outline.G, outline.B}; |
1265 | - //std::cerr<< "color.R: " << (int)color[0] << "color.G: " << (int)color[1] << "color.B: " << (int)color[2] << std::endl; |
1266 | - if ( arrow_type == "first" ){ |
1267 | - canvas.draw_arrow(p2.getX(), p2.getY(), p1.getX(), p1.getY(), color); |
1268 | - } |
1269 | - else if ( arrow_type == "last" ){ |
1270 | - canvas.draw_arrow(p1.getX(), p1.getY(), p2.getX(), p2.getY(), color); |
1271 | - } |
1272 | - else if ( arrow_type == "both" ){ |
1273 | - canvas.draw_arrow(p2.getX(), p2.getY(), p1.getX(), p1.getY(), color); |
1274 | - canvas.draw_arrow(p1.getX(), p1.getY(), p2.getX(), p2.getY(), color); |
1275 | - } |
1276 | - if ( this->width > 1 ){ |
1277 | - int x0=p1.getX(),x1=p2.getX(),y0=p1.getY(),y1=p2.getY(); |
1278 | - bool steep = abs(y0-y1) > abs(x0-x1); |
1279 | - if ( steep ){ |
1280 | - swap(x0,y0); |
1281 | - swap(x1,y1); |
1282 | - } |
1283 | - if ( x0 > x1 ){ |
1284 | - swap(x0,x1); |
1285 | - swap(y0,y1); |
1286 | - } |
1287 | - int deltax = x1-x0; |
1288 | - int deltay = abs(y1-y0); |
1289 | - float error = 0; |
1290 | - float deltaerr = (float)(deltay) / (deltax); |
1291 | - int ystep = y0 < y1 ? 1 : -1; |
1292 | - int y = y0; |
1293 | - // This is a sort of weird bresenhams thing where I sweep the end |
1294 | - // points so i can draw a "thick" line |
1295 | - for (int x = x0; x <= x1; x++){ |
1296 | - if ( steep ) |
1297 | - canvas.draw_circle(y,x,ceil(this->width/2.0),color); |
1298 | - else |
1299 | - canvas.draw_circle(x,y,ceil(this->width/2.0),color); |
1300 | - error += deltaerr; |
1301 | - if ( error >= 0.5 ){ |
1302 | - y += ystep; |
1303 | - error -= 1.0; |
1304 | - } |
1305 | - } |
1306 | - } |
1307 | - else if (this->width == 1){ |
1308 | - canvas.draw_line(p1.getX(), p1.getY(), p2.getX(), p2.getY(), color); |
1309 | - } |
1310 | -} |
1311 | - |
1312 | // ==================== |
1313 | // Polygon |
1314 | // ==================== |
1315 | Polygon::Polygon(std::vector<Point> points) |
1316 | -: pts(points.size(),2), points(points) |
1317 | { |
1318 | + drawData->common->type = GRAPHICS_OBJECT_POLYGON; |
1319 | + data = new polygon_draw_msg(); |
1320 | + drawData->data = data; |
1321 | + data->pts.assign(points.size(),2); |
1322 | + data->points = points; |
1323 | } |
1324 | |
1325 | std::vector<Point> Polygon::getPoints(){ |
1326 | - return points; |
1327 | -} |
1328 | - |
1329 | -std::vector<Point> Polygon::update_points(){ |
1330 | - Point p; |
1331 | - std::vector<Point> ret; |
1332 | - outline_lines.clear(); |
1333 | - outline_lines.push_back(Line(points[points.size()-1],points[0])); |
1334 | - |
1335 | - for (int i = 0; i < (int)points.size(); i++){ |
1336 | - if (i+1 < (int)points.size()) |
1337 | - outline_lines.push_back(Line(points[i],points[i+1])); |
1338 | - ret.push_back(points[i]); |
1339 | - p = this->points[i]; |
1340 | - pts(i,0) = p.getX(); |
1341 | - pts(i,1) = p.getY(); |
1342 | - } |
1343 | - for (int i = 0; i < (int)outline_lines.size(); i++) |
1344 | - outline_lines[i].setWidth(this->width); |
1345 | - return ret; |
1346 | -} |
1347 | - |
1348 | -void Polygon::draw_command(myro_img& canvas){ |
1349 | - unsigned char color[] = {fill.R, fill.G, fill.B}; |
1350 | - unsigned char border[] = {outline.R, outline.G, outline.B}; |
1351 | - |
1352 | - // TODO: I feel like this is somewhat of an expensive operation O(N) for |
1353 | - // the number of points, this should probably only be done when the points |
1354 | - // actually need to be updated when they've changed. |
1355 | - std::vector<Point> debug = update_points(); |
1356 | - |
1357 | - //canvas.draw_polygon(pts_bdr, border, 1, ~0U); |
1358 | - //canvas.draw_polygon(pts_bdr, border, 1, ~0U); |
1359 | - //canvas.draw_polygon(pts, color); |
1360 | - canvas.draw_polygon(pts, color); |
1361 | - if ( this->width == 1 ){ |
1362 | - canvas.draw_polygon(pts, border, 1, ~0U); |
1363 | - } else if ( this->width > 1 ){ |
1364 | - for (int i = 0; i < (int)outline_lines.size(); i++) |
1365 | - outline_lines[i].draw_command(canvas); |
1366 | - } |
1367 | - /* |
1368 | - std::cerr << debug.size() << " debug points" << std::endl; |
1369 | - for (int i = 0; i < (int)debug.size(); i++){ |
1370 | - canvas.draw_rectangle(debug[i].getX()-2,debug[i].getY()-2,debug[i].getX()+2,debug[i].getY()+2, red); |
1371 | - } |
1372 | - */ |
1373 | + return data->points; |
1374 | } |
1375 | |
1376 | void Polygon::move(int dx, int dy){ |
1377 | Point d(dx,dy); |
1378 | - for (int i = 0; i < (int)points.size(); i++){ |
1379 | - points[i] += d; |
1380 | + for (int i = 0; i < (int)data->points.size(); i++){ |
1381 | + data->points[i] += d; |
1382 | } |
1383 | - if ( this->canvas ) |
1384 | - this->canvas->check_and_update(); |
1385 | + if ( drawData->common->canvas ) |
1386 | + drawData->common->canvas->check_and_update(); |
1387 | } |
1388 | |
1389 | // ==================== |
1390 | // Text |
1391 | // ==================== |
1392 | Text::Text(Point anchor, std::string text) |
1393 | -: GraphicsObject(color_rgb(0,0,0), color_rgb(0,0,0), 0), |
1394 | - text(text), |
1395 | - font("helvetica"), |
1396 | - size(13), |
1397 | - background(false), |
1398 | - style("normal"), |
1399 | - anchor(anchor) |
1400 | -{} |
1401 | +: GraphicsObject(color_rgb(0,0,0), color_rgb(0,0,0), 0) |
1402 | +{ |
1403 | + drawData->common->type = GRAPHICS_OBJECT_TEXT; |
1404 | + data = new text_draw_msg(); |
1405 | + drawData->data = data; |
1406 | + data->text = text; |
1407 | + data->font = "helvetica"; |
1408 | + data->size = 13; |
1409 | + data->background = false; |
1410 | + data->style = "normal"; |
1411 | + data->anchor = anchor; |
1412 | +} |
1413 | |
1414 | -std::string Text::getText(){ return text; } |
1415 | -void Text::setText(std::string text){ this->text = text; } |
1416 | -Point Text::getAnchor(){ return anchor; } |
1417 | -void Text::setAnchor(Point anchor){ this->anchor=anchor; } |
1418 | +std::string Text::getText(){ return data->text; } |
1419 | +void Text::setText(std::string text){ data->text = text; } |
1420 | +Point Text::getAnchor(){ return data->anchor; } |
1421 | +void Text::setAnchor(Point anchor){ data->anchor=anchor; } |
1422 | void Text::setFace(std::string font){ |
1423 | // TODO |
1424 | //Joke's On you because I don't support any font other than the default! |
1425 | } |
1426 | void Text::setSize(int size){ |
1427 | - this->size = size; |
1428 | + data->size = size; |
1429 | } |
1430 | |
1431 | void Text::setStyle(std::string style){ |
1432 | @@ -562,24 +670,11 @@ |
1433 | void Text::setTextColor(std::string color){ this->setOutline(color); } |
1434 | |
1435 | void Text::drawBackground(bool background){ |
1436 | - this->background = background; |
1437 | -} |
1438 | - |
1439 | -void Text::draw_command(myro_img& canvas){ |
1440 | - unsigned char background[] = {fill.R, fill.G, fill.B}; |
1441 | - unsigned char text_color[] = {outline.R, outline.G, outline.B}; |
1442 | - if ( this->background ){ |
1443 | - canvas.draw_text(anchor.getX(), anchor.getY(), text.c_str(), |
1444 | - text_color, background, 1, size); |
1445 | - } |
1446 | - else{ |
1447 | - canvas.draw_text(anchor.getX(), anchor.getY(), text.c_str(), |
1448 | - text_color, 0, 1, size); |
1449 | - } |
1450 | + data->background = background; |
1451 | } |
1452 | |
1453 | void Text::move(int dx, int dy){ |
1454 | - anchor += Point(dx,dy); |
1455 | - if ( this->canvas ) |
1456 | - this->canvas->check_and_update(); |
1457 | + data->anchor += Point(dx,dy); |
1458 | + if ( drawData->common->canvas ) |
1459 | + drawData->common->canvas->check_and_update(); |
1460 | } |
1461 | |
1462 | === removed file 'src/GrayPicture.cpp' |
1463 | --- src/GrayPicture.cpp 2011-05-09 21:30:54 +0000 |
1464 | +++ src/GrayPicture.cpp 1970-01-01 00:00:00 +0000 |
1465 | @@ -1,73 +0,0 @@ |
1466 | -#define cimg_use_jpeg |
1467 | -#include "GrayPicture.h" |
1468 | -#include <cstdio> |
1469 | -#include "MyroInternals.h" |
1470 | - |
1471 | -using namespace std; |
1472 | - |
1473 | -GrayPicture::GrayPicture() : Picture() {}; |
1474 | - |
1475 | -GrayPicture::GrayPicture(int width, int height) |
1476 | -:Picture(width, height){ |
1477 | - image_data.assign(width,height,1,1,0) ; |
1478 | - this->width = width; |
1479 | - this->height = height; |
1480 | -} |
1481 | - |
1482 | -GrayPicture::GrayPicture(unsigned char * data, int width, int height) |
1483 | -:Picture(width, height){ |
1484 | - image_data.assign(data,width,height,1,1) ; |
1485 | - this->width = width; |
1486 | - this->height = height; |
1487 | -} |
1488 | - |
1489 | -GrayPicture::GrayPicture(GrayPicture& pic) |
1490 | -:Picture(pic.width, pic.height){ |
1491 | - image_data.assign(pic.image_data); |
1492 | - this->width = pic.width; |
1493 | - this->height = pic.height; |
1494 | - |
1495 | - image_data.assign(pic.image_data); |
1496 | -} |
1497 | - |
1498 | -Pixel GrayPicture::getPixel(int x, int y) { |
1499 | - if ( x >= width || y >= height ) |
1500 | - Picture::out_of_bounds_error(width,height,x,y); |
1501 | - Pixel result; |
1502 | - result.R = image_data(x,y,0,0); |
1503 | - result.G = result.R; |
1504 | - result.B = result.R; |
1505 | - return result; |
1506 | -} |
1507 | - |
1508 | -void GrayPicture::setPixel(int x, int y, Pixel pix) { |
1509 | - if ( x >= width || y >= height ) |
1510 | - Picture::out_of_bounds_error(width,height,x,y); |
1511 | - image_data(x,y,0,0) = pix.R; |
1512 | -} |
1513 | - |
1514 | -Picture* GrayPicture::clone(){ |
1515 | - Picture* newpic = new GrayPicture(*this); |
1516 | - return newpic; |
1517 | -} |
1518 | - |
1519 | -void GrayPicture::show(std::string windowname){ |
1520 | - displayMan.set_picture_window(image_data, windowname.c_str()); |
1521 | -} |
1522 | - |
1523 | -void GrayPicture::show(){ |
1524 | - const std::string windowname("Gray Picture"); |
1525 | - displayMan.set_picture_window(image_data, windowname.c_str()); |
1526 | - displayMan.block_on(windowname.c_str()); |
1527 | -} |
1528 | - |
1529 | -bool GrayPicture::loadPicture(const char* filename){ |
1530 | - image_data.load_jpeg(filename); |
1531 | - /* And we're done! */ |
1532 | - return true; |
1533 | -} |
1534 | - |
1535 | -void GrayPicture::savePicture(const char* filename){ |
1536 | - image_data.save_jpeg(filename); |
1537 | - /* And we're done! */ |
1538 | -} |
1539 | |
1540 | === modified file 'src/Picture.cpp' |
1541 | --- src/Picture.cpp 2011-04-17 13:57:45 +0000 |
1542 | +++ src/Picture.cpp 2011-07-22 02:32:35 +0000 |
1543 | @@ -1,6 +1,5 @@ |
1544 | #include "Picture.h" |
1545 | -#include "ColorPicture.h" |
1546 | -#include "GrayPicture.h" |
1547 | +#include "MyroInternals.h" |
1548 | #include <iostream> |
1549 | #include <sstream> |
1550 | #include <cstdlib> |
1551 | @@ -12,11 +11,97 @@ |
1552 | Picture::Picture(int width, int height) { |
1553 | this->width = width; |
1554 | this->height = height; |
1555 | + this->channels = 3; |
1556 | +} |
1557 | + |
1558 | +Picture::Picture(unsigned char * data, int channels, int width, int height){ |
1559 | + this->width = width; |
1560 | + this->height = height; |
1561 | + this->channels = channels; |
1562 | + |
1563 | + if ( channels == 3 ){ |
1564 | + image_data.assign(width,height,1,channels); |
1565 | + loadInterlacedImage(data); |
1566 | + } else { |
1567 | + image_data.assign(data,width,height,1,channels); |
1568 | + } |
1569 | +} |
1570 | + |
1571 | +Picture::Picture(Picture& pic) |
1572 | +{ |
1573 | + image_data.assign(pic.image_data); |
1574 | + this->width = pic.width; |
1575 | + this->height = pic.height; |
1576 | + this->channels = pic.channels; |
1577 | } |
1578 | |
1579 | Picture::~Picture() { |
1580 | } |
1581 | |
1582 | +Pixel Picture::getPixel(int x, int y) { |
1583 | + if ( x >= width || y >= height ) |
1584 | + Picture::out_of_bounds_error(width,height,x,y); |
1585 | + Pixel result; |
1586 | + if ( channels == 3 ){ |
1587 | + result.R = image_data(x,y,0,0); |
1588 | + result.G = image_data(x,y,0,1); |
1589 | + result.B = image_data(x,y,0,2); |
1590 | + } |
1591 | + else{ |
1592 | + result.R = result.G = result.B = image_data(x,y,0,0); |
1593 | + } |
1594 | + return result; |
1595 | +} |
1596 | + |
1597 | +void Picture::setPixel(int x, int y, Pixel pix) { |
1598 | + if ( x >= width || y >= height ) |
1599 | + Picture::out_of_bounds_error(width,height,x,y); |
1600 | + if ( channels == 3 ){ |
1601 | + image_data(x,y,0,0) = pix.R; |
1602 | + image_data(x,y,0,1) = pix.G; |
1603 | + image_data(x,y,0,2) = pix.B; |
1604 | + } else { |
1605 | + image_data(x,y,0,0) = pix.R; |
1606 | + } |
1607 | +} |
1608 | + |
1609 | +void Picture::show(std::string windowname){ |
1610 | + displayMan.set_picture_window(image_data, windowname.c_str()); |
1611 | +} |
1612 | + |
1613 | +void Picture::show(){ |
1614 | + const std::string windowname = "Picture"; |
1615 | + displayMan.set_picture_window(image_data, windowname.c_str()); |
1616 | + displayMan.block_on(windowname.c_str()); |
1617 | +} |
1618 | + |
1619 | +// TODO: Implement this! |
1620 | +PicturePtr Picture::clone(){ |
1621 | + PicturePtr p(new Picture(*this)); |
1622 | + //Picture* newpic = new ColorPicture(*this); |
1623 | + //return newpic; |
1624 | + return p; |
1625 | +} |
1626 | + |
1627 | +bool Picture::loadPicture(const char* filename){ |
1628 | + /* And we're done! */ |
1629 | + image_data.load_jpeg(filename); |
1630 | + return true; |
1631 | +} |
1632 | + |
1633 | +void Picture::savePicture(const char* filename){ |
1634 | + /* And we're done! */ |
1635 | + image_data.save_jpeg(filename); |
1636 | +} |
1637 | + |
1638 | +void Picture::loadInterlacedImage(unsigned char* img){ |
1639 | + cimg_forXY(image_data,x,y){ |
1640 | + image_data(x,y,0,0) = img[y*width*3+x*3]; |
1641 | + image_data(x,y,0,1) = img[y*width*3+x*3+1]; |
1642 | + image_data(x,y,0,2) = img[y*width*3+x*3+2]; |
1643 | + } |
1644 | +} |
1645 | + |
1646 | int Picture::getHeight() { |
1647 | return height; |
1648 | } |
1649 | @@ -64,43 +149,43 @@ |
1650 | */ |
1651 | |
1652 | // Below are the C-style Wrappers for the objects... |
1653 | -int getWidth(Picture *p) |
1654 | +int getWidth(PicturePtr p) |
1655 | { |
1656 | return p->getWidth(); |
1657 | } |
1658 | |
1659 | -int getHeight(Picture *p) |
1660 | +int getHeight(PicturePtr p) |
1661 | { |
1662 | return p->getHeight(); |
1663 | } |
1664 | |
1665 | -void show(Picture *p, std::string windowname) |
1666 | +void show(PicturePtr p, std::string windowname) |
1667 | { |
1668 | p->show(windowname); |
1669 | } |
1670 | |
1671 | -void show(Picture *p) |
1672 | +void show(PicturePtr p) |
1673 | { |
1674 | p->show(); |
1675 | } |
1676 | |
1677 | -Pixel getPixel(Picture *p, int x, int y) |
1678 | +Pixel getPixel(PicturePtr p, int x, int y) |
1679 | { |
1680 | return p->getPixel(x, y); |
1681 | } |
1682 | |
1683 | -int getPixelValue_grey(Picture *p, int x, int y) |
1684 | +int getPixelValue_grey(PicturePtr p, int x, int y) |
1685 | { |
1686 | Pixel P=p->getPixel(x,y); |
1687 | return P.R; |
1688 | } |
1689 | |
1690 | -void setPixel(Picture* p, int x, int y, Pixel pix) |
1691 | +void setPixel(PicturePtr p, int x, int y, Pixel pix) |
1692 | { |
1693 | p->setPixel(x, y, pix); |
1694 | } |
1695 | |
1696 | -void setPixelColor(Picture *p, int x, int y, int R, int G, int B) |
1697 | +void setPixelColor(PicturePtr p, int x, int y, int R, int G, int B) |
1698 | { |
1699 | Pixel P=p->getPixel(x,y); |
1700 | P.R=R; |
1701 | @@ -109,26 +194,22 @@ |
1702 | p->setPixel(x, y, P); |
1703 | } |
1704 | |
1705 | -Picture* loadPicture(const char* filename){ |
1706 | - Picture* ret = new ColorPicture(); |
1707 | - if ( ret->loadPicture(filename) ) |
1708 | - return ret; |
1709 | - delete ret; |
1710 | - ret = new GrayPicture(); |
1711 | - if ( ret->loadPicture(filename) ) |
1712 | - return ret; |
1713 | - |
1714 | - return NULL; |
1715 | +PicturePtr loadPicture(const char* filename){ |
1716 | + PicturePtr ret(new Picture()); |
1717 | + if ( ret->loadPicture(filename) ) |
1718 | + return ret; |
1719 | + // TODO: return NULL; |
1720 | + return ret; |
1721 | } |
1722 | |
1723 | -void loadPicture(Picture * p, const char* filename){ |
1724 | +void loadPicture(PicturePtr p, const char* filename){ |
1725 | p->loadPicture(filename); |
1726 | } |
1727 | -void savePicture(Picture * p, const char* filename){ |
1728 | +void savePicture(PicturePtr p, const char* filename){ |
1729 | p->savePicture(filename); |
1730 | } |
1731 | |
1732 | -Picture* clone(Picture* p){ |
1733 | +PicturePtr clone(PicturePtr p){ |
1734 | return p->clone(); |
1735 | } |
1736 | // END C-style Wrappers for the objects... |
1737 | |
1738 | === modified file 'src/Scribbler.cpp' |
1739 | --- src/Scribbler.cpp 2011-03-26 19:06:15 +0000 |
1740 | +++ src/Scribbler.cpp 2011-07-22 02:32:35 +0000 |
1741 | @@ -8,8 +8,6 @@ |
1742 | #include <iostream> |
1743 | #include <cstring> |
1744 | #include <cstdio> |
1745 | -#include "ColorPicture.h" |
1746 | -#include "GrayPicture.h" |
1747 | #include "VideoStream.h" |
1748 | #include <boost/thread/mutex.hpp> |
1749 | |
1750 | @@ -1224,7 +1222,9 @@ |
1751 | retval = (void*)ptr_bright; |
1752 | } |
1753 | else if( sensor == "picture" ) { |
1754 | - retval = (void*)this->takePicture(position.at(0)); |
1755 | + retval = NULL; |
1756 | + std::cerr << "Use takePicture() instead, don't use get(\"picture\")" << std::endl; |
1757 | + //retval = (void*)this->takePicture(position.at(0)); |
1758 | } |
1759 | else |
1760 | std::cout << "invalid sensor name: " << sensor << std::endl; |
1761 | @@ -1784,47 +1784,47 @@ |
1762 | return dataV; |
1763 | } |
1764 | |
1765 | -Picture * Scribbler::takePicture(std::string type) { |
1766 | +PicturePtr Scribbler::takePicture(std::string type) { |
1767 | unsigned char * imageBuffer = NULL; |
1768 | int size; |
1769 | |
1770 | - Picture * image = NULL; |
1771 | + PicturePtr image; |
1772 | |
1773 | // Don't take a picture if we're not connected. |
1774 | if ( con == NULL ) |
1775 | - return NULL; |
1776 | + return image; |
1777 | |
1778 | //pthread_mutex_lock(this->image_lock); |
1779 | boost::mutex::scoped_lock l(*(this->image_lock)); |
1780 | if(type == "color") { |
1781 | imageBuffer = grab_array_rgb(); |
1782 | - image = new ColorPicture(imageBuffer, 256, 192); |
1783 | + image = PicturePtr(new Picture(imageBuffer, 3, 256, 192)); |
1784 | } |
1785 | else if( type == "gray" || type == "grey") { |
1786 | conf_window(0, 1, 0, 255, 191, 2, 2); |
1787 | imageBuffer = grab_gray_array(); |
1788 | conf_gray_window(0, 2, 0, 128, 191, 1, 1); |
1789 | - image = new GrayPicture(imageBuffer, 256, 192); |
1790 | + image = PicturePtr(new Picture(imageBuffer, 1, 256, 192)); |
1791 | } |
1792 | else if( type == "blob" ) { |
1793 | imageBuffer = grab_blob_array(); |
1794 | - image = new ColorPicture(imageBuffer, 256, 192); |
1795 | + image = PicturePtr(new Picture(imageBuffer, 3, 256, 192)); |
1796 | } |
1797 | else if( type == "jpeg" ) { |
1798 | imageBuffer = grab_jpeg_color(1,size); |
1799 | - image = new ColorPicture(imageBuffer, 256, 192); |
1800 | + image = PicturePtr(new Picture(imageBuffer, 3, 256, 192)); |
1801 | } |
1802 | else if( type == "jpeg-fast") { |
1803 | imageBuffer = grab_jpeg_color(0,size); |
1804 | - image = new ColorPicture(imageBuffer, 256, 192); |
1805 | + image = PicturePtr(new Picture(imageBuffer, 3, 256, 192)); |
1806 | } |
1807 | else if( type == "grayjpeg") { |
1808 | imageBuffer = grab_jpeg_gray(1,size); |
1809 | - image = new GrayPicture(imageBuffer, 256, 192); |
1810 | + image = PicturePtr(new Picture(imageBuffer, 1, 256, 192)); |
1811 | } |
1812 | else if( type == "grayjpeg-fast" ) { |
1813 | imageBuffer = grab_jpeg_gray(0,size); |
1814 | - image = new GrayPicture(imageBuffer, 256, 192); |
1815 | + image = PicturePtr(new Picture(imageBuffer, 1, 256, 192)); |
1816 | } |
1817 | else { |
1818 | std::cerr << "Scribbler::takePicture(): Warning: Unknown picture type: " << type << std::endl; |
1819 | |
1820 | === modified file 'src/VideoStream.cpp' |
1821 | --- src/VideoStream.cpp 2011-05-09 21:30:54 +0000 |
1822 | +++ src/VideoStream.cpp 2011-07-22 02:32:35 +0000 |
1823 | @@ -62,8 +62,8 @@ |
1824 | } |
1825 | |
1826 | void DisplayThread::run() { |
1827 | - Picture* img_cur=NULL; |
1828 | - Picture* img_new=NULL; |
1829 | + PicturePtr img_cur; |
1830 | + PicturePtr img_new; |
1831 | |
1832 | while(!this->stopRequested){ |
1833 | //std::cerr << "DisplayThread::run()" << std::endl; |
1834 | @@ -84,7 +84,7 @@ |
1835 | } |
1836 | } |
1837 | // If we got a NULL picture, shut down thread |
1838 | - if ( img_new == NULL ) |
1839 | + if ( img_new.get() == NULL ) |
1840 | this->stopRequested = true; |
1841 | |
1842 | if ( this->stopRequested ) break; |
1843 | @@ -100,9 +100,10 @@ |
1844 | |
1845 | show(img_new, "Video Stream"); |
1846 | |
1847 | + /* |
1848 | if ( img_cur != NULL ){ |
1849 | delete img_cur; |
1850 | - } |
1851 | + }*/ |
1852 | img_cur = img_new; |
1853 | |
1854 | //boost::thread::yield(); |
1855 | |
1856 | === modified file 'test/graphics/circles.cpp' |
1857 | --- test/graphics/circles.cpp 2011-05-02 05:46:27 +0000 |
1858 | +++ test/graphics/circles.cpp 2011-07-22 02:32:35 +0000 |
1859 | @@ -1,26 +1,35 @@ |
1860 | #include <Myro.h> |
1861 | |
1862 | -static const int num_of_circles = 1000; |
1863 | -static const int width=500; |
1864 | -static const int height=500; |
1865 | static const int max_circle_radius=50; |
1866 | |
1867 | +Color makeColor(){ |
1868 | + //creates a new color using random RGB values |
1869 | + int red = rand()%256; |
1870 | + int green = rand()%256; |
1871 | + int blue = rand()%256; |
1872 | + return color_rgb(red, green,blue); |
1873 | +} |
1874 | + |
1875 | int main(){ |
1876 | - GraphWin win("Circles", width,height); |
1877 | - Circle* c[num_of_circles]; |
1878 | - for (int i = 0; i < num_of_circles; i++){ |
1879 | - int x=rand()%width; |
1880 | - int y=rand()%height; |
1881 | - int r=rand()%max_circle_radius; |
1882 | - int R=rand()%255; |
1883 | - int G=rand()%255; |
1884 | - int B=rand()%255; |
1885 | - c[i] = new Circle(Point(x,y),r); |
1886 | - c[i]->setFill(color_rgb(R,G,B)); |
1887 | - c[i]->draw(win); |
1888 | - wait(0.01); |
1889 | + const int width=500; |
1890 | + const int height=500; |
1891 | + GraphWin myCanvas("Circles",width,height); |
1892 | + myCanvas.setBackground("white"); |
1893 | + // draw a bunch of random |
1894 | + // circles with random |
1895 | + // colors. |
1896 | + int N = 500; |
1897 | + for (int i = 0; i < N; i++) { |
1898 | + // pick random center |
1899 | + // point and radius |
1900 | + // in the window |
1901 | + int x = rand()%width; |
1902 | + int y = rand()%height; |
1903 | + int r = rand()%20 + 5;//randrange(5, 25); |
1904 | + Circle c(Point(x, y), r); |
1905 | + // select a random color |
1906 | + c.setFill(makeColor()); |
1907 | + c.draw(myCanvas); |
1908 | } |
1909 | - wait(5); |
1910 | - win.close(); |
1911 | - win.waitWinClosed(); |
1912 | + myCanvas.waitWinClosed(); |
1913 | } |
1914 | |
1915 | === modified file 'test/take_snapshot.cpp' |
1916 | --- test/take_snapshot.cpp 2011-04-09 20:14:25 +0000 |
1917 | +++ test/take_snapshot.cpp 2011-07-22 02:32:35 +0000 |
1918 | @@ -43,7 +43,7 @@ |
1919 | if(quit == "E" || quit == "Exit" || quit == "e" || quit == "exit") |
1920 | close = 1; |
1921 | } |
1922 | - Picture* pic = robot.takePicture( argv[1] ); |
1923 | + PicturePtr pic = robot.takePicture( argv[1] ); |
1924 | pic->savePicture("snapshot.jpg"); |
1925 | video.endStream(); |
1926 | //sleep(5); |
1927 | |
1928 | === modified file 'test/test_blob.cpp' |
1929 | --- test/test_blob.cpp 2010-12-22 23:55:57 +0000 |
1930 | +++ test/test_blob.cpp 2011-07-22 02:32:35 +0000 |
1931 | @@ -17,7 +17,7 @@ |
1932 | ~PictureInPicture(){} |
1933 | |
1934 | virtual void filter(Picture* image) { |
1935 | - Picture* colorImage = rob.takePicture("jpeg"); |
1936 | + PicturePtr colorImage = rob.takePicture("jpeg"); |
1937 | |
1938 | int height = image->getHeight(); |
1939 | int width = image->getWidth(); |
1940 | @@ -34,7 +34,6 @@ |
1941 | } |
1942 | } |
1943 | |
1944 | - delete colorImage; |
1945 | } |
1946 | |
1947 | protected: |
1948 | @@ -142,8 +141,8 @@ |
1949 | int train = 0; |
1950 | string toTrain; |
1951 | |
1952 | - Picture* showpic = robot.takePicture(); |
1953 | - Picture* trainpic = showpic->clone(); |
1954 | + PicturePtr showpic = robot.takePicture(); |
1955 | + PicturePtr trainpic = showpic->clone(); |
1956 | while(!train) { |
1957 | for(int h = y1; h < y2; h++) { |
1958 | for(int w = x1; w < x2; w++) { |
1959 | @@ -158,9 +157,6 @@ |
1960 | cin >> toTrain; |
1961 | if(toTrain == "y" || toTrain == "yes") |
1962 | train = 1; |
1963 | - else { |
1964 | - delete showpic; |
1965 | - } |
1966 | } |
1967 | robot.conf_rle_range(trainpic->getRawImage(), x1, y1, x2, y2); |
1968 | |
1969 | |
1970 | === modified file 'test/test_camera.cpp' |
1971 | --- test/test_camera.cpp 2010-12-12 15:56:34 +0000 |
1972 | +++ test/test_camera.cpp 2011-07-22 02:32:35 +0000 |
1973 | @@ -15,24 +15,20 @@ |
1974 | cout << "Testing Color, if image looks correct close display" << endl; |
1975 | cout << "If not, try restarting robot, and rerunning test" << endl; |
1976 | |
1977 | - Picture* img = robot.takePicture("color"); |
1978 | + PicturePtr img = robot.takePicture("color"); |
1979 | img->show(); |
1980 | - delete img; |
1981 | |
1982 | cout << "Testing Grayscale" << endl; |
1983 | img = robot.takePicture("gray"); |
1984 | img->show(); |
1985 | - delete img; |
1986 | |
1987 | cout << "Testing Jpeg Color" << endl; |
1988 | img = robot.takePicture("jpeg"); |
1989 | img->show(); |
1990 | - delete img; |
1991 | |
1992 | cout << "Testing Jpeg Gray" << endl; |
1993 | img = robot.takePicture("grayjpeg"); |
1994 | img->show(); |
1995 | - delete img; |
1996 | |
1997 | cout << "All Camera Test Completed\n"; |
1998 | |
1999 | |
2000 | === modified file 'test/test_camera_fourwindow.cpp' |
2001 | --- test/test_camera_fourwindow.cpp 2011-04-17 12:51:20 +0000 |
2002 | +++ test/test_camera_fourwindow.cpp 2011-07-22 02:32:35 +0000 |
2003 | @@ -15,24 +15,20 @@ |
2004 | cout << "Testing Color, if image looks correct close display" << endl; |
2005 | cout << "If not, try restarting robot, and rerunning test" << endl; |
2006 | |
2007 | - Picture* color = robot.takePicture("color"); |
2008 | + PicturePtr color = robot.takePicture("color"); |
2009 | color->show("Color"); |
2010 | - //delete color; |
2011 | |
2012 | cout << "Testing Grayscale" << endl; |
2013 | - Picture* gray = robot.takePicture("gray"); |
2014 | + PicturePtr gray = robot.takePicture("gray"); |
2015 | gray->show("Gray"); |
2016 | - //delete gray; |
2017 | |
2018 | cout << "Testing Jpeg Color" << endl; |
2019 | - Picture* jpeg = robot.takePicture("jpeg"); |
2020 | + PicturePtr jpeg = robot.takePicture("jpeg"); |
2021 | jpeg->show("Color JPEG"); |
2022 | - //delete jpeg; |
2023 | |
2024 | cout << "Testing Jpeg Gray" << endl; |
2025 | - Picture* grayjpeg = robot.takePicture("grayjpeg"); |
2026 | + PicturePtr grayjpeg = robot.takePicture("grayjpeg"); |
2027 | grayjpeg->show("Gray JPEG"); |
2028 | - //delete grayjpeg; |
2029 | |
2030 | cout << "All Camera Test Completed\n"; |
2031 | cout << "Waiting 10 seconds" << endl; |
2032 | |
2033 | === modified file 'test/test_camera_nonblocking.cpp' |
2034 | --- test/test_camera_nonblocking.cpp 2011-04-17 12:51:20 +0000 |
2035 | +++ test/test_camera_nonblocking.cpp 2011-07-22 02:32:35 +0000 |
2036 | @@ -15,17 +15,17 @@ |
2037 | cout << "Testing Color, if image looks correct close display" << endl; |
2038 | cout << "If not, try restarting robot, and rerunning test" << endl; |
2039 | |
2040 | - Picture* color = robot.takePicture("color"); |
2041 | + PicturePtr color = robot.takePicture("color"); |
2042 | show(color, "Image"); |
2043 | wait(0.5); |
2044 | |
2045 | cout << "Testing Grayscale" << endl; |
2046 | - Picture* gray = robot.takePicture("gray"); |
2047 | + PicturePtr gray = robot.takePicture("gray"); |
2048 | gray->show("Image"); |
2049 | wait(0.5); |
2050 | |
2051 | cout << "Testing Jpeg Color" << endl; |
2052 | - Picture* img = robot.takePicture("jpeg"); |
2053 | + PicturePtr img = robot.takePicture("jpeg"); |
2054 | show(img,"Image"); |
2055 | wait(0.5); |
2056 |