summaryrefslogtreecommitdiff
path: root/components/gui/src/dc_rotozoom.c
blob: 9d1f59af2b24498d3e522b72c4ba33520e19a8f6 (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
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
/*
 * File      : dc_rotozoom.c
 * This file is part of RT-Thread GUI
 * COPYRIGHT (C) 2006 - 2014, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2014-03-15     Bernard      porting SDL_gfx to RT-Thread GUI
 */

/*

SDL_rotozoom.c: rotozoomer, zoomer and shrinker for 32bit or 8bit surfaces

Copyright (C) 2001-2012  Andreas Schiffler

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.

3. This notice may not be removed or altered from any source
distribution.

Andreas Schiffler -- aschiffler at ferzkopp dot net
*/

#include <stdlib.h>
#include <string.h>
#include <rtgui/rtgui.h>
#include <rtgui/dc.h>
#include <rtgui/dc_draw.h>
#include <rtgui/color.h>
#include <rtgui/rtgui_system.h>

#include <math.h>

/* ---- Internally used structures */

/*!
\brief A 32 bit RGBA pixel.
*/
typedef struct tColorRGBA
{
    rt_uint8_t r;
    rt_uint8_t g;
    rt_uint8_t b;
    rt_uint8_t a;
} tColorRGBA;

/*!
\brief A 8bit Y/palette pixel.
*/
typedef struct tColorY
{
    rt_uint8_t y;
} tColorY;

/*!
\brief Returns maximum of two numbers a and b.
*/
#define MAX(a,b)    (((a) > (b)) ? (a) : (b))

/*!
\brief Number of guard rows added to destination surfaces.

This is a simple but effective workaround for observed issues.
These rows allocate extra memory and are then hidden from the surface.
Rows are added to the end of destination surfaces when they are allocated.
This catches any potential overflows which seem to happen with
just the right src image dimensions and scale/rotation and can lead
to a situation where the program can segfault.
*/
#define GUARD_ROWS (2)

/*!
\brief Lower limit of absolute zoom factor or rotation degrees.
*/
#define VALUE_LIMIT 0.001

void rtgui_dc_zoom_size(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight);

/*!
\brief Internal 32 bit integer-factor averaging Shrinker.

Shrinks 32 bit RGBA/ABGR 'src' surface to 'dst' surface.
Averages color and alpha values values of src pixels to calculate dst pixels.
Assumes src and dst surfaces are of 32 bit depth.
Assumes dst surface was allocated with the correct dimensions.

\param src The surface to shrink (input).
\param dst The shrunken surface (output).
\param factorx The horizontal shrinking ratio.
\param factory The vertical shrinking ratio.

\return 0 for success or -1 for error.
*/
int _rtgui_dc_shrink_RGBA(struct rtgui_dc_buffer * src, struct rtgui_dc_buffer * dst, int factorx, int factory)
{
    int x, y, dx, dy, sgap, dgap, ra, ga, ba, aa;
    int n_average;
    tColorRGBA *sp, *osp, *oosp;
    tColorRGBA *dp;

    /*
    * Averaging integer shrink
    */

    /* Precalculate division factor */
    n_average = factorx*factory;

    /*
    * Scan destination
    */
    sp = (tColorRGBA *) src->pixel;
    sgap = src->pitch - src->width * 4;
    sgap = sgap;

    dp = (tColorRGBA *) dst->pixel;
    dgap = dst->pitch - dst->width * 4;

    for (y = 0; y < dst->height; y++)
    {
        osp=sp;
        for (x = 0; x < dst->width; x++)
        {
            /* Trace out source box and accumulate */
            oosp=sp;
            ra=ga=ba=aa=0;
            for (dy=0; dy < factory; dy++)
            {
                for (dx=0; dx < factorx; dx++)
                {
                    ra += sp->r;
                    ga += sp->g;
                    ba += sp->b;
                    aa += sp->a;

                    sp++;
                }
                /* src dx loop */
                sp = (tColorRGBA *)((rt_uint8_t*)sp + (src->pitch - 4*factorx)); // next y
            }
            /* src dy loop */

            /* next box-x */
            sp = (tColorRGBA *)((rt_uint8_t*)oosp + 4*factorx);

            /* Store result in destination */
            dp->r = ra/n_average;
            dp->g = ga/n_average;
            dp->b = ba/n_average;
            dp->a = aa/n_average;

            /*
            * Advance destination pointer
            */
            dp++;
        }
        /* dst x loop */

        /* next box-y */
        sp = (tColorRGBA *)((rt_uint8_t*)osp + src->pitch*factory);

        /*
        * Advance destination pointers
        */
        dp = (tColorRGBA *) ((rt_uint8_t *) dp + dgap);
    }
    /* dst y loop */

    return (0);
}

/*!
\brief Internal 32 bit Zoomer with optional anti-aliasing by bilinear interpolation.

Zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface.
Assumes src and dst surfaces are of 32 bit depth.
Assumes dst surface was allocated with the correct dimensions.

\param src The surface to zoom (input).
\param dst The zoomed surface (output).
\param flipx Flag indicating if the image should be horizontally flipped.
\param flipy Flag indicating if the image should be vertically flipped.
\param smooth Antialiasing flag; set to SMOOTHING_ON to enable.

\return 0 for success or -1 for error.
*/
int _rtgui_dc_zoom_RGBA(struct rtgui_dc_buffer * src, struct rtgui_dc_buffer * dst, int flipx, int flipy, int smooth)
{
    int x, y, sx, sy, ssx, ssy, *sax, *say, *csax, *csay, *salast, csx, csy, ex, ey, cx, cy, sstep, sstepx, sstepy;
    tColorRGBA *c00, *c01, *c10, *c11;
    tColorRGBA *sp, *csp, *dp;
    int spixelgap, spixelw, spixelh, dgap, t1, t2;

    /*
    * Allocate memory for row/column increments
    */
    if ((sax = (int *) rtgui_malloc((dst->width + 1) * sizeof(rt_uint32_t))) == RT_NULL)
    {
        return (-1);
    }
    if ((say = (int *) rtgui_malloc((dst->height + 1) * sizeof(rt_uint32_t))) == RT_NULL)
    {
        rtgui_free(sax);
        return (-1);
    }

    /*
    * Precalculate row increments
    */
    spixelw = (src->width - 1);
    spixelh = (src->height- 1);
    if (smooth)
    {
        sx = (int) (65536.0 * (double) spixelw / (double) (dst->width - 1));
        sy = (int) (65536.0 * (double) spixelh / (double) (dst->height - 1));
    }
    else
    {
        sx = (int) (65536.0 * (double) (src->width) / (double) (dst->width));
        sy = (int) (65536.0 * (double) (src->height) / (double) (dst->height));
    }

    /* Maximum scaled source size */
    ssx = (src->width << 16) - 1;
    ssy = (src->height << 16) - 1;

    /* Precalculate horizontal row increments */
    csx = 0;
    csax = sax;
    for (x = 0; x <= dst->width; x++)
    {
        *csax = csx;
        csax++;
        csx += sx;

        /* Guard from overflows */
        if (csx > ssx)
        {
            csx = ssx;
        }
    }

    /* Precalculate vertical row increments */
    csy = 0;
    csay = say;
    for (y = 0; y <= dst->height; y++)
    {
        *csay = csy;
        csay++;
        csy += sy;

        /* Guard from overflows */
        if (csy > ssy)
        {
            csy = ssy;
        }
    }

    sp = (tColorRGBA *) src->pixel;
    dp = (tColorRGBA *) dst->pixel;
    dgap = dst->pitch - dst->width * 4;
    spixelgap = src->pitch/4;

    if (flipx) sp += spixelw;
    if (flipy) sp += (spixelgap * spixelh);

    /*
    * Switch between interpolating and non-interpolating code
    */
    if (smooth)
    {

        /*
        * Interpolating Zoom
        */
        csay = say;
        for (y = 0; y < dst->height; y++)
        {
            csp = sp;
            csax = sax;
            for (x = 0; x < dst->width; x++)
            {
                /*
                * Setup color source pointers
                */
                ex = (*csax & 0xffff);
                ey = (*csay & 0xffff);
                cx = (*csax >> 16);
                cy = (*csay >> 16);
                sstepx = cx < spixelw;
                sstepy = cy < spixelh;
                c00 = sp;
                c01 = sp;
                c10 = sp;
                if (sstepy)
                {
                    if (flipy)
                    {
                        c10 -= spixelgap;
                    }
                    else
                    {
                        c10 += spixelgap;
                    }
                }
                c11 = c10;
                if (sstepx)
                {
                    if (flipx)
                    {
                        c01--;
                        c11--;
                    }
                    else
                    {
                        c01++;
                        c11++;
                    }
                }

                /*
                * Draw and interpolate colors
                */
                t1 = ((((c01->r - c00->r) * ex) >> 16) + c00->r) & 0xff;
                t2 = ((((c11->r - c10->r) * ex) >> 16) + c10->r) & 0xff;
                dp->r = (((t2 - t1) * ey) >> 16) + t1;
                t1 = ((((c01->g - c00->g) * ex) >> 16) + c00->g) & 0xff;
                t2 = ((((c11->g - c10->g) * ex) >> 16) + c10->g) & 0xff;
                dp->g = (((t2 - t1) * ey) >> 16) + t1;
                t1 = ((((c01->b - c00->b) * ex) >> 16) + c00->b) & 0xff;
                t2 = ((((c11->b - c10->b) * ex) >> 16) + c10->b) & 0xff;
                dp->b = (((t2 - t1) * ey) >> 16) + t1;
                t1 = ((((c01->a - c00->a) * ex) >> 16) + c00->a) & 0xff;
                t2 = ((((c11->a - c10->a) * ex) >> 16) + c10->a) & 0xff;
                dp->a = (((t2 - t1) * ey) >> 16) + t1;
                /*
                * Advance source pointer x
                */
                salast = csax;
                csax++;
                sstep = (*csax >> 16) - (*salast >> 16);
                if (flipx)
                {
                    sp -= sstep;
                }
                else
                {
                    sp += sstep;
                }

                /*
                * Advance destination pointer x
                */
                dp++;
            }
            /*
            * Advance source pointer y
            */
            salast = csay;
            csay++;
            sstep = (*csay >> 16) - (*salast >> 16);
            sstep *= spixelgap;
            if (flipy)
            {
                sp = csp - sstep;
            }
            else
            {
                sp = csp + sstep;
            }

            /*
            * Advance destination pointer y
            */
            dp = (tColorRGBA *) ((rt_uint8_t *) dp + dgap);
        }
    }
    else
    {
        /*
        * Non-Interpolating Zoom
        */
        csay = say;
        for (y = 0; y < dst->height; y++)
        {
            csp = sp;
            csax = sax;
            for (x = 0; x < dst->width; x++)
            {
                /*
                * Draw
                */
                *dp = *sp;

                /*
                * Advance source pointer x
                */
                salast = csax;
                csax++;
                sstep = (*csax >> 16) - (*salast >> 16);
                if (flipx) sstep = -sstep;
                sp += sstep;

                /*
                * Advance destination pointer x
                */
                dp++;
            }
            /*
            * Advance source pointer y
            */
            salast = csay;
            csay++;
            sstep = (*csay >> 16) - (*salast >> 16);
            sstep *= spixelgap;
            if (flipy) sstep = -sstep;
            sp = csp + sstep;

            /*
            * Advance destination pointer y
            */
            dp = (tColorRGBA *) ((rt_uint8_t *) dp + dgap);
        }
    }

    /*
    * Remove temp arrays
    */
    rtgui_free(sax);
    rtgui_free(say);

    return (0);
}

/*!
\brief Internal 32 bit rotozoomer with optional anti-aliasing.

Rotates and zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
parameters by scanning the destination surface and applying optionally anti-aliasing
by bilinear interpolation.
Assumes src and dst surfaces are of 32 bit depth.
Assumes dst surface was allocated with the correct dimensions.

\param src Source surface.
\param dst Destination surface.
\param cx Horizontal center coordinate.
\param cy Vertical center coordinate.
\param isin Integer version of sine of angle.
\param icos Integer version of cosine of angle.
\param flipx Flag indicating horizontal mirroring should be applied.
\param flipy Flag indicating vertical mirroring should be applied.
\param smooth Flag indicating anti-aliasing should be used.
*/
int _rtgui_dc_transform_RGBA(struct rtgui_dc_buffer * src, struct rtgui_dc_buffer * dst,
                             int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth)
{
    int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh;
    tColorRGBA c00, c01, c10, c11, cswap;
    tColorRGBA *pc, *sp;
    int gap;

    /*
    * Variable setup
    */
    xd = ((src->width - dst->width) << 15);
    yd = ((src->height - dst->height) << 15);
    ax = (cx << 16) - (icos * cx);
    ay = (cy << 16) - (isin * cx);
    sw = src->width - 1;
    sh = src->height - 1;
    pc = (tColorRGBA*) dst->pixel;
    gap = dst->pitch - dst->width * 4;

    /*
    * Switch between interpolating and non-interpolating code
    */
    if (smooth)
    {
        for (y = 0; y < dst->height; y++)
        {
            dy = cy - y;
            sdx = (ax + (isin * dy)) + xd;
            sdy = (ay - (icos * dy)) + yd;
            for (x = 0; x < dst->width; x++)
            {
                dx = (sdx >> 16);
                dy = (sdy >> 16);
                if (flipx) dx = sw - dx;
                if (flipy) dy = sh - dy;
                if ((dx > -1) && (dy > -1) && (dx < (src->width-1)) && (dy < (src->height-1)))
                {
                    sp = (tColorRGBA *)src->pixel;;
                    sp += ((src->pitch/4) * dy);
                    sp += dx;
                    c00 = *sp;
                    sp += 1;
                    c01 = *sp;
                    sp += (src->pitch/4);
                    c11 = *sp;
                    sp -= 1;
                    c10 = *sp;
                    if (flipx)
                    {
                        cswap = c00;
                        c00=c01;
                        c01=cswap;
                        cswap = c10;
                        c10=c11;
                        c11=cswap;
                    }
                    if (flipy)
                    {
                        cswap = c00;
                        c00=c10;
                        c10=cswap;
                        cswap = c01;
                        c01=c11;
                        c11=cswap;
                    }
                    /*
                    * Interpolate colors
                    */
                    ex = (sdx & 0xffff);
                    ey = (sdy & 0xffff);
                    t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
                    t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
                    pc->r = (((t2 - t1) * ey) >> 16) + t1;
                    t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
                    t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
                    pc->g = (((t2 - t1) * ey) >> 16) + t1;
                    t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
                    t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
                    pc->b = (((t2 - t1) * ey) >> 16) + t1;
                    t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
                    t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
                    pc->a = (((t2 - t1) * ey) >> 16) + t1;
                }
                sdx += icos;
                sdy += isin;
                pc++;
            }
            pc = (tColorRGBA *) ((rt_uint8_t *) pc + gap);
        }
    }
    else
    {
        for (y = 0; y < dst->height; y++)
        {
            dy = cy - y;
            sdx = (ax + (isin * dy)) + xd;
            sdy = (ay - (icos * dy)) + yd;
            for (x = 0; x < dst->width; x++)
            {
                dx = (short) (sdx >> 16);
                dy = (short) (sdy >> 16);
                if (flipx) dx = (src->width-1)-dx;
                if (flipy) dy = (src->height-1)-dy;
                if ((dx >= 0) && (dy >= 0) && (dx < src->width) && (dy < src->height))
                {
                    sp = (tColorRGBA *) ((rt_uint8_t *) src->pixel + src->pitch * dy);
                    sp += dx;
                    *pc = *sp;
                }
                sdx += icos;
                sdy += isin;
                pc++;
            }
            pc = (tColorRGBA *) ((rt_uint8_t *) pc + gap);
        }
    }

    return 0;
}

/*!
\brief Rotates a 32 bit surface in increments of 90 degrees.

Specialized 90 degree rotator which rotates a 'src' surface in 90 degree
increments clockwise returning a new surface. Faster than rotozoomer since
not scanning or interpolation takes place. Input surface must be 32 bit.
(code contributed by J. Schiller, improved by C. Allport and A. Schiffler)

\param src Source surface to rotate.
\param numClockwiseTurns Number of clockwise 90 degree turns to apply to the source.

\returns The new, rotated surface; or RT_NULL for surfaces with incorrect input format.
*/
struct rtgui_dc* rtgui_dc_rotate_90degrees(struct rtgui_dc_buffer* src, int numClockwiseTurns)
{
    int row, col, newWidth, newHeight;
    int bpp, src_ipr, dst_ipr;
    struct rtgui_dc_buffer* dst;
    rt_uint32_t* srcBuf;
    rt_uint32_t* dstBuf;

    /* sanity check */
    if (src == RT_NULL) return RT_NULL;
    /* we only support 32bit */
    if (rtgui_color_get_bits(src->pixel_format) != 32) return RT_NULL;

    /* normalize numClockwiseTurns */
    while(numClockwiseTurns < 0)
    {
        numClockwiseTurns += 4;
    }
    numClockwiseTurns = (numClockwiseTurns % 4);

    /* if it's even, our new width will be the same as the source surface */
    newWidth = (numClockwiseTurns % 2) ? (src->height) : (src->width);
    newHeight = (numClockwiseTurns % 2) ? (src->width) : (src->height);
    dst = (struct rtgui_dc_buffer*) rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888, newWidth, newHeight);
    if(!dst) return RT_NULL;

    /* Calculate int-per-row */
    bpp = rtgui_color_get_bpp(src->pixel_format);
    src_ipr = src->pitch / bpp;
    dst_ipr = dst->pitch / bpp;

    switch(numClockwiseTurns)
    {
    case 0: /* Make a copy of the surface */
    {
        /* Unfortunately SDL_BlitSurface cannot be used to make a copy of the surface
        since it does not preserve alpha. */
        if (src->pitch == dst->pitch)
        {
            /* If the pitch is the same for both surfaces, the memory can be copied all at once. */
            memcpy(dst->pixel, src->pixel, (src->height * src->pitch));
        }
        else
        {
            /* If the pitch differs, copy each row separately */
            srcBuf = (rt_uint32_t*)(src->pixel);
            dstBuf = (rt_uint32_t*)(dst->pixel);

            for (row = 0; row < src->height; row++)
            {
                memcpy(dstBuf, srcBuf, dst->width * bpp);
                srcBuf += src_ipr;
                dstBuf += dst_ipr;
            } /* end for(col) */
        } /* end for(row) */
    }
    break;

    /* rotate clockwise */
    case 1: /* rotated 90 degrees clockwise */
    {
        for (row = 0; row < src->height; ++row)
        {
            srcBuf = (rt_uint32_t*)(src->pixel) + (row * src_ipr);
            dstBuf = (rt_uint32_t*)(dst->pixel) + (dst->width - row - 1);
            for (col = 0; col < src->width; ++col)
            {
                *dstBuf = *srcBuf;
                ++srcBuf;
                dstBuf += dst_ipr;
            }
            /* end for(col) */
        }
        /* end for(row) */
    }
    break;

    case 2: /* rotated 180 degrees clockwise */
    {
        for (row = 0; row < src->height; ++row)
        {
            srcBuf = (rt_uint32_t*)(src->pixel) + (row * src_ipr);
            dstBuf = (rt_uint32_t*)(dst->pixel) + ((dst->height - row - 1) * dst_ipr) + (dst->width - 1);
            for (col = 0; col < src->width; ++col)
            {
                *dstBuf = *srcBuf;
                ++srcBuf;
                --dstBuf;
            }
        }
    }
    break;

    case 3:
    {
        for (row = 0; row < src->height; ++row)
        {
            srcBuf = (rt_uint32_t*)(src->pixel) + (row * src_ipr);
            dstBuf = (rt_uint32_t*)(dst->pixel) + row + ((dst->height - 1) * dst_ipr);
            for (col = 0; col < src->width; ++col)
            {
                *dstBuf = *srcBuf;
                ++srcBuf;
                dstBuf -= dst_ipr;
            }
        }
    }
    break;
    }
    /* end switch */

    return RTGUI_DC(dst);
}

