summaryrefslogtreecommitdiff
path: root/NxWidgets/libnxwidgets/src/cgraphicsport.cxx
blob: 73f2352eb37b33eaf49957d92333e8a7ca5d9e4a (plain) (blame)
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
/****************************************************************************
 * NxWidgets/libnxwidgets/src/cgraphicsport.cxx
 *
 *   Copyright (C) 2012 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
 *    me be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************
 *
 * Portions of this package derive from Woopsi (http://woopsi.org/) and
 * portions are original efforts.  It is difficult to determine at this
 * point what parts are original efforts and which parts derive from Woopsi.
 * However, in any event, the work of  Antony Dzeryn will be acknowledged
 * in most NxWidget files.  Thanks Antony!
 *
 *   Copyright (c) 2007-2011, Antony Dzeryn
 *   All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * * Neither the names "Woopsi", "Simian Zombie" nor the
 *   names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/
 
#include <nuttx/config.h>

#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <cerrno>
#include <debug.h>

#include <nuttx/nx/nxglib.h>
#include <nuttx/rgbcolors.h>

#include "nxconfig.hxx"
#include "inxwindow.hxx"
#include "cnxstring.hxx"
#include "crect.hxx"
#include "cnxfont.hxx"
#include "cgraphicsport.hxx"
#include "cwidgetstyle.hxx"
#include "cbitmap.hxx"
#include "singletons.hxx"

/****************************************************************************
 * Pre-Processor Definitions
 ****************************************************************************/

/****************************************************************************
 * Method Implementations
 ****************************************************************************/

using namespace NXWidgets;

/**
 * Constructor.
 *
 * @param pNxWnd. An instance of the underlying window type.
 * @param backColor.  The background color is only needed if we
 *   cannot read from the graphics device.
 */

#ifdef CONFIG_NX_WRITEONLY
CGraphicsPort::CGraphicsPort(INxWindow *pNxWnd, nxgl_mxpixel_t backColor)
{
  m_pNxWnd    = pNxWnd;
  m_backColor = backColor;
}
#else
CGraphicsPort::CGraphicsPort(INxWindow *pNxWnd)
{
  m_pNxWnd = pNxWnd;
}
#endif

/**
 * Destructor.
 */

CGraphicsPort::~CGraphicsPort(void)
{
  // m_pNxWnd is not deleted.  This is an abstract base class and
  // the caller of the CGraphicsPort instance is responsible for
  // the window destruction.
};

/**
 * Return the absolute x coordinate of the upper left hand corner of the
 * underlying window.
 *
 * @return The x coordinate of the underlying window.
 */

const nxgl_coord_t CGraphicsPort::getX(void) const
{
  struct nxgl_point_s pos;
  (void)m_pNxWnd->getPosition(&pos);
  return pos.x;
};

/**
 * Return the absolute y coordinate of the upper left hand corner of the
 * underlying window.
 *
 * @return The y coordinate of the underlying window.
 */

const nxgl_coord_t CGraphicsPort::getY(void) const
{
  struct nxgl_point_s pos;
  (void)m_pNxWnd->getPosition(&pos);
  return pos.y;
};

/**
 * Draw a pixel into the window.
 *
 * @param x The window-relative x coordinate of the pixel.
 * @param y The window-relative y coordinate of the pixel.
 * @param color The color of the pixel.
 */

void CGraphicsPort::drawPixel(nxgl_coord_t x, nxgl_coord_t y,
                              nxgl_mxpixel_t color)
{
  struct nxgl_point_s pos;
  pos.x = x;
  pos.y = y;
  m_pNxWnd->setPixel(&pos, color);
}

/**
 * Draw a horizontal line of the specified start position, width, and color.
 *
 * @param x The x coordinate of the line.
 * @param y The y coordinate of the top-most end of the line.
 * @param width The width of the line in pixels.
 * @param color The color of the line.
 */

void CGraphicsPort::drawHorizLine(nxgl_coord_t x, nxgl_coord_t y,
                                  nxgl_coord_t width, nxgl_mxpixel_t color)
{
  FAR struct nxgl_rect_s dest;
  nxgl_coord_t halfwidth;

  // Express the line as a rectangle

  halfwidth = width >> 1;

  dest.pt1.x = x;
  dest.pt1.y = y;
  dest.pt2.x = x + width - 1;
  dest.pt2.y = y;

  // Draw the line

  if (!m_pNxWnd->fill(&dest, color))
    {
      gdbg("INxWindow::fill failed\n");
    }
}

