5f61548c8f607fbcacd9d0d828272984a5bb6fff
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

1) /* BlinkenLib
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

2)  * version 0.5.2 date 2006-05-10
3)  * Copyright 2004-2006 Stefan Schuermans <1stein@schuermans.info>
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

4)  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5)  * a blinkenarea.org project
6)  */
7) 
8) #include <stdio.h>
9) #include <stdlib.h>
10) #include <string.h>
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

11) #ifdef WIN32
12) #include <winsock2.h>
13) #include <windows.h>
14) typedef WORD uint16_t;
15) typedef DWORD uint32_t;
16) #else
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

17) #include <stdint.h>
18) #include <netinet/in.h>
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

19) #endif
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

20) 
21) #include "BlinkenConstants.h"
22) #include "BlinkenFrame.h"
23) #include "Tools.h"
24) 
25) struct sBlinkenFrame
26) {
27)   int height;
28)   int width;
29)   int channels;
30)   int maxval;
31)   int duration;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

32)   unsigned char * * ppData;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

33) };
34) 
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

35) //blinken protocol headers
36) typedef struct sBlinkenProtoBlpHdr
37) {
38)   uint32_t magic;
39)   uint32_t frameNo;
40)   uint16_t width;
41)   uint16_t height;
42) } stBlinkenProtoBlpHdr;
43) #define BlinkenProtoBlpMagic 0xDEADBEEF
44) typedef struct sBlinkenProtoEblpHdr
45) {
46)   uint32_t magic;
47)   uint32_t frameNo;
48)   uint16_t width;
49)   uint16_t height;
50) } stBlinkenProtoEblpHdr;
51) #define BlinkenProtoEblpMagic 0xFEEDBEEF
52) typedef struct sBlinkenProtoMcufHdr
53) {
54)   uint32_t magic;
55)   uint16_t height;
56)   uint16_t width;
57)   uint16_t channels;
58)   uint16_t maxval;
59) } stBlinkenProtoMcufHdr;
60) #define BlinkenProtoMcufMagic 0x23542666
61) 
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

62) stBlinkenFrame * BlinkenFrameNew( int height, int width, int channels, int maxval, int duration )
63) {
64)   stBlinkenFrame * pFrame;
65) 
66)   if( height < BlinkenHeightMin ) height = BlinkenHeightMin;
67)   if( height > BlinkenHeightMax ) height = BlinkenHeightMax;
68)   if( width < BlinkenWidthMin ) width = BlinkenWidthMin;
69)   if( width > BlinkenWidthMax ) width = BlinkenWidthMax;
70)   if( channels < BlinkenChannelsMin ) channels = BlinkenChannelsMin;
71)   if( channels > BlinkenChannelsMax ) channels = BlinkenMaxvalMax;
72)   if( maxval < BlinkenMaxvalMin ) maxval = BlinkenMaxvalMin;
73)   if( maxval > BlinkenMaxvalMax ) maxval = BlinkenMaxvalMax;
74)   if( duration < BlinkenDurationMin ) duration = BlinkenDurationMin;
75)   if( duration > BlinkenDurationMax ) duration = BlinkenDurationMax;
76) 
77)   pFrame = (stBlinkenFrame *)malloc( sizeof( stBlinkenFrame ) );
78)   if( pFrame == NULL )
79)     return NULL;
80) 
81)   pFrame->height = height;
82)   pFrame->width = width;
83)   pFrame->channels = channels;
84)   pFrame->maxval = maxval;
85)   pFrame->duration = duration;
86) 
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

87)   pFrame->ppData = (unsigned char * *)malloc2D( height, width * channels, sizeof( unsigned char ) );
88)   if( pFrame->ppData == NULL )
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

89)   {
90)     free( pFrame );
91)     return NULL;
92)   }
93) 
94)   return pFrame;
95) }
96) 
97) stBlinkenFrame * BlinkenFrameClone( stBlinkenFrame * pSrcFrame )
98) {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

99)   int y, x, c, i;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