/*!
\brief Internal target surface sizing function for rotozooms with trig result return.

\param width The source surface width.
\param height The source surface height.
\param angle The angle to rotate in degrees.
\param zoomx The horizontal scaling factor.
\param zoomy The vertical scaling factor.
\param dstwidth The calculated width of the destination surface.
\param dstheight The calculated height of the destination surface.
\param canglezoom The sine of the angle adjusted by the zoom factor.
\param sanglezoom The cosine of the angle adjusted by the zoom factor.

*/
void _rtgui_dc_rotozoom_size(int width, int height, double angle, double zoomx, double zoomy,
                             int *dstwidth, int *dstheight,
                             double *canglezoom, double *sanglezoom)
{
    double x, y, cx, cy, sx, sy;
    double radangle;
    int dstwidthhalf, dstheighthalf;

    /*
    * Determine destination width and height by rotating a centered source box
    */
    radangle = angle * (M_PI / 180.0);
    *sanglezoom = sin(radangle);
    *canglezoom = cos(radangle);
    *sanglezoom *= zoomx;
    *canglezoom *= zoomx;
    x = (double)(width / 2);
    y = (double)(height / 2);
    cx = *canglezoom * x;
    cy = *canglezoom * y;
    sx = *sanglezoom * x;
    sy = *sanglezoom * y;

    dstwidthhalf = MAX((int)
                       ceil(MAX(MAX(MAX(fabs(cx + sy), fabs(cx - sy)), fabs(-cx + sy)), fabs(-cx - sy))), 1);
    dstheighthalf = MAX((int)
                        ceil(MAX(MAX(MAX(fabs(sx + cy), fabs(sx - cy)), fabs(-sx + cy)), fabs(-sx - cy))), 1);
    *dstwidth = 2 * dstwidthhalf;
    *dstheight = 2 * dstheighthalf;
}

