From 8216275fadfe830227eee7bb60a6aef4014d7d8b Mon Sep 17 00:00:00 2001
From: patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>
Date: Tue, 2 Aug 2011 01:12:16 +0000
Subject: [PATCH] Add nxgl_splitline()

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3835 42af7a65-404d-4744-a932-0658087f49c3
---
 ChangeLog                          |   3 +-
 configs/stm3210e-eval/src/up_lcd.c |  26 ++-
 graphics/nxglib/Make.defs          |   2 +-
 graphics/nxglib/nxglib_splitline.c | 314 +++++++++++++++++++++++++++++
 include/nuttx/nx/nxglib.h          |  51 +++++
 5 files changed, 391 insertions(+), 5 deletions(-)
 create mode 100644 graphics/nxglib/nxglib_splitline.c

diff --git a/ChangeLog b/ChangeLog
index 4ce1bab9b6..2171a4066c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1960,5 +1960,6 @@
 	  controller.
 	* graphics/nxglib/lcd and fb:  Add low level routines to set single pixels.
 	* lib/math/lib_b16atan2.c:  Add a fixed precision atan2() function
-
+	* graphics/nxglib/nxglib_splitline.c: Add logic to divide a wide line into
+	  trapezoidal components.
 
diff --git a/configs/stm3210e-eval/src/up_lcd.c b/configs/stm3210e-eval/src/up_lcd.c
index a183a04cb1..fccf680a5f 100755
--- a/configs/stm3210e-eval/src/up_lcd.c
+++ b/configs/stm3210e-eval/src/up_lcd.c
@@ -33,7 +33,15 @@
  * POSSIBILITY OF SUCH DAMAGE.
  *
  **************************************************************************************/
-
+/* This driver supports the following LCDs:
+ *
+ * 1. Ampire AM-240320LTNQW00H
+ * 2. Orise Tech SPFD5408B
+ * 3. RenesasSP R61580
+ *
+ * The driver dynamically selects the LCD based on the reported LCD ID value.
+ */
+ 
 /**************************************************************************************
  * Included Files
  **************************************************************************************/
@@ -259,6 +267,11 @@
 
 #define LCD_BL_TIMER_PERIOD   8999
 
+/* LCD IDs */
+
+#define SPFD5408B_ID          0x5408
+#define R61580_ID             0x1580
+
 /* Debug ******************************************************************************/
 
 #ifdef CONFIG_DEBUG_LCD