100)   stBlinkenFrame * pFrame;
101) 
102)   if( pSrcFrame == NULL )
103)     return NULL;
104) 
105)   pFrame = BlinkenFrameNew( pSrcFrame->height, pSrcFrame->width,
106)                             pSrcFrame->channels, pSrcFrame->maxval, pSrcFrame->duration );
107)   if( pFrame == NULL )
108)     return NULL;
109) 
110)   for( y = 0; y < pFrame->height; y++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

111)     for( x = 0, i = 0; x < pFrame->width; x++ )
112)       for( c = 0; c < pFrame->channels; c++, i++ )
113)         pFrame->ppData[y][i] = pSrcFrame->ppData[y][i];
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

114) 
115)   return pFrame;
116) }
117) 
118) void BlinkenFrameFree( stBlinkenFrame * pFrame )
119) {
120)   if( pFrame == NULL )
121)     return;
122) 
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

123)   free( pFrame->ppData );
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

124)   free( pFrame );
125) }
126) 
127) void BlinkenFrameClear( stBlinkenFrame * pFrame )
128) {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

129)   int y, x, c, i;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

130) 
131)   if( pFrame == NULL )
132)     return;
133) 
134)   for( y = 0; y < pFrame->height; y++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

135)     for( x = 0, i = 0; x < pFrame->width; x++ )
136)       for( c = 0; c < pFrame->channels; c++, i++ )
137)         pFrame->ppData[y][i] = 0;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

138) }
139) 
140) int BlinkenFrameGetHeight( stBlinkenFrame * pFrame )
141) {
142)   if( pFrame == NULL )
143)     return 0;
144) 
145)   return pFrame->height;
146) }
147) 
148) int BlinkenFrameGetWidth( stBlinkenFrame * pFrame )
149) {
150)   if( pFrame == NULL )
151)     return 0;
152) 
153)   return pFrame->width;
154) }
155) 
156) int BlinkenFrameGetChannels( stBlinkenFrame * pFrame )
157) {
158)   if( pFrame == NULL )
159)     return 0;
160) 
161)   return pFrame->channels;
162) }
163) 
164) int BlinkenFrameGetMaxval( stBlinkenFrame * pFrame )
165) {
166)   if( pFrame == NULL )
167)     return 0;
168) 
169)   return pFrame->maxval;
170) }
171) 
172) int BlinkenFrameGetDuration( stBlinkenFrame * pFrame )
173) {
174)   if( pFrame == NULL )
175)     return 0;
176) 
177)   return pFrame->duration;
178) }
179) 
180) void BlinkenFrameSetDuration( stBlinkenFrame * pFrame, int duration )
181) {
182)   if( pFrame == NULL )
183)     return;
184) 
185)   if( duration < BlinkenDurationMin ) duration = BlinkenDurationMin;
186)   if( duration > BlinkenDurationMax ) duration = BlinkenDurationMax;
187) 
188)   pFrame->duration = duration;
189) }
190) 
191) unsigned char BlinkenFrameGetPixel( stBlinkenFrame * pFrame, int y, int x, int c )
192) {
193)   if( pFrame == NULL ||
194)       y < 0 || y >= pFrame->height ||
195)       x < 0 || x >= pFrame->width ||
196)       c < 0 || c >= pFrame->channels )
197)       return 0;
198) 
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

199)   return pFrame->ppData[y][x * pFrame->channels + c];
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

200) }
201) 
202) void BlinkenFrameSetPixel( stBlinkenFrame * pFrame, int y, int x, int c, unsigned char val )
203) {
204)   if( pFrame == NULL ||
205)       y < 0 || y >= pFrame->height ||
206)       x < 0 || x >= pFrame->width ||
207)       c < 0 || c >= pFrame->channels )
208)       return;
209) 
210)   if( val > pFrame->maxval )
211)     val = pFrame->maxval;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

212)   pFrame->ppData[y][x * pFrame->channels + c] = val;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

