d0679a780426c735c007c4894904e76b09cadca8
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

1) /* BlinkenLightsInteractiveMovieProgram
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

2)  * version 1.2 date 2005-12-19
Christian Heimke Blimp v.0.6 (2005-03-10)

Christian Heimke authored 13 years ago

3)  * Copyright (C) 2004-2005: Stefan Schuermans <1stein@schuermans.info>
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

4)  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5)  * a blinkenarea.org project
6)  * powered by eventphone.de
7)  */
8) 
9) import java.awt.*;
10) 
11) public class BlinkenFrame
12) {
13) 
14)   private int height;
15)   private int width;
16)   private int channels;
17)   private int maxval;
18)   private int duration;
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

19)   private byte[][] data;
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

20) 
21)   BlinkenFrame( int height, int width, int channels, int maxval, int duration )
22)   {
23)     if( height < 1 ) height = 1;
24)     if( height > 1024 ) height = 1024;
25)     if( width < 1 ) width = 1;
26)     if( width > 1024 ) width = 1024;
27)     if( channels < 1 ) channels = 1;
28)     if( channels > 16 ) channels = 16;
29)     if( maxval < 1 ) maxval = 1;
30)     if( maxval > 255 ) maxval = 255;
31)     if( duration < 1 ) duration = 1;
32)     if( duration > 65535 ) duration = 65535;
33) 
34)     this.height = height;
35)     this.width = width;
36)     this.channels = channels;
37)     this.maxval = maxval;
38)     this.duration = duration;
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

39)     data = new byte[height][width * channels];
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

40)   }
41) 
42)   BlinkenFrame( BlinkenFrame frame )
43)   {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

44)     int y, x, c, i;
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

45)     height = frame.height;
46)     width = frame.width;
47)     channels = frame.channels;
48)     maxval = frame.maxval;
49)     duration = frame.duration;
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

50)     data = new byte[height][width * channels];
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

51)     for( y = 0; y < height; y++ )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

52)       for( x = 0, i = 0; x < width; x++ )
53)         for( c = 0; c < channels; c++, i++ )
54)           data[y][i] = frame.data[y][i];
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

55)   }
56) 
57)   public void clear( )
58)   {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

59)     int x, y, c, i;
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

60)     for( y = 0; y < height; y++ )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

61)       for( x = 0, i = 0; x < width; x++ )
62)         for( c = 0; c < channels; c++, i++ )
63)           data[y][i] = 0;
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

64)   }
65) 
66)   public int getHeight( )
67)   {
68)     return height;
69)   }
70) 
71)   public int getWidth( )
72)   {
73)     return width;
74)   }
75) 
76)   public int getChannels( )
77)   {
78)     return channels;
79)   }
80) 
81)   public int getMaxval( )
82)   {
83)     return maxval;
84)   }
85) 
86)   public int getDuration( )
87)   {
88)     return duration;
89)   }
90) 
91)   public void setDuration( int duration )
92)   {
93)     if( duration < 1 ) duration = 1;
94)     if( duration > 65535 ) duration = 65535;
95)     this.duration = duration;
96)   }
97) 
98)   public byte getPixel( int y, int x, int c )
99)   {
100)     if( y < 0 || y >= height ||
101)         x < 0 || x >= width ||
102) 	c < 0 || c >= channels )
103)       return 0;
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

104)     return data[y][x * channels + c];
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

105)   }
106) 
107)   public void setPixel( int y, int x, int c, byte val )
108)   {
109)     if( y < 0 || y >= height ||
110)         x < 0 || x >= width ||
111) 	c < 0 || c >= channels )
112)       return;
113)     if( val > maxval )
114)       val = (byte)maxval;
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

115)     data[y][x * channels + c] = val;
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

116)   }
117) 
118)   public Color getColor( int y, int x )
119)   {
120)     if( y < 0 || y >= height ||
121)         x < 0 || x >= width ||
122)         channels < 1 )
123)       return new Color( 0, 0, 0 );
124)     if( channels == 1 )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

