aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/TextureRender.cc
blob: df7a55058f886c294be83ecc2fadfcdaaa201aaa (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
// TextureRender.cc for fluxbox
// Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
//
// from Image.cc for Blackbox - an X11 Window manager
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes at tcac.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

#include "TextureRender.hh"

#include "ImageControl.hh"
#include "TextUtils.hh"
#include "Texture.hh"
#include "App.hh"
#include "FbPixmap.hh"
#include "GContext.hh"
#include "I18n.hh"
#include "StringUtil.hh"
#include "ColorLUT.hh"

#include <X11/Xutil.h>
#include <iostream>

// mipspro has no new(nothrow)
#if defined sgi && ! defined GCC
#define FB_new_nothrow new
#else
#define FB_new_nothrow new(std::nothrow)
#endif

using std::cerr;
using std::endl;
using std::string;
using std::max;
using std::min;
using FbTk::ColorLUT::PRE_MULTIPLY_0_75;
using FbTk::ColorLUT::BRIGHTER_4;
using FbTk::ColorLUT::BRIGHTER_8;

namespace FbTk {

struct RGBA {
    unsigned char r;
    unsigned char g;
    unsigned char b;
    unsigned char a; // align RGBA to 32bit, it's of no use (yet)

    // use of 'static void function()' here to have
    // simple function-pointers for interlace-code
    // (and avoid *this 'overhead')


    static void brighten_4(RGBA& color) {
        color.r = BRIGHTER_4[color.r];
        color.g = BRIGHTER_4[color.g];
        color.b = BRIGHTER_4[color.b];
    }

    static void brighten_8(RGBA& color) {
        color.r = BRIGHTER_8[color.r];
        color.g = BRIGHTER_8[color.g];
        color.b = BRIGHTER_8[color.b];
    }

    // 0.75 of old value
    static void darken(RGBA& color) {
        color.r = PRE_MULTIPLY_0_75[color.r];
        color.g = PRE_MULTIPLY_0_75[color.g];
        color.b = PRE_MULTIPLY_0_75[color.b];
    }

    static void noop(RGBA& color) { }

    typedef void (*colorFunc)(RGBA&);
    static const colorFunc pseudoInterlaceFuncs[3];
};

const RGBA::colorFunc RGBA::pseudoInterlaceFuncs[3] = {
    RGBA::noop,
    RGBA::brighten_4,
    RGBA::darken
};

}

namespace {

struct Vec2 {
    int x;
    int y;

    // positive: 'other' is clockwise of this
    // negative: 'other' is counterclockwise of this
    // 0: same line
    int cross(int other_x, int other_y) const {
        return (x * other_y) - (y * other_x);
    }
};

template <typename T> int sign(T val) {
    return (T(0) < val) - (val < T(0));
}

std::vector<char>& getGradientBuffer(size_t size) {
    static std::vector<char> buffer;
    if (buffer.size() < size)
        buffer.resize(size);
    return buffer;
}


void invertRGB(unsigned int w, unsigned int h, FbTk::RGBA* rgba) {

    FbTk::RGBA* l = rgba;
    FbTk::RGBA* r = rgba + (w * h);

    for (--r; l < r; ++l, --r) { // swapping 32bits (RGBA) at ones.
        std::swap(*((unsigned int*)l), *(unsigned int*)r);
    }
}


void mirrorRGB(unsigned int w, unsigned int h, FbTk::RGBA* rgba) {

    FbTk::RGBA* l = rgba;
    FbTk::RGBA* r = rgba + (w * h);

    for (--r; l < r; ++l, --r) {
        *(unsigned int*)r = *(unsigned int*)l;
    }
}



typedef void (*prepareFunc)(size_t, FbTk::RGBA*, const FbTk::Color*, const FbTk::Color*, double);

//
//
//   To   +          .   From +.
//        |        .          |  .
//        |      .            |    .
//        |    .              |      .
//        |  .                |        .
//        |.                  |          .
//   From +-----------+  To   +-----------+
//        0         size      0         size
//

void prepareLinearTable(size_t size, FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to, double scale) {

    const double r = from->red();
    const double g = from->green();
    const double b = from->blue();

    const double delta_r = (to->red() - r) / (double)size;
    const double delta_g = (to->green() - g) / (double)size;
    const double delta_b = (to->blue() - b) / (double)size;

    size_t i;
    for (i = 0; i < size; ++i) {
        rgba[i].r = static_cast<unsigned char>(scale * (r + (i * delta_r)));
        rgba[i].g = static_cast<unsigned char>(scale * (g + (i * delta_g)));
        rgba[i].b = static_cast<unsigned char>(scale * (b + (i * delta_b)));
    }
}

void prepareSquareTable(size_t size, FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to, double scale) {


    const double r = from->red();
    const double g = from->green();
    const double b = from->blue();

    const double delta_r = (to->red() - r);
    const double delta_g = (to->green() - g);
    const double delta_b = (to->blue() - b);

    double s;
    size_t i;
    for (i = 0; i < size; ++i) {
        s = 1.0 - ((double)(i + 1) / (double)size);
        s *= s;
        rgba[i].r = static_cast<unsigned char>(scale * (r + (s * delta_r)));
        rgba[i].g = static_cast<unsigned char>(scale * (g + (s * delta_g)));
        rgba[i].b = static_cast<unsigned char>(scale * (b + (s * delta_b)));
    }
}

//
//
//   To   +     .         From +           .
//        |    . .             |.         .
//        |   .   .            | .       .
//        |  .     .           |  .     .
//        | .       .          |   .   .
//        |.         .         |    . .
//   From +-----------+   To   +-----.-----+
//        0         size       0         size
//
void prepareMirrorTable(prepareFunc prepare, size_t size, FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to, double scale) {

    // for simplicity we just use given 'prepare' func to
    // prepare 2 parts of the 'mirrorTable'
    //
    // 2 cases: odd and even number of 'size'
    //
    //   even:   f..tt..f   (size == 8)
    //   odd:    f..t..f    (size == 7)
    //
    // for 'odd' we habe to 'overwrite' the last value of the left half
    // with the (same) value for 't' again
    //
    //
    // half_size for even: 4
    // half_size for odd: 4
    //
    size_t half_size = (size >> 1) + (size & 1);

    prepare(half_size, &rgba[0], from, to, scale);
    mirrorRGB(size, 1, rgba);
}

inline void pseudoInterlace(FbTk::RGBA& rgba, const bool& do_interlace, const size_t& y) {
    FbTk::RGBA::pseudoInterlaceFuncs[do_interlace + (do_interlace * (y & 1))](rgba);
}



/*

   x1 y1 ---- gc1 ---- x2 y1
     |                   |
     |                   |
    gc2                 gc1
     |                   |
     |                   |
   x1 y2 ---- gc2 ---- x2 y2

 */
void drawBevelRectangle(FbTk::FbDrawable& d, GC gc1, GC gc2, int x1, int y1, int x2, int y2) {
    d.drawLine(gc1, x1, y1, x2, y1);
    d.drawLine(gc1, x2, y1, x2, y2);
    d.drawLine(gc2, x1, y2, x2, y2);
    d.drawLine(gc2, x1, y1, x1, y2);
}





/*

    bbbbbbbbbbbbbbbbb
    b               d           b - brighter
    b               d           d - darker
    b               d           D - 2 times dark
    xdddddddddddddddD           x - darker(brighter())

 */

void renderBevel1(bool interlaced,
        unsigned int width, unsigned int height,
        FbTk::RGBA* rgba, const FbTk::Color* from, const FbTk::Color* to,
        FbTk::ImageControl& imgctrl) {

    if (! (width > 2 && height > 2))
        return;

    const size_t s = width * height;
    size_t i;

    // brighten top line and first pixel of the
    // 2nd line
    for (i = 0; i < width + 1; ++i) {
        FbTk::RGBA::brighten_4(rgba[i]);
    }

    // bright and darken left and right border
    for (i = 2 * width - 1; i < s - width; i += width) {
        FbTk::RGBA::darken(rgba[i]); // right border
        FbTk::RGBA::brighten_4(rgba[i + 1]);  // left border on the next line
    }

    // darken bottom line, except the first pixel
    for (i = s - width + 1; i < s; ++i) {
        FbTk::RGBA::darken(rgba[i]);
    }

    // and darken the lower corner pixels again
    FbTk::RGBA::darken(rgba[i - 1]);
    FbTk::RGBA::darken(rgba[i - width]);
}


/*
     ...................
     .bbbbbbbbbbbbbbbbd.
     .b...............d.
     .b...............d.    b - brighter
     .b...............d.    d - darker
     .bdddddddddddddddd.
     ...................

   */
void renderBevel2(bool interlaced,
        unsigned int width, unsigned int height,
        FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to,
        FbTk::ImageControl& imgctrl) {

    if (! (width > 4 && height > 4))
        return;

    const size_t s = width * height;
    size_t i;

    // top line, but stop 2 pixels before right border
    for (i = (width + 1); i < ((2 * width) - 2); i++) {
        FbTk::RGBA::brighten_4(rgba[i]);
    }

    // first darken the right border, then brighten the
    // left border
    for ( ; i < (s - (2 * width) - 1); i += width) {
        FbTk::RGBA::darken(rgba[i]);
        FbTk::RGBA::brighten_4(rgba[i + 3]);
    }

    // bottom line
    for (i = (s - (2 * width)) + 2; i < ((s - width) - 1); ++i) {
        FbTk::RGBA::darken(rgba[i]);
    }
}




void renderHorizontalGradient(bool interlaced,
        unsigned int width, unsigned int height,
        FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to,
        FbTk::ImageControl& imgctrl) {

    FbTk::RGBA* gradient = (FbTk::RGBA*)&getGradientBuffer(width * sizeof(FbTk::RGBA))[0];
    prepareLinearTable(width, gradient, from, to, 1.0);

    size_t y;
    size_t x;
    size_t i;

    for (i = 0, y = 0; y < height; ++y) {
        for (x = 0; x < width; ++x, ++i) {
            rgba[i] = gradient[x];
            pseudoInterlace(rgba[i], interlaced, y);
        }
    }
}

void renderVerticalGradient(bool interlaced,
        unsigned int width, unsigned int height,
        FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to,
        FbTk::ImageControl& imgctrl) {

    FbTk::RGBA* gradient = (FbTk::RGBA*)&getGradientBuffer(height * sizeof(FbTk::RGBA))[0];
    prepareLinearTable(height, gradient, from, to, 1.0);

    size_t y;
    size_t x;
    size_t i;

    for (i = 0, y = 0; y < height; ++y) {
        for (x = 0; x < width; ++x, ++i) {
            rgba[i] = gradient[y];
            pseudoInterlace(rgba[i], interlaced, y);
        }
    }
}


void renderPyramidGradient(bool interlaced,
        unsigned int width, unsigned int height,
        FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to,
        FbTk::ImageControl& imgctrl) {


    const size_t s = width + height;

    // we need 2 gradients but use only 'one' buffer
    FbTk::RGBA* x_gradient = (FbTk::RGBA*)&getGradientBuffer(s * sizeof(FbTk::RGBA))[0];
    FbTk::RGBA* y_gradient = x_gradient + width;

    prepareMirrorTable(prepareLinearTable, width, x_gradient, from, to, 0.5);
    prepareMirrorTable(prepareLinearTable, height, y_gradient, from, to, 0.5);

    size_t x;
    size_t y;
    size_t i;

    for (i = 0, y = 0; y < height; ++y) {
        for (x = 0; x < width; ++x, ++i) {

            rgba[i].r = x_gradient[x].r + y_gradient[y].r;
            rgba[i].g = x_gradient[x].g + y_gradient[y].g;
            rgba[i].b = x_gradient[x].b + y_gradient[y].b;

            pseudoInterlace(rgba[i], interlaced, y);
        }
    }
}


/*
    .................
      .............
        .........
          ....          '.' - x_gradient
            .           ' ' - y_gradient
          ....
        .........
      .............
    .................
 */
void renderRectangleGradient(bool interlaced,
        unsigned int width, unsigned int height,
        FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to,
        FbTk::ImageControl& imgctrl) {

    const size_t s = width + height;

    // we need 2 gradients but use only 'one' buffer
    FbTk::RGBA* x_gradient = (FbTk::RGBA*)&getGradientBuffer(s * sizeof(FbTk::RGBA))[0];
    FbTk::RGBA* y_gradient = x_gradient + width;

    prepareMirrorTable(prepareLinearTable, width, x_gradient, from, to, 1.0);
    prepareMirrorTable(prepareLinearTable, height, y_gradient, from, to, 1.0);

    // diagonal vectors
    const Vec2 a = { static_cast<int>(width) - 1, static_cast<int>(height) - 1 };
    const Vec2 b = { a.x, -a.y };

    int x;
    int y;
    size_t i;

    for (i = 0, y = 0; y < static_cast<int>(height); ++y) {
        for (x = 0; x < static_cast<int>(width); ++x, ++i) {

            // check, if the point (x, y) is left or right of the vectors
            // 'a' and 'b'. if the point is on the same side for both 'a' and
            // 'b' (sign(a.cross()) is equal to sign(b.cross())) then use the 
            // y_gradient, otherwise use x_gradient

            if (sign(a.cross(x, y)) * sign(b.cross(x, b.y + y)) < 0) {
                rgba[i] = x_gradient[x];
            } else {
                rgba[i] = y_gradient[y];
            }

            pseudoInterlace(rgba[i], interlaced, y);
        }
    }
}

void renderPipeCrossGradient(bool interlaced,
        unsigned int width, unsigned int height,
        FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to,
        FbTk::ImageControl& imgctrl) {

    size_t s = width + height;

    // we need 2 gradients but use only 'one' buffer
    FbTk::RGBA* x_gradient = (FbTk::RGBA*)&getGradientBuffer(s * sizeof(FbTk::RGBA))[0];
    FbTk::RGBA* y_gradient = x_gradient + width;

    prepareMirrorTable(prepareLinearTable, width, x_gradient, from, to, 1.0);
    prepareMirrorTable(prepareLinearTable, height, y_gradient, from, to, 1.0);

    // diagonal vectors
    const Vec2 a = { static_cast<int>(width) - 1,  static_cast<int>(height - 1) };
    const Vec2 b = { a.x, -a.y };

    int x;
    int y;
    size_t i;

    for (i = 0, y = 0; y < static_cast<int>(height); ++y) {
        for (x = 0; x < static_cast<int>(width); ++x, ++i) {

            // check, if the point (x, y) is left or right of the vectors
            // 'a' and 'b'. if the point is on the same side for both 'a' and
            // 'b' (sign(a.cross()) is equal to sign(b.cross())) then use the 
            // x_gradient, otherwise use y_gradient

            if (sign(a.cross(x, y)) * sign(b.cross(x, b.y + y)) > 0) {
                rgba[i] = x_gradient[x];
            } else {
                rgba[i] = y_gradient[y];
            }

            pseudoInterlace(rgba[i], interlaced, y);
        }
    }
}





void renderDiagonalGradient(bool interlaced,
        unsigned int width, unsigned int height,
        FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to,
        FbTk::ImageControl& imgctrl) {


    size_t s = width + height;

    // we need 2 gradients but use only 'one' buffer
    FbTk::RGBA* x_gradient = (FbTk::RGBA*)&getGradientBuffer(s * sizeof(FbTk::RGBA))[0];
    FbTk::RGBA* y_gradient = x_gradient + width;

    prepareLinearTable(width, x_gradient, from, to, 0.5);
    prepareLinearTable(height, y_gradient, from, to, 0.5);

    size_t x;
    size_t y;
    size_t i;

    for (i = 0, y = 0; y < height; ++y) {
        for (x = 0; x < width; ++x, ++i) {

            rgba[i].r = x_gradient[x].r + y_gradient[y].r;
            rgba[i].g = x_gradient[x].g + y_gradient[y].g;
            rgba[i].b = x_gradient[x].b + y_gradient[y].b;

            pseudoInterlace(rgba[i], interlaced, y);
        }
    }
}




void renderEllipticGradient(bool interlaced,
        unsigned int width, unsigned int height,
        FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to,
        FbTk::ImageControl& imgctrl) {

    size_t i;
    int x;
    int y;


    double r = to->red();
    double g = to->green();
    double b = to->blue();

    double dr = r - from->red();
    double dg = g - from->green();
    double db = b - from->blue();

    int w2 = width/2;
    int h2 = height/2;

    double d;

    for (i = 0, y = -h2; y < h2; ++y) {
        for (x = -w2; x < w2; ++x, ++i) {

            d = ((double)(x*x) / (double)(w2*w2))
                + ((double)(y*y) / (double)(h2*h2));

            d *= 0.5;

            rgba[i].r = (unsigned char)(r - (d * dr));
            rgba[i].g = (unsigned char)(g - (d * dg));
            rgba[i].b = (unsigned char)(b - (d * db));

            pseudoInterlace(rgba[i], interlaced, y);
        }
    }
}




void renderCrossDiagonalGradient(bool interlaced,
        unsigned int width, unsigned int height,
        FbTk::RGBA* rgba,
        const FbTk::Color* from, const FbTk::Color* to,
        FbTk::ImageControl& imgctrl) {

    size_t s = width + height;

    // we need 2 gradients but use only 'one' buffer
    FbTk::RGBA* x_gradient = (FbTk::RGBA*)&getGradientBuffer(s * sizeof(FbTk::RGBA))[0];
    FbTk::RGBA* y_gradient = x_gradient + width;

    prepareLinearTable(width, x_gradient, to, from, 0.5);
    prepareLinearTable(height, y_gradient, from, to, 0.5);

    size_t x;
    size_t y;
    size_t i;

    for (i = 0, y = 0; y < height; ++y) {
        for (x = 0; x < width; ++x, ++i) {

            rgba[i].r = x_gradient[x].r + y_gradient[y].r;
            rgba[i].g = x_gradient[x].g + y_gradient[y].g;
            rgba[i].b = x_gradient[x].b + y_gradient[y].b;

            pseudoInterlace(rgba[i], interlaced, y);
        }
    }
}



struct RendererActions {
    unsigned int type;
    void (*render)(bool, unsigned int, unsigned int,
            FbTk::RGBA*,
            const FbTk::Color*, const FbTk::Color*,
            FbTk::ImageControl&);
};

const RendererActions render_gradient_actions[] = {
    { FbTk::Texture::DIAGONAL, renderDiagonalGradient},
    { FbTk::Texture::ELLIPTIC, renderEllipticGradient },
    { FbTk::Texture::HORIZONTAL, renderHorizontalGradient },
    { FbTk::Texture::PYRAMID, renderPyramidGradient },
    { FbTk::Texture::RECTANGLE, renderRectangleGradient },
    { FbTk::Texture::VERTICAL, renderVerticalGradient },
    { FbTk::Texture::CROSSDIAGONAL, renderCrossDiagonalGradient },
    { FbTk::Texture::PIPECROSS, renderPipeCrossGradient }
};

const RendererActions render_bevel_actions[] = {
    { FbTk::Texture::BEVEL1, renderBevel1 },
    { FbTk::Texture::BEVEL2, renderBevel2 }
};

}

namespace FbTk {

TextureRender::TextureRender(ImageControl &imgctrl,
                             unsigned int w, unsigned int h,
                             FbTk::Orientation orient):
    control(imgctrl),
    cpc(imgctrl.colorsPerChannel()),
    cpccpc(cpc * cpc),
    rgba(0),
    orientation(orient),
    width(w),
    height(h) {

    Display* d = App::instance()->display();
    Screen* s = ScreenOfDisplay(d, imgctrl.screenNumber());

    unsigned int texture_max_width = WidthOfScreen(s) * 2;
    unsigned int texture_max_height = HeightOfScreen(s) * 2;

    _FB_USES_NLS;
    // clamp to "normal" size
    if (width > texture_max_width) {
        cerr<<"TextureRender: "<<_FBTK_CONSOLETEXT(Error, BigWidth, 
                "Warning! Width > 3200 setting Width = 3200", "Image width seems too big, clamping")
            <<endl;
        width = texture_max_width;
    }

    if (height > texture_max_height) {
        cerr<<"TextureRender: "<<_FBTK_CONSOLETEXT(Error, BigHeight,
                "Warning! Height > 3200 setting Height = 3200", "Image height seems too big, clamping")
            <<endl;
        height = texture_max_height;
    }
}


TextureRender::~TextureRender() {
    if (rgba)
        delete[] rgba;
}


Pixmap TextureRender::render(const FbTk::Texture &texture) {

    if (width == 0 || height == 0)
        return None;
    else if (texture.pixmap().drawable() != 0)
        return renderPixmap(texture);
    else if (texture.type() & FbTk::Texture::PARENTRELATIVE)
        return ParentRelative;
    else if (texture.type() & FbTk::Texture::SOLID)
        return renderSolid(texture);
    else if (texture.type() & FbTk::Texture::GRADIENT) {
        allocateColorTables();
        return renderGradient(texture);
    }

    return None;
}

void TextureRender::allocateColorTables() {

    const size_t s = width * height;
    rgba = FB_new_nothrow RGBA[s];
    if (rgba == 0) {

        _FB_USES_NLS;
        throw string("TextureRender::TextureRender(): ") +
              string(_FBTK_CONSOLETEXT(Error, OutOfMemoryRed,
                      "Out of memory while allocating red buffer.", "")) +
              StringUtil::number2String(s);
    }
}

Pixmap TextureRender::renderSolid(const FbTk::Texture &texture) {

    FbPixmap pixmap(RootWindow(FbTk::App::instance()->display(),
                               control.screenNumber()),
                    width, height,
                    control.depth());

    if (pixmap.drawable() == None) {
        _FB_USES_NLS;
        cerr << "FbTk::TextureRender::render_solid(): "
            <<_FBTK_CONSOLETEXT(Error, CreatePixmap, "Error creating pixmap", "Couldn't create a pixmap - image - for some reason")
            << endl;
        return None;
    }


    FbTk::GContext gc(pixmap);
    FbTk::GContext hgc(pixmap);
    FbTk::GContext lgc(pixmap);

    gc.setForeground(texture.color());
    gc.setFillStyle(FillSolid);

    hgc.setForeground(texture.hiColor());

    pixmap.fillRectangle(gc.gc(), 0, 0, width, height);

    if (texture.type() & Texture::INTERLACED) {
        lgc.setForeground(texture.colorTo());
        unsigned int i;
        for (i = 0; i < height; i += 2)
            pixmap.drawLine(lgc.gc(), 0, i, width - 1, i);

    }

    lgc.setForeground(texture.loColor());


    if (height > 1 && width > 1 && texture.type() & Texture::BEVEL1) {
        if (texture.type() & Texture::RAISED) {
            drawBevelRectangle(pixmap, lgc.gc(), hgc.gc(), 0, height - 1, width - 1, 0);
        } else if (texture.type() & Texture::SUNKEN) {
            drawBevelRectangle(pixmap, hgc.gc(), lgc.gc(), 0, height - 1, width - 1, 0);
        }
    } else if (width > 2 && height > 2 && texture.type() & Texture::BEVEL2) {
        if (texture.type() & Texture::RAISED) {
            drawBevelRectangle(pixmap, lgc.gc(), hgc.gc(), 1, height - 2, width - 2, 1);
        } else if (texture.type() & Texture::SUNKEN) {
            drawBevelRectangle(pixmap, hgc.gc(), lgc.gc(), 1, height - 2, width - 2, 1);
        }
    }

    return pixmap.release();
}


Pixmap TextureRender::renderGradient(const FbTk::Texture &texture) {

    // invert our width and height if necessary
    translateSize(orientation, width, height);

    const Color* from = &(texture.color());
    const Color* to = &(texture.colorTo());

    bool interlaced = texture.type() & Texture::INTERLACED;
    bool inverted = texture.type() & Texture::INVERT;

    if (texture.type() & Texture::SUNKEN) {
        std::swap(from, to);
        inverted = !inverted;
    }

    size_t i;
    // draw gradient
    for (i = 0; i < sizeof(render_gradient_actions)/sizeof(RendererActions); ++i) {
        if (render_gradient_actions[i].type & texture.type()) {
            render_gradient_actions[i].render(interlaced, width, height, rgba, from, to, control);
            break;
        }
    }

    // draw bevel
    for (i = 0; i < sizeof(render_bevel_actions)/sizeof(RendererActions); ++i) {
        if (texture.type() & render_bevel_actions[i].type) {
            render_bevel_actions[i].render(interlaced, width, height, rgba, from, to, control);
            break;
        }
    }

    if (inverted) {
        invertRGB(width, height, rgba);
    }

    return renderPixmap();

}

Pixmap TextureRender::renderPixmap(const FbTk::Texture &src_texture) {
    unsigned int tmpw = width, tmph = height;
    // we are given width and height in rotated form, we
    // unrotate it here to render it
    translateSize(orientation, tmpw, tmph);
    if (tmpw != src_texture.pixmap().width() ||
        tmph != src_texture.pixmap().height()) {

        // copy src_texture's pixmap and
        // scale/tile to fit our size
        FbPixmap new_pm(src_texture.pixmap());

        if ((src_texture.type() & Texture::TILED)) {
            new_pm.tile(tmpw,tmph);
        } else {
            new_pm.scale(tmpw, tmph);
        }
        new_pm.rotate(orientation);
        return new_pm.release();
    }
    // return copy of pixmap
    FbPixmap pm_copy = FbPixmap(src_texture.pixmap());
    pm_copy.rotate(orientation);

    return pm_copy.release();
}








XImage *TextureRender::renderXImage() {

    Display *disp = FbTk::App::instance()->display();
    XImage *image = XCreateImage(disp,
                     control.visual(), control.depth(), ZPixmap, 0, 0,
                     width, height, 32, 0);

    if (! image) {
        _FB_USES_NLS;
        cerr << "FbTk::TextureRender::renderXImage(): "
            << _FBTK_CONSOLETEXT(Error, CreateXImage, "Can't create XImage", "Couldn't create an XImage")
            << "." << endl;
        return 0;
    }

    image->data = 0;


    const unsigned char *red_table;
    const unsigned char *green_table;
    const unsigned char *blue_table;
    int red_offset;
    int green_offset;
    int blue_offset;

    control.colorTables(&red_table, &green_table, &blue_table,
                        &red_offset, &green_offset, &blue_offset,
                        0, 0, 0);

    unsigned char *d = new unsigned char[image->bytes_per_line * (height + 1)];
    unsigned int x, y, r, g, b, offset;

    unsigned char *pixel_data = d, *ppixel_data = d;
    unsigned long pixel;

    unsigned int o = image->bits_per_pixel + ((image->byte_order == MSBFirst) ? 1 : 0);


#define TRANSFER_PIXELS(pixel_stmt, transfer_stmt) { \
    RGBA _rgba; \
    for (y = 0, offset = 0; y < height; y++) { \
        for (x = 0; x < width; x++, offset++) { \
            _rgba = rgba[offset]; \
            r = red_table[_rgba.r]; \
            g = green_table[_rgba.g]; \
            b = blue_table[_rgba.b]; \
            pixel = pixel_stmt; \
            transfer_stmt; \
        } \
        pixel_data = (ppixel_data += image->bytes_per_line); \
    } }


    switch (control.visual()->c_class) {
    case StaticColor:
    case PseudoColor:
        TRANSFER_PIXELS((r * cpccpc) + (g * cpc) + b,
                *pixel_data++ = control.colors()[pixel].pixel);
        break;

    case TrueColor:
        switch (o) {
        case 8:
            TRANSFER_PIXELS((r << red_offset)|(g << green_offset)|(b << blue_offset),
                    *pixel_data++ = pixel);
            break;
        case 16:
            TRANSFER_PIXELS((r << red_offset)|(g << green_offset)|(b << blue_offset),
                    *pixel_data++ = pixel;
                    *pixel_data++ = pixel >> 8);
            break;
        case 17:
            TRANSFER_PIXELS((r << red_offset)|(g << green_offset)|(b << blue_offset),
                    *pixel_data++ = pixel >> 8;
                    *pixel_data++ = pixel);
            break;
        case 24:
            TRANSFER_PIXELS((r << red_offset)|(g << green_offset)|(b << blue_offset),
                    *pixel_data++ = pixel;
                    *pixel_data++ = pixel >> 8;
                    *pixel_data++ = pixel >> 16);
            break;
        case 25:
            TRANSFER_PIXELS((r << red_offset)|(g << green_offset)|(b << blue_offset),
                    *pixel_data++ = pixel >> 16;
                    *pixel_data++ = pixel >> 8;
                    *pixel_data++ = pixel);
            break;
        case 32:
            TRANSFER_PIXELS((r << red_offset)|(g << green_offset)|(b << blue_offset),
                    *pixel_data++ = pixel;
                    *pixel_data++ = pixel >> 8;
                    *pixel_data++ = pixel >> 16;
                    *pixel_data++ = pixel >> 24);
            break;
        case 33:
            TRANSFER_PIXELS((r << red_offset)|(g << green_offset)|(b << blue_offset),
                    *pixel_data++ = pixel >> 24;
                    *pixel_data++ = pixel >> 16;
                    *pixel_data++ = pixel >> 8;
                    *pixel_data++ = pixel);
            break;
        }

        break;

    case StaticGray:
    case GrayScale:
        TRANSFER_PIXELS(((r * 30) + (g * 59) + (b * 11)) / 100,
                    *pixel_data++ = control.colors()[pixel].pixel);

        break;

    default:
        _FB_USES_NLS;
        cerr << "TextureRender::renderXImage(): " <<
            _FBTK_CONSOLETEXT(Error, UnsupportedVisual, "Unsupported visual", "A visual is a technical term in X") << endl;
        delete [] d;
        XDestroyImage(image);
        return (XImage *) 0;
    }

#undef TRANSFER_PIXELS

    image->data = (char *) d;
    return image;
}


Pixmap TextureRender::renderPixmap() {

    Display *disp = FbTk::App::instance()->display();
    FbPixmap pixmap(RootWindow(disp, control.screenNumber()),
                    width, height, control.depth());

    if (pixmap.drawable() == None) {
        _FB_USES_NLS;
        cerr << "FbTk::TextureRender::renderPixmap(): "
            << _FBTK_CONSOLETEXT(Error, CreatePixmap, "Error creating pixmap", "Couldn't create a pixmap - image - for some reason") << endl;
        return None;
    }

    XImage *image = renderXImage();

    if (! image) {
        return None;
    } else if (! image->data) {
        XDestroyImage(image);
        return None;
    }

    XPutImage(disp, pixmap.drawable(),
              DefaultGC(disp, control.screenNumber()),
              image, 0, 0, 0, 0, width, height);

    if (image->data != 0) {
        delete [] image->data;
        image->data = 0;
    }

    XDestroyImage(image);

    pixmap.rotate(orientation);

    return pixmap.release();
}


} // end namespace FbTk