/*!
\brief Returns the size of the resulting target surface for a rotozoomSurface() call.

\param width The source surface width.
\param height The source surface height.
\param angle The angle to rotate in degrees.
\param zoom The scaling factor.
\param dstwidth The calculated width of the rotozoomed destination surface.
\param dstheight The calculated height of the rotozoomed destination surface.
*/
void rtgui_dc_rotozoom_size(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight)
{
    double dummy_sanglezoom, dummy_canglezoom;

    _rtgui_dc_rotozoom_size(width, height, angle, zoom, zoom, dstwidth, dstheight, &dummy_sanglezoom, &dummy_canglezoom);
}

/*!
\brief Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing.

Rotates and zooms a 32bit or 8bit 'src' surface to newly created 'dst' surface.
'angle' is the rotation in degrees, 'zoomx and 'zoomy' scaling factors. If 'smooth' is set
then the destination 32bit surface is anti-aliased. If the surface is not 8bit
or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.

\param src The surface to rotozoom.
\param angle The angle to rotate in degrees.
\param zoomx The horizontal scaling factor.
\param zoomy The vertical scaling factor.
\param smooth Antialiasing flag; set to SMOOTHING_ON to enable.

\return The new rotozoomed surface.
*/
struct rtgui_dc *rtgui_dc_rotozoom(struct rtgui_dc *dc, double angle, double zoomx, double zoomy, int smooth)
{
    struct rtgui_dc_buffer *rz_src;
    struct rtgui_dc_buffer *rz_dst;
    double zoominv;
    double sanglezoom, canglezoom, sanglezoominv, canglezoominv;
    int dstwidthhalf, dstwidth, dstheighthalf, dstheight;
    int flipx,flipy;
    int result;

