5813c6e756e1e0461aec050dd825c5b468f03a34
Stefan Schuermans start of implementation (ha...

Stefan Schuermans authored 13 years ago

1) /* JFlexiPix - Java implementation of FlexiPix output library
2)  *
3)  * Copyright 2010-2011 Stefan Schuermans <stefan blinkenarea org>
4)  *
5)  * This program is free software: you can redistribute it and/or modify
6)  * it under the terms of the GNU General Public License as published by
7)  * the Free Software Foundation, version 3 of the License.
8)  *
9)  *
10)  * This program is distributed in the hope that it will be useful,
11)  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12)  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13)  * GNU General Public License for more details.
14)  *
15)  * You should have received a copy of the GNU Lesser General Public License
16)  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17)  */
18) 
19) package org.blinkenarea.JFlexiPix;
20) 
21) import java.net.*;
22) 
23) /// FlexiPix display
24) public class Display
25) {
26)   /**
27)    * @brief constructor
28)    * @param[in] configFile name of config file to read
29)    * @param[in] messageIf message interface to report messages to or null
30)    */
31)   public Display(String configFile, MessageIf messageIf)
32)     throws Exception
33)   {
34)     Config config;
35) 
36)     // set default bind address
37)     try {
38)       InetAddress ip = InetAddress.getByAddress(Constants.bindIp);
39)       m_bindAddr = new InetSocketAddress(ip, Constants.bindPort);
40)     } catch (java.net.UnknownHostException e) {
Stefan Schuermans implemented more parts of c...

Stefan Schuermans authored 13 years ago

41)       String txt = "internal error: bad default bind address";
Stefan Schuermans start of implementation (ha...

Stefan Schuermans authored 13 years ago

42)       if (messageIf != null)
43)         messageIf.message(MsgType.Err, txt + "\n");
44)       throw new Exception(txt, e);
45)     }
46) 
47)     // allocate pixel object to store size
48)     m_size = new Pixel();
49) 
50)     // allocate distributor array
51)     m_distris = new Distri [Constants.distriMaxCnt];
52) 
53)     // no pixels yet
54)     m_distriCnt = 0;
55)     m_outputCnt = 0;
56)     m_pixelCnt  = 0;
57) 
58)     // parse config file
59)     config = new Config(this, messageIf);
60)     try {
61)       config.procFile(configFile);
62)     } catch (Exception e) {
63)       String txt = "reading config file failed\n";
64)       if (messageIf != null)
65)         messageIf.message(MsgType.Err, txt + "\n");
66)       throw new Exception(txt, e);
67)     }
68)     config = null;
69) 
70)     // create socket and bind it
Stefan Schuermans implemented more parts of c...

Stefan Schuermans authored 13 years ago

71)     try {
72)       m_sock = new DatagramSocket(m_bindAddr);
73)     } catch (SocketException e) {
74)       String txt = "creating and binding socket failed\n";
75)       if (messageIf != null)
76)         messageIf.message(MsgType.Err, txt + "\n");
77)       throw new Exception(txt, e);
78)     }
Stefan Schuermans start of implementation (ha...

Stefan Schuermans authored 13 years ago

79) 
80)     // clear display
81)     dataClear();
82)   }
83) 
84)   /// get width of display
85)   public int getWidth()
86)   {
87)     return m_size.m_x;
88)   }
89) 
90)   /// get height of display
91)   public int getHeight()
92)   {
93)     return m_size.m_y;
94)   }
95) 
96)   /// clear image data to output
97)   public void dataClear()
98)   {
99)     byte [] black = {(byte)0, (byte)0, (byte)0};
100)     data(black, 0, 0, 0, 0, m_size.m_x, m_size.m_y);
101)   }
102) 
103)   /**
104)    * @brief set image data to output
105)    * @param[in] data rectangular section of image data,
106)    *                 pixels need to be in R8G8B8 format
107)    * @param[in] strideX stride between two pixels in X direction
108)    * @param[in] strideY stride between two pixels in Y direction
109)    * @param[in] x X coordinate of left side of rectangular area
110)    * @param[in] y Y coordinate of top side of rectangular area
111)    * @param[in] width with of rectangular area
112)    * @param[in] height height of rectangular area
113)    */
114)   public void data(byte [] data, int strideX, int strideY,
115)                    int x, int y, int width, int height)
116)   {
117)     int dist, out, pix, i, c;
118)     Distri distri;
119)     int dest, src;
120)     Pixel pixel;
121)     int rx, ry;
122) 
123)     // set data for all distributors
124)     for (dist = 0; dist < Constants.distriMaxCnt; ++dist) {
125)       distri = m_distris[dist];
126)       if (distri != null) {
127) 
128)         /* set index to start of RGB data for pixels in message buffer
129)            (header is already set up and stays the same) */
130)         dest = Constants.mcufHdr.length;
131) 
132)         // get RGB data of pixels for every output
133)         for (out = 0, i = 0; out < distri.m_outputCnt; ++out) {
Stefan Schuermans implemented fade example, f...

Stefan Schuermans authored 13 years ago

134)           for (pix = 0; pix < distri.m_pixelCnt; ++pix, ++i) {
Stefan Schuermans start of implementation (ha...

Stefan Schuermans authored 13 years ago

135)             pixel = distri.m_pixels[i];
Stefan Schuermans implemented more parts of c...

Stefan Schuermans authored 13 years ago

136)             if (pixel != null) {
137) 
138)               // get pixel coordinates relative to rectangular area of image
139)               rx = pixel.m_x - x;
140)               ry = pixel.m_y - y;
141)               // check if pixel is within rectangular area of image
142)               if (rx >= 0 && rx < width && ry >= 0 && ry < height) {
143)                 // get pixel data and map it
144)                 src = rx * strideX + ry * strideY;
Stefan Schuermans remaining parts of config p...

Stefan Schuermans authored 13 years ago

145)                 distri.m_msgBuf[dest+0] = distri.m_mapRed.m_table[data[src+0] & 0xFF];
146)                 distri.m_msgBuf[dest+1] = distri.m_mapGreen.m_table[data[src+1] & 0xFF];
147)                 distri.m_msgBuf[dest+2] = distri.m_mapBlue.m_table[data[src+2] & 0xFF];
Stefan Schuermans implemented more parts of c...

Stefan Schuermans authored 13 years ago

148)               }
149) 
150)             } // if pixel