125)       return new Color( (((int)data[y][x * 1 + 0] & 0xFF) * 255 + maxval / 2) / maxval,
126)                         (((int)data[y][x * 1 + 0] & 0xFF) * 255 + maxval / 2) / maxval,
127)                         (((int)data[y][x * 1 + 0] & 0xFF) * 255 + maxval / 2) / maxval );
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

128)     if( channels == 2 )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

129)       return new Color( (((int)data[y][x * 2 + 0] & 0xFF) * 255 + maxval / 2) / maxval,
130)                         (((int)data[y][x * 2 + 1] & 0xFF) * 255 + maxval / 2) / maxval, 0 );
131)     int i = x * channels;
132)     return new Color( (((int)data[y][i + 0] & 0xFF) * 255 + maxval / 2) / maxval,
133)                       (((int)data[y][i + 1] & 0xFF) * 255 + maxval / 2) / maxval,
134)                       (((int)data[y][i + 2] & 0xFF) * 255 + maxval / 2) / maxval );
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

135)   }
136) 
137)   public void setColor( int y, int x, Color color )
138)   {
139)     int alpha, alpha_, c;
140)     if( y < 0 || y >= height ||
141)         x < 0 || x >= width )
142)       return;
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

143)     int i = x * channels;
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

144)     alpha = color.getAlpha( );
145)     alpha_ = 255 - alpha;
146)     if( channels >= 1 )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

147)       data[y][i + 0] = (byte)(( ((color.getRed( ) * maxval + 127) / 255) * alpha
148)                               + ((int)data[y][i + 0] & 0xFF) * alpha_
149)                               ) / 255);
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

150)     if( channels >= 2 )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

151)       data[y][i + 1] = (byte)(( ((color.getGreen( ) * maxval + 127) / 255) * alpha
152)                               + ((int)data[y][i + 1] & 0xFF) * alpha_
153)                               ) / 255);
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

154)     if( channels >= 3 )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

155)       data[y][i + 2] = (byte)(( ((color.getBlue( ) * maxval + 127) / 255) * alpha
156)                               + ((int)data[y][i + 2] & 0xFF) * alpha_
157)                               ) / 255);
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

158)     for( c = 3; c < channels; c++ )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

159)       data[y][i + c] = (byte)(( 0
160)                               + ((int)data[y][i + c] & 0xFF) * alpha_
161)                               ) / 255);
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

162)   }
163) 
164)   public void resize( int height, int width, int channels, int maxval )
165)   {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

166)     byte[][] data;
167)     int y, x, c, i;
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

168)     int emptyY, emptyX, skipY, skipX, rangeY, rangeX, val, div;
169) 
170)     if( height < 1 ) height = 1;
171)     if( height > 1024 ) height = 1024;
172)     if( width < 1 ) width = 1;
173)     if( width > 1024 ) width = 1024;
174)     if( channels < 1 ) channels = 1;
175)     if( channels > 16 ) channels = 16;
176)     if( maxval < 1 ) maxval = 1;
177)     if( maxval > 255 ) maxval = 255;
178) 
179)     if( height == this.height &&
180)         width == this.width &&
181)         channels == this.channels &&
182)         maxval == this.maxval )
183)       return;
184) 
185)     //allocate new data array
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

186)     data = new byte[height][width * channels];
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

187)     for( y = 0; y < height; y++ )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

188)       for( x = 0, i = 0; x < width; x++ )
189)         for( c = 0; c < channels; c++, i++ )
190)           data[y][i] = 0;
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