213) }
214) 
215) unsigned long BlinkenFrameGetColor( stBlinkenFrame * pFrame, int y, int x )
216) {
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

217)   int i;
218) 
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

219)   if( pFrame == NULL ||
220)       y < 0 || y >= pFrame->height ||
221)       x < 0 || x >= pFrame->width )
222)       return 0;
223) 
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

224)   i = x * pFrame->channels;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

225) 
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

226)   if( pFrame->channels == 1 )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

227)     return (((unsigned long)pFrame->ppData[y][i + 0] * 255 + pFrame->maxval / 2) / pFrame->maxval) << 16 |
228)            (((unsigned long)pFrame->ppData[y][i + 0] * 255 + pFrame->maxval / 2) / pFrame->maxval) << 8 |
229)            (((unsigned long)pFrame->ppData[y][i + 0] * 255 + pFrame->maxval / 2) / pFrame->maxval);
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

230)   if( pFrame->channels == 2 )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

231)     return (((unsigned long)pFrame->ppData[y][i + 0] * 255 + pFrame->maxval / 2) / pFrame->maxval) << 16 |
232)            (((unsigned long)pFrame->ppData[y][i + 1] * 255 + pFrame->maxval / 2) / pFrame->maxval) << 8;
233)   return (((unsigned long)pFrame->ppData[y][i + 0] * 255 + pFrame->maxval / 2) / pFrame->maxval) << 16 |
234)          (((unsigned long)pFrame->ppData[y][i + 1] * 255 + pFrame->maxval / 2) / pFrame->maxval) << 8 |
235)          (((unsigned long)pFrame->ppData[y][i + 2] * 255 + pFrame->maxval / 2) / pFrame->maxval);
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

236) }
237) 
238) void BlinkenFrameSetColor( stBlinkenFrame * pFrame, int y, int x, unsigned long color )
239) {
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

240)   int i, alpha, alpha_, c;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

241) 
242)   if( pFrame == NULL ||
243)       y < 0 || y >= pFrame->height ||
244)       x < 0 || x >= pFrame->width )
245)       return;
246) 
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

247)   i = x * pFrame->channels;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

248) 
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

249)   alpha = (color >> 24) & 0xFF;
250)   alpha_ = 255 - alpha;
251)   if( pFrame->channels >= 1 )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

252)     pFrame->ppData[y][i + 0] = (unsigned char)(( ((((color >> 16) & 0xFF) * pFrame->maxval + 127) / 255) * alpha
253)                                                + (unsigned long)pFrame->ppData[y][i + 0] * alpha_
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

254)                                                ) / 255);
255)   if( pFrame->channels >= 2 )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

256)     pFrame->ppData[y][i + 1] = (unsigned char)(( ((((color >> 8) & 0xFF) * pFrame->maxval + 127) / 255) * alpha
257)                                                + (unsigned long)pFrame->ppData[y][i + 1] * alpha_
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

258)                                                ) / 255);
259)   if( pFrame->channels >= 3 )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

260)     pFrame->ppData[y][i + 2] = (unsigned char)(( (((color & 0xFF) * pFrame->maxval + 127) / 255) * alpha
261)                                                + (unsigned long)pFrame->ppData[y][i + 2] * alpha_
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

262)                                                ) / 255);
263)   for( c = 3; c < pFrame->channels; c++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

264)     pFrame->ppData[y][i + c] = (unsigned char)(( 0
265)                                                + (unsigned long)pFrame->ppData[y][i + c] * alpha_
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

266)                                                ) / 255);
267) }
268) 
269) void BlinkenFrameResize( stBlinkenFrame * pFrame, int height, int width, int channels, int maxval )
270) {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

271)   unsigned char * * ppData;
272)   int y, x, c, i, j;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