/**
 * Draw a vertical line of the specified start position, width, and
 * color.
 *
 * @param x The x coordinate of the left-most end of the line.
 * @param y The y coordinate of the line.
 * @param height The height of the line in rows.
 * @param color The color of the line.
 */

void CGraphicsPort::drawVertLine(nxgl_coord_t x, nxgl_coord_t y,
                                 nxgl_coord_t height, nxgl_mxpixel_t color)
{
  FAR struct nxgl_rect_s dest;

  // Express the line as a rectangle

  dest.pt1.x = x;
  dest.pt1.y = y;
  dest.pt2.x = x;
  dest.pt2.y = y + height - 1;

  // Draw the line

  if (!m_pNxWnd->fill(&dest, color))
    {
      gdbg("INxWindow::fill failed\n");
    }
}

/**
 * Draw a line of a fixed color in the window.
 *
 * @param x1 The x coordinate of the start point of the line.
 * @param y1 The y coordinate of the start point of the line.
 * @param x2 The x coordinate of the end point of the line.
 * @param y2 The y coordinate of the end point of the line.
 * @param color The color of the line.
 */

void CGraphicsPort::drawLine(nxgl_coord_t x1, nxgl_coord_t y1,
                             nxgl_coord_t x2, nxgl_coord_t y2,
                             nxgl_mxpixel_t color)
{
  struct nxgl_vector_s vector;

  vector.pt1.x = x1;
  vector.pt1.y = y1;
  vector.pt2.x = x2;
  vector.pt2.y = y2;

  if (!m_pNxWnd->drawLine(&vector, 1, color))
    {
      gdbg("INxWindow::drawLine failed\n");
    }
}

/**
 * Draw a filled rectangle to the bitmap.
 *
 * @param x The window-relative x coordinate of the rectangle.
 * @param y The window-relative y coordinate of the rectangle.
 * @param width The width of the rectangle in pixels.
 * @param height The height of the rectangle in rows.
 * @param color The color of the rectangle.
 */

void CGraphicsPort::drawFilledRect(nxgl_coord_t x, nxgl_coord_t y,
                                   nxgl_coord_t width, nxgl_coord_t height,
                                   nxgl_mxpixel_t color)
{
  struct nxgl_rect_s rect;
  rect.pt1.x = x;
  rect.pt1.y = y;
  rect.pt2.x = x + width - 1;
  rect.pt2.y = y + height - 1;
  m_pNxWnd->fill(&rect, color);
}

/**
 * Draw an unfilled rectangle to the window
 *
 * @param x The window-relative x coordinate of the rectangle.
 * @param y The window-relative y coordinate of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @param color The color of the rectangle outline.
 */

void CGraphicsPort::drawRect(nxgl_coord_t x, nxgl_coord_t y,
                             nxgl_coord_t width, nxgl_coord_t height,
                             nxgl_mxpixel_t color)
{
  drawHorizLine(x, y, width, color);
  drawHorizLine(x, y + height - 1, width, color);
  drawVertLine(x, y, height, color);
  drawVertLine(x + width -1, y, height, color);
}

/**
 * Draw a bevelled rectangle to the window.
 *
 * @param x The x coordinate of the rectangle.
 * @param y The y coordinate of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @param shineColor The color of the top/left sides.
 * @param shadowColor The color of the bottom/right sides.
 */

void CGraphicsPort::drawBevelledRect(nxgl_coord_t x, nxgl_coord_t y,
                                     nxgl_coord_t width, nxgl_coord_t height,
                                     nxgl_mxpixel_t shineColor,
                                     nxgl_mxpixel_t shadowColor)
{
  drawHorizLine(x, y, width, shineColor);                        // Top
  drawHorizLine(x, y + height - 1, width, shadowColor);          // Bottom
  drawVertLine(x, y + 1, height - 2, shineColor);                // Left
  drawVertLine(x + width - 1, y + 1, height - 2, shadowColor);   // Right
}

/**
 * Draw an opaque bitmap to the window.
 *
 * @param x The window-relative x coordinate to draw the bitmap to.
 * @param y The window-relative y coordinate to draw the bitmap to.
 * @param width The width of the bitmap to draw.
 * @param height The height of the bitmap to draw.
 * @param bitmap Pointer to the bitmap to draw.
 * @param bitmapX The window-relative x coordinate within the supplied
 *    bitmap to use as the origin.
 * @param bitmapY The window-relative y coordinate within the supplied
 *   bitmap to use as the origin.
 */

