BlinkenLib v.0.3 (2005-02-16)
Christian Heimke

Christian Heimke commited on 2011-07-15 09:01:49
Showing 11 changed files, with 386 additions and 259 deletions.

... ...
@@ -1,5 +1,5 @@
1 1
 /* BlinkenLib
2
- * version 0.2 date 2005-01-27
2
+ * version 0.3 date 2005-02-16
3 3
  * Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>
4 4
  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5 5
  * a blinkenarea.org project
... ...
@@ -1,5 +1,5 @@
1 1
 /* BlinkenLib
2
- * version 0.2 date 2005-01-27
2
+ * version 0.3 date 2005-02-16
3 3
  * Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>
4 4
  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5 5
  * a blinkenarea.org project
... ...
@@ -9,7 +9,7 @@
9 9
 #include <stdio.h>
10 10
 #include <stdlib.h>
11 11
 
12
-#include "BlinkenMovie.h"
12
+#include "BlinkenLib.h"
13 13
 
14 14
 int main( int argCnt, char * * args )
15 15
 {
... ...
@@ -20,7 +20,7 @@ int main( int argCnt, char * * args )
20 20
 
21 21
   //print info
22 22
   printf( "BlinkenLib - BlinkenConv\n"
23
-          "version 0.2 date 2005-01-27\n"
23
+          "version 0.3 date 2005-02-16\n"
24 24
           "Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>\n"
25 25
           "Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html\n"
26 26
           "a blinkenarea.org project\n"
... ...
@@ -1,5 +1,5 @@
1 1
 /* BlinkenLib
2
- * version 0.2 date 2005-01-27
2
+ * version 0.3 date 2005-02-16
3 3
  * Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>
4 4
  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5 5
  * a blinkenarea.org project
... ...
@@ -9,6 +9,8 @@
9 9
 #include <stdio.h>
10 10
 #include <stdlib.h>
11 11
 #include <string.h>
12
+#include <stdint.h>
13
+#include <netinet/in.h>
12 14
 
13 15
 #include "BlinkenConstants.h"
14 16
 #include "BlinkenFrame.h"
... ...
@@ -24,6 +26,33 @@ struct sBlinkenFrame
24 26
   unsigned char * * * pppData;
25 27
 };
26 28
 
29
+//blinken protocol headers
30
+typedef struct sBlinkenProtoBlpHdr
31
+{
32
+  uint32_t magic;
33
+  uint32_t frameNo;
34
+  uint16_t width;
35
+  uint16_t height;
36
+} stBlinkenProtoBlpHdr;
37
+#define BlinkenProtoBlpMagic 0xDEADBEEF
38
+typedef struct sBlinkenProtoEblpHdr
39
+{
40
+  uint32_t magic;
41
+  uint32_t frameNo;
42
+  uint16_t width;
43
+  uint16_t height;
44
+} stBlinkenProtoEblpHdr;
45
+#define BlinkenProtoEblpMagic 0xFEEDBEEF
46
+typedef struct sBlinkenProtoMcufHdr
47
+{
48
+  uint32_t magic;
49
+  uint16_t height;
50
+  uint16_t width;
51
+  uint16_t channels;
52
+  uint16_t maxval;
53
+} stBlinkenProtoMcufHdr;
54
+#define BlinkenProtoMcufMagic 0x23542666
55
+
27 56
 stBlinkenFrame * BlinkenFrameNew( int height, int width, int channels, int maxval, int duration )
28 57
 {
29 58
   stBlinkenFrame * pFrame;
... ...
@@ -459,3 +488,162 @@ char * BlinkenFrameToString( stBlinkenFrame * pFrame )
459 488
   return str;
460 489
 }
461 490
 
491
+int BlinkenFrameToNetwork( stBlinkenFrame * pFrame, etBlinkenProto proto, char * pData, int maxLength )
492
+//returns length or -1 on error
493
+{
494
+  int y, x, c, i, val;
495
+
496
+  if( pFrame == NULL )
497
+    return -1;
498
+
499
+  switch( proto )
500
+  {
501
+
502
+    case BlinkenProtoNone:
503
+      return 0;
504
+
505
+    case BlinkenProtoBlp:
506
+      if( maxLength < (int)sizeof( stBlinkenProtoBlpHdr ) + pFrame->height * pFrame->width ) //buffer too short
507
+        return -1;
508
+      ((stBlinkenProtoBlpHdr *)pData)->magic = htonl( BlinkenProtoBlpMagic ); //build header
509
+      ((stBlinkenProtoBlpHdr *)pData)->frameNo = htonl( 0 );
510
+      ((stBlinkenProtoBlpHdr *)pData)->width = htons( pFrame->width );
511
+      ((stBlinkenProtoBlpHdr *)pData)->height = htons( pFrame->height );
512
+      i = sizeof( stBlinkenProtoBlpHdr ); //put data into packet
513
+      for( y = 0; y < pFrame->height; y++ )
514
+      {
515
+        for( x = 0; x < pFrame->width; x++, i++ )
516
+        {
517
+           val = 0;
518
+           for( c = 0; c < pFrame->channels; c++ )
519
+             val += pFrame->pppData[y][x][c];
520
+           pData[i] = (val >= pFrame->channels * pFrame->maxval / 2 ? 0x01 : 0x00);
521
+        }
522
+      }
523
+      return i; //return length
524
+
525
+    case BlinkenProtoEblp:
526
+      if( maxLength < (int)sizeof( stBlinkenProtoEblpHdr ) + pFrame->height * pFrame->width ) //buffer too short
527
+        return -1;
528
+      ((stBlinkenProtoEblpHdr *)pData)->magic = htonl( BlinkenProtoEblpMagic ); //build header
529
+      ((stBlinkenProtoEblpHdr *)pData)->frameNo = htonl( 0 );
530
+      ((stBlinkenProtoEblpHdr *)pData)->width = htons( pFrame->width );
531
+      ((stBlinkenProtoEblpHdr *)pData)->height = htons( pFrame->height );
532
+      i = sizeof( stBlinkenProtoEblpHdr ); //put data into packet
533
+      for( y = 0; y < pFrame->height; y++ )
534
+      {
535
+        for( x = 0; x < pFrame->width; x++, i++ )
536
+        {
537
+           val = 0;
538
+           for( c = 0; c < pFrame->channels; c++ )
539
+             val += pFrame->pppData[y][x][c];
540
+           val /= pFrame->channels;
541
+           pData[i] = (pFrame->maxval == 255 ? (unsigned char)val
542
+                                             : (unsigned char)((val * 255 + pFrame->maxval / 2) / pFrame->maxval));
543
+        }
544
+      }
545
+      return i; //return length
546
+
547
+    case BlinkenProtoMcuf:
548
+      if( maxLength < (int)sizeof( stBlinkenProtoMcufHdr ) + pFrame->height * pFrame->width * pFrame->channels ) //buffer too short
549
+        return -1;
550
+      ((stBlinkenProtoMcufHdr *)pData)->magic = htonl( BlinkenProtoMcufMagic ); //build header
551
+      ((stBlinkenProtoMcufHdr *)pData)->height = htons( pFrame->height );
552
+      ((stBlinkenProtoMcufHdr *)pData)->width = htons( pFrame->width );
553
+      ((stBlinkenProtoMcufHdr *)pData)->channels = htons( pFrame->channels );
554
+      ((stBlinkenProtoMcufHdr *)pData)->maxval = htons( pFrame->maxval );
555
+      i = sizeof( stBlinkenProtoMcufHdr ); //put data into packet
556
+      for( y = 0; y < pFrame->height; y++ )
557
+        for( x = 0; x < pFrame->width; x++ )
558
+           for( c = 0; c < pFrame->channels; c++, i++ )
559
+             pData[i] = pFrame->pppData[y][x][c];
560
+      return i; //return length
561
+
562
+    default:
563
+      return -1;
564
+
565
+  }
566
+}
567
+
568
+stBlinkenFrame * BlinkenFrameFromNetwork( char * pData, int length, etBlinkenProto * pProto )
569
+//returns protocol in *pProto if pProto not NULL
570
+{
571
+  stBlinkenFrame * pFrame;
572
+  int height, width, channels, maxval, y, x, c, i;
573
+
574
+  if( length >= (int)sizeof( stBlinkenProtoBlpHdr ) &&
575
+      ((stBlinkenProtoBlpHdr *)pData)->magic == htonl( BlinkenProtoBlpMagic ) )
576
+  {
577
+    if( pProto != NULL ) //return protocol
578
+      *pProto = BlinkenProtoBlp;
579
+    height = ntohs( ((stBlinkenProtoBlpHdr *)pData)->height ); //get header data
580
+    width = ntohs( ((stBlinkenProtoBlpHdr *)pData)->width );
581
+    if( length < (int)sizeof( stBlinkenProtoBlpHdr ) + height * width ) //check length of packet
582
+      return NULL;
583
+    if( height < BlinkenHeightMin || height > BlinkenHeightMax || //check header data
584
+        width < BlinkenWidthMin || width > BlinkenWidthMax )
585
+      return NULL;
586
+    pFrame = BlinkenFrameNew( height, width, 1, 1, 0 ); //create frame according to header data
587
+    if( pFrame == NULL )
588
+      return NULL;
589
+    i = sizeof( stBlinkenProtoEblpHdr ); //put data into frame
590
+    for( y = 0; y < pFrame->height; y++ )
591
+      for( x = 0; x < pFrame->width; x++, i++ )
592
+        pFrame->pppData[y][x][0] = pData[i];
593
+    return pFrame;
594
+  }
595
+
596
+  if( length >= (int)sizeof( stBlinkenProtoEblpHdr ) &&
597
+      ((stBlinkenProtoEblpHdr *)pData)->magic == htonl( BlinkenProtoEblpMagic ) )
598
+  {
599
+    if( pProto != NULL ) //return protocol
600
+      *pProto = BlinkenProtoEblp;
601
+    height = ntohs( ((stBlinkenProtoEblpHdr *)pData)->height ); //get header data
602
+    width = ntohs( ((stBlinkenProtoEblpHdr *)pData)->width );
603
+    if( length < (int)sizeof( stBlinkenProtoEblpHdr ) + width * height ) //check length of packet
604
+      return NULL;
605
+    if( height < BlinkenHeightMin || height > BlinkenHeightMax || //check header data
606
+        width < BlinkenWidthMin || width > BlinkenWidthMax )
607
+      return NULL;
608
+    pFrame = BlinkenFrameNew( height, width, 1, 255, 0 ); //create frame according to header data
609
+    if( pFrame == NULL )
610
+      return NULL;
611
+    i = sizeof( stBlinkenProtoEblpHdr ); //put data into frame
612
+    for( y = 0; y < pFrame->height; y++ )
613
+      for( x = 0; x < pFrame->width; x++, i++ )
614
+        pFrame->pppData[y][x][0] = pData[i];
615
+    return pFrame;
616
+  }
617
+
618
+  if( length >= (int)sizeof( stBlinkenProtoMcufHdr ) &&
619
+      ((stBlinkenProtoMcufHdr *)pData)->magic == htonl( BlinkenProtoMcufMagic ) )
620
+  {
621
+    if( pProto != NULL ) //return protocol
622
+      *pProto = BlinkenProtoMcuf;
623
+    height = ntohs( ((stBlinkenProtoMcufHdr *)pData)->height ); //get header data
624
+    width = ntohs( ((stBlinkenProtoMcufHdr *)pData)->width );
625
+    channels = ntohs( ((stBlinkenProtoMcufHdr *)pData)->channels );
626
+    maxval = ntohs( ((stBlinkenProtoMcufHdr *)pData)->maxval );
627
+    if( length < (int)sizeof( stBlinkenProtoMcufHdr ) + height * width * channels ) //check length of packet
628
+      return NULL;
629
+    if( height < BlinkenHeightMin || height > BlinkenHeightMax || //check header data
630
+        width < BlinkenWidthMin || width > BlinkenWidthMax ||
631
+        channels < BlinkenChannelsMin || channels > BlinkenChannelsMax ||
632
+        maxval < BlinkenMaxvalMin || maxval > BlinkenMaxvalMax )
633
+      return NULL;
634
+    pFrame = BlinkenFrameNew( height, width, channels, maxval, 0 ); //create frame according to header data
635
+    if( pFrame == NULL )
636
+      return NULL;
637
+    i = sizeof( stBlinkenProtoMcufHdr ); //put data into frame
638
+    for( y = 0; y < pFrame->height; y++ )
639
+      for( x = 0; x < pFrame->width; x++ )
640
+        for( c = 0; c < pFrame->channels; c++, i++ )
641
+          pFrame->pppData[y][x][c] = pData[i];
642
+    return pFrame;
643
+  }
644
+
645
+  if( pProto != NULL ) //return protocol
646
+    *pProto = BlinkenProtoNone;
647
+  return NULL;
648
+}
649
+
... ...
@@ -1,5 +1,5 @@
1 1
 /* BlinkenLib
2
- * version 0.2 date 2005-01-27
2
+ * version 0.3 date 2005-02-16
3 3
  * Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>
4 4
  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5 5
  * a blinkenarea.org project
... ...
@@ -11,6 +11,8 @@
11 11
 
12 12
 typedef struct sBlinkenFrame stBlinkenFrame;
13 13
 
14
+typedef enum eBlinkenProto { BlinkenProtoNone, BlinkenProtoBlp, BlinkenProtoEblp, BlinkenProtoMcuf } etBlinkenProto;
15
+
14 16
 stBlinkenFrame * BlinkenFrameNew( int height, int width, int channels, int maxval, int duration );
15 17
 
16 18
 stBlinkenFrame * BlinkenFrameClone( stBlinkenFrame * pSrcFrame );
... ...
@@ -38,5 +40,11 @@ void BlinkenFrameScale( stBlinkenFrame * pFrame, int height, int width );
38 40
 
39 41
 char * BlinkenFrameToString( stBlinkenFrame * pFrame );
40 42
 
43
+int BlinkenFrameToNetwork( stBlinkenFrame * pFrame, etBlinkenProto proto, char * pData, int maxLength );
44
+//returns length or -1 on error
45
+
46
+stBlinkenFrame * BlinkenFrameFromNetwork( char * pData, int length, etBlinkenProto * pProto );
47
+//returns protocol in *pProto if pProto not NULL
48
+
41 49
 #endif //#ifndef INC_BlinkenFrame
42 50
 
... ...
@@ -1,5 +1,5 @@
1 1
 /* BlinkenLib
2
- * version 0.2 date 2005-01-27
2
+ * version 0.3 date 2005-02-16
3 3
  * Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>
4 4
  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5 5
  * a blinkenarea.org project
... ...
@@ -1,5 +1,5 @@
1 1
 /* BlinkenLib
2
- * version 0.2 date 2005-01-27
2
+ * version 0.3 date 2005-02-16
3 3
  * Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>
4 4
  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5 5
  * a blinkenarea.org project
... ...
@@ -9,6 +9,11 @@
9 9
 #include <stdio.h>
10 10
 #include <stdlib.h>
11 11
 #include <string.h>
12
+#include <unistd.h>
13
+#include <sys/time.h>
14
+#include <sys/types.h>
15
+#include <sys/socket.h>
16
+#include <sys/select.h>
12 17
 
13 18
 #include "BlinkenConstants.h"
14 19
 #include "BlinkenFrame.h"
... ...
@@ -70,6 +75,7 @@ stBlinkenMovie * BlinkenMovieNew( int height, int width, int channels, int maxva
70 75
 stBlinkenMovie * BlinkenMovieClone( stBlinkenMovie * pSrcMovie )
71 76
 {
72 77
   stBlinkenMovie * pMovie;
78
+  stBlinkenFrame * pFrame;
73 79
   int i;
74 80
 
75 81
   pMovie = BlinkenMovieNew( pSrcMovie->height, pSrcMovie->width, pSrcMovie->channels, pSrcMovie->maxval );
... ...
@@ -80,7 +86,11 @@ stBlinkenMovie * BlinkenMovieClone( stBlinkenMovie * pSrcMovie )
80 86
     BlinkenMovieAppendInfo( pMovie, pSrcMovie->pppInfos[i][0], pSrcMovie->pppInfos[i][1] );
81 87
 
82 88
   for( i = 0; i < pSrcMovie->frameCnt; i++ )
83
-    BlinkenMovieAppendFrame( pMovie, BlinkenFrameClone( pSrcMovie->ppFrames[i] ) );
89
+  {
90
+    pFrame = BlinkenFrameClone( pSrcMovie->ppFrames[i] );
91
+    if( BlinkenMovieAppendFrame( pMovie, pFrame ) != 0 )
92
+      BlinkenFrameFree( pFrame );
93
+  }
84 94
 
85 95
   return pMovie;
86 96
 }
... ...
@@ -338,17 +348,17 @@ void BlinkenMovieSetFrame( stBlinkenMovie * pMovie, int frameNo, stBlinkenFrame
338 348
   pMovie->ppFrames[frameNo] = pFrame;
339 349
 }
340 350
 
341
-void BlinkenMovieInsertFrame( stBlinkenMovie * pMovie, int frameNo, stBlinkenFrame * pFrame )
351
+int BlinkenMovieInsertFrame( stBlinkenMovie * pMovie, int frameNo, stBlinkenFrame * pFrame )
342 352
 {
343 353
   stBlinkenFrame * * ppNewFrames;
344 354
   int i;
345 355
 
346 356
   if( pMovie == NULL || frameNo < 0 || frameNo > pMovie->frameCnt )
347
-    return;
357
+    return -1;
348 358
 
349 359
   ppNewFrames = (stBlinkenFrame * *)malloc1D( pMovie->frameCnt + 1, sizeof( stBlinkenFrame * ) );
350 360
   if( ppNewFrames == NULL )
351
-    return;
361
+    return -1;
352 362
 
353 363
   for( i = 0; i < frameNo; i++ )
354 364
     ppNewFrames[i] = pMovie->ppFrames[i];
... ...
@@ -362,14 +372,15 @@ void BlinkenMovieInsertFrame( stBlinkenMovie * pMovie, int frameNo, stBlinkenFra
362 372
   free( pMovie->ppFrames );
363 373
   pMovie->ppFrames = ppNewFrames;
364 374
   pMovie->frameCnt++;
375
+  return 0;
365 376
 }
366 377
 
367
-void BlinkenMovieAppendFrame( stBlinkenMovie * pMovie, stBlinkenFrame * pFrame )
378
+int BlinkenMovieAppendFrame( stBlinkenMovie * pMovie, stBlinkenFrame * pFrame )
368 379
 {
369 380
   if( pMovie == NULL )
370
-    return;
381
+    return -1;
371 382
 
372
-  BlinkenMovieInsertFrame( pMovie, pMovie->frameCnt, pFrame );
383
+  return BlinkenMovieInsertFrame( pMovie, pMovie->frameCnt, pFrame );
373 384
 }
374 385
 
375 386
 void BlinkenMovieDeleteFrame( stBlinkenMovie * pMovie, int frameNo )
... ...
@@ -577,7 +588,11 @@ stBlinkenMovie * BlinkenMovieLoadBlm( char * pFilename )
577 588
       if( pFrame != NULL )
578 589
       {
579 590
         BlinkenFrameClear( pFrame );
580
-        BlinkenMovieAppendFrame( pMovie, pFrame );
591
+        if( BlinkenMovieAppendFrame( pMovie, pFrame ) != 0 )
592
+        {
593
+          BlinkenFrameFree( pFrame );
594
+          pFrame = NULL;
595
+        }
581 596
         y = 0;
582 597
       }
583 598
     }
... ...
@@ -660,7 +675,11 @@ stBlinkenMovie * BlinkenMovieLoadBmm( char * pFilename )
660 675
       if( pFrame != NULL )
661 676
       {
662 677
         BlinkenFrameClear( pFrame );
663
-        BlinkenMovieAppendFrame( pMovie, pFrame );
678
+        if( BlinkenMovieAppendFrame( pMovie, pFrame ) != 0 )
679
+        {
680
+          BlinkenFrameFree( pFrame );
681
+          pFrame = NULL;
682
+        }
664 683
         y = 0;
665 684
       }
666 685
     }
... ...
@@ -830,7 +849,11 @@ stBlinkenMovie * BlinkenMovieLoadBml( char * pFilename )
830 849
         if( pFrame != NULL )
831 850
         {
832 851
           BlinkenFrameClear( pFrame );
833
-          BlinkenMovieAppendFrame( pMovie, pFrame );
852
+          if( BlinkenMovieAppendFrame( pMovie, pFrame ) != 0 )
853
+          {
854
+            BlinkenFrameFree( pFrame );
855
+            pFrame = NULL;
856
+          }
834 857
           y = 0;
835 858
         }
836 859
       }
... ...
@@ -875,237 +898,6 @@ stBlinkenMovie * BlinkenMovieLoadBml( char * pFilename )
875 898
   return pMovie;
876 899
 }
877 900
 
878
-/*
879
-
880
-public boolean loadBml( String filename )
881
-{
882
-  Pattern blmTag, blmHeight, blmWidth, blmChannels, blmBits;
883
-  Pattern infoTitle, infoDescription, infoGeneric, infoCreator, infoAuthor, infoEmail, infoUrl;
884
-  Pattern frameTag, frameDuration, rowTag, tag;
885
-  BufferedReader file;
886
-  String line, data, row;
887
-  boolean blmTagFound;
888
-  Matcher matcher, submatcher;
889
-  int height, width, channels, bits, maxval, chrs, duration, y, x, c, len, i, val;
890
-  BlinkenFrame frame;
891
-
892
-  //initialize needed regexp patterns
893
-  blmTag = Pattern.compile( "^[^<]*<blm([^>]*)>" );
894
-  blmHeight = Pattern.compile( "height=\"?([0-9]*)\"?" );
895
-  blmWidth = Pattern.compile( "width=\"?([0-9]*)\"?" );
896
-  blmChannels = Pattern.compile( "channels=\"?([0-9]*)\"?" );
897
-  blmBits = Pattern.compile( "bits=\"?([0-9]*)\"?" );
898
-  infoTitle = Pattern.compile( "^[^<]*<title>([^<]*)</title>" );
899
-  infoDescription = Pattern.compile( "[^<]*<description>([^<]*)</description>" );
900
-  infoGeneric = Pattern.compile( "^([A-Za-z0-9]+)(?: *= *|: *)(.*)" );
901
-  infoCreator = Pattern.compile( "^[^<]*<creator>([^<]*)</creator>" );
902
-  infoAuthor = Pattern.compile( "^[^<]*<author>([^<]*)</author>" );
903
-  infoEmail = Pattern.compile( "^[^<]*<email>([^<]*)</email>" );
904
-  infoUrl = Pattern.compile( "^[^<]*<url>([^<]*)</url>" );
905
-  frameTag = Pattern.compile( "^[^<]*<frame([^>]*)>" );
906
-  frameDuration = Pattern.compile( "duration=\"?([0-9]*)\"?" );
907
-  rowTag = Pattern.compile( "^[^<]*<row>([0-9A-Fa-f]*)</row>" );
908
-  tag = Pattern.compile( "^[^<]*<[^>]*>" );
909
-
910
-  //delete all frames
911
-  deleteInfos( );
912
-  deleteFrames( );
913
-  resize( 0, 0, 0, 0 );
914
-
915
-  //try to read file
916
-  try
917
-  {
918
-    //open file
919
-    file = new BufferedReader( new FileReader( filename ) );
920
-
921
-    //create unused dummy frame for beginning
922
-    frame = new BlinkenFrame( 0, 0, 0, 0, 0 );
923
-    y = 0;
924
-    chrs = 1;
925
-
926
-    //read file
927
-    data = "";
928
-    blmTagFound = false;
929
-    while( (line = file.readLine( )) != null )
930
-    {
931
-      data += " " + line; //add new line to data
932
-
933
-      //match tags
934
-      while( true )
935
-      {
936
-
937
-        //no blm tag yet
938
-        if( ! blmTagFound )
939
-        {
940
-
941
-          //blm tag
942
-          if( (matcher = blmTag.matcher( data )).find( ) )
943
-          {
944
-            //remove matched part
945
-            data = data.substring( matcher.end( ) );
946
-            //get attributes
947
-            width = 0;
948
-            height = 0;
949
-            channels = 0;
950
-            bits = 0;
951
-            maxval = 0;
952
-            if( (submatcher = blmHeight.matcher( matcher.group( 1 ) )).find( ) ) //height
953
-              height = Integer.parseInt( submatcher.group( 1 ) );
954
-            if( (submatcher = blmWidth.matcher( matcher.group( 1 ) )).find( ) ) //width
955
-              width = Integer.parseInt( submatcher.group( 1 ) );
956
-            if( (submatcher = blmChannels.matcher( matcher.group( 1 ) )).find( ) ) //channels
957
-              channels = Integer.parseInt( submatcher.group( 1 ) );
958
-            if( (submatcher = blmBits.matcher( matcher.group( 1 ) )).find( ) ) //bits
959
-              maxval = (1 << (bits = Integer.parseInt( submatcher.group( 1 ) ))) - 1;
960
-            //remember that blm tag was found
961
-            blmTagFound = true;
962
-            //set movie size
963
-            resize( height, width, channels, maxval );
964
-            //get number of characters per channel
965
-            chrs = (bits + 3) >> 2;
966
-          }
967
-
968
-          //unknown tag
969
-          else if( (matcher = tag.matcher( data )).find( ) )
970
-            //remove matched part
971
-            data = data.substring( matcher.end( ) );
972
-
973
-          //nothing matches
974
-          else
975
-            //end loop
976
-            break;
977
-
978
-        } //if( ! blmTagFound )
979
-
980
-        //blm tag was already found
981
-        else
982
-        {
983
-
984
-          //title tag
985
-          if( (matcher = infoTitle.matcher( data )).find( ) )
986
-          {
987
-            //remove matched part
988
-            data = data.substring( matcher.end( ) );
989
-            //add info to movie
990
-            appendInfo( "title", matcher.group( 1 ) );
991
-          }
992
-
993
-          //description tag
994
-          else if( (matcher = infoDescription.matcher( data )).find( ) )
995
-          {
996
-            //remove matched part
997
-            data = data.substring( matcher.end( ) );
998
-            //check if generic info
999
-            if( (submatcher = infoGeneric.matcher( matcher.group( 1 ) )).find( ) )
1000
-              //add info to movie
1001
-              appendInfo( submatcher.group( 1 ), submatcher.group( 2 ) );
1002
-            else
1003
-              //add info to movie
1004
-              appendInfo( "description", matcher.group( 1 ) );
1005
-          }
1006
-
1007
-          //creator tag
1008
-          else if( (matcher = infoCreator.matcher( data )).find( ) )
1009
-          {
1010
-            //remove matched part
1011
-            data = data.substring( matcher.end( ) );
1012
-            //add info to movie
1013
-            appendInfo( "creator", matcher.group( 1 ) );
1014
-          }
1015
-
1016
-          //author tag
1017
-          else if( (matcher = infoAuthor.matcher( data )).find( ) )
1018
-          {
1019
-            //remove matched part
1020
-            data = data.substring( matcher.end( ) );
1021
-            //add info to movie
1022
-            appendInfo( "author", matcher.group( 1 ) );
1023
-          }
1024
-
1025
-          //email tag
1026
-          else if( (matcher = infoEmail.matcher( data )).find( ) )
1027
-          {
1028
-            //remove matched part
1029
-            data = data.substring( matcher.end( ) );
1030
-            //add info to movie
1031
-            appendInfo( "email", matcher.group( 1 ) );
1032
-          }
1033
-
1034
-          //url tag
1035
-          else if( (matcher = infoUrl.matcher( data )).find( ) )
1036
-          {
1037
-            //remove matched part
1038
-            data = data.substring( matcher.end( ) );
1039
-            //add info to movie
1040
-            appendInfo( "url", matcher.group( 1 ) );
1041
-          }
1042
-
1043
-          //frame tag
1044
-          else if( (matcher = frameTag.matcher( data )).find( ) )
1045
-          {
1046
-            //remove matched part
1047
-            data = data.substring( matcher.end( ) );
1048
-            //get attributes
1049
-            duration = 0;
1050
-            if( (submatcher = frameDuration.matcher( matcher.group( 1 ) )).find( ) ) //duration
1051
-              duration = Integer.parseInt( submatcher.group( 1 ) );
1052
-            //create new frame and append it to movie
1053
-            frame = new BlinkenFrame( this.height, this.width, this.channels, this.maxval, duration );
1054
-            frame.clear( );
1055
-            appendFrame( frame );
1056
-            y = 0;
1057
-          }
1058
-
1059
-          //row tag
1060
-          else if( (matcher = rowTag.matcher( data )).find( ) )
1061
-          {
1062
-            //remove matched part
1063
-            data = data.substring( matcher.end( ) );
1064
-            //parse row
1065
-            row = matcher.group( 1 );
1066
-            len = row.length( );
1067
-            i = 0;
1068
-            for( x = 0; x < this.width && i + chrs <= len; x++ )
1069
-            {
1070
-              for( c = 0; c < this.channels && i + chrs <= len; c++, i += chrs )
1071
-              {
1072
-                val = Integer.parseInt( row.substring( i, i + chrs ), 0x10 );
1073
-                frame.setPixel( y, x, c, (byte)val ); //set pixel
1074
-              }
1075
-            }
1076
-            y++; //next row
1077
-          }
1078
-
1079
-          //unknown tag
1080
-          else if( (matcher = tag.matcher( data )).find( ) )
1081
-            //remove matched part
1082
-            data = data.substring( matcher.end( ) );
1083
-
1084
-          //nothing matches
1085
-          else
1086
-            //end loop
1087
-            break;
1088
-
1089
-        } //if( ! blmTagFound ) ... else
1090
-
1091
-      } //while( true )
1092
-
1093
-    } //while( (line = ...
1094
-
1095
-    //close file
1096
-    file.close( );
1097
-
1098
-    //success
1099
-    return true;
1100
-  }
1101
-  catch( IOException e ) { }
1102
-
1103
-  //some error
1104
-  return false;
1105
-}
1106
-
1107
-*/
1108
-
1109 901
 stBlinkenMovie * BlinkenMovieLoadBbm( char * pFilename )
