File: unittest_images.cxx

package info (click to toggle)
fltk1.3 1.3.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,300 kB
  • sloc: cpp: 105,017; ansic: 91,383; sh: 6,825; makefile: 1,948; perl: 24; xml: 7
file content (202 lines) | stat: -rw-r--r-- 6,961 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// "$Id$"
//
// Unit tests for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2016 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
//     http://www.fltk.org/str.php
//

#include <FL/Fl_Box.H>
#include <FL/fl_draw.H>

// Note: currently (March 2010) fl_draw_image() supports transparency with
//	 alpha channel only on Apple (Mac OS X), but Fl_RGB_Image->draw()
//	 supports transparency on all platforms !

//
//------- test the image drawing capabilities of this implementation ----------
//

// Parameters for fine tuning for developers.
// Default values: CB=1, DX=0, IMG=1, LX=0, FLIPH=0

#define CB (1)    // 1 to show the checker board background for alpha images,
		  // 0 otherwise
#define DX (0)	  // additional (undefined (0)) pixels per line, must be >= 0
		  // ignored (irrelevant), if LX == 0 (see below)
#define IMG (1)	  // 1 to use Fl_RGB_Image for drawing images with transparency,
		  // 0 to use fl_draw_image() instead.
		  // Note: as of Feb 2016, only 1 (Fl_RGB_Image) works with
		  // alpha channel, 0 (fl_draw_image()) ignores the alpha
		  // channel (FLTK 1.3.x).
		  // There are plans to support transparency (alpha channel)
		  // in fl_draw_image() in FLTK 1.4.0 and/or later.
#define LX (0)	  // 0 for default: ld() = 0, i.e. ld() defaults (internally) to w()*d()
		  // +1: ld() = (w() + DX) * d()
		  // -1 to flip image vertically: ld() = - ((w() + DX) * d())
#define FLIPH (0) // 1 = Flip image horizontally (only if IMG == 0)
		  // 0 = Draw image normal, w/o horizontal flipping

// ----------------------------------------------------------------------
//  Test scenario for fl_draw_image() with pos. and neg. d and ld args:
// ----------------------------------------------------------------------
//  (1) set IMG   =  0:	normal, but w/o transparency: no checker board
//  (2) set LX    = -1:	images flipped vertically
//  (3) set FLIPH =  1:	images flipped vertically and horizontally
//  (4) set LX    =  0:	images flipped horizontally
//  (5) set FLIPH =  0, IMG = 1: back to default (with transparency)
// ----------------------------------------------------------------------


class ImageTest : public Fl_Box {
public: 
  static Fl_Widget *create() {
    int x, y;
    uchar *dg, *dga, *drgb, *drgba;
    dg    = img_gray   = (uchar*)malloc((128+DX)*128*1);
    dga   = img_gray_a = (uchar*)malloc((128+DX)*128*2);
    drgb  = img_rgb    = (uchar*)malloc((128+DX)*128*3);
    drgba = img_rgba   = (uchar*)malloc((128+DX)*128*4);
    for (y=0; y<128; y++) {
      for (x=0; x<128; x++) {
        *drgba++ = *drgb++ = *dga++ = *dg++ = y<<1;
        *drgba++ = *drgb++                  = x<<1;
        *drgba++ = *drgb++                  = (127-x)<<1;
        *drgba++           = *dga++         = x+y;
      }
      if (DX > 0 && LX != 0) {
        memset(dg,   0,1*DX); dg    += 1*DX;
        memset(dga,  0,2*DX); dga   += 2*DX;
        memset(drgb, 0,3*DX); drgb  += 3*DX;
        memset(drgba,0,4*DX); drgba += 4*DX;
      }
    }
    if (LX<0) {
      img_gray   += 127*(128+DX);
      img_gray_a += 127*(128+DX)*2;
      img_rgb    += 127*(128+DX)*3;
      img_rgba   += 127*(128+DX)*4;
    }
    if (FLIPH && !IMG ) {
      img_gray   += 127;
      img_gray_a += 127*2;
      img_rgb    += 127*3;
      img_rgba   += 127*4;
    }
    i_g    = new Fl_RGB_Image (img_gray  ,128,128,1,LX*(128+DX));
    i_ga   = new Fl_RGB_Image (img_gray_a,128,128,2,LX*(128+DX)*2);
    i_rgb  = new Fl_RGB_Image (img_rgb,   128,128,3,LX*(128+DX)*3);
    i_rgba = new Fl_RGB_Image (img_rgba,  128,128,4,LX*(128+DX)*4);
    return new ImageTest(TESTAREA_X, TESTAREA_Y, TESTAREA_W, TESTAREA_H);
  }
  static uchar *img_gray;
  static uchar *img_gray_a;
  static uchar *img_rgb;
  static uchar *img_rgba;
  static Fl_RGB_Image *i_g;
  static Fl_RGB_Image *i_ga;
  static Fl_RGB_Image *i_rgb;
  static Fl_RGB_Image *i_rgba;
  ImageTest(int x, int y, int w, int h) : Fl_Box(x, y, w, h) {
    label("Testing Image Drawing\n\n"
	"This test renders four images, two of them with a checker board\n"
	"visible through the graphics. Color and gray gradients should be\n"
	"visible. This does not test any image formats such as JPEG.");
    align(FL_ALIGN_INSIDE|FL_ALIGN_BOTTOM|FL_ALIGN_LEFT|FL_ALIGN_WRAP);
    box(FL_BORDER_BOX);
  }
  void draw() {
    Fl_Box::draw();

    // top left: RGB

    int xx = x()+10, yy = y()+10;
    fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130);
#if IMG
    i_rgb->draw(xx+1,yy+1);
#else
    if (!FLIPH)
      fl_draw_image(img_rgb, xx+1, yy+1, 128, 128, 3, LX*((128+DX)*3));
    else
      fl_draw_image(img_rgb, xx+1, yy+1, 128, 128,-3, LX*((128+DX)*3));
#endif
    fl_draw("RGB", xx+134, yy+64);