void CGraphicsPort::drawBitmap(nxgl_coord_t x, nxgl_coord_t y,
                               nxgl_coord_t width, nxgl_coord_t height,
                               const struct SBitmap *bitmap,
                               int bitmapX, int bitmapY)
{
  // origin - The origin of the upper, left-most corner of the full bitmap.
  //          Both dest and origin are in window coordinates, however, origin
  //          may lie outside of the display.

  struct nxgl_point_s origin;
  origin.x   = x - bitmapX;
  origin.y   = y - bitmapY;

  // dest - Describes the rectangular on the display that will receive the
  //        the bit map.

  struct nxgl_rect_s dest;
  dest.pt1.x = x;
  dest.pt1.y = y;
  dest.pt2.x = x + width - 1;
  dest.pt2.y = y + height - 1;

  // Blit the bitmap

  (void)m_pNxWnd->bitmap(&dest, (FAR const void *)bitmap->data, &origin, bitmap->stride);
}

/**
 * Draw a bitmap to the window, using the supplied transparent
 * color as an invisible color.
 *
 * @param x The window-relative x coordinate to draw the bitmap to.
 * @param y The window-relative y coordinate to draw the bitmap to.
 * @param width The width of the bitmap to draw.
 * @param height The height of the bitmap to draw.
 * @param bitmap Pointer to the bitmap to draw.
 * @param bitmapX The window-relative x coordinate within the supplied bitmap to use as
 * the origin.
 * @param bitmapY The window-relative y coordinate within the supplied bitmap to use as
 * the origin.
 * @param transparentColor The transparent color used in the bitmap.
 */

void CGraphicsPort::drawBitmap(nxgl_coord_t x, nxgl_coord_t y,
                               nxgl_coord_t width, nxgl_coord_t height,
                               const struct SBitmap *bitmap,
                               int bitmapX, int  bitmapY,
                               nxgl_mxpixel_t transparentColor)
{
  // Get the starting position in the image, offset by bitmapX and bitmapY into the image.

  FAR uint8_t *srcLine = (uint8_t *)bitmap->data +
                         bitmapY * bitmap->stride +
                         ((bitmapX * bitmap->bpp + 7) >> 3);
  FAR nxwidget_pixel_t *srcPtr  = (nxwidget_pixel_t *)srcLine;
  
  // Loop until all rows have been displayed

  nxgl_coord_t firstX = x;
  nxgl_coord_t lastX  = x + width;
  nxgl_coord_t lastY  = y + height;

  while (y < lastY)
    {
      // Search for the next non-transparent pixel in the source image

      while (*srcPtr == transparentColor)
        {
          // The pixel is transparent.  Move to the next column

          if (++x >= lastX)
            {
              // We are at the end of the row.  Reset to the next row

              x = firstX;
              y++;

              // Was that the last row:

              if (y >= lastY)
                {
                  // Yes, then we are finished

                  return;
                }

              // Update the source data pointers to the beginning of the next
              // row

              srcLine += bitmap->stride;
              srcPtr   = (nxwidget_pixel_t *)srcLine;
            }
          else
            {
              // This is another transparent pixel on the same row

              srcPtr++;
            }
        }

      // srcPtr points to the next non-transparent color.  Save this
      // as the start of a run that has length of at least one

      FAR nxwidget_pixel_t *runPtr   = srcPtr;
      nxgl_coord_t          runX     = x;
      nxgl_coord_t          runWidth = 0;

      // Now search for the next transparent pixel on the same row.
      // This will determine the length of the run

      do
        {
          // This is another non-transparent pixel on the same row

          runWidth++;
          srcPtr++;

          // Move to the next column

          if (++x >= lastX)
            {
              // We are at the end of the row.  Reset to the next row

              x = firstX;
              y++;

              // Update the source data pointers to the beginning of the next
              // row

              srcLine += bitmap->stride;
              srcPtr   = (nxwidget_pixel_t *)srcLine;

              // And break out of this loop

              break;
            }
        }
      while (*srcPtr != transparentColor);

      // When we come out of the above loop, either (1) srcPtr points to the
      // next transparent pixel on the same row, or (2) srcPtr points to the
      // first pixel in the next row.

      // origin - The origin of the upper, left-most corner of the full bitmap.
      //          Both dest and origin are in window coordinates, however, origin
      //          may lie outside of the display.

      struct nxgl_point_s origin;
      origin.x   = runX;
      origin.y   = y;

      // dest - Describes the rectangular on the display that will receive the
      //        the bit map.

      struct nxgl_rect_s dest;
      dest.pt1.x = runX;
      dest.pt1.y = y;
      dest.pt2.x = runX + runWidth - 1;
      dest.pt2.y = y;

      // Blit the bitmap

      (void)m_pNxWnd->bitmap(&dest, (FAR const void *)runPtr, &origin, bitmap->stride);
    }
}