    /*
    * Sanity check
    */
    rz_src = (struct rtgui_dc_buffer*)(dc);
    if (rz_src == RT_NULL) return (RT_NULL);
    /* we only support 32bit */
    if (rtgui_color_get_bits(rz_src->pixel_format) != 32) return RT_NULL;

    /*
    * Sanity check zoom factor
    */
    flipx = (zoomx<0.0);
    if (flipx) zoomx=-zoomx;
    flipy = (zoomy<0.0);
    if (flipy) zoomy=-zoomy;
    if (zoomx < VALUE_LIMIT) zoomx = VALUE_LIMIT;
    if (zoomy < VALUE_LIMIT) zoomy = VALUE_LIMIT;
    zoominv = 65536.0 / (zoomx * zoomx);

    /*
    * Check if we have a rotozoom or just a zoom
    */
    if (fabs(angle) > VALUE_LIMIT)
    {
        /*
        * Angle!=0: full rotozoom
        */
        /*
        * -----------------------
        */

        /* Determine target size */
        _rtgui_dc_rotozoom_size(rz_src->width, rz_src->height, angle, zoomx, zoomy, &dstwidth, &dstheight, &canglezoom, &sanglezoom);

        /*
        * Calculate target factors from sin/cos and zoom
        */
        sanglezoominv = sanglezoom;
        canglezoominv = canglezoom;
        sanglezoominv *= zoominv;
        canglezoominv *= zoominv;

        /* Calculate half size */
        dstwidthhalf = dstwidth / 2;
        dstheighthalf = dstheight / 2;

        /*
        * Alloc space to completely contain the rotated surface
        */
        rz_dst = (struct rtgui_dc_buffer*)rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888,
                 dstwidth, dstheight + GUARD_ROWS);
        /* Check target */
        if (rz_dst == RT_NULL)return RT_NULL;