    // bottom left: RGBA

    xx = x()+10; yy = y()+10+134;
    fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130);	// black frame
    fl_color(FL_WHITE); fl_rectf(xx+1, yy+1, 128, 128);	// white background
#if CB							// checker board
    fl_color(FL_BLACK); fl_rectf(xx+65, yy+1, 64, 64);
    fl_color(FL_BLACK); fl_rectf(xx+1, yy+65, 64, 64);
#endif
#if IMG
    i_rgba->draw(xx+1,yy+1);
#else
    if (!FLIPH)
      fl_draw_image(img_rgba, xx+1, yy+1, 128, 128, 4, LX*((128+DX)*4));
    else
      fl_draw_image(img_rgba, xx+1, yy+1, 128, 128,-4, LX*((128+DX)*4));
#endif
    fl_color(FL_BLACK); fl_draw("RGBA", xx+134, yy+64);

    // top right: Gray

    xx = x()+10+200; yy = y()+10;
    fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130);
#if IMG
    i_g->draw(xx+1,yy+1);
#else
    if (!FLIPH)
      fl_draw_image(img_gray, xx+1, yy+1, 128, 128, 1, LX*((128+DX)*1));
    else
      fl_draw_image(img_gray, xx+1, yy+1, 128, 128,-1, LX*((128+DX)*1));
#endif
    fl_draw("Gray", xx+134, yy+64);

    // bottom right: Gray+Alpha

    xx = x()+10+200; yy = y()+10+134;
    fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130);	// black frame
    fl_color(FL_WHITE); fl_rectf(xx+1, yy+1, 128, 128);	// white background
#if CB							// checker board
    fl_color(FL_BLACK); fl_rectf(xx+65, yy+1, 64, 64);
    fl_color(FL_BLACK); fl_rectf(xx+1, yy+65, 64, 64);
#endif
#if IMG
    i_ga->draw(xx+1,yy+1);
#else
    if (!FLIPH)
      fl_draw_image(img_gray_a, xx+1, yy+1, 128, 128, 2, LX*((128+DX)*2));
    else
      fl_draw_image(img_gray_a, xx+1, yy+1, 128, 128,-2, LX*((128+DX)*2));
#endif
    fl_color(FL_BLACK); fl_draw("Gray+Alpha", xx+134, yy+64);
  }
};

uchar *ImageTest::img_gray = 0;
uchar *ImageTest::img_gray_a = 0;
uchar *ImageTest::img_rgb = 0;
uchar *ImageTest::img_rgba = 0;
Fl_RGB_Image *ImageTest::i_g = 0;
Fl_RGB_Image *ImageTest::i_ga = 0;
Fl_RGB_Image *ImageTest::i_rgb = 0;
Fl_RGB_Image *ImageTest::i_rgba = 0;

UnitTest images("drawing images", ImageTest::create);

//
// End of "$Id$"
//