1 //
2 // "$Id$"
3 //
4 // Unit tests for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2016 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file.  If this
10 // file is missing or damaged, see the license at:
11 //
12 //     http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 //     http://www.fltk.org/str.php
17 //
18 
19 #include <FL/Fl_Box.H>
20 #include <FL/fl_draw.H>
21 
22 // Note: currently (March 2010) fl_draw_image() supports transparency with
23 //	 alpha channel only on Apple (Mac OS X), but Fl_RGB_Image->draw()
24 //	 supports transparency on all platforms !
25 
26 //
27 //------- test the image drawing capabilities of this implementation ----------
28 //
29 
30 // Parameters for fine tuning for developers.
31 // Default values: CB=1, DX=0, IMG=1, LX=0, FLIPH=0
32 
33 #define CB (1)    // 1 to show the checker board background for alpha images,
34 		  // 0 otherwise
35 #define DX (0)	  // additional (undefined (0)) pixels per line, must be >= 0
36 		  // ignored (irrelevant), if LX == 0 (see below)
37 #define IMG (1)	  // 1 to use Fl_RGB_Image for drawing images with transparency,
38 		  // 0 to use fl_draw_image() instead.
39 		  // Note: as of Feb 2016, only 1 (Fl_RGB_Image) works with
40 		  // alpha channel, 0 (fl_draw_image()) ignores the alpha
41 		  // channel (FLTK 1.3.x).
42 		  // There are plans to support transparency (alpha channel)
43 		  // in fl_draw_image() in FLTK 1.4.0 and/or later.
44 #define LX (0)	  // 0 for default: ld() = 0, i.e. ld() defaults (internally) to w()*d()
45 		  // +1: ld() = (w() + DX) * d()
46 		  // -1 to flip image vertically: ld() = - ((w() + DX) * d())
47 #define FLIPH (0) // 1 = Flip image horizontally (only if IMG == 0)
48 		  // 0 = Draw image normal, w/o horizontal flipping
49 
50 // ----------------------------------------------------------------------
51 //  Test scenario for fl_draw_image() with pos. and neg. d and ld args:
52 // ----------------------------------------------------------------------
53 //  (1) set IMG   =  0:	normal, but w/o transparency: no checker board
54 //  (2) set LX    = -1:	images flipped vertically
55 //  (3) set FLIPH =  1:	images flipped vertically and horizontally
56 //  (4) set LX    =  0:	images flipped horizontally
57 //  (5) set FLIPH =  0, IMG = 1: back to default (with transparency)
58 // ----------------------------------------------------------------------
59 
60 
61 class ImageTest : public Fl_Box {
62 public:
create()63   static Fl_Widget *create() {
64     int x, y;
65     uchar *dg, *dga, *drgb, *drgba;
66     dg    = img_gray   = (uchar*)malloc((128+DX)*128*1);
67     dga   = img_gray_a = (uchar*)malloc((128+DX)*128*2);
68     drgb  = img_rgb    = (uchar*)malloc((128+DX)*128*3);
69     drgba = img_rgba   = (uchar*)malloc((128+DX)*128*4);
70     for (y=0; y<128; y++) {
71       for (x=0; x<128; x++) {
72         *drgba++ = *drgb++ = *dga++ = *dg++ = y<<1;
73         *drgba++ = *drgb++                  = x<<1;
74         *drgba++ = *drgb++                  = (127-x)<<1;
75         *drgba++           = *dga++         = x+y;
76       }
77       if (DX > 0 && LX != 0) {
78         memset(dg,   0,1*DX); dg    += 1*DX;
79         memset(dga,  0,2*DX); dga   += 2*DX;
80         memset(drgb, 0,3*DX); drgb  += 3*DX;
81         memset(drgba,0,4*DX); drgba += 4*DX;
82       }
83     }
84     if (LX<0) {
85       img_gray   += 127*(128+DX);
86       img_gray_a += 127*(128+DX)*2;
87       img_rgb    += 127*(128+DX)*3;
88       img_rgba   += 127*(128+DX)*4;
89     }
90     if (FLIPH && !IMG ) {
91       img_gray   += 127;
92       img_gray_a += 127*2;
93       img_rgb    += 127*3;
94       img_rgba   += 127*4;
95     }
96     i_g    = new Fl_RGB_Image (img_gray  ,128,128,1,LX*(128+DX));
97     i_ga   = new Fl_RGB_Image (img_gray_a,128,128,2,LX*(128+DX)*2);
98     i_rgb  = new Fl_RGB_Image (img_rgb,   128,128,3,LX*(128+DX)*3);
99     i_rgba = new Fl_RGB_Image (img_rgba,  128,128,4,LX*(128+DX)*4);
100     return new ImageTest(TESTAREA_X, TESTAREA_Y, TESTAREA_W, TESTAREA_H);
101   }
102   static uchar *img_gray;
103   static uchar *img_gray_a;
104   static uchar *img_rgb;
105   static uchar *img_rgba;
106   static Fl_RGB_Image *i_g;
107   static Fl_RGB_Image *i_ga;
108   static Fl_RGB_Image *i_rgb;
109   static Fl_RGB_Image *i_rgba;
ImageTest(int x,int y,int w,int h)110   ImageTest(int x, int y, int w, int h) : Fl_Box(x, y, w, h) {
111     label("Testing Image Drawing\n\n"
112 	"This test renders four images, two of them with a checker board\n"
113 	"visible through the graphics. Color and gray gradients should be\n"
114 	"visible. This does not test any image formats such as JPEG.");
115     align(FL_ALIGN_INSIDE|FL_ALIGN_BOTTOM|FL_ALIGN_LEFT|FL_ALIGN_WRAP);
116     box(FL_BORDER_BOX);
117   }
draw()118   void draw() {
119     Fl_Box::draw();
120 
121     // top left: RGB
122 
123     int xx = x()+10, yy = y()+10;
124     fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130);
125 #if IMG
126     i_rgb->draw(xx+1,yy+1);
127 #else
128     if (!FLIPH)
129       fl_draw_image(img_rgb, xx+1, yy+1, 128, 128, 3, LX*((128+DX)*3));
130     else
131       fl_draw_image(img_rgb, xx+1, yy+1, 128, 128,-3, LX*((128+DX)*3));
132 #endif
133     fl_draw("RGB", xx+134, yy+64);
134 
135     // bottom left: RGBA
136 
137     xx = x()+10; yy = y()+10+134;
138     fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130);	// black frame
139     fl_color(FL_WHITE); fl_rectf(xx+1, yy+1, 128, 128);	// white background
140 #if CB							// checker board
141     fl_color(FL_BLACK); fl_rectf(xx+65, yy+1, 64, 64);
142     fl_color(FL_BLACK); fl_rectf(xx+1, yy+65, 64, 64);
143 #endif
144 #if IMG
145     i_rgba->draw(xx+1,yy+1);
146 #else
147     if (!FLIPH)
148       fl_draw_image(img_rgba, xx+1, yy+1, 128, 128, 4, LX*((128+DX)*4));
149     else
150       fl_draw_image(img_rgba, xx+1, yy+1, 128, 128,-4, LX*((128+DX)*4));
151 #endif
152     fl_color(FL_BLACK); fl_draw("RGBA", xx+134, yy+64);
153 
154     // top right: Gray
155 
156     xx = x()+10+200; yy = y()+10;
157     fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130);
158 #if IMG
159     i_g->draw(xx+1,yy+1);
160 #else
161     if (!FLIPH)
162       fl_draw_image(img_gray, xx+1, yy+1, 128, 128, 1, LX*((128+DX)*1));
163     else
164       fl_draw_image(img_gray, xx+1, yy+1, 128, 128,-1, LX*((128+DX)*1));
165 #endif
166     fl_draw("Gray", xx+134, yy+64);
167 
168     // bottom right: Gray+Alpha
169 
170     xx = x()+10+200; yy = y()+10+134;
171     fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130);	// black frame
172     fl_color(FL_WHITE); fl_rectf(xx+1, yy+1, 128, 128);	// white background
173 #if CB							// checker board
174     fl_color(FL_BLACK); fl_rectf(xx+65, yy+1, 64, 64);
175     fl_color(FL_BLACK); fl_rectf(xx+1, yy+65, 64, 64);
176 #endif
177 #if IMG
178     i_ga->draw(xx+1,yy+1);
179 #else
180     if (!FLIPH)
181       fl_draw_image(img_gray_a, xx+1, yy+1, 128, 128, 2, LX*((128+DX)*2));
182     else
183       fl_draw_image(img_gray_a, xx+1, yy+1, 128, 128,-2, LX*((128+DX)*2));
184 #endif
185     fl_color(FL_BLACK); fl_draw("Gray+Alpha", xx+134, yy+64);
186   }
187 };
188 
189 uchar *ImageTest::img_gray = 0;
190 uchar *ImageTest::img_gray_a = 0;
191 uchar *ImageTest::img_rgb = 0;
192 uchar *ImageTest::img_rgba = 0;
193 Fl_RGB_Image *ImageTest::i_g = 0;
194 Fl_RGB_Image *ImageTest::i_ga = 0;
195 Fl_RGB_Image *ImageTest::i_rgb = 0;
196 Fl_RGB_Image *ImageTest::i_rgba = 0;
197 
198 UnitTest images("drawing images", ImageTest::create);
199 
200 //
201 // End of "$Id$"
202 //
203