| 1 | // |
| 2 | // Unit tests for the Fast Light Tool Kit (FLTK). |
| 3 | // |
| 4 | // Copyright 1998-2022 by Bill Spitzak and others. |
| 5 | // |
| 6 | // This library is free software. Distribution and use rights are outlined in |
| 7 | // the file "COPYING" which should have been included with this file. If this |
| 8 | // file is missing or damaged, see the license at: |
| 9 | // |
| 10 | // https://www.fltk.org/COPYING.php |
| 11 | // |
| 12 | // Please see the following page on how to report bugs and issues: |
| 13 | // |
| 14 | // https://www.fltk.org/bugs.php |
| 15 | // |
| 16 | |
| 17 | #include "unittests.h" |
| 18 | |
| 19 | #include <FL/Fl_Group.H> |
| 20 | #include <FL/Fl_Button.H> |
| 21 | #include <FL/Fl_Radio_Button.H> |
| 22 | #include <FL/Fl_Check_Button.H> |
| 23 | #include <FL/fl_draw.H> |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | // Note: currently (March 2010) fl_draw_image() supports transparency with |
| 28 | // alpha channel only on Apple (Mac OS X), but Fl_RGB_Image->draw() |
| 29 | // supports transparency on all platforms ! |
| 30 | |
| 31 | // |
| 32 | //------- test the image drawing capabilities of this implementation ---------- |
| 33 | // |
| 34 | |
| 35 | // Parameters for fine tuning for developers. |
| 36 | // Default values: CB=1, DX=0, IMG=1, LX=0, FLIPH=0 |
| 37 | |
| 38 | static int cb = 1; // 1 to show the checker board background for alpha images, |
| 39 | // 0 otherwise |
| 40 | static int dx = 0; // additional (undefined (0)) pixels per line, must be >= 0 |
| 41 | // ignored (irrelevant), if LX == 0 (see below) |
| 42 | static int img = 1; // 1 to use Fl_RGB_Image for drawing images with transparency, |
| 43 | // 0 to use fl_draw_image() instead. |
| 44 | // Note: as of Feb 2016, only 1 (Fl_RGB_Image) works with |
| 45 | // alpha channel, 0 (fl_draw_image()) ignores the alpha |
| 46 | // channel (FLTK 1.3.x). |
| 47 | // There are plans to support transparency (alpha channel) |
| 48 | // in fl_draw_image() in FLTK 1.4.0 and/or later. |
| 49 | static int lx = 0; // 0 for default: ld() = 0, i.e. ld() defaults (internally) to w()*d() |
| 50 | // +1: ld() = (w() + DX) * d() |
| 51 | // -1 to flip image vertically: ld() = - ((w() + DX) * d()) |
| 52 | static int flip_h = 0; // 1 = Flip image horizontally (only if IMG == 0) |
| 53 | // 0 = Draw image normal, w/o horizontal flipping |
| 54 | |
| 55 | // ---------------------------------------------------------------------- |
| 56 | // Test scenario for fl_draw_image() with pos. and neg. d and ld args: |
| 57 | // ---------------------------------------------------------------------- |
| 58 | // (1) set img = 0: normal, but w/o transparency: no checker board |
| 59 | // (2) set lx = -1: images flipped vertically |
| 60 | // (3) set flip_h = 1: images flipped vertically and horizontally |
| 61 | // (4) set lx = 0: images flipped horizontally |
| 62 | // (5) set flip_h = 0, IMG = 1: back to default (with transparency) |
| 63 | // ---------------------------------------------------------------------- |
| 64 | |
| 65 | class Ut_Image_Test : public Fl_Group { |
| 66 | static void build_imgs() { |
| 67 | int x, y; |
| 68 | uchar *dg, *dga, *drgb, *drgba; |
| 69 | dg = img_gray = img_gray_base = (uchar*)malloc((128+dx)*128*1); |
| 70 | dga = img_gray_a = img_gray_a_base = (uchar*)malloc((128+dx)*128*2); |
| 71 | drgb = img_rgb = img_rgb_base = (uchar*)malloc((128+dx)*128*3); |
| 72 | drgba = img_rgba = img_rgba_base = (uchar*)malloc((128+dx)*128*4); |
| 73 | for (y=0; y<128; y++) { |
| 74 | for (x=0; x<128; x++) { |
| 75 | *drgba++ = *drgb++ = *dga++ = *dg++ = y<<1; |
| 76 | *drgba++ = *drgb++ = x<<1; |
| 77 | *drgba++ = *drgb++ = (127-x)<<1; |
| 78 | *drgba++ = *dga++ = x+y; |
| 79 | } |
| 80 | if (dx > 0 && lx != 0) { |
| 81 | memset(dg, 0,1*dx); dg += 1*dx; |
| 82 | memset(dga, 0,2*dx); dga += 2*dx; |
| 83 | memset(drgb, 0,3*dx); drgb += 3*dx; |
| 84 | memset(drgba,0,4*dx); drgba += 4*dx; |
| 85 | } |
| 86 | } |
| 87 | if (lx<0) { |
| 88 | img_gray += 127*(128+dx); |
| 89 | img_gray_a += 127*(128+dx)*2; |
| 90 | img_rgb += 127*(128+dx)*3; |
| 91 | img_rgba += 127*(128+dx)*4; |
| 92 | } |
| 93 | if (flip_h && !img ) { |
| 94 | img_gray += 127; |
| 95 | img_gray_a += 127*2; |
| 96 | img_rgb += 127*3; |
| 97 | img_rgba += 127*4; |
| 98 | } |
| 99 | i_g = new Fl_RGB_Image (img_gray ,128,128,1,lx*(128+dx)); |
| 100 | i_ga = new Fl_RGB_Image (img_gray_a,128,128,2,lx*(128+dx)*2); |
| 101 | i_rgb = new Fl_RGB_Image (img_rgb, 128,128,3,lx*(128+dx)*3); |
| 102 | i_rgba = new Fl_RGB_Image (img_rgba, 128,128,4,lx*(128+dx)*4); |
| 103 | } // build_imgs method ends |
| 104 | |
| 105 | void free_images() { |
| 106 | if (i_rgba) { delete i_rgba; i_rgba = 0; } |
| 107 | if (i_rgb) { delete i_rgb; i_rgb = 0; } |
| 108 | if (i_ga) { delete i_ga; i_ga = 0; } |
| 109 | if (i_g) { delete i_g; i_g = 0; } |
| 110 | if (img_rgba_base) { free (img_rgba_base); img_rgba_base = 0; } |
| 111 | if (img_rgb_base) { free (img_rgb_base); img_rgb_base = 0; } |
| 112 | if (img_gray_a_base) { free (img_gray_a_base); img_gray_a_base = 0; } |
| 113 | if (img_gray_base) { free (img_gray_base); img_gray_base = 0; } |
| 114 | } // end of free_images method |
| 115 | |
| 116 | static void refresh_imgs_CB(Fl_Widget*,void *data) { |
| 117 | Ut_Image_Test* it = (Ut_Image_Test*)data; |
| 118 | it->free_images(); // release the previous images |
| 119 | // determine the state for the next images |
| 120 | cb = it->ck_CB->value(); |
| 121 | img = it->ck_IMG->value(); |
| 122 | flip_h = it->ck_FLIPH->value(); |
| 123 | // read the LX state radio buttons |
| 124 | if (it->rb_LXp1->value()) { lx = 1; } |
| 125 | else if (it->rb_LXm1->value()) { lx = (-1); } |
| 126 | else { lx = 0; } |
| 127 | // construct the next images |
| 128 | build_imgs(); |
| 129 | it->redraw(); |
| 130 | } |
| 131 | |
| 132 | public: |
| 133 | static Fl_Widget *create() { |
| 134 | build_imgs(); |
| 135 | return new Ut_Image_Test(UT_TESTAREA_X, UT_TESTAREA_Y, UT_TESTAREA_W, UT_TESTAREA_H); |
| 136 | } // create method ends |
| 137 | |
| 138 | static uchar *img_gray_base; |
| 139 | static uchar *img_gray_a_base; |
| 140 | static uchar *img_rgb_base; |
| 141 | static uchar *img_rgba_base; |
| 142 | static uchar *img_gray; |
| 143 | static uchar *img_gray_a; |
| 144 | static uchar *img_rgb; |
| 145 | static uchar *img_rgba; |
| 146 | static Fl_RGB_Image *i_g; |
| 147 | static Fl_RGB_Image *i_ga; |
| 148 | static Fl_RGB_Image *i_rgb; |
| 149 | static Fl_RGB_Image *i_rgba; |
| 150 | |
| 151 | // control widgets |
| 152 | Fl_Group *ctr_grp; |
| 153 | Fl_Check_Button *ck_CB; |
| 154 | Fl_Check_Button *ck_IMG; |
| 155 | Fl_Check_Button *ck_FLIPH; |
| 156 | Fl_Radio_Button *rb_LXm1; |
| 157 | Fl_Radio_Button *rb_LX0; |
| 158 | Fl_Radio_Button *rb_LXp1; |
| 159 | Fl_Button *refresh; |
| 160 | |
| 161 | Ut_Image_Test(int x, int y, int w, int h) |
| 162 | : Fl_Group(x, y, w, h) |
| 163 | { |
| 164 | label("Testing Image Drawing\n\n" |
| 165 | "This test renders four images, two of them with a checker board\n" |
| 166 | "visible through the graphics. Color and gray gradients should be\n" |
| 167 | "visible. This does not test any image formats such as JPEG."); |
| 168 | align(FL_ALIGN_INSIDE|FL_ALIGN_BOTTOM|FL_ALIGN_LEFT|FL_ALIGN_WRAP); |
| 169 | box(FL_BORDER_BOX); |
| 170 | int cw = 90; |
| 171 | int ch = 200; |
| 172 | int cx = x + w - cw - 5; |
| 173 | int cy = y + 10; |
| 174 | ctr_grp = new Fl_Group(cx, cy, cw, ch); |
| 175 | |
| 176 | ck_CB = new Fl_Check_Button(cx+10, cy+10, cw-20, 30, "CB"); |
| 177 | ck_CB->callback(refresh_imgs_CB, (void*)this); |
| 178 | ck_CB->value(cb); |
| 179 | ck_CB->tooltip("1 to show the checker board background for alpha images,\n" |
| 180 | "0 otherwise"); |
| 181 | |
| 182 | ck_IMG = new Fl_Check_Button(cx+10, cy+40, cw-20, 30, "IMG"); |
| 183 | ck_IMG->callback(refresh_imgs_CB, (void*)this); |
| 184 | ck_IMG->value(img); |
| 185 | ck_IMG->tooltip("1 to use Fl_RGB_Image for drawing images with transparency,\n" |
| 186 | "0 to use fl_draw_image() instead."); |
| 187 | |
| 188 | ck_FLIPH = new Fl_Check_Button(cx+10, cy+70, cw-20, 30, "FLIPH"); |
| 189 | ck_FLIPH->callback(refresh_imgs_CB, (void*)this); |
| 190 | ck_FLIPH->value(flip_h); |
| 191 | ck_FLIPH->tooltip("1 = Flip image horizontally (only if IMG == 0)\n" |
| 192 | "0 = Draw image normal, w/o horizontal flipping"); |
| 193 | |
| 194 | Fl_Group *rd_grp = new Fl_Group(cx+10, cy+100, cw-20, 90, "LX"); |
| 195 | |
| 196 | rb_LXp1 = new Fl_Radio_Button(cx+15, cy+105, cw-30, 20, "+1"); |
| 197 | rb_LXp1->callback(refresh_imgs_CB, (void*)this); |
| 198 | rb_LX0 = new Fl_Radio_Button(cx+15, cy+125, cw-30, 20, "0"); |
| 199 | rb_LX0->callback(refresh_imgs_CB, (void*)this); |
| 200 | rb_LX0->tooltip("0 for default: ld() = 0, i.e. ld() defaults (internally) to w()*d()\n" |
| 201 | "+1: ld() = (w() + DX) * d()\n" |
| 202 | "-1 to flip image vertically: ld() = - ((w() + DX) * d())"); |
| 203 | rb_LXm1 = new Fl_Radio_Button(cx+15, cy+145, cw-30, 20, "-1"); |
| 204 | rb_LXm1->callback(refresh_imgs_CB, (void*)this); |
| 205 | rb_LX0->value(1); |
| 206 | |
| 207 | rd_grp->box(FL_BORDER_BOX); |
| 208 | rd_grp->align(FL_ALIGN_INSIDE|FL_ALIGN_BOTTOM|FL_ALIGN_CENTER); |
| 209 | rd_grp->end(); |
| 210 | |
| 211 | ctr_grp->box(FL_BORDER_BOX); |
| 212 | ctr_grp->end(); |
| 213 | end(); // make sure this ImageTest group is closed |
| 214 | } // constructor ends |
| 215 | |
| 216 | void draw() FL_OVERRIDE { |
| 217 | Fl_Group::draw(); |
| 218 | |
| 219 | // top left: RGB |
| 220 | |
| 221 | int xx = x()+10, yy = y()+10; |
| 222 | fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130); |
| 223 | if (img) { |
| 224 | i_rgb->draw(xx+1,yy+1); |
| 225 | } |
| 226 | else { |
| 227 | if (!flip_h) { |
| 228 | fl_draw_image(img_rgb, xx+1, yy+1, 128, 128, 3, lx*((128+dx)*3)); |
| 229 | } |
| 230 | else { |
| 231 | fl_draw_image(img_rgb, xx+1, yy+1, 128, 128,-3, lx*((128+dx)*3)); |
| 232 | } |
| 233 | } |
| 234 | fl_draw("RGB", xx+134, yy+64); |
| 235 | |
| 236 | // bottom left: RGBA |
| 237 | |
| 238 | xx = x()+10; yy = y()+10+134; |
| 239 | fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130); // black frame |
| 240 | fl_color(FL_WHITE); fl_rectf(xx+1, yy+1, 128, 128); // white background |
| 241 | if (cb) { // checker board |
| 242 | fl_color(FL_BLACK); fl_rectf(xx+65, yy+1, 64, 64); |
| 243 | fl_color(FL_BLACK); fl_rectf(xx+1, yy+65, 64, 64); |
| 244 | } |
| 245 | if (img) { |
| 246 | i_rgba->draw(xx+1,yy+1); |
| 247 | } |
| 248 | else { |
| 249 | if (!flip_h) { |
| 250 | fl_draw_image(img_rgba, xx+1, yy+1, 128, 128, 4, lx*((128+dx)*4)); |
| 251 | } |
| 252 | else { |
| 253 | fl_draw_image(img_rgba, xx+1, yy+1, 128, 128,-4, lx*((128+dx)*4)); |
| 254 | } |
| 255 | } |
| 256 | fl_color(FL_BLACK); fl_draw("RGBA", xx+134, yy+64); |
| 257 | |
| 258 | // top right: Gray |
| 259 | |
| 260 | xx = x()+10+200; yy = y()+10; |
| 261 | fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130); |
| 262 | if (img) { |
| 263 | i_g->draw(xx+1,yy+1); |
| 264 | } |
| 265 | else { |
| 266 | if (!flip_h) { |
| 267 | fl_draw_image(img_gray, xx+1, yy+1, 128, 128, 1, lx*((128+dx)*1)); |
| 268 | } |
| 269 | else { |
| 270 | fl_draw_image(img_gray, xx+1, yy+1, 128, 128,-1, lx*((128+dx)*1)); |
| 271 | } |
| 272 | } |
| 273 | fl_draw("Gray", xx+134, yy+64); |
| 274 | |
| 275 | // bottom right: Gray+Alpha |
| 276 | |
| 277 | xx = x()+10+200; yy = y()+10+134; |
| 278 | fl_color(FL_BLACK); fl_rect(xx, yy, 130, 130); // black frame |
| 279 | fl_color(FL_WHITE); fl_rectf(xx+1, yy+1, 128, 128); // white background |
| 280 | if (cb) { // checker board |
| 281 | fl_color(FL_BLACK); fl_rectf(xx+65, yy+1, 64, 64); |
| 282 | fl_color(FL_BLACK); fl_rectf(xx+1, yy+65, 64, 64); |
| 283 | } |
| 284 | if (img) { |
| 285 | i_ga->draw(xx+1,yy+1); |
| 286 | } |
| 287 | else { |
| 288 | if (!flip_h) { |
| 289 | fl_draw_image(img_gray_a, xx+1, yy+1, 128, 128, 2, lx*((128+dx)*2)); |
| 290 | } |
| 291 | else { |
| 292 | fl_draw_image(img_gray_a, xx+1, yy+1, 128, 128,-2, lx*((128+dx)*2)); |
| 293 | } |
| 294 | } |
| 295 | fl_color(FL_BLACK); fl_draw("Gray+Alpha", xx+134, yy+64); |
| 296 | } // draw method end |
| 297 | }; |
| 298 | |
| 299 | uchar *Ut_Image_Test::img_gray_base = 0; |
| 300 | uchar *Ut_Image_Test::img_gray_a_base = 0; |
| 301 | uchar *Ut_Image_Test::img_rgb_base = 0; |
| 302 | uchar *Ut_Image_Test::img_rgba_base = 0; |
| 303 | uchar *Ut_Image_Test::img_gray = 0; |
| 304 | uchar *Ut_Image_Test::img_gray_a = 0; |
| 305 | uchar *Ut_Image_Test::img_rgb = 0; |
| 306 | uchar *Ut_Image_Test::img_rgba = 0; |
| 307 | Fl_RGB_Image *Ut_Image_Test::i_g = 0; |
| 308 | Fl_RGB_Image *Ut_Image_Test::i_ga = 0; |
| 309 | Fl_RGB_Image *Ut_Image_Test::i_rgb = 0; |
| 310 | Fl_RGB_Image *Ut_Image_Test::i_rgba = 0; |
| 311 | |
| 312 | UnitTest images(UT_TEST_IMAGES, "Drawing Images", Ut_Image_Test::create); |