191) 
192)     //get number of pixels to skip / to leave empty in X and Y direction
193)     if( height > this.height )
194)     {
195)       emptyY = (height - this.height) / 2; 
196)       skipY = 0;
197)       rangeY = this.height;
198)     }
199)     else
200)     {
201)       emptyY = 0;
202)       skipY = (this.height - height) / 2;
203)       rangeY = height;
204)     }
205)     if( width > this.width )
206)     {
207)       emptyX = (width - this.width) / 2; 
208)       skipX = 0;
209)       rangeX = this.width;
210)     }
211)     else
212)     {
213)       emptyX = 0;
214)       skipX = (this.width - width) / 2;
215)       rangeX = width;
216)     }
217) 
218)     //resize frame with help of calculated parameters
219)     for( y = 0; y < rangeY; y++ )
220)     {
221)       for( x = 0; x < rangeX; x++ )
222)       {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

223)         int srcI = (skipX + x) * this.channels;
224)         int destI = (emptyX + x) * channels;
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

225)         if( channels >= this.channels ) //add channels: copy last channel into new channels
226) 	{
227)           for( c = 0; c < this.channels; c++ )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

228)             data[emptyY + y][destI + c] = (byte)((((int)this.data[skipY + y][srcI + c] & 0xFF) * maxval + this.maxval / 2) / this.maxval);
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

229) 	  for( ; c < channels; c++ )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

230) 	    data[emptyY + y][destI + c] = data[emptyY + y][destI + this.channels - 1];
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

231) 	}
232) 	else //remove channels: merge leftover channels with last kept channel
233) 	{
234)           val = 0;
235)           for( c = 0; c < channels - 1; c++ )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

236)             data[emptyY + y][destI + c] = (byte)((((int)this.data[skipY + y][srcI + c] & 0xFF) * maxval + this.maxval / 2) / this.maxval);
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

237)           for( c = channels - 1; c < this.channels; c++ )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

238)             val += (int)this.data[skipY + y][srcI + c] & 0xFF;
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

239)           div = this.maxval * (this.channels - channels + 1);
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

240)           data[emptyY + y][destI + channels - 1] = (byte)((val * maxval + div / 2) / div);
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

241) 	}
242)       }
243)     }
244) 
245)     this.height = height;
246)     this.width = width;
247)     this.channels = channels;
248)     this.maxval = maxval;
249)     this.data = data;
250)   }
251) 
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

252)   public void scale( int height, int width )
253)   {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

254)     byte[][] data;
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

255)     double scaleHor, scaleVer, ox, oy, ox1, oy1, val;
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

256)     int c, nx, nx_c, ny, x, x_c, y, oxi, oxi_c, oyi, ox1i, ox1i_c, oy1i;
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

257) 
258)     if( height < 1 ) height = 1;
259)     if( height > 1024 ) height = 1024;
260)     if( width < 1 ) width = 1;
261)     if( width > 1024 ) width = 1024;
262) 
263)     if( height == this.height &&
264)         width == this.width )
265)       return;
266) 
267)     scaleHor = (double)width / (double)this.width;
268)     scaleVer = (double)height / (double)this.height;
269) 
270)     //allocate new data array
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

271)     data = new byte[height][width * channels];
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

272) 
273)     //scale every channel
274)     for( c = 0; c < channels; c++ )
275)     {
276)       for( ny = 0; ny < height; ny++ )
277)       {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

278)         for( nx = 0, nx_c = c; nx < width; nx++, nx_c += channels )
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

279)         {
280)           oy = (double)ny / scaleVer; //sub-pixel exact range in old picture
281)           ox = (double)nx / scaleHor;
282)           oy1 = (double)(ny + 1) / scaleVer - 0.000001;
283)           ox1 = (double)(nx + 1) / scaleHor - 0.000001;
284)           if( oy < 0 || ox < 0 || oy1 >= this.height || ox1 >= this.width) //out of old picture
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

285)             data[ny][nx_c] = 0;
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

286)           else
287)           {
288)             oyi = (int)oy;
289)             oxi = (int)ox;
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

290)             oxi_c = oxi * channels + c;
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

291)             oy1i = (int)oy1;
292)             ox1i = (int)ox1;
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

293)             ox1i_c = ox1i * channels + c;
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