        /*
        * Call the 32bit transformation routine to do the rotation (using alpha)
        */
        result = _rtgui_dc_transform_RGBA(rz_src, rz_dst, dstwidthhalf, dstheighthalf,
                                          (int) (sanglezoominv), (int) (canglezoominv),
                                          flipx, flipy,
                                          smooth);
        if (result != 0)
        {
            rtgui_dc_destory(RTGUI_DC(rz_dst));
            rz_dst = RT_NULL;
        }
    }
    else
    {
        /*
        * Angle=0: Just a zoom
        */

        /*
        * Calculate target size
        */
        rtgui_dc_zoom_size(rz_src->width, rz_src->height, zoomx, zoomy, &dstwidth, &dstheight);

        /*
        * Alloc space to completely contain the zoomed surface
        */
        rz_dst = (struct rtgui_dc_buffer*)rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888,
                 dstwidth, dstheight + GUARD_ROWS);
        /* Check target */
        if (rz_dst == RT_NULL) return RT_NULL;

        /*
        * Call the 32bit transformation routine to do the zooming (using alpha)
        */
        result = _rtgui_dc_zoom_RGBA(rz_src, rz_dst, flipx, flipy, smooth);
        if (result != 0)
        {
            rtgui_dc_destory(RTGUI_DC(rz_dst));
            rz_dst = RT_NULL;
        }
    }

    /*
    * Return destination surface
    */
    return RTGUI_DC(rz_dst);
}
RTM_EXPORT(rtgui_dc_rotozoom);
/*!
\brief Calculates the size of the target surface for a rtgui_dc_zoom() call.

The minimum size of the target surface is 1. The input factors can be positive or negative.

\param width The width of the source surface to zoom.
\param height The height of the source surface to zoom.
\param zoomx The horizontal zoom factor.
\param zoomy The vertical zoom factor.
\param dstwidth Pointer to an integer to store the calculated width of the zoomed target surface.
\param dstheight Pointer to an integer to store the calculated height of the zoomed target surface.
*/
void rtgui_dc_zoom_size(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight)
{
    /*
    * Make zoom factors positive
    */
    int flipx, flipy;
    flipx = (zoomx<0.0);
    if (flipx) zoomx = -zoomx;
    flipy = (zoomy<0.0);
    if (flipy) zoomy = -zoomy;

    /*
    * Sanity check zoom factors
    */
    if (zoomx < VALUE_LIMIT)
    {
        zoomx = VALUE_LIMIT;
    }
    if (zoomy < VALUE_LIMIT)
    {
        zoomy = VALUE_LIMIT;
    }

    /*
    * Calculate target size
    */
    *dstwidth = (int) floor(((double) width * zoomx) + 0.5);
    *dstheight = (int) floor(((double) height * zoomy) + 0.5);
    if (*dstwidth < 1)
    {
        *dstwidth = 1;
    }
    if (*dstheight < 1)
    {
        *dstheight = 1;
    }
}

