source: trunk/MagicSoft/Cosy/videodev/PngReader.cc@ 1803

Last change on this file since 1803 was 1803, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 3.1 KB
Line 
1#include "PngReader.h"
2#include "PixClient.h"
3
4#include <iostream.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <errno.h>
8#include <fcntl.h>
9#include <string.h>
10#include <signal.h>
11#include <endian.h>
12#include <sys/ioctl.h>
13#include <sys/mman.h>
14#include <sys/ipc.h>
15#include <sys/shm.h>
16
17#include "png.h"
18
19#include "videodev.h"
20
21#include <sys/time.h>
22#include <sys/resource.h>
23
24ClassImp(PngReader);
25
26char *PngReader::GetImg(unsigned int frame)
27{
28 char txt1[100];
29
30 frame %= 333;
31
32 sprintf(txt1, "file%04d.png", frame);
33
34 // cout << "Reading PNG '" << txt1 << "'" << endl;
35
36 char txt2[200];
37 sprintf(txt2, "/home/caos/Dani/Cosy/smallleds/%s", txt1);
38 //sprintf(txt2, "/home/caos/cosy.lp/pix/stable_1min/%s", txt1);
39
40 //
41 // open file
42 //
43 FILE *fd = fopen(txt2, "r");
44 if (!fd)
45 {
46 cout << "Cannot open file." << endl;
47 return 0;
48 }
49 //
50 // allocate memory
51 //
52 png_structp fPng = png_create_read_struct(PNG_LIBPNG_VER_STRING,
53 NULL, NULL, NULL);
54
55 if (!fPng)
56 {
57 cout << "Warning: Unable to create PNG structure" << endl;
58 fclose(fd);
59 return 0;
60 }
61
62
63 png_infop fInfo = png_create_info_struct(fPng);
64
65 if (!fInfo)
66 {
67 cout << "Warning: Unable to create PNG info structure" << endl;
68 png_destroy_read_struct (&fPng, NULL, NULL);
69 fclose(fd);
70 return 0;
71 }
72
73 //
74 // set jump-back point in case of errors
75 //
76 if (setjmp(fPng->jmpbuf))
77 {
78 cout << "longjmp Warning: PNG encounterd an error!" << endl;
79 png_destroy_read_struct (&fPng, &fInfo, NULL);
80 fclose(fd);
81 return 0;
82 }
83
84 //
85 // connect file to PNG-Structure
86 //
87 png_init_io(fPng, fd);
88
89 // png_set_compression_level (fPng, Z_BEST_COMPRESSION);
90
91 png_read_png(fPng, fInfo, 0, NULL);
92
93 //
94 // Write bitmap data
95 //
96 static int first = 0;
97 for (unsigned int y=0; y<576; y++)
98 for (unsigned int x=0; x<768; x++)
99 *(fImg+(y*768)+x) = fInfo->row_pointers[y][x];
100
101 //
102 // free memory
103 //
104 png_destroy_read_struct (&fPng, &fInfo, NULL);
105
106 fclose(fd);
107
108 return fImg;
109}
110
111PngReader::PngReader(PixClient &client) : fClient(client)
112{
113 cout << "Starting thread..." << flush;
114 pthread_create(&fThread, NULL, MapThread, this);
115 cout << "done." << endl;
116
117}
118
119PngReader::~PngReader()
120{
121 cout << "Stopping thread..." << flush;
122 pthread_cancel(fThread);
123 cout << "done." << endl;
124}
125
126void *PngReader::MapThread(void *arg)
127{
128 PngReader *cam = (PngReader*)arg;
129
130 // setpriority(PRIO_PROCESS, 0, 10);
131 pthread_detach(pthread_self());
132
133 cam->Thread();
134
135 return 0;
136}
137
138void PngReader::Thread()
139{
140 fIsRunning = 1;
141 usleep(3000000);
142 int i=0;
143
144 gettimeofday(&fTime, NULL);
145
146 while (!fStop)
147 {
148 // if (i<536)
149 // {
150 GetImg(i);
151 fClient.ProcessFrame(i++, (byte*)fImg, &fTime);
152 // usleep(10000);
153 // }
154 // else exit();
155 fTime.tv_sec += 1;
156 usleep(1000);
157 }
158
159
160 fIsRunning = 0;
161}
162
Note: See TracBrowser for help on using the repository browser.