152a2bc2b5dbcb2ecf9c746be019efa57fa3f3aa
Stefan Schuermans start of implementation (ha...

Stefan Schuermans authored 13 years ago

1) /* JFlexiPix - Java implementation of FlexiPix output library
2)  *
Stefan Schuermans change email address in fil...

Stefan Schuermans authored 13 years ago

3)  * Copyright 2010-2011 Stefan Schuermans <stefan schuermans info>
Stefan Schuermans start of implementation (ha...

Stefan Schuermans authored 13 years ago

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};
Stefan Schuermans added additional base index...

Stefan Schuermans authored 13 years ago

100)     data(black, 0, 0, 0, 0, 0, m_size.m_x, m_size.m_y);
Stefan Schuermans start of implementation (ha...

Stefan Schuermans authored 13 years ago

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
Stefan Schuermans added additional base index...

Stefan Schuermans authored 13 years ago

109)    * @param[in] base array index of top-left pixel
Stefan Schuermans start of implementation (ha...

Stefan Schuermans authored 13 years ago

110)    * @param[in] x X coordinate of left side of rectangular area
111)    * @param[in] y Y coordinate of top side of rectangular area
112)    * @param[in] width with of rectangular area
113)    * @param[in] height height of rectangular area
114)    */
Stefan Schuermans added additional base index...

Stefan Schuermans authored 13 years ago

115)   public void data(byte [] data, int strideX, int strideY, int base,
Stefan Schuermans start of implementation (ha...

Stefan Schuermans authored 13 years ago

116)                    int x, int y, int width, int height)
117)   {
118)     int dist, out, pix, i, c;
119)     Distri distri;
120)     int dest, src;
121)     Pixel pixel;
122)     int rx, ry;
123) 
124)     // set data for all distributors
125)     for (dist = 0; dist < Constants.distriMaxCnt; ++dist) {
126)       distri = m_distris[dist];
127)       if (distri != null) {
128) 
129)         /* set index to start of RGB data for pixels in message buffer
130)            (header is already set up and stays the same) */
131)         dest = Constants.mcufHdr.length;
132) 
133)         // get RGB data of pixels for every output
134)         for (out = 0, i = 0; out < distri.m_outputCnt; ++out) {
Stefan Schuermans implemented fade example, f...

Stefan Schuermans authored 13 years ago

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

Stefan Schuermans authored 13 years ago

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

Stefan Schuermans authored 13 years ago

137)             if (pixel != null) {
138) 
139)               // get pixel coordinates relative to rectangular area of image
140)               rx = pixel.m_x - x;
141)               ry = pixel.m_y - y;
142)               // check if pixel is within rectangular area of image
143)               if (rx >= 0 && rx < width && ry >= 0 && ry < height) {
144)                 // get pixel data and map it
Stefan Schuermans added additional base index...

Stefan Schuermans authored 13 years ago

145)                 src = rx * strideX + ry * strideY + base;
Stefan Schuermans remaining parts of config p...

Stefan Schuermans authored 13 years ago

146)                 distri.m_msgBuf[dest+0] = distri.m_mapRed.m_table[data[src+0] & 0xFF];
147)                 distri.m_msgBuf[dest+1] = distri.m_mapGreen.m_table[data[src+1] & 0xFF];
148)                 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

149)               }
150) 
151)             } // if pixel
Stefan Schuermans start of implementation (ha...

Stefan Schuermans authored 13 years ago

152)             dest += 3;
153)           } // for pix
154)         } // for out
155) 
156)       } // if distri
157)     } // for distri
158)   }
159) 
160)   /// send image data to distributors
161)   public void send()
162)   {
163)     int dist;
164)     Distri distri;
165) 
166)     // send data to all distributors
167)     for (dist = 0; dist < Constants.distriMaxCnt; ++dist) {
168)       distri = m_distris[dist];
169)       if (distri != null) {
170) 
Stefan Schuermans add support for configuring...

Stefan Schuermans authored 7 years ago

171)         if (distri.m_addr != null) {
172)           try {
173)             // send message as UDP packet
174)             DatagramPacket pack = new DatagramPacket(distri.m_msgBuf,
175)                                                      distri.m_msgBuf.length,
176)                                                      distri.m_addr);
177)             m_sock.send(pack);
178)           } catch (java.io.IOException e) {
179)           }