You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.5KB

  1. //
  2. // "$Id: image.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Fl_Image test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Notice that Fl_Image is for a static, multiple-reuse image, such
  7. // as an icon or postage stamp. Use fl_draw_image to go directly
  8. // from an buffered image that changes often.
  9. //
  10. // Copyright 1998-2010 by Bill Spitzak and others.
  11. //
  12. // This library is free software; you can redistribute it and/or
  13. // modify it under the terms of the GNU Library General Public
  14. // License as published by the Free Software Foundation; either
  15. // version 2 of the License, or (at your option) any later version.
  16. //
  17. // This library is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. // Library General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU Library General Public
  23. // License along with this library; if not, write to the Free Software
  24. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  25. // USA.
  26. //
  27. // Please report all bugs and problems on the following page:
  28. //
  29. // http://www.fltk.org/str.php
  30. //
  31. #include <FL/Fl.H>
  32. #include <FL/Fl_Double_Window.H>
  33. #include <FL/Fl_Button.H>
  34. #include <FL/Fl_Image.H>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <math.h>
  38. int width = 100;
  39. int height = 100;
  40. uchar *image;
  41. void make_image() {
  42. image = new uchar[4*width*height];
  43. uchar *p = image;
  44. for (int y = 0; y < height; y++) {
  45. double Y = double(y)/(height-1);
  46. for (int x = 0; x < width; x++) {
  47. double X = double(x)/(width-1);
  48. *p++ = uchar(255*((1-X)*(1-Y))); // red in upper-left
  49. *p++ = uchar(255*((1-X)*Y)); // green in lower-left
  50. *p++ = uchar(255*(X*Y)); // blue in lower-right
  51. X -= 0.5;
  52. Y -= 0.5;
  53. int alpha = (int)(255 * sqrt(X * X + Y * Y));
  54. if (alpha < 255) *p++ = uchar(alpha); // alpha transparency
  55. else *p++ = 255;
  56. Y += 0.5;
  57. }
  58. }
  59. }
  60. #include <FL/Fl_Toggle_Button.H>
  61. Fl_Toggle_Button *leftb,*rightb,*topb,*bottomb,*insideb,*overb,*inactb;
  62. Fl_Button *b;
  63. Fl_Double_Window *w;
  64. void button_cb(Fl_Widget *,void *) {
  65. int i = 0;
  66. if (leftb->value()) i |= FL_ALIGN_LEFT;
  67. if (rightb->value()) i |= FL_ALIGN_RIGHT;
  68. if (topb->value()) i |= FL_ALIGN_TOP;
  69. if (bottomb->value()) i |= FL_ALIGN_BOTTOM;
  70. if (insideb->value()) i |= FL_ALIGN_INSIDE;
  71. if (overb->value()) i |= FL_ALIGN_TEXT_OVER_IMAGE;
  72. b->align(i);
  73. if (inactb->value()) b->deactivate();
  74. else b->activate();
  75. w->redraw();
  76. }
  77. #include <FL/x.H>
  78. #if !defined(WIN32) && !defined(__APPLE__)
  79. #include "list_visuals.cxx"
  80. #endif
  81. int visid = -1;
  82. int arg(int argc, char **argv, int &i) {
  83. if (argv[i][1] == 'v') {
  84. if (i+1 >= argc) return 0;
  85. visid = atoi(argv[i+1]);
  86. i += 2;
  87. return 2;
  88. }
  89. return 0;
  90. }
  91. int main(int argc, char **argv) {
  92. #if !defined(WIN32) && !defined(__APPLE__)
  93. int i = 1;
  94. Fl::args(argc,argv,i,arg);
  95. if (visid >= 0) {
  96. fl_open_display();
  97. XVisualInfo templt; int num;
  98. templt.visualid = visid;
  99. fl_visual = XGetVisualInfo(fl_display, VisualIDMask, &templt, &num);
  100. if (!fl_visual) {
  101. fprintf(stderr, "No visual with id %d, use one of:\n",visid);
  102. list_visuals();
  103. exit(1);
  104. }
  105. fl_colormap = XCreateColormap(fl_display, RootWindow(fl_display,fl_screen),
  106. fl_visual->visual, AllocNone);
  107. fl_xpixel(FL_BLACK); // make sure black is allocated in overlay visuals
  108. } else {
  109. Fl::visual(FL_RGB);
  110. }
  111. #endif
  112. Fl_Double_Window window(400,400); ::w = &window;
  113. window.color(FL_WHITE);
  114. Fl_Button b(140,160,120,120,"Image w/Alpha"); ::b = &b;
  115. Fl_RGB_Image *rgb;
  116. Fl_Image *dergb;
  117. make_image();
  118. rgb = new Fl_RGB_Image(image, width, height,4);
  119. dergb = rgb->copy();
  120. dergb->inactive();
  121. b.image(rgb);
  122. b.deimage(dergb);
  123. leftb = new Fl_Toggle_Button(25,50,50,25,"left");
  124. leftb->callback(button_cb);
  125. rightb = new Fl_Toggle_Button(75,50,50,25,"right");
  126. rightb->callback(button_cb);
  127. topb = new Fl_Toggle_Button(125,50,50,25,"top");
  128. topb->callback(button_cb);
  129. bottomb = new Fl_Toggle_Button(175,50,50,25,"bottom");
  130. bottomb->callback(button_cb);
  131. insideb = new Fl_Toggle_Button(225,50,50,25,"inside");
  132. insideb->callback(button_cb);
  133. overb = new Fl_Toggle_Button(25,75,100,25,"text over");
  134. overb->callback(button_cb);
  135. inactb = new Fl_Toggle_Button(125,75,100,25,"inactive");
  136. inactb->callback(button_cb);
  137. window.resizable(window);
  138. window.end();
  139. window.show(argc, argv);
  140. return Fl::run();
  141. }
  142. //
  143. // End of "$Id: image.cxx 7903 2010-11-28 21:06:39Z matt $".
  144. //