@@ -848,9 +861,16 @@ static int stm3210e_setcontrast(struct lcd_dev_s *dev, unsigned int contrast)
 
 static inline void stm3210e_lcdinitialize(void)
 {
-  /* Check if the LCD is SPFD5408B Controller */
+  uint16_t id;
+
+  /* Check if the LCD is Orise Tech SPFD5408B Controller (or the compatible RenesasSP
+   * R61580).
+   */
+
+  id = stm3210e_readreg(LCD_REG_0);
+  lcddbg("ID: %04x\n", id);
 
-  if (stm3210e_readreg(LCD_REG_0) == 0x5408)
+  if (id == SPFD5408B_ID || id == R61580_ID)
     {
       g_lcddev.spfd5408b = true;
 
diff --git a/graphics/nxglib/Make.defs b/graphics/nxglib/Make.defs
index 44b2801a0a..22c9573968 100644
--- a/graphics/nxglib/Make.defs
+++ b/graphics/nxglib/Make.defs
@@ -77,7 +77,7 @@ COLOR_CSRCS	= nxglib_colorcopy.c
 
 LCD_CSRCS	= 
 
-NXGLIB_CSRCS	= nxglib_rgb2yuv.c nxglib_yuv2rgb.c \
+NXGLIB_CSRCS	= nxglib_rgb2yuv.c nxglib_yuv2rgb.c nxglib_splitline.c \
 		  $(SETP1_CSRCS) $(SETP2_CSRCS) $(RFILL1_CSRCS) $(RFILL2_CSRCS) \
 		  $(TFILL1_CSRCS) $(TFILL2_CSRCS) $(RMOVE1_CSRCS) $(RMOVE2_CSRCS) \
 		  $(RCOPY1_CSRCS) $(RCOPY2_CSRCS) $(RECT_CSRCS) $(TRAP_CSRCS) \
diff --git a/graphics/nxglib/nxglib_splitline.c b/graphics/nxglib/nxglib_splitline.c
new file mode 100644
index 0000000000..d4d2899b3a
--- /dev/null
+++ b/graphics/nxglib/nxglib_splitline.c
@@ -0,0 +1,314 @@
+/****************************************************************************
+ * graphics/nxglib/nxsglib_splitline.c
+ *
+ *   Copyright (C) 2011 Gregory Nutt. All rights reserved.
+ *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * 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 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 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <string.h>
+#include <errno.h>
+
+#include <nuttx/nx/nxglib.h>
+
+/****************************************************************************
+ * Pre-Processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxgl_splitline
+ *
+ * Description:
+ *   In the general case, a line with width can be represented as a
+ *   parallelogram with a triangle at the top and bottom.  Triangles and
+ *   parallelograms are both degenerate versions of a trapeziod.  This
+ *   function breaks.  This function also detects other degenerate cases:
+ *
+ *   1. If y1 == y2 then the line is horizontal and is better represented
+ *      as a rectangle.
+ *   2. If x1 == x2 then the line is vertical and also better represented
+ *      as a rectangle.
+ *   3. If the width of the line is 1, then there are no triangles at the
+ *      top and bottome (this may also be the case if the width is narrow
+ *      and the line is near vertical)
+ *   4. If the line is oriented is certain angles, it may consist only of
+ *      the upper and lower triangles with no trapezoid inbetween.  In
+ *      this case, 3 trapezoids will be returned, but traps[1] will be
+ *      degenerate.
+ *
+ * Input parameters:
+ *   vector - A pointer to the vector described the line to be drawn.
+ *   traps  - A pointer to a array of trapezoids (size 3).
+ *   rect   - A pointer to a rectangle.
+ *
+ * Returned value:
+ *   0: Line successfully broken up into three trapezoids.  Values in
+ *      traps[0], traps[1], and traps[2] are valid.
+ *   1: Line successfully represented by one trapezoid. Value in traps[1]
+ *      is valid.
+ *   2: Line successfully represented by one rectangle. Value in rect is
+ *      valid
+ *  <0: On errors, a negated errno value is returned.
+ *
+ ****************************************************************************/
+
+int nxgl_splitline(FAR struct nxgl_vector_s *vector,
+                   FAR struct nxgl_trapezoid_s *traps,
+                   FAR struct nxgl_rect_s *rect,
+                   nxgl_coord_t linewidth)
+{
+  struct nxgl_vector_s line;
+  struct nxgl_point_s pt;
+  nxgl_coord_t iheight;
+  nxgl_coord_t iwidth;
+  nxgl_coord_t triheight;
+  nxgl_coord_t adjwidth;
+  nxgl_coord_t xoffset;
+  nxgl_coord_t halfoffset;
+  nxgl_coord_t halfheight;
+  b16_t angle;
+
+  /* First, check the linewidth */
+
+  if (linewidth < 1)
+    {
+      return -EINVAL;
+    }
+
+  /* Then make sure that the start position of the line is above the end
+   * position of the line... in raster order.
+   */
+
+  if (vector->pt1.y < vector->pt2.y)
+    {
+      /* Vector is already in raster order */
+
+      memcpy(&line, vector, sizeof(struct nxgl_vector_s));
+    }
+  else if (vector->pt1.y > vector->pt2.y)
+    {
+      /* Swap the top and bottom */
+
+      line.pt1.x = vector->pt2.x;
+      line.pt1.y = vector->pt2.y;
+      line.pt2.x = vector->pt1.x;
+      line.pt2.y = vector->pt1.y;
+    }
+  else
+    {
+      /* First degenerate case:  The line is horizontal. */
+
+      if (vector->pt1.x < vector->pt2.x)
+        {
+          rect->pt1.x = vector->pt1.x;
+          rect->pt2.x = vector->pt2.x;
+        }
+      else
+        {
+          rect->pt1.x = vector->pt2.x;
+          rect->pt2.x = vector->pt1.x;
+        }
+
+      /* The height of the rectangle is the width of the line, half above
+       * and half below.
+       */
+
+      rect->pt1.y = vector->pt1.y - (linewidth >> 1);
+      rect->pt2.y = rect->pt1.y + linewidth - 1;
+      return 2;
+    }
+
+  /* Check if the line is vertical */
+
+  if (line.pt1.x == line.pt2.x)
+    {
+      /* Second degenerate case:  The line is vertical. */
+
+      rect->pt1.y = line.pt1.y;
+      rect->pt2.y = line.pt2.y;
+
+      rect->pt1.x = line.pt1.x - (linewidth >> 1);
+      rect->pt2.x = rect->pt1.x + linewidth - 1;
+      return 2;
+    }
+
+  /* The final degenerate case */
+
+  if (linewidth == 1)
+    {
+      /* A line of width 1 is basically a single parallelogram of width 1 */
+
+      traps[1].top.x1 = line.pt1.x;
+      traps[1].top.x2 = line.pt1.x;
+      traps[1].top.y  = line.pt1.y;
+
+      traps[1].bot.x1 = line.pt2.x;
+      traps[1].bot.x2 = line.pt2.x;
+      traps[1].bot.y  = line.pt2.y;
+      return 1;
+    }
+
+  /* Okay, then what remains is interesting.
+   *
+   * iheight = |y2 - y1|
+   * iwidth  = |x2 - x1|
+   */
+
+  iheight = line.pt2.y - line.pt1.y + 1;
+  if (line.pt1.x < line.pt2.x)
+    {
+      iwidth  = line.pt2.x - line.pt1.x + 1;
+    }
+  else
+    {
+      iwidth  = line.pt1.x - line.pt2.x + 1;
+    }
+
+  /* 
+   * Triangle height: linewidth * cosA
+   * Adjusted width:  triheight / sinA
+   * X offset :  linewidth * linewidth / adjusted line width
+   */
+
+  angle        = b16atan2(itob16(iheight), itob16(iwidth));
+  triheight    = b16toi(linewidth * b16cos(angle));
+  adjwidth     = b16toi(b16divb16(itob16(linewidth), b16sin(angle)));
+  xoffset      = (linewidth * linewidth + (adjwidth >> 1)) / adjwidth;
+
+  halfoffset   = (xoffset   >> 1);
+  halfheight   = (triheight >> 1);
+
+  /* Return the top triangle (if there is one) */
+
+  if (triheight > 0)
+    {
+      if (line.pt1.x < line.pt2.x)
+        {
+          /* Line is going "south east" */
+
+          pt.x = line.pt1.x - halfoffset;
+          pt.y = line.pt1.y + halfheight;
+
+          traps[0].top.x1 = pt.x + xoffset;
+          traps[0].top.x2 = traps[0].top.x1;
+          traps[0].top.y  = pt.y - triheight + 1;
+          traps[0].bot.x1 = pt.x;
+          traps[0].bot.x2 = pt.x + adjwidth - 1;
+          traps[0].bot.y  = pt.y;
+
+          pt.x = line.pt2.x + halfoffset;
+          pt.y = line.pt2.y - halfheight;
+
+          traps[2].top.x1 = pt.x - adjwidth + 1;
+          traps[2].top.x2 = pt.x;
+          traps[2].top.y  = pt.y;
+          traps[2].bot.x1 = pt.x - xoffset;
+          traps[2].bot.x2 = traps[2].bot.x1;
+          traps[2].bot.y  = pt.y + triheight - 1;
+        }
+      else
+        {
+          /* Line is going "south west" */
+
+          pt.x = line.pt1.x + halfoffset;
+          pt.y = line.pt1.y + halfheight;
+
+          traps[0].top.x1 = pt.x - xoffset;
+          traps[0].top.x2 = traps[0].top.x1;
+          traps[0].top.y  = pt.y - triheight + 1;
+          traps[0].bot.x1 = pt.x - adjwidth + 1;
+          traps[0].bot.x2 = pt.x;
+          traps[0].bot.y  = pt.y;
+
+          pt.x = line.pt2.x - halfoffset;
+          pt.y = line.pt2.y - halfheight;
+
+          traps[2].top.x1 = pt.x;
+          traps[2].top.x2 = pt.x + adjwidth - 1;
+          traps[2].top.y  = pt.y;
+          traps[2].bot.x1 = pt.x + xoffset;
+          traps[2].bot.x2 = traps[2].bot.x1;
+          traps[2].bot.y  = pt.y + triheight - 1;
+        }
+
+      /* The center parallelogram is the horizontal edge of each triangle.
+       * Note the minor inefficency: that horizontal edge is drawn twice.
+       */
+
+      traps[1].top.x1 = traps[0].bot.x1;
+      traps[1].top.x2 = traps[0].bot.x2;
+      traps[1].top.y  = traps[0].bot.y;
+ 
+      traps[1].bot.x1 = traps[2].top.x1;
+      traps[1].bot.x2 = traps[2].top.x2;
+      traps[1].bot.y  = traps[2].top.y;
+ 
+      return 0;
+    }
+
+  /* The line is too vertical to have any significant triangular top or
+   * bottom.  Just return the center parallelogram.
+   */
+
+  traps[1].top.x1 = line.pt1.x - halfoffset;
+  traps[1].top.x2 = traps[1].top.x1 + adjwidth - 1;
+  traps[1].top.y  = line.pt1.y;
+ 
+  traps[1].bot.x1 = line.pt2.x - halfoffset;
+  traps[1].bot.x2 = traps[1].bot.x1 + adjwidth - 1;
+  traps[1].bot.y  = line.pt2.y;
+  return 1;
+}
diff --git a/include/nuttx/nx/nxglib.h b/include/nuttx/nx/nxglib.h
index 06208b39f5..583eedf573 100644
--- a/include/nuttx/nx/nxglib.h
+++ b/include/nuttx/nx/nxglib.h
@@ -146,6 +146,14 @@ struct nxgl_rect_s
   struct nxgl_point_s pt2; /* Lower, right-hand corner */
 };
 
+/* Describes a vector starting at pt1 and extending throug pt2 */
+
+struct nxgl_vector_s
+{
+  struct nxgl_point_s pt1; /* Start position */
+  struct nxgl_point_s pt2; /* End position */
+};
+
 /* Describes a run, i.e., a horizontal line.  Note that the start/end positions
  * have fractional precision.  This is necessary for good joining of trapezoids
  * when a more complex shape is decomposed into trapezoids
@@ -579,6 +587,49 @@ EXTERN void nxgl_trapcopy(FAR struct nxgl_trapezoid_s *dest,
 
 EXTERN void nxgl_colorcopy(nxgl_mxpixel_t dest[CONFIG_NX_NPLANES],
                            const nxgl_mxpixel_t src[CONFIG_NX_NPLANES]);
+
+/****************************************************************************
+ * Name: nxgl_splitline
+ *
+ * Description:
+ *   In the general case, a line with width can be represented as a
+ *   parallelogram with a triangle at the top and bottom.  Triangles and
+ *   parallelograms are both degenerate versions of a trapeziod.  This
+ *   function breaks.  This function also detects other degenerate cases:
+ *
+ *   1. If y1 == y2 then the line is horizontal and is better represented
+ *      as a rectangle.
+ *   2. If x1 == x2 then the line is vertical and also better represented
+ *      as a rectangle.
+ *   3. If the width of the line is 1, then there are no triangles at the
+ *      top and bottome (this may also be the case if the width is narrow
+ *      and the line is near vertical)
+ *   4. If the line is oriented is certain angles, it may consist only of
+ *      the upper and lower triangles with no trapezoid inbetween.  In
+ *      this case, 3 trapezoids will be returned, but traps[1] will be
+ *      degenerate.
+ *
+ * Input parameters:
+ *   vector - A pointer to the vector described the line to be drawn.
+ *   traps  - A pointer to a array of trapezoids (size 3).
+ *   rect   - A pointer to a rectangle.
+ *
+ * Returned value:
+ *   0: Line successfully broken up into three trapezoids.  Values in
+ *      traps[0], traps[1], and traps[2] are valid.
+ *   1: Line successfully represented by one trapezoid. Value in traps[1]
+ *      is valid.
+ *   2: Line successfully represented by one rectangle. Value in rect is
+ *      valid
+ *  <0: On errors, a negated errno value is returned.
+ *
+ ****************************************************************************/
+
+EXTERN int nxgl_splitline(FAR struct nxgl_vector_s *vector,
+                          FAR struct nxgl_trapezoid_s *traps,
+                          FAR struct nxgl_rect_s *rect,
+                          nxgl_coord_t linewidth);
+
 #undef EXTERN
 #if defined(__cplusplus)
 }
-- 
GitLab