294)             if( oyi == oy1i )
295)             {
296)               if( oxi == ox1i) //one source pixel
297)               {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

298)                 val = (double)((int)this.data[oyi][oxi_c] & 0xFF);
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

299)               }
300)               else //one line of source pixels
301)               {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

302)                 val = (double)((int)this.data[oyi][oxi_c] & 0xFF) * (1 - ox + oxi)
303)                     + (double)((int)this.data[oyi][ox1i_c] & 0xFF) * (ox1 - ox1i);
304)                 for( x_c = oxi_c + channels; x_c < ox1i_c; x_c += channels )
305)                   val += (double)((int)this.data[oyi][x_c] & 0xFF);
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

306)                 val /= ox1 - ox;
307)               }
308)             }
309)             else //one column of source pixels
310)             {
311)               if( oxi == ox1i )
312)               {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

313)                 val = (double)((int)this.data[oyi][oxi_c] & 0xFF) * (1 - oy + oyi)
314)                     + (double)((int)this.data[oy1i][oxi_c] & 0xFF) * (oy1 - oy1i);
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

315)                 for( y = oyi + 1; y < oy1i; y++ )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

316)                   val += (double)((int)this.data[y][oxi_c] & 0xFF);
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

317)                 val /= oy1 - oy;
318)               }
319)               else //rectangle of source pixels
320)               {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

321)                 val = (double)((int)this.data[oyi][oxi_c] & 0xFF) * (1 - oy + oyi) * (1 - ox + oxi)
322)                     + (double)((int)this.data[oyi][ox1i_c] & 0xFF) * (1 - oy + oyi) * (ox1 - ox1i)
323)                     + (double)((int)this.data[oy1i][oxi_c] & 0xFF) * (oy1 - oy1i) * (1 - ox + oxi)
324)                     + (double)((int)this.data[oy1i][ox1i_c] & 0xFF) * (oy1 - oy1i) * (ox1 - ox1i);
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

325)                 for( y = oyi + 1; y < oy1i; y++ )
326)                 {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

327)                   val += (double)((int)this.data[y][oxi_c] & 0xFF) * (1 - ox + oxi)
328)                        + (double)((int)this.data[y][ox1i_c] & 0xFF) * (ox1 - ox1i);
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

329)                 }
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

330)                 for( x_c = oxi_c + channels; x_c < ox1i_c; x_c += channels )
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

331)                 {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

332)                   val += (double)((int)this.data[oyi][x_c] & 0xFF) * (1 - oy + oyi)
333)                        + (double)((int)this.data[oy1i][x_c] & 0xFF) * (oy1 - oy1i);
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

334)                 }
335)                 for( y = oyi + 1; y < oy1i; y++ )
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

336)                   for( x_c = oxi_c + channels; x_c < ox1i_c; x_c += channels )
337)                     val += (double)((int)this.data[y][x_c] & 0xFF);
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

338)                 val /= (oy1 - oy) * (ox1 - ox);
339)               }
340)             }
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

341)             data[ny][nx_c] = (byte)(int)(val + 0.5);
Christian Heimke Blimp v.0.3 (2004-11-12)

Christian Heimke authored 13 years ago

342)           }
343)         } //for( nx ...
344)       } //for( ny ...
345)     } //for( c ...
346) 
347)     this.height = height;
348)     this.width = width;
349)     this.data = data;
350)   }
351) 
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

352)   public String toString( )
353)   {
354)     String str = "";
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

355)     int h, w, c, i, val;
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

356)     for( h = 0; h < height; h++ )
357)     {
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

358)       for( w = 0, i = 0; w < width; w++ )
Christian Heimke Blimp v.0.2 (2004-11-10)

Christian Heimke authored 13 years ago

359)       {
360)         val = 0;
Christian Heimke Blimp v.1.2 (2005-12-19)

Christian Heimke authored 13 years ago

361)         for( val = 0, c = 0; c < channels; c++, i++ )
362)           val += ((int)data[h][i] & 0xFF);