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.

178 lines
6.1KB

  1. //
  2. // "$Id: unittest_images.cxx 8609 2011-04-20 13:38:28Z AlbrechtS $"
  3. //
  4. // Unit tests for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2011 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. #include <FL/Fl_Box.H>
  28. #include <FL/fl_draw.H>
  29. // Note: currently (March 2010) fl_draw_image() supports transparency with
  30. // alpha channel only on Apple (Mac OS X), but Fl_RGB_Image->draw()
  31. // supports transparency on all platforms !
  32. //
  33. //------- test the image drawing capabilities of this implementation ----------
  34. //
  35. // Some parameters for fine tuning for developers - their
  36. // default values ought to be: CB=1, DX=0, LX=0, IMG=1
  37. #define CB (1) // 1 to show the checker board background for alpha images, 0 otherwise
  38. #define DX (0) // additional (undefined (0)) pixels per line, must be >= 0
  39. // ignored (irrelevant), if LX == 0 (see below)
  40. #define LX (0) // 0 for default: ld() = 0, i.e. ld() defaults (internally) to w()*d()
  41. // +1: ld() = (w() + DX) * d()
  42. // -1 to flip image vertically: ld() = - ((w() + DX) * d())
  43. #define IMG (1) // 1 to use Fl_RGB_Image for drawing images,
  44. // 0 to use fl_draw_image() instead.
  45. // Note: as of April 2011, only 1 (Fl_RGB_Image) works correctly with alpha
  46. // channel, 0 (fl_draw_image()) ignores the alpha channel (FLTK 1.3.0).
  47. // There are plans to support alpha in fl_draw_image() in FLTK 1.3.x,
  48. // but not in FLTK 1.1.x .
  49. class ImageTest : public Fl_Box {
  50. public:
  51. static Fl_Widget *create() {
  52. int x, y;
  53. uchar *dg, *dga, *drgb, *drgba;
  54. dg = img_gray = (uchar*)malloc((128+DX)*128*1);
  55. dga = img_gray_a = (uchar*)malloc((128+DX)*128*2);
  56. drgb = img_rgb = (uchar*)malloc((128+DX)*128*3);
  57. drgba = img_rgba = (uchar*)malloc((128+DX)*128*4);
  58. for (y=0; y<128; y++) {
  59. for (x=0; x<128; x++) {
  60. *drgba++ = *drgb++ = *dga++ = *dg++ = y<<1;
  61. *drgba++ = *drgb++ = x<<1;
  62. *drgba++ = *drgb++ = (127-x)<<1;
  63. *drgba++ = *dga++ = x+y;
  64. }
  65. if (DX > 0 && LX != 0) {
  66. memset(dg, 0,1*DX); dg += 1*DX;
  67. memset(dga, 0,2*DX); dga += 2*DX;
  68. memset(drgb, 0,3*DX); drgb += 3*DX;
  69. memset(drgba,0,4*DX); drgba += 4*DX;
  70. }
  71. }
  72. if (LX<0) {
  73. img_gray += 127*(128+DX);
  74. img_gray_a += 127*(128+DX)*2;
  75. img_rgb += 127*(128+DX)*3;
  76. img_rgba += 127*(128+DX)*4;
  77. }
  78. i_g = new Fl_RGB_Image (img_gray ,128,128,1,LX*(128+DX));
  79. i_ga = new Fl_RGB_Image (img_gray_a,128,128,2,LX*(128+DX)*2);
  80. i_rgb = new Fl_RGB_Image (img_rgb, 128,128,3,LX*(128+DX)*3);
  81. i_rgba = new Fl_RGB_Image (img_rgba, 128,128,4,LX*(128+DX)*4);
  82. return new ImageTest(TESTAREA_X, TESTAREA_Y, TESTAREA_W, TESTAREA_H);
  83. }
  84. static uchar *img_gray;
  85. static uchar *img_gray_a;
  86. static uchar *img_rgb;
  87. static uchar *img_rgba;
  88. static Fl_RGB_Image *i_g;
  89. static Fl_RGB_Image *i_ga;
  90. static Fl_RGB_Image *i_rgb;
  91. static Fl_RGB_Image *i_rgba;
  92. ImageTest(int x, int y, int w, int h) : Fl_Box(x, y, w, h) {
  93. label("Testing Image Drawing\n\n"
  94. "This test renders four images, two of them with a checker board\n"
  95. "visible through the graphics. Color and gray gradients should be\n"
  96. "visible. This does not test any image formats such as JPEG.");
  97. align(FL_ALIGN_INSIDE|FL_ALIGN_BOTTOM|FL_ALIGN_LEFT|FL_ALIGN_WRAP);
  98. box(FL_BORDER_BOX);
  99. }
  100. void draw() {
  101. Fl_Box::draw();
  102. // top left: RGB
  103. int xx = x()+10, yy = y()+10;
  104. fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130);
  105. #if IMG
  106. i_rgb->draw(xx+1,yy+1);
  107. #else
  108. fl_draw_image(img_rgb, xx+1, yy+1, 128, 128, 3, LX*((128+DX)*3));
  109. #endif
  110. fl_draw("RGB", xx+134, yy+64);
  111. // bottom left: RGBA
  112. xx = x()+10; yy = y()+10+134;
  113. fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130); // black frame
  114. fl_color(FL_WHITE); fl_rectf(xx+1, yy+1, 128, 128); // white background
  115. #if CB // checker board
  116. fl_color(FL_BLACK); fl_rectf(xx+65, yy+1, 64, 64);
  117. fl_color(FL_BLACK); fl_rectf(xx+1, yy+65, 64, 64);
  118. #endif
  119. #if IMG
  120. i_rgba->draw(xx+1,yy+1);
  121. #else
  122. fl_draw_image(img_rgba, xx+1, yy+1, 128, 128, 4, LX*((128+DX)*4));
  123. #endif
  124. fl_color(FL_BLACK); fl_draw("RGBA", xx+134, yy+64);
  125. // top right: Gray
  126. xx = x()+10+200; yy = y()+10;
  127. fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130);
  128. #if IMG
  129. i_g->draw(xx+1,yy+1);
  130. #else
  131. fl_draw_image(img_gray, xx+1, yy+1, 128, 128, 1, LX*((128+DX)*1));
  132. #endif
  133. fl_draw("Gray", xx+134, yy+64);
  134. // bottom right: Gray+Alpha
  135. xx = x()+10+200; yy = y()+10+134;
  136. fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130); // black frame
  137. fl_color(FL_WHITE); fl_rectf(xx+1, yy+1, 128, 128); // white background
  138. #if CB // checker board
  139. fl_color(FL_BLACK); fl_rectf(xx+65, yy+1, 64, 64);
  140. fl_color(FL_BLACK); fl_rectf(xx+1, yy+65, 64, 64);
  141. #endif
  142. #if IMG
  143. i_ga->draw(xx+1,yy+1);
  144. #else
  145. fl_draw_image(img_gray_a, xx+1, yy+1, 128, 128, 2, LX*((128+DX)*2));
  146. #endif
  147. fl_color(FL_BLACK); fl_draw("Gray+Alpha", xx+134, yy+64);
  148. }
  149. };
  150. uchar *ImageTest::img_gray = 0;
  151. uchar *ImageTest::img_gray_a = 0;
  152. uchar *ImageTest::img_rgb = 0;
  153. uchar *ImageTest::img_rgba = 0;
  154. Fl_RGB_Image *ImageTest::i_g = 0;
  155. Fl_RGB_Image *ImageTest::i_ga = 0;
  156. Fl_RGB_Image *ImageTest::i_rgb = 0;
  157. Fl_RGB_Image *ImageTest::i_rgba = 0;
  158. UnitTest images("drawing images", ImageTest::create);
  159. //
  160. // End of "$Id: unittest_images.cxx 8609 2011-04-20 13:38:28Z AlbrechtS $"
  161. //