/*!
\brief Zoom a surface by independent horizontal and vertical factors with optional smoothing.

Zooms a 32bit or 8bit 'src' surface to newly created 'dst' surface.
'zoomx' and 'zoomy' are scaling factors for width and height. If 'smooth' is on
then the destination 32bit surface is anti-aliased. If the surface is not 8bit
or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
If zoom factors are negative, the image is flipped on the axes.

\param src The surface to zoom.
\param zoomx The horizontal zoom factor.
\param zoomy The vertical zoom factor.
\param smooth Antialiasing flag; set to SMOOTHING_ON to enable.

\return The new, zoomed surface.
*/
struct rtgui_dc *rtgui_dc_zoom(struct rtgui_dc *dc, double zoomx, double zoomy, int smooth)
{
    struct rtgui_dc_buffer *rz_src;
    struct rtgui_dc_buffer *rz_dst;
    int dstwidth, dstheight;
    int flipx, flipy;

    /* Sanity check */
    if (dc == RT_NULL) return dc;
    rz_src = (struct rtgui_dc_buffer*)(dc);
    /* we only support 32bit */
    if (rtgui_color_get_bits(rz_src->pixel_format) != 32) return RT_NULL;

    flipx = (zoomx<0.0);
    if (flipx) zoomx = -zoomx;
    flipy = (zoomy<0.0);
    if (flipy) zoomy = -zoomy;