/**
 * Draw a bitmap to the port in greyscale.
 *
 * @param x The window-relative x coordinate to draw the bitmap to.
 * @param y The window-relative y coordinate to draw the bitmap to.
 * @param width The width of the bitmap to draw.
 * @param height The height of the bitmap to draw.
 * @param bitmap Pointer to the bitmap to draw.
 * @param bitmapX The window-relative x coordinate within the supplied bitmap to use as
 * the origin.
 * @param bitmapY The window-relative y coordinate within the supplied bitmap to use as
 * the origin.
 */

void CGraphicsPort::drawBitmapGreyScale(nxgl_coord_t x, nxgl_coord_t y,
                                        nxgl_coord_t width, nxgl_coord_t height,
                                        const struct SBitmap *bitmap,
                                        int bitmapX, int bitmapY)
{
  // Working buffer.  Holds one converted row from the bitmap

  FAR nxwidget_pixel_t *run = new nxwidget_pixel_t[width];

  // Pointer to the beginning of the first source row

  FAR uint8_t *src = (FAR uint8_t *)bitmap->data + bitmapY * bitmap->stride + bitmapX;

  // Setup non-changing blit parameters

  struct nxgl_point_s origin;
  origin.x   = 0;
  origin.y   = 0;

  struct nxgl_rect_s dest;
  dest.pt1.x = x;
  dest.pt1.y = y;
  dest.pt2.x = x + width - 1;
  dest.pt2.y = y;

  // Convert each row to greyscale and send it to the display
   
  for (int row = 0; row < height; row++)
    {
      // Convert the next row

      FAR nxwidget_pixel_t *runSrc  = (FAR nxwidget_pixel_t *)src;
      FAR nxwidget_pixel_t *runDest = run;

      for (int col = 0; col < width; col++)
        {
          // Get the next RGB pixel and break out the individual components

          nxwidget_pixel_t rgb = *runSrc++;
          nxwidget_pixel_t r = RGB2RED(rgb);
          nxwidget_pixel_t g = RGB2GREEN(rgb);
          nxwidget_pixel_t b = RGB2BLUE(rgb);

          // A truly accurate greyscale conversion would be complex.  Let's
          // just average.
 
          nxwidget_pixel_t avg = (r + g + b) / 3;
          *runDest++ = MKRGB(avg, avg, avg);
        }

      // Now blit the single row

      (void)m_pNxWnd->bitmap(&dest, (FAR void *)bitmap->data, &origin, bitmap->stride);

       // Setup for the next source row

       y++;
       dest.pt1.y = y;
       dest.pt2.y = y;

       src += bitmap->stride;
    }

  delete run;
}

/**
 * Draw a string to the window.
 *
 * @param pos The window-relative x/y coordinate of the string.
 * @param bound The window-relative bounds of the string.
 * @param font The font to draw with.
 * @param string The string to output.
 */

void CGraphicsPort::drawText(struct nxgl_point_s *pos, CRect *bound,
                             CNxFont *font, const CNxString &string)
{
  drawText(pos, bound, font, string, 0, string.getLength());
}

/**
 * Draw a particular length of a string to the window in a secific color.
 *
 * @param pos The window-relative x/y coordinate of the string.
 * @param bound The window-relative bounds of the string.
 * @param font The font to draw with.
 * @param string The string to output.
 * @param startIndex The start index within the string from which
 *   drawing will commence.
 * @param length The number of characters to draw.
 * @param color The color of the string.
 */

void CGraphicsPort::drawText(struct nxgl_point_s *pos, CRect *bound,
                             CNxFont *font, const CNxString &string,
                             int startIndex, int length,
                             nxgl_mxpixel_t color)
{
  // Temporarily change the font color

  nxgl_mxpixel_t savedColor = font->getColor();
  font->setColor(color);

  // Draw the string with this new color

  drawText(pos, bound, font, string, startIndex, length);

  // Restore the font color

  font->setColor(savedColor);
}