1110 902
 {
1111 903
   FILE * pFile;
... ...
@@ -1252,7 +1044,11 @@ stBlinkenMovie * BlinkenMovieLoadBbm( char * pFilename )
1252 1044
       for( x = 0; x < headerWidth; x++ )
1253 1045
         for( c = 0; c < headerChannels; c++, i++ )
1254 1046
           BlinkenFrameSetPixel( pFrame, y, x, c, pFrameData[i] );
1255
-    BlinkenMovieAppendFrame( pMovie, pFrame );
1047
+    if( BlinkenMovieAppendFrame( pMovie, pFrame ) != 0 )
1048
+    {
1049
+      BlinkenFrameFree( pFrame );
1050
+      pFrame = NULL;
1051
+    }
1256 1052
 
1257 1053
   } //for( ; ; )
1258 1054
 
... ...
@@ -1612,3 +1408,118 @@ int BlinkenMovieSave( stBlinkenMovie * pMovie, char * pFilename )
1612 1408
   return -1;
1613 1409
 }
1614 1410
 
1411
+void BlinkenMovieSend( stBlinkenMovie * pMovie, int udpSocket, etBlinkenProto proto )
1412
+//udp socket must be "connected"
1413
+{
1414
+  int i, len;
1415
+  char buffer[65536]; //64kB is more tham maximum UDP size
1416
+
1417
+  for( i = 0; i < pMovie->frameCnt; i++ )
1418
+  {
1419
+    len = BlinkenFrameToNetwork( pMovie->ppFrames[i], proto, buffer, sizeof( buffer ) );
1420
+    if( len > 0 )
1421
+      send( udpSocket, buffer, len, 0 );
1422
+    usleep( BlinkenFrameGetDuration( pMovie->ppFrames[i] ) * 1000 );
1423
+  }
1424
+}
1425
+
1426
+stBlinkenMovie * BlinkenMovieReceive( int udpSocket, int timeout, etBlinkenProto * pProto )
1427
+//udp socket must be "bound" and should be "connected"
1428
+//after timeout ms of no reception, the movie is considered to be complete
1429
+//returns protocol in *pProto if pProto not NULL
1430
+{
1431
+  stBlinkenMovie * pMovie;
1432
+  stBlinkenFrame * pLastFrame, * pFrame;
1433
+  etBlinkenProto proto, p;
1434
+  fd_set readFds;
1435
+  struct timeval timeo, lastTime, curTime;
1436
+  char buffer[65536]; //64kB is more tham maximum UDP size
1437
+  int len;
1438
+
1439
+  //correct timeout
1440
+  if( timeout < 0 )
1441
+    timeout = 0;
1442
+
1443
+  //wait for frames
1444
+  pMovie = NULL;
1445
+  proto = BlinkenProtoNone;
1446
+  pLastFrame = NULL;
1447
+  for( ; ; )
1448
+  {
1449
+    //wait for next frame
1450
+    FD_ZERO( &readFds );
1451
+    FD_SET( udpSocket, &readFds );
1452
+    timeo.tv_sec = timeout / 1000;
1453
+    timeo.tv_usec = (timeout % 1000) * 1000;
1454
+    if( select( udpSocket + 1, &readFds, NULL, NULL, &timeo ) <= 0 ) //timeout or error
1455
+      break;
1456
+
1457
+    //fetch data
1458
+    len = recv( udpSocket, buffer, sizeof( buffer ), 0 );
1459
+    if( len <= 0 )
1460
+      break;
1461
+
1462
+    //get frame from data
1463
+    pFrame = BlinkenFrameFromNetwork( buffer, len, &p );
1464
+    if( pFrame != NULL )
1465
+    {
1466
+      //valid protocol
1467
+      if( p != BlinkenProtoNone )
1468
+      {
1469
+        //first frame
1470
+        if( proto == BlinkenProtoNone )
1471
+          proto = p; //use this protocol
1472
+        //protocol matches
1473
+        if( p == proto )
1474
+        {
1475
+          //no movie yet
1476
+          if( pMovie == NULL )
1477
+          {
1478
+            //allocate a new movie
1479
+            pMovie = BlinkenMovieNew( BlinkenFrameGetHeight( pFrame ),
1480
+                                      BlinkenFrameGetWidth( pFrame ),
1481
+                                      BlinkenFrameGetChannels( pFrame ),
1482
+                                      BlinkenFrameGetMaxval( pFrame ) );
1483
+            if( pMovie == NULL )
1484
+            {
1485
+              BlinkenFrameFree( pFrame );
1486
+              break;
1487
+            }
1488
+          }
1489
+
1490
+          //append frame to movie
1491
+          if( BlinkenMovieAppendFrame( pMovie, pFrame ) == 0 )
1492
+          {
1493
+            //get current time
1494
+            gettimeofday( &curTime, NULL );
1495
+            //set duration of last frame
1496
+            if( pLastFrame != NULL )
1497
+              BlinkenFrameSetDuration( pLastFrame, (curTime.tv_sec - lastTime.tv_sec) * 1000
1498
+                                                 + (curTime.tv_usec - lastTime.tv_usec) / 1000 );
1499
+            //remember this frame as last frame appended to movie
1500
+            pLastFrame = pFrame;
1501
+            pFrame = NULL; //do not free this frame (it was appended)
1502
+            lastTime = curTime; //remember time of this frame
1503
+          }
1504
+        } //if( p == proto )
1505
+      } //if( p != BlinkenProtoNone )
1506
+      //free frame if it was not appended
1507
+      if( pFrame != NULL )
1508
+        BlinkenFrameFree( pFrame );
1509
+    } //if( pFrame != NULL )
1510
+  } //for( ; ; )
1511
+
1512
+  //get current time
1513
+  gettimeofday( &curTime, NULL );
1514
+  //set duration of last frame
1515
+  if( pLastFrame != NULL )
1516
+    BlinkenFrameSetDuration( pLastFrame, (curTime.tv_sec - lastTime.tv_sec) * 1000
1517
+                                       + (curTime.tv_usec - lastTime.tv_usec) / 1000 );
1518
+
1519
+  //return protocol
1520
+  if( pProto != NULL )
1521
+    *pProto = proto;
1522
+  //return movie
1523
+  return pMovie;
1524
+}
1525
+
... ...
@@ -1,5 +1,5 @@
1 1
 /* BlinkenLib
2
- * version 0.2 date 2005-01-27
2
+ * version 0.3 date 2005-02-16
3 3
  * Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>
4 4
  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5 5
  * a blinkenarea.org project
... ...
@@ -37,8 +37,8 @@ void BlinkenMovieDeleteInfos( stBlinkenMovie * pMovie );
37 37
 int BlinkenMovieGetFrameCnt( stBlinkenMovie * pMovie );
38 38
 stBlinkenFrame * BlinkenMovieGetFrame( stBlinkenMovie * pMovie, int frameNo );
39 39
 void BlinkenMovieSetFrame( stBlinkenMovie * pMovie, int frameNo, stBlinkenFrame * pFrame );
40
-void BlinkenMovieInsertFrame( stBlinkenMovie * pMovie, int frameNo, stBlinkenFrame * pFrame );
41
-void BlinkenMovieAppendFrame( stBlinkenMovie * pMovie, stBlinkenFrame * pFrame );
40
+int BlinkenMovieInsertFrame( stBlinkenMovie * pMovie, int frameNo, stBlinkenFrame * pFrame );
41
+int BlinkenMovieAppendFrame( stBlinkenMovie * pMovie, stBlinkenFrame * pFrame );
42 42
 void BlinkenMovieDeleteFrame( stBlinkenMovie * pMovie, int frameNo );
43 43
 void BlinkenMovieDeleteFrames( stBlinkenMovie * pMovie );
44 44
 
... ...
@@ -59,5 +59,13 @@ int BlinkenMovieSaveBml( stBlinkenMovie * pMovie, char * pFilename );
59 59
 int BlinkenMovieSaveBbm( stBlinkenMovie * pMovie, char * pFilename );
60 60
 int BlinkenMovieSave( stBlinkenMovie * pMovie, char * pFilename );
61 61
 
62
+void BlinkenMovieSend( stBlinkenMovie * pMovie, int udpSocket, etBlinkenProto proto );
63
+//udp socket must be "connected"
64
+
65
+stBlinkenMovie * BlinkenMovieReceive( int udpSocket, int timeout, etBlinkenProto * pProto );
66
+//udp socket must be "bound" and should be "connected"
67
+//after timeout ms of no reception, the movie is considered to be complete
68
+//returns protocol in *pProto if pProto not NULL
69
+
62 70
 #endif //#ifndef INC_BlinkenMovie
63 71
 
... ...
@@ -1,3 +1,9 @@
1
+0.3 2005-02-16
2
+--------------
3
+fixed memory leak when adding frames to movies fails
4
+added support for converting frames into network packtes and back
5
+added small tools to send and receive streams
6
+
1 7
 0.2 2005-01-27
2 8
 --------------
3 9
 extended BlinkenConv so that resizing and scaling is supported
... ...
@@ -1,5 +1,5 @@
1 1
 # BlinkenLib
2
-# version 0.2 date 2005-01-27
2
+# version 0.3 date 2005-02-16
3 3
 # Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>
4 4
 # Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5 5
 # a blinkenarea.org project
... ...
@@ -13,7 +13,7 @@ ARFLAGS=cr
13 13
 
14 14
 .phony: all clean
15 15
 
16
-all: BlinkenLib.a BlinkenConv
16
+all: BlinkenLib.a BlinkenConv BlinkenSend BlinkenRecv
17 17
 
18 18
 BlinkenFrame.o: BlinkenFrame.c BlinkenFrame.h Tools.h
19 19
 	$(CC) $(CFLAGS) -c -o BlinkenFrame.o BlinkenFrame.c
... ...
@@ -27,8 +27,14 @@ Tools.o: Tools.c Tools.h
27 27
 BlinkenLib.a: BlinkenFrame.o BlinkenMovie.o Tools.o
28 28
 	$(AR) $(ARFLAGS) BlinkenLib.a BlinkenFrame.o BlinkenMovie.o Tools.o
29 29
 
30
-BlinkenConv: BlinkenConv.c BlinkenMovie.h BlinkenLib.a
30
+BlinkenConv: BlinkenConv.c BlinkenLib.h BlinkenLib.a
31 31
 	$(CC) $(LFLAGS) -o BlinkenConv BlinkenConv.c BlinkenLib.a
32 32
 
33
+BlinkenSend: BlinkenSend.c BlinkenLib.h BlinkenLib.a
34
+	$(CC) $(LFLAGS) -o BlinkenSend BlinkenSend.c BlinkenLib.a
35
+
36
+BlinkenRecv: BlinkenRecv.c BlinkenLib.h BlinkenLib.a
37
+	$(CC) $(LFLAGS) -o BlinkenRecv BlinkenRecv.c BlinkenLib.a
38
+
33 39
 clean:
34
-	rm -f *.o BlinkenLib.a BlinkenConv
40
+	rm -f *.o BlinkenLib.a BlinkenConv BlinkenSend BlinkenRecv
... ...
@@ -1,5 +1,5 @@
1 1
 /* BlinkenLib
2
- * version 0.2 date 2005-01-27
2
+ * version 0.3 date 2005-02-16
3 3
  * Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>
4 4
  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5 5
  * a blinkenarea.org project
... ...
@@ -1,5 +1,5 @@
1 1
 /* BlinkenLib
2
- * version 0.2 date 2005-01-27
2
+ * version 0.3 date 2005-02-16
3 3
  * Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>
4 4
  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
5 5
  * a blinkenarea.org project
6 6