    /* Get size if target */
    rtgui_dc_zoom_size(rz_src->width, rz_src->height, zoomx, zoomy, &dstwidth, &dstheight);

    /*
    * Alloc space to completely contain the zoomed surface
    */
    rz_dst = (struct rtgui_dc_buffer*)rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888,
             dstwidth, dstheight + GUARD_ROWS);
    /* Check target */
    if (rz_dst == RT_NULL) return RT_NULL;

    /*
    * Call the 32bit transformation routine to do the zooming (using alpha)
    */
    if (_rtgui_dc_zoom_RGBA(rz_src, rz_dst, flipx, flipy, smooth) != 0)
    {
        rtgui_dc_destory(RTGUI_DC(rz_dst));
        rz_dst = RT_NULL;
    }

    /*
    * Return destination surface
    */
    return RTGUI_DC(rz_dst);
}
RTM_EXPORT(rtgui_dc_zoom);

/*!
\brief Shrink a dc by an integer ratio using averaging.

Shrinks a 32bit or 8bit 'src' buffer dc to a newly created 'dst' dc.
'factorx' and 'factory' are the shrinking ratios (i.e. 2=1/2 the size,
3=1/3 the size, etc.) The destination dc is antialiased by averaging
the source box RGBA or Y information. If the surface is not 8bit
or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
The input surface is not modified. The output surface is newly allocated.

\param src The surface to shrink.
\param factorx The horizontal shrinking ratio.
\param factory The vertical shrinking ratio.

\return The new, shrunken surface.
*/
struct rtgui_dc *rtgui_dc_shrink(struct rtgui_dc *dc, int factorx, int factory)
{
    struct rtgui_dc_buffer *rz_src;
    struct rtgui_dc_buffer *rz_dst;
    int dstwidth, dstheight;