273)   int emptyY, emptyX, skipY, skipX, rangeY, rangeX;
274)   unsigned long val, div;
275) 
276)   if( pFrame == NULL )
277)     return;
278) 
279)   if( height < BlinkenHeightMin ) height = BlinkenHeightMin;
280)   if( height > BlinkenHeightMax ) height = BlinkenHeightMax;
281)   if( width < BlinkenWidthMin ) width = BlinkenWidthMin;
282)   if( width > BlinkenWidthMax ) width = BlinkenWidthMax;
283)   if( channels < BlinkenChannelsMin ) channels = BlinkenChannelsMin;
284)   if( channels > BlinkenChannelsMax ) channels = BlinkenMaxvalMax;
285)   if( maxval < BlinkenMaxvalMin ) maxval = BlinkenMaxvalMin;
286)   if( maxval > BlinkenMaxvalMax ) maxval = BlinkenMaxvalMax;
287) 
288)   if( height == pFrame->height &&
289)       width == pFrame->width &&
290)       channels == pFrame->channels &&
291)       maxval == pFrame->maxval )
292)     return;
293) 
294)   //allocate new data array
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

295)   ppData = (unsigned char * *)malloc2D( height, width * channels, sizeof( unsigned char ) );
296)   if( ppData == NULL )
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

297)     return;
298)   for( y = 0; y < height; y++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

299)     for( x = 0, i = 0; x < width; x++ )
300)       for( c = 0; c < channels; c++, i++ )
301)         ppData[y][i] = 0;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

302) 
303)   //get number of pixels to skip / to leave empty in X and Y direction
304)   if( height > pFrame->height )
305)   {
306)     emptyY = (height - pFrame->height) / 2; 
307)     skipY = 0;
308)     rangeY = pFrame->height;
309)   }
310)   else
311)   {
312)     emptyY = 0;
313)     skipY = (pFrame->height - height) / 2;
314)     rangeY = height;
315)   }
316)   if( width > pFrame->width )
317)   {
318)     emptyX = (width - pFrame->width) / 2; 
319)     skipX = 0;
320)     rangeX = pFrame->width;
321)   }
322)   else
323)   {
324)     emptyX = 0;
325)     skipX = (pFrame->width - width) / 2;
326)     rangeX = width;
327)   }
328) 
329)   //resize frame with help of calculated parameters
330)   for( y = 0; y < rangeY; y++ )
331)   {
332)     for( x = 0; x < rangeX; x++ )
333)     {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

334)       i = (skipX + x) * pFrame->channels;
335)       j = (emptyX + x) * channels;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

336)       if( channels >= pFrame->channels ) //add channels: copy last channel into new channels
337)       {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

338)         for( c = 0; c < pFrame->channels; c++, i++, j++ )
339)           ppData[emptyY + y][j] = (unsigned char)(((unsigned long)pFrame->ppData[skipY + y][i] * maxval + pFrame->maxval / 2) / pFrame->maxval);
340)         for( ; c < channels; c++, j++ )
341)           ppData[emptyY + y][j] = ppData[emptyY + y][j - 1];
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

342)       }
343)       else //remove channels: merge leftover channels with last kept channel
344)       {
345)         val = 0;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

346)         for( c = 0; c < channels - 1; c++, i++, j++ )
347)           ppData[emptyY + y][j] = (unsigned char)(((unsigned long)pFrame->ppData[skipY + y][i] * maxval + pFrame->maxval / 2) / pFrame->maxval);
348)         for( c = channels - 1; c < pFrame->channels; c++, i++ )
349)           val += (unsigned long)pFrame->ppData[skipY + y][i];
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

350)         div = pFrame->maxval * (pFrame->channels - channels + 1);
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

351)         ppData[emptyY + y][j++] = (unsigned char)((val * maxval + div / 2) / div);
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

352)       }
353)     }
354)   }
355) 
356)   pFrame->height = height;
357)   pFrame->width = width;
358)   pFrame->channels = channels;
359)   pFrame->maxval = maxval;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

360)   free( pFrame->ppData );
361)   pFrame->ppData = ppData;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

362) }
363) 
364) void BlinkenFrameScale( stBlinkenFrame * pFrame, int height, int width )
365) {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

366)   unsigned char * * ppData;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