/**
 * Draw a portion of a string to the window.
 * @param pos The window-relative x/y coordinate of the string.
 * @param bound The window-relative bounds of the string.
 * @param font The font to draw with.
 * @param string The string to output.
 * @param startIndex The start index within the string from which
 * drawing will commence.
 * @param length The number of characters to draw.
 */

void CGraphicsPort::drawText(struct nxgl_point_s *pos, CRect *bound,
                             CNxFont *font, const CNxString &string,
                             int startIndex, int length)
{
  drawText(pos, bound, font, string, startIndex, length, 0, true);
}

/**
 * Draw a portion of a string to the window and fill the background
 * in one go.
 * @param pos The window-relative x/y coordinate of the string.
 * @param bound The window-relative bounds of the string.
 * @param font The font to draw with.
 * @param string The string to output.
 * @param startIndex The start index within the string from which
 * drawing will commence.
 * @param length The number of characters to draw.
 * @param color Foreground color
 * @param background Background color
 */

void CGraphicsPort::drawText(struct nxgl_point_s *pos, CRect *bound,
                             CNxFont *font, const CNxString &string,
                             int startIndex, int length,
                             nxgl_mxpixel_t color,
                             nxgl_mxpixel_t background)
{
  nxgl_mxpixel_t savedColor = font->getColor();
  font->setColor(color);
  
  drawText(pos, bound, font, string, startIndex, length, background, false);
  
  font->setColor(savedColor);
}

/**
 * The underlying implementation for drawText functions
 * @param pos The window-relative x/y coordinate of the string.
 * @param bound The window-relative bounds of the string.
 * @param font The font to draw with.
 * @param string The string to output.
 * @param startIndex The start index within the string from which
 * drawing will commence.
 * @param length The number of characters to draw.
 * @param background Color to use for background if transparent is false.
 * @param transparent Whether to fill the background.
 */
void CGraphicsPort::drawText(struct nxgl_point_s *pos, CRect *bound,
                             CNxFont *font, const CNxString &string,
                             int startIndex, int length,
                             nxgl_mxpixel_t background,
                             bool transparent)
{
  // Verify index and length

  int stringLength = string.getLength();
  if (startIndex >= stringLength)
    {
      return;
    }

  int endIndex = startIndex + length;
  if (endIndex > stringLength)
    {
      endIndex = stringLength;
    }

#ifdef CONFIG_NX_WRITEONLY
  if (transparent)
    {
      // Can't render transparently without reading memory.

      transparent = false;
      background = m_backColor;
    }
#endif
    
  // Allocate a bit of memory to hold the largest rendered font

  unsigned int bmWidth   = ((unsigned int)font->getMaxWidth() * CONFIG_NXWIDGETS_BPP + 7) >> 3;
  unsigned int bmHeight  = (unsigned int)font->getHeight();

  unsigned int glyphSize =  bmWidth * bmHeight;
  FAR uint8_t  *glyph    =  new uint8_t[glyphSize];

  // Get the bounding rectangle in NX form

  struct nxgl_rect_s boundingBox;
  bound->getNxRect(&boundingBox);
  
  // Loop setup

  struct SBitmap bitmap;
  bitmap.bpp    = CONFIG_NXWIDGETS_BPP;
  bitmap.fmt    = CONFIG_NXWIDGETS_FMT;
  bitmap.data   = (FAR const nxgl_mxpixel_t*)glyph;

  // Loop for each letter in the sub-string

  for (int i = startIndex; i < endIndex; i++)
    {
      // Get the next letter in the string

      const nxwidget_char_t letter = string.getCharAt(i);

      // Get the font metrics for this letter

      struct nx_fontmetric_s metrics;
      font->getCharMetrics(letter, &metrics);

      // Get the width of the font (in pixels)

      nxgl_coord_t fontWidth  = (nxgl_coord_t)(metrics.width + metrics.xoffset);

      // Does the letter have height?  Spaces have width, but no height

      if (metrics.height > 0 || !transparent)
        {
          // Set the current, effective size of the bitmap

          bitmap.width  = fontWidth;
          bitmap.height = bmHeight;
          bitmap.stride = (fontWidth * bitmap.bpp + 7) >> 3;

          // Describe the destination of the font as a bounding box

          struct nxgl_rect_s dest;
          dest.pt1.x = pos->x;
          dest.pt1.y = pos->y;
          dest.pt2.x = pos->x + fontWidth - 1;
          dest.pt2.y = pos->y + bmHeight - 1;

          // Get the interection of the font box and the bounding box

          struct nxgl_rect_s intersection;
          nxgl_rectintersect(&intersection, &dest, &boundingBox);

          // Skip to the next character if this one is completely outside
          // the bounding box.

          if (!nxgl_nullrect(&intersection))
            {
              // If we have been given a background color, use it to fill the array.
              // Otherwise initialize the bitmap memory by reading from the display.
              // The font renderer always renders the fonts on a transparent background.
              
              if (!transparent)
                {
                  // Set the glyph memory to the background color

                  nxwidget_pixel_t *bmPtr   = (nxwidget_pixel_t *)bitmap.data;
                  unsigned int      npixels = fontWidth * bmHeight;
                  for (unsigned int j = 0; j < npixels; j++)
                    {
                      *bmPtr++ = background;
                    }
                }
              else
                {
                  // Read the current contents of the destination into the glyph memory

                  m_pNxWnd->getRectangle(&dest, &bitmap);
                }

              // Render the font into the initialized bitmap

              font->drawChar(&bitmap, letter);

              // Then put the font on the display

              if (!m_pNxWnd->bitmap(&intersection, (FAR const void *)bitmap.data,
                                   pos, bitmap.stride))
                {
                  gvdbg("nx_bitmapwindow failed: %d\n", errno);
                }
            }
        }

      // Adjust the X position for the next character in the string        

      pos->x += fontWidth;
    }

  delete glyph;
}