    /*
    * Sanity check
    */
    if (dc == RT_NULL) return dc;
    rz_src = (struct rtgui_dc_buffer*)(dc);
    /* we only support 32bit */
    if (rtgui_color_get_bits(rz_src->pixel_format) != 32) return RT_NULL;

    /* Get size for target */
    dstwidth=rz_src->width/factorx;
    while (dstwidth*factorx>rz_src->width)
    {
        dstwidth--;
    }
    dstheight=rz_src->height/factory;
    while (dstheight*factory>rz_src->height)
    {
        dstheight--;
    }

    /*
    * Target surface is 32bit with source RGBA/ABGR ordering
    */
    rz_dst = (struct rtgui_dc_buffer*)rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888,
             dstwidth, dstheight + GUARD_ROWS);
    /* Check target */
    if (rz_dst == RT_NULL) return RT_NULL;

    /*
    * Call the 32bit transformation routine to do the shrinking (using alpha)
    */
    if (_rtgui_dc_shrink_RGBA(rz_src, rz_dst, factorx, factory) != 0)
    {
        rtgui_dc_destory(RTGUI_DC(rz_dst));
        rz_dst = RT_NULL;
    }

    /*
    * Return destination surface
    */
    return RTGUI_DC(rz_dst);
}
RTM_EXPORT(rtgui_dc_shrink);