367)   double scaleHor, scaleVer, ox, oy, ox1, oy1, val;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

368)   int chans, c, nx, ny, x, y, oxi, oyi, ox1i, oy1i;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

369) 
370)   if( pFrame == NULL )
371)     return;
372) 
373)   if( height < BlinkenHeightMin ) height = BlinkenHeightMin;
374)   if( height > BlinkenHeightMax ) height = BlinkenHeightMax;
375)   if( width < BlinkenWidthMin ) width = BlinkenWidthMin;
376)   if( width > BlinkenWidthMax ) width = BlinkenWidthMax;
377) 
378)   if( height == pFrame->height &&
379)       width == pFrame->width )
380)     return;
381) 
382)   scaleHor = (double)width / (double)pFrame->width;
383)   scaleVer = (double)height / (double)pFrame->height;
384) 
385)   //allocate new data array
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

386)   ppData = (unsigned char * *)malloc2D( height, width * pFrame->channels, sizeof( unsigned char ) );
387)   if( ppData == NULL )
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

388)     return;
389) 
390)   //scale every channel
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

391)   chans = pFrame->channels;
392)   for( c = 0; c < chans; c++ )
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

393)   {
394)     for( ny = 0; ny < height; ny++ )
395)     {
396)       for( nx = 0; nx < width; nx++ )
397)       {
398)         oy = (double)ny / scaleVer; //sub-pixel exact range in old picture
399)         ox = (double)nx / scaleHor;
400)         oy1 = (double)(ny + 1) / scaleVer - 0.000001;
401)         ox1 = (double)(nx + 1) / scaleHor - 0.000001;
402)         if( oy < 0 || ox < 0 || oy1 >= pFrame->height || ox1 >= pFrame->width) //out of old picture
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

403)           ppData[ny][nx * chans + c] = 0;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

404)         else
405)         {
406)           oyi = (int)oy;
407)           oxi = (int)ox;
408)           oy1i = (int)oy1;
409)           ox1i = (int)ox1;
410)           if( oyi == oy1i )
411)           {
412)             if( oxi == ox1i) //one source pixel
413)             {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

414)               val = (double)pFrame->ppData[oyi][oxi * chans + c];
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

415)             }
416)             else //one line of source pixels
417)             {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

418)               val = (double)pFrame->ppData[oyi][oxi * chans + c] * (1 - ox + oxi)
419)                   + (double)pFrame->ppData[oyi][ox1i * chans + c] * (ox1 - ox1i);
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

420)               for( x = oxi + 1; x < ox1i; x++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

421)                 val += (double)pFrame->ppData[oyi][x * chans + c];
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

422)               val /= ox1 - ox;
423)             }
424)           }
425)           else //one column of source pixels
426)           {
427)             if( oxi == ox1i )
428)             {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

429)               val = (double)pFrame->ppData[oyi][oxi * chans + c] * (1 - oy + oyi)
430)                   + (double)pFrame->ppData[oy1i][oxi * chans + c] * (oy1 - oy1i);
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

431)               for( y = oyi + 1; y < oy1i; y++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

432)                 val += (double)pFrame->ppData[y][oxi * chans + c];
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

433)               val /= oy1 - oy;
434)             }
435)             else //rectangle of source pixels
436)             {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

437)               val = (double)pFrame->ppData[oyi][oxi* chans + c] * (1 - oy + oyi) * (1 - ox + oxi)
438)                   + (double)pFrame->ppData[oyi][ox1i * chans + c] * (1 - oy + oyi) * (ox1 - ox1i)
439)                   + (double)pFrame->ppData[oy1i][oxi * chans + c] * (oy1 - oy1i) * (1 - ox + oxi)
440)                   + (double)pFrame->ppData[oy1i][ox1i * chans + c] * (oy1 - oy1i) * (ox1 - ox1i);
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

441)               for( y = oyi + 1; y < oy1i; y++ )
442)               {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

443)                 val += (double)pFrame->ppData[y][oxi * chans + c] * (1 - ox + oxi)
444)                      + (double)pFrame->ppData[y][ox1i * chans + c] * (ox1 - ox1i);
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

