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