| 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 |  | 
|---|
| 24 | ClassImp(PngReader); | 
|---|
| 25 |  | 
|---|
| 26 | char *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 | for (unsigned int y=0; y<576; y++) | 
|---|
| 97 | for (unsigned int x=0; x<768; x++) | 
|---|
| 98 | *(fImg+(y*768)+x) = fInfo->row_pointers[y][x]; | 
|---|
| 99 |  | 
|---|
| 100 | // | 
|---|
| 101 | // free memory | 
|---|
| 102 | // | 
|---|
| 103 | png_destroy_read_struct (&fPng, &fInfo, NULL); | 
|---|
| 104 |  | 
|---|
| 105 | fclose(fd); | 
|---|
| 106 |  | 
|---|
| 107 | return fImg; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | PngReader::PngReader(PixClient &client) : fClient(client) | 
|---|
| 111 | { | 
|---|
| 112 | cout << "Starting thread..." << flush; | 
|---|
| 113 | pthread_create(&fThread, NULL, MapThread, this); | 
|---|
| 114 | cout << "done." << endl; | 
|---|
| 115 |  | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | PngReader::~PngReader() | 
|---|
| 119 | { | 
|---|
| 120 | cout << "Stopping thread..." << flush; | 
|---|
| 121 | pthread_cancel(fThread); | 
|---|
| 122 | cout << "done." << endl; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | void *PngReader::MapThread(void *arg) | 
|---|
| 126 | { | 
|---|
| 127 | PngReader *cam = (PngReader*)arg; | 
|---|
| 128 |  | 
|---|
| 129 | // setpriority(PRIO_PROCESS, 0, 10); | 
|---|
| 130 | pthread_detach(pthread_self()); | 
|---|
| 131 |  | 
|---|
| 132 | cam->Thread(); | 
|---|
| 133 |  | 
|---|
| 134 | return 0; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | void PngReader::Thread() | 
|---|
| 138 | { | 
|---|
| 139 | fIsRunning = 1; | 
|---|
| 140 | usleep(3000000); | 
|---|
| 141 | int i=0; | 
|---|
| 142 |  | 
|---|
| 143 | gettimeofday(&fTime, NULL); | 
|---|
| 144 |  | 
|---|
| 145 | while (!fStop) | 
|---|
| 146 | { | 
|---|
| 147 | // if (i<536) | 
|---|
| 148 | // { | 
|---|
| 149 | GetImg(i); | 
|---|
| 150 | fClient.ProcessFrame(i++, (byte*)fImg, &fTime); | 
|---|
| 151 | // usleep(10000); | 
|---|
| 152 | // } | 
|---|
| 153 | // else exit(); | 
|---|
| 154 | fTime.tv_sec += 1; | 
|---|
| 155 | usleep(1000); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 |  | 
|---|
| 159 | fIsRunning = 0; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|