445)               }
446)               for( x = oxi + 1; x < ox1i; x++ )
447)               {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

448)                 val += (double)pFrame->ppData[oyi][x * chans + c] * (1 - oy + oyi)
449)                      + (double)pFrame->ppData[oy1i][x * chans + c] * (oy1 - oy1i);
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

450)               }
451)               for( y = oyi + 1; y < oy1i; y++ )
452)                 for( x = oxi + 1; x < ox1i; x++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

453)                   val += (double)pFrame->ppData[y][x * chans + c];
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

454)               val /= (oy1 - oy) * (ox1 - ox);
455)             }
456)           }
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

457)           ppData[ny][nx * chans + c] = (unsigned char)(val + 0.5);
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

458)         }
459)       } //for( nx ...
460)     } //for( ny ...
461)   } //for( c ...
462) 
463)   pFrame->height = height;
464)   pFrame->width = width;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

465)   free( pFrame->ppData );
466)   pFrame->ppData = ppData;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

467) }
468) 
469) char * BlinkenFrameToString( stBlinkenFrame * pFrame )
470) {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

471)   int size, y, x, c, i;
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

472)   char * str, * ptr;
473)   unsigned long val;
474) 
475)   if( pFrame == NULL )
476)     return NULL;
477) 
478)   size = pFrame->height * (pFrame->width + 1) + 32;
479) 
480)   str = (char *)malloc( size );
481)   if( str == NULL )
482)     return NULL;
483) 
484)   ptr = str;
485)   
486)   for( y = 0; y < pFrame->height; y++ )
487)   {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

488)     for( x = 0, i = 0; x < pFrame->width; x++ )
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

489)     {
490)       val = 0;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

491)       for( val = 0, c = 0; c < pFrame->channels; c++, i++ )
492)         val += pFrame->ppData[y][i];
Christian Heimke BlinkenLib v.0.1.1 (2005-01...

Christian Heimke authored 13 years ago

493)       val = val * 7 / pFrame->maxval / pFrame->channels;
494)       *ptr = " -+*%#&@"[val];
495)       ptr++;
496)     }
497)     *ptr = '\n';
498)     ptr++;
499)   }
500) 
501)   sprintf( ptr, "%u ms\n", pFrame->duration );
502) 
503)   return str;
504) }
505) 
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

506) int BlinkenFrameToNetwork( stBlinkenFrame * pFrame, etBlinkenProto proto, char * pData, int maxLength )
507) //returns length or -1 on error
508) {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

509)   int y, x, c, i, j, val;
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

510) 
511)   if( pFrame == NULL )
512)     return -1;
513) 
514)   switch( proto )
515)   {
516) 
517)     case BlinkenProtoNone:
518)       return 0;
519) 
520)     case BlinkenProtoBlp:
521)       if( maxLength < (int)sizeof( stBlinkenProtoBlpHdr ) + pFrame->height * pFrame->width ) //buffer too short
522)         return -1;
523)       ((stBlinkenProtoBlpHdr *)pData)->magic = htonl( BlinkenProtoBlpMagic ); //build header
524)       ((stBlinkenProtoBlpHdr *)pData)->frameNo = htonl( 0 );
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

525)       ((stBlinkenProtoBlpHdr *)pData)->width = htons( (uint16_t)pFrame->width );
526)       ((stBlinkenProtoBlpHdr *)pData)->height = htons( (uint16_t)pFrame->height );
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

527)       i = sizeof( stBlinkenProtoBlpHdr ); //put data into packet
528)       for( y = 0; y < pFrame->height; y++ )
529)       {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

530)         for( x = 0, j = 0; x < pFrame->width; x++, i++ )
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

531)         {
532)            val = 0;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

533)            for( c = 0; c < pFrame->channels; c++, j++ )
534)              val += pFrame->ppData[y][j];
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