/**
 * Copy a rectangular region from the source coordinates to the
 * destination coordinates.
 *
 * @param sourceX Source x coordinate.
 * @param sourceY Source y coordinate.
 * @param destX Destination x coordinate.
 * @param destY Destination y coordinate.
 * @param width Width of the rectangle to copy.
 * @param height Height of the rectangle to copy.
 */

void CGraphicsPort::copy(nxgl_coord_t sourceX, nxgl_coord_t sourceY,
                         nxgl_coord_t destX, nxgl_coord_t destY,
                         nxgl_coord_t width, nxgl_coord_t height)
{
  struct nxgl_rect_s rect;
  struct nxgl_point_s offset;

  rect.pt1.x = sourceX;
  rect.pt1.y = sourceY;
  rect.pt1.x = sourceX + width - 1;
  rect.pt1.y = sourceY + height - 1;

  offset.x = destX - sourceX;
  offset.y = destY - sourceY;

  (void)m_pNxWnd->move(&rect, &offset);
}

/**
 * Move a region by a specified distance in two dimensions.
 *
 * @param x X coordinate of the source area to move.
 * @param y Y coordinate of the source area to move.
 * @param deltaX Horizontal distance to move.
 * @param deltaY Vertical distance to move.
 * @param width Width of the area to move.
 * @param height Height of the area to move.
 */

void CGraphicsPort::move(nxgl_coord_t x, nxgl_coord_t y,
                         nxgl_coord_t deltaX, nxgl_coord_t deltaY,
                         nxgl_coord_t width, nxgl_coord_t height)
{
  struct nxgl_rect_s rect;
  rect.pt1.x = x;
  rect.pt1.y = y;
  rect.pt2.x = x + width - 1;
  rect.pt2.y = y + height - 1;

  struct nxgl_point_s offset;
  offset.x = deltaX;
  offset.y = deltaY;

  (void)m_pNxWnd->move(&rect, &offset);
}

/**
 * Convert the region to greyscale.
 *
 * @param x X coordinate of the region to change.
 * @param y Y coordinate of the region to change.
 * @param width Width of the region to change.
 * @param height Height of the region to change.
 */