535)            pData[i] = (val >= pFrame->channels * pFrame->maxval / 2 ? 0x01 : 0x00);
536)         }
537)       }
538)       return i; //return length
539) 
540)     case BlinkenProtoEblp:
541)       if( maxLength < (int)sizeof( stBlinkenProtoEblpHdr ) + pFrame->height * pFrame->width ) //buffer too short
542)         return -1;
543)       ((stBlinkenProtoEblpHdr *)pData)->magic = htonl( BlinkenProtoEblpMagic ); //build header
544)       ((stBlinkenProtoEblpHdr *)pData)->frameNo = htonl( 0 );
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

545)       ((stBlinkenProtoEblpHdr *)pData)->width = htons( (uint16_t)pFrame->width );
546)       ((stBlinkenProtoEblpHdr *)pData)->height = htons( (uint16_t)pFrame->height );
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

547)       i = sizeof( stBlinkenProtoEblpHdr ); //put data into packet
548)       for( y = 0; y < pFrame->height; y++ )
549)       {
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

550)         for( x = 0, j = 0; x < pFrame->width; x++, i++ )
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

551)         {
552)            val = 0;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

553)            for( c = 0; c < pFrame->channels; c++, j++ )
554)              val += pFrame->ppData[y][j];
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

555)            val /= pFrame->channels;
556)            pData[i] = (pFrame->maxval == 255 ? (unsigned char)val
557)                                              : (unsigned char)((val * 255 + pFrame->maxval / 2) / pFrame->maxval));
558)         }
559)       }
560)       return i; //return length
561) 
562)     case BlinkenProtoMcuf:
563)       if( maxLength < (int)sizeof( stBlinkenProtoMcufHdr ) + pFrame->height * pFrame->width * pFrame->channels ) //buffer too short
564)         return -1;
565)       ((stBlinkenProtoMcufHdr *)pData)->magic = htonl( BlinkenProtoMcufMagic ); //build header
Christian Heimke BlinkenLib v.0.5.2 (2006-05...

Christian Heimke authored 13 years ago

566)       ((stBlinkenProtoMcufHdr *)pData)->height = htons( (uint16_t)pFrame->height );
567)       ((stBlinkenProtoMcufHdr *)pData)->width = htons( (uint16_t)pFrame->width );
568)       ((stBlinkenProtoMcufHdr *)pData)->channels = htons( (uint16_t)pFrame->channels );
569)       ((stBlinkenProtoMcufHdr *)pData)->maxval = htons( (uint16_t)pFrame->maxval );
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

570)       i = sizeof( stBlinkenProtoMcufHdr ); //put data into packet
571)       for( y = 0; y < pFrame->height; y++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

572)         for( x = 0, j = 0; x < pFrame->width; x++ )
573)            for( c = 0; c < pFrame->channels; c++, i++, j++ )
574)              pData[i] = pFrame->ppData[y][j];
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

575)       return i; //return length
576) 
577)     default:
578)       return -1;
579) 
580)   }
581) }
582) 
583) stBlinkenFrame * BlinkenFrameFromNetwork( char * pData, int length, etBlinkenProto * pProto )
584) //returns protocol in *pProto if pProto not NULL
585) {
586)   stBlinkenFrame * pFrame;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

587)   int height, width, channels, maxval, y, x, c, i, j;
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

588) 
589)   if( length >= (int)sizeof( stBlinkenProtoBlpHdr ) &&
590)       ((stBlinkenProtoBlpHdr *)pData)->magic == htonl( BlinkenProtoBlpMagic ) )
591)   {
592)     if( pProto != NULL ) //return protocol
593)       *pProto = BlinkenProtoBlp;
594)     height = ntohs( ((stBlinkenProtoBlpHdr *)pData)->height ); //get header data
595)     width = ntohs( ((stBlinkenProtoBlpHdr *)pData)->width );
596)     if( length < (int)sizeof( stBlinkenProtoBlpHdr ) + height * width ) //check length of packet
597)       return NULL;
598)     if( height < BlinkenHeightMin || height > BlinkenHeightMax || //check header data
599)         width < BlinkenWidthMin || width > BlinkenWidthMax )
600)       return NULL;
601)     pFrame = BlinkenFrameNew( height, width, 1, 1, 0 ); //create frame according to header data
602)     if( pFrame == NULL )
603)       return NULL;
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

604)     i = sizeof( stBlinkenProtoBlpHdr ); //put data into frame
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

605)     for( y = 0; y < pFrame->height; y++ )
606)       for( x = 0; x < pFrame->width; x++, i++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

607)         pFrame->ppData[y][x * 1 + 0] = pData[i];
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

608)     return pFrame;
609)   }
610) 
611)   if( length >= (int)sizeof( stBlinkenProtoEblpHdr ) &&
612)       ((stBlinkenProtoEblpHdr *)pData)->magic == htonl( BlinkenProtoEblpMagic ) )
613)   {
614)     if( pProto != NULL ) //return protocol
615)       *pProto = BlinkenProtoEblp;
616)     height = ntohs( ((stBlinkenProtoEblpHdr *)pData)->height ); //get header data
617)     width = ntohs( ((stBlinkenProtoEblpHdr *)pData)->width );
618)     if( length < (int)sizeof( stBlinkenProtoEblpHdr ) + width * height ) //check length of packet
619)       return NULL;
620)     if( height < BlinkenHeightMin || height > BlinkenHeightMax || //check header data
621)         width < BlinkenWidthMin || width > BlinkenWidthMax )
622)       return NULL;
623)     pFrame = BlinkenFrameNew( height, width, 1, 255, 0 ); //create frame according to header data
624)     if( pFrame == NULL )
625)       return NULL;
626)     i = sizeof( stBlinkenProtoEblpHdr ); //put data into frame
627)     for( y = 0; y < pFrame->height; y++ )
628)       for( x = 0; x < pFrame->width; x++, i++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

629)         pFrame->ppData[y][x * 1 + 0] = pData[i];
Christian Heimke BlinkenLib v.0.3 (2005-02-16)

Christian Heimke authored 13 years ago

630)     return pFrame;
631)   }
632) 
633)   if( length >= (int)sizeof( stBlinkenProtoMcufHdr ) &&
634)       ((stBlinkenProtoMcufHdr *)pData)->magic == htonl( BlinkenProtoMcufMagic ) )
635)   {
636)     if( pProto != NULL ) //return protocol
637)       *pProto = BlinkenProtoMcuf;
638)     height = ntohs( ((stBlinkenProtoMcufHdr *)pData)->height ); //get header data
639)     width = ntohs( ((stBlinkenProtoMcufHdr *)pData)->width );
640)     channels = ntohs( ((stBlinkenProtoMcufHdr *)pData)->channels );
641)     maxval = ntohs( ((stBlinkenProtoMcufHdr *)pData)->maxval );
642)     if( length < (int)sizeof( stBlinkenProtoMcufHdr ) + height * width * channels ) //check length of packet
643)       return NULL;
644)     if( height < BlinkenHeightMin || height > BlinkenHeightMax || //check header data
645)         width < BlinkenWidthMin || width > BlinkenWidthMax ||
646)         channels < BlinkenChannelsMin || channels > BlinkenChannelsMax ||
647)         maxval < BlinkenMaxvalMin || maxval > BlinkenMaxvalMax )
648)       return NULL;
649)     pFrame = BlinkenFrameNew( height, width, channels, maxval, 0 ); //create frame according to header data
650)     if( pFrame == NULL )
651)       return NULL;
652)     i = sizeof( stBlinkenProtoMcufHdr ); //put data into frame
653)     for( y = 0; y < pFrame->height; y++ )
Christian Heimke BlinkenLib v.0.4 (2005-07-02)

Christian Heimke authored 13 years ago

654)       for( x = 0, j = 0; x < pFrame->width; x++ )
655)         for( c = 0; c < pFrame->channels; c++, i++, j++ )
656)           pFrame->ppData[y][j] = pData[i];