void CGraphicsPort::greyScale(nxgl_coord_t x, nxgl_coord_t y,
                              nxgl_coord_t width, nxgl_coord_t height)
{
  // Allocate memory to hold one row of graphics data

  unsigned int stride    = ((unsigned int)width * CONFIG_NXWIDGETS_BPP + 7) >> 3;
  FAR uint8_t *rowBuffer = new uint8_t[height * stride];
  if (!rowBuffer)
    {
      return;
    }

  // Describe the receiving bitmap memory

  SBitmap rowBitmap;
  rowBitmap.bpp    = CONFIG_NXWIDGETS_BPP;
  rowBitmap.fmt    = CONFIG_NXWIDGETS_FMT;
  rowBitmap.width  = width;
  rowBitmap.height = 1;
  rowBitmap.stride = stride;
  rowBitmap.data   = (FAR const nxgl_mxpixel_t *)rowBuffer;

  // Partially describe the source rectangle

  struct nxgl_rect_s rect;
  rect.pt1.x = x;
  rect.pt2.x = x + width - 1;

  // Partially setup the graphics origin

  struct nxgl_point_s origin;
  origin.x = x;

  // Loop for each row in the region to be inverted

  for (int row = 0; row < height; row++)
    {
      // Read the graphic memory corresponding to the row

      rect.pt1.y = rect.pt2.y = y + row;
      m_pNxWnd->getRectangle(&rect, &rowBitmap);

      // Convert each bit to  each bit

      nxwidget_pixel_t *ptr = (nxwidget_pixel_t *)rowBuffer;
      for (int col = 0; col < width; col++)
        {
          // Get the next RGB pixel and break out the individual components

          nxwidget_pixel_t color = *ptr;
          uint8_t red   = RGB2RED(color);
          uint8_t green = RGB2GREEN(color);
          uint8_t blue  = RGB2BLUE(color);

          // A truly accurate greyscale conversion would be complex.  Let's
          // just average.

          nxwidget_pixel_t avg = (red + green + blue) / 3;
          *ptr++ = MKRGB(avg, avg, avg);
        }

      // Then write the row back to graphics memory

      origin.y = rect.pt1.y;
      m_pNxWnd->bitmap(&rect, (FAR const void *)rowBitmap.data,
                       &origin, rowBitmap.stride) ;
    }

  delete rowBuffer;
}

/**
 * Invert colors in a region.  NOTE:  This allocates an in-memory
 * buffer the size of one row in graphic memory.  So it may only be
 * useful for inverting small regions and its only current use of for
 * the inverted cursor text.
 *
 * @param x X coordinate of the region to change.
 * @param y Y coordinate of the region to change.
 * @param width Width of the region to change.
 * @param height Height of the region to change.
 */

void CGraphicsPort::invert(nxgl_coord_t x, nxgl_coord_t y,
                           nxgl_coord_t width, nxgl_coord_t height)
{
  // Allocate memory to hold one row of graphics data

  unsigned int stride    = ((unsigned int)width * CONFIG_NXWIDGETS_BPP + 7) >> 3;
  FAR uint8_t *rowBuffer = new uint8_t[height * stride];
  if (!rowBuffer)
    {
      return;
    }

  // Describe the receiving bitmap memory

  SBitmap rowBitmap;
  rowBitmap.bpp    = CONFIG_NXWIDGETS_BPP;
  rowBitmap.fmt    = CONFIG_NXWIDGETS_FMT;
  rowBitmap.width  = width;
  rowBitmap.height = 1;
  rowBitmap.stride = stride;
  rowBitmap.data   = (FAR const nxgl_mxpixel_t *)rowBuffer;

  // Partially describe the source rectangle

  struct nxgl_rect_s rect;
  rect.pt1.x = x;
  rect.pt2.x = x + width - 1;

  // Partially setup the graphics origin

  struct nxgl_point_s origin;
  origin.x = x;

  // Loop for each row in the region to be inverted

  for (int row = 0; row < height; row++)
    {
      // Read the graphic memory corresponding to the row

      rect.pt1.y = rect.pt2.y = y + row;
      m_pNxWnd->getRectangle(&rect, &rowBitmap);

      // Invert each bit

      nxwidget_pixel_t *ptr = (nxwidget_pixel_t *)rowBuffer;
      for (int col = 0; col < width; col++)
        {
          nxwidget_pixel_t color = *ptr;
          uint8_t red   = RGB2RED(color);
          uint8_t green = RGB2GREEN(color);
          uint8_t blue  = RGB2BLUE(color);
          *ptr++ = MKRGB(~red, ~green, ~blue);
        }

      // Then write the row back to graphics memory

      origin.y = rect.pt1.y;
      m_pNxWnd->bitmap(&rect, (FAR const void *)rowBitmap.data,
                       &origin, rowBitmap.stride) ;
    }

  delete rowBuffer;
};